1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
6 $FORTH_CONST (FCB-DISK-BUFFER) fcb_disk_buffer
7 $FORTH_CONST (FCB-DISK-BUFFER-DATA) fcb_disk_buffer+2
12 fcb_line_buffer_size equ 80
13 $FORTH_CONST (FILE-LINE-BUF) fcb_line_buffer
14 $FORTH_CONST (FILE-LINE-BUF-SIZE) fcb_line_buffer_size
16 defw 0 ;; working area
18 defs fcb_line_buffer_size,0
22 $FORTH_CONST (FCB) fcb_data
23 $FORTH_CONST (FCB-SIZE) fcb_size
26 dw 0 ;; offset in the current sector
27 dw 0 ;; bytes left in file
28 dw 0 ;; current file sector
29 fcb_size equ $-fcb_data
31 $FORTH_CONST (FCB-BLKIN) fcb_data
32 $FORTH_CONST (FCB-BLEFT) fcb_data+2
33 $FORTH_CONST (FCB-SECTOR) fcb_data+4
36 $FORTH_WORD (FCB-DUMP)
47 $FORTH_END_WORD (FCB-DUMP)
50 ;; ensure that FCB sector is read
51 $FORTH_WORD (FENSURE-SECTOR)
54 (FCB-SECTOR) @ DUP 0BRANCH fensure_sector_nofile
55 (FCB-DISK-BUFFER) @ OVER = TBRANCH fensure_sector_done
56 ;;(.") ~( reading disk sector ~ ;;"
57 ;;DUP U. (.") ~)\n~ ;;"
59 DUP (FCB-DISK-BUFFER) !
60 DUP 1 (FCB-DISK-BUFFER-DATA) TR-SREAD
64 fensure_sector_nofile:
65 (.") ~DISK FILE NOT OPENED!\n~ ;;"
67 $FORTH_END_WORD (FENSURE-SECTOR)
70 ;; ensure that FCB sector is read, and BLKIN is valid
71 $FORTH_WORD (FENSURE-BLKIN)
75 (FCB-BLEFT) @ 0BRANCH fensure_blkin_eof
76 (FCB-BLKIN) @ LIT 256 < TBRANCH fensure_blkin_sector
84 (FCB-SECTOR) @ 0BRANCH fensure_sector_nofile
86 $FORTH_END_WORD (FENSURE-BLKIN)
94 $FORTH_END_WORD FOPENED?
99 (FCB-SECTOR) @ 0BRANCH fword_feof_tan
100 (FCB-BLEFT) @ 0BRANCH fword_feof_tan
104 $FORTH_END_WORD FEOF?
107 ;; "close" current file -- clear vars
111 (FCB) (FCB-SIZE) ERASE
113 $FORTH_END_WORD FCLOSE
116 ;; prepare variables for reading file
120 FOPENED? 0BRANCH fword_fopen_allowed
121 (.") ~DISK FILE ALREADY OPENED!\n~ ;;"
124 (TR-FFIND) DUP 0< 0BRANCH fopen_found
126 (.") ~DISK FILE NOT FOUND!\n~ ;;"
129 ;; do not read file right now
130 (TR-FILEBUF) 1- (FCB-SECTOR) !
131 LIT 256 (FCB-BLKIN) !
132 (TR-GET-LAST-FSIZE) (FCB-BLEFT) !
133 ;;(.") ~OPENED DISK FILE; LSEC:~ ;;"
134 ;;(FCB-SECTOR) @ 1+ U.
139 $FORTH_END_WORD FOPEN
142 ;; caller must ensure that FCB is valid
147 (FCB-BLEFT) @ TBRANCH fpeekch_not_eof
152 (FCB-BLKIN) @ (FCB-DISK-BUFFER-DATA) + C@
154 $FORTH_END_WORD FPEEKCH
157 ;; caller must ensure that FCB is valid and the sector is loaded
158 $FORTH_WORD (FREADCH)
162 (FCB-BLEFT) @ TBRANCH freadch_not_eof
165 ;; (.") ~(reading char...)\n~ ;;"
167 DUP LIT 256 < TBRANCH freadch_ok
176 ;; decrement bytes left
179 (FCB-DISK-BUFFER-DATA) + C@
181 $FORTH_END_WORD (FREADCH)
184 ;; read text file line by line
185 ;; all variables must be set!
188 ;; ( -- addr len TRUE | FALSE )
190 0 ;; ( counter/result )
191 FEOF? 0BRANCH fword_freadln_noteof
194 fword_freadln_noteof:
195 ;; scan for CHCR or CHLF
196 ;;(.") ~(reading line...)\n~ ;;"
198 (FREADCH) DUP 0< TBRANCH freadln_done
200 DUP CHCR = 0BRANCH fword_freadln_notcr
201 ;; cr, check for possible lf
202 FPEEKCH CHLF = 0BRANCH freadln_done
203 (FREADCH) DROP BRANCH freadln_done
205 DUP CHLF = TBRANCH freadln_done
209 OVER (FILE-LINE-BUF) + C!
210 1+ ;; increment length
211 ;; check if our line is too big
212 DUP (FILE-LINE-BUF-SIZE) <
213 TBRANCH fword_freadln_loop
214 ;; no need to drop anything
215 (.") ~DISK FILE LINE TOO LONG!\n~ ;;"
221 (FILE-LINE-BUF) OVER + BL SWAP !
222 (FILE-LINE-BUF) SWAP 1
224 ;;>R 2DUP SWAP U. U. CR R>
225 ;;>R 2DUP XTYPE CR R>
228 $FORTH_END_WORD FREADLN
232 ;; interpret text file
237 FREADLN 0BRANCH tload_eof
239 DROP TIB ! IN 0! INTERPRET
240 ;;(.") ~(TLOAD DONE)\n~ ;;"
245 $FORTH_END_WORD (TLOAD)
248 $FORTH_CONST (TLOAD-NAME-BUF) tload_name_buf
249 ;; 9 bytes: name and ext
250 tload_name_buf: defb 0,0,0,0,0,0,0,0,0
252 $FORTH_WORD (TRD-PARSE-NAME)
254 ;; ( addr len defext -- newaddr len )
255 (TLOAD-NAME-BUF) LIT 9 BLANKS
257 LIT 8 MIN (TLOAD-NAME-BUF) SWAP CMOVE
258 R> -DUP 0BRANCH trd_parse_name_done
259 (TLOAD-NAME-BUF) LIT 8 + C!
261 (TLOAD-NAME-BUF) LIT 9
263 $FORTH_END_WORD (TRD-PARSE-NAME)
265 ;; interpret text file
269 LIT 70 (TRD-PARSE-NAME)
270 (.") "loading: " 2DUP XTYPE CR ;;"
273 $FORTH_END_WORD TLOAD