Page 1 of 1

Basic Bank Switch question

Posted: Tue Sep 15, 2020 9:15 pm
by casprog
Hi all, I've been playing around with UNROM, having 8 16KB banks.

I've defined the following in the first 3 banks:

Code: Select all

;----------------------------------------------------------------
; program bank 0
;----------------------------------------------------------------

	.base $8000

nametable_1:
	.incbin "nt1.nam"

	.org $c000

;----------------------------------------------------------------
; program bank 1
;----------------------------------------------------------------

	.base $8000

nametable_2:
		.incbin "nt2.nam"

	.org $c000

;----------------------------------------------------------------
; program bank 2
;----------------------------------------------------------------

	.base $8000

nametable_3:
	.incbin "nt3.nam"

	.org $c000

To test this, before I turn on the ppu, I switch to a bank and then load a nametable

Code: Select all

        LDY #$02  ; bank 3
	JSR switch_bank_y

	; load nametable
	LDA #<nametable_1
	STA temp_ptr + 0
	LDA #>nametable_1
	STA temp_ptr + 1

	LDA #$20
	STA $2006
	LDA #$00
	STA $2006

	LDY #$00
	LDX #$04
-:
	LDA (temp_ptr), y
	STA $2007
	INY
	BNE -
	INC temp_ptr + 1
	DEX
	BNE -
If I switch to bank 0, 1, or 2, I see the expected nametable. However notice how I'm always reading from memory pointed to by label 'nametable_1'? How come it's loading the correct nametable, even though other banks have different labels? Is it because each bank starts with a nametable, and the CPU resolves to the same address regardless?

Re: Basic Bank Switch question

Posted: Tue Sep 15, 2020 9:43 pm
by tokumaru
If the data is in the same position within each bank, all the labels will resolve to the same address, and the assembled binary will be the same regardless of which one you use.

Assembly is not like high-level languages, where the names of variables and functions are strongly tied to the objects they refer to... In assembly, labels are just nicknames you can give to memory addresses so you don't have to memorize where everything is in memory. When you define a label in assembly, the assembler only assigns the current PC value to that name, but it doesn't create any link whatsoever between the label and whatever data you're placing after it. Neither the assembler nor the NES knows that those things are connected, only you do.

Re: Basic Bank Switch question

Posted: Tue Sep 15, 2020 9:49 pm
by casprog
Makes perfect sense, thank you tokumaru!