-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.m65
153 lines (135 loc) · 2.65 KB
/
utils.m65
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
; Utilities & Macros
; Macro to take location (addr) and put at (pointer)
; Only uses the A register
.macro staddr ; addr,pointer
.IF %0<>2
.ERROR "staddr needs 2 params"
.ENDIF
lda #<%1
sta %2
lda #>%1
sta %2+1
.endm
; Macro to copy a 16-bit word from one location to another
.macro mov16 ; to,from
.IF %0<>2
.ERROR "mov16 needs 2 16 bit params"
.ENDIF
lda %1
sta %2
lda %1+1
sta %2+1
.endm
; Macro to multiply A by a constant value -- Give # shifts, # adds
.macro constmult8 ; k
.if %0<>2
.error "mult8 needs #shifts, #adds"
.endif
sta MULTREG
.rept %1
asl
.endr
.rept %2
adc MULTREG
.endr
.endm
; macro to increment a 16-bit value in memory (LSB, MSB)
.macro inc16 ; addr
.if %0<>1
.error "inc16 needs addr"
.endif
clc
inc %1
bcc @incdone
inc %1+1
@incdone
nop
.endm
; macro to decrement a 16-bit value in memory (LSB, MSB)
; Uses A
.macro dec16 ; addr
.if %0<>1
.error "dec16 needs addr"
.endif
lda %1
bne @sbdecdone ; Branch past msb decrement if init lsb not zero
dec %1+1 ; Decrement the MSB
@msbdecdone
dec %1 ; Decrement LSB no matter what
.endm
; macro to subtract (w/carry) 8-bit val from a 16-bit value in memory (LSB, MSB)
; Uses A; ends with lsb stored in A
.macro sbc168 ; addr,val
.if %0<>2
.error "sbc16 needs addr,val"
.endif
lda %1
; cmp #%2
sec
sbc #%2
bcs @msbborrowdone ; Branch past msb decrement if init lsb not zero
dec %1+1 ; Decrement the MSB
@msbborrowdone
;lda %1 ; subtract from LSB no matter what
sta %1
.endm
; macro to add (w/carry) 8-bit val to a 16-bit value in memory (LSB, MSB)
; Uses A; ends with lsb stored in A
.macro adc168 ; addr,val
.if %0<>2
.error "adc16 needs addr,val"
.endif
lda %1
; cmp #%2
clc
adc #%2
bcc @msbborrowdone ; Branch past msb decrement if init lsb not zero
inc %1+1 ; Decrement the MSB
@msbborrowdone
;lda %1 ; subtract from LSB no matter what
sta %1
.endm
; Macro to shift left or right (if negative) n times
.macro aslconst
.rept %1
asl A
.endr
.endm
.macro asrconst
.rept %1
and #~11111110 ; turn rotate into shift
clc
ror A
.endr
.endm
; BEQ but a jump, like for long range
.local
.macro jmpeq
bne @nojmp
jmp %1
@nojmp
.endm
.macro phx
txa
pha
.endm
.macro phy
tya
pha
.endm
.macro plx
pla
tax
.endm
.macro ply
pla
tay
.endm
; LDA #%2 if A == #%1
.local
.macro ldaifeq
cmp #%1
bne @donelie
lda #%2
@donelie
.endm