Bank Switching loop

Discuss technical or other issues relating to programming the Nintendo Entertainment System, Famicom, or compatible systems.

Moderator: Moderators

Post Reply
Bionja
Posts: 7
Joined: Thu Jan 29, 2015 10:28 am
Location: Moscow, Russian Federation

Bank Switching loop

Post by Bionja » Tue Feb 24, 2015 6:26 am

A little newbie question adout changing the banks: I get how to change it pressing buttons, but I confused making a loop for it. I mean, I don't know how to loop switching like first goes Bank 1 then Bank 2, next Bank 3 and all over again from Bank 1 without any pressing buttons.

Code: Select all


ReadStart:
  lda $4016
  and #%00000001
  beq ReadStartDone
  lda #$01
  jsr Bankswitch
ReadStartDone:

Bankswitch:
  tax
  sta Bankvalues, x
  rts 
  
Bankvalues:

  .db $00,$01,$02,$03 ; Numbers of banks 
The last thing which I reached is where I press the start button, I change bank number by jumping to subroutine.

User avatar
tokumaru
Posts: 11864
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: Bank Switching loop

Post by tokumaru » Tue Feb 24, 2015 6:57 am

You mean you want to go 0, 1, 2, 3, 0, 1, 2, 3, ... over and over? All you have to do is put the bank index in a variable and increment that variable every time you want to change banks forcing it back to 0 when it overflows. Something like this:

Code: Select all

	clc
	lda BankIndex
	adc #$01
	cmp #$04
	bcc SaveIndex
	lda #$00
SaveIndex:
	sta BankIndex
There's a shortcut you can take whenever you're working with a power of 2 range. Look at how numbers look like in binary:

00 = %00000000
01 = %00000001
02 = %00000010
03 = %00000011
04 = %00000100
05 = %00000101
06 = %00000110
07 = %00000111
08 = %00001000
09 = %00001001
10 = %00001010
11 = %00001011
12 = %00001100
13 = %00001101
14 = %00001110
15 = %00001111
(...)

Notice how the last 2 digits repeat the sequence 00, 01, 10, 11 over and over, which are the numbers you want. This means you can get the values you need by simply masking out the upper bits:

Code: Select all

	clc
	lda BankIndex
	adc #$01
	and #%00000011
	sta BankIndex

Post Reply