Writing to EEPROMs

From BeebWiki
Revision as of 01:04, 15 February 2016 by Jgharston (talk | contribs) (Initial page.)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Writing data to sideways RAM is simply a matter of paging in the sideways RAM bank and writing to it. A typical piece of code would be:

 LDY #0
 .loop1
 LDA (src),Y:STA (dst),Y   :\ Copy byte to sideways RAM
 INY:BNE loop1             :\ Loop for 256 bytes
 INC src+1:INC dst+1       :\ Increment addresses
 BIT dst+1:BVC loop1       :\ Loop until dst=&C000

If you are writing to EEPROM (Electrically Erasable Programmable ROM) you need to wait for the write cycle to complete by testing that the data has been written. The simplest way to do this is to just add two more instructions:

 LDA (src),Y:STA (dst),Y   :\ Copy byte to sideways RAM/EEPROM
 .loop2
 CMP (dst),Y:BNE loop2     :\ Wait until the byte settles

Doing this is inefficent as you have to wait for the write cycle to complete for every byte. EEPROMs allow you to write in pages of 64 bytes so you only have to wait for the write cycle to complete every 64 bytes. The following code will do this.

 LDY #0
 .loop0
 LDX #64                   :\ Write 64 bytes at a time
 .loop1
 LDA (src),Y:STA (dst),Y   :\ Copy byte to sideways RAM/EEPROM
 INY:DEX:BNE loop1         :\ Loop for 64 bytes
 DEY                       :\ Step back to the last byte written
 .loop2
 CMP (dst),Y:BNE loop2     :\ Wait until the byte settles
 INY:BNE loop0             :\ Do another 64 bytes until 256 bytes done
 INC src+1:INC dst+1       :\ Increment addresses
 BIT dst+1:BVC loop0       :\ Loop until dst=&C000

Note that if the bank is actually ROM the test loop will never exit, so you should either test the bank before attempting to write to it, or break out of the loop:

 .loop2
 LDA (dst),Y:CMP (dst),Y   :\ Test the byte just written
 BNE loop2                 :\ Wait until the byte settles
 CMP (src),Y:BNE errNotRAM :\ Compare with the source byte
 INY:BNE loop0             :\ Do another 64 bytes until 256 bytes done

See also

Jgharston (talk) 00:04, 15 February 2016 (UTC)