YouTube home Comedy Week on YouTube
Upload

Creating a NES emulator in C++11 (PART 1/2)

Bisqwit Bisqwit·284 videos
1,990
60,098
Like     Dislike 18

Sign in to YouTube

Sign in with your Google Account (YouTube, Google+, Gmail, Orkut, Picasa, or Chrome) to like Bisqwit's video.

Sign in to YouTube

Sign in with your Google Account (YouTube, Google+, Gmail, Orkut, Picasa, or Chrome) to dislike Bisqwit's video.

Sign in to YouTube

Sign in with your Google Account (YouTube, Google+, Gmail, Orkut, Picasa, or Chrome) to add Bisqwit's video to your playlist.

Uploaded on Dec 4, 2011

In this tool-assisted education video I create a NES emulator with C++0x. You see me type every line.
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 ( http://youtu.be/y71lli8MS8s ): Creating the emulator.
-- Part 2 ( http://youtu.be/XZWw745wPXY ): Compiling and running.

The source code is about 940 lines in total.
Download & resources: http://bisqwit.iki.fi/jutut/kuvat/pro...
FAQ: http://bisqwit.iki.fi/jutut/kuvat/pro...

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)

Background music: Jinguji 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 still imposed on my account when this video was posted (for some reason that never got explained); 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 documentation I found on nesdev.com regarding how they work. There may be a few minor shortcuts that I took in order to avoid the trap of investing 90 % of source code to fix a problem that affects 10 % of games.

In design, my primary guiding principle was the assumption, that the _hardware_ was always designed for simplicity (with regards to chipspace consumption). Most compact design that gets the work done. This means, that whenever there is a behavior that seems to require extra-ordinary amount of work (code-lengthwise) from emulator authors to replicate accurately, I would take the extra effort to try and see the big picture and figure out how to generalize it. If the hardware does unproductive work at time, it is because they reused some component and did not needlessly create a special case to disable the component for those times where its function does not produce benefit. I would, rather than adding a special case to replicate that unproductive work, also find a way to reuse a productive part, so that the unproductive work comes automatically as a side effect, just like on the hardware.
I always tried to replicate that simplicity in my emulator. I think I managed quite well in that regard. It is not perfect though: It does not pass _all_ tests by Blargg, and some games that should run, outright crash at start. But the compatibility according to my tests is still very high, and interestingly, many TASes made with FCEUX run also on my emulator.

Note that creating an emulator is perfectly legal. I wrote all the code from scratch; it comes from my mind, and is therefore entirely my copyright; it is not anyone else's copyright. I believe that programming is art, and my code is my means of expressing myself. There exists no legislation that prohibits anyone from e.g. printing this source code on a t-shirt (assuming that *I* gave permission to that). I am also not selling this as a product, i.e. even in the dystopian event that I should happen to be using algorithmic methods that someone else has previously patented against every notion of conscience and wisdom, I am still not infringing on any patents. Or so I believe.

Loading icon Loading...

Loading icon Loading...

Loading icon Loading...

Loading icon Loading...

Ratings have been disabled for this video.
Rating is available when the video has been rented.
This feature is not available right now. Please try again later.

Uploader Comments (Bisqwit)

  • MrSlowestD16

    I'm a dev for a fortune 50 working in C/C++ all day.....I like to think of myself as at least "decently" knowledgeable, especially when it comes to C/C++...

    ...

    What I just watched in this video completely blew me the fuck away....hats off to your skills, sir...

    .

    Also, didn't know anybody was actively using the C++ 1x, I saw lots of weird syntax here, I'm guessing it was attributed to that, is it really that much better than the current spec?

    · 3

    Sign in to YouTube

    Sign in with your YouTube Account (YouTube, Google+, Gmail, Orkut, Picasa, or Chrome) to rate MrSlowestD16's comment.

    Sign in to YouTube

    Sign in with your YouTube Account (YouTube, Google+, Gmail, Orkut, Picasa, or Chrome) to rate MrSlowestD16's comment.
  • Bisqwit

    C++11 _is_ the current spec. It was approved as ISO/IEC standard 14882:2011.

    Yes, it is very good. The next working draft is called C++14.

    Thank you for your feedback. Now mind you, the only C++11 things I used in this video were constexpr, auto, and the lambda functions which are identified with this weird syntax: [&]() {}

    There is plenty more in C++11. Definitely recommended to study it, if you like programming in C++!

    P.S. C11 (update to standard C) was also released around the same time.

    ·

    Sign in to YouTube

    Sign in with your YouTube Account (YouTube, Google+, Gmail, Orkut, Picasa, or Chrome) to rate Bisqwit's comment.

    Sign in to YouTube

    Sign in with your YouTube Account (YouTube, Google+, Gmail, Orkut, Picasa, or Chrome) to rate Bisqwit's comment.
    in reply to MrSlowestD16 (Show the comment)
  • MrSlowestD16

    Oh, and some of that code made me feel completely and utterly inadequate, haha. Your passion for the C obfuscation code contest shows I guess ^_^. Though I'm wondering, how much did you have done before you made this video? I mean there's no restructuring, no backspacing, nothing, it's just 100% done in one shot. Even at 8x speed that's only 2 hours. I mean some other people mentioned time to look at spec sheets and the like but maybe you just know the HW that well..? Thx.

    ·

    Sign in to YouTube

    Sign in with your YouTube Account (YouTube, Google+, Gmail, Orkut, Picasa, or Chrome) to rate MrSlowestD16's comment.

    Sign in to YouTube

    Sign in with your YouTube Account (YouTube, Google+, Gmail, Orkut, Picasa, or Chrome) to rate MrSlowestD16's comment.
    in reply to Bisqwit (Show the comment)
  • Bisqwit

    The whole program was designed beforehand to the letter. It is explained in the FAQ.

    ·

    Sign in to YouTube

    Sign in with your YouTube Account (YouTube, Google+, Gmail, Orkut, Picasa, or Chrome) to rate Bisqwit's comment.

    Sign in to YouTube

    Sign in with your YouTube Account (YouTube, Google+, Gmail, Orkut, Picasa, or Chrome) to rate Bisqwit's comment.
    in reply to MrSlowestD16 (Show the comment)
  • Arson Izer

    Wow dude, amazing work. I'm trying to code a CHIP8 emulator in Java to learn more about this stuff, and I'm looking at tutorials/other people's source codes/documentation, and really, I only know the very basics of processors, and pretty much have 0 knowledge of assembly, so it's all VERY hard. I can't even understand why all the bit shifts happen, it all seems so random to me. What did you study to be able to do this, and how long did it take you to be this good?

    ·

    Sign in to YouTube

    Sign in with your YouTube Account (YouTube, Google+, Gmail, Orkut, Picasa, or Chrome) to rate Arson Izer's comment.

    Sign in to YouTube

    Sign in with your YouTube Account (YouTube, Google+, Gmail, Orkut, Picasa, or Chrome) to rate Arson Izer's comment.
    in reply to Bisqwit (Show the comment)
  • Bisqwit

    Bit shifts are usually done because data is in one format and it needs to be in another format. Why? Because the format is specified as such, for example in PPU's specs.

    I'm sorry, but questions like "how long did it take" are kind of useless in my opinion. How do you even respond to something like that? Should I tell when I began programming (1992)? Should I somehow discount times when I did not do something relevant? That is difficult. I do know that you can learn quickly if you are motivated.

    ·

    Sign in to YouTube

    Sign in with your YouTube Account (YouTube, Google+, Gmail, Orkut, Picasa, or Chrome) to rate Bisqwit's comment.

    Sign in to YouTube

    Sign in with your YouTube Account (YouTube, Google+, Gmail, Orkut, Picasa, or Chrome) to rate Bisqwit's comment.
    in reply to Arson Izer (Show the comment)

Top Comments

  • Bisqwit

    I await eagerly to see what you will make!

    · 23

    Sign in to YouTube

    Sign in with your YouTube Account (YouTube, Google+, Gmail, Orkut, Picasa, or Chrome) to rate Bisqwit's comment.

    Sign in to YouTube

    Sign in with your YouTube Account (YouTube, Google+, Gmail, Orkut, Picasa, or Chrome) to rate Bisqwit's comment.
    in reply to Marcia lowe (Show the comment)
  • Bisqwit

    You seem to have missed the part where I said "I'm not an iPhone user and don't really even know what development tools you might use on that platform." You go ahead. I do not take application requests, unless you're willing to pay a competitive hourly price (which in the case of phone platforms would include also the time required to study how to develop stuff for that platform).

    · 15

    Sign in to YouTube

    Sign in with your YouTube Account (YouTube, Google+, Gmail, Orkut, Picasa, or Chrome) to rate Bisqwit's comment.

    Sign in to YouTube

    Sign in with your YouTube Account (YouTube, Google+, Gmail, Orkut, Picasa, or Chrome) to rate Bisqwit's comment.
    in reply to Vkrispies (Show the comment)

Video Responses


All Comments (387)

Sign in now to post a comment!
  • neoknux009

    this is crazy. and amazing. and beautiful. 

    ·

    Sign in to YouTube

    Sign in with your YouTube Account (YouTube, Google+, Gmail, Orkut, Picasa, or Chrome) to rate neoknux009's comment.

    Sign in to YouTube

    Sign in with your YouTube Account (YouTube, Google+, Gmail, Orkut, Picasa, or Chrome) to rate neoknux009's comment.
  • 707494718100a

    DosBox

    ·

    Sign in to YouTube

    Sign in with your YouTube Account (YouTube, Google+, Gmail, Orkut, Picasa, or Chrome) to rate 707494718100a's comment.

    Sign in to YouTube

    Sign in with your YouTube Account (YouTube, Google+, Gmail, Orkut, Picasa, or Chrome) to rate 707494718100a's comment.
    in reply to Sudo Bash (Show the comment)
  • Sudo Bash

    What editor are you using?

    ·

    Sign in to YouTube

    Sign in with your YouTube Account (YouTube, Google+, Gmail, Orkut, Picasa, or Chrome) to rate Sudo Bash's comment.

    Sign in to YouTube

    Sign in with your YouTube Account (YouTube, Google+, Gmail, Orkut, Picasa, or Chrome) to rate Sudo Bash's comment.
  • weedoctor1

    yeah im watching this like WTF is happening here xD

    ·

    Sign in to YouTube

    Sign in with your YouTube Account (YouTube, Google+, Gmail, Orkut, Picasa, or Chrome) to rate weedoctor1's comment.

    Sign in to YouTube

    Sign in with your YouTube Account (YouTube, Google+, Gmail, Orkut, Picasa, or Chrome) to rate weedoctor1's comment.
    in reply to MrMoodyKSA (Show the comment)
  • cartographer43

    The x86 PC book by Mazidi is pretty good.

    · 2

    Sign in to YouTube

    Sign in with your YouTube Account (YouTube, Google+, Gmail, Orkut, Picasa, or Chrome) to rate cartographer43's comment.

    Sign in to YouTube

    Sign in with your YouTube Account (YouTube, Google+, Gmail, Orkut, Picasa, or Chrome) to rate cartographer43's comment.
    in reply to MegaFitaCrepe (Show the comment)
  • jjgamepro16

    Awesome video! Sifting through your source code is pretty daunting though... I'm no where near you in skill (And I thought I was pretty good haha). Care to share some of your best resources in learning how to learn the architecture? I had started a project a while ago to implement a MOS 6502 emulator but never finished it... Your way of doing it is totally different from how I was doing it. To me, your way is really obscure (No offence). You've definitely got mad skills though!

    ·

    Sign in to YouTube

    Sign in with your YouTube Account (YouTube, Google+, Gmail, Orkut, Picasa, or Chrome) to rate jjgamepro16's comment.

    Sign in to YouTube

    Sign in with your YouTube Account (YouTube, Google+, Gmail, Orkut, Picasa, or Chrome) to rate jjgamepro16's comment.
  • Arson Izer

    Thanks for the bit shift explanation. I appreciate the answer. I agree that the "how long did it take" question doesn't really have an answer, but it's interesting to know that you've been programming for at least 20 years. I wish to someday be as good as you are with this kind of stuff (or maybe even better, hehe). Again, thanks for the answer.

    ·

    Sign in to YouTube

    Sign in with your YouTube Account (YouTube, Google+, Gmail, Orkut, Picasa, or Chrome) to rate Arson Izer's comment.

    Sign in to YouTube

    Sign in with your YouTube Account (YouTube, Google+, Gmail, Orkut, Picasa, or Chrome) to rate Arson Izer's comment.
    in reply to Bisqwit (Show the comment)
  • MrSlowestD16

    Ah, gotcha, I was too busy picking my jaw up off the floor to read the 'about' :-P. Thanks for the direction :).

    ·

    Sign in to YouTube

    Sign in with your YouTube Account (YouTube, Google+, Gmail, Orkut, Picasa, or Chrome) to rate MrSlowestD16's comment.

    Sign in to YouTube

    Sign in with your YouTube Account (YouTube, Google+, Gmail, Orkut, Picasa, or Chrome) to rate MrSlowestD16's comment.
    in reply to Bisqwit (Show the comment)
  • Loading comment...
Loading...
Loading...
Working...
Sign in to add this to Watch Later