1 ; Common macros. 2 3 ; Copyright (C) 2014, 2015 Paul Boddie <paul@boddie.org.uk> 4 5 ; This program is free software; you can redistribute it and/or modify it under 6 ; the terms of the GNU General Public License as published by the Free Software 7 ; Foundation; either version 3 of the License, or (at your option) any later 8 ; version. 9 10 ; This program is distributed in the hope that it will be useful, but WITHOUT 11 ; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 12 ; FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 13 ; details. 14 15 ; You should have received a copy of the GNU General Public License along with 16 ; this program. If not, see <http://www.gnu.org/licenses/>. 17 18 .alias oswrch $ffee 19 20 .macro mode 21 lda #22 22 jsr oswrch 23 lda #_1 24 jsr oswrch 25 .macend 26 27 ; store values in locations 28 ; 29 ; _1: valueW 30 ; _2: {_, _} -> {valueL, valueH} 31 ; 32 ; affects: A 33 34 .macro store16 35 lda #<_1 36 sta _2 37 lda #>_1 38 sta _2+1 39 .macend 40 41 ; copy word between locations 42 ; 43 ; _1: source 44 ; _2: target 45 ; 46 ; affects: A 47 48 .macro mov16 49 lda _1 50 sta _2 51 lda _1+1 52 sta _2+1 53 .macend 54 55 ; copy word from location to indirect location 56 ; 57 ; _1: source 58 ; _2: target reference 59 ; 60 ; affects: A, Y 61 62 .macro mov16_to_ref 63 ldy #0 64 lda _1 65 sta (_2), y 66 iny 67 lda _1+1 68 sta (_2), y 69 .macend 70 71 ; copy word from indirect location to location 72 ; 73 ; _1: source reference 74 ; _2: target 75 ; 76 ; affects: A, Y 77 78 .macro mov16_from_ref 79 ldy #0 80 lda (_1), y 81 sta _2 82 iny 83 lda (_1), y 84 sta _2+1 85 .macend 86 87 ; copy byte between indirect locations 88 ; 89 ; _1: source reference 90 ; _2: target reference 91 ; 92 ; Y: offset 93 ; affects: A 94 95 .macro mov8_refs 96 lda (_1), y 97 sta (_2), y 98 .macend 99 100 ; copy word from location to stack 101 ; 102 ; _1: source 103 ; 104 ; affects: A 105 106 .macro push16 107 lda _1 108 pha 109 lda _1+1 110 pha 111 .macend 112 113 ; copy word to location from stack 114 ; 115 ; _1: source 116 ; 117 ; affects: A 118 119 .macro pull16 120 pla 121 sta _1+1 122 pla 123 sta _1 124 .macend 125 126 ; add word to locations 127 ; 128 ; _1: valueW 129 ; _2: {valueL, valueH} -> {valueL', valueH'} 130 ; 131 ; affects: A, C 132 133 .macro add16 134 clc 135 lda _2 136 adc #<_1 137 sta _2 138 lda _2+1 139 adc #>_1 140 sta _2+1 141 .macend 142 143 ; subtract word from locations 144 ; 145 ; _1: valueW 146 ; _2: {valueL, valueH} -> {valueL', valueH'} 147 ; 148 ; affects: A, C 149 150 .macro sub16 151 sec 152 lda _2 153 sbc #<_1 154 sta _2 155 lda _2+1 156 sbc #>_1 157 sta _2+1 158 .macend 159 160 ; negate accumulator 161 ; 162 ; affects: A, C 163 164 .macro negate 165 clc 166 eor #$ff 167 adc #1 168 .macend 169 170 ; push A onto the "user space" stack, adding an empty byte for compatibility 171 ; with PC storage 172 ; 173 ; affects: USER (gains empty, A), TEMP 174 175 .macro pushA 176 sei 177 php ; F -> stack 178 pha ; A -> stack 179 sty TEMP 180 ldy #0 181 sta (USER), y 182 .invoke sub16 2, USER 183 ldy TEMP 184 pla ; stack -> A 185 plp ; stack -> F 186 cli 187 .macend 188 189 ; pull A from the "user space" stack 190 ; 191 ; affects: A, USER (loses empty, A), TEMP 192 193 .macro pullA 194 sei 195 php ; F -> stack 196 sty TEMP 197 .invoke add16 2, USER 198 ldy #0 199 lda (USER), y 200 ldy TEMP 201 plp ; stack -> F 202 cli 203 .macend 204 205 ; push a return address onto the "user space" stack and jump to a subroutine 206 ; 207 ; _1: subroutine address 208 ; _2: return address 209 ; 210 ; affects: USER (gains LSB, MSB), TEMP 211 212 .macro call 213 sei 214 php ; F -> stack 215 pha ; A -> stack 216 sty TEMP 217 ldy #0 218 lda #<_2 219 sta (USER), y 220 iny 221 lda #>_2 222 sta (USER), y 223 .invoke sub16 2, USER 224 ldy TEMP 225 pla ; stack -> A 226 plp ; stack -> F 227 cli 228 jmp _1 229 .macend 230 231 ; pull an address from the "user space" stack and return to it 232 ; 233 ; affects: USER (loses LSB, MSB), ABSTEMP 234 235 .macro return 236 sei 237 php ; F -> stack 238 pha ; A -> stack 239 .invoke add16 2, USER 240 .invoke mov16_from_ref USER, ABSTEMP 241 pla ; stack -> A 242 plp ; stack -> F 243 cli 244 jmp (ABSTEMP) 245 .macend 246 247 ; vim: tabstop=4 expandtab shiftwidth=4