Originally posted by: tree of might
Originally posted by: KHAN Games
Originally posted by: tree of might
Sorry, guys, I just don't get this. At the end of the bankswitch subroutine the value in X gets stored in the bankvalues area, then what? What part of the code actually causes the bank switching to happen? Sorry if this is a dumb question.
It's storing it into the $8000 address, right?
;;code
STA $8000 ;;new bank to use
RTS
What section of the code does it write to $8000? I can't seem to find it.
THIS IS KEY
"The actual switch is done by writing the desired bank number anywhere in the $8000-FFFF memory range."
Bus Conflicts
"When you start running your code on real hardware there is one catch to worry about. For basic mappers, the PRG ROM does not care if it receives a read or a write command. It will respond to both like a read by putting the data on the data bus. This is a problem for bank switching, where the CPU is also trying to put data on the data bus at the same time. They electrically fit in a "bus conflict". The CPU could win, giving you the right value. Or the ROM could win, giving you the wrong value. This is solved by having the ROM and CPU put the same value on the data bus, so there is no conflict. First a table of bank numbers is made, and the value from that table is written to do the bank switch."
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
.bank 0 ;;Code is stored starting at $C000
.org $C000
... code ...
LDA #$01 ;;Load A with $01 (For bank 1)
JSR Bankswitch ;;jump to bank switching code
... code ...
Bankswitch:
TAX ;;copy $01 into X
STA Bankvalues, X ;;Write $01 to Bankvalues,$01 (Which is stored somewhere between $C000-$DFFF). This is done to avoid the Bus Conflict in the paragraph above
RTS
Bankvalues:
.db $00, $01, $02, $03 ;;bank numbers
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Since you write to Bankvalues, that table is stored in $8000-FFFF memory range. You can write to ANY number in that range, so you write the value of X into Bankvalues.
Hope that helps clarify. This is the way I understand it.