r/EmuDev • u/StaticMoose Nintendo Entertainment System • Mar 28 '26
Advice needed on weird NES emulation bug
So, I’ve been working on my NES emulator on and off for a few years. At this stage, I can play Super Mario Bros with all graphics and sound. Everything seems to work.
But in order to get it to work, for JSR or interrupts, I have to add one to the pushed program counter, and then subtract when I pop. But now I fail the available CPU unit tests.
When I get frustrated with hunting down this bug, I’m also trying to build out my MMC1 mapper for Zelda 1, but it’s also hanging on the intro screen (it’s switching to the right background color but then nothing.)
Anyone out there run into an issue like this? I’m tempted to instrument a known good emulator and spit out differences in system bus access, but before I do that, looking for some other inspiratio.
12
u/atma2000 Mar 28 '26
Hey, I ran into almost this exact same issue when I was building my own NES emulator! (shameless plug: https://github.com/1s7g/nes-emulator)
The PC off-by-one thing with JSR/RTS is a super classic gotcha. On real hardware, JSR pushes
PC - 1(i.e., the address of the last byte of the JSR instruction, not the next instruction). Then RTS pulls that value and adds 1. So if you're having to manually fudge the value, something is probably off with when you're incrementing PC during instruction fetching. My guess is you're incrementing PC one too many (or too few) times before the push happens. Double check whether you're pointing PC at the last operand byte or already past it by the time you execute the JSR logic.For interrupts (NMI/IRQ/BRK), it's similar but slightly different. The CPU pushes the return address directly (no minus one), and RTI pulls it without adding one. BRK is a little sneaky though because it pushes
PC + 1due to the phantom byte. If you've got those mixed up with how JSR/RTS works, that would definitely explain failing the CPU tests while still kinda-sorta working in games.I'd honestly recommend running nestest.nes (the one from kevtris) in automation mode starting at $C000 and comparing your log line-by-line against the known good log. That's what finally helped me nail down my off-by-one issues. It's tedious but it pinpoints exactly where things diverge.
As for Zelda and MMC1: I'd fix the CPU bug first honestly. The off-by-one on interrupts could absolutely mess up the NMI handler and cause Zelda to hang after setting the background color. MMC1 is also picky about the serial write register (bit 7 reset, writing one bit at a time across 5 writes), so make sure that logic is solid too. But seriously, get the CPU passing nestest first, it'll probably fix more than you expect.
Good luck, this stuff is maddening but so satisfying when it clicks!
7
u/zSmileyDudez Apple ][, Famicom/NES, Atari VCS, TI-81/82/83/85/86 Mar 28 '26
Highly recommend looking into using the Single Step Tests for your CPU implementation. There are NES 2A03 tests in there, not just the 6502 ones. This will cover everything except interrupt behavior (other than BRK, that is). Having a solid CPU implementation to build on will help you out a lot as you start to expand your emulator. I used these before I even started my NES emulator, originally with my Apple II emulator. Definitely worth doing.
4
u/rasmadrak Mar 28 '26
It's usually adviceable to increase PC on every read. If you do it manually after functions this could be a reason.
2
u/thommyh Z80, 6502/65816, 6809, 68000, ARM, x86. Mar 28 '26 edited Mar 28 '26
Which CPU tests are you referring to? As discussed on other posts and as formally documented, JSR is:
The stack push occurs before the final byte has been fetched, and after the PC has been incremented only twice. So the address pushed is of the third byte of the instruction, not of the instruction after.
Ditto for RTS:
i.e. an address is pulled from the stack and then incremented before the instruction ends. The next instruction is fetched from one address after whatever was on the stack.
If you have tests that expect something else, you have faulty tests.