Emulators 101: how to write a program that functions like an NES CPU

We’d bet everyone reading this article has played a game on an emulator at some time or another. And you may have a base idea of how those emulators work. But we’d wager the vast majority of you are clueless about the actual implementation of game emulators (we know we are). But that has all changed after seeing this demonstration of how [Bisqwit] wrote his own NES emulator. The description doesn’t cover anything more than the basics of writing code that emulates the NES CPU hardware itself. But it’s presented in such a way that makes it quite easy to understand for anyone who has a basic knowledge of programming. He starts with a switch statement for handling the processor’s opcodes and then moves through piece by piece showing how he refined his code to make it work while keeping it readable. We think this is a great teaching method and appreciate the time he put into producing this tutorial.

The explanation starts about 4:22 into the video which is embedded after the break. You’ll also find the first two demo videos there. Those involve mostly fast-motion text editing of the emulator coding process with some gameplay tests at the end of the second video.

Explaining how to emulate CPU operations (jump to 4:22)

Emulator programming & Demo:

[via Reddit]

26 thoughts on “Emulators 101: how to write a program that functions like an NES CPU

    1. I suggest you start with a CHIP-8 emulator. Simple CPU (well, more of a virtual machine), simple graphics.

      I’ve written one, then I moved onto a Space Invaders emulator. Problem was you would die as soon as the game started so I was obviously missing some flag or mis-emulating a Z80 instruction. Never could find that bug.

      1. Yeah I saw sprite_m’s article on the chip8 and I found it pretty interesting. I was considering that but I would love nothing more than to write an emulator that could play SMB.

  1. Am I the only person that really wishes this was a series of articles rather than videos? What’s up with all these programming tutorials converting to videos ?

    1. On my internet I can’t even watch youtube on the 240p setting in realtime, ie it buffering at a rate greater than me watching it. I have to wait a good 10 minutes to watch a 5 minute video….

      Video tutorials for me are useless, also they are harder to look back and forth over. I prefer a well written series of pages to read, maybe the odd 30 second video clip if needed.

    2. I feel the same way; I’d much rather be able to read at my leisure, skim around, switch to my project and back to the text, etc. With a video you just have to sit and watch.

    3. I wholeheartedly agree (also with the other comments to your comment). Videos can be a great addition to see something “in action” but as the primary means of delivery it is really bad (and there are so many ways to do it wrong; bad picture, bad sound, accents, etc)

      But I also think anyone who makes something have the right to present it in the way they want to. however stupid I may happen to find it :-)

    4. Actually I thought that the iterative coding in the first video provided a very effective way of explaining what he did that was really easy to follow. If I was trying to get any actual work done with this information I would probably be annoyed, but I actually thought the video was pretty entertaining to watch.

    5. I actually prefer the video. This may be a generation gap kind of thing. I usually feel that older people prefer written articles. I enjoy them as well but I usually will first try and get the info I am looking for in video format.

      Kudos to the author of the video. Great stuff!! I wonder what was the editor he was using.

  2. I have played with CPU emulation on a PIC18F before, (doing a 6502) it was always a toss up of writing code for each instruction or writing code that understands the bits in each opcode, that is to simulate the gates, etc… Trying to minimize and speed up emulation

    1. Are you serious about this? Intuition tells me that attempting to simulate the gates would be orders of magnitude slower. It would be very interesting if this is not the case.

      1. There is actually a 6502 emulator that works gate-by-gate basis: See visual6502.org.

        You are right about it being orders of magnitudes slower [than instruction level emulation].

  3. my favorite thing is that the first video has OMF2197 theme song playing. awesome classic game. interesting since i was just looking that up yesterday. what a coincidence!

  4. As a fellow emulator author, I have to say I’m impressed. I wonder how well this captures the odd overflow flag and BRK behavior. One strong advantage of this code is because it uses a decode matrix, it is much more likely to reproduce the behavior of the bare-metal 6502. Look at his code and then look at a physical picture of the CPU circuit. Mind. Blown.

    1. Hi BLuRry, thank you for your feedback. I just noticed this page here.

      My program passes all the most rigorous CPU tests that currently exist, but it probably does NOT handle the IRQ-BRK coincidence properly (there is no test that tests it, and I could not produce one even though I spend days trying to make one). But that’s allright, for there is probably zero games that utilize the BRK opcode in the first place. There were two bugs in the YouTube release concerning the CPU that I fixed later.

      One advantage of the opcode decoding matrix is that I maximally avoid any code duplication related bugs. In the real 6502 there is just one section in the CPU that does $action, and in my core similarly there is just one. There is no five different implementations. Mostly! It is not an authentic reproduction of the opcode decoding matrix though. It is still an emulator, not a simulator.

  5. An incredibly good example on how write-only code is evolved. Its good work!
    I dont know a compiler that will handle constants larger than… 64 or 96 bits (long long and long double) long long is 0x55AA669955AA6699
    An evil trick I do often thats similar is to use 64 bit variables for 4 bit strings.
    long long a = ‘HELP'; note the single quotes, compliler complains, but it works. good reason to make 4 character commands on console interfaces. You can test them in 1 instruction.
    I’m glad to see someone out there does optmizations as crazy as mine.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s