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 .macro sub16 149 sec 150 lda _2 151 sbc #<_1 152 sta _2 153 lda _2+1 154 sbc #>_1 155 sta _2+1 156 .macend 157 158 ; negate accumulator 159 ; 160 ; affects: A, C 161 162 .macro negate 163 clc 164 eor #$ff 165 adc #1 166 .macend 167 168 ; push PC onto a "user space" stack and jump to subroutine 169 ; 170 ; _1: target 171 172 .macro call 173 sei ; prevent CPU stack disturbance 174 jsr copy_to_user_stack 175 cli ; permit CPU stack access again 176 jmp _1 177 nop 178 .macend 179 180 ; pull PC from a "user space" stack and return from subroutine 181 182 .macro return 183 jmp copy_from_user_stack 184 .macend 185 186 ; push A onto the "user space" stack 187 ; 188 ; affects: USER (gains A), TEMP 189 190 .macro pushA 191 sei 192 pha 193 sty TEMP 194 ldy #0 195 sta (USER), y 196 .invoke sub16 1, USER 197 ldy TEMP 198 pla 199 cli 200 .macend 201 202 ; pull A from the "user space" stack 203 ; 204 ; affects: A, USER (loses A), TEMP 205 206 .macro pullA 207 sei 208 sty TEMP 209 .invoke add16 1, USER 210 ldy #0 211 lda (USER), y 212 ldy TEMP 213 cli 214 .macend 215 216 ; vim: tabstop=4 expandtab shiftwidth=4