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