I've been analyzing code very thoroughly and I've come up with a few minor questions that I can't seem to answer. The questions I am asking are in the comments of the following code. I've attempted to detail every line of code to ingrain it into my head.
Also, I can't seem to figure out why the background colour of my rom is the 17th byte. I thought the background colour was supposed to be the first byte of the palette at $3F00, am I doing something differently? Here are the following snippets.
Code:
Reset:
SEI ;disable IRQs - Why?
CLD ;disable decimal mode - NES has no Decimal mode
; would it really matter if this were left out?
LDX #$FF
TXS ;Set up stack - Is this necessary?
SEI ;disable IRQs - Why?
CLD ;disable decimal mode - NES has no Decimal mode
; would it really matter if this were left out?
LDX #$FF
TXS ;Set up stack - Is this necessary?
Code:
;NMI routine, is called at the beginning of every frame if turned on
;uses: update graphics
NMI:
LDA #$00
STA $2003 ;$2003 is the PPU's OAMADDR address
; OAM (Object Attribute Memory)
; write the address of OAM you want to access (?)
; this is the low byte of the full address
; since our sprites are at $0200, $xx00 is low byte
LDA #$02
STA $4014 ;$4014 is the PPU's OAMDATA (mirrored from $2004)
; write OAM data here (NESWIKI)
; -Why do we write to this address instead of $2004?
RTI ;Returns program to where it was interrupted,
; (the Forever loop!)
;uses: update graphics
NMI:
LDA #$00
STA $2003 ;$2003 is the PPU's OAMADDR address
; OAM (Object Attribute Memory)
; write the address of OAM you want to access (?)
; this is the low byte of the full address
; since our sprites are at $0200, $xx00 is low byte
LDA #$02
STA $4014 ;$4014 is the PPU's OAMDATA (mirrored from $2004)
; write OAM data here (NESWIKI)
; -Why do we write to this address instead of $2004?
RTI ;Returns program to where it was interrupted,
; (the Forever loop!)
Also, I can't seem to figure out why the background colour of my rom is the 17th byte. I thought the background colour was supposed to be the first byte of the palette at $3F00, am I doing something differently? Here are the following snippets.
Code:
LoadPalettes:
LDA $2002 ;Set-up PPU to choose your colours
LDA #$3F ; a read to $2002 resets the address gate
STA $2006 ; at $2006 which reads the high byte ($00xx)
LDA #$00 ; followed by the low byte ($xx00)
STA $2006
LDX #$00
LoadPalettesLoop: ;This loop goes through all the colours we chose
LDA palette, x ; in the palette routine at the bottom
STA $2007 ; and sends them to the PPU's $2007 address
INX ; where all data is sent/received
CPX #$20 ;This checks if X has reached $20
BNE LoadPalettesLoop ;Go back if X isn't at $20 (32 bytes) yet
LDA $2002 ;Set-up PPU to choose your colours
LDA #$3F ; a read to $2002 resets the address gate
STA $2006 ; at $2006 which reads the high byte ($00xx)
LDA #$00 ; followed by the low byte ($xx00)
STA $2006
LDX #$00
LoadPalettesLoop: ;This loop goes through all the colours we chose
LDA palette, x ; in the palette routine at the bottom
STA $2007 ; and sends them to the PPU's $2007 address
INX ; where all data is sent/received
CPX #$20 ;This checks if X has reached $20
BNE LoadPalettesLoop ;Go back if X isn't at $20 (32 bytes) yet
Code:
palette:
.db $04,$17,$37,$3C ;Background Palette (first 16 colours/bytes)
.db $00,$00,$00,$00 ; arranged in colour sets of 4
.db $00,$00,$00,$00 ; Byte 1 is always transparent
.db $00,$00,$00,$00 ; Byte 2-4 will be whatever colour you choose
.db $00,$20,$22,$00 ;Sprite Palette (second 16 colours/bytes)
.db $00,$00,$00,$00
.db $00,$00,$00,$00
.db $00,$00,$00,$00
.db $04,$17,$37,$3C ;Background Palette (first 16 colours/bytes)
.db $00,$00,$00,$00 ; arranged in colour sets of 4
.db $00,$00,$00,$00 ; Byte 1 is always transparent
.db $00,$00,$00,$00 ; Byte 2-4 will be whatever colour you choose
.db $00,$20,$22,$00 ;Sprite Palette (second 16 colours/bytes)
.db $00,$00,$00,$00
.db $00,$00,$00,$00
.db $00,$00,$00,$00