⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ide_test.c

📁 源程序是在VisualDSP环境下开发出来的
💻 C
📖 第 1 页 / 共 4 页
字号:
  printf("\n\n");
  
  return 1;
}

//copy file (include the DFB and the data of the file)
unsigned char CopyFile()
{
  unsigned int i=0, j=0;
  unsigned int srcgroup = 0, dstgroup = 0;
  groupnum = 0;
 
  OperatorFile = myfile;
  ViewFile();
  
  //copy the DFB of the file
  mycopyfile = myfile;
  mycopyfile.FileName[0] = 'C';
  mycopyfile.FileName[1] = 'O';
  mycopyfile.FileName[2] = 'P';
  mycopyfile.FileName[3] = 'Y';
  mycopyfile.FileName[4] = 'F';
  mycopyfile.FileName[5] = 'I';
  mycopyfile.FileName[6] = 'L';
  mycopyfile.FileName[7] = 'E';
  mycopyfile.LastTime = (COPYHOUR << 11) | (COPYMINUTE << 5) | (COPYSECOND / 2);
  mycopyfile.LastDate = ((COPYYEAR-1980) << 9) | (COPYMONTH << 5) | COPYDAY;
  
  OperatorFile = mycopyfile;
  ViewFile();
  
  //copy the data of the file  
  srcgroup = myfile.BeginGroup;
  dstgroup = newgrp()/*NxtFreeGrp*/;//find the first new unused cluster
  mycopyfile.BeginGroup = dstgroup;
  if(dstgroup == 0)
  {
  	printf("\n\r Can not find free space!");
  	return 0;  	
  }
  
  while(1)
  {
  	  printf("\n\r srcgroup=%d,  dstgroup=%d",srcgroup,dstgroup);
	  //copy cluster
	  CopyGroup(srcgroup, dstgroup);
	  
	  //wait for the next source cluster
	  i = srcgroup * 4 / 512;
	  j = srcgroup - i * 512 / 4;
	  
	  atadev_get_block(CPartition.FAT1BeginSector + i, 1, uRcvBuffer);
	  srcgroup = uRcvBuffer[4*j+0] | (uRcvBuffer[4*j+1]<<8) |  
		        (uRcvBuffer[4*j+2]<<16) | (uRcvBuffer[4*j+3]<<24);
	  
	  //source cluster to the end
	  if(srcgroup == 0x0fffffff)
	  {
	  	//modify destination cluster
	  	i = dstgroup * 4 / 512;
		j = dstgroup - i * 512 / 4;
	  	atadev_get_block(CPartition.FAT1BeginSector + i, 1, uRcvBuffer);
	    uRcvBuffer[4*j+0] = 0xFF;
	    uRcvBuffer[4*j+1] = 0xFF;
	    uRcvBuffer[4*j+2] = 0xFF;
	    uRcvBuffer[4*j+3] = 0x0F;
	    atadev_put_block(CPartition.FAT1BeginSector + i, 1, uRcvBuffer);
	
	  	//write file register item
	  	if(!WriteDFT())
	  	{
	  		printf("\n\r ERROR: Write DFT failed!");
	  		return 0;
	  	}
	  	return 1;
	  }
	  
	  //modify destination cluster to the previous destination cluster
	  i = dstgroup * 4 / 512;
	  j = dstgroup - i * 512 / 4;
	  
	  //first write something,prevent find new cluster is this cluster again
	  atadev_get_block(CPartition.FAT1BeginSector + i, 1, uRcvBuffer);
	  uRcvBuffer[4*j+0] = 0x55;
	  atadev_put_block(CPartition.FAT1BeginSector + i, 1, uRcvBuffer);
    			
	  //next new cluster,content is the content of the next object cluster
	  dstgroup = newgrp();
	  if(dstgroup == 0)
	  {
	  	printf("\n\r Can not find free space!");
	  	return 0;  	
	  }
	  
	  atadev_get_block(CPartition.FAT1BeginSector + i, 1, uRcvBuffer);
	  uRcvBuffer[4*j+0] = dstgroup & 0X000000FF;
	  uRcvBuffer[4*j+1] = (dstgroup & 0X0000FF00) >> 8;
	  uRcvBuffer[4*j+2] = (dstgroup & 0X00FF0000) >> 16;
	  uRcvBuffer[4*j+3] = (dstgroup & 0XFF000000) >> 24;
	  atadev_put_block(CPartition.FAT1BeginSector + i, 1, uRcvBuffer);
  }
}

