So I'm going through the Nerdy Nights tutorials (the version for ASM6) and I was trying to move parts of my code into subroutines to make things cleaner, easier to read, and a little more object oriented. For some reason the subroutines don't execute, but if I manually put my code where the JSR calls are made it works just fine. It compiles just fine either way, and I can't figure out what I'm doing wrong. Any help would be appreciated.
Here's the main chunk of the program:
Here's the main chunk of the program:
Code:
;********** Header ****************************************
byte "NES",$1a ;"NES" plus a terminator
byte $01 ;1x16Kb Program ROM block ($C000)
byte $01 ;1x8Kb Character ROM block
byte $00 ;don't care
byte $00 ;don't care
dsb 8 ;8 bytes padding
;********** Program Code ****************************************
.org $C000
vblankWait: ;vblankWait - waits for Vblank (NMI)
BIT $2002
BPL vblankWait
RTS
clearMem: ;Clears memory to get ready to start game
LDA #$00
STA $0000, x
STA $0100, x
STA $0200, x
STA $0400, x
STA $0500, x
STA $0600, x
STA $0700, x
LDA #$FE
STA $0300, x
INX
BNE clearMem ;Branch if not equal to zero
RTS
loadPalettes: ;loads Palette data into $3F00
LDA $2002 ;read PPU status to reset the high/low latch
LDA #$3F
STA $2006 ;write high byte of $3F00 address
LDA #$00
STA $2006 ;write low byte of $3F00 address
LDX #$00
loadPalettesLoop:
LDA palette, x ;load data from address (palette + x)
STA $2007
INX
CPX #$20 ;Compare X to 32
BNE loadPalettesLoop
RTS
loadSprites:
LDX #$00 ;start at 0
loadSpritesLoop:
LDA player1_sprite, x ;load data from address (player1_sprite + x)
STA $0200, x ;store into RAM address ($0200 + x)
INX
CPX #$20 ;Compare X to 32
BNE loadSpritesLoop ;Branch if not equal to 0
RTS
RESET: ;Reset Vector - Program execution starts here on Reset/PowerOn
SEI ;Disable IRQs
CLD ;disable Decimal mode
LDX #$40 ;load 0x40 (64) into X
STX $4017 ;store X into $4017, disables APU frame IRQ
LDX #$FF ;load 0xFF (255) into X
TXS ;transfer X to Stack, sets up stack
INX ;increment X, X now = 0
STX $2000 ;disable NMI
STX $2001 ;disable rendering
STX $4010 ;disable DMC IRQs
JSR vblankWait
JSR clearMem
JSR vblankWait
JSR loadPalettes
JSR loadSprites
;PPU CONTROL REGISTER SETUP
;VPHB SINN - Bits to write to PPU Control register ($2000)
;V = Vblank NMI enable
;P = PPU Master/Slave select
;S = Sprite Size. 0 = 8x8, 1 = 8x16
;B = Background pattern table address. 0 = $0000, 1 = $1000
;S = Sprite pattern table address for 8x8 sprites (ignored in 8x16 mode). 0 = $0000, 1 = $1000
;I = Pattern table increment. 0 = Add 1 (going across), 1 = add 32 (going down)
;NN = Base nametable address. 0 = $2000, 1 = $2400, 2 = $2800, 3 = $2C00
LDA #%10000000 ;enable NMI
STA $2000 ;Write to PPU Control register
;PPU MASK REGISTER SETUP
;BGRs bMmG
;B = emphasize blue
;G = emphasize green
;R = emphasize red
;s = show sprites
;b = show background
;M = show sprites in leftmost margin (8 pixels)
;m = show sprites in rightmost margin (8 pixels)
;G = grayscale (0 = normal colour, 1 = grayscale)
LDA #%00010000 ;enable sprites
STA $2001 ;Write to PPU Mask register
MAIN: ;MAIN loop
JMP MAIN
NMI: ;NMI - Vblank interrupt
LDA #$00
STA $2003 ;set the low byte of the RAM address
LDA #$02
STA $4014 ;set the high byte of the RAM address and start the transfer
RTI ;Return from Interrupt
byte "NES",$1a ;"NES" plus a terminator
byte $01 ;1x16Kb Program ROM block ($C000)
byte $01 ;1x8Kb Character ROM block
byte $00 ;don't care
byte $00 ;don't care
dsb 8 ;8 bytes padding
;********** Program Code ****************************************
.org $C000
vblankWait: ;vblankWait - waits for Vblank (NMI)
BIT $2002
BPL vblankWait
RTS
clearMem: ;Clears memory to get ready to start game
LDA #$00
STA $0000, x
STA $0100, x
STA $0200, x
STA $0400, x
STA $0500, x
STA $0600, x
STA $0700, x
LDA #$FE
STA $0300, x
INX
BNE clearMem ;Branch if not equal to zero
RTS
loadPalettes: ;loads Palette data into $3F00
LDA $2002 ;read PPU status to reset the high/low latch
LDA #$3F
STA $2006 ;write high byte of $3F00 address
LDA #$00
STA $2006 ;write low byte of $3F00 address
LDX #$00
loadPalettesLoop:
LDA palette, x ;load data from address (palette + x)
STA $2007
INX
CPX #$20 ;Compare X to 32
BNE loadPalettesLoop
RTS
loadSprites:
LDX #$00 ;start at 0
loadSpritesLoop:
LDA player1_sprite, x ;load data from address (player1_sprite + x)
STA $0200, x ;store into RAM address ($0200 + x)
INX
CPX #$20 ;Compare X to 32
BNE loadSpritesLoop ;Branch if not equal to 0
RTS
RESET: ;Reset Vector - Program execution starts here on Reset/PowerOn
SEI ;Disable IRQs
CLD ;disable Decimal mode
LDX #$40 ;load 0x40 (64) into X
STX $4017 ;store X into $4017, disables APU frame IRQ
LDX #$FF ;load 0xFF (255) into X
TXS ;transfer X to Stack, sets up stack
INX ;increment X, X now = 0
STX $2000 ;disable NMI
STX $2001 ;disable rendering
STX $4010 ;disable DMC IRQs
JSR vblankWait
JSR clearMem
JSR vblankWait
JSR loadPalettes
JSR loadSprites
;PPU CONTROL REGISTER SETUP
;VPHB SINN - Bits to write to PPU Control register ($2000)
;V = Vblank NMI enable
;P = PPU Master/Slave select
;S = Sprite Size. 0 = 8x8, 1 = 8x16
;B = Background pattern table address. 0 = $0000, 1 = $1000
;S = Sprite pattern table address for 8x8 sprites (ignored in 8x16 mode). 0 = $0000, 1 = $1000
;I = Pattern table increment. 0 = Add 1 (going across), 1 = add 32 (going down)
;NN = Base nametable address. 0 = $2000, 1 = $2400, 2 = $2800, 3 = $2C00
LDA #%10000000 ;enable NMI
STA $2000 ;Write to PPU Control register
;PPU MASK REGISTER SETUP
;BGRs bMmG
;B = emphasize blue
;G = emphasize green
;R = emphasize red
;s = show sprites
;b = show background
;M = show sprites in leftmost margin (8 pixels)
;m = show sprites in rightmost margin (8 pixels)
;G = grayscale (0 = normal colour, 1 = grayscale)
LDA #%00010000 ;enable sprites
STA $2001 ;Write to PPU Mask register
MAIN: ;MAIN loop
JMP MAIN
NMI: ;NMI - Vblank interrupt
LDA #$00
STA $2003 ;set the low byte of the RAM address
LDA #$02
STA $4014 ;set the high byte of the RAM address and start the transfer
RTI ;Return from Interrupt