-
Notifications
You must be signed in to change notification settings - Fork 1
/
SW32VM.inc
411 lines (327 loc) · 13.4 KB
/
SW32VM.inc
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
; SW32VM Include file
sw32_regb = $69
R0 = $00 + sw32_regb
R1 = $01 + sw32_regb
R2 = $02 + sw32_regb
R3 = $03 + sw32_regb
R4 = $04 + sw32_regb
R5 = $05 + sw32_regb
R6 = $06 + sw32_regb
R7 = $07 + sw32_regb
R8 = $08 + sw32_regb
R9 = $09 + sw32_regb
R10 = $0A + sw32_regb
R11 = $0B + sw32_regb
R12 = $0C + sw32_regb
R13 = $0D + sw32_regb
R14 = $0E + sw32_regb
R15 = $0F + sw32_regb
Z = %00000010 ; Used for the SET/CLR Instructions
C = %00000100
N = %00001000
F4 = %00010000
F5 = %00100000
F6 = %01000000
F7 = %10000000
SR = R0 ; Used for the SET/CLR Instructions
ZERO = R0
; These are potentially used for a C Compiler, leave them commented for now
;SP = R1
;FP = R2
;A0 = R3
;A1 = R4
;A2 = R5
;A3 = R6
;T0 = R7
;T1 = R8
;T2 = R9
;T3 = R10
;
;.macro PSH Ra
; SL (SP, R0, -3), Ra
; ADI SP, -4
;.endmacro
;
;.macro PLL Re
; LL Re, (SP, R0, 1)
; ADI SP, 4
;.endmacro
; <---------------------------------------------------------------------------------------------------------->
; Branch on Clear (ie branch if bit "v" of SR is 0)
; <---------------------------------------------------------------------------------------------------------->
; Branch on "Not Zero"
.macro BNZ addr
.byte $20
.byte <((.LOWORD(addr) - (* + 1)) >> 1)
.endmacro
; Branch on "Not Carry"
.macro BNC addr
.byte $40
.byte <((.LOWORD(addr) - (* + 1)) >> 1)
.endmacro
; Branch on "Not Negative"
.macro BNN addr
.byte $60
.byte <((.LOWORD(addr) - (* + 1)) >> 1)
.endmacro
; Branch on "Not F4"
.macro BNF4 addr
.byte $80
.byte <((.LOWORD(addr) - (* + 1)) >> 1)
.endmacro
; Branch on "Not F5"
.macro BNF5 addr
.byte $A0
.byte <((.LOWORD(addr) - (* + 1)) >> 1)
.endmacro
; Branch on "Not F6"
.macro BNF6 addr
.byte $C0
.byte <((.LOWORD(addr) - (* + 1)) >> 1)
.endmacro
; Branch on "Not F7"
.macro BNF7 addr
.byte $E0
.byte <((.LOWORD(addr) - (* + 1)) >> 1)
.endmacro
; <---------------------------------------------------------------------------------------------------------->
; Branch on Set (ie branch if bit "v" of SR is 1)
; <---------------------------------------------------------------------------------------------------------->
; Branch on "Zero"
.macro BZ addr
.byte $30
.byte <((.LOWORD(addr) - (* + 1)) >> 1)
.endmacro
; Branch on "Carry"
.macro BC addr
.byte $50
.byte <((.LOWORD(addr) - (* + 1)) >> 1)
.endmacro
; Branch on "Negative"
.macro BN addr
.byte $70
.byte <((.LOWORD(addr) - (* + 1)) >> 1)
.endmacro
; Branch on "F4"
.macro BF4 addr
.byte $90
.byte <((.LOWORD(addr) - (* + 1)) >> 1)
.endmacro
; Branch on "F5"
.macro BF5 addr
.byte $B0
.byte <((.LOWORD(addr) - (* + 1)) >> 1)
.endmacro
; Branch on "F6"
.macro BF6 addr
.byte $D0
.byte <((.LOWORD(addr) - (* + 1)) >> 1)
.endmacro
; Branch on "F7"
.macro BF7 addr
.byte $F0
.byte <((.LOWORD(addr) - (* + 1)) >> 1)
.endmacro
; <---------------------------------------------------------------------------------------------------------->
; Jump and Link (PC -> Re; Ra -> PC)
; <---------------------------------------------------------------------------------------------------------->
.macro JAL Re, Ra
.byte (($F0 & <((Ra - sw32_regb) << 4)) | $01)
.byte ($0F & <(Re - sw32_regb))
.endmacro
; <---------------------------------------------------------------------------------------------------------->
; Add (Re = Ra + Rb)
; <---------------------------------------------------------------------------------------------------------->
.macro ADR Re, Ra, Rb
.byte (($F0 & <((Ra - sw32_regb) << 4)) | $02)
.byte (($F0 & (<(Rb - sw32_regb) << 4)) | ($0F & <(Re - sw32_regb)))
.endmacro
; <---------------------------------------------------------------------------------------------------------->
; Subtract (Re = Ra - Rb)
; <---------------------------------------------------------------------------------------------------------->
.macro SBR Re, Ra, Rb
.byte (($F0 & <((Ra - sw32_regb) << 4)) | $03)
.byte (($F0 & (<(Rb - sw32_regb) << 4)) | ($0F & <(Re - sw32_regb)))
.endmacro
; <---------------------------------------------------------------------------------------------------------->
; Logic AND (Re = Ra & Rb)
; <---------------------------------------------------------------------------------------------------------->
.macro ANR Re, Ra, Rb
.byte (($F0 & <((Ra - sw32_regb) << 4)) | $04)
.byte (($F0 & (<(Rb - sw32_regb) << 4)) | ($0F & <(Re - sw32_regb)))
.endmacro
; <---------------------------------------------------------------------------------------------------------->
; Logic OR (Re = Ra | Rb)
; <---------------------------------------------------------------------------------------------------------->
.macro ORR Re, Ra, Rb
.byte (($F0 & <((Ra - sw32_regb) << 4)) | $05)
.byte (($F0 & (<(Rb - sw32_regb) << 4)) | ($0F & <(Re - sw32_regb)))
.endmacro
; <---------------------------------------------------------------------------------------------------------->
; Logic XOR (Re = Ra ^ Rb)
; <---------------------------------------------------------------------------------------------------------->
.macro XOR Re, Ra, Rb
.byte (($F0 & <((Ra - sw32_regb) << 4)) | $06)
.byte (($F0 & (<(Rb - sw32_regb) << 4)) | ($0F & <(Re - sw32_regb)))
.endmacro
; <---------------------------------------------------------------------------------------------------------->
; Logic Shift Left (Re = Ra << 1) (0 -> LSB; MSB -> Carry)
; <---------------------------------------------------------------------------------------------------------->
.macro SFL Re, Ra
.byte (($F0 & <((Ra - sw32_regb) << 4)) | $07)
.byte ($0F & <(Re - sw32_regb))
.endmacro
; <---------------------------------------------------------------------------------------------------------->
; Logic Shift Right (Re = Ra >> 1) (0 -> MSB; LSB -> Carry)
; <---------------------------------------------------------------------------------------------------------->
.macro SFR Re, Ra
.byte (($F0 & <((Ra - sw32_regb) << 4)) | $08)
.byte ($0F & <(Re - sw32_regb))
.endmacro
; <---------------------------------------------------------------------------------------------------------->
; Rotate Left (Re = Ra << 1) (Carry -> LSB; MSB -> Carry)
; <---------------------------------------------------------------------------------------------------------->
.macro RLR Re, Ra
.byte (($F0 & <((Ra - sw32_regb) << 4)) | $09)
.byte ($0F & <(Re - sw32_regb))
.endmacro
; <---------------------------------------------------------------------------------------------------------->
; Rotate Right (Re = Ra >> 1) (Carry -> MSB; LSB -> Carry)
; <---------------------------------------------------------------------------------------------------------->
.macro RRR Re, Ra
.byte (($F0 & <((Ra - sw32_regb) << 4)) | $0A)
.byte ($0F & <(Re - sw32_regb))
.endmacro
; <---------------------------------------------------------------------------------------------------------->
; Add Immediate (Rx = Rx + imm)
; <---------------------------------------------------------------------------------------------------------->
.macro ADI Rx, imm
.byte (($F0 & <((Rx - sw32_regb) << 4)) | $0B)
.byte <(.right (.tcount ({imm})-1, {imm}))
.endmacro
; <---------------------------------------------------------------------------------------------------------->
; Load Byte (Re = Memory[Ra + Rc + imm])
; <---------------------------------------------------------------------------------------------------------->
.define lbr(tok) (.right (.tcount ({tok})-1, {tok}))
.define rbr(tok) (.left (.tcount ({tok})-1, {tok}))
.macro LB Re, Ra, Rc, imm
.byte (($F0 & <((lbr(Ra) - sw32_regb) << 4)) | $0C)
.byte ($F0 & <((Re - sw32_regb) << 4))
.byte <rbr(imm)
.byte (($F0 & <((Rc - sw32_regb) << 4)) | >(rbr(imm) & $FF00))
.endmacro
; <---------------------------------------------------------------------------------------------------------->
; Load Byte (Unsigned) (Re = Memory[Ra + Rc + imm])
; <---------------------------------------------------------------------------------------------------------->
.macro LBU Re, Ra, Rc, imm
.byte (($F0 & <((lbr(Ra) - sw32_regb) << 4)) | $0C)
.byte (($F0 & <((Re - sw32_regb) << 4)) | $01)
.byte <rbr(imm)
.byte (($F0 & <((Rc - sw32_regb) << 4)) | >(rbr(imm) & $FF00))
.endmacro
; <---------------------------------------------------------------------------------------------------------->
; Load Word (Re = Memory[Ra + Rc + imm])
; <---------------------------------------------------------------------------------------------------------->
.macro LW Re, Ra, Rc, imm
.byte (($F0 & <((lbr(Ra) - sw32_regb) << 4)) | $0C)
.byte (($F0 & <((Re - sw32_regb) << 4)) | $02)
.byte <rbr(imm)
.byte (($F0 & <((Rc - sw32_regb) << 4)) | >(rbr(imm) & $FF00))
.endmacro
; <---------------------------------------------------------------------------------------------------------->
; Load Word (Unsigned) (Re = Memory[Ra + Rc + imm])
; <---------------------------------------------------------------------------------------------------------->
.macro LWU Re, Ra, Rc, imm
.byte (($F0 & <((lbr(Ra) - sw32_regb) << 4)) | $0C)
.byte (($F0 & <((Re - sw32_regb) << 4)) | $03)
.byte <rbr(imm)
.byte (($F0 & <((Rc - sw32_regb) << 4)) | >(rbr(imm) & $FF00))
.endmacro
; <---------------------------------------------------------------------------------------------------------->
; Load Long (Re = Memory[Ra + Rc + imm])
; <---------------------------------------------------------------------------------------------------------->
.macro LL Re, Ra, Rc, imm
.byte (($F0 & <((lbr(Ra) - sw32_regb) << 4)) | $0C)
.byte (($F0 & <((Re - sw32_regb) << 4)) | $04)
.byte <rbr(imm)
.byte (($F0 & <((Rc - sw32_regb) << 4)) | >(rbr(imm) & $FF00))
.endmacro
; <---------------------------------------------------------------------------------------------------------->
; Store Byte (Memory[Ra + Rc + imm] = Rb)
; <---------------------------------------------------------------------------------------------------------->
.macro SB Ra, Rc, imm, Rb
.byte (($F0 & <((lbr(Ra) - sw32_regb) << 4)) | $0C)
.byte (($F0 & <((Rb - sw32_regb) << 4)) | $08)
.byte <rbr(imm)
.byte (($F0 & <((Rc - sw32_regb) << 4)) | >(rbr(imm) & $FF00))
.endmacro
; <---------------------------------------------------------------------------------------------------------->
; Store Word (Memory[Ra + Rc + imm] = Rb)
; <---------------------------------------------------------------------------------------------------------->
.macro SW Ra, Rc, imm, Rb
.byte (($F0 & <((lbr(Ra) - sw32_regb) << 4)) | $0C)
.byte (($F0 & <((Rb - sw32_regb) << 4)) | $09)
.byte <rbr(imm)
.byte (($F0 & <((Rc - sw32_regb) << 4)) | >(rbr(imm) & $FF00))
.endmacro
; <---------------------------------------------------------------------------------------------------------->
; Store Long (Memory[Ra + Rc + imm] = Rb)
; <---------------------------------------------------------------------------------------------------------->
.macro SL Ra, Rc, imm, Rb
.byte (($F0 & <((lbr(Ra) - sw32_regb) << 4)) | $0C)
.byte (($F0 & <((Rb - sw32_regb) << 4)) | $0A)
.byte <rbr(imm)
.byte (($F0 & <((Rc - sw32_regb) << 4)) | >(rbr(imm) & $FF00))
.endmacro
; <---------------------------------------------------------------------------------------------------------->
; Set bit (Rx = Rx | bitmask[imm])
; <---------------------------------------------------------------------------------------------------------->
.macro SET Rx, imm
.byte (($F0 & <((Rx - sw32_regb) << 4)) | $0D)
.byte <(imm)
.endmacro
; <---------------------------------------------------------------------------------------------------------->
; Clear bit (Rx = Rx | bitmask[imm])
; <---------------------------------------------------------------------------------------------------------->
.macro CLR Rx, imm
.byte (($F0 & <((Rx - sw32_regb) << 4)) | $0E)
.byte <(imm)
.endmacro
; <---------------------------------------------------------------------------------------------------------->
; Load Word Immediate (Re = imm)
; <---------------------------------------------------------------------------------------------------------->
.macro LWI Re, imm
.byte (($F0 & <((Re - sw32_regb) << 4)) | $0F)
.byte $00
.word .LOWORD(.right (.tcount ({imm})-1, {imm}))
.endmacro
; <---------------------------------------------------------------------------------------------------------->
; Load Word (Unsigned) Immediate (Re = imm)
; <---------------------------------------------------------------------------------------------------------->
.macro LWIU Re, imm
.byte (($F0 & <((Re - sw32_regb) << 4)) | $0F)
.byte $01
.word .LOWORD(.right (.tcount ({imm})-1, {imm}))
.endmacro
; <---------------------------------------------------------------------------------------------------------->
; Load Long Immediate (Re = imm)
; <---------------------------------------------------------------------------------------------------------->
.macro LLI Re, imm
.byte (($F0 & <((Re - sw32_regb) << 4)) | $0F)
.byte $02
.word .LOWORD(.right (.tcount ({imm})-1, {imm}))
.word .HIWORD(.right (.tcount ({imm})-1, {imm}))
.endmacro
; <---------------------------------------------------------------------------------------------------------->
; DEBUG, aka Print Register contents
; <---------------------------------------------------------------------------------------------------------->
.macro DBG Ra
.byte (($F0 & <((Ra - sw32_regb) << 4)) | $0F)
.byte $03
.endmacro
; <---------------------------------------------------------------------------------------------------------->
; Return to 65816 Mode
; <---------------------------------------------------------------------------------------------------------->
.macro EXIT
.word $FFFF
.endmacro