Refactor SetBackgroundColor.

Some other minor comment clarifications & code formatting.
This commit is contained in:
Colin McMillen 2015-05-22 13:34:37 -04:00
parent ff6aa49f3e
commit ab032a4348

View File

@ -1,20 +1,18 @@
.INCLUDE "header.inc" .INCLUDE "header.inc"
.INCLUDE "InitSNES.asm" .INCLUDE "InitSNES.asm"
.BANK 0 SLOT 0
.ORG 0
.SECTION "MainCode"
; Memory layout: ; Memory layout:
; 00-0F: scratch space for functions. ; 00-0F: scratch space for functions.
; 10-13: controller state. ; 10-13: controller state.
; 14-17: 32-bit counter of vblanks. ; 14-17: 32-bit counter of vblanks.
; 20-22: [rgb] color values to use for background color. ; 20-22: [rgb] color values to use for background color.
;========================
; Start
;========================
.BANK 0 SLOT 0
.ORG 0
.SECTION "MainCode"
Start: Start:
InitSNES ; Initialize SNES. InitSNES ; Initialize SNES.
@ -48,9 +46,11 @@ Start:
jsr FillScratch jsr FillScratch
Loop:
wai MainLoop:
jmp Loop wai ; Wait for interrupt.
jmp MainLoop
VBlankHandler: VBlankHandler:
@ -61,8 +61,9 @@ VBlankHandler:
rti rti
VBlankCounter: VBlankCounter:
; Keeps a counter of how many VBlanks we've done. ; Increment a counter of how many VBlanks we've done.
inc $14 inc $14
lda $14 lda $14
cmp #$00 cmp #$00
@ -80,12 +81,13 @@ VBlankCounterDone:
rts rts
JoypadHandler: JoypadHandler:
; $4218: Joypad #1 status [JOY1L] ; $4218: Joypad #1 status [JOY1L]
; Format: AXLR0000 ; Format: AXLR0000
; $4219: Joypad #1 status [JOY1H] ; $4219: Joypad #1 status [JOY1H]
; Format: BYsSudlr (s=select, S=start, udlr = joypad) ; Format: BYsSudlr (s=select, S=start, udlr = joypad)
; Joypad #2 status is $421A [JOY2L] and $421B [JOY2H]. ; Joypad #2 status would be $421A [JOY2L] and $421B [JOY2H].
jsr JoypadDebug ; DEBUG jsr JoypadDebug ; DEBUG
; TODO(mcmillen): read joypad from local memory instead of registers? ; TODO(mcmillen): read joypad from local memory instead of registers?
@ -153,6 +155,7 @@ JoypadDone:
rts rts
JoypadDebug: JoypadDebug:
; Load joypad registers into RAM for easier inspection. ; Load joypad registers into RAM for easier inspection.
lda $4218 lda $4218
@ -166,42 +169,35 @@ JoypadDebug:
rts rts
SetBackgroundColor:
; $20 $21 $22 are R, G, B, as appropriate.
; The color format is 15-bit: [0bbbbbgg][gggrrrrr]
; We store the low-order byte first.
lda $20 ; RED
sta $00
lda $22 ; BLUE
asl
asl
sta $01
lda $21 ; GREEN.
; First store the high-order bits of green into $01.
lsr
lsr
lsr
ora $01
pha ; push 1
; Now store the low-order bits into $00.
lda $21
and #%00000111
asl
asl
asl
asl
asl
ora $00
pha ; push 2
; Now store the two values from the stack into the palette. SetBackgroundColor:
pla ; pull 2 ; $20 $21 $22 are (R, G, B), each ranging from [0-31].
; The palette color format is 15-bit: [0bbbbbgg][gggrrrrr]
; Compute and store the low-order byte.
lda $21 ; Green.
.rept 5
asl
.endr
ora $20 ; Red.
sta $2122 sta $2122
pla ; pull 1
; Compute and store the high-order byte.
lda $22 ; Blue.
.rept 2
asl
.endr
sta $00
lda $21 ; Green.
.rept 3
lsr
.endr
ora $00
sta $2122 sta $2122
rts rts
FillScratch: FillScratch:
lda #$42 ; B lda #$42 ; B
ldx #0 ldx #0
@ -213,4 +209,3 @@ FillScratchLoop:
rts rts
.ENDS .ENDS