Working on my first emulator, things aren't working right

This is an archive of a topic from NESdev BBS, taken in mid-October 2019 before a server upgrade.
View original topic
Working on my first emulator, things aren't working right
by on (#219368)
I decided to work on an NES emulator to understand hardware better, but it's not working right.

I think it has to do with some misunderstanding I have with mappers. I'm using cpu_dummy_reads.nes as my test rom. My loading process; I read the ROM, read the 16 byte header, the (in this case) 32767 bit long program, the CHR, and I do nothing with the rest of the file because I've reached the end of it.

Because I'm using C#, I decided to be lazy and just have one 64 KB byte[0xFFFF] as all readable memory. I load my PRG into 0x8000 to 0xFFFF and set my program counter to 0x8000 (The addresses that mapper five says the PRG should go). I hit the run button and expect to start programming some opcodes. The opcodes are invalid, though. I'm running into 24576 empty opcodes (00), on top of some other strange things.

I also tried with donkey kong and I get some other problem with reading invalid opcodes.

Source https://pastebin.com/sCVFPcuY

And if I look at the actual hex data for cpu_dummy_reads.nes, after the header there are tons of 00's. I'm pointing my emulator to this code to start executing right off. Is this right?
Re: Working on my first emulator, things aren't working righ
by on (#219369)
You need to start execution at $FFFC, not $8000. In other words, the value stored at $FFFC tells you where to set your program counter to start execution. See http://wiki.nesdev.com/w/index.php/CPU_memory_map.
Re: Working on my first emulator, things aren't working righ
by on (#219370)
I get invalid opcodes at FFFC

Am I loading it incorrectly?
Re: Working on my first emulator, things aren't working righ
by on (#219372)
isosceles wrote:
In other words, the value stored at $FFFC tells you where to set your program counter to start execution.
This is the important part.

The first sentence (which I haven't quoted) was misleading.
Re: Working on my first emulator, things aren't working righ
by on (#219373)
You're not supposed to execute those bytes. That's two bytes representing the address for the reset vector location. Take those two bytes and bring the program counter to the address they represent and start executing the code there. I think it means go to address 0xE57F in that case.
viewtopic.php?t=3677
Re: Working on my first emulator, things aren't working righ
by on (#219377)
Or if the bytes at $FFFC are $EF $CD, execution begins with PC at $CDEF. So to put it another way: Treat reset as if it does SEI followed by an indirect JMP ($FFFC) and you'll be 99% correct.
Re: Working on my first emulator, things aren't working righ
by on (#219390)
That seemed to do the trick, I think it's all working as far as I can tell. Thank you. :)