Help to debug Mesen Tracelog

Discuss emulation of the Nintendo Entertainment System and Famicom.
dan1bot
Posts: 2
Joined: Sun Aug 26, 2018 9:06 am

Help to debug Mesen Tracelog

Post by dan1bot »

Hi all,
I have some questions about Mesen's tracelog.

I'm debugging the 'instr_test-v3 / rom_singles / 01-implied.nes'.

And this is the output of the tracelog:

Code: Select all

E783 SEI                                 A:00 X:00 Y:00 P:nv--dIzc SP:FD CYC:30  SL:0   CPU Cycle:0
E784 JMP $EBCF                           A:00 X:00 Y:00 P:nv--dIzc SP:FD CYC:36  SL:0   CPU Cycle:2
EBCF LDA #$00                            A:00 X:00 Y:00 P:nv--dIzc SP:FD CYC:45  SL:0   CPU Cycle:5
....
Why does the CYC PPU start from 30?


thanks.
tepples
Posts: 22993
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)

Re: Help to debug Mesen Tracelog

Post by tepples »

Probably because it takes a few cycles for the 2A03's clock divider to spin up and the DMA controller and 6502 core to come out of reset. The 6502 alone accounts for 7 CPU cycles, per "Internals of BRK/IRQ/NMI/RESET on a MOS 6502" and "How many cycles for R65C02 Reset?", which covers 21 of the 30 dots.
Sour
Posts: 999
Joined: Sun Feb 07, 2016 6:16 pm

Re: Help to debug Mesen Tracelog

Post by Sour »

tepples is correct - there are (at least?) 7 CPU cycles of delay before the first instruction is executed (to fetch the reset address, etc.). This is 21 PPU cycles, but I ended up going with ~28? cycles purely because this is the value that gave the closest result compared to hardware results for the read2004.nes test, but there is no science behind the 28 cycles (odds are that it is somewhat incorrect).

The trace log's "CYC" value for the PPU is shown after the first byte of the instruction has been loaded, so it ends up writing 30 (which is the value of the last PPU cycle that was executed - e.g a total of 28+3 cycles have been executed by the time the CPU finishes processing the first instruction's first byte)

Trying it out in "Visual NES" and loading a NROM game into it, it looks like the first instruction starts executing around cycle 25-26. So Mesen is probably delaying the CPU's startup compared to the PPU a bit too much.

If you're just working on your CPU's code, though, you can just ignore the PPU cycles for now, the PPU should have no impact on any of those tests.
dan1bot
Posts: 2
Joined: Sun Aug 26, 2018 9:06 am

Re: Help to debug Mesen Tracelog

Post by dan1bot »

Thanks.

I found out where Mesen is doing this cycles.

https://github.com/SourMesen/Mesen/blob ... U.cpp#L106

Code: Select all

	//The CPU takes some cycles before starting its execution after a reset/power up
	for(int i = 0; i < (model == NesModel::NTSC ? 28 : 30); i++) {
		_console->GetPpu()->Exec();
	}