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