I wanted to start writing a new console emulator for some time now. There’s still work do on my other projects — could add some fun features to Boyo.js and mr.system has not been updated in a while — but I need something fresh. I chose to try to emulate the Sega Genesis/Megadrive since it was my first game console and it’s a bit special to me.
Motorola 68000 emulation
I’ll start by coding the Motorola 68000 CPU (m68k). The first step is to look at some documentation. I found a nice table with all its opcodes and this seems more manageable than the GameBoy’s Z80.
I need to think about a nice way to emulate all those instructions. Writing all the opcode variations by hand would take weeks and be no fun at all.
In my earlier GameBoy emulator, I abstracted addressing modes and instructions to factorize all the existing variations (eg. one
add() function compatible with a set of addressing mode proxies). It was ok but the virtualization was bad for performance and I may not get away with this approach on this particular project (gotta go fast!).
In my unfinished Master System emulator, I generated C++ code in Python. The Python part inspected the chip’s specs and outputted all the instruction variations. Going back and forth being generator and generated code was a pain when debugging. I’d prefer a nicer workflow.
The m68k’s opcodes are coded on 16 bits so there are only 65536 possible instructions. I could generate each one of them and store them in some sort of jump table. Pseudo-code:
// Generate the instruction set
for (int i = 0; i < 0x10000; i++)
instructions[i] = generate(i);
// Run the program
generate(opcode) is the tricky part. There, I would need to parse the bit pattern of
I looked at other m68k implementations and Musashi, a reference emulator used in Mame, seems to take this approach (see build_opcode_table). I’ll start experimenting.