]> cloudbase.mooo.com Git - avrcpm.git/blobdiff - dsk_fat16.asm
git-svn-id: svn://cu.loc/avr-cpm/trunk/avrcpm/avr@115 57430480-672e-4586-8877-bcf8adb...
[avrcpm.git] / dsk_fat16.asm
index fd52479a60522bc6359f97777572e943dc33b9d4..0a7510a7a9fb0242a7f07968633976e2fc876230 100644 (file)
@@ -1,6 +1,8 @@
 ;    Various functions for the Interaction with the FAT16 Filesystem
 ;
 ;    Copyright (C) 2010 Frank Zoll
+;    Copyright (C) 2010 Sprite_tm
+;    Copyright (C) 2010 Leo C.\r
 ;
 ;    This file is part of avrcpm.
 ;
 ;    $Id$
 ;
 
-; ===========================================================================
+; ============================================================================
 ; Prelimitary !
 ; °°°°°°°°°°°°°
 ; Size of a Sector is fixed to 512 Bytes by Base - MMC Driver implementation
 ; The Functions below therefore assume a fixed Size of 512 Bytes per Sector.
-; ===========================================================================
+; ============================================================================
 
 #if FAT16_SUPPORT
 
 
-;-------------------------------- Defines for FAT16 Structures
+; ############################################################################ \r
+;                              Defines for FAT16 Structures
+; ############################################################################ \r
+\r
 #define PARTID_FAT16 0x0E
-
-;#define FAT16_BSO_SECSIZE     0x0b            ; BootSectorOffset to Sectorsize Word
-#define FAT16_BSO_CLUSTSZ   0x0d        ; BootSectorOffset to Clustersize Byte
-#define FAT16_BSO_RESSECT   0x0e               ; BootSectorOffset to Number of Reserved Sectors
-#define FAT16_BSO_VOLPTR    0x1c        ; BootSectorOffset to First VolumeSector
-#define FAT16_BSO_SECPERFAT 0x16        ; BootSectorOffset to Number of Sectors per Fat
-#define FAT16_BSO_NUMFATCP  0x10               ; BootSectorOffset to Ammount of FAT Copys
-#define FAT16_BSO_NUMDIRENT 0x11               ; BootSectorOffset to Max. Root Dir. Entrys
-
-;-------------------------------- Start of Data Segment
-
+\r
+/*These are the Offsets to the Variables within the Bootsector of a FAT16\r
+  Partition.\r
+ */
+;#define FAT16_BSO_SECSIZE     0x0b    ; Offset to Sectorsize Word
+#define FAT16_BSO_CLUSTSZ      0x0d    ; Offset to Clustersize Byte
+#define FAT16_BSO_RESSECT      0x0e    ; Offset to Number of Reserved Sectors
+#define FAT16_BSO_VOLPTR       0x1c    ; Offset to First VolumeSector
+#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
+
+; ############################################################################ \r
+;                              Start of Data Segment
+; ############################################################################ \r
        .dseg
 
-fat_partfound:   .byte   1   ; (0= no fat partition found 1=found partition)
-fat_parttbl:    .byte   8       ; first fat16 partition entry (start sector, sector count)
-;fat_sectorsize:  .byte   2   ; size of sector in bytes
-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. ammount of Directory 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
-
-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
-; ------------------------------- Start of Code Segment
+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_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
+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
+
+; ############################################################################ \r
+;                              Start of Code Segment
+; ############################################################################ \r
        .cseg
 
-; ====================================================================
-; Function: Does a Disk read/write operation
-; ====================================================================
+; ============================================================================
+; Function: Initialize internal FAT-Partition Variables
+; ============================================================================
 ; Parameters
-; --------------------------------------------------------------------
-; Registers  : none
-; Variables  : [r] seekdsk             Number of Disk to Read
-;                         [r] seeksec          Sector to read
-;              [r] seektrk             Track  to read
-; --------------------------------------------------------------------
-; Description:
-; ====================================================================
+; ----------------------------------------------------------------------------
+; Registers  : none
+; Variables  : [out]   fat_parttabl
+; ----------------------------------------------------------------------------
+; Description:\r
+; This Routine initializes the internal Variables, that point to the\r
+; first Found FAT16 Partition.
+; ============================================================================
 fat_init_partitiontable:
 
-       sts fat_partfound,_0
+       sts     fat_partfound,_0
 
        ldiw    y,fat_parttbl
        st      y+,_0
@@ -89,26 +110,46 @@ fat_init_partitiontable:
        st      y+,_0
        st      y+,_0
        st      y+,_0
+       ret\r
+\r
+; ============================================================================\r
+; 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
        ldi     yl,0xFF
-       sts fat_log_clust  ,yl
+       sts     fat_log_clust  ,yl
        sts     fat_log_clust+1,yl
        sts     fat_last_dsk   ,yl
+       pop     yl\r
        ret
 
-; ====================================================================
-; Function: Add's a FAT16 Partition for later Scanning
-; ====================================================================
+; ============================================================================
+; Function: Saves FAT16 Partitiondata for later Scanning
+; ============================================================================
 ; Parameters
