Some time ago I created a list of things I wanted to do as a programmer. Creating a minecraft clone, a game engine and a software renderer are some of the items sharing the list with “create an emulator”. After some talks with my good friend Kleverson at the office, he convinced me to make a Gameboy emulator. It’s not as complex as other game consoles from that time, but it’s definitely not trivial.
I got motivated because I saw all the potential subjects I would be exposed while doing this. Learning computer organization in practice, get more familiar with assembly language (not a specific dialect, but whole low-level concept), understand how primitive graphics systems worked, improve my debug techniques, and end up with working project. You can realize by now that I’m selling the idea of creating an emulator, so grab a coffee and let’s do this together.
Repository mygbemu is already live on GitHub and you can check the code here. It’s not even close to be functional, it only runs the bios software so far and I’m currently working to make Tetris run on it. You’ll see more in this project as I write the posts.
Environment and Setting
I decided to use C because I wanted to get more familiar with it, since I hadn’t coded in C for a long time. It should run on Windows and Linux from the beginning, even though I mostly work on it on Windows with Visual Studio. The only 3rd-party library I’m using is SDL for the graphics, but we don’t need this right now.
The first version of the gameboy was released in 1989 and was based on a hardware from almost 15 years before. An 8-bit CPU (4MHz) based on the Intel 8080, 8kB RAM and 8kB VRAM. It’s very important to read the Wikipedia entry for the Gameboy and watch this amazing video before continuing this article.
Where to start?
That’s the question my programming friends ask me when I say I’m working on an emulator. And I did that myself. You know that an emulator emulates, but emulates what? Every electronic component? What are the components? Do I need to understand the target platform from the physical level?
Well, first we need to understand that “emulation” is not “simulation”. You don’t have to implement things exactly the way they are, no need for wires or electric pulses. Remember the goal is to run a program that was made for that architecture and the results should be pretty much similar, but the way you do does not matter. For instance, the console uses a memory mapping as a bridge to connect the “video card” to the cpu, but you can make a direct call from your cpu module to the video, if the outcome is the same, you’re doing it right.
We have many components we need to emulate, mostly important the Memory, CPU, Video Unit and Sound Unit. This also is the order that we are going to implement. Since everything relies on the memory mapping, that’s what we are going to do first.
Memory Management Unit
First thing we need to know is that since gameboy’s CPU is 8bit, its registers can only hold 8 bits of data at a time. But they can be accessed in pairs, for instance A, F, B, C, H, and L registers can be accessed with the combination AF, BC, and HL. The PC and SP are 16 bit only registers. So we have 16 bit addressable memory, going from 0x0000 to 0xFFFF, that means we have 65,536 positions. However, it’s not all RAM memory or anything. As mentioned previously, gameboy uses the memory as a bridge between CPU and other components. So this memory has regions that access data from the ROM, the cartridge, video memory, sound, etc.
This is an accurate map of the gameboy’s MMU from this page, which I recommend you to visit to gather more information about each area.
Jumping to the code, we can just define our memory management, basically following the areas:
bioswill store the 256 bytes startup code. When the gameboy starts, the MMU will map it in the first 256 bytes of the memory, so that’s why we need the
finished_biosflag. And it’s a pointer because it needs to be initialized pointed to position
addris the complete addressable memory, so it’s really easy to write and read from it
- the’re in a union because we can also directly access each fraction of the MMU
Let’s initialize (and destroy) our MMU:
Now we need to write basic read/write functions:
These functions will read and write 8 bit values, but since our CPU can work with 16 bit values, our MMU also needs to be able to read and write these values:
Now we can play a little bit with this code. Read and write both 8 and 16 bit values to our MMU.
Let’s also load the bios code:
You can also check the complete assembly for this code here. It’s gonna be very important once we start the CPU.
Can you believe our MMU is basically done? I mean it does almost everything the CPU needs, so we can move on to it. But before, let’s play a little:
You can check the final code at GitHub. It’s a bit different, but that’s because I’m going by steps.
Next time we will be looking at the CPU.Share