Port Mapped PC I/O Command Examples

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

10 CLEAR 500: DEFINT A-Z
20 REM DEFINE STRING & FILENAME FOR "OPEN" FUNCTION
30 A$="TEST.BAS"+CHR$(0): REM STRING MUST BE ZERO TERMINATED
40 REM DEFINE & SAVE ATTRIBUTES TO USE IN OPEN
50 OUT 112,64: OUT 113,128: OUT 113,1: REM CREATE FILE WITH READ & WRITE PERMISSION
60 OUT 112,66: OUT 113,65: OUT 113,2: REM CREATE OR TRUNCATE FILE AND OPEN WRITE ONLY
60 REM SAVE STRING POINTER
70 A=VARPTR(A$)+1: OUT 112,65: OUT 113,PEEK(A): OUT 113,PEEK(A+1): REM LSB FIRST, MSB SECOND
80 REM EXECUTE "OPEN" FUNCTION, IF SUCCESSFUL DE WILL CONTAIN FILE'S CHANNEL NUMBER
90 OUT 112,48: IF INP(112)<>0 THEN GOTO 300
100 REM DEFINE BUFFER AND LOAD WITH DATA
110 A$="THIS FILE WAS WRITTEN FROM THE TRS-80 EMULATOR"+CHR$(10)+CHR$(13)
120 REM SAVE BUFFER LENGTH
130 OUT 112,66: OUT 113,LEN(A$): OUT 113,0
140 REM SAVE STRING POINTER
150 A=VARPTR(A$)+1: OUT 112,65: OUT 113,PEEK(A): OUT 113,PEEK(A+1): REM LSB FIRST, MSB SECOND
160 REM EXECUTE "WRITE" FUNCTION
170 OUT 112,51: IF INP(112)<>0 THEN GOTO 300
180 REM EXECUTE "CLOSE" FUNCTION
190 OUT 112,49: IF INP(112)<>0 THEN GOTO 300
200 END
300 REM ERROR
310 PRINT "AN ERROR OCCURED"
320 END

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

10 CLEAR 500: DEFINT A-Z
20 REM DEFINE STRING & WILDCARD FOR "FIND FIRST" FUNCTION
30 A$="*.*"+STRING$(40,0): REM STRING MUST BE 43 CHAR LONG
40 REM DEFINE & SAVE ATTRIBUTES TO USE IN SEARCH
50 OUT 112,64: OUT 113,0: OUT 113,0: REM FIND ALL FILES
60 REM SAVE STRING LENGTH (MUST BE >=43, EXTRA UNUSED)
70 OUT 112,66: OUT 113,43: OUT 113,0
80 REM SAVE STRING POINTER
90 A=VARPTR(A$)+1: OUT 112,65: OUT 113,PEEK(A): OUT 113,PEEK(A+1): REM LSB FIRST, MSB SECOND
100 REM EXECUTE "FIND FIRST" FUNCTION
110 OUT 112,54: IF INP(112)<>0 THEN GOTO 190
120 REM PRINT FILENAME FOUND
130 A=INSTR(31,A$,CHR$(0)): IF A>31 THEN PRINT MID$(A$,31,A-31)
140 REM RESAVE BUFFER ADDRESS BECAUSE BASIC MOVES STRINGS
150 A=VARPTR(A$)+1: OUT 112,65: OUT 113,PEEK(A): OUT 113,PEEK(A+1): REM LSB FIRST, MSB SECOND
160 REM EXECUTE "FIND NEXT" FUNCTION
170 OUT 112,55: IF INP(112)<>0 THEN GOTO 190
180 GOTO 120
190 PRINT "END OF LIST ..."
200 END

Return to emulator documentation