dsforth: added space in comment ;-)
[urasm.git] / dsforth / dos_low_p3dos.zas
blobccb8b03e42b49aa9d70e58d5fb65103c0e5eee11
1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2 ;; low-level +3DOS words
3 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
5 ; Pages 1, 3, 4, 6 are considered as an array
6 ; of 128 sector buffers (numbered 0...127),
7 ; each of 512 bytes.
8 ; The cache and RAMdisk occupy two separate
9 ; (contiguous) areas of this array.
12 IF !defined(USE_P3DOS_ERROR_MESSAGES)
13 USE_P3DOS_ERROR_MESSAGES equ 0
14 ENDIF
16 IF USE_P3DOS_ERROR_MESSAGES
17 p3dos_error_msg_table:
18   defb 0+1
19     defc "Drive not ready"
20   defb 1+1
21     defc "Disk is write protected"
22   defb 2+1
23     defc "Seek fail"
24   defb 3+1
25     defc "CRC data error"
26   defb 4+1
27     defc "No data"
28   defb 5+1
29     defc "Missing address mark"
30   defb 6+1
31     defc "Unrecognised disk format"
32   defb 7+1
33     defc "Unknown disk error"
34   defb 8+1
35     defc "Disk changed"
36   defb 9+1
37     defc "Unsuitable media"
38 ;;Non-recoverable errors:
39   defb 20+1
40     defc "Bad filename"
41   defb 21+1
42     defc "Bad parameter"
43   defb 22+1
44     defc "Drive not found"
45   defb 23+1
46     defc "File not found"
47   defb 24+1
48     defc "File already exists"
49   defb 25+1
50     defc "End of file"
51   defb 26+1
52     defc "Disk full"
53   defb 27+1
54     defc "Directory full"
55   defb 28+1
56     defc "Read-only file"
57   defb 29+1
58     defc "File number not open"
59   defb 30+1
60     defc "Access denied"
61   defb 31+1
62     defc "Cannot rename between drives"
63   defb 32+1
64     defc "Extent missing"
65   defb 33+1
66     defc "Uncached"
67   defb 34+1
68     defc "File too big"
69   defb 35+1
70     defc "Disk not bootable"
71   defb 36+1
72     defc "Drive in use"
73   ;; k8 errors
74   defb 66+1
75     defc "Too many open files"
76   defb 0
77 ENDIF
80 ;; show warning message
81 $FORTH_WORD (+3DOS-ERR-TEXT)
82 ;; k8
83 ;; ( num -- addr count )
84   $IF USE_P3DOS_ERROR_MESSAGES
85   1+ >R
86   LIT p3dos_error_msg_table
87   ;; ( tbl | num )
88 .loop:
89   ;; ( tbl | num )
90   COUNT  ;; ( tbl+1 msgid | num )
91   ?DUP 0BRANCH .loop_done
92   ;; ( tbl+1 msgid | num )
93   R@ = 0BRANCH .loop_skip
94   COUNT     ;; ( addr count | num )
95   RDROP ;S
96 .loop_skip:
97     COUNT +  ;; skip string
98   BRANCH .loop
99 .loop_done:
100   ;; ( tbl | num )
101   DROP R> 1-
102   $ENDIF
103   BASE @ >R DECIMAL
104   ;; ERR#
105   0 <# #S [CHAR] # HOLD [CHAR] R DUP HOLD HOLD [CHAR] E HOLD #>
106   R> BASE !
107   ;S
108 $FORTH_END_WORD (+3DOS-ERR-TEXT)
111 $FORTH_VAR (+3DOS-R/O)  0
112 ;; registers for +3DOS calls
113 ;; loaded before calling a function, and the result will be stored here
114 $FORTH_VAR (+3DOS-BC)  0
115 fvar_p3dos_bc equ $-2
116 fvar_p3dos_b equ $-1
117 fvar_p3dos_c equ $-2
118 $FORTH_CONST (+3DOS-B) fvar_p3dos_bc+1
119 $FORTH_CONST (+3DOS-C) fvar_p3dos_bc
121 $FORTH_VAR (+3DOS-DE)  0
122 fvar_p3dos_de equ $-2
123 fvar_p3dos_d equ $-1
124 fvar_p3dos_e equ $-2
125 $FORTH_CONST (+3DOS-D) fvar_p3dos_de+1
126 $FORTH_CONST (+3DOS-E) fvar_p3dos_de
128 $FORTH_VAR (+3DOS-HL)  0
129 fvar_p3dos_hl equ $-2
130 fvar_p3dos_h equ $-1
131 fvar_p3dos_l equ $-2
132 $FORTH_CONST (+3DOS-H) fvar_p3dos_hl+1
133 $FORTH_CONST (+3DOS-L) fvar_p3dos_hl
135 $FORTH_VAR (+3DOS-AF)  0
136 fvar_p3dos_af equ $-2
137 fvar_p3dos_a equ $-1
138 fvar_p3dos_f equ $-2
139 $FORTH_CONST (+3DOS-A) fvar_p3dos_af+1
140 $FORTH_CONST (+3DOS-F) fvar_p3dos_af
143 $FORTH_CODE_WORD (+3DOS-CF?)
144 ;; k8
145 ;; ( -- carry flag )
146   ld    a,(fvar_p3dos_af)
147   and   #01
148   jp    i_pusha
149 $FORTH_END_CODE_WORD (+3DOS-CF?)
151 $FORTH_CODE_WORD (+3DOS-ZF?)
152 ;; k8
153 ;; ( -- carry flag )
154   ld    a,(fvar_p3dos_af)
155   and   #40
156   cp    #40
157   ld    a,0
158   adc   a,0
159   jp    i_pusha
160 $FORTH_END_CODE_WORD (+3DOS-ZF?)
163 ;; check error, show error message, return error flag
164 $FORTH_WORD (+3DOS-ERR?)
165 ;; k8
166 ;; ( -- errflag )
167   (+3DOS-CF?) TBRANCH .noerror
168   (.") ~\x02+3DOS ERROR: ~  ;;"
169   (+3DOS-A) C@  ;;U. CR
170   (+3DOS-ERR-TEXT) XTYPE CR
171   1 ;S
172 .noerror:
173   0 ;S
174 $FORTH_END_WORD (+3DOS-ERR?)
177 ;; call +3DOS
178 ;; WARNING! SP should not be at TSTACK!
179 $FORTH_CODE_WORD (+3DOS-CMD)
180 ;; k8
181 ;; ( fnidx -- )
182 dop3dos:
183   pop   hl
184   push  bc
185   push  ix         ; some +3DOS functions destroys it
187   ; L: function index
188   ; *3
189   ld    a,l
190   add   a,a
191   add   a,l
192   ld    l,a
193   ld    h,0x01
194   ld    (p3dos_call_addr),hl
196   ; prepare stack and RAM/ROM
197   ld    a,(BANKM)  ; RAM/ROM switching system variable
198   res   5,a        ; reset locking (just in case)
199   ld    (p3dos_saved_bankm),a  ; save it to be restored later
200   or    7          ; want RAM page 7
201   res   4,a        ; and DOS ROM
202   ld    bc,#7FFD   ; port used for horiz ROM switch and RAM paging
204   di
205   ld    (BANKM),a  ; keep system variables up to date
206   out   (c),a      ; RAM page 7 to top and DOS ROM
208   ; set +2A/+3 port #1FFD
209   ld    a,(BANK678)
210   ld    (p3dos_saved_bank678),a
211   set   2,a        ; switch on +3DOS ROM
212   ld    (BANK678),a
213   ld    b,#1F
214   out   (c),a
216   ld    (p3dos_saved_sp),sp
218   ; load AF
219   ld    sp,fvar_p3dos_af
220   pop   af
222   ; switch stack to TSTACK
223   ;ld    sp,#5C00
224   ld    sp,#5FFF
226   ld    hl,p3dosret
227   push  hl
229   ; switch to IM1
230   ld    a,#3F
231   ld    i,a
232   im    1
234   ei
236   ; load all registers (except AF)
237   ld    hl,(fvar_p3dos_hl)
238   ld    de,(fvar_p3dos_de)
239   ld    bc,(fvar_p3dos_bc)
241   jp    0
242 p3dos_call_addr equ $-2
244 p3dosret:
245   di
246   ; save af
247   ld    sp,fvar_p3dos_af+2
248   push  af
249   ; save all registers (except AF)
250   ld    (fvar_p3dos_hl),hl
251   ld    (fvar_p3dos_de),de
252   ld    (fvar_p3dos_bc),bc
254   ; restore banking
255   ;ld    a,(BANKM)
256   ;and   #F8        ; reset bits for page 0
257   ;set   4,a        ; switch to ROM 3 (48 BASIC)
258   ;res   5,a        ; reset locking (just in case)
259   ld    a,(p3dos_saved_bankm)
260   ld    bc,#7FFD
261   ld    (BANKM),a
262   out   (c),a      ; switch back to RAM page 0 and 48 BASIC
264   ; restore +2A/+3 port #1FFD
265   ld    a,(p3dos_saved_bank678)
266   ld    b,#1F
267   ld    (BANK678),a
268   out   (c),a
270   ; restore stack
271   ld    sp,(p3dos_saved_sp)
273   ; switch back to IM2
274   ld    a,(f_useIM2)
275   or    a
276   jr    z,.notim2
278   ld    a,(f_im2Ireg)
279   ld    i,a
280   im    2
282 .notim2:
283   ei
284   pop   ix
285   pop   bc
286   jp    i_next
288 p3dos_saved_bankm: defb 0
289 p3dos_saved_bank678: defb 0
290 p3dos_saved_sp: defw 0
291 $FORTH_END_CODE_WORD (+3DOS-CMD)
294 fword_p3dos_cat_mask: defm "*.*",0xff
296 fword_p3dos_cat_buffer equ #5D40
297 fword_p3dos_cat_buffer_len equ 8
299 $FORTH_WORD +3-CAT
300 ;; k8
301 ;; ( -- )
302   ;; maximum is actually 64x13+13 = 845
303   (SCROLL-CR-RESET)
304   LIT fword_p3dos_cat_buffer LIT fword_p3dos_cat_buffer_len*13 ERASE
305 .catloop:
306   LIT fword_p3dos_cat_buffer_len (+3DOS-B) C!    ;; the number of entries in the buffer
307   1 (+3DOS-C) C!     ;; include system files in the catalog
308   LIT fword_p3dos_cat_buffer (+3DOS-DE) !  ;; buffer address
309   LIT fword_p3dos_cat_mask (+3DOS-HL) !
310   LIT 10 (+3DOS-CMD) (+3DOS-ERR?) TBRANCH .exit
311   (+3DOS-B) C@ DUP 1 > 0BRANCH .exit
312   LIT fword_p3dos_cat_buffer+13
313   SWAP 1 (DO)
314 .printloop:
315     ENDCR
316     DUP LIT 8 XTYPE [CHAR] . XEMIT DUP LIT 8 + 3 XTYPE
317     SPACE DUP LIT 11 + @ 0 U.R (.") ~kb~  ;;"
318     (SCROLL-CR)
319     LIT 13 +
320   (LOOP) .printloop
321   DROP
322   ;; continue?
323   ;;(+3DOS-B) C@ . CR
324   (+3DOS-B) C@ LIT fword_p3dos_cat_buffer_len = 0BRANCH .exit_nodrop
325   LIT fword_p3dos_cat_buffer+13*(fword_p3dos_cat_buffer_len-1) LIT fword_p3dos_cat_buffer LIT 13 CMOVE
326   BRANCH .catloop
327 .exit:
328   DROP
329 .exit_nodrop:
330   ;S
331 $FORTH_END_WORD +3-CAT
334 $FORTH_WORD (+3DOS-NO-RAMDISK)
335   ;; set
336   LIT 0x0008 (+3DOS-DE) !  ;; cache
337   LIT 0x0000 (+3DOS-HL) !  ;; ramdist
338   LIT 21 (+3DOS-CMD)
339   (+3DOS-ERR?) DROP
340   ;S
341 $FORTH_END_WORD (+3DOS-NO-RAMDISK)