It is currently Sun Dec 17, 2017 2:47 pm

All times are UTC - 7 hours





Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: Expected Memory Values
PostPosted: Sun Aug 06, 2017 3:12 pm 
Offline

Joined: Fri Aug 04, 2017 4:32 pm
Posts: 7
Location: Atlanta, Georgia
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:
...
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?


Top
 Profile  
 
PostPosted: Sun Aug 06, 2017 3:22 pm 
Online

Joined: Sun Apr 13, 2008 11:12 am
Posts: 6538
Location: Seattle
NekadZut wrote:
Code:
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.


Top
 Profile  
 
PostPosted: Sun Aug 06, 2017 3:35 pm 
Offline
User avatar

Joined: Sat Feb 12, 2005 9:43 pm
Posts: 10169
Location: Rio de Janeiro - Brazil
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.


Top
 Profile  
 
PostPosted: Sun Aug 06, 2017 3:46 pm 
Online

Joined: Sun Sep 19, 2004 11:12 pm
Posts: 19354
Location: NE Indiana, USA (NTSC)
$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.


Top
 Profile  
 
PostPosted: Mon Aug 07, 2017 7:30 am 
Offline

Joined: Mon Nov 10, 2008 3:09 pm
Posts: 431
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:
...
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)


Top
 Profile  
 
PostPosted: Wed Aug 09, 2017 6:32 am 
Offline

Joined: Fri Aug 04, 2017 4:32 pm
Posts: 7
Location: Atlanta, Georgia
Oh, I'm boosted.

Sorry for that question then

I swear I know CS :lol:


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 7 hours


Who is online

Users browsing this forum: No registered users and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group