Friday, October 12, 2012

CA65 Highlevel Macro Updated

I've added a cleaner method for adding functions to the IF/WHILE statements for better readability.
The idea is for a user of the macros to write a simple macro that results in some flag being set (in the CPU) and then set that flag so the conditional check knows what code to generate. You can achieve some nice things with it, and as an example and for my own use I've written both a signed and unsigned comparison function.

First, how to make a function - a simple example:
     

.macro reg_is_negative reg
  .if .xmatch(reg,a)
    pha
    pla
 .elseif .xmatch(reg,x)
    txa
 .elseif .xmatch(reg,y)
    tya
 .else
   .error "no register"
 .endif
  set_flag_test N set
.endmacro
You can do anything you want like a normal macro, just add set_flag_test followed by the flag you want to test. You can still use all the .defines as well form the first post on this: http://mynesdev.blogspot.ca/2012/09/ca65-highlevel-macros.html So in this example you could:
if reg_is_negative a
;code
endif
The if macro "knows" to check for the status of the N flag, where N set means TRUE.

Macros comp and scomp

 Both of these macros accept the same values and can evaluate anything on either side of one comparison operator except for a comparison of two registers (they can only evaluate one comparison, no and or or). If using a greater or less compare with a low byte or high byte operator, you must enclose the operator in brackets: Use would be like this:
if scomp a >= #(<-29)
; code
endif
There is not much more to it, operators supported are the standard <, >, <=, >=, =, <>. You can also use not at the beginning as so:
if not scomp a >= #(<-29)
; code
endif
Another example..
if comp my_var1 >= #29
; code
endif
Full example code here: https://app.dumptruck.goldenfrog.com/p/b9UI5-8_ck

No comments:

Post a Comment