-
Notifications
You must be signed in to change notification settings - Fork 1
/
op4.go
376 lines (332 loc) · 6.79 KB
/
op4.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
package m68k
import (
"bytes"
"encoding/binary"
"fmt"
)
// opLea implements LEA (pg. 4-110)
func opLea(c *Processor) (t *stepTrace) {
an := (c.op & 0x0E00) >> 9
t = &stepTrace{
addr: c.PC,
op: "lea",
sz: noSize,
dst: fmt.Sprintf("A%d", an),
}
c.PC += 2
ea := decodeEA(c.op)
mod := ea & 0x38 >> 3
reg := ea & 0x07
switch mod {
default:
t.err = errBadAddress
return
case 0x02: // memory address
c.A[an] = c.A[reg]
t.src = fmt.Sprintf("(A%d)", reg)
case 0x07: // other
switch reg {
default:
t.err = errBadAddress
return
case 0x00: // absolute word
var n uint16
n, _, t.err = c.readImmWord()
c.A[an] = uint32(wordToInt32(n))
t.src = fmt.Sprintf("$%X", int32(n))
case 0x01: // absolute long
var n uint32
n, _, t.err = c.readImmLong()
c.A[an] = n
t.src = fmt.Sprintf("$%X", int32(n))
case 0x02: // program counter with displacement
var n uint16
n, _, t.err = c.readImmWord()
disp := wordToInt32(n)
c.A[an] = uint32(int32(c.PC) + disp - 2)
t.src = fmt.Sprintf("($%X,PC)", disp)
}
}
return
}
// opClr implements CLR (4-73).
// Clear operand.
func opClr(c *Processor) (t *stepTrace) {
t = &stepTrace{
addr: c.PC,
n: 1,
op: "clr",
sz: c.op >> 6 & 0x3,
}
c.PC += 2
switch t.sz {
default:
t.err = errBadOpSize
case SizeByte:
t.dst, t.err = c.writeByte(c.op, 0)
case SizeWord:
t.dst, t.err = c.writeWord(c.op, 0)
case SizeLong:
t.dst, t.err = c.writeLong(c.op, 0)
}
if t.err != nil {
return
}
c.SR &= 0xFFFFFFF0
c.SR |= StatusZero
return
}
// opMoveToSr implements MOVE to SR (6-19).
func opMoveToSr(c *Processor) (t *stepTrace) {
t = &stepTrace{
addr: c.PC,
n: 1,
op: "move",
sz: noSize,
dst: "SR",
}
c.PC += 2
var n uint16
n, t.src, t.err = c.readWord(c.op)
if t.err != nil {
return
}
c.SR = uint32(n & StatusMask)
return
}
// opMovem implements MOVEM (pg. 4-128)
func opMovem(c *Processor) (t *stepTrace) {
t = &stepTrace{
addr: c.PC,
n: 1,
op: "movem",
sz: []uint16{SizeWord, SizeLong}[c.op&0x40>>6],
}
c.PC += 2
regl, _, _ := c.readImmWord() // register list
dir := c.op & 0x0400 >> 10 // direction
if dir == 0 {
// register to memory
t.src = fmt.Sprintf("$%X", regl)
b := &bytes.Buffer{}
for i := uint16(0); i < 16; i++ {
ok := regl & (1 << i)
if ok == 0 {
continue // skip unset registers
}
// read register value
var v uint32
if i < 8 {
v = c.A[7-i]
} else {
v = c.D[7-(i-8)]
}
// write value to buffer
binary.BigEndian.PutUint32(c.buf[:4], v)
if t.sz == SizeWord { // movem.w <list>,<ea>
b.Write(c.buf[2:4])
} else { // movem.l <list>,<ea>
b.Write(c.buf[:4])
}
}
// flush buffer to memory
t.dst, t.err = c.writeBytes(c.op, b.Bytes())
return
}
// memory to register
t.dst = fmt.Sprintf("$%X", regl)
// count registers
regc := 0
for i := uint16(0); i < 16; i++ {
if regl&(1<<i) != 0 {
regc++
}
}
// read values from memory
buflen := regc * 2
if t.sz == SizeLong {
buflen = regc * 4
}
t.src, t.err = c.readBytes(c.op, c.buf[:buflen])
if t.err != nil {
return
}
// set register values
x := 0
for i := uint16(0); i < 16; i++ {
ok := regl & (1 << i)
if ok == 0 {
continue // skip unset registers
}
var v uint32
if t.sz == SizeWord { // movem.w <ea>,<list>
v = uint32(wordToInt32(binary.BigEndian.Uint16(c.buf[x : x+2])))
x += 2
} else { // movem.l <ea>,<list>
v = binary.BigEndian.Uint32(c.buf[x : x+4])
x += 4
}
if i < 8 {
c.D[i] = v
} else {
c.A[i-8] = v
}
}
return
}
// opTrap implements TRAP (4-188)
func opTrap(c *Processor) (t *stepTrace) {
v := c.op & 0x000F
t = &stepTrace{
addr: c.PC,
n: 1,
op: "trap",
sz: noSize,
src: fmt.Sprintf("#%d", v),
}
c.PC += 2
v += 32
if c.handlers[v] != nil {
// TODO: should trap handlers be executed inline?
t.err = c.handlers[v].Trap(c, int(v))
}
return
}
// opMoveUsp implements MOVE USP (pg. 6-21)
func opMoveUsp(c *Processor) (t *stepTrace) {
reg := c.op & 0x07
t = &stepTrace{
addr: c.PC,
op: "move",
n: 1,
sz: noSize,
}
c.PC += 2
dir := c.op & 0x08 >> 3
if dir == 0 { // An to USP
t.src, t.dst = fmt.Sprintf("A%d", reg), "USP"
c.A[7] = c.A[reg]
} else { // USP to An
t.src, t.dst = "USP", fmt.Sprintf("A%d", reg)
c.A[reg] = c.A[7]
}
return
}
// opStop implements STOP (6-85)
func opStop(c *Processor) (t *stepTrace) {
t = &stepTrace{
addr: c.PC,
op: "stop",
sz: noSize,
}
c.PC += 2
var n uint16
n, t.err = c.M.Word(int(c.PC))
if t.err != nil {
return
}
c.PC += 2
t.dst = fmt.Sprintf("#$%X", n)
c.SR = uint32(n) & StatusMask
// TODO: handle case where interrupt mask is maximum (0x0700)
t.err = c.Stop()
return
}
// opRts implements RTS (Return from Subroutine) (4-169)
func opRts(c *Processor) (t *stepTrace) {
t = &stepTrace{
addr: c.PC,
op: "rts",
sz: noSize,
n: 1,
}
// move program counter to the address at the top of the stack
c.PC, t.err = c.M.Long(int(c.A[7]))
if t.err != nil {
return
}
// increment stack pointer
c.A[7] += 4
return
}
// opJsr implements JSR (4-109)
func opJsr(c *Processor) (t *stepTrace) {
t = &stepTrace{
addr: c.PC,
op: "jsr",
n: 1,
sz: noSize,
}
c.PC += 2
// compute jump address and mutate PC to return address
var jmp uint32
mod := c.op & 0x38 >> 3
reg := c.op & 0x07
switch mod {
// TODO: implement remaining address modes for JSR
default:
t.err = errBadAddress
return
case 0x02: // address register indirect
jmp = c.A[reg]
t.src = fmt.Sprintf("(A%d)", reg)
case 0x07: // other
switch reg {
default:
t.err = errBadAddress
return
case 0x00: // absolute word
var n uint16
n, _, t.err = c.readImmWord()
jmp = uint32(wordToInt32(n))
t.src = fmt.Sprintf("$%X", int32(n))
case 0x01: // absolute long
var n uint32
n, _, t.err = c.readImmLong()
jmp = n
t.src = fmt.Sprintf("$%X", int32(n))
case 0x02: // program counter with displacement
var n uint16
n, _, t.err = c.readImmWord()
disp := wordToInt32(n)
jmp = uint32(int32(c.PC) + disp - 2)
t.src = fmt.Sprintf("($%X,PC)", disp)
}
}
// push return address (current PC) to stack
c.A[7] -= 4
binary.BigEndian.PutUint32(c.buf[:4], c.PC)
_, t.err = c.M.Write(int(c.A[7]), c.buf[:4])
if t.err != nil {
return
}
// jump
c.PC = jmp
return
}
// opTst implements TST (4-192)
func opTst(c *Processor) (t *stepTrace) {
t = &stepTrace{
addr: c.PC,
op: "tst",
sz: c.op & 0xC0 >> 6,
n: 1,
}
c.PC += 2
c.SR &= 0xFFFFFFF0 // reset last 4 bits only
switch t.sz {
case SizeByte:
var b byte
b, t.src, t.err = c.readByte(c.op)
c.setStatusForByte(b)
case SizeWord:
var n uint16
n, t.src, t.err = c.readWord(c.op)
c.setStatusForWord(n)
case SizeLong:
var n uint32
n, t.src, t.err = c.readLong(c.op)
c.setStatusForLong(n)
}
return
}