bit more of an attempt , steve can you check the logic ,cleanup ,


Code:
Flash_Byte_Modify:
 ' assumes address , length ( upto 256bytes )
 ' assumes sector 0 is not protected
 ' assumes selectted sector is not protected
 
  Flash_Addr_Copy.byte3 =  Data_Length   ' save the selected address & length
  Flash_Addr_Copy.byte2 =  SDC_Sector 
  Flash_Addr_Copy.byte1 =  SDC_Page 
  Flash_Addr_Copy.byte0 =  SDC_Byte 
 
  SDC_sector = 0
  gosub Flash_Sec_Erase  ' erase sector 0
 
  SDC_sector = 0                            ' use sector 0,instead of given sector , but use given page, byte length  
  gosub Flash_write                         ' Write the the changed data to sector 0
 
  if Flash_Addr_Copy.byte1 = 0 then         ' find the pages to copy - copy all pages/ bytes  above 0 for given sector  
     for SDC_Page = 1 to 255                 ' read all pages above 0  of selected sector  
         SDC_Sector  = Flash_Addr_Copy.byte2    ' of read selected sector 
         SDC_byte  = 0                          ' bytes 0 - 255
         Data_Length = 255                      ' all bytes 
         gosub Flash_Read                       ' read 255 bytes at a time 
         SDC_Sector  = 0                        ' use sector 0 aselected sector 
         SDC_byte  = 0                          ' bytes 0 - 255
         Data_Length = 255                      ' all bytes 
         gosub Flash_Write                      ' write  
      next SDC_Page
   endif

  if Flash_Addr_Copy.byte1 = 255 then       ' find the pages to copy - copy all pages/ bytes  below given page/ byte of sector  
     for SDC_Page = 0 to 254                 ' read all pages below 255  of selected sector  
         SDC_Sector  = Flash_Addr_Copy.byte2    ' of read selected sector 
         SDC_byte  = 0                          ' bytes 0 - 255
         Data_Length = 255                      ' all bytes 
         gosub Flash_Read                       ' read 255 bytes at a time 
         SDC_Sector  = 0                        ' use sector 0 aselected sector 
         SDC_byte  = 0                          ' bytes 0 - 255
         Data_Length = 255                      ' all bytes 
         gosub Flash_Write                      ' write  
      next SDC_Page
   endif

  if Flash_Addr_Copy.byte1 >0 and Flash_Addr_Copy.byte1 < 254 then       ' find the pages to copy - copy all pages/ bytes  below given page/ byte of sector  
        
     for SDC_Page = 0 to Flash_Addr_Copy.byte1 - 1   ' read all pages from 0 to selected page -1   
         SDC_Sector  = Flash_Addr_Copy.byte2         ' of read selected sector 
         SDC_byte  = 0                               ' bytes 0 - 255
         Data_Length = 255                           ' all bytes 
         gosub Flash_Read                            ' read 255 bytes at a time 
         SDC_Sector  = 0                             ' use sector 0 aselected sector 
         SDC_byte  = 0                               ' bytes 0 - 255
         Data_Length = 255                           ' all bytes 
         gosub Flash_Write                           ' write  
      next SDC_Page
   endif
 
    for SDC_Page = Flash_Addr_Copy.byte1 + 1 to 255  ' read all pages from selected page + 1  to 255   
         SDC_Sector  = Flash_Addr_Copy.byte2         ' of read selected sector 
         SDC_byte  = 0                               ' bytes 0 - 255
         Data_Length = 255                           ' all bytes 
         gosub Flash_Read                            ' read 255 bytes at a time 
         SDC_Sector  = 0                             ' use sector 0 aselected sector 
         SDC_byte  = 0                               ' bytes 0 - 255
         Data_Length = 255                           ' all bytes 
         gosub Flash_Write                           ' write  
      next SDC_Page
   endif
 endif
 
 if Flash_Addr_Copy.byte0 + Flash_Addr_Copy.byte3  < 255 then   ' if the start byte + data length of the data changed is < 255 bytes , 
           ' then copy  bytes  below the selected changed data 
         SDC_Sector  = Flash_Addr_Copy.byte2         ' read selected sector 
         SDC_PAGE   =  Flash_Addr_Copy.byte1         ' of selected page 
         SDC_byte = 0                                ' start address bytes 0 to  
         Data_Length = Flash_Addr_Copy.byte0 - 1     ' selected byte - 1  ( length)
         gosub Flash_Read                            ' read  
         SDC_Sector  = 0                             ' use sector 0 aselected sector 
         SDC_PAGE   =  Flash_Addr_Copy.byte1         ' of selected page 
         SDC_byte = 0                                ' start address bytes 0 to  
         Data_Length = Flash_Addr_Copy.byte0 - 1     ' selected byte - 1  ( length)
         gosub Flash_Write                           ' write  
      
          ' then copy  bytes above the selected changed data 
         SDC_Sector  = Flash_Addr_Copy.byte2                          ' read selected sector 
         SDC_PAGE    = Flash_Addr_Copy.byte1                          ' of selected page 
         SDC_byte    = Flash_Addr_Copy.byte0 + Flash_Addr_Copy.byte3  ' start address bytes above given address and length  
         Data_Length = 255 - SDC_byte                                 ' data above changed bytes ( length = 255 bytes  - given addrees + given length )
         gosub Flash_Read                                             ' read the data  
         SDC_Sector  = 0                                              ' use sector 0 
         SDC_PAGE   =  Flash_Addr_Copy.byte1                          ' of selected page 
         SDC_byte    = Flash_Addr_Copy.byte0 + Flash_Addr_Copy.byte3  ' start address bytes above given address and length  
         Data_Length = 255 - SDC_byte                                 ' data above changed bytes ( length = 255 bytes  - given addrees + given length )
         gosub Flash_Write                                            ' write  the data 
     
 endif
 
    ' Sector 0 should now have a complete copy of the data + changed data from the orginal selected sector 
   
      SDC_sector = Flash_Addr_Copy.byte2
      gosub Flash_Sec_Erase              ' erase orginal sector 
      
      for SDC_PAGE = 0 to 255                                          '  copy sector 0 pages to selected sector 0 ( 256bytes at a time) 
           SDC_Sector  = 0                                             ' use sector 0 
           SDC_byte    = 0
           Data_Length = 255
           gosub Flash_Read                                             ' read the data  
           SDC_Sector  = Flash_Addr_Copy.byte2                          ' use selected sector  
           SDC_byte    = 0
           Data_Length = 255
      next SDC_PAGE
 
      SDC_sector = 0                     ' clean up sector 0 
      gosub Flash_Sec_Erase              ' erase orginal sector
 return