iNES header missing entry point?

This is an archive of a topic from NESdev BBS, taken in mid-October 2019 before a server upgrade.
View original topic
iNES header missing entry point?
by on (#159849)
Where can I find the entry point for a ROM from parsing the iNES header? After getting a lot of my logic working I just realized that I have no idea where I'd be setting PC when a ROM gets loaded.

I originally thought that it was PRG ($8000), but it doesn't appear to be. Thanks.
Re: iNES header missing entry point?
by on (#159852)
The entry point is as if JMP ($FFFC), an indirect jump, had been executed.
Re: iNES header missing entry point?
by on (#159853)
On a RESET, the CPU loads the vector from $FFFC/$FFFD into the program counter and continues fetching instructions from there.

Source
Re: iNES header missing entry point?
by on (#159854)
And what's mapped at $FFFC/$FFFD on power up depends on the mapper, which IS specified in the header.
Re: iNES header missing entry point?
by on (#159856)
Bowie90333212391 wrote:
entry point

Only executable file formats have an entry point. The iNES header doesn't contain an entry point because it's not an executable file format.

It sounds like you're already familiar with executable file formats, so you probably already know that executable files are loaded into memory by some other program, and the entry point tells the loader where to jump after the file is loaded. On the NES, there is no loader. As soon as you plug the cartridge in, the program is present in the CPU's address space. From there, the CPU's reset behavior causes it to begin running the game.


For comparison, N64 ROMs are an executable file format. The N64 BIOS parses the executable header in order to load the game from the cartridge ROM into RAM and begin executing it.
Re: iNES header missing entry point?
by on (#159864)
Wrong. The NES's entry point is the reset vector at the end of the ROM. Don't say different, that is where it is. It's not contained in the header, no. But like tokumaru said, the header tells which mapper, which tells the layout, which tells the entry.
Re: iNES header missing entry point?
by on (#159869)
Some mappers have multiple possible entry points, depending on which page of PRG is mapped into the $FXXX region on reset. Many mappers do not guarantee any particular banking setup, so a reset stub needs to be present in every bank.

Like, $FFFC/D should usually correspond to 4 bytes before the end of PRG region of the iNES file, but it could be in other places as well, and some mappers might not map the last bank to that region.
Re: iNES header missing entry point?
by on (#159872)
rainwarrior wrote:
Like, $FFFC/D should usually correspond to 4 bytes before the end of PRG region of the iNES file, but it could be in other places as well, and some mappers might not map the last bank to that region.

Hence the need to know the mapper in order to map the contents of the ROM in the NES memory map as a real cartridge would, even if that means mapping random banks in some cases (an emulator doesn't have to bother with randomizing banks though, since games that are programmed to work on such mappers are able to boot from any bank, so you could get away with always loading the last, or the first, or whatever bank you'd like). Once everything is mapped, just read the 16-bit value at $FFFC and use that as the starting value of your PC.