a2d/macros.inc

244 lines
6.5 KiB
PHP
Raw Normal View History

2018-02-06 03:01:17 +00:00
;;; ==================================================
;;; Generic Macros
;;; ==================================================
2018-02-06 05:49:39 +00:00
;;; ==================================================
;;; Pad with zeros to the given address
.macro PAD_TO addr
.res addr - *, 0
.endmacro
2018-02-06 03:01:17 +00:00
;;; ==================================================
;;; Calls with one parameter (address in A,X)
.macro addr_call target, addr
lda #<addr
ldx #>addr
jsr target
.endmacro
.macro addr_jump target, addr
lda #<addr
ldx #>addr
jmp target
.endmacro
;;; ==================================================
;;; Calls with two paramters (call # in y, address in A,X)
;;; (various output orders to match original binary)
.macro axy_call target, yparam, addr
lda #<addr
ldx #>addr
ldy #yparam
jsr target
.endmacro
.macro yax_call target, yparam, addr
ldy #yparam
lda #<addr
ldx #>addr
jsr target
.endmacro
.macro yxa_call target, yparam, addr
ldy #yparam
ldx #>addr
lda #<addr
jsr target
.endmacro
.macro yxa_jump target, yparam, addr
ldy #yparam
ldx #>addr
lda #<addr
jmp target
.endmacro
;;; ==================================================
;;; 16-bit pseudo-ops
2018-02-06 23:36:39 +00:00
.define is_immediate(arg) (.match (.mid (0, 1, {arg}), #))
.define is_register(arg) (.match ({arg}, x) .or .match ({arg}, y))
;;; Load A,X
;;; ldax #$1234 ; immediate
;;; ldax $1234 ; absolute
2018-02-06 03:01:17 +00:00
.macro ldax arg
2018-02-06 23:36:39 +00:00
.if is_immediate {arg}
2018-02-06 03:01:17 +00:00
lda #<(.right (.tcount ({arg})-1, {arg}))
ldx #>(.right (.tcount ({arg})-1, {arg}))
.else
lda arg
ldx arg+1
.endif
.endmacro
2018-02-07 02:42:00 +00:00
;;; Load X,Y
;;; ldxy #$1234 ; immediate
;;; ldxy $1234 ; absolute
.macro ldxy arg
.if is_immediate {arg}
ldx #<(.right (.tcount ({arg})-1, {arg}))
ldy #>(.right (.tcount ({arg})-1, {arg}))
.else
ldx arg
ldy arg+1
.endif
.endmacro
2018-02-06 23:36:39 +00:00
;;; Store A,X
;;; stax $1234 ; absolute
2018-02-06 03:01:17 +00:00
.macro stax arg
sta arg
stx arg+1
.endmacro
2018-02-06 23:36:39 +00:00
;;; Add arg1 to arg2, store to arg3
;;; add16 $1111, $2222, $3333 ; absolute, absolute, absolute
;;; add16 $1111, #$2222, $3333 ; absolute, immediate, absolute
2018-02-06 03:01:17 +00:00
.macro add16 aa, bb, rr
2018-02-06 23:36:39 +00:00
.if is_immediate {bb}
2018-02-06 03:01:17 +00:00
lda aa
clc
adc #<(.right (.tcount ({bb})-1, {bb}))
sta rr
lda aa+1
adc #>(.right (.tcount ({bb})-1, {bb}))
sta rr+1
.else
lda aa
clc
adc bb
sta rr
lda aa+1
adc bb+1
sta rr+1
.endif
.endmacro
;;; Add arg1 (absolute) to arg2 (8-bit absolute), store to arg3
2018-02-06 23:36:39 +00:00
;;; add16_8 $1111, #$22, $3333 ; absolute, immediate, absolute
;;; add16_8 $1111, $22, $3333 ; absolute, absolute, absolute
2018-02-06 03:01:17 +00:00
.macro add16_8 aa, bb, rr
lda aa
clc
adc bb
sta rr
lda aa+1
adc #0
sta rr+1
.endmacro
2018-02-06 23:36:39 +00:00
;;; Subtract arg2 from arg1, store to arg3
;;; sub16 $1111, #$2222, $3333 ; absolute, immediate, absolute
;;; sub16 $1111, $2222, $3333 ; absolute, absolute, absolute
2018-02-06 03:01:17 +00:00
.macro sub16 aa, bb, rr
2018-02-06 23:36:39 +00:00
.if is_immediate {bb}
2018-02-06 03:01:17 +00:00
lda aa
sec
sbc #<(.right (.tcount ({bb})-1, {bb}))
sta rr
lda aa+1
sbc #>(.right (.tcount ({bb})-1, {bb}))
sta rr+1
.else
lda aa
sec
sbc bb
sta rr
lda aa+1
sbc bb+1
sta rr+1
.endif
.endmacro
2018-02-06 17:31:58 +00:00
2018-02-06 23:36:39 +00:00
;;; Subtract arg2 from arg1, store to arg3
;;; sub16_8 $1111, #$22, $3333 ; absolute, immediate, absolute
;;; sub16_8 $1111, $22, $3333 ; absolute, absolute, absolute
.macro sub16_8 aa, bb, rr
lda aa
sec
sbc bb
sta rr
lda aa+1
sbc #0
sta rr+1
.endmacro
2018-02-06 17:31:58 +00:00
;;; Copy 16-bit value
2018-02-06 23:36:39 +00:00
;;; copy16 #$1111, $2222 ; immediate, absolute
;;; copy16 $1111, $2222 ; absolute, absolute
;;; copy16 $1111,x, $2222 ; indirect load, absolute store
;;; copy16 $1111,y, $2222 ; indirect load, absolute store
;;; copy16 $1111, $2222,x ; absolute load, indirect store
;;; copy16 $1111, $2222,y ; absolute load, indirect store
2018-02-06 17:31:58 +00:00
.macro copy16 arg1, arg2, arg3
2018-02-06 23:36:39 +00:00
.if is_register {arg2}
2018-02-06 17:31:58 +00:00
;; indexed load variant (arg2 is x or y)
lda arg1,arg2
sta arg3
lda arg1+1,arg2
sta arg3+1
2018-02-06 23:36:39 +00:00
.elseif is_register {arg3}
2018-02-06 17:31:58 +00:00
;; indexed store variant (arg3 is x or y)
lda arg1
sta arg2,arg3
lda arg1+1
sta arg2+1,arg3
2018-02-06 23:36:39 +00:00
.elseif is_immediate {arg1}
2018-02-06 17:31:58 +00:00
;; immediate load variant (arg1 is #nnnn)
lda #<(.right (.tcount ({arg1})-1, {arg1}))
sta arg2
lda #>(.right (.tcount ({arg1})-1, {arg1}))
sta arg2+1
.else
lda arg1
sta arg2
lda arg1+1
sta arg2+1
.endif
.endmacro
2018-02-06 18:06:44 +00:00
2018-02-06 23:36:39 +00:00
;;; Compare 16-bit values
;;; cmp16 #$1111, $2222 ; immediate, absolute
;;; cmp16 $1111, #$2222 ; absolute, immediate
;;; cmp16 $1111, $2222 ; absolute, absolute
;;; cmp16 $1111,x, $2222 ; indirect, absolute
;;; cmp16 $1111,y, $2222 ; indirect, absolute
;;; cmp16 $1111, $2222,x ; absolute, indirect
;;; cmp16 $1111, $2222,y ; absolute indirect
2018-02-06 18:06:44 +00:00
.macro cmp16 arg1, arg2, arg3
2018-02-06 23:36:39 +00:00
.if is_register {arg2}
2018-02-06 18:06:44 +00:00
;; indexed variant (arg2 is x or y)
lda arg1,arg2
cmp arg3
lda arg1+1,arg2
sbc arg3+1
2018-02-06 23:36:39 +00:00
.elseif is_register {arg3}
2018-02-06 18:06:44 +00:00
;; indexed variant (arg3 is x or y)
lda arg1
cmp arg2,arg3
lda arg1+1
sbc arg2+1,arg3
2018-02-06 23:36:39 +00:00
.elseif is_immediate {arg1}
2018-02-06 18:06:44 +00:00
;; immediate variant (arg1 is #nnnn)
lda #<(.right (.tcount ({arg1})-1, {arg1}))
cmp arg2
lda #>(.right (.tcount ({arg1})-1, {arg1}))
sbc arg2+1
2018-02-06 23:36:39 +00:00
.elseif is_immediate {arg2}
2018-02-06 18:06:44 +00:00
;; immediate variant (arg2 is #nnnn)
lda arg1
cmp #<(.right (.tcount ({arg2})-1, {arg2}))
lda arg1+1
sbc #>(.right (.tcount ({arg2})-1, {arg2}))
.else
lda arg1
cmp arg2
lda arg1+1
sbc arg2+1
.endif
.endmacro