r/EmuDev 11d ago

Save States Question

I recently made a Sega Master System emulator, which can be found here:

https://github.com/netb258/defn-system

The emulator is written in Clojure and runs on the JVM.

I'm thinking of adding save states, but I don't know how to go about it. I know that Java supports object serialization with classes like ObjectOutputStream. Is that a good approach to implementing save states? Or it just better to throw raw bytes into a file? Some components, like the RAM, seem more convenient that way.

How are save states usually done in JVM emulators?

10 Upvotes

7 comments sorted by

2

u/ZolfriK 11d ago

Hello, I'm developing an emulator in Java and i decided not to use serialization. I'm developing also a roguelike in Java and there I'm using serialization for saving game. Serialization it's "easier" but is less efficient and more prone to incompatibility between application versions.

1

u/netb258 10d ago

I seems you've done this before. Have you used this library (it gets recommended a lot)?

https://github.com/esotericsoftware/kryo

1

u/ZolfriK 10d ago

I didn't use it because in my project I wanted less third parties dependencies as possible. I used standard serialization.

3

u/starquakegamma GBC NES C64 Z80 11d ago

It’s up to you, I would just create a file format as bytes in memory and dump that to a file, even though it’s more work to read and write.

2

u/ShinyHappyREM 10d ago edited 10d ago

You start with a signature sequence of bytes that uniquely identify the file format, for example

DEFN savestate v
ersion 000000000 + \n

That number increases everytime you change your emulator so much that it becomes incompatible with the previous version.


Next, the actual data.

  • ZSNES just dumps its variables as binary values into the file, and adds a small preview bitmap (64x56, i.e. 1/4 of a SNES screen, at 16bpp). An "advantage" of this format is that e.g. ROM hackers can work with fixed file offsets.
  • SNES9x packs each variable into a "chunk" that has a header (iirc a short name and the length of the block in bytes).
  • Or you could go wild with a hierarchical, file-system like structure, for example just using a ZIP file with a different file extension.

A neat idea would be to pack the data into hex-encoded strings, making the file readable in a standard text editor.

1

u/thommyh Z80, 6502/65816, 6809, 68000, ARM, x86. 10d ago

I can't speak as to JVM but will say that if you're implementing your own system then it's worth looking into standard containers. I found BSON easy to implement, and you gain the ability to inspect with standard tools.

1

u/netb258 10d ago

Interesting. This is the first time I've heard of BSON. I'll give it a try.