The next piece of the puzzle to solve is the high score memory problem. Space Duel, Gravitar and Black Widow all use an ER2055 EAROM memory chip to save high scores and some game statistics.
The ER2055 is a 64 byte non volatile memory that is the forerunner to moderm flash memory, but is a very different beast from chips today. Apart from it's special -28V power rail it was very slow, requiring 50mS per byte to erase, then another 50mS per byte to write a new byte. Although the chip is a bytewide parallel device, the game code could not afford for the 6502 CPU to sit with it's data and address busses stuck while the EAROM erased or wrote each byte of data. Not when there are vectors to maintain on screen, interrupts to service and bolts of plasma energy to unleash across the inky void of space towards marauding alien ships! Atari designers therefore added address and data latches to the PCB so that the 6502 could quickly write a byte of data to the appropriate EAROM address and then carry on with executing game code, while the latches held the states until the EAROM had erased or written it's data.
These latches make the EAROM appear as 64 bytes for writing addresses, but only a single byte for reading. To write or erase data you write your data to an address in the range 0x0F00 to 0x0F3F, then select the appropriate control bits by writing to 0x0E80 to perform the desired erase or write function.
For reading a byte you again *write* the the desired address, set the control bits for read, but then read from the single address 0x0A00 where the actual data will be presented.
Investigation of the software shows that all three games use a very similar EAROM control function (yay for Atari code re-use!) that is called once per second and runs a small state machine to control each of three possible modes for read, erase and write, shown in part below.
EAROM_Read: .equ 0xA00
EAROM_Ctrl: .equ 0xE80
EAROM_Write: .equ 0xF00
Read function
LDA #8
STA EAROM_Ctrl
STA EAROM_Write,X ; Set address latch
LDA #9
STA EAROM_Ctrl ; Toggle clock line in read mode
NOP
LDA #8
STA EAROM_Ctrl
CPX EAROM_End_Addr
LDA EAROM_Read ; Read EAROM data byte
Erase function
LDY EAROM_Struct_Siz
LDX EAROM_Address
ASL A
BCC loc_E577
STA EAROM_Write,X ; Set address latch
LDA #40h
STA byte_174
LDY #0Eh ; EAROM selected, erase mode, clock low
Write function
STA EAROM_Write,X ; Set address and data latches
LDY #0Ch ; EAROM selected, write mode, clock low
I saw that I could fairly easily modify some of this code so that I could use normal RAM instead, with the intention of making it battery backed RAM in future. There is already 2K of RAM for Gravitar and Black Widow, but neither game actually uses all 2K. I chose to use the area from 0x700 to 0x7BF as three chunks of 64 byte RAM, one chunk for each game.
Define new address base in Space Duel source
EAROM_Write: .equ 0x700
Define new address base in Gravitar source
EAROM_Write: .equ 0x740
Define new address base in Black Widow source
EAROM_Write: .equ 0x780
The erase function now becomes redundant, so I could simply remove the instruction so that it never writes junk ro RAM:
STA EAROM_Write,X ; Set address latch
The write function stays exactly the same:
STA EAROM_Write,X ; Set address and data latches
The read function needs the address latch write instruction removing so it doesn't overwrite RAM
STA EAROM_Write,X ; Set address latch
And then it is a simple case of replacing
LDA EAROM_Read ; Read EAROM data byte
with
LDA EAROM_Write,X ; Read EAROM data byte indexed from X
The X register still contains the correct index from before the removed STA instruction, so now it simply reads the data from that 64 byte range.
I made changes to the Space Duel and Gravitar code, assembled it, burned a ROM and tested it by playing a game to get a high score and entering my initials. Then I waited 20 or 30 seconds to give the still slow function time to do it's stuff. Then I pressed the PCB reset button and waited for the attract mode to cycle through to the high score list. My initials were still there!
I then flicked the DIP switch to bring in the Gravitar ROM bank and did the same test, again my initials were retained. I then switched back to Space Duel and the initials were still there too. Not an exhaustive test I agree, but a good start and proved that my move to RAM could work.
I also had to modify the Gravitar reset function so that it did not clear 0x700-0x7FF to zero upon reset, but that was a single instruction to remove.
Next I can do the same for the Black Widow code and perhaps rig up a battery backed RAM on the CPU expander to test it properly.
I'm fairly confident that the high score issue is solved and I can remove my ER2055 chip from my PCB now.