//copy cluster
unsigned char CopyGroup(unsigned int srcgroup, unsigned int dstgroup)
{
  unsigned int i=0,j=0, srcsector=0, dstsector=0;
  unsigned char flag=0;

  for(i=0; i<CPartition.bpb.SectorPerGroup; i++)
  {
  	srcsector = i + (srcgroup-2)*CPartition.bpb.SectorPerGroup + 
  						CPartition.DATABeginSector;
  	dstsector = i + (dstgroup-2)*CPartition.bpb.SectorPerGroup + 
  						CPartition.DATABeginSector;
  	flag=1;
  	while(flag)
  	{
  		atadev_get_block_quick(srcsector, 1, uRcvBuffer);
  		atadev_put_block_quick(dstsector, 1, uRcvBuffer);  
  		  //translate uRcvBuffer[256] to uRcvBuffer[512]
    		for (j = 0; j < 256; j++)
    		{
    			temp[2*j] = uRcvBuffer[j] & 0x000000FF;
    			temp[2*j+1] = (uRcvBuffer[j] & 0x0000FF00) >> 8;
    		}
   			for (j = 0; j < 512; j++)
    			uRcvBuffer[j] = temp[j];
    
  		flag = 0;
  		atadev_get_block_quick(dstsector, 1, uRcvBuffer1);
  		for(j = 0; j < 512; j++)
  		{
  			if(uRcvBuffer[j]!=uRcvBuffer1[j])
  			{
  				flag = 1;
  				printf("\n\r Copy sector error, now repeating!");
  			}
  		}
  	}
  }
  return 1;  
}


//find new unused cluster
unsigned int newgrp()
{
  unsigned int i=0, j=0;
  unsigned int temp;
  static unsigned int ClusNumb = 0;

  for(i=0; i<CPartition.bpb.SectorPerFAT ; i++)
  {
  	atadev_get_block(CPartition.FAT1BeginSector + i, 1, uRcvBuffer);
	for(j=0; j<512/4; j++)//4 bytes(32 bits) describe 1 cluster number(occupies 1 cote)
	{
	  temp = uRcvBuffer[4*j+0] | (uRcvBuffer[4*j+1]<<8) |  
	        (uRcvBuffer[4*j+2]<<16) | (uRcvBuffer[4*j+3]<<24);
	  if(  (temp == 0x00000000)   &&  ((i!=0) || (j >= 4))  )//the foremost 2 cotes(8 bytes) of the FAT1 are reserved for the identification
	  {                                                      //the following 2 cotes are reserved for the DFT
	  	ClusNumb = j + i * 512 / 4;
	  	return ClusNumb;
	  }
	}
  }
  return 0;   
}

//write file register item
unsigned char WriteDFT()
{
  unsigned int i=0, j=0, k=0;
 
  for(i=0; i< 2 * CPartition.bpb.SectorPerGroup ; i++)
  {
  	atadev_get_block_quick(CPartition.DATABeginSector + i, 1, uRcvBuffer);
    for(j=0; j<16; j++)
    {
      InitFile(j);
      if(OperatorFile.Attrib != 32)
      {
      	for(k=0; k<8; k++)
  			uRcvBuffer[32*j+k] = mycopyfile.FileName[k];
  			
		for(k=0; k<3; k++)
  			uRcvBuffer[32*j+k+8] = mycopyfile.FileExt[k];

  		uRcvBuffer[32*j+0x0b] = mycopyfile.Attrib;
  		uRcvBuffer[32*j+0x16] = mycopyfile.LastTime ;
  		uRcvBuffer[32*j+0x17] = mycopyfile.LastTime>>8;
  		uRcvBuffer[32*j+0x18] = mycopyfile.LastDate;
  		uRcvBuffer[32*j+0x19] = mycopyfile.LastDate>>8;
  		uRcvBuffer[32*j+0x1a] = mycopyfile.BeginGroup;
  		uRcvBuffer[32*j+0x1b] = mycopyfile.BeginGroup>>8;
  		uRcvBuffer[32*j+0x1c] = mycopyfile.FileLength;
  		uRcvBuffer[32*j+0x1d] = mycopyfile.FileLength>>8;
  		uRcvBuffer[32*j+0x1e] = mycopyfile.FileLength>>16;
  		uRcvBuffer[32*j+0x1f] = mycopyfile.FileLength>>24;
  		atadev_put_block_quick(CPartition.DATABeginSector + i, 1, uRcvBuffer);
  	  	
  		return 1;
  	  }
  	}
  }
  
  return 0;   
}

