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]
Definitely need to check out later. I have always been interested in writing an emulator.
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.
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.
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 ?
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.
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.
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 :-)
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.
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.
Looks like joe http://en.m.wikipedia.org/wiki/Joe's_Own_Editor
He wrote the editor. I’m not sure if it has a name but this FAQ has some information about the tools: http://bisqwit.iki.fi/jutut/kuvat/programming_examples/nesemu1/FAQ.txt
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
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.
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].
Thanks for the link, that’s very neat!
Byuu wrote an article for ars technica about bsnes and emulation in general. It is a *very* good read, and I recommend everyone to check it out: http://arstechnica.com/gaming/2011/08/accuracy-takes-power-one-mans-3ghz-quest-to-build-a-perfect-snes-emulator/
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!
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.
Bisquit is the *man*.
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.
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.
haha ‘bit’….
Write only code? Is that used with write only memory? ;) http://en.wikipedia.org/wiki/Write-only_memory_(joke)
From links in your article: http://en.wikipedia.org/wiki/Write-only_language
From the video this is the link to his site that contains the code for the emulator
http://bisqwit.iki.fi/source/nes.html
http://bisqwit.iki.fi/src/
I program and I like the format. Haters gonna hate.
Can you put an NES emulator onto an microcontroller?
Like this one:
https://www.youtube.com/watch?v=mk8DjcywBxM