Nes 6 kb rom program address ?

Building your first 6502-based project? We'll help you get started here.
Santa-Fe
Posts: 14
Joined: 06 May 2021

Nes 6 kb rom program address ?

Post by Santa-Fe »

Hi everybody
I am new here
I am doing some basic work reading Nes rom , I've choosed a small size rom (Nuts & Milk) , which is 6000 bytes
I have read that the program's banks addresses are :
$8000 $4000 PRG-ROM
$C000 $4000 PRG-ROM

how that would be applicable on the rom I've mentioned above ?
any explanations are welcomed .
User avatar
BigEd
Posts: 11464
Joined: 11 Dec 2008
Location: England
Contact:

Re: Nes 6 kb rom program address ?

Post by BigEd »

Welcome! 6000 bytes sounds very unlikely to me. A quick look leads me to expect a 16k program ROM and an 8k character ROM.

AFAICT a 16k NES ROM will appear at $8000 and $c000 because of incomplete address decoding, so that's probably what's meant by the bank addresses.

http://wiki.nesdev.com/ is a good place to look for details.
Santa-Fe
Posts: 14
Joined: 06 May 2021

Re: Nes 6 kb rom program address ?

Post by Santa-Fe »

Thanks for welcoming me :)
you are right
I opened it with a hex viewer , last address was 6000 , but that is in hexadecimal !
then it should be 24576 bytes , windows explorer says it is 24.0 KB (24,592 bytes)
I will check the link you've posted , I will be back soon , I need to ask some more questions .
User avatar
BigEd
Posts: 11464
Joined: 11 Dec 2008
Location: England
Contact:

Re: Nes 6 kb rom program address ?

Post by BigEd »

If you're seeing 24k, you're probably seeing the concatenation of the program ROM and the character ROM. I think not how the two ROMs look to the NES: "An NES cartridge has at least two memory chips on it: PRG (connected to the CPU) and CHR (connected to the PPU)."

If you're seeing 24k+16 bytes, thats a file format for emulators which has a 16 byte header.
Santa-Fe
Posts: 14
Joined: 06 May 2021

Re: Nes 6 kb rom program address ?

Post by Santa-Fe »

I am using a C compiler to read the entire rom inside a buffer byte array as the following :

Code: Select all

rom = (unsigned char*) malloc (sizeof(unsigned char)*64001); // allocate enough space
fread (rom,sizeof(unsigned char),24592,fp); // 
I checked my array compared to the hex editor , and it's fine , both are equal
now , how to jump to program first instruction ?
just like the following :

Code: Select all

pc=0x8000; // program counter variable
instruction = rom [pc] ;
something confusing me alot :
$8000 in hexadecimal = 32768 !
$C000 in hexadecimal = 786432 !

how would these values fit to my rom file , it's only 24 kb ?

forgive me I am new to that stuff .
Last edited by Santa-Fe on Sat May 08, 2021 9:43 pm, edited 2 times in total.
User avatar
BigEd
Posts: 11464
Joined: 11 Dec 2008
Location: England
Contact:

Re: Nes 6 kb rom program address ?

Post by BigEd »

You need to read the reset vector to find the start address of the code. Check out the nesdev wiki.
JimBoyd
Posts: 931
Joined: 05 May 2017

Re: Nes 6 kb rom program address ?

Post by JimBoyd »

Santa-Fe wrote:
I am using a C compiler to read the entire rom inside a buffer byte array as the following :

Code: Select all

rom = (unsigned char*) malloc (sizeof(unsigned char)*64001); // allocate enough space
fread (rom,sizeof(unsigned char),24592,fp); // 
I checked my array compared to the hex editor , and it's fine , both are equal
now , how to jump to program first instruction ?
just like the following :

Code: Select all

pc=0x8000; // program counter variable
instruction = rom [pc] ;
something confusing me alot :
$8000 in hexadecimal = 32768 !
$C000 in hexadecimal = 786432 !

how would these values fit to my rom file , it's only 24 kb ?

forgive me I am new to that stuff .

Hexadecimal $C000 = 49152 decimal.
Hexadecimal $C0000 = 786432 decimal.
Santa-Fe
Posts: 14
Joined: 06 May 2021

Re: Nes 6 kb rom program address ?

Post by Santa-Fe »