//delete DFB of file(the data of the file are still in the DATA section) 
unsigned char DelFile(unsigned int srcgroup)
{
  unsigned int i=0, j=0, k=0;

  for(i=0; i< 2 * CPartition.bpb.SectorPerGroup ; i++)
  {
  	atadev_get_block_quick(CPartition.DATABeginSector + i, 1, uRcvBuffer);
    for(j=0; j<16; j++)
    {
      InitFile(j);
      if(OperatorFile.BeginGroup == srcgroup)
      {
        printf("\n\r Found the file, cleaning it now!");
      	for(k=0; k<32; k++)
  			uRcvBuffer[32*j+k] = 0;
  		atadev_put_block_quick(CPartition.DATABeginSector + i, 1, uRcvBuffer);
      }
  	}
  }
  
  return 1;  
}
 
unsigned char NewFile()
{
  unsigned int i=0;
  	
  //set the DFB parameters
  mynewfile.FileName[0] = 'T';
  mynewfile.FileName[1] = 'E';
  mynewfile.FileName[2] = 'S';
  mynewfile.FileName[3] = 'T';
  mynewfile.FileName[4] = ' ';
  mynewfile.FileName[5] = ' ';
  mynewfile.FileName[6] = ' ';
  mynewfile.FileName[7] = ' ';
  mynewfile.FileExt[0] = 'T';
  mynewfile.FileExt[1] = 'X';
  mynewfile.FileExt[2] = 'T';
  mynewfile.Attrib = ATTRIB;
  mynewfile.LastTime = (NEWHOUR << 11) | (NEWMINUTE << 5) | (NEWSECOND / 2);
  mynewfile.LastDate = ((NEWYEAR-1980) << 9) | (NEWMONTH << 5) | NEWDAY;
  mynewfile.BeginGroup = NEWBEGINGRP;
  mynewfile.FileLength = NEWLENGTH;
  
  //write the content of the file to be created
  NewFileBuf[0] = 'T';
  NewFileBuf[1] = 'E';
  NewFileBuf[2] = 'S';
  NewFileBuf[3] = 'T';
  NewFileBuf[4] = 'i';
  NewFileBuf[5] = 'n';
  NewFileBuf[6] = 'g';
  NewFileBuf[7] = ':';

  for(i = 8; i < 512; i++) NewFileBuf[i] = '.';
  NewFileBuf[512] = 't';
  NewFileBuf[513] = 's';
  NewFileBuf[514] = 't';
  for(i = 515; i < 2*512; i++) NewFileBuf[i] = '.';  
  NewFileBuf[2*512] ='t';
  NewFileBuf[2*512+1] ='s';
  NewFileBuf[2*512+2] ='t';
  for(i = 2*512+3; i < 3*512; i++) NewFileBuf[i] = '.';
  NewFileBuf[3*512] = 't';  
  NewFileBuf[3*512+1] = 's';
  NewFileBuf[3*512+2] = 't';
  for(i = 3*512+3; i < 4*512; i++) NewFileBuf[i] = '.';
  NewFileBuf[4*512] = 't';  
  NewFileBuf[4*512+1] = 's';
  NewFileBuf[4*512+2] = 't';
  for(i = 4*512+3; i < 5*512; i++) NewFileBuf[i] = '.';
  NewFileBuf[5*512] = 't';  
  NewFileBuf[5*512+1] = 's';
  NewFileBuf[5*512+2] = 't';
  for(i = 5*512+3; i < 6*512; i++) NewFileBuf[i] = '.';
  NewFileBuf[6*512] = 't';  
  NewFileBuf[6*512+1] = 's';
  NewFileBuf[6*512+2] = 't';
  for(i = 6*512+3; i < 7*512; i++) NewFileBuf[i] = '.';
  NewFileBuf[7*512] = 't';  
  NewFileBuf[7*512+1] = 's';
  NewFileBuf[7*512+2] = 't';
  for(i = 7*512+3; i < 720*625*2-3; i++) NewFileBuf[i] = ':';
  NewFileBuf[720*625*2-3] = 'E';  
  NewFileBuf[720*625*2-2] = 'N';
  NewFileBuf[720*625*2-1] = 'D';
 
  WriteFile();

  return 1;  
  
}  

