]> cloudbase.mooo.com Git - avrcpm.git/blob - avrcpm/avr/dsk_fat16.asm
ac4b2e8b795259218f469f8dd10395876e44a2e1
[avrcpm.git] / avrcpm / avr / dsk_fat16.asm
1 ; Various functions for the Interaction with the FAT16 Filesystem
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 ; ===========================================================================
24 ; Prelimitary !
25 ; °°°°°°°°°°°°°
26 ; Size of a Sector is fixed to 512 Bytes by Base - MMC Driver implementation
27 ; The Functions below therefore assume a fixed Size of 512 Bytes per Sector.
28 ; ===========================================================================
29
30 #if FAT16_SUPPORT
31
32
33 ;-------------------------------- Defines for FAT16 Structures
34 #define PARTID_FAT16 0x0E
35
36 ;#define FAT16_BSO_SECSIZE 0x0b ; BootSectorOffset to Sectorsize Word
37 #define FAT16_BSO_CLUSTSZ 0x0d ; BootSectorOffset to Clustersize Byte
38 #define FAT16_BSO_RESSECT 0x0e ; BootSectorOffset to Number of Reserved Sectors
39 #define FAT16_BSO_VOLPTR 0x1c ; BootSectorOffset to First VolumeSector
40 #define FAT16_BSO_SECPERFAT 0x16 ; BootSectorOffset to Number of Sectors per Fat
41 #define FAT16_BSO_NUMFATCP 0x10 ; BootSectorOffset to Ammount of FAT Copys
42 #define FAT16_BSO_NUMDIRENT 0x11 ; BootSectorOffset to Max. Root Dir. Entrys
43 #define FAT16_FIRST_IMAGENAME 'A'
44 #define FAT16_LAST_IMAGENAME 'Z'
45
46 ;-------------------------------- Start of Data Segment
47
48 .dseg
49
50 fat_partfound: .byte 1 ; (0= no fat partition found 1=found partition)
51 fat_parttbl: .byte 8 ; first fat16 partition entry (start sector, sector count)
52 ;fat_sectorsize: .byte 2 ; size of sector in bytes
53 fat_clustersize: .byte 1 ; sectors per cluster
54 fat_ressectors: .byte 2 ; number of reserved sectors
55 fat_secperfat: .byte 2 ; number of sectors per fat
56 fat_numfatcp: .byte 1 ; Number of FAT Copies
57 fat_numdirentrys:.byte 2 ; Max. ammount of Directory Entrys within Rootdirektory
58 fat_ptr2fat: .byte 4 ; pointer to the first fat sector
59 fat_ptr2dir: .byte 4 ; pointer to the first root directory sector
60 fat_ptr2dat: .byte 4 ; pointer to the first data sector
61
62 fat_last_dsk: .byte 1 ; number of disk with entry in cache
63 fat_log_clust: .byte 2 ; last searched logical cluster
64 fat_clust_offset: .byte 1 ; offset within the cluster
65 fat_clust_ptr: .byte 4; ; sector of last real cluster
66 ; ------------------------------- Start of Code Segment
67 .cseg
68
69 ; ====================================================================
70 ; Function: Does a Disk read/write operation
71 ; ====================================================================
72 ; Parameters
73 ; --------------------------------------------------------------------
74 ; Registers : none
75 ; Variables : [r] seekdsk Number of Disk to Read
76 ; [r] seeksec Sector to read
77 ; [r] seektrk Track to read
78 ; --------------------------------------------------------------------
79 ; Description:
80 ; ====================================================================
81 fat_init_partitiontable:
82
83 sts fat_partfound,_0
84
85 ldiw y,fat_parttbl
86 st y+,_0
87 st y+,_0
88 st y+,_0
89 st y+,_0
90 st y+,_0
91 st y+,_0
92 st y+,_0
93 st y+,_0
94 ret
95
96 ; ====================================================================
97 ; Function: Resets the Cache
98 ; ====================================================================
99 ; Parameters
100 ; --------------------------------------------------------------------
101 ; Registers : none
102 ; Variables : [r] seekdsk Number of Disk to Read
103 ; [r] seeksec Sector to read
104 ; [r] seektrk Track to read
105 ; --------------------------------------------------------------------
106 ; Description:
107 ; ====================================================================
108 fat_reset_cache:
109 push yl
110 ldi yl,0xFF
111 sts fat_log_clust ,yl
112 sts fat_log_clust+1,yl
113 sts fat_last_dsk ,yl
114 pop yl
115 ret
116
117 ; ====================================================================
118 ; Function: Add's a FAT16 Partition for later Scanning
119 ; ====================================================================
120 ; Parameters
121 ; --------------------------------------------------------------------
122 ; Registers :
123 ; Variables :
124 ; --------------------------------------------------------------------
125 ; Description:
126 ; This funktion sets the internal Variables to set Start and Size
127 ; of a given FAT16 Paritition. This Information will be used for a
128 ; later scanning of the Partition. See Function "fat_scan_partition"
129 ; for more information.
130 ; ====================================================================
131 fat_add_partition:
132
133 .if FAT16_DEBUG > 0
134 printstring "fat16 part found",0
135 printnewline
136 .endif
137
138
139 ; save variables on stack
140 push yl
141 push yh
142
143 ; set fat16 partition found flag
144 ldi yl,1
145 sts fat_partfound,yl
146
147 ; save data from first fat16 partition
148 ldiw y,fat_parttbl
149
150 ldd temp,z+PART_START
151 st y+,temp
152 ldd temp,z+PART_START+1
153 st y+,temp
154 ldd temp,z+PART_START+2
155 st y+,temp
156 ldd temp,z+PART_START+3
157 st y+,temp
158
159 ldd temp,z+PART_SIZE
160 st y+,temp
161 ldd temp,z+PART_SIZE+1
162 st y+,temp
163 ldd temp,z+PART_SIZE+2
164 st y+,temp
165 ldd temp,z+PART_SIZE+3
166 st y+,temp
167
168
169 ; reload variables from stack
170 pop yh
171 pop yl
172
173 ret
174
175 ; ---------------------------------------------------------------------------
176 ; Read and Scann a FAT16 Partition for Imagedatefiles
177 ; ---------------------------------------------------------------------------
178 ; This Routine reads the Bootblock and scanns it for a Diskimage
179 ; Input Registers : none
180 ; Output Registers : none
181 ; Changes Variables: none
182 ; ---------------------------------------------------------------------------
183
184
185 fat_scan_partition:
186
187 .if FAT16_DEBUG > 0
188 printstring "fat16 scanning",0
189 printnewline
190 .endif
191
192 ; Check if a FAT16 Partition was realy found
193 lds yl,fat_partfound
194 cpi yl,1
195 brne fat_scan_error
196
197
198 .if FAT16_DEBUG > 0
199 printstring "free entrys in ptable ?",0
200 printnewline
201 .endif
202
203 ; Check for free Entrys in Partition table
204 lds yl,ndisks
205 cpi yl,MAXDISKS
206 breq fat_scan_error
207
208 .if FAT16_DEBUG > 0
209 printstring "read fat bootblock.",0
210 printnewline
211 .endif
212
213 ; Scan partition start
214 ldiw z,fat_parttbl
215 ldd xl,z+0
216 ldd xh,z+1
217 ldd yl,z+2
218 ldd yh,z+3
219
220 ; Load first sector from Partition
221 rcall mmcReadSect
222 tst temp
223 breq fat_bootblock_check
224
225 ; Read error: Block not found
226 fat_scan_error:
227 clr temp
228 ret
229
230 fat_bootblock_check:
231
232 .if FAT16_DEBUG > 0
233 printstring "fat16 bootblock check",0
234 printnewline
235 .endif
236
237 ; -> Size of Sectors fixed at 512 Bytes
238 ; Get ammount of Bytes per Sector
239 ; ldiw z,hostbuf+FAT16_BSO_SECSIZE
240 ; ldiw y,fat_sectorsize
241 ; ld temp,z
242 ; st y+, temp
243 ; ldd temp2,z+1
244 ; st y , temp2
245 ;
246 ;.if FAT16_DEBUG > 0
247 ; printstring "Bytes per Sector ",0
248 ; rcall printhexw
249 ; printnewline
250 ;.endif
251
252 ; Anzahl der Sectoren pro Cluster lesen
253 ldiw z,hostbuf+FAT16_BSO_CLUSTSZ
254 ld temp,z
255 sts fat_clustersize,temp
256
257 .if FAT16_DEBUG > 0
258 printstring "Sectors per Cluster ",0
259 rcall printhex
260 printnewline
261 .endif
262
263 ; Anzahl der reservierten Sectoren
264 ldiw z,hostbuf+FAT16_BSO_RESSECT
265 ld temp,z+
266 sts fat_ressectors,temp ; low byte
267 ld temp2,z
268 sts fat_ressectors+1,temp2 ; high byte
269
270 .if FAT16_DEBUG > 0
271 printstring "Reserved Sectors__: ",0
272 rcall printhexw
273 printnewline
274 .endif
275
276 ; Anzahl der Sectoren pro FAT
277 ldiw z,hostbuf+FAT16_BSO_SECPERFAT
278 ld temp,z+
279 sts fat_secperfat,temp ; low byte
280 ld temp2,z
281 sts fat_secperfat+1,temp2 ; high byte
282
283 .if FAT16_DEBUG > 0
284 printstring "Sectors per FAT__: ",0
285 rcall printhexw
286 printnewline
287 .endif
288
289 ; Anzahl der FAT kopien
290 ldiw z,hostbuf+FAT16_BSO_NUMFATCP
291 ld temp,z
292 sts fat_numfatcp,temp ; low byte
293
294 .if FAT16_DEBUG > 0
295 printstring "Ammount of FAT copies: ",0
296 rcall printhex
297 printnewline
298 .endif
299
300 ; Max. Anzahl der Dir. Enträge im Root Verz.
301 ldiw z,hostbuf+FAT16_BSO_NUMDIRENT
302 ld temp,z+
303 sts fat_numdirentrys,temp ; low byte
304 ld temp2,z
305 sts fat_numdirentrys+1,temp2 ; high byte
306
307 .if FAT16_DEBUG > 0
308 printstring "Max. entrys in Rootdir.: ",0
309 rcall printhexw
310 printnewline
311 .endif
312
313 ; Print begin of Volume
314 .if FAT16_DEBUG > 1
315
316 ldiw z,fat_parttbl
317 ldd xl,z+0
318 ldd xh,z+1
319 ldd yl,z+2
320 ldd yh,z+3
321
322 printstring "Begin of Volume at: ",0
323 mov temp ,yl
324 mov temp2,yh
325 rcall printhexw
326 mov temp ,xl
327 mov temp2,xh
328 rcall printhexw
329 printnewline
330 .endif
331
332 ; Calculate begin of FAT within the Volume
333 lds temp ,fat_ressectors
334 lds temp2,fat_ressectors+1
335
336 ldiw z,fat_parttbl
337 ldd xl,z+0
338 ldd xh,z+1
339 ldd yl,z+2
340 ldd yh,z+3
341
342 add xl,temp
343 adc xh,temp2
344 adc yl,_0
345 adc yh,_0
346
347 sts fat_ptr2fat ,xl
348 sts fat_ptr2fat+1,xh
349 sts fat_ptr2fat+2,yl
350 sts fat_ptr2fat+3,yh
351
352 .if FAT16_DEBUG > 1
353 printstring "Begin of FAT at___: ",0
354 mov temp ,yl
355 mov temp2,yh
356 rcall printhexw
357 mov temp ,xl
358 mov temp2,xh
359 rcall printhexw
360 printnewline
361 .endif
362
363 ; Calculate begin of Root- Directory within the Volume
364 ldiw z,fat_ptr2fat
365 ldd xl,z+0
366 ldd xh,z+1
367 ldd yl,z+2
368 ldd yh,z+3
369
370 lds temp ,fat_secperfat
371 lds temp2,fat_secperfat+1
372 lds temp3,fat_numfatcp
373
374 fat_calc_dp_loop:
375 cp temp3,_0
376 breq fat_calc_dp_lend
377
378 add xl,temp
379 adc xh,temp2
380 adc yl,_0
381 adc yh,_0
382
383 dec temp3
384
385 jmp fat_calc_dp_loop
386 fat_calc_dp_lend:
387
388 sts fat_ptr2dir ,xl
389 sts fat_ptr2dir+1,xh
390 sts fat_ptr2dir+2,yl
391 sts fat_ptr2dir+3,yh
392
393
394 .if FAT16_DEBUG > 1
395 printstring "Begin of DIR at___: ",0
396 mov temp ,yl
397 mov temp2,yh
398 rcall printhexw
399 mov temp ,xl
400 mov temp2,xh
401 rcall printhexw
402 printnewline
403 .endif
404
405 ; Calculate begin of DATA Clusters within the Volume
406 ; Num. Dir.Sektors = (Num. of Dir. Entrys * 32) / Bytes per Sektor
407
408 ; Sectorsize is fixed at 512 Bytes, makes 16 Entrys per Sektor
409
410 lds zl,fat_numdirentrys ; low byte
411 lds zh,fat_numdirentrys+1 ; high byte
412
413 ; Num. Direntrys / 16
414 lsr zh
415 ror zl
416 lsr zh
417 ror zl
418 lsr zh
419 ror zl
420 lsr zh
421 ror zl
422
423 lds xl,fat_ptr2dir
424 lds xh,fat_ptr2dir+1
425 lds yl,fat_ptr2dir+2
426 lds yh,fat_ptr2dir+3
427
428 add xl,zl
429 adc xh,zh
430 adc yl,_0
431 adc yh,_0
432
433 sts fat_ptr2dat ,xl
434 sts fat_ptr2dat+1,xh
435 sts fat_ptr2dat+2,yl
436 sts fat_ptr2dat+3,yh
437
438 .if FAT16_DEBUG > 1
439 printstring "Begin of Data at__: ",0
440 mov temp ,yl
441 mov temp2,yh
442 rcall printhexw
443 mov temp ,xl
444 mov temp2,xh
445 rcall printhexw
446 printnewline
447 .endif
448
449 ; Here Starts the Scann of the Directory for valid image Files.
450
451 ; Init Image-Namecounter
452 ldi temp,FAT16_FIRST_IMAGENAME
453 sts fat_last_dsk,temp
454
455 fat_scan_for_next_image:
456
457 ; Init Offset into Directory-Sectors
458 ldi temp,0
459 sts fat_clust_offset,temp
460
461 ; Init counter for number of entry left to scan
462 lds temp,fat_numdirentrys
463 sts fat_log_clust ,temp
464
465 lds temp,fat_numdirentrys+1
466 sts fat_log_clust+1,temp
467
468 fat_next_sector_loop:
469 ; Get a Pointer to the first Directory sector
470 lds xl,fat_ptr2dir
471 lds xh,fat_ptr2dir+1
472 lds yl,fat_ptr2dir+2
473 lds yh,fat_ptr2dir+3
474
475 ; Add actual offset
476 lds temp,fat_clust_offset
477 add xl,temp
478 adc xh,_0
479 adc yl,_0
480 adc yh,_0
481
482 ; Load sector from Directory
483 call mmcReadSect
484 tst temp
485 breq fat_look_for_images
486
487 ; Read error: Block not found
488 clr temp
489 ret
490
491 ; Looks at a read directory block for image entrys
492 fat_look_for_images:
493
494 ldiw z,hostbuf
495 ldi temp2,0
496
497 fat_look_for_loop:
498 ldd temp,z+0
499 cpi temp,'C'
500 brne fat_look_not_ok
501
502 ldd temp,z+1
503 cpi temp,'P'
504 brne fat_look_not_ok
505
506 ldd temp,z+2
507 cpi temp,'M'
508 brne fat_look_not_ok
509
510 ldd temp,z+3
511 cpi temp,'D'
512 brne fat_look_not_ok
513
514 ldd temp,z+4
515 cpi temp,'S'
516 brne fat_look_not_ok
517
518 ldd temp,z+5
519 cpi temp,'K'
520 brne fat_look_not_ok
521
522 ldd temp,z+6
523 cpi temp,'_'
524 brne fat_look_not_ok
525
526 lds temp3,fat_last_dsk ; Get actual Diskname (A to Z)
527 ldd temp,z+7
528 cp temp,temp3
529 brne fat_look_not_ok
530 ldd temp,z+8
531 cpi temp,'I'
532 brne fat_look_not_ok
533
534 ldd temp,z+9
535 cpi temp,'M'
536 brne fat_look_not_ok
537
538 ldd temp,z+10
539 cpi temp,'G'
540 brne fat_look_not_ok
541
542 sts fat_clust_ptr ,zl
543 sts fat_clust_ptr+1,zh
544 sts fat_clust_ptr+2,temp2
545 jmp fat_store_new_entry
546
547 fat_scan_for_more:
548
549
550 .if FAT16_DEBUG > 1
551 printstring "At:Scann for more ",0
552 printnewline
553 .endif
554
555 lds zl ,fat_clust_ptr
556 lds zh ,fat_clust_ptr+1
557 lds temp2,fat_clust_ptr+2
558 fat_look_not_ok:
559
560 .if FAT16_DEBUG > 1
561 printstring "+",0
562 .endif
563
564 adiw z,32
565
566 inc temp2
567 cpi temp2,16 ; max entrys/sector
568 breq fat_scan_next_sector
569 jmp fat_look_for_loop
570
571 fat_scan_next_sector:
572
573 .if FAT16_DEBUG > 1
574 printstring "At:Scan next Sector",0
575 printnewline
576 .endif
577
578 lds temp3, fat_log_clust
579 lds temp4, fat_log_clust+1
580
581 sub temp3,temp2
582 sbc temp4,_0
583
584 sts fat_log_clust,temp3
585 sts fat_log_clust+1,temp4
586
587 cp temp3,_0
588 cpc temp4,_0
589 breq fat_scan_at_end
590
591 lds temp,fat_clust_offset
592 inc temp
593 sts fat_clust_offset,temp
594
595 rjmp fat_next_sector_loop
596
597 fat_scan_at_end:
598
599 .if FAT16_DEBUG > 1
600 printstring "At:Scan at end",0
601 printnewline
602 .endif
603
604 lds temp,fat_last_dsk
605 inc temp
606 sts fat_last_dsk,temp
607
608 ldi temp2,FAT16_LAST_IMAGENAME
609 cp temp,temp2
610 brge fat_scaned_last_disk
611
612 rjmp fat_scan_for_next_image
613
614 fat_scaned_last_disk:
615
616 rjmp fat_scan_end
617
618
619 ; Create new Partition Entry
620 fat_store_new_entry:
621
622 ; Found a valid image
623 .if FAT16_DEBUG > 1
624 printstring "Found a valid Image ! Z=",0
625 mov temp ,zl
626 mov temp2,zh
627 rcall printhexw
628 printnewline
629 .endif
630
631 ldiw y,hostparttbl
632 lds temp,ndisks
633
634 fat_look_store_loop:
635 cp temp,_0
636 breq fat_look_store
637
638 adiw y,PARTENTRY_SIZE
639 dec temp
640 jmp fat_look_store_loop
641
642 fat_look_store:
643 ; Set Type of Partition to FAT16- Fileimage
644 ldi temp,dskType_FAT
645 st y+,temp
646
647 .if FAT16_DEBUG > 1
648 printstring "ClusterID",0
649 .endif
650 ; Offset to Startcluster + 2
651 ldd temp,z+0x1A
652 st y+,temp
653 .if FAT16_DEBUG > 1
654 rcall printhex
655 .endif
656 ldd temp,z+0x1B
657 st y+,temp
658 .if FAT16_DEBUG > 1
659 rcall printhex
660 .endif
661 ldi temp,0
662 st y+,temp
663 st y+,temp
664 .if FAT16_DEBUG > 1
665 printnewline
666 .endif
667
668 ; Filesize in Bytes
669 ; ldd temp,z+0x1C
670 ; st y+,temp
671 ; ldd temp,z+0x1D
672 ; st y+,temp
673 ; ldd temp,z+0x1E
674 ; st y+,temp
675 ; ldd temp,z+0x1F
676 ; st y+,temp
677
678 ; Convert Filesize to ammount of sectors
679 ; (calc with 512byte/sector)
680 ldd xl,z+0x1D
681 ldd xh,z+0x1E
682 ldd zl,z+0x1F
683 mov zh,_0
684
685 lsr zh
686 ror zl
687 ror xh
688 ror xl
689
690 ; store ammount of sectors in partitiontable
691 st y+,xl
692 st y+,xh
693 st y+,zl
694 st y+,zh
695
696 .if FAT16_DEBUG > 1
697 ; Test finding of the first sector
698
699 ldd xl,z+0x1A
700 ldd xh,z+0x1B
701
702 rcall fat_gethostsec
703
704 printstring "Begin of Image at: ",0
705 mov temp ,yl
706 mov temp2,yh
707 rcall printhexw
708 mov temp ,xl
709 mov temp2,xh
710 rcall printhexw
711 printnewline
712
713 .endif
714 ; Check for another free entry in partition table
715 lds temp,ndisks
716 inc temp
717 sts ndisks,temp
718
719 cpi temp,MAXDISKS
720 breq fat_scan_end
721
722 jmp fat_scan_for_more
723
724 fat_scan_end:
725
726 ret
727
728
729 ; ====================================================================
730 ; Function: Cluster to HostSector
731 ; ====================================================================
732 ; Parameters: [in] xh,xl Cluster Number
733 ; [out] yh,yl,xh,xl Sector Number on Disk
734 ; --------------------------------------------------------------------
735 ; Registers :
736 ; Variables : [used] fat_clustersize Ammount of Sectors per Cluster
737 ; [changes] temp
738 ; --------------------------------------------------------------------
739 ; Description:
740 ; ! Only works with Clustersizes 2,4,8,16,32,64,128 !
741 ; ====================================================================
742 fat_gethostsec:
743
744 ; Get Offset into Data area of Disk
745 rcall fat_clusttosec
746
747
748 ; add begin of data area to offset
749 lds temp,fat_ptr2dat+0
750 add xl,temp
751 lds temp,fat_ptr2dat+1
752 adc xh,temp
753 lds temp,fat_ptr2dat+2
754 adc yl,temp
755 lds temp,fat_ptr2dat+3
756 adc yh,temp
757 ret
758
759 ; ====================================================================
760 ; Function: Cluster to Sector
761 ; ====================================================================
762 ; Parameters: [in] xl,xh Cluster Number
763 ; [out] xl,xh,yl,yh Sector Number
764 ; --------------------------------------------------------------------
765 ; Registers :
766 ; Variables : [used] fat_clustersize Ammount of Sectors per Cluster
767 ; [changes] temp
768 ; --------------------------------------------------------------------
769 ; Description:
770 ; ! Only works with Clustersizes 2,4,8,16,32,64,128 !
771 ; ====================================================================
772 fat_clusttosec:
773 clr yl
774 clr yh
775
776 ldi temp,2
777 sub xl,temp ; Substract the 2 reserved clusters
778 sbc xh,_0
779
780 lds temp,fat_clustersize
781 lsr temp
782
783 fat_c2s_loop:
784 tst temp
785 breq fat_c2s_end
786 lsr temp
787
788 lsl xl
789 rol xh
790 rol yl
791 rol yh
792 rjmp fat_c2s_loop
793
794 fat_c2s_end:
795 ret
796
797 ; ====================================================================
798 ; Function: Searches a physical Cluster, given the logical Cluster
799 ; ====================================================================
800 ; Parameters
801 ; --------------------------------------------------------------------
802 ; Registers : [r] xh,xl logical- Cluster
803 ; [w] yh,yl physical- Cluster
804 ; --------------------------------------------------------------------
805 ; Description:
806 ; ====================================================================
807 fat_find_phsy_clust:
808 mov temp2,xl
809 lds xl,hostdsk
810
811 rcall dsk_getpartentry ; get partition entry
812 mov xl,temp2
813
814 ; Get First FAT- Cluster Number of Diskimage
815
816 ldd yl,z+1
817 ldd yh,z+2
818
819 .if FAT16_DBG_FAT > 0
820 printstring "Search log. Cluster ",0
821 mov temp,xl
822 mov temp2,xh
823 rcall printhexw
824 printnewline
825
826 printstring "Search phys. Cluster ",0
827 mov temp ,yl
828 mov temp2,yh
829 rcall printhexw
830 printnewline
831 .endif
832
833 fat_next_phsy_clust:
834 cp xl,_0
835 cpc xh,_0
836 breq fat_found_phsy_clust
837 ; Get Next Cluster from Fat
838
839 ; Trick: 512 Bytes Per Sector equals to 256 FAT- Entrys per Sector
840 ; so given: yl is the Offset within the FAT Sector
841 ; yh is the number off se FAT sector to Read
842
843 ; in zh,zl: Pointer to Word within the Sector to read
844 ; in yh..xl: Start sector number (LBA)
845 ; out zh,zl : word thats been read
846 push xl
847 push xh
848
849 ; Create FAT Offset Value
850 clr zh
851 mov zl,yl
852 lsl zl
853 rol zh
854 ; Get FAT Start
855 mov temp,yh
856 lds xl,fat_ptr2fat
857 lds xh,fat_ptr2fat+1
858 lds yl,fat_ptr2fat+2
859 lds yh,fat_ptr2fat+3
860 ; Add Cluster offset within sector
861 add xl,temp
862 adc xh,_0
863 adc yl,_0
864 adc yh,_0
865 call mmcReadWord
866
867 pop xh
868 pop xl
869
870 mov yl,zl
871 mov yh,zh
872
873 ; Check next logical Cluster
874 ldi zl,1
875 sub xl,zl
876 sbc xh,_0
877 rjmp fat_next_phsy_clust
878
879 ; Found the physical cluster
880 fat_found_phsy_clust:
881 .if FAT16_DBG_FAT > 0
882 printstring "Found phys. Cluster at:",0
883 mov temp,yl
884 mov temp2,yh
885 rcall printhexw
886 printnewline
887 .endif
888
889 ret
890
891 ; ====================================================================
892 ; Function: Does a Disk write operation
893 ; ====================================================================
894 ; Parameters
895 ; --------------------------------------------------------------------
896 ; Registers : none
897 ; Variables : [r] seekdsk Number of Disk to Read
898 ; [r] seeksec Sector to read
899 ; [r] seektrk Track to read
900 ; hostdsk = host disk #, (partition #)
901 ; hostlba = host block #, relative to partition start
902 ; Read/Write "hostsize" bytes to/from hostbuf
903 ; --------------------------------------------------------------------
904 ; Description:
905 ; ====================================================================
906
907 fat_hostparam:
908 lds xl,hostdsk
909
910
911 rcall dsk_getpartentry ; get partition entry
912
913 fat_hostlend:
914 lds temp ,hostlba
915 lds temp2,hostlba+1
916 lds temp3,hostlba+2
917
918
919 ldd xl,z+5 ; get size of disk in sectors
920 ldd xh,z+6
921 ldd yl,z+7
922
923 cp temp,xl ; check given sector against disksize
924 cpc temp2,xh
925 cpc temp3,yl
926 brcs fat_hp1
927
928 clr temp
929 ret
930
931 fat_hp1:
932 ; ################# Get logical Number of Cluster within the imagefile
933 ; printstring "calc log sector"
934 ; Get logical Sectornumber from temp
935 mov xl,temp
936 mov xh,temp2
937 mov yl,temp3
938 mov yh,_0
939 ; Divide logical Sectornumber by size of Cluster in sectors
940 lds zl, fat_clustersize
941 lsr zl
942 fat_search_clst_lp:
943 tst zl
944 breq fat_found_clst
945
946 lsr yh
947 ror yl
948 ror xh
949 ror xl
950
951 lsr zl
952
953 rjmp fat_search_clst_lp
954 fat_found_clst:
955 ; at this point xh and xl are carying the logical cluster number
956 ; printstring "find subsector"
957 ; ################# Get Subsector within the logical Cluster for later use
958 mov yl,xl
959 lds zl, fat_clustersize
960 lsr zl
961 fat_search_clst_lp2:
962 tst zl
963 breq fat_found_subsec
964 lsl yl
965
966 lsr zl
967 rjmp fat_search_clst_lp2
968
969 fat_found_subsec:
970 mov zl,temp
971 sub zl,yl
972 sts fat_clust_offset,zl
973
974 ; Check against last HOSTDISK searched
975 lds yl,fat_last_dsk
976 lds yh,hostdsk
977 cp yl,yh
978 brne fat_wrong_cache_clst
979
980 ; Check against last Cluster searched
981 lds yl,fat_log_clust
982 lds yh,fat_log_clust+1
983
984 cp yl,xl
985 brne fat_wrong_cache_clst
986 cp yh,xh
987 brne fat_wrong_cache_clst
988
989 ; Last Cluster = searched Cluster -> get Sectornumber from cache
990 lds xl,fat_clust_ptr
991 lds xh,fat_clust_ptr+1
992 lds yl,fat_clust_ptr+2
993 lds yh,fat_clust_ptr+3
994
995 rjmp fat_add_offset
996
997 ; ################# Cluster is not in cache, so we must search for it
998 fat_wrong_cache_clst:
999 lds yl,hostdsk
1000 sts fat_last_dsk,yl
1001 sts fat_log_clust,xl
1002 sts fat_log_clust+1,xh
1003
1004 ; ################# Map Logical Cluster-Number to "Physical" Cluster Number using the FAT
1005 rcall fat_find_phsy_clust
1006
1007 ; ################# Get StartSector of "physical" Cluster
1008 mov xl,yl
1009 mov xh,yh
1010 rcall fat_gethostsec
1011 ; Found the physical sector
1012 .if FAT16_DBG_FAT > 0
1013 printstring "Found phys. Sector at:",0
1014 mov temp,yl
1015 mov temp2,yh
1016 rcall printhexw
1017 mov temp,xl
1018 mov temp2,xh
1019 rcall printhexw
1020 printnewline
1021 .endif
1022
1023 ; Save the found Sector for later use into cache
1024 sts fat_clust_ptr ,xl
1025 sts fat_clust_ptr+1,xh
1026 sts fat_clust_ptr+2,yl
1027 sts fat_clust_ptr+3,yh
1028
1029 ; Add- Subsector to Startsector
1030 fat_add_offset:
1031 lds zl,fat_clust_offset
1032 add xl,zl
1033 adc xh,_0
1034 adc yl,_0
1035 adc yh,_0
1036
1037 ; Found the physical sector
1038 .if FAT16_DBG_FAT > 0
1039 printstring "Sector with Offset at:",0
1040 mov temp,yl
1041 mov temp2,yh
1042 rcall printhexw
1043 mov temp,xl
1044 mov temp2,xh
1045 rcall printhexw
1046 printnewline
1047 .endif
1048
1049 ori temp,255
1050 fat_hpex:
1051 ret
1052
1053 ; ====================================================================
1054 ; Function: Does a Disk write operation
1055 ; ====================================================================
1056 ; Parameters
1057 ; --------------------------------------------------------------------
1058 ; Registers : none
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
1066 fat_writehost:
1067 .if FAT16_RWDEBUG > 1
1068 printnewline
1069 printstring "host write "
1070 .endif
1071 rcall fat_hostparam
1072 breq fat_rdwr_err
1073
1074 ;call mmcWriteSect ; disabled till read is functioning
1075 tst temp
1076 breq fat_rdwr_ok
1077
1078 rcall mgr_init_partitions
1079 cbr temp,0x80
1080 breq fat_rdwr_err
1081
1082 rcall fat_hostparam
1083 breq fat_rdwr_err
1084 ;call mmcWriteSect ; disabled till read is functioning
1085 tst temp
1086 brne fat_rdwr_err
1087 rjmp fat_rdwr_ok
1088
1089 fat_rdwr_ok:
1090 sts erflag,_0
1091 ret
1092
1093 fat_rdwr_err:
1094 sts erflag,_255
1095 ret
1096 ; ====================================================================
1097 ; Function: Does a Disk read operation
1098 ; ====================================================================
1099 ; Parameters
1100 ; --------------------------------------------------------------------
1101 ; Registers : none
1102 ; Variables : [r] seekdsk Number of Disk to Read
1103 ; [r] seeksec Sector to read
1104 ; [r] seektrk Track to read
1105 ; --------------------------------------------------------------------
1106 ; Description:
1107 ; ====================================================================
1108
1109 fat_readhost:
1110 .if FAT16_RWDEBUG > 1
1111 printnewline
1112 printstring "host read "
1113 .endif
1114
1115 rcall fat_hostparam
1116 breq fat_rdwr_err
1117
1118
1119 .if FAT16_RWDEBUG > 0
1120 printstring "Read Image Sector:"
1121 push temp4
1122 push temp3
1123 push temp2
1124 push temp
1125 movw temp,x
1126 movw temp3,y
1127 rcall print_ultoa
1128 pop temp
1129 pop temp2
1130 pop temp3
1131 pop temp4
1132 printnewline
1133 .endif
1134
1135 call mmcReadSect
1136 tst temp
1137 breq fat_rdwr_ok
1138
1139 rcall mgr_init_partitions
1140 cbr temp,0x80
1141 breq fat_rdwr_err
1142
1143 rcall fat_hostparam
1144 breq fat_rdwr_err
1145 call mmcReadSect
1146 tst temp
1147 brne fat_rdwr_err
1148
1149 #endif
1150