JimBoyd wrote:
Hexadecimal $C000 = 49152 decimal.
Hexadecimal $C0000 = 786432 decimal.
Still larger than my 24 kb rom addresses .
Martin A
Posts: 197
Joined: 02 Jan 2016

Re: Nes 6 kb rom program address ?

Post by Martin A »

If your file is 16 bytes longer than 24k, then as, there's likely a 16 byte header. Unless you know the format of the header, ignore it ! it's very unlikely to be part of the orignal game.

If you're expexting data from 2 chips one 8k for graphic definitions one 16k for the game code, then there's 2 options, 8k first or 16k first.

What you're interested in is the last bytes of the code rom, as they will be most likely be the 6502 hardware vectors.

If the 8k graphic one is first, then the last 6 bytes of the 16k rom will be the very last 6 bytes of the file.

Code: Select all

Option 1 -  file offsets
0000-000F - Header
0010-200F - Graphic data
2010-6009 - game code
600A-600F - 6502 Vectors
If the 16k rom is first, then the last 6 bytes of the 16k rom will be at offset 0x400A

Code: Select all

Option 2 -  file offsets
0000-000F - Header
0010-4009 - game code
400A-400F - 6502 Vectors
4010-600F - Graphic data
If all 3 vectors look sensible, then you could have the right spot. If they don't make sense, you could be looking at graphic data. Where the vecors jump too should give you clues as to where the rom expects to be run from.

For example reset or interrupt vector of 0xBXXX wouldn't be in a rom located at 0xC000 to 0xFFFF so the rom in that case would probably expect to be 0x8000 to 0xFFFF.
Santa-Fe
Posts: 14
Joined: 06 May 2021

Re: Nes 6 kb rom program address ?

Post by Santa-Fe »

Hi Martin
your explanation is interested
is that the way how emulators get the start address of the program ?
I am currently working on a (mapper 0) rom , (no mapper ,no scrolling right now)
I just need to start with a simple emulator (currently the CPU)

I am still confused how addresses like : $C000 = 49152 decimal, $8000 = 32768 decimal
can be used to addressing a rom with 24592 decimal ?
User avatar
BigEd
Posts: 11464
Joined: 11 Dec 2008
Location: England
Contact:

Re: Nes 6 kb rom program address ?

Post by BigEd »

Hmm, what did you make of my message mentioning the PRG and CHR ROMs?
Santa-Fe
Posts: 14
Joined: 06 May 2021

Re: Nes 6 kb rom program address ?

Post by Santa-Fe »

BigEd wrote:
Hmm, what did you make of my message mentioning the PRG and CHR ROMs?
I am totally lost , I have read the wiki that you've mentioned , also googled alot before and after posted my question
but I can't figure how a rom ends with address $6000, can be addressed with range $8000 and $c000 !
User avatar
BigEd
Posts: 11464
Joined: 11 Dec 2008
Location: England
Contact:

Re: Nes 6 kb rom program address ?

Post by BigEd »

I've just realised something which might be a root of confusion.

In the world of emulated cartridge games, people say 'ROM' when they mean 'Some binary data which encapsulates everything important about a particular cartridge'

In the world of 6502 computers, people say 'ROM' when they mean 'Some binary data which will be found in an EPROM which is seen at some range of addresses in the 6502 address space'

The *.nes file which you have, of length 24592, is a ROM in the first sense. It's not a ROM in the second sense. It contains 3 things
- a 16 byte header
- 16k of program data to be mapped at $8000 and also at $C000
- 8k of character data to be made available to the PPU

If you want to investigate the 6502's view of the game, you need to extract the 16k PRG ROM image and map it into memory twice, once at $8000 and again at $C000. And then you can read the reset vector at $FFFC to get the start address.
Santa-Fe
Posts: 14
Joined: 06 May 2021

Re: Nes 6 kb rom program address ?

Post by Santa-Fe »

Hi BigEd
Quote:
- 16k of program data to be mapped at $8000 and also at $C000
1. What I can understand of that is :
the 16k prog. data will start at $8000 and end at $C000

2. is the meaning of map it
put it at a new addresses ranges which are ($8000 - $C000) to be suitable for the 6502 nes system ?
User avatar
BigEd
Posts: 11464
Joined: 11 Dec 2008
Location: England
Contact:

Re: Nes 6 kb rom program address ?

Post by BigEd »

The same data has to appear twice in the memory map, because that's how the machine works. An emulator might possibly do that for you, or you might need to place the ROM data twice.
Post Reply