Memory Mapped PC I/O Command Examples

Example program PGM1 using OPEN, WRITE and CLOSE to create and write to a file on the PC:

10 CLEAR 500
20 REM SET I/O AREA TO ENHANCED INSTRUCTION WORK AREA
30 POKE &HFFEF,&H00
40 REM DEFINE STRING & FILENAME FOR "OPEN" FUNCTION
50 A$="TEST.BAS"+CHR$(0): REM STRING MUST BE ZERO TERMINATED
60 REM DEFINE & SAVE ATTRIBUTES TO USE IN OPEN
70 REM CREATE FILE WITH READ & WRITE PERMISSION
80 POKE &HFFE4,&H01: POKE &HFFE5,&H80
90 REM SAVE STRING POINTER, MSB FIRST, LSB SECOND
100 POKE &HFFE2,&H02: POKE &HFFE3,&H41
110 A=VARPTR(A$)+2: POKE &HFFE0,PEEK(A): POKE &HFFE1,PEEK(A+1)
120 REM EXECUTE "OPEN" FUNCTION, IF SUCCESSFUL &HFFE4-&HFFE5 WILL CONTAIN FILE'S CHANNEL NUMBER
130 POKE &HFFEE,&H10: IF PEEK(&HFFEE)<>0 THEN GOTO 250
140 REM DEFINE BUFFER AND LOAD WITH DATA
150 A$="THIS FILE WAS WRITTEN FROM THE COCO EMULATOR"+CHR$(10)+CHR$(13)
160 REM SAVE BUFFER LENGTH
170 POKE &HFFE2,&H00: POKE &HFFE3,LEN(A$)
180 REM SAVE STRING POINTER, MSB FIRST, LSB SECOND
190 A=VARPTR(A$)+2: POKE &HFFE0,PEEK(A): POKE &HFFE1,PEEK(A+1)
200 REM EXECUTE "WRITE" FUNCTION
210 POKE &HFFEE,&H13: IF PEEK(&HFFEE)<>0 THEN GOTO 250
220 REM EXECUTE "CLOSE" FUNCTION
230 POKE &HFFEE,&H11: IF PEEK(&HFFEE)<>0 THEN GOTO 250
240 END
250 REM ERROR
260 PRINT "AN ERROR OCCURED"
270 END

Example program PGM2 using FINDFIRST and FINDNEXT to read a PC directory:

10 CLEAR 500
20 REM SET I/O AREA TO ENHANCED INSTRUCTION WORK AREA
30 POKE &HFFEF,&H00
40 REM DEFINE STRING & WILDCARD FOR "FIND FIRST" FUNCTION
50 A$="*.*"+STRING$(43,0): REM STRING MUST BE 43 CHAR LONG
60 REM DEFINE & SAVE ATTRIBUTES TO USE IN SEARCH
70 POKE &HFFE4,&H00: POKE &HFFE5,&H00: REM FIND ALL FILES
80 REM SAVE STRING LENGTH (MUST BE >=43, EXTRA UNUSED)
90 POKE &HFFE2,&H00: POKE &HFFE3,43
100 REM SAVE STRING POINTER, MSB FIRST, LSB SECOND
110 A=VARPTR(A$)+2: POKE &HFFE0,PEEK(A): POKE &HFFE1,PEEK(A+1)
120 REM EXECUTE "FIND FIRST" FUNCTION
130 POKE &HFFEE,&H16: IF PEEK(&HFFEE)<>0 THEN GOTO 210
140 REM PRINT FILENAME FOUND
150 A=INSTR(31,A$,CHR$(0)): IF A>31 THEN PRINT MID$(A$,31,A-31)
160 REM RESAVE BUFFER ADDRESS BECAUSE BASIC MOVES STRINGS, MSB FIRST, LSB SECOND
170 A=VARPTR(A$)+2: POKE &HFFE0,PEEK(A): POKE &HFFE1,PEEK(A+1)
180 REM EXECUTE "FIND NEXT" FUNCTION
190 POKE &HFFEE,&H17: IF PEEK(&HFFEE)<>0 THEN GOTO 210
200 GOTO 140
210 PRINT "END OF LIST ..."
220 END

Example program PGM3 that reads and displays the value of the PC's joystick port:

10 POKE &HFFEF,&H03: REM SET I/O AREA TO PC PORT ADDRESSES
20 REM SET &HFFE0 TO PC JOYSTICK PORT ($0201)
30 POKE &HFFE0,&H02: POKE &HFFE0,&H01
40 POKE &HFFEF,&H02: REM SET I/O AREA TO PC PORTS
50 PRINT PEEK(&HFFE0): REM DISPLAY VALUE READ FROM PC GAME PORT

Example program PGM4 that uses the PC mouse to draw on a 256 x 192 graphics screen:

10 CLEAR 500
20 REM SET DISPLAY SCREEN TO HI-RES 256 X 192
30 PMODE 4,1: PCLS: SCREEN 1,1
40 REM SET I/O AREA TO PC MOUSE AREA
50 POKE &HFFEF,4: REM MOUSE
60 REM SET THE MOUSE COORDINATE RANGE TO 0-255 X 0-191
70 REM SET MINIMUM X COORDINATE
80 POKE &HFFE3,0: POKE &HFFE3,0
90 REM SET MINIMUM Y COORDINATE
100 POKE &HFFE4,0: POKE &HFFE4,0
110 REM SET MAXIMUM X COORDINATE
120 POKE &HFFE5,0: POKE &HFFE5,255
130 REM SET MAXIMUM Y COORDINATE
140 POKE &HFFE6,0: POKE &HFFE6,191
150 REM SET INITIAL X COORDINATE
160 POKE &HFFE1,0: POKE &HFFE1,0
170 REM SET INITIAL Y COORDINATE
180 POKE &HFFE2,0: POKE &HFFE2,0
190 REM SET MOUSE X & Y SENSITIVITY TO 12
200 POKE &HFFE7,204
210 A=PEEK(&HFFEF): REM INSURE MSB OF 16BIT VALUES
220 C=1: COLOR 1,0: REM SET FORGROUND/BACKGROUND COLORS
230 REM SCREEN DRAWING LOOP
240 REM READ MOUSE & LOAD B WITH BUTTONS, THEN CLEAR PRESSED BUTTONS
250 B=PEEK(&HFFE0): POKE &HFFE0,0
260 REM IF RIGHT BUTTON PUSHED THEN CLEAR SCREEN
270 IF (B AND 32) THEN PCLS
280 REM GET NEW X COORDINATE
290 X=PEEK(&HFFE1)*256+PEEK(&HFFE1)
300 REM GET NEW Y COORDINATE
310 Y=PEEK(&HFFE2)*256+PEEK(&HFFE2)
320 REM IF LEFT BUTTON PUSHED THEN DRAW LINE BETWEEN OLD X&Y AND NEW X&Y
330 IF (B AND 16) AND ((X1<>X) OR (Y1<>Y)) THEN LINE (X1,Y1)-(X,Y),PSET
340 PSET(X1,Y1,P): REM RESET PIXEL AT OLD X & Y
350 X1=X: Y1=Y: P=PPOINT(X,Y): REM SAVE X & Y & COLOR
360 PSET(X,Y,C): REM SET PIXEL AT NEW X & Y
370 GOTO 230


The above programs can be found on a virtual cassette found in the COCOSUPP.ZIP file or can be downloaded here.

COCOPGMS.ZIP (2k)


Return to emulator documentation