]> cloudbase.mooo.com Git - avrcpm.git/blobdiff - avrcpm/avr/dsk_fat16.asm
* Merged fat16-test back into trunk.
[avrcpm.git] / avrcpm / avr / dsk_fat16.asm
index ac477080526a108707a20e970138672fae42fe4e..86d25198dfc207e48332bde911f33c5b943452a0 100644 (file)
@@ -2,7 +2,7 @@
 ;
 ;    Copyright (C) 2010 Frank Zoll
 ;    Copyright (C) 2010 Sprite_tm
-;    Copyright (C) 2010 Leo C.\r
+;    Copyright (C) 2010 Leo C.
 ;
 ;    This file is part of avrcpm.
 ;
 #if FAT16_SUPPORT
 
 
-; ############################################################################ \r
+; ############################################################################ 
 ;                              Defines for FAT16 Structures
-; ############################################################################ \r
-\r
-#define PARTID_FAT16 0x0E
-\r
-/*These are the Offsets to the Variables within the Bootsector of a FAT16\r
-  Partition.\r
+; ############################################################################ 
+
+#define PARTID1_FAT16 0x0E
+#define PARTID2_FAT16 0x06
+
+/*These are the Offsets to the Variables within the Bootsector of a FAT16
+  Partition.
  */
 ;#define FAT16_BSO_SECSIZE     0x0b    ; Offset to Sectorsize Word
 #define FAT16_BSO_CLUSTSZ      0x0d    ; Offset to Clustersize Byte
 #define FAT16_BSO_SECPERFAT    0x16    ; Offset to Number of Sectors per Fat
 #define FAT16_BSO_NUMFATCP     0x10    ; Offset to Ammount of FAT Copys
 #define FAT16_BSO_NUMDIRENT    0x11    ; Offset to Max. Root Dir. Entrys
-#define        FAT16_FIRST_IMAGENAME   'A'     ; First letter of filename to search \r
-#define FAT16_LAST_IMAGENAME    'Z'    ; Last letter of filename to search \r
+#define        FAT16_FIRST_IMAGENAME   'A'     ; First letter of filename to search 
+#define FAT16_LAST_IMAGENAME    'A'+MAXDISKS-1 ; Last letter of filename to 
+                                               ; search 
 
-; ############################################################################ \r
+; ############################################################################ 
 ;                              Start of Data Segment
-; ############################################################################ \r
+; ############################################################################ 
        .dseg
 
 fat_partfound:   .byte   1     ; (partition: 0= no found 1=found )
-fat_parttbl:    .byte   8      ; first fat16 partition entry \r
-                               ; (start sector, sector count)
-;fat_sectorsize:  .byte   2    ; size of sector in bytes
+fat_parttbl:    .byte   4      ; first fat16 partition entry 
+                               ; only startsector is needed
 fat_clustersize: .byte   1     ; sectors per cluster
-fat_ressectors:  .byte   2     ; number of reserved sectors
-fat_secperfat:   .byte   2     ; number of sectors per fat
-fat_numfatcp:    .byte   1     ; number of FAT Copies
 fat_numdirentrys:.byte   2     ; Max. num. of entrys within Rootdirektory
 fat_ptr2fat:     .byte   4     ; pointer to the first fat sector
-fat_ptr2dir:     .byte   4     ; pointer to the first root directory sector
 fat_ptr2dat:     .byte   4     ; pointer to the first data sector
 
-/*These variables define a cache that holds the last Cluster and Sector\r
-  thats been searched vor. To save some of the valuabe SRAM- Space these\r
-  variables also are used as temporary variables by the function \r
-  fat_scan_partition.\r
-  */\r
+/*These variables define a cache that holds the last Cluster and Sector
+  thats been searched vor. To save some of the valuabe SRAM- Space these
+  variables also are used as temporary variables by the function 
+  fat_scan_partition.
+  */
 fat_last_dsk:    .byte  1      ; number of disk with entry in cache
 fat_log_clust:   .byte  2      ; last searched logical cluster
 fat_clust_offset: .byte         1      ; offset within the cluster
-fat_clust_ptr:   .byte  4;     ; sector of last real cluster\r
-\r
+fat_clust_ptr:   .byte  4      ; sector of last real cluster
+
+/* This Variable is only needed within the scanning of the directory 
+for tempoary variable storage.. todo: optimize away :-) */
+fat_temp:        .byte 3       ; for tempoary use
 
-; ############################################################################ \r
+; ############################################################################ 
 ;                              Start of Code Segment
