jgilchrist/gbemu

save data (state restore) - how does it work?

marcushultman opened this issue ยท 4 comments

Hi! Great emulator, I got a custom Gameboy instance working very quickly in my environment. However, I think I am struggling with state restore/save data. I'm saving the get_cartridge_ram() to disk and pass it as save_data in the ctor, but the game I'm trying out is always starting from the beginning. I've tried saving both during the run-loop and afterwards, and data seem to be written (there is binary data in the file). Am I misunderstanding something, or is it maybe the ROM I'm using that is to blame?

@marcushultman Hi! Thanks for the kind words and thanks for trying it out.

It's a bit hard to say what might be happening without more information, but it sounds like what you're trying to do is correct. You can see how I implemented saving/loading here in case that helps.

I've only tried it with a small number of games, so it's very possible that it's an issue with the particular game you're using. Would you mind sharing the name of the game so I can take a look into it?

I used the classic https://www.emulatorgames.net/roms/gameboy/pokemon-red/. Yes, I basically used the same code as the SDL version. ๐Ÿ˜„ Is the intention of save state that the emulator should go back to the screen that it had open when the save occurred?

Great choice ๐Ÿ˜

No, currently no internal state is saved - there are other emulators that 'snapshot' the internal state of the emulator (registers, internal memory, etc.) and allow you to return to the exact state of the emulator from that snapshot, but I haven't got round to implementing that yet.

The saving that is implemented here is closer to saving on the original Gameboy - that is, it only saves the state of the cartridge RAM. For example, if you were to go into the save menu in Pokemon and save the game, you should see that the saved game data is persisted if you close and re-open the emulator (i.e. you will see 'Continue' in the main menu) - that is because the cartridge RAM data is persisted via the code in the file I linked above. This simulates the battery in the real cartridges keeping the cartridge RAM data even when the Gameboy itself is switched on and off, clearing the Gameboy's internal memory.

It's on my TODO list of things to implement but sadly I haven't had much time for this project recently, and the time I have spent on it has mostly been around supporting Gameboy Color games.

Ah, cool then I think I understand what to expect better! ๐Ÿ™๐Ÿป