Most of the games I ported from Arduboy to the Micro:Gamer use EEPROM to save high score or game state when the console is powered off. I think this is must have feature for a game console so I implemented it for the Micro:Gamer.
There is no EEPROM on the micro:bit board and I don't want to add it as an extra component on my hardware design, so I had to use the flash memory inside the micro-controller.
The interface to use the flash as persistent storage is provided by the MicroGamerMemoryCard class. This class uses two different memory areas:
- The first one is a 1k bytes page in the flash memory, this is where the data will be stored permanently. The reason for a fixed size of 1k bytes is because flash memory have to be erased/written by pages of 1k.
- The second memory area is the temporary RAM buffer. This is where the program will read/write the data before saving it permanently in the flash page. Since there is not a lot of RAM available, the program can decide to have a temporary RAM buffer that is smaller than 1k.
Here is an example of how it looks like in the code:
// Create a memory card of one 32bit word
MicroGamerMemoryCard mem(1);
// Load the content of the flash page to the temporary RAM buffer
mem.load();
// Read a value from the temporary RAM buffer
if (mem.read(0) != 42) {
// Write a value to the temporary RAM buffer
mem.write(0, 42);
// Permanently save the RAM buffer into flash memory
mem.save();
}
And here is a demo with the game Micro City:
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.