]> cloudbase.mooo.com Git - z180-stamp-cpm3.git/blob - cbios/sdio.180
e6c12ea45e0674deaebacd3b01164c9df08fdec3
[z180-stamp-cpm3.git] / cbios / sdio.180
1 TITLE 'sd disk handler'
2
3 ; CP/M-80 Version 3 -- Modular BIOS
4
5
6 dseg
7
8 ; Disk drive dispatching tables for linked BIOS
9
10 public sd0,sd1,sd2,sd3
11
12 ; Variables containing parameters passed by BDOS
13
14 extrn @adrv,@rdrv
15 extrn @trk,@sect,@cnt
16 extrn @dma,@dbnk
17
18 ; System Control Block variables
19
20 extrn @ermde ; BDOS error mode
21
22 ; Utility routines in standard BIOS
23
24 extrn ?wboot ; warm boot vector
25 extrn ?pmsg ; print message @<HL> up to 00, saves <BC> & <DE>
26 extrn ?pdec ; print binary number in <A> from 0 to 99.
27 extrn ?pderr ; print BIOS disk error header
28 extrn ?conin,?cono ; con in and out
29 extrn ?const ; get console status
30
31 extrn bnk2phy ;
32 extrn msg.sm ;
33 extrn msg.recv ;
34 extrn add_hla
35
36
37 ; Port Address Equates
38
39 include config.inc
40 include z180reg.inc
41
42 ; CP/M 3 Disk definition macros
43
44 include cpm3slr.lib
45
46 ; Z180 macro library instruction definitions (ignored by slr180)
47
48 include z180.lib
49
50 ; common control characters
51
52 cr equ 13
53 lf equ 10
54 bell equ 7
55
56
57 ; Extended Disk Parameter Headers (XPDHs)
58
59 dw sd$write
60 dw sd$read
61 dw sd$login
62 dw sd$init0
63 db 0,0 ; relative drive zero
64 sd0: dph 0,dpbsimhd512
65
66 dw sd$write
67 dw sd$read
68 dw sd$login
69 dw sd$init1
70 db 1,0 ; relative drive one
71 sd1: dph 0,dpbsimhd512
72
73 dw sd$write
74 dw sd$read
75 dw sd$login
76 dw sd$init0
77 db 2,0 ; relative drive zero
78 sd2: dph 0,dpbsimhd512
79
80 dw sd$write
81 dw sd$read
82 dw sd$login
83 dw sd$init1
84 db 3,0 ; relative drive one
85 sd3: dph 0,dpbsimhd512
86
87
88 cseg ; DPB must be resident
89
90 dpbsimhd512:
91 dpb 512,8,2048,4096,1024,6
92
93
94 dseg ; rest is banked
95
96 ; Disk I/O routines for standardized BIOS interface
97
98 ; Initialization entry point.
99 ; called for first time initialization.
100
101 sd$init0:
102 ret
103
104 sd$init1:
105 sd$init2:
106 sd$init3:
107 ret ; all initialization done by drive 0
108
109
110
111
112 ; This entry is called when a logical drive is about to
113 ; be logged into for the purpose of density determination.
114 ; It may adjust the parameters contained in the disk
115 ; parameter header pointed at by <DE>
116
117 sd$login:
118 xor a
119 ld (residual),a
120 ld hl,send_msg+1
121 ld (hl),0 ;login function
122 inc hl
123 ld bc,(@adrv)
124 ld (hl),c ;@adrv
125 inc hl
126 ld (hl),b ;@rdrv
127 inc hl
128
129 ex de,hl ;xdph to hl
130 xor a ;bank 0
131 call bnk2phy ;phys. linear address
132 ex de,hl
133 ld (hl),e
134 inc hl
135 ld (hl),d
136 inc hl
137 ld (hl),a
138
139 ld hl,send_msg
140 ld b,send_msg_login_len
141 call msg.sm
142
143 ld hl,recv_msg
144 ld b,recv_msg_len ; max receive message len
145 call msg.recv
146
147 ; ld a,(recv_msg_rc)
148 ; or a
149 ret ;
150
151
152
153 ; disk READ and WRITE entry points.
154 ; these entries are called with the following arguments:
155 ;
156 ; absolute drive number in @adrv (8 bits) +0
157 ; relative drive number in @rdrv (8 bits) +1
158 ; disk track address in @trk (16 bits) +2
159 ; disk sector address in @sect(16 bits) +4
160 ; multi sector count in @cnt (8 bits) +6
161 ; disk transfer address in @dma (16 bits) +7
162 ; disk transfer bank in @dbnk (8 bits) +9
163 ; pointer to XDPH in <DE>
164 ;
165 ; they transfer the appropriate data, perform retries
166 ; if necessary, then return an error code in <A>
167
168 sd$read:
169 ld de,read$msg ; point at " Read "
170 ld c,1
171 jr rw$common
172 sd$write:
173 ld de,write$msg ; point at " Write "
174 ld c,2
175 rw$common:
176 ld hl,residual ; remainng sectors from last multi io?
177 ld a,(hl)
178 sub a,1
179 jr c,rwc_new_sectors
180
181 ld (hl),a
182 xor a
183 ret
184
185 rwc_new_sectors:
186 ld b,1 ; assume 1 sector to transfer
187 ld a,(@cnt)
188 or a
189 jr z,rwc_doit
190
191 ld b,a ; number of sectors to transfer
192 dec a ; save remaining
193 ld (hl),a
194 xor a ; reset multi sector count
195 ld (@cnt),a
196
197 ; compute pysical transfer address
198 ; prepare message
199 ; and send it to AVR.
200
201 rwc_doit:
202 ld hl,send_msg+1
203 ld (hl),c
204 inc hl
205 ld a,b
206 ex de,hl
207 ld (operation$name),hl ; save message for errors
208 ld hl,@adrv ;address of arguments
209 ld bc,6
210 ldir
211 ld (de),a
212 inc de
213 push de
214 ld e,(hl) ;dma address
215 inc hl
216 ld d,(hl)
217 inc hl
218 ld a,(hl) ;bank
219 ex de,hl
220 call bnk2phy ;phys. linear address
221 ex de,hl
222 pop hl
223 ld (hl),e
224 inc hl
225 ld (hl),d
226 inc hl
227 ld (hl),a
228
229 more$retries:
230
231 ld hl,send_msg
232 ld b,send_msg_rw_len
233 call msg.sm
234
235 ld hl,recv_msg
236 ld b,recv_msg_len ; max receive message len
237 call msg.recv
238 ld a,(recv_msg_rc)
239 or a
240 ret z ; check status and return to BDOS if no error
241
242 xor a
243 ld (residual),a
244
245 ; suppress error message if BDOS is returning errors to application...
246
247 ld a,(@ermde)
248 cp 0ffh
249 jr z,hard$error
250
251 ; Had permanent error, print message like:
252 ; BIOS Err on d: T-nn, S-mm, <operation> <type>, Retry ?
253
254 call ?pderr ; print message header
255
256 ld hl,(operation$name)
257 call ?pmsg ; last function (read or write)
258
259 ld a,(recv_msg_rc)
260
261 ;TODO: rc errorcode
262
263 ld a,(recv_msg_rc)
264 tst 080h
265 jr z,fs_end
266
267 fatfs_err:
268
269 ld a,(recv_msg_rc+1)
270 cp 20
271 jr c,fs_err1
272 xor a
273 fs_err1
274 ld hl,fr$msg0 ; point at first message
275 ld bc,fr$msg$size
276 ld e,a ; save message number
277 xor a
278 inc e
279 nxt_str:
280 dec e
281 jr z,fs_err_found
282 cpir
283 jr z,nxt_str
284 ld hl,fr$msg0 ; not found, point at first message
285
286 fs_err_found:
287 call ?pmsg
288
289 fs_end:
290 ld hl,error$msg
291 call ?pmsg ; print "<BEL>, Retry (Y/N) ? "
292 call u$conin$echo ; get operator response
293 cp 'y'
294 jr z,more$retries ; Yes, then retry 10 more times
295 hard$error:
296 ; otherwise,
297 ld a,1
298 ret ; return hard error to BDOS
299
300 cancel: ; here to abort job
301 jp ?wboot ; leap directly to warmstart vector
302
303
304
305 u$conin$echo:
306 ; get console input, echo it, and shift to upper case
307 call ?const
308 or a
309 jp z,u$c1 ; see if any char already struck
310 call ?conin
311 jp u$conin$echo ; yes, eat it and try again
312 u$c1:
313 call ?conin
314 push af
315 ld c,a
316 call ?cono
317 pop af
318 cp 'a'
319 ret c
320 sub 'a'-'a' ; make upper case
321 ret
322
323
324 residual:
325 db 0
326
327 send_msg:
328 db 2 ; disk command
329 ds 1 ; subcommand (login/read/write)
330 ds 1 ; @adrv
331 ds 1 ; @rdrv
332 ; -read/write- -login-
333 ds 2 ; @trk xdph address
334 ds 2 ; @sect (3 byte)
335 ds 1 ; @cnt
336 ds 3 ; transfer addr
337 send_msg_rw_len equ $ - send_msg
338 send_msg_login_len equ 7
339
340
341 recv_msg:
342 ds 1 ; len
343 ds 1 ; command
344 ds 1 ; subcommand
345 recv_msg_rc:
346 ds 1 ; result code
347 ds 4 ; room for additional parameter
348 recv_msg_len equ $ - recv_msg
349
350
351 ; error message components
352
353 operation$name:
354 dw read$msg
355 read$msg:
356 db ', Read',0
357 write$msg:
358 db ', Write',0
359
360
361 ; table of pointers to error message strings
362
363 fr$msg0: db ' Unknown FatFs Error,',0
364 fr$msg1: db ' DISK_ERR,',0
365 fr$msg2: db ' INT_ERR,',0
366 fr$msg3: db ' NOT_READY,',0
367 fr$msg4: db ' NO_FILE,',0
368 fr$msg5: db ' NO_PATH,',0
369 fr$msg6: db ' INVALID_NAME,',0
370 fr$msg7: db ' DENIED,',0
371 fr$msg8: db ' EXIST,',0
372 fr$msg9: db ' INVALID_OBJECT,',0
373 fr$msg10: db ' WRITE_PROTECTED,',0
374 fr$msg11: db ' INVALID_DRIVE,',0
375 fr$msg12: db ' NOT_ENABLED,',0
376 fr$msg13: db ' NO_FILE_SYSTEM,',0
377 fr$msg14: db ' MKFS_ABORTED,',0
378 fr$msg15: db ' TIMEOUT,',0
379 fr$msg16: db ' LOCKED,',0
380 fr$msg17: db ' NOT_ENOUGH_CORE,',0
381 fr$msg18: db ' TOO_MANY_OPEN_FILES,',0
382 fr$msg19: db ' FR_INVALID_PARAMETER,',0
383 fr$msg$size equ $ - fr$msg0
384
385
386 error$msg:
387 db ' Retry (Y/N) ? ',0
388
389
390 end
391
392 /* File function return code (FRESULT) */
393
394 FR_OK = 0, /* (0) Succeeded */
395 FR_DISK_ERR, /* (1) A hard error occurred in the low level disk I/O layer */
396 FR_INT_ERR, /* (2) Assertion failed */
397 FR_NOT_READY, /* (3) The physical drive cannot work */
398 FR_NO_FILE, /* (4) Could not find the file */
399 FR_NO_PATH, /* (5) Could not find the path */
400 FR_INVALID_NAME, /* (6) The path name format is invalid */
401 FR_DENIED, /* (7) Access denied due to prohibited access or directory full */
402 FR_EXIST, /* (8) Access denied due to prohibited access */
403 FR_INVALID_OBJECT, /* (9) The file/directory object is invalid */
404 FR_WRITE_PROTECTED, /* (10) The physical drive is write protected */
405 FR_INVALID_DRIVE, /* (11) The logical drive number is invalid */
406 FR_NOT_ENABLED, /* (12) The volume has no work area */
407 FR_NO_FILESYSTEM, /* (13) There is no valid FAT volume */
408 FR_MKFS_ABORTED, /* (14) The f_mkfs() aborted due to any parameter error */
409 FR_TIMEOUT, /* (15) Could not get a grant to access the volume within defined period */
410 FR_LOCKED, /* (16) The operation is rejected according to the file sharing policy */
411 FR_NOT_ENOUGH_CORE, /* (17) LFN working buffer could not be allocated */
412 FR_TOO_MANY_OPEN_FILES, /* (18) Number of open files > _FS_SHARE */
413 FR_INVALID_PARAMETER /* (19) Given parameter is invalid */