unsigned char WriteFile()
{
  unsigned int i=0, j=0, k=0;
  unsigned int dstgroup=0;
  unsigned int ClusNums = 0;
   
  //(groupnum+1):count of the cluster the new file occupies
  //(FileLength-1):the reason -1 is to prevent the FileLength is just the integral multiple of 1 cluster but occupies one more cluster
  groupnum = (mynewfile.FileLength - 1)/ CPartition.bpb.BytePerSector / CPartition.bpb.SectorPerGroup;
  ClusNums = groupnum;
  
  OperatorFile = mynewfile;
  ViewFile();
  
  //write the data of the file 
  dstgroup = mynewfile.BeginGroup;
  while(1)
  {
        for(k=0;k<ClusNums;k++)
  	{
	  //the new created data is smaller than 1 cluster
	  if(groupnum == 0)
	  {
	  	WriteGroup(dstgroup,ClusNums);
	  	
	  	//modify destination cluster
	  	i = dstgroup * 4 / 512;
		j = dstgroup - i * 512 / 4;
		atadev_get_block(CPartition.FAT1BeginSector + i, 1, uRcvBuffer);
	    uRcvBuffer[4*j+0] = 0xFF;
	    uRcvBuffer[4*j+1] = 0xFF;
	    uRcvBuffer[4*j+2] = 0xFF;
	    uRcvBuffer[4*j+3] = 0x0F;
	    atadev_put_block(CPartition.FAT1BeginSector + i, 1, uRcvBuffer);

	  	//write file register item
	  	if(!WriteNewDFT())
	  	{
	  		printf("\n\r ERROR: Write DFT failed!");
	  		return 0;
	  	}
	  	return 1;
	  }

	  //the new created data is larger than 1 cluster
      groupnum = groupnum - 1;
      
      WriteWholeGroup(dstgroup,k);
	  	    	  
	  //write the next destination cluster number to the previous destination cluster in FAT1
	  i = dstgroup * 4 / 512;
	  j = dstgroup - i * 512 / 4;
	  
	  //first write something,prevent find new cluster is this cluster again
	  atadev_get_block(CPartition.FAT1BeginSector + i, 1, uRcvBuffer);
	  uRcvBuffer[4*j+0] = 0x55;
	  atadev_put_block(CPartition.FAT1BeginSector + i, 1, uRcvBuffer);
    			
	  //next new cluster,content is the content of the next object cluster
	  dstgroup = newgrp();
	  if(dstgroup == 0)
	  {
	  	printf("\n\r Can not find free space!");
	  	return 0;  	
	  }
  	
  	  //modify destination cluster in FAT1
	  atadev_get_block(CPartition.FAT1BeginSector + i, 1, uRcvBuffer);
	  uRcvBuffer[4*j+0] = dstgroup & 0X000000FF;
	  uRcvBuffer[4*j+1] = (dstgroup & 0X0000FF00) >> 8;
	  uRcvBuffer[4*j+2] = (dstgroup & 0X00FF0000) >> 16;
	  uRcvBuffer[4*j+3] = (dstgroup & 0XFF000000) >> 24;
	  atadev_put_block(CPartition.FAT1BeginSector + i, 1, uRcvBuffer);
        }
  }
}

//write the register item of the new created file
unsigned char WriteNewDFT()
{
  unsigned int i=0, j=0, k=0;
 
  for(i=0; i< 2 * CPartition.bpb.SectorPerGroup ; i++)
  {
  	atadev_get_block_quick(CPartition.DATABeginSector + i, 1, uRcvBuffer);
    for(j=0; j<16; j++)
    {
      InitFile(j);
      if(OperatorFile.Attrib != 32)
      {
      	for(k=0; k<8; k++)
  			uRcvBuffer[32*j+k] = mynewfile.FileName[k];
  			
		for(k=0; k<3; k++)
  			uRcvBuffer[32*j+k+8] = mynewfile.FileExt[k];

  		uRcvBuffer[32*j+0x0b] = mynewfile.Attrib;
  		uRcvBuffer[32*j+0x16] = mynewfile.LastTime ;
  		uRcvBuffer[32*j+0x17] = mynewfile.LastTime>>8;
  		uRcvBuffer[32*j+0x18] = mynewfile.LastDate;
  		uRcvBuffer[32*j+0x19] = mynewfile.LastDate>>8;
  		uRcvBuffer[32*j+0x1a] = mynewfile.BeginGroup;
  		uRcvBuffer[32*j+0x1b] = mynewfile.BeginGroup>>8;
  		uRcvBuffer[32*j+0x1c] = mynewfile.FileLength;
  		uRcvBuffer[32*j+0x1d] = mynewfile.FileLength>>8;
  		uRcvBuffer[32*j+0x1e] = mynewfile.FileLength>>16;
  		uRcvBuffer[32*j+0x1f] = mynewfile.FileLength>>24;
  		atadev_put_block_quick(CPartition.DATABeginSector + i, 1, uRcvBuffer);
  	  	
  		return 1;
  	  }
 	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -