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