Code: Select all
SwitchBankCHR:
;this code will change all eight banks of CHR-ROM.
;input
;A = the bank you want to switch to. Currently, anything other than 0 or 1 is an invalid input. Call this with the following:
;lda bank_number
;sta temp
;jsr SwitchBankCHR
txa
pha
tya
pha
lda currentBankCHR ;backup the current bank.
sta prevBankCHR
lda temp ;take the input and make it the new current bank.
sta currentBankCHR
tax ;transfer the bank number to X to use it as an index.
ldy #0
;GOAL: Each bank follows this pattern: 00 01 02 03 04 05 06 07. The next bank is each of those numbers plus 8. For the number of banks we have we need to be able to quickly add or subtract 8 enough times to get to the desired bank.
lda chr_lookup,x ;this equals x times 8. We will use this as our base value.
sta temp
SwitchBankCHRloop:
lda temp ;if our input equals 1 this should be 8 on the first iteration of the loop. Next is 9, then 10, and so on.
sta softR0,y ;we initialized this to zero so next time it will be 1, then 2, and so on. These "soft" registers will be copied to the real ones during nmi
inc temp
iny
cpy #$08
bne SwitchBankCHRloop
pla
tay
pla
tax
rts
chr_lookup:
.db $00, $08, $10, $18, $20, $28, $30, $38