-; ############################################################################ \r
+; ############################################################################ 
        .cseg
 
 ; ============================================================================
@@ -93,8 +93,8 @@ fat_clust_ptr:          .byte  4;     ; sector of last real cluster
 ; Registers  : none
 ; Variables  : [out]   fat_parttabl
 ; ----------------------------------------------------------------------------
-; Description:\r
-; This Routine initializes the internal Variables, that point to the\r
+; Description:
+; This Routine initializes the internal Variables, that point to the
 ; first Found FAT16 Partition.
 ; ============================================================================
 fat_init_partitiontable:
@@ -106,33 +106,29 @@ fat_init_partitiontable:
        st      y+,_0
        st      y+,_0
        st      y+,_0
-       st      y+,_0
-       st      y+,_0
-       st      y+,_0
-       st      y+,_0
-       ret\r
-\r
-; ============================================================================\r
+       ret
+
+; ============================================================================
 ; Function: Resets the Cache
-; ============================================================================\r
-; Parameters\r
-; ----------------------------------------------------------------------------\r
-; Registers  : none\r
-; Variables  : [out]   fat_log_clust\r
-;              [out]   fat_last_dsk\r
-; ----------------------------------------------------------------------------\r
-; Description:\r
-; This Routine resets the internal Cache- Variables. After reset, the\r
-; next read or write Command will initialize a scan of the FAT of\r
-; the FAT16-Partition for the given sector.\r
-; ============================================================================\r
-fat_reset_cache:\r
-       push    yl\r
+; ============================================================================
+; Parameters
+; ----------------------------------------------------------------------------
+; Registers  : none
+; Variables  : [out]   fat_log_clust
+;              [out]   fat_last_dsk
+; ----------------------------------------------------------------------------
+; Description:
+; This Routine resets the internal Cache- Variables. After reset, the
+; next read or write Command will initialize a scan of the FAT of
+; the FAT16-Partition for the given sector.
+; ============================================================================
+fat_reset_cache:
+       push    yl
        ldi     yl,0xFF
        sts     fat_log_clust  ,yl
        sts     fat_log_clust+1,yl
        sts     fat_last_dsk   ,yl
-       pop     yl\r
+       pop     yl
        ret
 
 ; ============================================================================
@@ -141,7 +137,7 @@ fat_reset_cache:
 ; Parameters
 ; ----------------------------------------------------------------------------
 ; Registers  : [in]    z               Pointer to the Partitondata
-; Variables  : [out]   fat_partfound   Boolean for "Partition found"\r
+; Variables  : [out]   fat_partfound   Boolean for "Partition found"
 ;              [out]   fat_parttbl     Pointer to Partitiontable
 ; ----------------------------------------------------------------------------
 ; Description:
@@ -153,7 +149,7 @@ fat_reset_cache:
 fat_add_partition:
        
 .if FAT16_DEBUG > 0
-       printstring "fat16 part found",0
+       printstring "fat16 part found"
        printnewline
 .endif
 
@@ -177,16 +173,6 @@ fat_add_partition:
        st      y+,temp
        ldd     temp,z+PART_START+3
        st      y+,temp
-       
-       ldd     temp,z+PART_SIZE
-       st      y+,temp
-       ldd     temp,z+PART_SIZE+1
-       st      y+,temp
-       ldd     temp,z+PART_SIZE+2
-       st      y+,temp
-       ldd     temp,z+PART_SIZE+3
-       st      y+,temp
-
 
 ;   reload variables from stack
        pop     yh
@@ -207,7 +193,7 @@ fat_add_partition:
 fat_scan_partition:
 
 .if FAT16_DEBUG > 0
-       printstring "fat16 scanning",0
+       printstring "fat16 scanning"
        printnewline
 .endif
 
@@ -218,7 +204,7 @@ fat_scan_partition:
 
 
 .if FAT16_DEBUG > 0
-       printstring "free entrys in ptable ?",0
+       printstring "free entrys in ptable ?"
        printnewline
 .endif
 
@@ -228,7 +214,7 @@ fat_scan_partition:
        breq    fat_scan_error
 
 .if FAT16_DEBUG > 0
-       printstring "read fat bootblock.",0
+       printstring "read fat bootblock."
        printnewline
 .endif
 
@@ -252,69 +238,28 @@ fat_scan_error:
 fat_bootblock_check:
 
 .if FAT16_DEBUG > 0
-       printstring "fat16 bootblock check",0
+       printstring "fat16 bootblock check"
        printnewline
 .endif
 
