blog-static/content/blog/01_learning_emulation.md

3.3 KiB

title date tags
Learning Emulation, Part 1 2016-11-23 23:22:42.779811
C and C++
Emulation

I've decided that the purpose of a blog is to actually use it every once in a while. So, to fill up this blank space, I'll be documenting my own experience of starting to learn how emulation works. I'd like to say right now that my main goal was not to learn emulation. Rather, I needed to emulate to refresh my skills for a different subject area. However, emulation turned out fun enough to write about.

Choosing a Target

To emulate is to mimic the behavior of something. In terms of video games, emulation attempts to run games made for a console - without the console. To do so, one must read and execute the game's code, which is written as machine code for the processor inside the console. To be able to execute the code, we must know exactly how the processor would do so.

At this point, you can probably imagine that emulating really complex processors would be, if nothing else, very tedious. They have huge amounts of different instructions and these instructions would need to be each implemented in our code. So, jumping too far ahead and trying to emulate something complex like a PS2 may make you overwhelmed. When it comes to starting out, small, rewarding, steps are the best.

As many people who have just started to learn about emulation, I picked Chip-8 as the first thing I'm going to emulate. Chip-8 isn't actually a console by itself. It's an interpreted programming language which is intended to be run by a processor. However, Chip-8 is very low-level and running it will give us a good taste of what console emulation is like.

Doing Some Reading

Remember how I said we'd need to know exactly how the processor executes the game code? I wasn't lying. If we want to emulate Chip-8, we need to understand exactly how it was intended to be run. Fortunately, its Wikipedia page contains lots of information about it. Since Chip-8 isn't run on a console - it's an interpreted language - it's instead run in a "virtual machine". A virtual machine we can think of, for now, as an equivalent to a console that only exists in our imagination. So, without further ado, let's start examining this virtual machine.

According to the Wiki page, Chip-8 was commonly implemented on machines with 4096 memory locations all of which are a byte. However, since on these machines the "interpreter" - what actually reads and executes the Chip-8 code - occupied the first 512 bytes, programs typically start right after 512 bytes. The last 256 + 96 bytes are used "internally", so they're not accessed either. Chip-8 has 16 1-byte registers, named V0 through VF and the last register, VF, is used as a carry flag. Another register, I, is present in Chip-8 and is twice as big as the others. This is so that it can be used in operations involving memory. Chip-8 also has a stack with a maximum length of 16, used only for subroutines as well as two timers counting down at 60hz. The display is 64x32 with pixels being either "on" or "off", and input is done through a hex keyboard with 16 keys.

Phew, that was a lot of information. Whoever might read this will probably need to take time to process this much information, and I'll need to take time to write more. So long, for now!