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 dw 0 ;; original file size
30 fcb_size equ $-fcb_data
32 $FORTH_CONST (FCB-BLKIN) fcb_data
33 $FORTH_CONST (FCB-BLEFT) fcb_data+2
34 $FORTH_CONST (FCB-SECTOR) fcb_data+4
35 $FORTH_CONST (FCB-FSIZE) fcb_data+6
37 $FORTH_VAR (TLOAD-PROGRESS) 1
40 $FORTH_WORD (FCB-DUMP)
51 $FORTH_END_WORD (FCB-DUMP)
55 ;; ensure that FCB sector is read
56 $FORTH_WORD (FENSURE-SECTOR)
59 (FCB-SECTOR) @ DUP 0BRANCH fensure_sector_nofile
60 (FCB-DISK-BUFFER) @ OVER = TBRANCH fensure_sector_done
61 ;;(.") ~( reading disk sector ~ ;;"
62 ;;DUP U. (.") ~)\n~ ;;"
64 DUP (FCB-DISK-BUFFER) !
65 DUP 1 (FCB-DISK-BUFFER-DATA) TR-SREAD
69 fensure_sector_nofile:
70 (.") ~DISK FILE NOT OPENED!\n~ ;;"
72 $FORTH_END_WORD (FENSURE-SECTOR)
75 ;; ensure that FCB sector is read, and BLKIN is valid
76 $FORTH_WORD (FENSURE-BLKIN)
80 (FCB-BLEFT) @ 0BRANCH fensure_blkin_eof
81 (FCB-BLKIN) @ LIT 256 < TBRANCH fensure_blkin_sector
89 (FCB-SECTOR) @ 0BRANCH fensure_sector_nofile
91 $FORTH_END_WORD (FENSURE-BLKIN)
99 $FORTH_END_WORD FOPENED?
104 (FCB-SECTOR) @ 0BRANCH fword_feof_tan
105 (FCB-BLEFT) @ 0BRANCH fword_feof_tan
109 $FORTH_END_WORD FEOF?
112 ;; "close" current file -- clear vars
116 (FCB) (FCB-SIZE) ERASE
118 $FORTH_END_WORD FCLOSE
121 ;; prepare variables for reading file
125 FOPENED? 0BRANCH fword_fopen_allowed
126 (.") ~DISK FILE ALREADY OPENED!\n~ ;;"
129 (TR-FFIND) DUP 0< 0BRANCH fopen_found
131 (.") ~DISK FILE NOT FOUND!\n~ ;;"
134 ;; do not read file right now
135 (TR-FILEBUF) 1- (FCB-SECTOR) !
136 LIT 256 (FCB-BLKIN) !
137 (TR-GET-LAST-FSIZE) DUP (FCB-BLEFT) ! (FCB-FSIZE) !
138 ;;(.") ~OPENED DISK FILE; LSEC:~ ;;"
139 ;;(FCB-SECTOR) @ 1+ U.
144 $FORTH_END_WORD FOPEN
147 ;; caller must ensure that FCB is valid
152 (FCB-BLEFT) @ TBRANCH fpeekch_not_eof
157 (FCB-BLKIN) @ (FCB-DISK-BUFFER-DATA) + C@
159 $FORTH_END_WORD FPEEKCH
162 ;; caller must ensure that FCB is valid and the sector is loaded
163 $FORTH_WORD (FREADCH)
167 (FCB-BLEFT) @ TBRANCH freadch_not_eof
170 ;; (.") ~(reading char...)\n~ ;;"
172 DUP LIT 256 < TBRANCH freadch_ok
181 ;; decrement bytes left
184 (FCB-DISK-BUFFER-DATA) + C@
186 $FORTH_END_WORD (FREADCH)
189 ;; read text file line by line
190 ;; all variables must be set!
193 ;; ( -- addr len TRUE | FALSE )
195 0 ;; ( counter/result )
196 FEOF? 0BRANCH fword_freadln_noteof
199 fword_freadln_noteof:
200 ;; scan for CHCR or CHLF
201 ;;(.") ~(reading line...)\n~ ;;"
203 (FREADCH) DUP 0< TBRANCH freadln_done
205 DUP CHCR = 0BRANCH fword_freadln_notcr
206 ;; cr, check for possible lf
207 FPEEKCH CHLF = 0BRANCH freadln_done
208 (FREADCH) DROP BRANCH freadln_done
210 DUP CHLF = TBRANCH freadln_done
214 OVER (FILE-LINE-BUF) + C!
215 1+ ;; increment length
216 ;; check if our line is too big
217 DUP (FILE-LINE-BUF-SIZE) <
218 TBRANCH fword_freadln_loop
219 ;; no need to drop anything
220 (.") ~DISK FILE LINE TOO LONG!\n~ ;;"
226 (FILE-LINE-BUF) OVER + BL SWAP !
227 (FILE-LINE-BUF) SWAP 1
229 ;;>R 2DUP SWAP U. U. CR R>
230 ;;>R 2DUP XTYPE CR R>
233 (TLOAD-PROGRESS) @ 0BRANCH freadln_exit
234 LIT emit6y @ LIT emit6y 0!
235 (FCB-FSIZE) @ (FCB-BLEFT) @ - LIT 100 (FCB-FSIZE) @ */
236 LIT 3 .R (.") ~ % done~ ;;"
240 $FORTH_END_WORD FREADLN
244 ;; interpret text file
249 FREADLN 0BRANCH tload_eof
251 DROP TIB ! IN 0! INTERPRET
252 ;;(.") ~(TLOAD DONE)\n~ ;;"
257 $FORTH_END_WORD (TLOAD)
260 $FORTH_CONST (TLOAD-NAME-BUF) tload_name_buf
261 ;; 9 bytes: name and ext
262 tload_name_buf: defb 0,0,0,0,0,0,0,0,0
264 $FORTH_WORD (TRD-PARSE-NAME)
266 ;; ( addr len defext -- newaddr len )
267 (TLOAD-NAME-BUF) LIT 9 BLANKS
269 LIT 8 MIN (TLOAD-NAME-BUF) SWAP CMOVE
270 R> -DUP 0BRANCH trd_parse_name_done
271 (TLOAD-NAME-BUF) LIT 8 + C!
273 (TLOAD-NAME-BUF) LIT 9
275 $FORTH_END_WORD (TRD-PARSE-NAME)
277 ;; interpret text file
281 LIT 70 (TRD-PARSE-NAME)
282 (.") "loading: " 2DUP XTYPE CR ;;"
285 $FORTH_END_WORD TLOAD