This page is a mirror of Tepples' nesdev forum mirror (URL TBD).
Last updated on Oct-18-2019 Download

Bank Switching loop

Bank Switching loop
by on (#141947)
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:

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.
Re: Bank Switching loop
by on (#141950)
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:
   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:
   clc
   lda BankIndex
   adc #$01
   and #%00000011
   sta BankIndex