The ca65 Users Guide lists a pseudo-variable .CPU for use in assertions about the CPU type. But it doesn't list anything for the state of a given CPU type, namely whether a 65816 is in 8-bit or 16-bit mode for the accumulator or index registers. So I wrote some macros to test these. They're useful if you're using smart mode (which tracks 65816 REP and SEP instructions to know which instruction size to generate for immediate operands. It requires the linker script to define a segment called "DEVNULL" that is not written to the output file.
Am I doing it wrong?
Am I doing it wrong?
Code:
.macro assert_regbits_816 ldr, expected_size
.pushseg
.segment "DEVNULL"
.local Code
.proc Code
ldr #0
.endproc
.popseg
.assert .sizeof(Code) = expected_size, error, .sprintf("expected %d-bit mode for %s but got %d-bit", 8 * (expected_size - 1), .string(ldr), 8 * (.sizeof(Code) - 1))
.endmacro
.macro assert_a16
assert_regbits_816 lda, 3
.endmacro
.macro assert_i16
assert_regbits_816 ldx, 3
.endmacro
.macro assert_a8
assert_regbits_816 lda, 2
.endmacro
.macro assert_i8
assert_regbits_816 ldx, 2
.endmacro
; Test cases ;;;;;;;;;;;;;;;;;;;
.out "The following should all succeed"
.p02
assert_a8
assert_i8
.p816
.a16
assert_a16
.a8
assert_a8
.i16
assert_i16
.i8
assert_i8
.out "Six errors should follow"
.a16
assert_a8
.i16
assert_i8
.a8
assert_a16
.i8
assert_i16
.p02
assert_i16
assert_a16
.pushseg
.segment "DEVNULL"
.local Code
.proc Code
ldr #0
.endproc
.popseg
.assert .sizeof(Code) = expected_size, error, .sprintf("expected %d-bit mode for %s but got %d-bit", 8 * (expected_size - 1), .string(ldr), 8 * (.sizeof(Code) - 1))
.endmacro
.macro assert_a16
assert_regbits_816 lda, 3
.endmacro
.macro assert_i16
assert_regbits_816 ldx, 3
.endmacro
.macro assert_a8
assert_regbits_816 lda, 2
.endmacro
.macro assert_i8
assert_regbits_816 ldx, 2
.endmacro
; Test cases ;;;;;;;;;;;;;;;;;;;
.out "The following should all succeed"
.p02
assert_a8
assert_i8
.p816
.a16
assert_a16
.a8
assert_a8
.i16
assert_i16
.i8
assert_i8
.out "Six errors should follow"
.a16
assert_a8
.i16
assert_i8
.a8
assert_a16
.i8
assert_i16
.p02
assert_i16
assert_a16