📄 fat.s
字号:
.module fat.c
.area text(rom, con, rel)
.dbfile G:\IccPro\Mp3Ide\fat.c
.dbfunc e InitFat _InitFat fV
.even
_InitFat::
xcall push_gset2
sbiw R28,8
.dbline -1
.dbline 23
; #include <iom128v.h>
; #include <macros.h>
; #include <string.h>
; #include <stdio.h>
; #include "vs1001.h"
; #include "generic.h"
; #include "remote.h"
; #include "fat.h"
; #include "ata.h"
;
; //******************************************************************
; //* FAT Global Variable
; //******************************************************************
; struct PartRecord Part;
; struct BootRecord Boot;
; struct FileInfo File;
; unsigned long FirstDataSector;
;
; //*************************************
; // void init_fat(void)
; //*************************************
; void InitFat(void)
; {
.dbline 24
; ata_read_sector_byte(0, 0, 446, sizeof(Part) , (unsigned char*)&Part);
ldi R24,<_Part
ldi R25,>_Part
std y+7,R25
std y+6,R24
ldi R24,16
ldi R25,0
std y+5,R25
std y+4,R24
ldi R24,446
ldi R25,1
std y+3,R25
std y+2,R24
ldi R20,0
ldi R21,0
ldi R22,0
ldi R23,0
std y+0,R22
std y+1,R23
movw R18,R20
clr R16
xcall _ata_read_sector_byte
.dbline 25
; ata_read_sector_byte(0, Part.StartLBA, 0, sizeof(Boot) , (unsigned char*)&Boot);
ldi R24,<_Boot
ldi R25,>_Boot
std y+7,R25
std y+6,R24
ldi R24,52
ldi R25,0
std y+5,R25
std y+4,R24
clr R2
clr R3
std y+3,R3
std y+2,R2
lds R4,_Part+8+2
lds R5,_Part+8+2+1
lds R2,_Part+8
lds R3,_Part+8+1
std y+0,R4
std y+1,R5
movw R18,R2
clr R16
xcall _ata_read_sector_byte
.dbline 26
; FirstDataSector = Part.StartLBA + Boot.ResSectors + (Boot.FATs * Boot.FATSize);
lds R4,_Boot+36+2
lds R5,_Boot+36+2+1
lds R2,_Boot+36
lds R3,_Boot+36+1
lds R6,_Boot+16
clr R7
clr R8
clr R9
st -y,R5
st -y,R4
st -y,R3
st -y,R2
movw R16,R6
movw R18,R8
xcall empy32u
lds R2,_Boot+14
lds R3,_Boot+14+1
clr R4
clr R5
lds R8,_Part+8+2
lds R9,_Part+8+2+1
lds R6,_Part+8
lds R7,_Part+8+1
add R6,R2
adc R7,R3
adc R8,R4
adc R9,R5
add R6,R16
adc R7,R17
adc R8,R18
adc R9,R19
sts _FirstDataSector+1,R7
sts _FirstDataSector,R6
sts _FirstDataSector+2+1,R9
sts _FirstDataSector+2,R8
.dbline -2
L1:
adiw R28,8
xcall pop_gset2
.dbline 0 ; func end
ret
.dbend
.dbfunc e Clust2LBA _Clust2LBA fl
; clust -> y+4
.even
_Clust2LBA::
xcall push_arg4
xcall push_gset2
.dbline -1
.dbline 33
; }
;
; //*************************************
; // unsigned long clust2LBA(unsigned long clust)
; //*************************************
; unsigned long Clust2LBA(unsigned long clust)
; {
.dbline 34
; return ((clust-2) * Boot.SecPerClust) + FirstDataSector;
ldi R20,2
ldi R21,0
ldi R22,0
ldi R23,0
movw R30,R28
ldd R2,z+4
ldd R3,z+5
ldd R4,z+6
ldd R5,z+7
sub R2,R20
sbc R3,R21
sbc R4,R22
sbc R5,R23
lds R6,_Boot+13
clr R7
clr R8
clr R9
st -y,R9
st -y,R8
st -y,R7
st -y,R6
movw R16,R2
movw R18,R4
xcall empy32u
movw R2,R16
movw R4,R18
lds R8,_FirstDataSector+2
lds R9,_FirstDataSector+2+1
lds R6,_FirstDataSector
lds R7,_FirstDataSector+1
add R2,R6
adc R3,R7
adc R4,R8
adc R5,R9
movw R16,R2
movw R18,R4
.dbline -2
L7:
xcall pop_gset2
adiw R28,4
.dbline 0 ; func end
ret
.dbsym l clust 4 l
.dbend
.dbfunc e GetNextCluster _GetNextCluster fl
; Fat32_Entry -> y+16
; FatSectorOffset -> y+12
; FatSectorNum -> y+8
; Clust -> y+24
.even
_GetNextCluster::
xcall push_arg4
xcall push_gset2
sbiw R28,20
.dbline -1
.dbline 49
; }
;
; //****************************************************************
; // unsigned long GetNextCluster(unsigned long Clust)
; //
; // Description:
; // Returns the FAT32 Entry for the given Cluster Number.
; // The FAT32 Entry is the next cluster number of the cluster
; // chain. **May return a termination
; //
; // if Fat32_Entry >= 0x0FFFFFF8 EOC (End of Chain)
; // if Fat32_Entry == 0x0FFFFFF7 Bad Cluster
; //****************************************************************
; unsigned long GetNextCluster(unsigned long Clust)
; {
.dbline 54
; unsigned long FatSectorNum;
; unsigned long FatSectorOffset;
; unsigned long Fat32_Entry;
;
; FatSectorNum = Part.StartLBA + Boot.ResSectors + ((Clust * 4) / Boot.BytesPerSec);;
movw R30,R28
ldd R2,z+24
ldd R3,z+25
ldd R4,z+26
ldd R5,z+27
ldi R20,4
ldi R21,0
ldi R22,0
ldi R23,0
st -y,R5
st -y,R4
st -y,R3
st -y,R2
movw R16,R20
movw R18,R22
xcall empy32u
lds R2,_Boot+11
lds R3,_Boot+11+1
clr R4
clr R5
st -y,R5
st -y,R4
st -y,R3
st -y,R2
xcall div32u
lds R2,_Boot+14
lds R3,_Boot+14+1
clr R4
clr R5
lds R8,_Part+8+2
lds R9,_Part+8+2+1
lds R6,_Part+8
lds R7,_Part+8+1
add R6,R2
adc R7,R3
adc R8,R4
adc R9,R5
add R6,R16
adc R7,R17
adc R8,R18
adc R9,R19
movw R30,R28
std z+8,R6
std z+9,R7
std z+10,R8
std z+11,R9
.dbline 54
.dbline 55
; FatSectorOffset = (Clust * 4) % Boot.BytesPerSec;
movw R30,R28
ldd R2,z+24
ldd R3,z+25
ldd R4,z+26
ldd R5,z+27
ldi R20,4
ldi R21,0
ldi R22,0
ldi R23,0
st -y,R5
st -y,R4
st -y,R3
st -y,R2
movw R16,R20
movw R18,R22
xcall empy32u
lds R2,_Boot+11
lds R3,_Boot+11+1
clr R4
clr R5
st -y,R5
st -y,R4
st -y,R3
st -y,R2
xcall mod32u
movw R30,R28
std z+12,R16
std z+13,R17
std z+14,R18
std z+15,R19
.dbline 57
;
; ata_read_sector_byte(0, FatSectorNum, FatSectorOffset, 4 , (unsigned char*)&Fat32_Entry);
movw R24,R28
adiw R24,16
std y+7,R25
std y+6,R24
ldi R24,4
ldi R25,0
std y+5,R25
std y+4,R24
movw R30,R28
ldd R2,z+12
ldd R3,z+13
std y+3,R3
std y+2,R2
movw R30,R28
ldd R2,z+8
ldd R3,z+9
ldd R4,z+10
ldd R5,z+11
std y+0,R4
std y+1,R5
movw R18,R2
clr R16
xcall _ata_read_sector_byte
.dbline 59
;
; return(Fat32_Entry & 0x0FFFFFFF);
ldi R20,255
ldi R21,255
ldi R22,255
ldi R23,15
movw R30,R28
ldd R2,z+16
ldd R3,z+17
ldd R4,z+18
ldd R5,z+19
and R2,R20
and R3,R21
and R4,R22
and R5,R23
movw R16,R2
movw R18,R4
.dbline -2
L9:
adiw R28,20
xcall pop_gset2
adiw R28,4
.dbline 0 ; func end
ret
.dbsym l Fat32_Entry 16 l
.dbsym l FatSectorOffset 12 l
.dbsym l FatSectorNum 8 l
.dbsym l Clust 24 l
.dbend
.dbfunc e GetDir _GetDir fI
; k -> <dead>
; NextCluster -> y+49
; LBA -> y+45
; LinePtr -> y+44
; i -> y+40
; LongFileName -> R14,R15
; Files -> y+55
; Offset -> y+53
; LongFilePtr -> R10,R11
; Entry -> y+8
; j -> R12,R13
; Display -> y+75
; Qte -> y+73
; From -> y+71
; Cluster -> y+67
.even
_GetDir::
xcall push_arg4
xcall push_gset5
sbiw R28,57
.dbline -1
.dbline 67
; }
;
; //*************************************
; // int GetDir(unsigned long Cluster, unsigned int FileWanted, unsigned int Type)
; //
; //*************************************
; int GetDir(unsigned long Cluster, unsigned int From, unsigned int Qte, unsigned int Display)
; {
.dbline 80
; extern unsigned char SectorBuffer[512];
; unsigned long LBA,i,NextCluster;
; unsigned int Offset,j,k,LongFilePtr,LongFileName,Files;
; unsigned char LinePtr;
; union FileEntry
; {
; struct DirEntry Dir;
; struct WinEntry Win;
; };
;
; union FileEntry Entry;
;
; NextCluster = Cluster;
movw R30,R28
subi R30,189 ; addi 67
sbci R31,255
ldd R2,z+0
ldd R3,z+1
ldd R4,z+2
ldd R5,z+3
movw R30,R28
std z+49,R2
std z+50,R3
std z+51,R4
std z+52,R5
.dbline 81
; Files = 0;
clr R0
clr R1
std y+56,R1
std y+55,R0
.dbline 82
; LinePtr = 0;
clr R2
std y+44,R2
xjmp L16
L15:
.dbline 84
; while (1)
; {
.dbline 85
; LBA = Clust2LBA(NextCluster);
movw R30,R28
ldd R16,z+49
ldd R17,z+50
ldd R18,z+51
ldd R19,z+52
xcall _Clust2LBA
movw R30,R28
std z+45,R16
std z+46,R17
std z+47,R18
std z+48,R19
.dbline 86
; for (i=0;i<Boot.SecPerClust;i++)
ldi R20,0
ldi R21,0
ldi R22,0
ldi R23,0
movw R30,R28
std z+40,R20
std z+41,R21
std z+42,R22
std z+43,R23
xjmp L21
L18:
.dbline 87
; {
.dbline 88
; ata_read_sector_byte(0, LBA+i, 0, 512, (unsigned char*)&SectorBuffer);
ldi R24,<_SectorBuffer
ldi R25,>_SectorBuffer
std y+7,R25
std y+6,R24
ldi R24,512
ldi R25,2
std y+5,R25
std y+4,R24
clr R2
clr R3
std y+3,R3
std y+2,R2
movw R30,R28
ldd R2,z+40
ldd R3,z+41
ldd R4,z+42
ldd R5,z+43
movw R30,R28
ldd R6,z+45
ldd R7,z+46
ldd R8,z+47
ldd R9,z+48
add R6,R2
adc R7,R3
adc R8,R4
adc R9,R5
std y+0,R8
std y+1,R9
movw R18,R6
clr R16
xcall _ata_read_sector_byte
.dbline 89
; for (Offset=0;Offset<Boot.BytesPerSec;Offset=Offset+32)
clr R0
clr R1
std y+54,R1
std y+53,R0
xjmp L26
L23:
.dbline 90
; {
.dbline 91
; memcpy((unsigned char*)&Entry,(unsigned char*)&SectorBuffer[Offset],sizeof(Entry));
ldi R24,32
ldi R25,0
std y+1,R25
std y+0,R24
ldi R24,<_SectorBuffer
ldi R25,>_SectorBuffer
ldd R18,y+53
ldd R19,y+54
add R18,R24
adc R19,R25
movw R16,R28
subi R16,248 ; offset = 8
sbci R17,255
xcall _memcpy
.dbline 92
; if (Entry.Dir.Attributes == ATTR_LONG_FILENAME)
ldd R24,y+19
cpi R24,15
breq X1
xjmp L28
X1:
.dbline 93
; {
.dbline 94
; LongFileName = TRUE;
ldi R24,1
ldi R25,0
movw R14,R24
.dbline 95
; LongFilePtr = ((Entry.Win.Cnt & 0x3f) * 13) - 13;
ldd R24,y+8
andi R24,63
ldi R25,13
mul R25,R24
movw R24,R0
sbiw R24,13
movw R10,R24
.dbline 96
clr R12
clr R13
xjmp L34
L31:
.dbline 96
movw R2,R10
movw R24,R2
adiw R24,1
movw R10,R24
ldi R16,2
ldi R17,0
movw R18,R12
xcall empy16s
movw R30,R16
movw R24,R28
adiw R24,9
add R30,R24
adc R31,R25
ldd R4,z+0
ldd R5,z+1
ldi R24,<_File
ldi R25,>_File
movw R30,R2
add R30,R24
adc R31,R25
std z+0,R4
L32:
.dbline 96
movw R24,R12
adiw R24,1
movw R12,R24
L34:
.dbline 96
; for (j=0;j<5;j++) File.Name[LongFilePtr++] = Entry.Win.Part1[j];
movw R24,R12
cpi R24,5
ldi R30,0
cpc R25,R30
brlo L31
.dbline 97
clr R12
clr R13
xjmp L39
L36:
.dbline 97
movw R2,R10
movw R24,R2
adiw R24,1
movw R10,R24
ldi R16,2
ldi R17,0
movw R18,R12
xcall empy16s
movw R30,R16
movw R24,R28
adiw R24,22
add R30,R24
adc R31,R25
ldd R4,z+0
ldd R5,z+1
ldi R24,<_File
ldi R25,>_File
movw R30,R2
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -