Skip to content
0:00 / 0:00

#nesemulator#6502#c++

I made a NES emulator in 15 minutes

Bisqwit

147K subscribers • 9 videos on Hallway

YouTube
  • Recommended by 3 people: 3 on Hacker News.
  • A classic. People kept recommending it from 2014 to 2022.
  • Presented by Bisqwit

Advanced. Deep dive for people who know the field.

389K views14 years ago3 recommendations

Systems programmingHistory and cultureRetro computingC++

Bisqwit demonstrates how to build an accurate NES emulator in C++ using compile-time template metaprogramming for instruction decoding.

In this tool-assisted education video I create a NES emulator with C++0x. ENABLE CAPTIONS! The emulator is very accurate, and sort-of portable; it compiles on any architecture that support libSDL, but it outputs audio through an external program and reads joypad input from a file. This video is part 1/2. -- Part 1 ( youtu.be/y71lli8MS8s ): Creating the emulator. -- Part 2 ( youtu.be/XZWw745wPXY ): Compiling and running. Twitter: twitter.com/RealBisqwit Patreon: patreon.com/Bisqwit (alternatives at iki.fi/bisqwit/donate.html) Twitch: twitch.tv/RealBisqwit Homepage: iki.fi/bisqwit/ The source code is about 940 lines in total. Download & FAQ & resources: bisqwit.iki.fi/jutut/kuvat/programming_examples/nesemu1/ Approximate count of lines of code per topic, in the source code: - 20 lines, CPU: Declarations (registers, flags and internal memory) - 100 lines, CPU: Interpreting and executing CPU opcodes & signals - 100 lines, CPU&GamePak: Memory mapping (iNES mappers 0,1,2,3,7 are supported) - 40 lines, PPU: Declarations (registers and emulator-specific status variables) - 50 lines, PPU: Memory mapping and I/O - 180 lines, PPU: Rendering and timing control - 30 lines, PPU & IO: Joypad updating (PPU provides the timing for input-file access) - 40 lines, PPU & IO: Color NTSC emulation (converting NES colors into RGB through NTSC modem) - 15 lines, IO: libSDL initialization and rendering - 35 lines, main&IO: ROM loading from file, initialization and emulation loop - 200 lines, APU: Sound emulation (of which 40 lines are because of DMC/DPCM support) - 130 lines, (other, such as comments, and inaccuracies in the above numbers) Check out MaiZure’s excellent walkthrough of the source code of the emulator from this video! maizure.org/projects/decoded-bisqwit-nes-emulator/ Background music: Jinguuji detective series. It was difficult to choose. I wanted good NES music, but I did not want it to too recognizable. I intended to convert something, but I had already spent months on this video... It was getting late. Because of the 15 minute limit that YouTube was imposing on my account when I made this video, I had to develop many techniques to shorten the code dramatically. I also had to type it very quickly… And I had to split the actual demonstration into a second video. I use C++11 exclusive techniques extensively in the code. I am particularly proud of the CPU emulator. It is cycle and memory access accurate, and feature-complete. It is not very slow, either. It is only 100 lines long, thanks to a number of clever ideas. I thank byuu for a particular idea that helped make it horizontally shorter. Each if() in ins() is completely parsed and evaluated at compile-time. Effectively the compiler synthesizes 259 distinct ins() functions, each performing only the particular opcode's worth of work. I architected all the components in as close conformance as I could to whatever documentati

Why it's here

3 people recommend this

What people say. Widely recommended as a masterclass in clever C++ programming, template metaprogramming, and emulator design.

petercooperon Hacker News • Recommends

On “Let’s Create a Speech Synthesizer in C++17 [video]”

Agreed. My favourite programming YouTuber by a long stretch. If anyone is unfamiliar with him and wants to watch some of the "classics" I'd recommend these: His "cracking videogame passwords" series: link Creating a Doom-style 3D engine in C: link Creating a Rogue-like game in C++11: link Creating a PIC16 emulator and NES music player: link Creating a NES emulator: link to this video Creating a DOS VGA raytracer: link

petercooperon Hacker News • Recommends

On “Ask HN: Your favorite YouTube channels?”

…guy who founded Something Awful has a ridiculously addictive channel - link - he generally plays horrifically poorly produced indie games with his sidekick Shmorky and I could listen to their absurdist banter all day. It's a bit of a mish mash but link always blows my mind when he does his coding videos such as coding a NES emulator in C++11: link to this video link is awesome if you're into supercars. He's currently doing a tour of European supercar events and factories putting up a video each day. Far Lands or Bust - link - is a guy who started to walk towards the 'far lands' in a Minecraft maps years ago. He's something like 10% of the way but is still plodding along recording his progress.…

atestion Hacker News • Recommends

On “Nes emulator written in go”

Here's a very clean and clever NES emulator in C++ by Bisqwit of TAS fame: link to this video He presents the code in the style of a tool assisted run, typing it in in high speed. I never saw templates used like that to create the unrolled CPU dispatcher

petercooperon Hacker News • Recommends

On “Ask HN: What is your favorite YouTube channel for developers?”

link is by far my favorite and I've spent many hours watching him. He does things like create a Doom-style engine from scratch: link .. create a NES emulator: link to this video .. work back from a C++17 example to show why new C++ standards are needed: link .. and even building a Tetris clone in GW-BASIC: link .. Right now, he's doing a series on cracking 80s videogame passwords: link Sirajology - link - is another interesting one. He moves a bit too quickly for me, but does things like explain machine learning in 5 minutes or how to generate music with systems like Tensorflow.

molticrystalon Hacker News • Recommends

On “Smolnes: A NES emulator in less than 5000 significant bytes of C++”

Those who enjoy this might also enjoy "Creating a NES emulator in C++11" 0 1 2 while not short in bytes of code(space) there is a nice video series short in minutes(time). Bisqwit takes you on a 30 minute tool assisted coding journey of making an NES emulator and also provides a 15 minute followup Q&A video explaining different aspects. 0 link to this video 1 link 2 link

GhotiFishon Hacker News • Mentions

On “A quick look at the Redis source code”

Yes. It is certainly more pithy. ACTUALLY! It reminds me of a technique Bisqwit used when he made his emulator. He used strings to define the behavior of certain instructions, the strings were actually interpreted at compile time. Though I think this is a C++ specific trick. link to this video he brings in the instruction table at 1:30

grapeshoton Hacker News • Mentions

On “Show HN: I made an easy-to-understand 6502 emulator in pure C”

It's possible to do something like that in C. In C++, you can even do some template magic to create all the appropriate functions for each opcode based on something like the original instruction decode ROM. Brilliant and short code, but very difficult to write in the first place. Here's a very apropos example of this technique as applied to the 6502. link to this video

iofjon Hacker News • Mentions

On “The Strange Appeal of Watching Coders Code”

I love having streams of coding open while working. It makes me type faster (I think). Sound is off though. link to this video

0xcde4c3dbon Hacker News • Mentions

On “Goboy: Multi-Platform Nintendo Game Boy Color Emulator Written in Go”

The main advantage to NES emulation is that it's a very mature area of study. High-quality documentation is much more readily available than for other systems, and there's more public discussion of the specific problems that NES emulator authors encounter (both primarily on nesdev.com 1 2). There's even a sort of tool-assisted speedrun of writing an accurate NES emulator, if you're into that sort of thing 3 4. 1 link 2 link 3 link to this video 4 link

u/Smellypuce2in r/C_Programming • Mentions

On “I'm going to start to write a NES emulator in plain C. So...”

This isn't in C, but bisqwit did a video of him making one in C++. You might be able to gather some useful info from it.

u/serendibin r/C_Programming • Mentions

On “Writing an emulator”

That massive switch statement is exactly why I would recommend C++ for this type of project just for templates. Check out Bisqwit's NES emulator video here, it will blow your mind: link to this video

u/ooqqin r/C_Programming • Mentions

On “How would one write an emulator in C?”

link to this video warning c++ ahead