It is currently Sat Dec 16, 2017 6:17 am

All times are UTC - 7 hours





Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: Bank Switching loop
PostPosted: Tue Feb 24, 2015 6:26 am 
Offline

Joined: Thu Jan 29, 2015 10:28 am
Posts: 7
Location: Moscow, Russian Federation
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.


Top
 Profile  
 
 Post subject: Re: Bank Switching loop
PostPosted: Tue Feb 24, 2015 6:57 am 
Offline
User avatar

Joined: Sat Feb 12, 2005 9:43 pm
Posts: 10165
Location: Rio de Janeiro - Brazil
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


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 7 hours


Who is online

Users browsing this forum: No registered users and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group