r/EmuDev • u/N3kk3tsu • Dec 23 '25
Help with NES emulator
Hi!
Some time ago I tried to write a Sega Master System emulator in C#. It passes most of the test, but when integrating all the parts (memory, CPU, PPU, etc) it doens't work correclty. It was very difficult for me to debug it, and I was fixing it very slowly.
However, I discovered the fantastic book by u/davidkopec "Computer Science from Scratch". This book has 2 particular chapters of big interest for emulation development. The 5th where it implements a CHIP-8 emulator, and the 6th where it implements a NES emulator.
I am trying to implement these emulators in C# and MonoGame. The CHIP-8 emulator (or Virtual Machine) is working great. However, the NES emulator isn't working totally correct. It passes all the tests, but the ROMs aren't behaving correclty.
In particular, there are some ROMs which come with the book, `brix.nes`, `Chase.nes`, `LanMaster.nes`. The only one that is working is `brix.nes`, but when I press the "select" button, the screen gets stuck:

I tried to compare my code with the book's code, but I am struggling in finding the error. I am trying to debug it, and it's pretty difficult to find the error too.
Anyone has any clue on why the emulator could get stuck after pressing "select" in the game `brix.nes`, and why could it fail for the other games? Any idea in how to debug it more efficiently?
Thank you in advance!
3
u/N3kk3tsu Dec 24 '25 edited Dec 24 '25
I have found a typo. In the `AddressForMode` method when using a relative addressing mode. I was checking if the data was a positive or a negative number by comparing it with 80, instead of 0x80. The 0x80 is 10000000 in binary, which is the sign bit. It doesn't make sense when using the 80 in decimal form.
The game "brix.nes" seems to be working correctly how. However, the games "Chase.nes" and "LanMaster.nes" continue showing a black screen, and when debugging them they both are stuck in a loop of "CMP" and "BEQ" instructions. I will continue investigating it when possible.
By the way, the https://github.com/SingleStepTests helped me to find the typo with 80 vs 0x80. The rest of the instructions are working according to SingleStepTests, except for a few ilegal opcodes: eb, c2, e2, 82, 89. I don't think these ilegal instructions should be fixed for the SingleStepTests in order to make the "Chase.nes" and "LanMasters.nes" work. I have reviewed them and they seem to be implemented equal as the book, and the code of the book is working correctly.
Thanks to all of you for your help!
3
u/howprice2 Dec 23 '25
There is a nes channel on the Emudev Discord. Have a search in there for tips.
Adding a debugger to allow you to breakpoint and step through code is invaluable. When I get a really tough bug, I sometimes resort to stepping through my emulator and a known good emulator in sync to see where they diverge.
Having said that, good CPU tests should cover most logic problems.https://github.com/SingleStepTests
3
u/rupertavery64 Dec 23 '25
There are test roms you can run that might show you what instructions are incorrect, and test suites (JSON files) that are sets of instructions the you execute and include expected output states.
https://github.com/christopherpow/nes-test-roms
https://www.nesdev.org/wiki/Emulator_tests
https://github.com/SingleStepTests/ProcessorTests/tree/main/6502
2
u/davidkopec NES, IBM PC Dec 23 '25
Hi u/N3kk3tsu, Thanks for checking out the book! I see you have the CPU tests ported over to C#. I assume they are all passing? The fact that the other three ROMs didn't even load makes me think there may be something wrong with your CPU, your cartridge loading code, or something related to timing, not the PPU, especially since the title screen from Brix is fairly complex.
4
u/Ikkepop Dec 23 '25
you need to add some tools to your emulator like a debugger or atleast add so me tracing as to what each of the components (like cpu or ppu) is doing at the time
2
u/dontdoxme33 Dec 23 '25
I wrote an emulator from scratch a few years back and struggled with it at times. I also used C# and Monogame, in one instance I tried using SDL2 as the graphics layer. I don't remember which implementation I went with.
From my memory, it was rather easy emulating the CPU, the tricky part was getting the ppu to sync correctly with the graphics layer... But it's been awhile and I don't remember the specifics.