📄 main.c
字号:
#ifdef DEBUG_READ
printf("read %x bytes\n",len);
#endif
return 1;
}
else
#ifdef DEBUG_READ
printf("lenth=%d\n",(int)lenth);
#endif
start_clus=start_clus+i;
}
return 1;
}
//function:close a file,put 0xff in fat table
//return:-1 false,1 success
char CFCloseFile()
//char CloseFile(unsigned int file_handle)
{
xdata unsigned char i;
xdata struct sRootDir file;
if(buf_pos!=0)
{
if(WriteOneSec(write_buf,(s_fileinfo.start_clustor-2)
*bootinfo.sec_per_clust
+(1+bootinfo.fat_size*bootinfo.fat_num+bootinfo.root_dir_size*32/512)
+s_fileinfo.current_sector)==0)
{
return -1;
}
}
if(ReadOneSec(s_fileinfo.file_record_sector)==0)
{
return -1;
}
for(i=0;i<32;i++)
{
((unsigned char*)(&file))[i]=data_buf[s_fileinfo.file_pos_in_sector*32+i];
}
// memcpy((unsigned char*)(&file),data_buf+s_fileinfo.file_pos_in_sector*32,32);
file.file_size[3]=(s_fileinfo.size>>24) & 0x000000ff;
file.file_size[2]=(s_fileinfo.size>>16) & 0x000000ff;
file.file_size[1]=(s_fileinfo.size>>8) & 0x000000ff;
file.file_size[0]=s_fileinfo.size & 0x000000ff;
for(i=0;i<32;i++)
{
data_buf[s_fileinfo.file_pos_in_sector*32+i]=((unsigned char*)(&file))[i];
}
if(WriteOneSec(data_buf,s_fileinfo.file_record_sector)==0) //put file infomation in FDT table
{
return -1;
}
/*
if(ReadOneSec((s_fileinfo.start_clustor*2)/512+1)==0) //read one sector in Fat table area
{
return 0xffff;
}
for(i=(s_fileinfo.start_clustor*2)%512;i<(s_fileinfo.start_clustor*2)%512+2;i++) //wrire fat table
{
data_buf[i]=0xff;
}
if(WriteOneSec(data_buf,(s_fileinfo.start_clustor*2)/512+1)==0)
{
return 0xffff; //write fdt error
}
*/
return 1;
}
unsigned int CFFreeSpace()
{
static xdata unsigned int i,j;
static xdata unsigned int free_count;
free_count=0;
for(i=1;i<bootinfo.fat_size+1;i++)
{
if(ReadOneSec(i)==0)
{
return 0xffff;
}
for(j=0;j<512;j=j+2)
{
if((data_buf[j]+data_buf[j+1]*256)==0)
{
free_count++;
}
}
}
return free_count;
// return free_count*bootinfo.sec_per_clust*512;
}
//function: use to empty next fdt sector
//input: start sector
char EmptyDirFDT(unsigned int start_sec)
{
unsigned char i;
unsigned int j;
xdata unsigned char temp_buf[512];
for(j=0;j<512;j++)
{
temp_buf[j]=data_buf[j];
}
for(j=0;j<512;j++)
{
data_buf[j]=0;
}
for(i=0;i<bootinfo.sec_per_clust;i++)
{
if(WriteOneSec(data_buf,start_sec+i)==0)
{
return -1;
}
}
for(j=0;j<512;j++)
{
data_buf[j]=temp_buf[j];
}
return 1;
}
void main()
{
char c;
xdata char name[8];
// xdata struct sCIS cis;
// xdata struct sBoot bt;
xdata unsigned int temp;
static unsigned int fdt_pos;
static unsigned int fdt_record_pos;
xdata unsigned char aaa[500],buf[600];
static xdata unsigned char f_count='a';
xdata unsigned int un_use;
xdata unsigned int read_count;
xdata unsigned int to_read;
SystemInit();
// puts("start!");
while(1)
{
if(RI)
{
c=_getkey();
switch (c)
{
case 'h':
while(1)
{
EraseSec(0,1);
CFInit();
FatLinkEnd(1,6);
DelFatRecord(2);
ReadCIS();
CheckCFStatu();
WriteOneSec(BPB,0);
ReadOneSec(0);
FormatCF();
CFFreeSpace();
CFFindFileInRoot("R3000 ",&un_use,&un_use);
CFFindFileInDir("TRIGGER ",62,&un_use,&un_use);
CFMakeDirectory("R3000 ",0);
CFMakeSubDirectory("SYSINFO ","R3000 ");
CFDelete("R41 TXT",62,2);
CFOpenFile(62,"R41 TXT",1);
CFReadFile(buf,600,read_count,&to_read);
read_count=read_count+600;
CFWriteFile(aaa,356);
CFCloseFile();
CFGetBoot();
}
if(ReadCIS()==0)
{
// printf("error\n");
}
// for(temp=0;temp<512;temp++)
// {
// zxPutChar(data_buf[temp]);
// }
putchar(((struct sCIS*)data_buf)->cur_head_num[0]);
putchar(((struct sCIS*)data_buf)->cur_head_num[1]);
break;
case 't':
printf("test=%x\n",(int)TestRam(0x2000,0x00));
printf("test=%x\n",(int)TestRam(0x2000,0xff));
printf("test=%x\n",(int)TestRam(0x2000,0xAA));
printf("test=%x\n",(int)TestRam(0x2000,0x55));
printf("test=%x\n",TestAddr(0x2000));
break;
case 'f':
printf("format.\n");
if(FormatCF()==1)
printf("success\n");
else
printf("error\n");
break;
case 'i':
if(SerchFreeFDT(&fdt_pos,&fdt_record_pos,0))
{
printf("\n");
printf("FDT=%x\n",(unsigned int)fdt_pos);
printf("pos=%x\n",(unsigned int)fdt_record_pos);
}
else
{
printf("No free s");
}
break;
case '1':
printf("CFstatu:%x\n",CheckCFStatu());
break;
case 'e':
printf("CFerror:%x\n",CF_ERROR);
break;
case '2':
printf("CF ADDR:%x\n",CF_ID);
break;
case '3':
printf("sec num:\n");
scanf("%d",&temp);
ReadOneSec(temp);
printf("finish\n");
break;
case '4':
for(temp=0;temp<512;temp++)
{
zxPutChar(data_buf[temp]);
}
break;
case '5':
PrintBoot();
// if(WriteOneSec(BPB,512)==1)
// printf("write finished\n");
break;
case '6':
temp=CFFindFileInRoot("R3000 ",&un_use,&un_use);
printf("\nR3000=%x\n",temp);
if(temp==0xffff)
break;
temp=CFFindFileInDir("TRIGGER ",temp,&un_use,&un_use);
temp=(temp-2)*bootinfo.sec_per_clust
+(bootinfo.fat_num*bootinfo.fat_size
+bootinfo.root_dir_size*32/512+1);
printf("\nTRIGGER=%x\n",temp);
if(CFOpenFile(temp,"R41 TXT",1)==0xffff)
printf("open fail\n");
printf("open\n");
f_count='a';
// ReadCIS();
break;
case 'w':
break;
case '9':
temp=CFFindFileInRoot("R3000 ",&un_use,&un_use);
// temp=CFFindFileInRoot("R4000 ",&un_use,&un_use);
printf("\nR3000=%x\n",temp);
if(temp==0xffff)
break;
temp=CFFindFileInDir("TRIGGER ",temp,&un_use,&un_use);
temp=(temp-2)*bootinfo.sec_per_clust
+(bootinfo.fat_num*bootinfo.fat_size
+bootinfo.root_dir_size*32/512+1);
printf("\nTRIGGER=%x\n",temp);
// printf("\nR4000=%x\n",temp);
if(temp==0xffff)
break;
if(CFOpenFile(temp,"R41 TXT",2)==0xffff)
printf("open fail\n");
else
printf("open\n");
// ReadCIS();
read_count=0;
break;
case 'r':
temp=CFReadFile(buf,600,read_count,&to_read);
if(temp==-1)
{
printf("read error\n");
}
else if(temp==-2)
{
printf("read finish\n");
}
else
{
if(to_read==600)
{
for(temp=0;temp<600;temp++)
{
putchar((char)buf[temp]);
}
}
else
{
for(temp=0;temp<to_read;temp++)
{
putchar((char)buf[temp]);
}
}
}
read_count=read_count+600;
break;
case 'a':
temp=CFFindFileInRoot("R4000 ",&un_use,&un_use);
printf("\nR4000=%x\n",temp);
if(temp==0xffff)
break;
temp=CFFindFileInDir("TRIGGER ",temp,&un_use,&un_use);
temp=(temp-2)*bootinfo.sec_per_clust
+(bootinfo.fat_num*bootinfo.fat_size
+bootinfo.root_dir_size*32/512+1);
printf("\nTRIGGER=%x\n",temp);
if(CFOpenFile(temp,"R41 TXT",2)==0xffff)
printf("open false\n");
else
printf("opened add\n");
f_count='a';
break;
case 'p':
for(temp=0;temp<356;temp++)
{
aaa[temp]=(char)f_count;
}
f_count++;
if(CFWriteFile(aaa,356)==1)
{
putchar('w');
}
else
{
putchar('e');
}
break;
case 'c':
if(CFCloseFile()==1)
printf("closed\n");
else
printf("close fail\n");
break;
case '7':
CFGetBoot();
if(ReadOneSec(bootinfo.fat_size*2+1)==0)
{
printf("read error\n");
}
ListFile();
printf("\nlist end\n");
break;
case '8':
// printf("input filename:");
// gets(name,9);
name[0]='C';
name[1]='F';
name[2]=' ';
name[3]=' ';
name[4]=' ';
name[5]=' ';
name[6]=' ';
name[7]=' ';
printf("\nstart sector:%x",CFFindFileInRoot(name,&un_use,&un_use));
name[0]='I';
name[1]='D';
name[2]='W';
name[3]='R';
name[4]=' ';
name[5]=' ';
name[6]=' ';
name[7]=' ';
printf("\nCF start sector:%x",CFFindFileInRoot(name,&un_use,&un_use));
break;
// case 'a':
// printf("boot size:%d",sizeof(bt));
// printf("CIS size:%d",sizeof(cis));
// reg_select=!reg_select;
// printf("AUXR:%x",AUXR);
// break;
case 'm':
if(CFMakeDirectory("R3000 ",0))
{
puts("R3000");
}
else
{
puts("R3000 md e!\n");
break;
}
if(CFMakeSubDirectory("SYSINFO ","R3000 "))
{
puts("sysinfo");
}
if(CFMakeSubDirectory("OPERATE ","R3000 "))
{
puts("operate");
}
if(CFMakeSubDirectory("CONFIG ","R3000 "))
{
puts("CONFIG");
}
if(CFMakeSubDirectory("ALARM ","R3000 "))
{
puts("ALARM");
}
if(CFMakeSubDirectory("REPORT ","R3000 "))
{
puts("REPORT");
}
if(CFMakeSubDirectory("TRIGGER ","R3000 "))
{
puts("TRIGGER");
}
if(CFMakeSubDirectory("HISTORY ","R3000 "))
{
puts("HISTORY");
}
if(CFMakeDirectory("R4000 ",0))
{
puts("R4000");
}
else
{
puts("R4000 md e\n");
break;
}
if(CFMakeSubDirectory("SYSINFO ","R4000 "))
{
puts("sysinfo");
}
if(CFMakeSubDirectory("OPERATE ","R4000 "))
{
puts("operate");
}
if(CFMakeSubDirectory("CONFIG ","R4000 "))
{
puts("CONFIG");
}
if(CFMakeSubDirectory("ALARM ","R4000 "))
{
puts("ALARM");
}
if(CFMakeSubDirectory("REPORT ","R4000 "))
{
puts("REPORT");
}
if(CFMakeSubDirectory("TRIGGER ","R4000 "))
{
puts("TRIGGER");
}
if(CFMakeSubDirectory("HISTORY ","R4000 "))
{
puts("HISTORY");
}
break;
case 'd':
temp=CFFindFileInRoot("R3000 ",&un_use,&un_use);
printf("\nR3000=%x\n",(unsigned int)temp);
if(temp==0xffff)
break;
temp=CFFindFileInDir("TRIGGER ",temp,&un_use,&un_use);
temp=(temp-2)*bootinfo.sec_per_clust
+(bootinfo.fat_num*bootinfo.fat_size
+bootinfo.root_dir_size*32/512+1);
printf("\nTRIGGER=%x\n",temp);
if(CFDelete("R41 TXT",temp,2)==1)
{
printf("del suc\n");
}
break;
case 's':
printf("Free:%x\n",CFFreeSpace());
break;
default:break;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -