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

Discuss emulation of the Nintendo Entertainment System and Famicom.

Moderator: Moderators

Post Reply
jamieyello
Posts: 3
Joined: Mon Jun 04, 2018 8:56 am

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

Post by jamieyello »

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?
Attachments
The output I'm getting
The output I'm getting
isosceles
Posts: 17
Joined: Sat Mar 08, 2014 12:59 pm

Re: Working on my first emulator, things aren't working righ

Post by isosceles »

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.
jamieyello
Posts: 3
Joined: Mon Jun 04, 2018 8:56 am

Re: Working on my first emulator, things aren't working righ

Post by jamieyello »

I get invalid opcodes at FFFC

Am I loading it incorrectly?
Attachments
broke2.PNG
broke2.PNG (13.06 KiB) Viewed 7084 times
lidnariq
Posts: 11432
Joined: Sun Apr 13, 2008 11:12 am

Re: Working on my first emulator, things aren't working righ

Post by lidnariq »

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.
User avatar
nesrocks
Posts: 563
Joined: Thu Aug 13, 2015 4:40 pm
Location: Rio de Janeiro - Brazil
Contact:

Re: Working on my first emulator, things aren't working righ

Post by nesrocks »

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
https://twitter.com/bitinkstudios <- Follow me on twitter! Thanks!
https://www.patreon.com/bitinkstudios <- Support me on Patreon!
tepples
Posts: 22708
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: Working on my first emulator, things aren't working righ

Post by tepples »

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.
jamieyello
Posts: 3
Joined: Mon Jun 04, 2018 8:56 am

Re: Working on my first emulator, things aren't working righ

Post by jamieyello »

That seemed to do the trick, I think it's all working as far as I can tell. Thank you. :)
Post Reply