]> cloudbase.mooo.com Git - avrcpm.git/blob - avr/dsk_fsys.asm
* Test: Change directory hierarchy.
[avrcpm.git] / avr / dsk_fsys.asm
1 ; Filesystem functions for the Interaction with BIOS and Disks
2 ;
3 ; Copyright (C) 2010 Frank Zoll
4 ;
5 ; This file is part of avrcpm.
6 ;
7 ; avrcpm is free software: you can redistribute it and/or modify it
8 ; under the terms of the GNU General Public License as published by
9 ; the Free Software Foundation, either version 3 of the License, or
10 ; (at your option) any later version.
11 ;
12 ; avrcpm is distributed in the hope that it will be useful,
13 ; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ; GNU General Public License for more details.
16 ;
17 ; You should have received a copy of the GNU General Public License
18 ; along with avrcpm. If not, see <http://www.gnu.org/licenses/>.
19 ;
20 ; $Id$
21 ;
22
23 .equ DSKSEL_DEBUG = 0
24
25 ; ---------------- Defines for the Filesystem Interface -------
26
27 ;*****************************************************
28 ;* Disk-Manager constants *
29 ;*****************************************************
30
31 ; Fields in the parttabl
32
33 .equ MAXDISKS = 8 ;Max number of Disks (partitions)
34 .equ PARTENTRY_SIZE = 9 ;Size of a Partitiontableentry
35
36 .equ PTAB_TYPE = 0
37 .equ PTAB_START = 1
38 .equ PTAB_SIZE = 5
39 .equ PTAB_SPT = 7
40 .equ PTAB_BSH = 8
41
42 .equ dskType_None = 0 << 4
43 .equ dskType_CPM = 1 << 4
44 .equ dskType_FAT = 2 << 4
45 ; .equ dskType_RAM = 3 << 4
46 .equ dskType_MASK = 0xf << 4
47
48 ;*****************************************************
49 ;* CP/M to host disk constants *
50 ;*****************************************************
51 ; .equ blksize = 1024 ;CP/M allocation size
52 ; .equ CPMSPT = 26 ;
53
54 .equ HOSTSIZE = 512 ;host disk sector size
55 .equ HOSTBLK = HOSTSIZE/128 ;CP/M sects/host buff
56 .equ SECMSK = HOSTBLK-1 ;sector mask
57 .equ SECSHF = log2(HOSTBLK) ;sector shift
58
59 ;*****************************************************
60 ;* BDOS constants on entry to write *
61 ;*****************************************************
62 .equ WRALL = 0 ;write to allocated
63 .equ WRDIR = 1 ;write to directory
64 .equ WRUAL = 2 ;write to unallocated
65 .equ WRTMSK= 3 ;write type mask
66
67 .equ READ_FUNC = 7
68 .equ WRITE_FUNC = 6
69 .equ BOOT_FUNC = 5
70 .equ HOME_FUNC = 4
71
72 ;----------------------------------------------- Start of Data Segment
73
74 .dseg
75
76 ; The following 3 variables are copied from DRAM.
77 ; Don't change order.
78
79 biosdrvtbl: .byte 2 ;
80 biosdirbuf: .byte 2 ;
81 biosenddat: .byte 2 ;
82
83 ndisks: .byte 1 ;Number of CP/M disks
84
85 ; The following 5 variables are accessed from 8080/z80 via the
86 ; virtual port interface. Don't change order.
87
88 biospar_base:
89 bcbadr: .byte 2 ;adr of BiosControlBlock
90 seekdsk: .byte 1 ;seek disk number
91 seektrk: .byte 2 ;seek track number
92 seeksec: .byte 2 ;seek sector number
93 dmaadr: .byte 2 ;last dma address
94
95 hdrsize: .byte 1 ;Image header size (offset)
96 cpmspt: .byte 1 ;CP/M sectors per track
97 secpblk: .byte 1 ;sectors per block (alloc size)
98 unacnt: .byte 1 ;unalloc rec cnt
99 unadsk: .byte 1 ;last unalloc disk
100 unatrk: .byte 2 ;last unalloc track
101 unasec: .byte 1 ;last unalloc sector
102
103 erflag: .byte 1 ;error reporting
104 wrtype: .byte 1 ;write operation type
105
106 hostdsk: .byte 1 ;host disk number
107 hostlba: .byte 3 ;host sector number (relative to partition start)
108 hostparttbl: .byte PARTENTRY_SIZE*MAXDISKS ;host partition table (type, start sector, sector count)
109 hostparttbltop:
110 hostbuf: .byte HOSTSIZE ;host buffer (from/to SD-card)
111
112
113 ; ------------------------------- Start of Code Segment
114 .cseg
115
116 ;---------------------------------------------------------------------
117
118 .if DSKSEL_DEBUG
119
120 dbg_prdrvtbl:
121 push xh
122 push xl
123 push temp3
124 push temp2
125 push temp
126 printnewline
127 printstring "drvtbl ("
128 lds xl,biosdrvtbl
129 lds xh,biosdrvtbl+1
130 movw temp,x
131 rcall printhexw
132 printstring "): "
133 ldi temp3,16
134 dbg_pcpel:
135 rcall dram_readw_pp
136 rcall printhexw
137 printstring " "
138 dec temp3
139 brne dbg_pcpel
140 pop temp
141 pop temp2
142 pop temp3
143 pop xl
144 pop xh
145 ret
146
147 dbg_print_biosd:
148 printnewline
149 lds temp,bcbadr
150 lds temp2,bcbadr+1
151 rcall printhexw
152 printstring " "
153 lds temp,biosdrvtbl
154 lds temp2,biosdrvtbl+1
155 rcall printhexw
156 printstring " "
157 lds temp,biosdirbuf
158 lds temp2,biosdirbuf+1
159 rcall printhexw
160 printstring " "
161 lds temp,biosenddat
162 lds temp2,biosenddat+1
163 rcall printhexw
164 printstring " "
165 ret
166 .endif
167
168 ; ====================================================================
169 ; Function: Get a Pointer to a Partitiontable entry
170 ; ====================================================================
171 ; Parameters
172 ; --------------------------------------------------------------------
173 ; Registers : [w] z Pointer to the Partitionentry
174 ; [r] zl Number of Diskentry to Read
175 ; [w] _tmp0 scratch
176 ; [w] _tmp1 "
177 ; --------------------------------------------------------------------
178 ; Description:
179 ; ====================================================================
180 dsk_getpartentry:
181
182 ldi zh,PARTENTRY_SIZE
183 mul zh,zl
184 ldiw z,hostparttbl
185 add zl,_tmp0
186 add zh,_tmp1
187 ret
188
189 ; ====================================================================
190 ; Function: Virtual Port Interface
191 ; ====================================================================
192 ; Parameters
193 ; --------------------------------------------------------------------
194 ; Registers :
195 ; Variables :
196 ;
197 ; --------------------------------------------------------------------
198 ; Description:
199 ; ====================================================================
200
201 dsk_param_getadr:
202 ldiw z,biospar_base
203 add zl,temp3
204 adc zh,_0
205 ret
206
207 dsk_param_wr:
208 rcall dsk_param_getadr
209 st z,temp
210 cpi temp3,bcbadr+1-seektrk
211 brne dsk_param_wr1
212 std z+1,_0
213 dsk_param_wr1:
214 cpi temp3,bcbadr+1-biospar_base
215 breq SetBCB
216 ret
217
218 dsk_param_rd:
219 cpi temp3,seekdsk-biospar_base
220 breq dskDiskCheck
221 rcall dsk_param_getadr
222 ld temp,z
223 ret
224
225 SetBCB:
226 lds xl,bcbadr
227 mov xh,temp
228 ldiw z,biosdrvtbl
229 ldi temp3,6
230 sbcb_l:
231 rcall dram_read_pp
232 st z+,temp
233 dec temp3
234 brne sbcb_l
235
236 ; rcall dbg_print_biosd
237 rcall dpb_drvtblinit
238 ; rcall dbg_prdrvtbl
239
240 ret
241
242 ; ====================================================================
243 ; Function: Check if disk exists
244 ; ====================================================================
245 ; Parameters
246 ; --------------------------------------------------------------------
247 ; Registers :
248 ; Variables :
249 ; return 0, if selected disk not exist.
250 ; return !0, if disk exist
251 ; --------------------------------------------------------------------
252 ; Description:
253 ; ====================================================================
254 dskDiskCheck:
255 lds temp2,ndisks
256 lds temp,seekdsk
257 .if DSKSEL_DEBUG
258 printnewline
259 printstring "DiskCheck: "
260 rcall printhexw
261 .endif
262 cpi temp,RAMDISKNR
263 brsh dsk_dchrd ;maybe ramdisk
264
265 ; Check if selected disk # is less then # of disks.
266
267 lds temp2,ndisks
268 tst temp2 ;0 disks?
269 brne dsk_dchpart1
270
271 ; No disks yet, need to init
272
273 rcall mgr_init_partitions ;disk chanched?
274 ; sbrs temp,7
275 ; rjmp dsk_dchpart0
276 push temp
277 rcall dpb_drvtblinit
278 ; rcall dbg_prdrvtbl
279 pop temp2
280 dsk_dchpart0:
281 cbr temp2,0x80
282 lds temp,seekdsk
283
284 dsk_dchpart1:
285 cp temp,temp2
286 brsh dsk_dch_err
287
288 .if DSKSEL_DEBUG
289 printnewline
290 printstring "Select: "
291 rcall printhex
292 .endif
293 rcall dpb_drvtbl_entry_get
294 or temp,temp2 ;if !0, drive is allready initialized
295 brne dsk_dchend
296 lds temp,seekdsk
297 rcall dpb_diskinit
298
299 dsk_dchend:
300 .if DSKSEL_DEBUG
301 push temp
302 lds temp,seekdsk
303 rcall dpb_drvtbl_entry_get
304
305 printstring ", "
306 rcall printhexw
307 pop temp
308 .endif
309
310 ret
311
312 dsk_dch_err:
313 ldi temp,0 ;error return
314 ret
315
316 ; Check RAMDISK
317
318 dsk_dchrd:
319 #if RAMDISKCNT
320 cpi temp,RAMDISKNR+RAMDISKCNT
321 brsh dsk_dchrd_err
322
323 ldi temp,0xff ;return ok
324 ret
325 #endif
326 dsk_dchrd_err:
327 ldi temp,0 ;error return
328 ret
329
330
331 ; ====================================================================
332 ; Function: Return status of last disk i/o function
333 ; ====================================================================
334 ; Parameters
335 ; --------------------------------------------------------------------
336 ; Registers :
337 ; Variables :
338 ; --------------------------------------------------------------------
339 ; Description:
340 ; ====================================================================
341 dskErrorRet:
342 lds temp,erflag
343 ret
344
345
346 ; -------------------------------------------------------------------
347
348
349 .dseg
350 tmpdpb: .byte 16
351
352 .cseg
353
354 ; DPBs for varios fixed formats
355 ; dpb data starts at 2. byte
356
357 dpbdat_avrcpm: ;(dpb243)
358 .db 0x00,0x1A ;sector offset, low(spt)
359 .db 0x00,0x03 ;high (spt), block shift
360 .db 0x07,0x00 ;bock mask, extent mask
361 .db 0xF2,0x00 ;disk size - 1,
362 .db 0x3F,0x00 ;dir max
363 .db 0xC0,0x00 ;alloc0, alloc1
364 .db 0x10,0x00 ;chk size
365 .db 0x02,0x00 ;offset
366
367
368
369 dpbdat_myz80: ;
370 .db 0x02,0x80 ;sector offset, low(spt)
371 .db 0x00,0x05 ;high (spt), block shift
372 .db 0x1F,0x01 ;bock mask, extent mask
373 .db 0xFF,0x07 ;disk size - 1,
374 .db 0xFF,0x03 ;dir max
375 .db 0xFF,0x00 ;alloc0, alloc1
376 .db 0x00,0x01 ;chk size
377 .db 0x00,0x00 ;offset
378
379 #if 0
380 ;rd1016
381 .db 0x20,0x00 ;spt
382 .db 0x04,0x0F ;block shift, bock mask
383 .db 0x00,0xFB ;extent mask, low(disk size -1),
384 .db 0x01,0xBF ;high(disk size -1), low(dir max)
385 .db 0x00,0xE0 ;high(dir max), alloc0
386 .db 0x00,0x30 ;alloc1, low(chk size)
387 .db 0x00,0x02 ;high(chk size), low(offset)
388 .db 0x00,0x00 ;high(offset), fill
389 ;rd9192s
390 .db 0x20,0x00 ;spt
391 .db 0x05,0x1F ;block shift, bock mask
392 .db 0x01,0xFD ;extent mask, low(disk size -1),
393 .db 0x07,0xFF ;high(disk size -1), low(dir max)
394 .db 0x01,0xF0 ;high(dir max), alloc0
395 .db 0x00,0x80 ;alloc1, low(chk size)
396 .db 0x00,0x02 ;high(chk size), low(offset)
397 .db 0x00,0x00 ;high(offset), fill
398 #endif
399
400
401 ; Copy the dpb data in flash memory, pointed to by Z to temp ram.
402
403 dpb_copy_p:
404 push yh
405 push yl
406 ldi temp2,16
407 ldiw y,tmpdpb
408 cpydpb_pl:
409 lpm temp,z+
410 st y+,temp
411 dec temp2
412 brne cpydpb_pl
413 pop yl
414 pop yh
415 ret
416
417 ; Copy the dpb data, pointed to by Z to temp ram.
418
419 dpb_copy:
420 st y+,temp
421 ldi temp2,15
422 cpydpb_l:
423 ld temp,z+
424 st y+,temp
425 dec temp2
426 brne cpydpb_l
427 ret
428
429
430 ; String compare (z, y), one z-string in flash.
431
432 strcmp_p:
433 lpm _tmp0,z+
434 tst _tmp0
435 breq strcmp_pex
436
437 ld temp, y+
438 lpm _tmp0, z+
439 sub temp,_tmp0
440 brne strcmp_pex
441 tst _tmp0
442 brne strcmp_p
443 strcmp_pex:
444 ret
445
446 ; String compare (x, y, temp2). Max temp2 bytes are compared.
447
448 strncmp_p:
449 subi temp2,1
450 brcs strncmp_peq
451 ld temp,y+
452 lpm _tmp0, z+
453 sub temp,_tmp0
454 brne strncmp_pex
455 tst _tmp0
456 brne strncmp_p
457 strncmp_peq:
458 sub temp,temp
459 strncmp_pex:
460 ret
461
462 ; ====================================================================
463 ; Function: get drive table entry pointer for drive # in temp
464 ; ====================================================================
465 ; Parameters
466 ; --------------------------------------------------------------------
467 ; Registers :
468 ; Variables :
469 ;
470 ; --------------------------------------------------------------------
471 ; Description:
472 ; ====================================================================
473
474 dpb_drvtbl_entry_p:
475
476 ldsw x,biosdrvtbl
477 lsl temp ;drive #
478 add xl,temp
479 adc xh,_0
480 ret
481
482
483 ; ====================================================================
484 ; Function: get drive table entry for drive # in temp
485 ; ====================================================================
486 ; Parameters
487 ; --------------------------------------------------------------------
488 ; Registers :
489 ; Variables :
490 ;
491 ; --------------------------------------------------------------------
492 ; Description:
493 ; ====================================================================
494
495 dpb_drvtbl_entry_get:
496
497 rcall dpb_drvtbl_entry_p
498 ljmp dram_readw_pp
499
500
501 ; ====================================================================
502 ; Function: Clear drive table (entries 0 to 7)
503 ; ====================================================================
504 ; Parameters
505 ; --------------------------------------------------------------------
506 ; Registers :
507 ; Variables :
508 ;
509 ; --------------------------------------------------------------------
510 ; Description:
511 ; ====================================================================
512
513 ;
514
515 dpb_drvtblinit:
516 ldsw x,biosdrvtbl
517 ; adiw x,2
518 ldi temp3,8
519 dpb_drvi_l:
520 ldi temp,0
521 ldi temp2,0
522 rcall dram_writew_pp
523 dec temp3
524 brne dpb_drvi_l
525
526 lds temp,biosenddat
527 lds temp2,biosenddat+1
528 rcall heap_init
529 clr temp
530 ret
531
532 ; ====================================================================
533 ; Function:
534 ; ====================================================================
535 ; Parameters
536 ; --------------------------------------------------------------------
537 ; Registers : temp drive #
538 ;
539 ; --------------------------------------------------------------------
540 ; Description:
541 ; ====================================================================
542
543 dsk_tst_avrcpmhd:
544 ori temp,0xff ; Test, return 'not found' for now.
545 ret
546
547
548 ; ====================================================================
549 ; Function: Test disk format: YAZE
550 ; ====================================================================
551 ; Parameters
552 ; --------------------------------------------------------------------
553 ; Registers : temp drive #
554 ;
555 ; --------------------------------------------------------------------
556 ; Description: From the YAZE Doc:
557 ;
558 ; The new disk header occupies the first 128 BYTES of the file and has the
559 ; new format:
560 ;
561 ; 0 - 9 <CPM_Disk>
562 ; 10 - 15 a null-terminated ascii comment (may be empty)
563 ; new 16 version (0 = yaze-1.06/1.10, 1 = yaze-ag-2.xx)
564 ; 17 - 31 a null-terminated ascii comment (may be empty)
565 ; 32 - 33 sectors per track
566 ; 34 block shift factor
567 ; 35 block mask
568 ; 36 extent mask
569 ; 37 - 38 disk size max
570 ; 39 - 40 directory max
571 ; 41 al0
572 ; 42 al1
573 ; 43 - 44 check size (always zero)
574 ; 45 - 46 track offset
575 ; new 47 psh (used if version=1 and CP/M 3.1 is running)
576 ; new 48 phm ( " " " " " " " " " )
577 ; 49 - 127 unused (zeros)
578 ; ====================================================================
579
580
581 pstrn_YAZE:
582 .db 10,"<CPM_Disk>",0
583
584
585 dsk_tst_yaze:
586
587 ldiw y,hostbuf
588 ldiw z,pstrn_YAZE*2
589 lpm temp2,z+ ; get length
590 rcall strncmp_p
591 brne dsk_tyze_not
592
593 ldiw z,hostbuf+32
594 ldiw y,tmpdpb
595 ldi temp,1 ;1 sector header size
596 rcall dpb_copy
597
598 clr temp
599 ret
600
601 dsk_tyze_not:
602 ori temp,0xff ;Not a YAZE disk image.
603 ret
604
605 ; ====================================================================
606 ; Function: Test disk format: MyZ80
607 ; ====================================================================
608 ; Parameters
609 ; --------------------------------------------------------------------
610 ; Registers : temp drive #
611 ;
612 ; --------------------------------------------------------------------
613 ; Description: Test, if first 2 Sectors are filled with 0xE5,
614 ; and Size = 8192 KB.
615 ; ====================================================================
616 dsk_tst_myz80:
617
618 mov zl,temp3
619 rcall dsk_getpartentry ;get partition entry
620 ldd temp,z+PTAB_SIZE
621 ldd temp2,z+PTAB_SIZE+1 ;check, if (rounded) size is 8192KB
622 lsr temp2
623 ror temp
624 cpi temp,low(8192)
625 ldi temp,high(8192)
626 cpc temp2,temp
627 brne dsk_tmyz80_not ;wrong size
628
629 ldiw z,hostbuf
630 ldi temp2,0
631
632 dsk_tmyz80_loop:
633 ld temp,z+
634 cpi temp,0xE5
635 brne dsk_tmyz80_not
636 dec temp2
637 brne dsk_tmyz80_loop
638
639 clr temp
640 ret
641
642 dsk_tmyz80_not:
643 ori temp,0xff ;Not a MyZ80 hard disk image.
644 ret
645
646 ; ====================================================================
647 ; Function:
648 ; ====================================================================
649 ; Parameters
650 ; --------------------------------------------------------------------
651 ; Registers : temp drive #
652 ;
653 ; --------------------------------------------------------------------
654 ; Description:
655 ; ====================================================================
656
657 dsk_img_type:
658
659 ; todo: flush hostbuf!
660 ; todo: flush hostbuf!
661
662 ; Get first sector (512 byte) of current drive into hostbuf.
663
664 push temp3
665 mov temp3,temp
666 ldi temp,0
667 ldi temp2,0 ;
668 rcall dsk_readhost_lba
669
670 pop temp3
671 push temp3
672
673 ; Test for variable format avrcpmhd.
674
675 rcall dsk_tst_avrcpmhd
676 breq dsk_img_prep
677
678 ; Test for YAZE formats.
679
680 rcall dsk_tst_yaze
681 breq dsk_img_prep
682
683 ; Test for MyZ80 format.
684
685 rcall dsk_tst_myz80
686 ldiw z,dpbdat_myz80*2
687 breq dsk_img_fix
688
689 ; No special image found. Use avrcpm.
690
691 ldiw z,dpbdat_avrcpm*2
692
693 dsk_img_fix:
694 rcall dpb_copy_p
695
696 dsk_img_prep:
697
698 pop temp3
699 mov zl,temp3
700 rcall dsk_getpartentry ;get partition entry
701 ldiw y,tmpdpb
702
703 ; std y+12,_0 ;Test: set check size to 0
704 ; std y+13,_0
705
706 ldd temp,y+0
707 andi temp,~dskType_MASK
708 ldd temp2,z+PTAB_TYPE
709 andi temp2,dskType_MASK
710 or temp,temp2
711 std z+PTAB_TYPE,temp
712 ldd temp,y+1
713 std z+PTAB_SPT,temp
714 ldd temp,y+2
715 tst temp ;more then 256 sectors per track?
716 brne dsk_imgprp_err ;todo: support 16 bit sector numbers
717 ldd temp2,y+3
718 andi temp2,0x0f
719 swap temp2
720 std z+PTAB_BSH,temp2
721
722 ori temp,255
723 ret
724
725 dsk_imgprp_err:
726 printnewline
727 ldi temp,'A'
728 add temp,temp3
729 call uartputc
730 printstring ": Format not supported: Too much sectors per track! "
731 printnewline
732
733 clr temp
734 ret
735
736 ; ----------------------------------------------------------------------
737 ; Init CP/M data structures
738 ; temp = drive #
739 ;
740 ; return !0 if ok
741 ; 0 on error
742
743
744 dpb_diskinit:
745 mov temp3,temp ;save disk #
746
747 rcall dsk_img_type
748 brne dpb_di_0
749 rjmp dpb_di_err
750
751 dpb_di_0:
752
753 ; get mem for DPH
754 ; -----------------------------------------------------------------
755 ; DPH: | XLT | | | |DIRBUF | DPB | CSV | ALV |
756 ; -----------------------------------------------------------------
757 ;(offset) 0 2 4 6 8 10 12 14
758
759 ldi temp, low (16)
760 ldi temp2,high(16)
761 rcall heap_get
762 brne dpb_di_1
763 rjmp dpb_di_err ;
764 dpb_di_1:
765 movw x,temp
766 movw y,temp ;save dph pointer
767 ldi temp,0
768 ldi temp2,0
769 rcall dram_writew_pp ;XLT
770 adiw x,6
771 lds temp,biosdirbuf
772 lds temp2,biosdirbuf+1
773 rcall dram_writew_pp ;DIRBUF
774
775 ; get mem for DPB
776 ; -------------------------------------------------------------
777 ; DPB: | SPT |BSH|BLM|EXM| DSM | DRM |AL0|AL1| CKS | OFF |
778 ; -------------------------------------------------------------
779 ; 0 5 7 11 13
780
781 ldi temp, low (15)
782 ldi temp2,high(15)
783 rcall heap_get
784 breq dpb_di_err_p1
785 movw x,temp
786
787 ldiw z,tmpdpb+1 ;skip sector offset byte
788 dpb_dicpl:
789 ld temp,z+
790 rcall dram_write_pp
791 cpi zl,low(tmpdpb + 16)
792 brne dpb_dicpl
793 sbiw x,15
794 movw temp,x
795 movw x,y
796 adiw x,10
797 rcall dram_writew_pp ;DPB
798
799 ; get mem for dir check vector
800
801 lds temp,tmpdpb+12 ;cks
802 lds temp2,tmpdpb+12+1
803 cp temp,_0
804 cpc temp2,_0
805 breq dpb_dicks0
806 rcall heap_get
807 breq dpb_di_err_p1
808 dpb_dicks0:
809 rcall dram_writew_pp ;CSV
810
811 ; get mem for alloc vector
812
813 lds temp,tmpdpb+6 ;dsm
814 lds temp2,tmpdpb+6+1
815 subi temp, low (-8)
816 sbci temp2,high(-8)
817 lsr temp2
818 ror temp
819 lsr temp2
820 ror temp
821 lsr temp2
822 ror temp ;(dsm+1+7)/8
823 rcall heap_get
824 breq dpb_di_err_p1
825 rcall dram_writew_pp ;ALV
826
827 ; success, insert DPH into drvtbl
828
829 mov temp,temp3
830 rcall dpb_drvtbl_entry_p
831 movw temp,y
832 rcall dram_writew_pp
833
834 ori temp,0xff ;return ok
835 ret
836
837 ; error, free mem
838
839 dpb_di_err_p1:
840 movw temp,y
841 rcall heap_release
842 dpb_di_err:
843 eor temp,temp ;return 0 (+ Z-flag)
844 ret
845
846 ; ====================================================================
847 ; Function:
848 ; ====================================================================
849 ; Parameters
850 ; --------------------------------------------------------------------
851 ; Registers :
852 ;
853 ; --------------------------------------------------------------------
854 ; Description:
855 ; ====================================================================
856 dsk_setdrvparam:
857 ldd temp2,z+PTAB_TYPE
858 andi temp2,~dskType_MASK ;Lower nibble is image header size
859 sts hdrsize,temp2
860 ldd temp2,z+PTAB_SPT
861 sts cpmspt,temp2
862 ldd temp2,z+PTAB_BSH
863 swap temp2
864 andi temp2,0x0f
865 clr _tmp0
866 inc _tmp0
867 dsk_sdrvpl:
868 lsl _tmp0
869 dec temp2
870 brne dsk_sdrvpl
871 sts secpblk,_tmp0
872 ret
873
874
875 ; ====================================================================
876 ; Function: Does a Disk interaction
877 ; ====================================================================
878 ; Parameters
879 ; --------------------------------------------------------------------
880 ; Registers : none
881 ; Variables : [r] seeksec Sector to read
882 ; [r] seektrk Track to read
883 ; --------------------------------------------------------------------
884 ; Description:
885 ; ====================================================================
886 dskDoIt:
887 .if DISK_DEBUG
888 push temp
889 sbrc temp,READ_FUNC
890 rjmp dskdbgr
891 sbrc temp,WRITE_FUNC
892 rjmp dskdbgw
893 rjmp dskdbge
894
895 dskdbgr:
896 printnewline
897 printstring "Disk read: "
898 rjmp dskdbg1
899 dskdbgw:
900 printnewline
901 printstring "Disk write: "
902 dskdbg1:
903 lds temp,seekdsk
904 subi temp,-('A')
905 rcall uartputc
906 printstring ": track "
907 lds temp2,seektrk+1
908 lds temp,seektrk
909 rcall printhexw
910 printstring ", sector "
911 lds temp,seeksec
912 rcall printhex
913 printstring ", dma-addr "
914 lds temp2,dmaadr+1
915 lds temp,dmaadr
916 rcall printhexw
917 pop temp
918 push temp
919 sbrs temp,WRITE_FUNC
920 rjmp dskdbge
921 printstring " wrtype "
922 andi temp,3
923 rcall printhex
924 dskdbge:
925 pop temp
926 .endif
927 ;See what has to be done.
928 sbrc temp,READ_FUNC
929 rjmp dsk_read
930 sbrc temp,WRITE_FUNC
931 rjmp dsk_write
932 sbrc temp,HOME_FUNC
933 rjmp dsk_home
934 sbrc temp,BOOT_FUNC
935 rjmp dsk_boot
936
937 printstring "DISK I/O: Invalid Function code: "
938 rcall printhex
939 rjmp haltinv
940
941 dsk_boot:
942 sts ndisks,_0 ;no active partitions
943 dsk_inval_hostbuf:
944 cbi flags,hostact ;host buffer inactive
945 sts unacnt,_0 ;clear unalloc count
946 ret
947
948 dsk_home:
949 sbis flags,hostwrt ;check for pending write
950 cbi flags,hostact ;clear host active flag
951 ret
952
953
954
955 ; ====================================================================
956 ; Function: Does a Disk read operation
957 ; ====================================================================
958 ; Parameters
959 ; --------------------------------------------------------------------
960 ; Registers : in: temp
961 ; Variables : [r] seekdsk Number of Disk to Read
962 ; [r] seeksec Sector to read
963 ; [r] seektrk Track to read
964 ; --------------------------------------------------------------------
965 ; Description:
966 ; ====================================================================
967 dsk_read:
968 sts erflag,_0
969 sbi flags,readop ; Set read operation flag
970
971 ;RAM disk?
972 lds zl,seekdsk
973 #if RAMDISKCNT
974 cpi zl,RAMDISKNR
975 brlt PC+2
976 rjmp rdsk_read
977 #endif
978 rcall dsk_getpartentry ; Get Paritiontableentry
979 ld temp,z ; Get Partitiontype
980 andi temp,dskType_MASK
981
982 ; Isn't it a Disk ?
983 cpi temp,dskType_None
984 brne PC+2
985 rjmp dsk_read_err
986
987 ; It must be a FAT16-Imagefile or CP/M Partition.
988
989 rcall dsk_setdrvparam ;todo: do this only if needed (disk change)
990
991 sts unacnt,_0
992 sbi flags,rsflag ;must read data
993 ldi temp,WRUAL ;write type
994 sts wrtype,temp ;treat as unalloc
995
996 rjmp dsk_rwoper ;to perform the read
997
998 dsk_read_err:
999 ret
1000
1001 ; ====================================================================
1002 ; Function: Does a Disk write operation
1003 ; ====================================================================
1004 ; Parameters
1005 ; --------------------------------------------------------------------
1006 ; Registers : in: temp Write type
1007 ; Variables : [r] seekdsk Number of Disk to Read
1008 ; [r] seeksec Sector to read
1009 ; [r] seektrk Track to read
1010 ; --------------------------------------------------------------------
1011 ; Description:
1012 ; ====================================================================
1013 dsk_write:
1014 ;write the selected sector
1015 sts erflag,_0
1016 cbi flags,readop ; not a read operation
1017 ;RAM disk?
1018 lds zl,seekdsk
1019 #if RAMDISKCNT
1020 cpi zl,RAMDISKNR
1021 brlt PC+2
1022 rjmp rdsk_write
1023 #endif
1024 rcall dsk_getpartentry ; Get Paritiontableentry
1025 ld temp2,z ; Get Partitiontype
1026 andi temp,dskType_MASK
1027
1028 ; Isn't it a Disk ?
1029 cpi temp2,dskType_None
1030 brne PC+2
1031 rjmp dsk_write_err
1032
1033
1034 ; It must be a FAT16-Imagefile or CP/M Partition.
1035
1036 rcall dsk_setdrvparam ;todo: do this only if needed (disk change)
1037
1038 cbi flags,readop ;not a read operation
1039
1040 andi temp,WRTMSK
1041 sts wrtype,temp ;save write type
1042
1043 cpi temp,WRUAL ;write unallocated?
1044 brne dsk_chkuna ;check for unalloc
1045
1046 ; write to unallocated, set parameters
1047 lds temp,secpblk ;next unalloc recs (blocksize/128)
1048 sts unacnt,temp
1049 lds temp,seekdsk ;disk to seek
1050 sts unadsk,temp ;unadsk = sekdsk
1051 lds temp,seektrk
1052 sts unatrk,temp ;unatrk = sectrk
1053 lds temp,seektrk+1
1054 sts unatrk+1,temp ;unatrk = sectrk
1055 lds temp,seeksec
1056 sts unasec,temp ;unasec = seksec
1057 ;
1058 dsk_chkuna:
1059 ;check for write to unallocated sector
1060 lds temp,unacnt ;any unalloc remain?
1061 tst temp
1062 breq dsk_alloc ;skip if not
1063
1064 ; more unallocated records remain
1065 dec temp ;unacnt = unacnt-1
1066 sts unacnt,temp
1067 lds temp,seekdsk ;same disk?
1068 lds temp2,unadsk
1069 cp temp,temp2 ;seekdsk = unadsk?
1070 brne dsk_alloc ;skip if not
1071
1072 ; disks are the same
1073 lds temp,unatrk
1074 lds temp2,unatrk+1
1075 lds temp3,seektrk
1076 lds temp4,seektrk+1
1077 cp temp,temp3 ;seektrk = unatrk?
1078 cpc temp2,temp4
1079 brne dsk_alloc ;skip if not
1080
1081 ; tracks are the same
1082 lds temp,seeksec ;same sector?
1083 lds temp2,unasec
1084 cp temp,temp2 ;seeksec = unasec?
1085 brne dsk_alloc ;skip if not
1086
1087 ; match, move to next sector for future ref
1088 inc temp2 ;unasec = unasec+1
1089 sts unasec,temp2
1090 lds _tmp0,cpmspt
1091 cp temp2,_tmp0 ;end of track? (count CP/M sectors)
1092 brlo dsk_noovf ;skip if no overflow
1093
1094 ; overflow to next track
1095 sts unasec,_0 ;unasec = 0
1096 lds temp,unatrk
1097 lds temp2,unatrk+1
1098 subi temp, low(-1) ;unatrk = unatrk+1
1099 sbci temp2,high(-1)
1100 sts unatrk,temp
1101 sts unatrk+1,temp2
1102 ;
1103 dsk_noovf:
1104 cbi flags,rsflag ;rsflag = 0
1105 rjmp dsk_rwoper ;to perform the write
1106 ;
1107 dsk_alloc:
1108 ;not an unallocated record, requires pre-read
1109 sts unacnt,_0 ;unacnt = 0
1110 sbi flags,rsflag ;rsflag = 1
1111 rjmp dsk_rwoper
1112
1113 dsk_write_err:
1114 ret
1115
1116 ; ====================================================================
1117 ; Function: Does a Disk read/write operation
1118 ; ====================================================================
1119 ; Parameters
1120 ; --------------------------------------------------------------------
1121 ; Registers : none
1122 ; Variables : [r] seekdsk Number of Disk to Read
1123 ; [r] seeksec Sector to read
1124 ; [r] seektrk Track to read
1125 ; --------------------------------------------------------------------
1126 ; Description:
1127 ; ====================================================================
1128 dsk_rwoper:
1129 ;enter here to perform the read/write
1130 .if DISK_DEBUG
1131 printstring ", flags: "
1132 in temp,flags
1133 rcall printhex
1134 .endif
1135 sts erflag,_0 ;no errors (yet)
1136
1137 ;Convert track/sector to an LBA address (in 128byte blocks)
1138
1139 lds xl,seeksec ;
1140 ldi xh,0 ;
1141 ldi yl,0 ;
1142 lds temp,hdrsize ;add image header size
1143 add xl,temp ;
1144 adc xh,_0 ;
1145 lds temp3,seektrk ;
1146 lds temp4,seektrk+1 ;
1147 lds temp,cpmspt ;
1148 mul temp3,temp ;
1149 add xl,r0 ;
1150 adc xh,r1 ;
1151 mul temp4,temp ;
1152 add xh,r0 ;yl:xh:xl := sec + trk * SectorsPerTrack
1153 adc yl,r1 ;
1154
1155 mov temp,xl
1156 andi temp,SECMSK ;mask buffer number
1157 push temp ;save for later
1158
1159 ;Convert from CP/M LBA blocks to host LBA blocks
1160 ldi temp,SECSHF
1161 dsk_sh1:
1162 lsr yl
1163 ror xh
1164 ror xl
1165 dec temp
1166 brne dsk_sh1
1167 ;yl:xh:xl = host block to seek
1168 ; active host sector?
1169 in _tmp0,flags ;host active flag
1170 sbi flags,hostact ;always becomes 1
1171 sbrs _tmp0,hostact ;was it already?
1172 rjmp dsk_filhst ;fill host if not
1173
1174 ; host buffer active, same as seek buffer?
1175 lds temp,seekdsk
1176 lds temp2,hostdsk ;same disk?
1177 cp temp,temp2 ;seekdsk = hostdsk?
1178 brne dsk_nomatch
1179
1180 ; same disk, same block?
1181 lds temp,hostlba
1182 lds temp2,hostlba+1
1183 lds temp3,hostlba+2
1184 cp xl,temp
1185 cpc xh,temp2
1186 cpc yl,temp3
1187 breq dsk_match
1188 ;
1189 dsk_nomatch:
1190 ;proper disk, but not correct sector
1191 sbis flags,hostwrt ;host written?
1192 rjmp dsk_filhst
1193 push xl
1194 push xh
1195 push yl
1196 rcall dsk_writehost ;clear host buff
1197 pop yl
1198 pop xh
1199 pop xl
1200
1201 dsk_filhst:
1202 ;may have to fill the host buffer
1203 lds temp,seekdsk
1204 sts hostdsk,temp
1205 sts hostlba,xl
1206 sts hostlba+1,xh
1207 sts hostlba+2,yl
1208
1209 sbic flags,rsflag ;need to read?
1210 rcall dsk_readhost ;yes, if 1
1211 cbi flags,hostwrt ;no pending write
1212
1213 dsk_match:
1214
1215 ;copy data to or from buffer
1216 ldiw z,hostbuf
1217 ldi temp,128
1218 pop temp2 ;get buffer number (which part of hostbuf)
1219 mul temp2,temp
1220 add zl,r0 ;offset in hostbuf
1221 adc zh,r1
1222
1223 .if DISK_DEBUG > 2
1224 push r0
1225 push r1
1226 printstring "; host buf adr: "
1227 pop temp2
1228 pop temp
1229 rcall printhexw
1230 .endif
1231
1232 lds xl,dmaadr
1233 lds xh,dmaadr+1
1234 ldi temp3,128 ;length of move
1235 sbic flags,readop ;which way?
1236 rjmp dsk_rmove ;skip if read
1237
1238 ; mark write operation
1239 sbi flags,hostwrt ;hostwrt = 1
1240 dsk_wmove:
1241 mem_read
1242 st z+,temp
1243 adiw xl,1
1244 dec temp3
1245 brne dsk_wmove
1246 rjmp dsk_rwmfin
1247
1248 dsk_rmove:
1249 ld temp,z+
1250 mem_write
1251 adiw xl,1
1252 dec temp3
1253 brne dsk_rmove
1254 dsk_rwmfin:
1255 ; data has been moved to/from host buffer
1256 lds temp,wrtype ;write type
1257 cpi temp,WRDIR ;to directory?
1258 breq dsk_wdir
1259 ret ;no further processing
1260 dsk_wdir:
1261 ; clear host buffer for directory write
1262 lds temp,erflag
1263 tst temp ;errors?
1264 breq dsk_wdir1
1265 ret ;skip if so
1266 dsk_wdir1:
1267 rcall dsk_writehost ;clear host buff
1268 cbi flags,hostwrt ;buffer written
1269 ret
1270
1271 ; ====================================================================
1272 ; Function:
1273 ; ====================================================================
1274 ; Parameters
1275 ; --------------------------------------------------------------------
1276 ; Registers : temp2:temp block to read (lba)
1277 ; temp3 disk #
1278 ;
1279 ; --------------------------------------------------------------------
1280 ; Description:
1281 ; ====================================================================
1282 dsk_readhost_lba:
1283 sts hostlba+0,temp
1284 sts hostlba+1,temp2
1285 sts hostlba+2,_0
1286 sts hostdsk,temp3
1287 #if 0
1288 printnewline
1289 printstring "readhst lba"
1290 #endif
1291 rjmp dsk_readhost ;returns 0, if ok
1292
1293
1294 ; ====================================================================
1295 ; Function: Does a Disk write operation
1296 ; ====================================================================
1297 ; Parameters
1298 ; --------------------------------------------------------------------
1299 ; Registers : none
1300 ; Variables : [r] seekdsk Number of Disk to Read
1301 ; [r] seeksec Sector to read
1302 ; [r] seektrk Track to read
1303 ; --------------------------------------------------------------------
1304 ; Description:
1305 ; ====================================================================
1306 dsk_writehost:
1307 lds zl,hostdsk
1308 rcall dsk_getpartentry
1309 ld temp,z
1310 andi temp,dskType_MASK
1311
1312 #if FAT16_SUPPORT
1313 ; Is it a FAT16 Diskimage ?
1314 cpi temp,dskType_FAT
1315 brne PC+2
1316 rjmp fat_writehost
1317 #endif
1318
1319 ; Is it a CP/M Partition ?
1320 cpi temp,dskType_CPM
1321 brne PC+2
1322 rjmp cpm_writehost
1323 ; Disktype not supported -> Return
1324 ret
1325
1326 ; ====================================================================
1327 ; Function: Does a Disk read operation
1328 ; ====================================================================
1329 ; Parameters
1330 ; --------------------------------------------------------------------
1331 ; Registers : none
1332 ; Variables : [r] seekdsk Number of Disk to Read
1333 ; [r] seeksec Sector to read
1334 ; [r] seektrk Track to read
1335 ; --------------------------------------------------------------------
1336 ; Description:
1337 ; ====================================================================
1338 dsk_readhost:
1339
1340 #if 0
1341 printnewline
1342 printstring "readhost"
1343 ldiw z,biosdrvtbl
1344 rcall dbg_hexdump_line
1345 adiw z,16
1346 rcall dbg_hexdump_line
1347 #endif
1348
1349 lds zl,hostdsk
1350 rcall dsk_getpartentry
1351 ld temp,z
1352 andi temp,dskType_MASK
1353
1354 #if FAT16_SUPPORT
1355 ; Is it a FAT16 Diskimage ?
1356 cpi temp,dskType_FAT
1357 brne PC+2
1358 rjmp fat_readhost
1359 #endif
1360
1361 ; Is it a CP/M Partition ?
1362 cpi temp,dskType_CPM
1363 brne PC+2
1364 rjmp cpm_readhost
1365 ; Disktype not supported -> Return
1366 ret
1367