Just for fun

I didn't want to spend too much time on this, so I used a very naive approach.

I used the C language. The code is available here.

As I said, I used a very naive approach, so don't take this code as an example of how to write an emulator. I just wanted to have some fun. If I ever feel that I don't have anything better to do, I might come back to this project and improve it :-)

A low level rabbit hole

A few months back I got into a "low level rabbit hole". It all started after I watched some Ben Eater videos. That guy is amazing! I think I watched all of his videos. I even bought his 6502 computer kit and built it myself.

After that, I discovered the Nand2Tetris courses on Coursera, part 1 and part 2. Those were the best courses I've ever taken. I highly recommend them.

And the journey continued. I wanted to go a bit deeper and so I got interested in emulators. I took the course Creating a Chip-8 Emulator in C on Udemy. I liked it a lot, but there was not much room for struggling with the code, and we all know that we only learn when we struggle. So I had to try and build something on my own.

I decided to build a NES emulator, that would be quite a project, I thought to myself.

I had to learn more about the 6502 assembly language and the NES architecture. The website nesdev.org is a big help. I started to write some code, trying to emulate the CPU. During that process I stumbled upon the course NES Programming with 6502 Assembly by Gustavo Pezzi - an amazing teacher - and I had a blast with it. I would learn just enough about one 6502 instruction and then try to implement it in my emulator. The course is not about emulation, but on how to actually build games for the NES. So I would build one of the very simple "games" taught in the course and then try to make my emulator run it. It was a unique way to learn that language.

Where I didn't go very far

I also had to learn a bit about the PPU (Picture Processing Unit). I didn't want to spend too much time on those, so I just implemented the bare minimum to get some games running. I if were to do this properly I would have to work on this for several months.

I think the PPU was the trickiest part for me. There are a lot concerns regarding timing and synchronization, and how it implements screen scrolling. Instead of drawing the screen line by line, as televisions do, I made it so it draws tile by tile, instead. And to get the timing somewhat right, I just played with the numbers in the main loop until I got something that sort of worked.

I did not implement the APU, the sound processor.

Conclusion

The whole process took me about a month. If you know a bit about the 6502 assembly language, the nesdev.org website should be everything you need to build an emulator, even if you want to go a lot further than I did, but you can have a lot of fun by also consulting other sources.

Building these things is a very nice way to solidify some computer science concepts.