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