-; --------------------------------------------------------------------
-; Registers  : 
-; Variables  : 
-; --------------------------------------------------------------------
+; ----------------------------------------------------------------------------
+; Registers  : [in]    z               Pointer to the Partitondata
+; Variables  : [out]   fat_partfound   Boolean for "Partition found"\r
+;              [out]   fat_parttbl     Pointer to Partitiontable
+; ----------------------------------------------------------------------------
 ; Description:
-; This funktion sets the internal Variables to set Start and Size
+; This funktion sets the internal Variables to the Start and Size
 ; of a given FAT16 Paritition. This Information will be used for a
 ; later scanning of the Partition. See Function "fat_scan_partition"
 ; for more information. 
-; ====================================================================
+; ============================================================================
 fat_add_partition:
        
 .if FAT16_DEBUG > 0
@@ -128,39 +169,39 @@ fat_add_partition:
 ;   save data from first fat16 partition
        ldiw    y,fat_parttbl
 
-       ldd             temp,z+PART_START
-       st              y+,temp
-       ldd             temp,z+PART_START+1
-       st              y+,temp
-       ldd             temp,z+PART_START+2
-       st              y+,temp
-       ldd             temp,z+PART_START+3
-       st              y+,temp
+       ldd     temp,z+PART_START
+       st      y+,temp
+       ldd     temp,z+PART_START+1
+       st      y+,temp
+       ldd     temp,z+PART_START+2
+       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
+       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
+       pop     yh
        pop     yl
 
        ret
 
-; --------------------------------------------------------------------------- 
+; ============================================================================
 ; Read and Scann a FAT16 Partition for Imagedatefiles 
-; --------------------------------------------------------------------------- 
+; ============================================================================
+; Registers    : none
+; Variables    : none
+; ----------------------------------------------------------------------------
 ; This Routine reads the Bootblock and scanns it for a Diskimage
-; Input Registers  : none
-; Output Registers : none
-; Changes Variables: none
-; --------------------------------------------------------------------------- 
+; ============================================================================
 
 
 fat_scan_partition:
@@ -171,7 +212,7 @@ fat_scan_partition:
 .endif
 
 ; Check if a FAT16 Partition was realy found
-       lds             yl,fat_partfound
+       lds     yl,fat_partfound
        cpi     yl,1    
        brne    fat_scan_error 
 
@@ -182,8 +223,8 @@ fat_scan_partition:
 .endif
 
 ; Check for free Entrys in Partition table
-       lds             yl,ndisks
-       cpi             yl,MAXDISKS
+       lds     yl,ndisks
+       cpi     yl,MAXDISKS
        breq    fat_scan_error
 
 .if FAT16_DEBUG > 0
@@ -193,19 +234,19 @@ fat_scan_partition:
 
 ; Scan partition start
        ldiw    z,fat_parttbl   
-       ldd             xl,z+0          
-       ldd             xh,z+1
-       ldd             yl,z+2
-       ldd             yh,z+3
+       ldd     xl,z+0          
+       ldd     xh,z+1
+       ldd     yl,z+2
+       ldd     yh,z+3
 
 ; Load first sector from Partition
        rcall   mmcReadSect
-       tst         temp
+       tst     temp
        breq    fat_bootblock_check
 
 ; Read error: Block not found
 fat_scan_error:
-       clr temp
+       clr     temp
        ret
 
 fat_bootblock_check:
@@ -215,7 +256,7 @@ fat_bootblock_check:
        printnewline
 .endif
 
-; -> Size of Sectors fixed at 512 Bytes
+; 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
@@ -230,9 +271,9 @@ fat_bootblock_check:
 ;      printnewline
 ;.endif
 
-;   Anzahl der Sectoren pro Cluster lesen
+;      get sectors per cluster from bootblock
        ldiw    z,hostbuf+FAT16_BSO_CLUSTSZ
-       ld              temp,z
+       ld      temp,z
        sts     fat_clustersize,temp
 
 .if FAT16_DEBUG > 0
@@ -241,12 +282,12 @@ fat_bootblock_check:
        printnewline
 .endif
 
-;   Anzahl der reservierten Sectoren
+;      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
+       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
@@ -254,12 +295,12 @@ fat_bootblock_check:
        printnewline
 .endif
 
-;   Anzahl der Sectoren pro FAT
+;      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
+       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
@@ -267,10 +308,10 @@ fat_bootblock_check:
        printnewline
 .endif
 
-;   Anzahl der FAT kopien
+;      get num of FAT Tables from bootblock
        ldiw    z,hostbuf+FAT16_BSO_NUMFATCP
-       ld              temp,z
-       sts     fat_numfatcp,temp                       ; low byte
+       ld      temp,z
+       sts     fat_numfatcp,temp               ; low byte
 
 .if FAT16_DEBUG > 0
        printstring "Ammount of FAT copies: ",0
@@ -278,12 +319,12 @@ fat_bootblock_check:
        printnewline
 .endif
 
-;   Max. Anzahl der Dir. Enträge im Root Verz.
+;      get max num of entrys in root direktory from bootblock
        ldiw    z,hostbuf+FAT16_BSO_NUMDIRENT
-       ld              temp,z+
-       sts     fat_numdirentrys,temp                   ; low byte
-       ld              temp2,z
-       sts             fat_numdirentrys+1,temp2                ; high byte
+       ld      temp,z+
+       sts     fat_numdirentrys,temp           ; low byte
+       ld      temp2,z
+       sts     fat_numdirentrys+1,temp2        ; high byte
 
 .if FAT16_DEBUG > 0
        printstring "Max. entrys in Rootdir.: ",0