-; Not needed / used by now because size of Sectors fixed at 512 Bytes
-;   Get ammount of Bytes per Sector
-;      ldiw    z,hostbuf+FAT16_BSO_SECSIZE
-;      ldiw    y,fat_sectorsize
-;      ld          temp,z
-;      st      y+, temp
-;      ldd         temp2,z+1
-;      st      y , temp2
-;
-;.if FAT16_DEBUG > 0
-;      printstring "Bytes per Sector ",0
-;      rcall printhexw
-;      printnewline
-;.endif
-
 ;      get sectors per cluster from bootblock
        ldiw    z,hostbuf+FAT16_BSO_CLUSTSZ
        ld      temp,z
        sts     fat_clustersize,temp
 
 .if FAT16_DEBUG > 0
-       printstring "Sectors per Cluster ",0
+       printstring "Sectors per Cluster "
        rcall printhex
        printnewline
 .endif
 
-;      get num of reseved sectors from bootblock
-       ldiw    z,hostbuf+FAT16_BSO_RESSECT
-       ld      temp,z+
-       sts     fat_ressectors,temp             ; low byte
-       ld      temp2,z
-       sts     fat_ressectors+1,temp2          ; high byte
-
-.if FAT16_DEBUG > 0
-       printstring "Reserved Sectors__: ",0
-       rcall printhexw
-       printnewline
-.endif
-
-;      get num of sectors per FAT-Table from bootblock
-       ldiw    z,hostbuf+FAT16_BSO_SECPERFAT
-       ld      temp,z+
-       sts     fat_secperfat,temp              ; low byte
-       ld      temp2,z
-       sts     fat_secperfat+1,temp2           ; high byte
-
-.if FAT16_DEBUG > 0
-       printstring "Sectors per FAT__: ",0
-       rcall printhexw
-       printnewline
-.endif
-
 ;      get num of FAT Tables from bootblock
        ldiw    z,hostbuf+FAT16_BSO_NUMFATCP
        ld      temp,z
-       sts     fat_numfatcp,temp               ; low byte
+       sts     fat_last_dsk,temp               ; low byte
 
 .if FAT16_DEBUG > 0
-       printstring "Ammount of FAT copies: ",0
+       printstring "Ammount of FAT copies: "
        rcall printhex
        printnewline
 .endif
@@ -327,7 +272,7 @@ fat_bootblock_check:
        sts     fat_numdirentrys+1,temp2        ; high byte
 
 .if FAT16_DEBUG > 0
-       printstring "Max. entrys in Rootdir.: ",0
+       printstring "Max. entrys in Rootdir.: "
        rcall printhexw
        printnewline
 .endif
@@ -341,7 +286,7 @@ fat_bootblock_check:
        ldd     yl,z+2
        ldd     yh,z+3
 
-       printstring "Begin of Volume at: ",0
+       printstring "Begin of Volume at: "
        mov     temp ,yl
        mov     temp2,yh
        rcall   printhexw
@@ -351,10 +296,25 @@ fat_bootblock_check:
        printnewline
 .endif
 
-;      Calculate begin of FAT within the Volume
-       lds     temp ,fat_ressectors
-       lds     temp2,fat_ressectors+1
+;      get num of sectors per FAT-Table from bootblock
+       ldiw    z,hostbuf+FAT16_BSO_SECPERFAT
+       ld      temp,z+
+       sts     fat_log_clust,temp              ; low byte
+       ld      temp2,z
+       sts     fat_log_clust+1,temp2           ; high byte
+
+.if FAT16_DEBUG > 0
+       printstring "Sectors per FAT__: "
+       rcall printhexw
+       printnewline
+.endif
+
+;      get num of reseved sectors from bootblock
+       ldiw    z,hostbuf+FAT16_BSO_RESSECT
+       ld      temp,z+
+       ld      temp2,z
 
+;      Calculate begin of FAT within the Volume
        ldiw    z,fat_parttbl   
        ldd     xl,z+0          
        ldd     xh,z+1
@@ -372,7 +332,7 @@ fat_bootblock_check:
        sts     fat_ptr2fat+3,yh
 
 .if FAT16_DEBUG > 1
-       printstring "Begin of FAT at___: ",0
+       printstring "Begin of FAT at___: "
        mov     temp ,yl
        mov     temp2,yh
        rcall   printhexw
@@ -389,9 +349,9 @@ fat_bootblock_check:
        ldd     yl,z+2
        ldd     yh,z+3
 
