Jump to content
IGNORED

Bankswitch per jmp/jsr/rts


SvOlli

Recommended Posts

To save space you could use BRK instead of JSR. Also the target address could be stored in the two bytes following the BRK. You just have to slightly adjust the return address then.

 

I now share the code for the JSR/JMP and the RTS. I was pondering around with any other options like BRK, and adding the address after the JSR/BRK, but the code always got to complex and long, and even though not finished I needed to make dozens of call to get the benefit again.

 

Right now the math for "my" bankswitching is: 17 bytes per bank for the JSR/JMP/RTS code (for F4), 7 (+4) bytes per JSR/JMP and 3 (+2) per RTS, A and X registers are destroyed, Y is unchanged. Feels not too bad for a really generic approach.

 

Best of all it blends beautifully in with another part of my code, all demoparts are called like this in 4k:

 

   ldx demopartno
   lda demopartshi,x
   pha
   lda demopartslo,x
   pha
   rts

This can be changed to:

 

   ldx demopartno
   lda demopartshi,x
   pha
   lda demopartslo,x
   jmp bankrtscode-1

Costing just one byte extra.

Link to comment
Share on other sites

I have had a go at implementing bank switching and this is what I came up with (currently only in F8). I have the following code at the bottom of my banks.

MAC F8_BANK_HOTSPOT

.setbank SET F8_SET_BANK1 ; switch bank 1
	IF F8_BANK_ORIGIN < $F000
.setbank SET F8_SET_BANK2 ; switch bank 2
	ENDIF
	
	ORG F8_BANK_ORIGIN + $FE7
	RORG $FFE7
	
	sta .setbank
	jmp ({1})
	pla				
	tsx 			
	dec $01,x		
	dec $01,x		
	sta .setbank
	rts	
	
	ENDM

I have setup the break vector to point at this routine.

 

I use the following code to perform the bank switch jsr

	MAC JSR_F8_BANKSWITCH ; address, pointer

	lda #<{1}
	sta {2}    
	lda #>{1}
	sta {2}+1
	brk

	ENDM

This seems fairly easy to extend to F6 too i will just need to load x with the appropriate bank index and do a sta $1FF6,x. I could work out x at compilation time too by looking at the target address.

Link to comment
Share on other sites

I was working on something similar as well, but the two "dec $01,x" won't work, if the return address is pushed as $xx00 or $xx01, because decrementing the hibyte on underflow is missing. Catching that corner-case blows up the code, and so I kept sticking with my approach.

Link to comment
Share on other sites

I think rather than coding around the high byte issue it would be nice to add a compilation check to ensure it won't assemble if the high byte is < 2.

	MAC JSR_F8_BANKSWITCH

	IF <. < $02
		echo "jsr bankswitch location is ", ., " high byte must be > $01" 
		ERR
	ENDIF

	lda #<{1}
	sta {2}    
	lda #>{1}
	sta {2}+1
	brk

	ENDM
Link to comment
Share on other sites

  • 4 weeks later...

For the sake of completeness, while digging around in this forum I found this set of bankswitching macros, by Robert M:

 

It's not optimal speed- or sizewise, but it's *very* easy to use, provides nice source readability and has some nice compile-time convenience macros for debugging. As long as I won't need to optimize my code for every byte to spare (unlikely when you do bank-switching in the first place), I go with this.
Link to comment
Share on other sites

 

As long as I won't need to optimize my code for every byte to spare (unlikely when you do bank-switching in the first place), I go with this.

 

 

 

Not true - I, along with a number of others, spent a lot of time optimizing code for Space Rocks and Stay Frosty 2. I used the freed up space to add new features, levels, music, etc.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...