Saturday, September 1, 2012

6502 techniques I like/use


I like to use some of these ideas, or variations on them:

Flags

It's important to be able to set flags. If you want to be quick about it and avoid using any CPU registers you can use:
inc flag          ;set flag to 1
;later in code:
dec flag          ;set flag to 0
This is all great until you run into some logic where you aren't sure if the flag was set or not and you want to clear it. Instead, I use this:
sec
ror flag          ;set flag bit7 to 1
; later in code:
lsr flag          ;set flag bit7 to 0
In this case you are achieving the same effect but clearing the flag clears the flag everytime as long as you only ever use bit7 to test the flag:
bit flag           ;test the flag
bmi label          ;branch if set
So I just implement some ca65 macros to make it easier to utilize:
.macro setflag flag
   sec
   ror flag
.endmacro

.macro clrflag flag
   lsr flag
.endmacro

.macro bfs label
   bmi label
.endmacro

.macro bfc label
   bpl label
.endmacro
A slightly faster way to clear the flag is do a lda #0 / sta flag but it does take up a bit more code space and clears the accumulator. More to come...

No comments:

Post a Comment