-       lds     temp ,fat_secperfat
-       lds     temp2,fat_secperfat+1
-       lds     temp3,fat_numfatcp
+       lds     temp ,fat_log_clust
+       lds     temp2,fat_log_clust+1
+       lds     temp3,fat_last_dsk
 
 fat_calc_dp_loop:
        cp      temp3,_0
@@ -404,17 +364,17 @@ fat_calc_dp_loop:
 
        dec     temp3
 
-       jmp     fat_calc_dp_loop
+       rjmp    fat_calc_dp_loop
 fat_calc_dp_lend:
 
-       sts     fat_ptr2dir  ,xl
-       sts     fat_ptr2dir+1,xh
-       sts     fat_ptr2dir+2,yl
-       sts     fat_ptr2dir+3,yh
+       sts     fat_clust_ptr  ,xl
+       sts     fat_clust_ptr+1,xh
+       sts     fat_clust_ptr+2,yl
+       sts     fat_clust_ptr+3,yh
 
 
 .if FAT16_DEBUG > 1
-       printstring "Begin of DIR at___: ",0
+       printstring "Begin of DIR at___: "
        mov     temp ,yl
        mov     temp2,yh
        rcall   printhexw
@@ -442,10 +402,10 @@ fat_calc_dp_lend:
        lsr     zh
        ror     zl
 
-       lds     xl,fat_ptr2dir
-       lds     xh,fat_ptr2dir+1
-       lds     yl,fat_ptr2dir+2
-       lds     yh,fat_ptr2dir+3
+       lds     xl,fat_clust_ptr
+       lds     xh,fat_clust_ptr+1
+       lds     yl,fat_clust_ptr+2
+       lds     yh,fat_clust_ptr+3
 
        add     xl,zl
        adc     xh,zh
@@ -458,7 +418,7 @@ fat_calc_dp_lend:
        sts     fat_ptr2dat+3,yh
 
 .if FAT16_DEBUG > 1
-       printstring "Begin of Data at__: ",0
+       printstring "Begin of Data at__: "
        mov     temp ,yl
        mov     temp2,yh
        rcall   printhexw
@@ -470,39 +430,39 @@ fat_calc_dp_lend:
 
 ; Here Starts the Scann of the Directory for valid image Files.
 
-;      Init Image-Namecounter\r
-       ldi     temp,FAT16_FIRST_IMAGENAME\r
-       sts     fat_last_dsk,temp\r
-\r
-fat_scan_for_next_image:\r
-\r
-;      Init Offset into Directory-Sectors\r
-       ldi     temp,0\r
-       sts     fat_clust_offset,temp\r
-\r
-;      Init counter for number of entry left to scan\r
-       lds     temp,fat_numdirentrys\r
-       sts     fat_log_clust  ,temp\r
-\r
-       lds     temp,fat_numdirentrys+1\r
-       sts     fat_log_clust+1,temp\r
-\r
-fat_next_sector_loop:\r
-;   Get a Pointer to the first Directory sector\r
-       lds     xl,fat_ptr2dir
-       lds     xh,fat_ptr2dir+1
-       lds     yl,fat_ptr2dir+2
-       lds     yh,fat_ptr2dir+3
-
-;      Add actual offset\r
-       lds     temp,fat_clust_offset\r
-       add     xl,temp\r
-       adc     xh,_0\r
-       adc     yl,_0\r
-       adc     yh,_0\r
-\r
+;      Init Image-Namecounter
+       ldi     temp,FAT16_FIRST_IMAGENAME
+       sts     fat_last_dsk,temp
+
+fat_scan_for_next_image:
+
+;      Init Offset into Directory-Sectors
+       ldi     temp,0
+       sts     fat_clust_offset,temp
+
+;      Init counter for number of entry left to scan
+       lds     temp,fat_numdirentrys
+       sts     fat_log_clust  ,temp
+
+       lds     temp,fat_numdirentrys+1
+       sts     fat_log_clust+1,temp
+
+fat_next_sector_loop:
+;   Get a Pointer to the first Directory sector
+       lds     xl,fat_clust_ptr
+       lds     xh,fat_clust_ptr+1
+       lds     yl,fat_clust_ptr+2
+       lds     yh,fat_clust_ptr+3
+
+;      Add actual offset
+       lds     temp,fat_clust_offset
+       add     xl,temp
+       adc     xh,_0
+       adc     yl,_0
+       adc     yh,_0
+
 ;  Load sector from Directory
-       call    mmcReadSect
+       lcall   mmcReadSect
        tst     temp
        breq    fat_look_for_images
 
@@ -545,11 +505,11 @@ fat_look_for_loop:
        cpi     temp,'_'
        brne    fat_look_not_ok
 
-       lds     temp3,fat_last_dsk      ; Get actual Diskname (A to Z)\r
-       ldd     temp,z+7\r
-       cp      temp,temp3\r
-       brne    fat_look_not_ok\r
-\r
+       lds     temp3,fat_last_dsk      ; Get actual Diskname (A to Z)
+       ldd     temp,z+7
+       cp      temp,temp3
+       brne    fat_look_not_ok
+
        ldd     temp,z+8
        cpi     temp,'I'
        brne    fat_look_not_ok
@@ -562,81 +522,62 @@ fat_look_for_loop:
        cpi     temp,'G'
        brne    fat_look_not_ok
 
-       sts     fat_clust_ptr  ,zl\r
-       sts     fat_clust_ptr+1,zh\r
-       sts     fat_clust_ptr+2,temp2\r
-       jmp     fat_store_new_entry
-
-fat_scan_for_more:\r
-\r
-\r
-.if FAT16_DEBUG > 1\r
-       printstring "At:Scann for more ",0\r
-       printnewline\r
-.endif\r
-\r
-       lds     zl   ,fat_clust_ptr\r
-       lds     zh   ,fat_clust_ptr+1\r
-       lds     temp2,fat_clust_ptr+2\r
+       sts     fat_temp  ,zl
+       sts     fat_temp+1,zh
+       sts     fat_temp+2,temp2
+       rjmp    fat_store_new_entry
+
+fat_scan_for_more:
+
+       lds     zl   ,fat_temp
+       lds     zh   ,fat_temp+1
+       lds     temp2,fat_temp+2
 fat_look_not_ok:
-       
-.if FAT16_DEBUG > 1\r
-       printstring "+",0\r
-.endif\r
-       \r
-       adiw    z,32\r
+               
+       adiw    z,32
 
        inc     temp2
        cpi     temp2,16                                ; max entrys/sector
        breq    fat_scan_next_sector
-       jmp     fat_look_for_loop
+       rjmp    fat_look_for_loop
 
 fat_scan_next_sector:
 
-.if FAT16_DEBUG > 1\r
-       printstring "At:Scan next Sector",0\r
-       printnewline\r
-.endif\r
-       \r
-       lds     temp3, fat_log_clust\r
-       lds     temp4, fat_log_clust+1\r
-\r
-       sub     temp3,temp2\r
-       sbc     temp4,_0\r
-\r
-       sts     fat_log_clust,temp3\r
-       sts     fat_log_clust+1,temp4\r
-       \r
-       cp      temp3,_0\r
-       cpc     temp4,_0\r
-       breq    fat_scan_at_end \r
-\r
-       lds     temp,fat_clust_offset\r
-       inc     temp\r
-       sts     fat_clust_offset,temp\r
-\r
-       rjmp    fat_next_sector_loop\r
-\r
-fat_scan_at_end:\r
-\r
-.if FAT16_DEBUG > 1\r
-       printstring "At:Scan at end",0\r
-       printnewline\r
-.endif\r
-\r
-       lds     temp,fat_last_dsk\r
-       inc     temp\r
-       sts     fat_last_dsk,temp\r
-\r
-       ldi     temp2,FAT16_LAST_IMAGENAME\r
-       cp      temp,temp2\r
-       brge    fat_scaned_last_disk\r
-\r
-       rjmp    fat_scan_for_next_image\r
-\r
-fat_scaned_last_disk:\r
-\r
-       rjmp    fat_scan_end\r
+       
+       lds     temp3, fat_log_clust
+       lds     temp4, fat_log_clust+1
+
+       sub     temp3,temp2
+       sbc     temp4,_0
+
+       sts     fat_log_clust,temp3
+       sts     fat_log_clust+1,temp4
+       
+       cp      temp3,_0
+       cpc     temp4,_0
+       breq    fat_scan_at_end 
+
+       lds     temp,fat_clust_offset
+       inc     temp
+       sts     fat_clust_offset,temp
+
+       rjmp    fat_next_sector_loop
+
+fat_scan_at_end:
+
+       lds     temp,fat_last_dsk
+       inc     temp
+       sts     fat_last_dsk,temp
+
+       ldi     temp2,FAT16_LAST_IMAGENAME
+       cp      temp,temp2
+       brge    fat_scaned_last_disk
+
+       rjmp    fat_scan_for_next_image
+
+fat_scaned_last_disk:
+
+       rjmp    fat_scan_end
 
 
 ;      Create new Partition Entry
@@ -644,10 +585,10 @@ fat_store_new_entry:
 
 ;   Found a valid image
 .if FAT16_DEBUG > 1
-       printstring "Found a valid Image ! Z=",0\r
-       mov     temp ,zl\r
-       mov     temp2,zh\r
-       rcall   printhexw\r
+       printstring "Found a valid Image ! Z="
+       mov     temp ,zl
+       mov     temp2,zh
+       rcall   printhexw
        printnewline
 .endif
 
@@ -660,36 +601,26 @@ fat_look_store_loop:
 
        adiw    y,PARTENTRY_SIZE
        dec     temp
-       jmp     fat_look_store_loop
+       rjmp    fat_look_store_loop
 
 fat_look_store:
 ;   Set Type of Partition to FAT16- Fileimage
        ldi     temp,dskType_FAT
        st      y+,temp
 
-.if FAT16_DEBUG > 1\r
-       printstring "ClusterID",0\r
-.endif\r
+
 ;   Offset to Startcluster + 2
        ldd     temp,z+0x1A
        st      y+,temp
-.if FAT16_DEBUG > 1\r
-       rcall   printhex\r
-.endif
+
        ldd     temp,z+0x1B
        st      y+,temp 
-.if FAT16_DEBUG > 1\r
-       rcall   printhex\r
-.endif
+
        ldi     temp,0
        st      y+,temp
-       st      y+,temp\r
-
-.if FAT16_DEBUG > 1\r
-       printnewline\r
-.endif
+       st      y+,temp
 
-;   Filesize in Bytes\r
+;   Filesize in Bytes
 ;      ldd     temp,z+0x1C
 ;      st      y+,temp
 ;      ldd     temp,z+0x1D
@@ -717,35 +648,35 @@ fat_look_store:
        st      y+,zl
        st      y+,zh
 
-.if FAT16_DEBUG > 1\r
-; Test finding of the first sector\r
-       \r
-       ldd     xl,z+0x1A\r
-       ldd     xh,z+0x1B\r
-\r
-       rcall   fat_gethostsec\r
-\r
-       printstring "Begin of Image at: ",0\r
-       mov     temp ,yl\r
-       mov     temp2,yh\r
-       rcall   printhexw\r
-       mov     temp ,xl\r
-       mov     temp2,xh\r
-       rcall   printhexw\r
-       printnewline\r
-\r
-.endif\r
+.if FAT16_DEBUG > 1
+; Test finding of the first sector
+       
+       ldd     xl,z+0x1A
+       ldd     xh,z+0x1B
+
+       rcall   fat_gethostsec
+
+       printstring "Begin of Image at: "
+       mov     temp ,yl
+       mov     temp2,yh
+       rcall   printhexw
+       mov     temp ,xl
+       mov     temp2,xh
+       rcall   printhexw
+       printnewline
+
+.endif
 ; Check for another free entry in partition table
        lds     temp,ndisks
        inc     temp
        sts     ndisks,temp
        
-       cpi     temp,MAXDISKS\r
-       breq    fat_scan_end    \r
+       cpi     temp,MAXDISKS
+       breq    fat_scan_end    
 
-       jmp     fat_scan_for_more       \r
+       rjmp    fat_scan_for_more       
 
-fat_scan_end:\r
+fat_scan_end:
                
        ret
 
@@ -788,11 +719,11 @@ fat_gethostsec:
 ; Variables:   [in]    fat_clustersize         Ammount of Sectors per Cluster
 ;              [out]   temp                    =0
 ; ----------------------------------------------------------------------------
-; Description:\r
-; Calculates the logical Sectornumber given the physical ClusterNumber\r
-; and the size of a Cluster un sectors.\r
+; Description:
+; Calculates the logical Sectornumber given the physical ClusterNumber
+; and the size of a Cluster un sectors.
 ;
-; ! Only works with Clustersizes 2,4,8,16,32,64,128,512,1024 !
+; ! Only works with Clustersizes 2,4,8,16,32,64,128 !
 ; ============================================================================
 fat_clusttosec:
        clr     yl
@@ -824,7 +755,7 @@ fat_c2s_end:
 ; ====================================================================
 ; Registers:   [in]    xh,xl   logical- Cluster 
 ;              [out]   yh,yl   physical- Cluster
-; Variables:\r
+; Variables:
 ; --------------------------------------------------------------------
 ; Description:
 ; ====================================================================
