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

📄 nf.c

📁 c51snd1c硬盘播放器全部资料.源码.线路图.protel99se的pcb图
💻 C
📖 第 1 页 / 共 5 页
字号:
      }
      gl_address += NF_PAGE_PER_BLOCK;
    }
    if (nf_spare_block_number[i] > tmp_spare)
    {
      nf_spare_block_number[i] = tmp_spare;
    }
  }


  Nf_CS_EVEN();
  Nf_wait_busy_even();
  for (i = 0; i < NF_ZONE_MAX_CPT; i++)
  { /* Determine the index for each correct lut */
    if ((lut_is_present & (0x01 << i)))
    {
      gl_address = (Uint32)(nf_lut_block[i]) << NF_SHIFT_SECTOR_BLOCK;
      do
      {
        Nf_read_open_spare_area_even(gl_address, 0x06);
        nf_lut_index[i]++;
        gl_address += NF_PAGE_BY_LUT;
        byte_6 = Nf_rd_byte_even();
      }
      while ((byte_6 == 0xE8) && ((gl_address & 0x3F) != 0)); 
      if (byte_6 == 0xE8)
      {
        nf_lut_index[i]--;
        gl_address-=NF_PAGE_BY_LUT;
      }
      else
      {
        nf_lut_index[i]-=2;
        gl_address-=(NF_PAGE_BY_LUT << 1);
      }      

      Nf_read_open_spare_area_even(gl_address, 0x00);
      if (Nf_rd_byte_even() != 0xFF)                       /* LUT have been modified */
      {
        nf_block_erase(gl_address);
        lut_is_present &= ~((0x01) << i);             /* Reset bit */
        nf_lut_index[i] = 0;
        Nf_wait_busy_even();
      }

    }
  }
  /*****************************************************************/
  /*          Find free physical block for LUT for each zone       */
  /*****************************************************************/
  for (i = 0; i < NF_ZONE_MAX_CPT; i++)
  {
    if (!(lut_is_present & (0x01 << i)))
    {
      block = last_physical_used_block_even[i];
      start = (Uint16)(i) << NF_SHIFT_BLOCK_ZONE;                   /* starting value for each zone */
      j = 0;
      if (block == start)                                           /* starting block for scan      */
      {
        block = start + 1023;
      }
      block_valid = FALSE;                                          /* init flag block valid */
      do
      {
        gl_address = (Uint32)(block) << NF_SHIFT_SECTOR_BLOCK;
        Nf_read_open_spare_area_even(gl_address, 0x05);
        byte_5 = Nf_rd_byte_even();
        byte_6 = Nf_rd_byte_even();
  
        if ( (byte_5 == 0xFF) && (byte_6 == 0xFF) ) /* not assigned and valid block */
        {
          block_valid = TRUE;                       /* find a correct block         */
        }
        else                                        /* else decrease block number   */
        {
          if (block == start)                       
          {
            block = start + 1023;
          }
          else
          {
            block--;
          }
        }
        j++;
      }
      while ( (!block_valid) && (j < 1024) );
  
      if (j == 1024)
      {
        return KO;
      }
      nf_lut_block[i] = block;                         /* Update look up table address */
    }
  }
  

  /**********************************************************/
  /* Create the look-up table                               */
  /* Process is done zone per zone and by step of 256 blocks*/
  /* ********************************************************/
  for (nf_zone = 0; nf_zone < NF_ZONE_MAX_CPT; nf_zone++)  /* for each zone */
  {
    if ( !(lut_is_present & (0x01 << nf_zone)))            /* if there is no LUT */
    {
      start         = 0x00; /* reset start value for scan */
      end           = 0x40;               
      free_bloc_pos_even = (Uint16)(last_physical_used_block_even[nf_zone]);
      free_bloc_pos_odd  = (Uint16)(last_physical_used_block_odd[nf_zone]);
      gl_address         = (Uint32)(nf_lut_block[nf_zone]) << NF_SHIFT_SECTOR_BLOCK;        
      page               = 0;
      do
      {
        Nf_CS_EVEN();     /* start in nf even */
        nf_init_buffer(); /* reset gl_buffer with 0xFF */                      
        temp_address  = (Uint32)(nf_zone) << NF_SHIFT_SECTOR_ZONE;   
        block         = (Uint16)(nf_zone) << NF_SHIFT_BLOCK_ZONE;
        bit_double_block_odd = FALSE;     /* reset double affected block bit */
        nf_close_write_session = FALSE;   /* reset flag to indicate a recovery condition */
        Nf_wait_busy_even();
        for (j = NF_BLOCK_PER_ZONE; j != 0 ; j--)   /* scan all zone's block */
        { 
          Nf_read_open_spare_area_even(temp_address, 0x05);
          if (Nf_rd_byte_even() == 0xFF)            /* if not a bad block */
          {
            block2.b[0] = Nf_rd_byte_even();        /* read logical block address : upper byte */
            block2.b[1] = Nf_rd_byte_even();        /* read logical block address : lower byte */
            byte_6 = 2 * (block2.b[1] & 0xFE);      /* gl_buffer index */
            if ( (block2.b[0] & 0xF8) == 0x10)      /* if block is affected to a logical block */
            {
              block2.w = (block2.w & 0x0FFF) >> 1;  /* calculate the logical block value */          
              if ( (block2.w < end) && (block2.w >= start)) /* if logical block is in the window */
              { 
                if (gl_buffer[byte_6] != 0xFF)      /* check if the logical block is already affected */
                {
                  /* if it is, then find the block to be deleted */
                  i = block;
                  Nf_read_open_spare_area_even(temp_address, 0x11);/* marker : block have to be delete */
                  if (Nf_rd_byte_even() == 0x00)                   /* if marker is set */
                  {
                    nf_block_to_be_deleted_even = block;           /* current block have to be delete */
                    block = (Uint16)(gl_buffer[byte_6] << 8) + gl_buffer[byte_6 + 1];
                  }
                  else
                  {
                    nf_block_to_be_deleted_even = (Uint16)(gl_buffer[byte_6] << 8) + gl_buffer[byte_6 + 1];
                    gl_buffer[byte_6]      = block >> 8;
                    gl_buffer[byte_6 + 1]  = block;
                  }
                  /* for the last block, find the last sector used */
                  temp_address = (Uint32)(block) << NF_SHIFT_SECTOR_BLOCK;
                  do
                  {
                    Nf_read_open_spare_area_even(temp_address, 0x06);
                    (((Byte*)&temp_address)[3])++;
                    byte_6 = Nf_rd_byte_even();
                  }
                  while ((byte_6 != 0xFF) && ( ((Byte*)&temp_address)[3] & 0x3F));

                  if (byte_6 != 0xFF)
                  {
                    (((Byte*)&temp_address)[3])--;
                  }
                  else
                  {
                    (((Byte*)&temp_address)[3])-=2;
                  }

                  nf_current_physical_sector_addr_even = ((Uint32)(block) << NF_SHIFT_SECTOR_BLOCK) 
                                                       + (temp_address & 0x3F); 
                  gl_cpt_page = 0;
                  nf_block_used = TRUE;
                  Nf_read_open_spare_area_even(temp_address, 0x10);    /* Byte addr 10 -> internal counter page */
                  byte_5 = ~(Nf_rd_byte_even());
                  while (byte_5 != 0)
                  {
                    ((Byte*)&gl_cpt_page)[0] += 2;
                    byte_5 = byte_5 >> 1;
                  }
                  if (gl_cpt_page)
                    ((Byte*)&gl_cpt_page)[0] -= 2;

                  save_gl_cpt_page_even = ((Byte*)&gl_cpt_page)[0];
                  nf_logical_block = block2.w;

                  block = i;
                  temp_address = (Uint32)(block) << NF_SHIFT_SECTOR_BLOCK;
                  nf_close_write_session = TRUE;
                }
                else
                { /* New affected physical block */
                  gl_buffer[byte_6]      = block >> 8;
                  gl_buffer[byte_6 + 1]  = block;
                }
              }
            }
          }
          temp_address += NF_PAGE_PER_BLOCK; 
          block++;
        }
          
        if (!nf_close_write_session)
        {
          /* affect to the free physical block a fictive logical block */
          /* free physical block => gl_buffer[x] = 0xFF */
          temp_address = (Uint32)(free_bloc_pos_even) << NF_SHIFT_SECTOR_BLOCK;
          for (i = 0; i <= 0xFE; i+=4)
          {
            if (gl_buffer[i] == 0xFF)
            {
              do                                                /* Search free physical block */
              {
                temp_address += NF_PAGE_PER_BLOCK;
                if (temp_address >= ((Uint32)(nf_zone + 1) << NF_SHIFT_SECTOR_ZONE) )
                    temp_address = (Uint32)(nf_zone) << NF_SHIFT_SECTOR_ZONE;
      
                Nf_read_open_spare_area_even(temp_address, 0x05);
                
                byte_5 = Nf_rd_byte_even();                          /* Invalid/Valid block */
                byte_6 = Nf_rd_byte_even();                          /* Used/Unused block */
              }
              while ( ( (byte_6 != 0xFF) && (byte_6 != 0xE8) ) || (byte_5 != 0xFF) );
    
              free_bloc_pos_even     = (temp_address  >> NF_SHIFT_SECTOR_BLOCK);
              gl_buffer[i]           = ((Byte*)&free_bloc_pos_even)[0] | 0x80;
              gl_buffer[i + 1]       = ((Byte*)&free_bloc_pos_even)[1];
            }
          }
        }

        Nf_CS_ODD();
        temp_address  = (Uint32)(nf_zone) << NF_SHIFT_SECTOR_ZONE;   
        block         = (Uint16)(nf_zone) << NF_SHIFT_BLOCK_ZONE;
        Nf_wait_busy_odd();
        for (j = NF_BLOCK_PER_ZONE; j != 0 ; j--)                   /* for each block                     */
        { 
          Nf_read_open_spare_area_odd(temp_address, 0x05);
          if (Nf_rd_byte_odd() == 0xFF)                                 /* If not a bad block                 */
          {
            block2.b[0] = Nf_rd_byte_odd();                             /* Read logical block address         */
            block2.b[1] = Nf_rd_byte_odd();
            byte_6 = (2 * (block2.b[1] & 0xFE)) + 2;
            if ( (block2.b[0] & 0xF8) == 0x10)
            {
              block2.w = (block2.w & 0x0FFF) >> 1;          
              if ( (block2.w < end) && (block2.w >= start)) 
              { 
                if (gl_buffer[byte_6] != 0xFF)
                {
                  /* This physical block is already affected */
                  /* We have to find block to be deleted */
                  bit_double_block_odd = TRUE;
                  i = block;
                  Nf_read_open_spare_area_odd(temp_address, 0x11);     /* Byte addr 11 -> block have to be deleted */
                  if (Nf_rd_byte_odd() == 0x00)                        /* This is the block to be deleted */
                  {
                    nf_block_to_be_deleted_odd = block;
                    block = (Uint16)(gl_buffer[byte_6] << 8) + gl_buffer[byte_6 + 1];
                  }
                  else
                  {
                    nf_block_to_be_deleted_odd = (Uint16)(gl_buffer[byte_6] << 8) + gl_buffer[byte_6 + 1];
                    gl_buffer[byte_6]      = block >> 8;
                    gl_buffer[byte_6 + 1]  = block;
                  }
                  /* for the last block, find the last sector used */
                  temp_address = (Uint32)(block) << NF_SHIFT_SECTOR_BLOCK;
                  do
                  {
                    Nf_read_open_spare_area_odd(temp_address, 0x06);
                    (((Byte*)&temp_address)[3])++;
                    byte_6 = Nf_rd_byte_odd();
                  }
                  while ((byte_6 != 0xFF) && ( ((Byte*)&temp_address)[3] & 0x3F));

                  if (byte_6 != 0xFF)
                  {
                    (((Byte*)&temp_address)[3])--;
                  }
                  else
                  {
                    (((Byte*)&temp_address)[3])-=2;
                  }
                  /* determinate physical sector addr */
                  nf_current_physical_sector_addr_odd = ((Uint32)(block) << NF_SHIFT_SECTOR_BLOCK) + (temp_address & 0x3F); 

                  /* Byte addr 10 -> internal counter page */
                  Nf_read_open_spare_area_odd(temp_address, 0x10);    
                  /* determinate gl_cpt_page value */
                  gl_cpt_page = 0;
                  byte_5 = ~(Nf_rd_byte_odd());
                  while (byte_5 != 0)
                  {
                    ((Byte*)&gl_cpt_page)[0] += 2;
                    byte_5 = byte_5 >> 1;
                  }
                  if (gl_cpt_page)
                    ((Byte*)&gl_cpt_page)[0] -= 2;
                  save_gl_cpt_page_odd = ((Byte*)&gl_cpt_page)[0];
                  block = i;
                  temp_address = (Uint32)(block) << NF_SHIFT_SECTOR_BLOCK;
                  nf_close_write_session = TRUE;
                }
                else
                { /* new affected physical block */
                  gl_buffer[byte_6]      = block >> 8;

⌨️ 快捷键说明

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