@@ -291,95 +332,95 @@ fat_bootblock_check:
        printnewline
 .endif
 
-; Print begin of Volume
+;      Print begin of Volume
 .if FAT16_DEBUG > 1
 
        ldiw    z,fat_parttbl   
-       ldd             xl,z+0          
-       ldd             xh,z+1
-       ldd             yl,z+2
-       ldd             yh,z+3
+       ldd     xl,z+0          
+       ldd     xh,z+1
+       ldd     yl,z+2
+       ldd     yh,z+3
 
        printstring "Begin of Volume at: ",0
-       mov             temp ,yl
-       mov             temp2,yh
-       rcall printhexw
-       mov             temp ,xl
-       mov             temp2,xh
-       rcall printhexw
+       mov     temp ,yl
+       mov     temp2,yh
+       rcall   printhexw
+       mov     temp ,xl
+       mov     temp2,xh
+       rcall   printhexw
        printnewline
 .endif
 
-; Calculate begin of FAT within the Volume
-       lds             temp ,fat_ressectors
-       lds             temp2,fat_ressectors+1
+;      Calculate begin of FAT within the Volume
+       lds     temp ,fat_ressectors
+       lds     temp2,fat_ressectors+1
 
        ldiw    z,fat_parttbl   
-       ldd             xl,z+0          
-       ldd             xh,z+1
-       ldd             yl,z+2
-       ldd             yh,z+3
+       ldd     xl,z+0          
+       ldd     xh,z+1
+       ldd     yl,z+2
+       ldd     yh,z+3
 
-       add             xl,temp
+       add     xl,temp
        adc     xh,temp2
-       adc             yl,_0
-       adc             yh,_0
+       adc     yl,_0
+       adc     yh,_0
 
-       sts             fat_ptr2fat  ,xl
-       sts             fat_ptr2fat+1,xh
-       sts             fat_ptr2fat+2,yl
-       sts             fat_ptr2fat+3,yh
+       sts     fat_ptr2fat  ,xl
+       sts     fat_ptr2fat+1,xh
+       sts     fat_ptr2fat+2,yl
+       sts     fat_ptr2fat+3,yh
 
 .if FAT16_DEBUG > 1
        printstring "Begin of FAT at___: ",0
-       mov             temp ,yl
-       mov             temp2,yh
-       rcall printhexw
-       mov             temp ,xl
-       mov             temp2,xh
-       rcall printhexw
+       mov     temp ,yl
+       mov     temp2,yh
+       rcall   printhexw
+       mov     temp ,xl
+       mov     temp2,xh
+       rcall   printhexw
        printnewline
 .endif
 
 ; Calculate begin of Root- Directory within the Volume
        ldiw    z,fat_ptr2fat
-       ldd             xl,z+0
-       ldd             xh,z+1
-       ldd             yl,z+2
-       ldd             yh,z+3
+       ldd     xl,z+0
+       ldd     xh,z+1
+       ldd     yl,z+2
+       ldd     yh,z+3
 
-       lds             temp ,fat_secperfat
-       lds             temp2,fat_secperfat+1
-       lds             temp3,fat_numfatcp
+       lds     temp ,fat_secperfat
+       lds     temp2,fat_secperfat+1
+       lds     temp3,fat_numfatcp
 
 fat_calc_dp_loop:
-       cp              temp3,_0
+       cp      temp3,_0
        breq    fat_calc_dp_lend
 
-       add             xl,temp
-       adc             xh,temp2
-       adc             yl,_0
-       adc             yh,_0
+       add     xl,temp
+       adc     xh,temp2
+       adc     yl,_0
+       adc     yh,_0
 
-       dec             temp3
+       dec     temp3
 
-       jmp             fat_calc_dp_loop
+       jmp     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_ptr2dir  ,xl
+       sts     fat_ptr2dir+1,xh
+       sts     fat_ptr2dir+2,yl
+       sts     fat_ptr2dir+3,yh
 
 
 .if FAT16_DEBUG > 1
        printstring "Begin of DIR at___: ",0
-       mov             temp ,yl
-       mov             temp2,yh
-       rcall printhexw
-       mov             temp ,xl
-       mov             temp2,xh
-       rcall printhexw
+       mov     temp ,yl
+       mov     temp2,yh
+       rcall   printhexw
+       mov     temp ,xl
+       mov     temp2,xh
+       rcall   printhexw
        printnewline
 .endif
 
@@ -388,123 +429,214 @@ fat_calc_dp_lend:
 
 ; Sectorsize is fixed at 512 Bytes, makes 16 Entrys per Sektor
 
-       lds     zl,fat_numdirentrys                     ; low byte
-       lds             zh,fat_numdirentrys+1           ; high byte
+       lds     zl,fat_numdirentrys             ; low byte
+       lds     zh,fat_numdirentrys+1           ; high byte
 
 ;   Num. Direntrys / 16