@@ -841,16 +772,16 @@ fat_find_phsy_clust:
        ldd     yh,z+2
 
 .if FAT16_DBG_FAT > 0
-       printstring "Search log. Cluster ",0
+       printstring "Search log. Cluster "
        mov     temp,xl
        mov     temp2,xh
-       rcall   printhexw
+       lcall   printhexw
        printnewline
                
-       printstring "Search phys. Cluster ",0
+       printstring "Search phys. Cluster "
        mov     temp ,yl
        mov     temp2,yh
-       rcall   printhexw
+       lcall   printhexw
        printnewline
 .endif
 
@@ -886,7 +817,7 @@ fat_next_phsy_clust:
        adc     xh,_0
        adc     yl,_0
        adc     yh,_0
-       call    mmcReadWord
+       lcall   mmcReadWord
 
        pop     xh
        pop     xl
@@ -901,30 +832,49 @@ fat_next_phsy_clust:
        rjmp    fat_next_phsy_clust
        
 ; Found the physical cluster
-fat_found_phsy_clust:  
+fat_found_phsy_clust:
+       
 .if FAT16_DBG_FAT > 0
-       printstring "Found phys. Cluster at:",0
+       printstring "Found phys. Cluster at:"
        mov     temp,yl
        mov     temp2,yh
-       rcall   printhexw
+       lcall   printhexw
        printnewline
 .endif 
 
        ret
 
 ; ============================================================================
-; Function: Does a Disk write operation
+; Function: This Routine searches for the Sector within an Imagefile 
 ; ============================================================================
-; Parameters
-; ----------------------------------------------------------------------------
-; Registers:   none
-; Variables:   [in] seekdsk            Number of Disk to Read
-;              [in] seeksec            Sector to read
-;              [in] seektrk            Track  to read
-;              [in] hostdsk            host disk #,  (partition #)
+; Registers:   [out] xl,xh,yl,yh       Pointer to the Sector on the SD-Card
+;              [out] temp              Error- Variable (0= No Error)
+; Variables:   [in] hostdsk            host disk #,  (partition #)
 ;              [in] hostlba            host block #, relative to part.start
+;              [in] fat_last_dsk       number of disk with entry in cache
+;              [in] fat_log_clust      last searched logical cluster
+;              [in] fat_clust_offset   offset within the cluster
+;               [in] fat_clust_ptr     sector of last real cluster
 ; ----------------------------------------------------------------------------
 ; Description:
+; This Routine uses the variables hostdsk and hostlba to find an Sector
+; in the Imagefile.
+; The CP/M Sector given within "hostlba" are splited to a logical Cluster-
+; Number and the Subsector within this logical Cluster.
+; logical Cluster Number = hostlba / fat_clustersize
+; The logical Cluster Number will be compared to the logical Cluster- Number
+; within the Cache. When this Clusters are the same and the DiskID's are
+; also the same, then the cached physical Sector will be used.
+; When the Clusters or the Disks don't match, a seek for the physical
+; Cluster is performed. This seek is done thru an access over the FAT of
+; the FAT16 Partition. The Routine starts at the first Cluster of the 
+; Imagefile and goes along the linked list of Clusternumber till it reaches
+; the searched cluster. The found Clusternumber will be used to calculate
+; the Sektor where this Cluster lies on the SD- Card. Both the found physical
+; Cluster and the logical Cluster together with the physical Sectornumber
+; are stored in the cache.
+; The last step done is to add the SubSectorOffset to the found physical
+; Sector. This gives the pointer to the Sector to be read and or written.
 ; ============================================================================
 
 fat_hostparam:
@@ -960,7 +910,7 @@ fat_hp1:
        mov     yl,temp3
        mov     yh,_0
 ; Divide logical Sectornumber by size of Cluster in sectors
-       lds     zl,     fat_clustersize
+       lds     zl,fat_clustersize
        lsr     zl
 fat_search_clst_lp:
        tst     zl
@@ -973,13 +923,14 @@ fat_search_clst_lp:
        
        lsr     zl
 
-       rjmp    fat_search_clst_lp      
+       rjmp    fat_search_clst_lp
+               
 fat_found_clst:                        
 ; at this point xh and xl are carying the logical cluster number
 ;      printstring "find subsector"
 ; ################# Get Subsector within the logical Cluster for later use
        mov     yl,xl
-       lds     zl,     fat_clustersize
+       lds     zl,fat_clustersize
        lsr     zl
 fat_search_clst_lp2:
        tst     zl
