Page 1 of 1

Expected Memory Values

Posted: Sun Aug 06, 2017 3:12 pm
by NekadZut
Other than I/O ports for PPU, vectors, service routines, and program code, should there be any specific values saved in memory? Looking at the iNES rom for Super Mario Brothers, there are the following instructions after the header:

Code: Select all

...
LDA $2020      ; contents of A now 0x00
BPL FB         ; branch is not taken
LDA $2020      ; contents of A still 0x00
BPL FB         ; branch is not taken (A is not negative)
LDY #FE
LDX #05
LDA ($07D7, X) ; contents of A still 0x00
CMP #0A        ; 0x00 - 0x0A
BCS 0C         ; branch is not taken (carry not set)
DEX
BPL F6         ; Branch is taken, but the offset is a large negative which throws PC below the beginning of ROM
...
I initialize all values of memory to 0x00 before I load in the ROM and setup interrupt vectors and handlers.
Is the rom expecting values at address $2020 and $07DC? Is it supposed to send PC below the starting point of the ROM because the BPL is checking the flag after a DEX which is still positive?
Or should one of the previous branches be taken?

Re: Expected Memory Values

Posted: Sun Aug 06, 2017 3:22 pm
by lidnariq
NekadZut wrote:

Code: Select all

LDA $2020      ; contents of A now 0x00
BPL FB         ; branch is not taken
LDA $2020      ; contents of A still 0x00
BPL FB         ; branch is not taken (A is not negative)
Those are $2002, not $2020.


To directly answer your question:
RAM in the NES does not have a well-defined power-on state. The specific values depend on the specific RAM IC and the power supply design. They will often have a pattern (which may even be "mostly $FF") but there will be bits that are random every time it powers up. Which specific bits will vary from machine to machine.

Re: Expected Memory Values

Posted: Sun Aug 06, 2017 3:35 pm
by tokumaru
Also, "instructions after the header" might not mean much, because those aren't necessarily the first instructions to run. Those instructions might run at a later time, never run at all, or not even be instructions (it could be data)!

If you want to know for sure which instructions are the first to run, you have to look at the address indicated in the reset vector.

Re: Expected Memory Values

Posted: Sun Aug 06, 2017 3:46 pm
by tepples
$2002 is a memory-mapped input and output (MMIO) port. Its value will change depending on the state of the PPU. Usually when you see a game spinning on $2002 bit 7 to become true twice in a row, it's waiting for the PPU to stabilize.

Re: Expected Memory Values

Posted: Mon Aug 07, 2017 7:30 am
by AWJ
NekadZut wrote:Other than I/O ports for PPU, vectors, service routines, and program code, should there be any specific values saved in memory? Looking at the iNES rom for Super Mario Brothers, there are the following instructions after the header:

Code: Select all

...
LDA $2020      ; contents of A now 0x00
BPL FB         ; branch is not taken
LDA $2020      ; contents of A still 0x00
BPL FB         ; branch is not taken (A is not negative)
LDY #FE
LDX #05
LDA ($07D7, X) ; contents of A still 0x00
CMP #0A        ; 0x00 - 0x0A
BCS 0C         ; branch is not taken (carry not set)
DEX
BPL F6         ; Branch is taken, but the offset is a large negative which throws PC below the beginning of ROM
...
I initialize all values of memory to 0x00 before I load in the ROM and setup interrupt vectors and handlers.
Is the rom expecting values at address $2020 and $07DC? Is it supposed to send PC below the starting point of the ROM because the BPL is checking the flag after a DEX which is still positive?
Or should one of the previous branches be taken?
The problem is you shouldn't be executing those instructions at all (they look to me like data disassembled as code). The 6502 starts execution at the address pointed to by the RESET vector, not at the start of the ROM. Also, there's something wrong with your disassembly; LDA ($07D7, X) isn't a valid 6502 addressing mode. Indirect addresses are always 8-bit zero page addresses except for the JMP instruction.

EDIT: whoops, the reset vector in SMB1 does point to $8000 so those are the right instructions; multiple errors in your disassembly just made it look like garbage code to me.

LDA $2020 should be LDA $2002, which is a PPU register.

LDA ($07D7,X) should be LDA $07D7,X (plain indexed addressing, not indirect)

Yes, the game really is reading from uninitialized memory here. What it's doing is detecting a cold or warm boot. If it's a warm boot (the reset button was pressed) then the previous high score is retained (the RAM starting at $07D7 contains the high score, stored as one decimal digit per byte) If it's a cold boot (the console was just powered on and RAM contains garbage) then the high score is reset to a default value.

Finally BPL $F6 isn't a "large branch", it's a branch by -10 which goes back to the LDA $07D7,X instruction. Branch displacements are twos complement: $FF is -1, $FE is -2, $FD is -3, and so on up to $80 which is -128. If you're programming in modern C or C++ you can just cast the operand to an int8_t (signed 8-bit integer) to get the correct branch displacement. If you're programming in a language that doesn't have fixed-width integer types, you have to do it by hand like this: (operand & 0x7f) - (operand & 0x80)

Re: Expected Memory Values

Posted: Wed Aug 09, 2017 6:32 am
by NekadZut
Oh, I'm boosted.

Sorry for that question then

I swear I know CS :lol: