-
Notifications
You must be signed in to change notification settings - Fork 1
/
op0.go
388 lines (345 loc) · 6.59 KB
/
op0.go
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
package m68k
import (
"fmt"
)
// opMove implements MOVE, MOVEA and MOVEP
func opMove(c *Processor) (t *stepTrace) {
t = &stepTrace{
addr: c.PC,
op: "move",
n: 1,
// decode unusual size encoding for move instructions
sz: []uint16{0xFFFF, SizeByte, SizeLong, SizeWord}[(c.op&0x3000)>>12],
}
c.PC += 2
src, dst := decodeEA(c.op), decodeExEA(c.op)
if src&0x38 == 0x08 && dst&0x40 == 0x40 {
t.op = "movep"
}
if dst&0x38 == 0x08 {
t.op = "movea"
// TODO: sign extension and drop support for bytes
}
c.SR &= 0xFFFFFFF0
switch t.sz {
case SizeByte:
var b byte
b, t.src, t.err = c.readByte(src)
if t.err != nil {
break
}
t.dst, t.err = c.writeByte(dst, b)
if b == 0 {
c.SR |= StatusZero
}
case SizeWord:
var v uint16
v, t.src, t.err = c.readWord(src)
if t.err != nil {
break
}
t.dst, t.err = c.writeWord(dst, v)
if v == 0 {
c.SR |= StatusZero
}
case SizeLong:
var v uint32
v, t.src, t.err = c.readLong(src)
if t.err != nil {
break
}
t.dst, t.err = c.writeLong(dst, v)
if v == 0 {
c.SR |= StatusZero
}
}
return
}
// opOri implements ORI, ORI to CCR and ORI to SR.
func opOri(c *Processor) (t *stepTrace) {
t = &stepTrace{
addr: c.PC,
op: "ori",
n: 1,
sz: (c.op & 0xC0) >> 6,
}
c.PC += 2
ea := decodeEA(c.op)
switch t.sz {
default:
t.err = errBadOpSize
return
case SizeByte:
// read immediate byte
var src, dst byte
src, t.src, t.err = c.readImmByte()
if t.err != nil {
break
}
if ea == 0x3C { // ori.b to CCR
c.SR |= uint32(src) & StatusMask
t.sz = noSize
t.dst = "CCR"
break
}
// ori.b
dst, _, t.err = c.readByte(ea)
if t.err != nil {
break
}
b := dst | src
t.dst, t.err = c.writeByte(ea, b)
case SizeWord:
// read immediate word
var src, dst uint16
src, t.src, t.err = c.readImmWord()
if t.err != nil {
break
}
if ea == 0x3C { // ori.w to SR
c.SR |= uint32(src) & StatusMask
t.sz = noSize
t.dst = "SR"
break
}
// ori.w
dst, _, t.err = c.readWord(ea)
if t.err != nil {
break
}
v := dst | src
t.dst, t.err = c.writeWord(ea, v)
case SizeLong:
// read immediate long
var src, dst uint32
src, t.src, t.err = c.readImmLong()
if t.err != nil {
break
}
dst, _, t.err = c.readLong(ea)
if t.err != nil {
break
}
v := dst | src
t.dst, t.err = c.writeLong(ea, v)
}
return
}
// opAndi implements ANDI (4-18), ANDI to CCR and ANDI to SR
func opAndi(c *Processor) (t *stepTrace) {
t = &stepTrace{
addr: c.PC,
op: "andi",
n: 1,
sz: (c.op & 0xC0) >> 6,
}
c.PC += 2
ea := decodeEA(c.op)
sr := c.SR // cache value in case destination is status register
c.SR &= 0xFFFFFFF0 // reset last 4 bits only
switch t.sz {
default:
t.err = errBadOpSize
return
case SizeByte:
// read immediate byte
var src, dst byte
src, t.src, t.err = c.readImmByte()
if ea == 0x3C { // and.b to CCR
c.SR = sr | uint32(src)&StatusMask
t.sz = noSize
t.dst = "CCR"
break
}
// and.b
dst, _, t.err = c.readByte(ea)
if t.err != nil {
break
}
b := dst & src
t.dst, t.err = c.writeByte(ea, b)
c.setStatusForByte(b)
case SizeWord:
// read immediate word
var src, dst uint16
src, t.src, t.err = c.readImmWord()
if ea == 0x3C { // andi.w to SR
c.SR = sr | uint32(src)&StatusMask
t.sz = noSize
t.dst = "SR"
break
}
// ori.w
dst, _, t.err = c.readWord(ea)
if t.err != nil {
break
}
v := dst & src
t.dst, t.err = c.writeWord(ea, v)
c.setStatusForWord(v)
case SizeLong:
// read immediate long
var src, dst uint32
src, t.src, t.err = c.readImmLong()
// andi.l
dst, _, t.err = c.readLong(ea)
if t.err != nil {
break
}
v := dst & src
t.dst, t.err = c.writeLong(ea, v)
c.setStatusForLong(v)
}
return
}
// opSubi implements SUBI
func opSubi(c *Processor) (t *stepTrace) {
// TODO: eliminate illegal address schemes
t = &stepTrace{
addr: c.PC,
op: "subi",
n: 1,
sz: (c.op & 0xC0) >> 6,
}
c.PC += 2
ea := decodeEA(c.op)
var a, b Int
a, t.src, t.err = c.readImmInt(t.sz)
if t.err != nil {
return
}
b, t.dst, t.err = c.readInt(ea, t.sz)
if t.err != nil {
return
}
b = b.Sub(a)
t.dst, t.err = c.writeInt(ea, b)
return
}
// opAddi implements ADDI
func opAddi(c *Processor) (t *stepTrace) {
// TODO: eliminate illegal address schemes
t = &stepTrace{
addr: c.PC,
op: "addi",
n: 1,
sz: (c.op & 0xC0) >> 6,
}
c.PC += 2
ea := decodeEA(c.op)
var a, b Int
a, t.src, t.err = c.readImmInt(t.sz)
b, _, t.err = c.readInt(ea, t.sz)
b = b.Add(a)
t.dst, t.err = c.writeInt(ea, b)
return
}
// opEori implements EORI, EORI to CCR and EORI to SR.
func opEori(c *Processor) (t *stepTrace) {
t = &stepTrace{
addr: c.PC,
op: "eori",
n: 1,
sz: (c.op & 0xC0) >> 6,
}
c.PC += 2
ea := decodeEA(c.op)
switch t.sz {
default:
t.err = errBadOpSize
return
case SizeByte:
// read immediate byte
var src, dst byte
src, t.src, t.err = c.readImmByte()
if t.err != nil {
break
}
if ea == 0x3C { // eori.b to CCR
c.SR = (c.SR ^ uint32(src)) & StatusMask
t.sz = noSize
t.dst = "CCR"
break
}
// eori.b
dst, _, t.err = c.readByte(ea)
if t.err != nil {
break
}
b := dst ^ src
t.dst, t.err = c.writeByte(ea, b)
case SizeWord:
// read immediate word
var src, dst uint16
src, t.src, t.err = c.readImmWord()
if t.err != nil {
break
}
if ea == 0x3C { // eori.w to SR
c.SR = (c.SR ^ uint32(src)) & StatusMask
t.sz = noSize
t.dst = "SR"
break
}
// eori.w
dst, _, t.err = c.readWord(ea)
if t.err != nil {
break
}
v := dst ^ src
t.dst, t.err = c.writeWord(ea, v)
case SizeLong:
// read immediate long
var src, dst uint32
src, t.src, t.err = c.readImmLong()
if t.err != nil {
break
}
// eori.l
dst, _, t.err = c.readLong(ea)
if t.err != nil {
break
}
v := dst ^ src
t.dst, t.err = c.writeLong(ea, v)
}
return
}
// opBtst implements BTST (4-61)
func opBtst(c *Processor) (t *stepTrace) {
// TODO: implement bit number as immediate value (4-63)
t = &stepTrace{
addr: c.PC,
op: "btst",
sz: noSize,
n: 1,
}
c.PC += 2
// read bit number from data register
src := (c.op >> 9) & 0x7
t.src = fmt.Sprintf("D%d", src)
bit := c.D[src]
// read destination value
mod := c.op & 0x38 >> 3
var v, width uint32
if mod == 0 { // data register
v, t.dst, t.err = c.readLong(c.op)
if t.err != nil {
return
}
width = 32
} else {
var b byte
b, t.dst, t.err = c.readByte(c.op)
v = uint32(b)
width = 8
}
// set status bit
bit %= width
if v&bit == 0 {
c.SR |= StatusZero
} else {
c.SR &= ^StatusZero
}
return
}