@@ -1031,15 +982,16 @@ fat_wrong_cache_clst:
        mov     xl,yl
        mov     xh,yh
        rcall   fat_gethostsec
+
 ; Found the physical sector
 .if FAT16_DBG_FAT > 0
-       printstring "Found phys. Sector at:",0
+       printstring "Found phys. Sector at:"
        mov     temp,yl
        mov     temp2,yh
-       rcall   printhexw
+       lcall   printhexw
        mov     temp,xl
        mov     temp2,xh
-       rcall   printhexw
+       lcall   printhexw
        printnewline
 .endif 
 
@@ -1059,13 +1011,13 @@ fat_add_offset:
 
 ; Found the physical sector
 .if FAT16_DBG_FAT > 0
-       printstring "Sector with Offset at:",0
+       printstring "Sector with Offset at:"
        mov     temp,yl
        mov     temp2,yh
-       rcall   printhexw
+       lcall   printhexw
        mov     temp,xl
        mov     temp2,xh
-       rcall   printhexw
+       lcall   printhexw
        printnewline
 .endif
 
@@ -1076,14 +1028,13 @@ fat_hpex:
 ; ============================================================================
 ; Function: Does a Disk write operation
 ; ============================================================================
-; Parameters
-; ----------------------------------------------------------------------------
-; Registers  : none
-; Variables  : [r] seekdsk             Number of Disk to Read
-;                         [r] seeksec          Sector to read
-;              [r] seektrk             Track  to read
+; Registers:   [out]   temp    Error-Variable ( 0= No Error)           
+; Variables:   [in]    hostdsk host disk #,  (partition #)
+;              [in]    hostlba host block #, relative to part.start
+;              [in]    hostbuf Sector to be written
 ; ----------------------------------------------------------------------------
 ; Description:
+; This Routine writes a Sector to the Imagefile inside an FAT16 Partition.
 ; ============================================================================
 
 fat_writehost:
@@ -1094,17 +1045,25 @@ fat_writehost:
        rcall   fat_hostparam
        breq    fat_rdwr_err
        
-       ;call   mmcWriteSect    ; disabled till read is functioning
+       call    mmcWriteSect
        tst     temp
        breq    fat_rdwr_ok
        
+       rjmp    fat_rdwr_err            ; skip disk change detection code
+       
+; After a second thought, the following  code doesn't make sense, because
+; disk change (change of one or more disk images) can not reliably detected.
+; At least with the existing code.
+
+
+
        rcall   mgr_init_partitions
        cbr     temp,0x80
        breq    fat_rdwr_err
 
        rcall   fat_hostparam
        breq    fat_rdwr_err
-       ;call   mmcWriteSect    ; disabled till read is functioning
+       call    mmcWriteSect    ; disabled till read is functioning
        tst     temp
        brne    fat_rdwr_err
        rjmp    fat_rdwr_ok
@@ -1115,17 +1074,18 @@ fat_rdwr_ok:
 
 fat_rdwr_err:
        sts     erflag,_255
-       ret\r
+       ret
 
 ; ============================================================================
 ; Function: Does a Disk read operation
 ; ============================================================================
-; Registers:   none
-; Variables:   [in]    seekdsk         Number of Disk to Read
-;              [in]    seeksec         Sector to read
-;              [in]    seektrk         Track  to read
+; Registers:   none
+; Variables:   [in]    hostdsk         host disk #,  (partition #)
+;              [in]    hostlba         host block #, relative to part.start
+;              [out]   hostbuf         Sector read by this routine
 ; ----------------------------------------------------------------------------
 ; Description:
+; This Routine reads a Sector from the Imagefile inside an FAT16 Partition. 
 ; ============================================================================
 
 fat_readhost:
@@ -1146,7 +1106,7 @@ fat_readhost:
        push    temp
        movw    temp,x
        movw    temp3,y
-       rcall   print_ultoa
+       lcall   print_ultoa
        pop     temp
        pop     temp2
        pop     temp3
@@ -1154,17 +1114,19 @@ fat_readhost:
        printnewline
 .endif
        
-       call    mmcReadSect
+       lcall   mmcReadSect
        tst     temp
        breq    fat_rdwr_ok
 
+       rjmp    fat_rdwr_err            ; skip disk change detection code
+       
        rcall   mgr_init_partitions
        cbr     temp,0x80
        breq    fat_rdwr_err
 
        rcall   fat_hostparam
        breq    fat_rdwr_err
-       call    mmcReadSect
+       lcall   mmcReadSect
        tst     temp
        brne    fat_rdwr_err