-       lsr             zh
-       ror             zl
-       lsr             zh
-       ror             zl
-       lsr             zh
-       ror             zl
-       lsr             zh
-       ror             zl
-
-       lds             xl,fat_ptr2dir
-       lds             xh,fat_ptr2dir+1
-       lds             yl,fat_ptr2dir+2
-       lds             yh,fat_ptr2dir+3
-
-       add             xl,zl
-       adc             xh,zh
-       adc             yl,_0
-       adc             yh,_0
-
-       sts             fat_ptr2dat  ,xl
-       sts             fat_ptr2dat+1,xh
-       sts             fat_ptr2dat+2,yl
-       sts             fat_ptr2dat+3,yh
+       lsr     zh
+       ror     zl
+       lsr     zh
+       ror     zl
+       lsr     zh
+       ror     zl
+       lsr     zh
+       ror     zl
+
+       lds     xl,fat_ptr2dir
+       lds     xh,fat_ptr2dir+1
+       lds     yl,fat_ptr2dir+2
+       lds     yh,fat_ptr2dir+3
+
+       add     xl,zl
+       adc     xh,zh
+       adc     yl,_0
+       adc     yh,_0
+
+       sts     fat_ptr2dat  ,xl
+       sts     fat_ptr2dat+1,xh
+       sts     fat_ptr2dat+2,yl
+       sts     fat_ptr2dat+3,yh
 
 .if FAT16_DEBUG > 1
        printstring "Begin of Data at__: ",0
-       mov             temp ,yl
-       mov             temp2,yh
-       rcall printhexw
-       mov             temp ,xl
-       mov             temp2,xh
-       rcall printhexw
+       mov     temp ,yl
+       mov     temp2,yh
+       rcall   printhexw
+       mov     temp ,xl
+       mov     temp2,xh
+       rcall   printhexw
        printnewline
 .endif
 
 ; Here Starts the Scann of the Directory for valid image Files.
 
-       lds             xl,fat_ptr2dir
-       lds             xh,fat_ptr2dir+1
-       lds             yl,fat_ptr2dir+2
-       lds             yh,fat_ptr2dir+3
-
-;  Load first sector from Directory
+;      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
+;  Load sector from Directory
        call    mmcReadSect
-       tst         temp
+       tst     temp
        breq    fat_look_for_images
 
 ; Read error: Block not found
-       clr temp
+       clr     temp
        ret
 
 ; Looks at a read directory block for image entrys
 fat_look_for_images:
        
        ldiw    z,hostbuf
-       ldi             temp2,0
+       ldi     temp2,0
 
 fat_look_for_loop:     
        ldd     temp,z+0
-       cpi             temp,'C'
+       cpi     temp,'C'
        brne    fat_look_not_ok
        
-       ldd             temp,z+1
-       cpi             temp,'P'
+       ldd     temp,z+1
+       cpi     temp,'P'
        brne    fat_look_not_ok
 
-       ldd             temp,z+2
-       cpi             temp,'M'
+       ldd     temp,z+2
+       cpi     temp,'M'
        brne    fat_look_not_ok
 
-       ldd             temp,z+3
-       cpi             temp,'D'
+       ldd     temp,z+3
+       cpi     temp,'D'
        brne    fat_look_not_ok
 
-       ldd             temp,z+4
-       cpi             temp,'S'
+       ldd     temp,z+4
+       cpi     temp,'S'
        brne    fat_look_not_ok
 
-       ldd             temp,z+5
-       cpi             temp,'K'
+       ldd     temp,z+5
+       cpi     temp,'K'
        brne    fat_look_not_ok
 
-       ldd             temp,z+6
-       cpi             temp,'_'
+       ldd     temp,z+6
+       cpi     temp,'_'
        brne    fat_look_not_ok
 
-       ldd             temp,z+8
-       cpi             temp,'I'
+       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
+       ldd     temp,z+8
+       cpi     temp,'I'
        brne    fat_look_not_ok
 
-       ldd             temp,z+9
-       cpi             temp,'M'
+       ldd     temp,z+9
+       cpi     temp,'M'
        brne    fat_look_not_ok
 
-       ldd             temp,z+10
-       cpi             temp,'G'
+       ldd     temp,z+10
+       cpi     temp,'G'
        brne    fat_look_not_ok
 
-       jmp             fat_store_new_entry
-
+       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
 fat_look_not_ok:
        
-       //ldi           temp,32
-       addiw   z,32                    
-
-       inc             temp2
-       cpi             temp2,16                                ; max entrys/sector
+.if FAT16_DEBUG > 1\r
+       printstring "+",0\r
+.endif\r
+       \r
+       adiw    z,32\r
+
+       inc     temp2
+       cpi     temp2,16                                ; max entrys/sector
        breq    fat_scan_next_sector
        jmp     fat_look_for_loop
 
 fat_scan_next_sector:
 
-       ret
+.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
 
 
 ;      Create new Partition Entry
@@ -512,36 +644,52 @@ fat_store_new_entry:
 
 ;   Found a valid image
 .if FAT16_DEBUG > 1
-       printstring "Found a valid Image ! ",0
+       printstring "Found a valid Image ! Z=",0\r
+       mov     temp ,zl\r
+       mov     temp2,zh\r
+       rcall   printhexw\r
        printnewline
 .endif
 
        ldiw    y,hostparttbl
-       lds             temp,ndisks
+       lds     temp,ndisks
 
 fat_look_store_loop:
-       cp              temp,_0
+       cp      temp,_0
        breq    fat_look_store
 
        adiw    y,PARTENTRY_SIZE
-       dec             temp
-       jmp             fat_look_store_loop
+       dec     temp
+       jmp     fat_look_store_loop
 
 fat_look_store:
 ;   Set Type of Partition to FAT16- Fileimage
-       ldi temp,dskType_FAT
+       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
+       st      y+,temp\r
 
-;   Filesize in Bytes - 2,4,8,16,32,64,128,256,512 
+.if FAT16_DEBUG > 1\r
+       printnewline\r
+.endif
+
+;   Filesize in Bytes\r
 ;      ldd     temp,z+0x1C
 ;      st      y+,temp
 ;      ldd     temp,z+0x1D
@@ -552,66 +700,69 @@ fat_look_store:
 ;      st      y+,temp
 
 ;   Convert Filesize to ammount of sectors
+;   (calc with 512byte/sector)
        ldd     xl,z+0x1D
        ldd     xh,z+0x1E
        ldd     zl,z+0x1F
        mov     zh,_0
 
        lsr     zh
-       ror zl
-       ror xh
-       ror xl
+       ror     zl
+       ror     xh
+       ror     xl
 
 ;   store ammount of sectors in partitiontable 
-       st  y+,xl
-       st  y+,xh
-       st  y+,zl
-       st  y+,zh
-
+       st      y+,xl
+       st      y+,xh
+       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
 ; 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
 
-.if FAT16_DEBUG > 1
-; Test finding of the first sector
-       ldd     xl,z+0x1A
-       ldd     xh,z+0x1B
-       ldi     zl,0
-
-       rcall   fat_gethostsec
-
-       printstring "Begin of Image at: ",0
-       mov             temp ,yl
-       mov             temp2,yh
-       rcall printhexw
-       mov             temp ,xl
-       mov             temp2,xh
-       rcall printhexw
-       printnewline
+       jmp     fat_scan_for_more       \r
 
-.endif
-       
-;      cp              temp,MAXDISKS
-;      brne    fat_scan_for_more       
+fat_scan_end:\r
                
        ret
 
 
-; ====================================================================
+; ============================================================================
 ; Function: Cluster to HostSector 
-; ====================================================================
-; Parameters: [in]     xh,xl                   Cluster Number
-;                        [out] yh,yl,xh,xl             Sector Number on Disk
-; --------------------------------------------------------------------
+; ============================================================================
+; Parameters:  [in]    xh,xl           Cluster Number
+;              [out]   yh,yl,xh,xl     Sector Number on Disk
+; ----------------------------------------------------------------------------
 ; Registers  :         
 ; Variables  :         [used]  fat_clustersize Ammount of Sectors per Cluster
-;                              [changes] temp
-; --------------------------------------------------------------------
+;              [changes] temp
+; ----------------------------------------------------------------------------
 ; Description:
 ; ! Only works with Clustersizes 2,4,8,16,32,64,128 !
-; ====================================================================
+; ============================================================================
 fat_gethostsec:
 
 ;      Get Offset into Data area of Disk
@@ -619,48 +770,50 @@ fat_gethostsec:
 
 
 ;      add begin of data area to offset
-       lds             temp,fat_ptr2dat+0
-       add             xl,temp
-       lds             temp,fat_ptr2dat+1
-       adc             xh,temp
-       lds             temp,fat_ptr2dat+2
-       adc             yl,temp
-       lds             temp,fat_ptr2dat+3
-       adc             yh,temp
+       lds     temp,fat_ptr2dat+0
+       add     xl,temp
+       lds     temp,fat_ptr2dat+1
+       adc     xh,temp
+       lds     temp,fat_ptr2dat+2
+       adc     yl,temp
+       lds     temp,fat_ptr2dat+3
+       adc     yh,temp
        ret
 
-; ====================================================================
+; ============================================================================
 ; Function: Cluster to Sector 
-; ====================================================================
-; Parameters: [in]     xl,xh                   Cluster Number
-;                        [out] xl,xh,yl,yh             Sector Number
-; --------------------------------------------------------------------
-; Registers  :         
-; Variables  :         [used]  fat_clustersize Ammount of Sectors per Cluster
-;                              [changes] temp
-; --------------------------------------------------------------------
-; Description:
-; ! Only works with Clustersizes 2,4,8,16,32,64,128 !
-; ====================================================================
+; ============================================================================
+; Registers:   [in]    xl,xh                   Cluster Number
+;              [out]   xl,xh,yl,yh             Sector Number
+; 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
+;
+; ! Only works with Clustersizes 2,4,8,16,32,64,128,512,1024 !
+; ============================================================================
 fat_clusttosec:
        clr     yl
-       clr             yh
+       clr     yh
 
-       ldi             temp,2
-       sub             xl,temp         ; Substract the 2 reserved clusters
-       sbc             xh,_0
+       ldi     temp,2
+       sub     xl,temp         ; Substract the 2 reserved clusters
+       sbc     xh,_0
 
-       lds             temp,fat_clustersize
+       lds     temp,fat_clustersize
+       lsr     temp
 
 fat_c2s_loop:
-       tst             temp
+       tst     temp
        breq    fat_c2s_end
-       lsr             temp
+       lsr     temp
 
-       lsl             xl
-       rol             xh
-       rol             yl
-       rol             yh
+       lsl     xl
+       rol     xh
+       rol     yl
+       rol     yh
        rjmp    fat_c2s_loop
 
 fat_c2s_end:
@@ -669,28 +822,41 @@ fat_c2s_end:
 ; ====================================================================
 ; Function: Searches a physical Cluster, given the logical Cluster
 ; ====================================================================
-; Parameters
-; --------------------------------------------------------------------
-; Registers  : [r] xh,xl               logical- Cluster 
-;                         [w] yh,yl            physical- Cluster
+; Registers:   [in]    xh,xl   logical- Cluster 
+;              [out]   yh,yl   physical- Cluster
+; Variables:\r
 ; --------------------------------------------------------------------
 ; Description:
 ; ====================================================================
 fat_find_phsy_clust:
        mov     temp2,xl
-       lds             xl,hostdsk
+       lds     xl,hostdsk
 
        rcall   dsk_getpartentry        ; get partition entry
        mov     xl,temp2
 
 ;      Get First FAT- Cluster Number of Diskimage
        
-       ldd             xl,z+1
-       ldd             xh,z+2
+       ldd     yl,z+1
+       ldd     yh,z+2
+
+.if FAT16_DBG_FAT > 0
+       printstring "Search log. Cluster ",0
+       mov     temp,xl
+       mov     temp2,xh
+       rcall   printhexw
+       printnewline
+               
+       printstring "Search phys. Cluster ",0
+       mov     temp ,yl
+       mov     temp2,yh
+       rcall   printhexw
+       printnewline
+.endif
 
 fat_next_phsy_clust:   
-       cp              xl,_0
-       cpc             xh,_0
+       cp      xl,_0
+       cpc     xh,_0
        breq    fat_found_phsy_clust
 ;      Get Next Cluster from Fat
 
@@ -701,109 +867,106 @@ fat_next_phsy_clust:
 ;      in      zh,zl:          Pointer to Word within the Sector to read       
 ;   in yh..xl: Start sector number (LBA)
 ;      out     zh,zl   : word thats been read
-       push xl
-       push xh
+       push    xl
+       push    xh
 
 ;   Create FAT Offset Value
        clr     zh
-       mov zl,yl
+       mov     zl,yl
        lsl     zl
-       rol zh
+       rol     zh
 ;   Get FAT Start
-       mov temp,yh
-       lds xl,fat_ptr2fat
-       lds xh,fat_ptr2fat+1
-       lds yl,fat_ptr2fat+2
-       lds yh,fat_ptr2fat+3
-;   Add Sector offset
+       mov     temp,yh
+       lds     xl,fat_ptr2fat
+       lds     xh,fat_ptr2fat+1
+       lds     yl,fat_ptr2fat+2
+       lds     yh,fat_ptr2fat+3
+;   Add Cluster offset within sector
        add     xl,temp
        adc     xh,_0
-       adc yl,_0
-       adc yh,_0
-       call mmcReadWord
+       adc     yl,_0
+       adc     yh,_0
+       call    mmcReadWord
 
-       pop xh
-       pop xl
+       pop     xh
+       pop     xl
 
-       mov yl,zl
-       mov yh,zh
+       mov     yl,zl
+       mov     yh,zh
 
 ;      Check next logical Cluster
-       ldi             zl,1
-       sub             xl,zl
-       sbc             xh,_0
+       ldi     zl,1
+       sub     xl,zl
+       sbc     xh,_0
        rjmp    fat_next_phsy_clust
        
 ; Found the physical cluster
 fat_found_phsy_clust:  
+.if FAT16_DBG_FAT > 0
+       printstring "Found phys. Cluster at:",0
+       mov     temp,yl
+       mov     temp2,yh
+       rcall   printhexw
+       printnewline
+.endif 
+
        ret
 
-; ====================================================================
-; 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
-; hostdsk = host disk #,  (partition #)
-; hostlba = host block #, relative to partition start 
-; Read/Write "hostsize" bytes to/from hostbuf
-; --------------------------------------------------------------------
+; ============================================================================
+; Function: This Routine searches for the Sector within an Imagefile 
+; ============================================================================
+; Registers:   [out] xl,xh,yl,yh       Pointer to the Sector on the SD-Card\r
+;              [out] temp              Error- Variable (0= No Error)
+; Variables:   [in] hostdsk            host disk #,  (partition #)
+;              [in] hostlba            host block #, relative to part.start\r
+;              [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\r
+; ----------------------------------------------------------------------------
 ; Description:
-; ====================================================================
+; This Routine uses the variables hostdsk and hostlba to find an Sector\r
+; in the Imagefile.\r
+; The CP/M Sector given within "hostlba" are splited to a logical Cluster-\r
+; Number and the Subsector within this logical Cluster.\r
+; logical Cluster Number = hostlba / fat_clustersize\r
+; The logical Cluster Number will be compared to the logical Cluster- Number\r
+; within the Cache. When this Clusters are the same and the DiskID's are\r
+; also the same, then the cached physical Sector will be used.\r
+; When the Clusters or the Disks don't match, a seek for the physical\r
+; Cluster is performed. This seek is done thru an access over the FAT of\r
+; the FAT16 Partition. The Routine starts at the first Cluster of the \r
+; Imagefile and goes along the linked list of Clusternumber till it reaches\r
+; the searched cluster. The found Clusternumber will be used to calculate\r
+; the Sektor where this Cluster lies on the SD- Card. Both the found physical\r
+; Cluster and the logical Cluster together with the physical Sectornumber\r
+; are stored in the cache.\r
+; The last step done is to add the SubSectorOffset to the found physical\r
+; Sector. This gives the pointer to the Sector to be read and or written.\r
+; ============================================================================
 
 fat_hostparam:
-       lds             xl,hostdsk
+       lds     xl,hostdsk
 
-.if HOSTRW_DEBUG
-       mov     temp,xl
-       subi    temp,-('A')
-       rcall   uartputc
-       printstring ": "
-.endif
 
-       rcall dsk_getpartentry  ; get partition entry
+       rcall   dsk_getpartentry        ; get partition entry
 
 fat_hostlend:
-       lds             temp ,hostlba
-       lds             temp2,hostlba+1
-       lds             temp3,hostlba+2
+       lds     temp ,hostlba
+       lds     temp2,hostlba+1
+       lds     temp3,hostlba+2
 
-.if HOSTRW_DEBUG
-       printstring "lba: "
-       clr             temp4
-       rcall   print_ultoa
-.endif
 
-       ldd             xl,z+5                  ; get size of disk in sectors
-       ldd             xh,z+6
-       ldd             yl,z+7
+       ldd     xl,z+5                  ; get size of disk in sectors
+       ldd     xh,z+6
+       ldd     yl,z+7
        
-       cp              temp,xl                 ; check given sector against disksize
-       cpc             temp2,xh
-       cpc             temp3,yl
+       cp      temp,xl                 ; check given sector against disksize
+       cpc     temp2,xh
+       cpc     temp3,yl
        brcs    fat_hp1
        
-.if HOSTRW_DEBUG
-       printstring ", max: "
-       push    temp4
-       push    temp3
-       push    temp2
-       push    temp
-       movw    temp,x
-       mov             temp3,yl
-       clr             temp4
-       rcall   print_ultoa
-       pop             temp
-       pop             temp2
-       pop             temp3
-       pop             temp4
-       printstring " "
-.endif
-       
-       clr             temp
+       clr     temp
        ret
 
 fat_hp1:
@@ -815,125 +978,133 @@ 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
+       tst     zl
        breq    fat_found_clst
 
-       lsr             yh
-       ror             yl
-       ror             xh
-       ror             xl
+       lsr     yh
+       ror     yl
+       ror     xh
+       ror     xl
        
-       lsr             zl
+       lsr     zl
 
        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
+       mov     yl,xl
+       lds     zl,fat_clustersize
+       lsr     zl
 fat_search_clst_lp2:
-       tst             zl
+       tst     zl
        breq    fat_found_subsec
-       lsl             yl
+       lsl     yl
 
-       lsr             zl
+       lsr     zl
        rjmp    fat_search_clst_lp2             
 
 fat_found_subsec:
-       mov             zl,temp
-       sub             zl,yl
-       sts             fat_clust_offset,zl
+       mov     zl,temp
+       sub     zl,yl
+       sts     fat_clust_offset,zl
 
 ; Check against last HOSTDISK searched
-       lds             yl,fat_last_dsk
-       lds             yh,hostdsk
-       cp              yl,yh
+       lds     yl,fat_last_dsk
+       lds     yh,hostdsk
+       cp      yl,yh
        brne    fat_wrong_cache_clst
 
 ; Check against last Cluster searched
-       lds             yl,fat_log_clust
-       lds             yh,fat_log_clust+1
+       lds     yl,fat_log_clust
+       lds     yh,fat_log_clust+1
 
-       cp              yl,xl
+       cp      yl,xl
        brne    fat_wrong_cache_clst
-       cp              yh,xh
+       cp      yh,xh
        brne    fat_wrong_cache_clst
 
 ;   Last Cluster = searched Cluster -> get Sectornumber from cache
-       lds             xl,fat_clust_ptr
-       lds             xh,fat_clust_ptr+1
-       lds             yl,fat_clust_ptr+2
-       lds             yh,fat_clust_ptr+3
+       lds     xl,fat_clust_ptr
+       lds     xh,fat_clust_ptr+1
+       lds     yl,fat_clust_ptr+2
+       lds     yh,fat_clust_ptr+3
 
        rjmp    fat_add_offset
 
-; ################# Cluster is not in cache, so we must search for it
+;  Cluster is not in cache, so we must search for it
 fat_wrong_cache_clst:
-       lds             yl,hostdsk
-       sts             fat_last_dsk,yl
-       sts             fat_log_clust,xl
-       sts             fat_log_clust+1,xh
+       lds     yl,hostdsk
+       sts     fat_last_dsk,yl
+       sts     fat_log_clust,xl
+       sts     fat_log_clust+1,xh
 
-; ################# Map Logical Cluster-Number to "Physical" Cluster Number using the FAT
+;  Map Logical Cluster-Number to "Physical" Cluster Number using the FAT
        rcall   fat_find_phsy_clust
 
-; ################# Get StartSector of "physical" Cluster
+;  Get StartSector of "physical" Cluster
        mov     xl,yl
-       mov             xh,yh
+       mov     xh,yh
        rcall   fat_gethostsec
+; Found the physical sector
+.if FAT16_DBG_FAT > 0
+       printstring "Found phys. Sector at:",0
+       mov     temp,yl
+       mov     temp2,yh
+       rcall   printhexw
+       mov     temp,xl
+       mov     temp2,xh
+       rcall   printhexw
+       printnewline
+.endif 
 
 ;   Save the found Sector for later use into cache
-       sts             fat_clust_ptr  ,xl
-       sts             fat_clust_ptr+1,xh
-       sts             fat_clust_ptr+2,yl
-       sts             fat_clust_ptr+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
 
 ;   Add- Subsector to Startsector 
 fat_add_offset:
-       lds             zl,fat_clust_offset
-       add             xl,zl
-       adc             xh,_0
-       adc             yl,_0
-       adc             yh,_0
-
-.if HOSTRW_DEBUG
-       printstring ", abs:"
-       push    temp4
-       push    temp3
-       push    temp2
-       push    temp
-       movw    temp,x
-       movw    temp3,y
-       rcall   print_ultoa
-       pop             temp
-       pop             temp2
-       pop             temp3
-       pop             temp4
-       printstring " "
+       lds     zl,fat_clust_offset
+       add     xl,zl
+       adc     xh,_0
+       adc     yl,_0
+       adc     yh,_0
+
+; Found the physical sector
+.if FAT16_DBG_FAT > 0
+       printstring "Sector with Offset at:",0
+       mov     temp,yl
+       mov     temp2,yh
+       rcall   printhexw
+       mov     temp,xl
+       mov     temp2,xh
+       rcall   printhexw
+       printnewline
 .endif
 
-       ori             temp,255
+       ori     temp,255
 fat_hpex:
        ret
 
-; ====================================================================
+; ============================================================================
 ; 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
-; --------------------------------------------------------------------
-; Description:
-; ====================================================================
+; ============================================================================
+; Registers:   [out]   temp    Error-Variable ( 0= No Error)           
+; Variables:   [in]    hostdsk host disk #,  (partition #)
+;              [in]    hostlba host block #, relative to part.start\r
+;              [in]    hostbuf Sector to be written
+; ----------------------------------------------------------------------------
+; Description:\r
+; This Routine writes a Sector to the Imagefile inside an FAT16 Partition.
+; ============================================================================
 
 fat_writehost:
-.if HOSTRW_DEBUG
+.if FAT16_RWDEBUG > 1
        printnewline
        printstring "host write "
 .endif
@@ -941,35 +1112,42 @@ fat_writehost:
        breq    fat_rdwr_err
        
        ;call   mmcWriteSect    ; disabled till read is functioning
-       tst             temp
+       tst     temp
        breq    fat_rdwr_ok
        
        rcall   mgr_init_partitions
-       cbr             temp,0x80
+       cbr     temp,0x80
        breq    fat_rdwr_err
 
        rcall   fat_hostparam
        breq    fat_rdwr_err
        ;call   mmcWriteSect    ; disabled till read is functioning
-       tst             temp
+       tst     temp
        brne    fat_rdwr_err
        rjmp    fat_rdwr_ok
 
-; ====================================================================
+fat_rdwr_ok:
+       sts     erflag,_0
+       ret
+
+fat_rdwr_err:
+       sts     erflag,_255
+       ret\r
+
+; ============================================================================
 ; Function: Does a Disk read operation
-; ====================================================================
-; Parameters
-; --------------------------------------------------------------------
-; Registers  : none
-; Variables  : [r] seekdsk             Number of Disk to Read
-;                         [r] seeksec          Sector to read
-;              [r] seektrk             Track  to read
-; --------------------------------------------------------------------
-; Description:
-; ====================================================================
+; ============================================================================
+; Registers:   none
+; Variables:   [in]    hostdsk         host disk #,  (partition #)
+;              [in]    hostlba         host block #, relative to part.start\r
+;              [out]   hostbuf         Sector read by this routine
+; ----------------------------------------------------------------------------
+; Description:\r
+; This Routine reads a Sector from the Imagefile inside an FAT16 Partition. 
+; ============================================================================
 
 fat_readhost:
-.if HOSTRW_DEBUG
+.if FAT16_RWDEBUG > 1
        printnewline
        printstring "host read  "
 .endif
@@ -978,6 +1156,7 @@ fat_readhost:
        breq    fat_rdwr_err
        
        
+.if FAT16_RWDEBUG > 0
        printstring "Read Image Sector:"
        push    temp4
        push    temp3
@@ -986,32 +1165,26 @@ fat_readhost:
        movw    temp,x
        movw    temp3,y
        rcall   print_ultoa
-       pop             temp
-       pop             temp2
-       pop             temp3
-       pop             temp4
-       printstring " "
+       pop     temp
+       pop     temp2
+       pop     temp3
+       pop     temp4
+       printnewline
+.endif
        
        call    mmcReadSect
-       tst             temp
+       tst     temp
        breq    fat_rdwr_ok
 
        rcall   mgr_init_partitions
-       cbr             temp,0x80
+       cbr     temp,0x80
        breq    fat_rdwr_err
 
        rcall   fat_hostparam
        breq    fat_rdwr_err
        call    mmcReadSect
-       tst             temp
+       tst     temp
        brne    fat_rdwr_err
 
-fat_rdwr_ok:
-       sts             erflag,_0
-       ret
-
-fat_rdwr_err:
-       sts             erflag,_255
-       ret
 #endif