Verifying a 6502 Emulator with Nestest
Quest for the Golden Logs and how I verified the CPU logic:: ON THIS PAGE 3
Writing the NES enumerator has been an interesting exercise. There is a lot of opcodes and logic that needs to be implemented and while you can test each opcode in isolation, it doesn't provide any sense of security that you have is correct until you start stitching more pieces together.
How do I know that my logic holds up once we are a few thousand cycles into a program where we hit some edge case I didn't think about?
What is Nestest?
nestest.nes from what I can determine seems to be the holy grail of NES CPU testing. Written by Kevin Horton (kevtris), who is well known in the NES emulator community, created a test ROM that exhaustively checks every official (and many unofficial) instructions.
The ROM itself is only half to tool, the power lies in the Golden Log which is a text file generated by the cycle-accurate Nintendulator that lists the exact state of every register and flag after every single instruction.
My goal? Run my emulator and compare it to the Golden Log, line for line.
Setting Up The Test
After some digging around and reading on how to even load a ROM and run it. "Simulating NROM-128 Mirroring" Since nestest is a 16KB ROM but the NES CPU expects a 32KB cartridge address space, we must mirror the data. We copy the 16KB payload into both the lower bank ($8000) and the upper bank ($C000). This ensures that the Interrupt Vectors at $FFFC (which tell the CPU where to start) are present and pointing to the correct code.
// Mock Bus for testing
With that out of the way now we can read in the nestest.nes load it and run it.
Unoffical Opcodes
While I had implemented all the supported opcodes, it was kind of funny to watch this fail on so quickly.
)
$04 is a Zero Page NOP. The difference here from a regular NOP is that this takes 2 bytes. It fetches an operand from Zero Page and ignores it. Why? I need to read a little more and try to understand the benefit of this...
Anyway, after I implemented the rest of the NES opcodes I was able to execute this test.
====
====
So I was at least able to process and run the ROM to completion.
Since the Golden Log is from an actual emulator with PPU and includes a disassembler. I need to find a way to see if I can match my output with the Gold Logs. If not ill have to revisit the Golden Log validation later on in the process. Maybe I should look into building a disassembler...