It's super interesting to write your first emulator. I wrote a GBC emulator too once, and I wrote down all the problems I encountered that weren't easy to solve:
Many of the issues are not documented anywhere, and it is just about guessing what the hardware did - and thus, what the developers back in the day relied on!
Nice idea to write that stuff down. Something that flummoxed me for a while was how Pokemon Yellow does the Pikachu sound, since the Game Boy doesn't really support sample-based audio. Turns out it does something akin to spending 100% of its CPU time turning the sound on and off to play the samples.
For anyone who might be curious as to how this worked in practice, Retro Game Mechanics does an excellent breakdown of the technique used here: https://www.youtube.com/watch?v=fooSxCuWvZ4
Maybe a full month to get GameBoy ROMs up, if I remember correctly. It did take a while, as there's always room for misunderstanding how something is supposed to work!
I also wrote a gameboy emulator a few years ago... But I disagree about it being fun.
The truth is it's rather tedious. There are 256 opcodes you will need to create. Each has certain flags you will need to check that change it's operation.
Some of those flags are undocumented, and several of the opcodes actually have hardware level bugs that make them act differently than the CPU document describes.
So after creating every opcode, you will inevitably have bugs which you need to fix.
After fixing those you will still not have games running because of so many undocumented/buggy opcodes, so now you need to figure out how an actual gameboy operated.
At this point I was only able to solve it be getting a working emulator and executing the same program on each until their states diverged.
I did find it interesting to learn about low level hardware, but I'd say the project is 50% reading specs, 10% interesting, and 40% tedious pain-in-the-butt.
I never did get sound working, and the graphics are buggy, but I think I got far enough that I learned all there was to learn.
I targetted getting the boot rom to animate and make sound over a short time, and it ended up taking a bit longer. But when I got the boot rom to run, I did stop. I didn't know about the fact that the halt opcode caused clicks in the audio. I used ALSA for the sound and the hard part was understanding what the memory register flags meant.
> Some of those flags are undocumented, and several of the opcodes actually have hardware level bugs that make them act differently than the CPU document describes.
They don't, whatever documentation you used was either wrong or based on the Intel 8080 or Z80 instead of the Sharp SM83 core that's actually used in the original Gaemboy.
Yes it was wrong! I used the 'gameboy programming manual' which was sort of the magnum opus of gameboy documentation at the time that was available to the fan community.
I am googling it now, and there are some forum posters saying ' don't use that, it's old and not completely accurate' but it was all that I could find at the time.
I'm aware that it's not official documentation, but you have to remember this was over 10 years ago--there might be better resources now.
If you want to write a game console emulator, but prefer to start with something simple — I suggest to look into https://en.wikipedia.org/wiki/CHIP-8 It could be implemented in a couple of evenings in any language.
There are dozens of toy GB emulators, but nobody has yet emulated the wildly successful Bandai Digimon Digivice. It's based on near-identical technology to Bandai's "Tamagotchi" virtual pet though Digimon was marketed mostly towards at boys and Tamagotchi was marketed mostly towards girls.
It's from the same era as the Game Boy Color and has some interesting features (serial communications between devices, and the infamous "tab cheat" that momentarily disconnecting the battery with a plastic tab to randomize the memory contents).
Interesting how Pokemon is widely emulated, but nobody ever bothered with Digimon. There has been multiple generations of Digivice hardware, so best start at the original.
I'd like to see somebody take on the reverse-engineering challenge. I don't think there's even a ROM dump yet.
They've actually started selling new stock recently (20th Anniversary Digivice), including at retail stores for around $30 each.
Thank you for sharing! One of my very slow (two kids one new born makes life slow) but deliberate projects is to write a Gameboy emulator and every new article or write-up provides more context into this confusing world.
Very interesting article but a bit short. If anyone have links to more in-depths journals of writing emulators, please let me know.
I'm very interested in learning.
MAME may be a better choice. Despite its reputation as an arcade emulator, it is more accurately described as an emulator construction kit, which is logically necessary since most arcade games differ on the hardware level at least to some degree.
That said, MAME drivers for most home consoles and computers already exist since the MAME and MESS projects merged.
https://github.com/fwsGonzo/gamebro/blob/master/POSTERITY.md
Many of the issues are not documented anywhere, and it is just about guessing what the hardware did - and thus, what the developers back in the day relied on!