-
Notifications
You must be signed in to change notification settings - Fork 0
/
disassembler.go
674 lines (668 loc) · 22.5 KB
/
disassembler.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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
package main
import (
"fmt"
)
func (cpu *CPU) dissAssemble(b []byte) Instr {
dm := map[byte]byte{
0: 24,
1: 26,
2: 28,
3: 30,
}
m := lookUp(b)
inst := Instr{family: m.family, label: m.label}
switch m.label {
case INSN_UNK:
inst.objdump = fmt.Sprintf("\nBZZZT! THANKS FOR PLAYING! %.4x\n", b2u16big(b))
return inst
case INSN_NOP:
inst.objdump = fmt.Sprintf("%.4x\tnop\n", b2u16big(b))
return inst
case INSN_CLI:
inst.objdump = fmt.Sprintf("%.4x\tcli\n", b2u16big(b))
return inst
case INSN_CLT:
inst.objdump = fmt.Sprintf("%.4x\tclt\n", b2u16big(b))
return inst
case INSN_SET:
inst.objdump = fmt.Sprintf("%.4x\tset\n", b2u16big(b))
return inst
case INSN_SEC:
inst.objdump = fmt.Sprintf("%.4x\tsec\n", b2u16big(b))
return inst
case INSN_ADC:
// 0001 11rd dddd rrrr
inst.source = (((b[1]&0x02)>>1)<<4 | (b[0] & 0x0f))
inst.dest = ((b[1]&0x01)<<4 | ((b[0] & 0xf0) >> 4))
inst.objdump = fmt.Sprintf("%.4x\tadc\tr%d, r%d\n", b2u16big(b), inst.dest, inst.source)
return inst
case INSN_EOR:
// 0010 01rd dddd rrrr
inst.source = (((b[1]&0x02)>>1)<<4 | (b[0] & 0x0f))
inst.dest = ((b[1]&0x01)<<4 | ((b[0] & 0xf0) >> 4))
inst.objdump = fmt.Sprintf("%.4x\teor\tr%d, r%d\n", b2u16big(b), inst.source, inst.dest)
return inst
case INSN_OUT:
// 1011 1AAr rrrr AAAA
//out := (b[1] >> 3) & 0xff
inst.ioaddr = ((b[1] & 0x06) << 3) | (b[0] & 0x0f)
inst.source = ((b[1] & 0x01) << 4) | ((b[0] & 0xf0) >> 4)
inst.objdump = fmt.Sprintf("%.4x\tout\t0x%.2x, r%d\t\t;%d\n", b2u16big(b), inst.ioaddr, inst.source, inst.ioaddr)
return inst
case INSN_LDI:
// 1110 KKKK dddd KKKK
inst.kdata = ((b[1] & 0x0f) << 4) | (b[0] & 0x0f)
inst.dest = ((b[0] & 0xf0) >> 4) + 0x10
inst.objdump = fmt.Sprintf("%.4x\tldi\tr%d, 0x%.2x\n", b2u16big(b), inst.dest, inst.kdata)
return inst
case INSN_RCALL:
// 1101 kkkk kkkk kkkk
k := (uint32(b[1]&0x0f)<<8 | uint32(b[0]))
if ((k & 0x0800) >> 11) == 1 {
inst.k16 = int16((k + 0xf000) << 1)
inst.objdump = fmt.Sprintf("%.4x\trcall\t.%d\t;%.4x\n", b2u16big(b), inst.k16, (cpu.pc+uint16(inst.k16))%8192)
} else {
inst.k16 = int16(k << 1)
inst.objdump = fmt.Sprintf("%.4x\trcall\t.+%d\t;%.4x\n", b2u16big(b), inst.k16, (cpu.pc+uint16(inst.k16))%8192)
}
return inst
case INSN_SBI:
// 1001 1010 AAAA Abbb
inst.ioaddr = b[0] >> 3
inst.registerBit = b[0] & 0x7
inst.objdump = fmt.Sprintf("%.4x\tsbi\t0x%x, %d\n", b2u16big(b), inst.ioaddr, inst.registerBit)
return inst
case INSN_CBI:
//1001 1000 AAAA Abbb
inst.ioaddr = (b[0] & 0xf8) >> 3
inst.registerBit = b[0] & 0x07
inst.objdump = fmt.Sprintf("%.4x\tcbi\t0x%.2x, %d\n", b2u16big(b), inst.ioaddr, inst.registerBit)
return inst
case INSN_SBIC:
// 1001 1001 AAAA Abbb
inst.ioaddr = (b[0] & 0xf8) >> 3
inst.registerBit = b[0] & 0x07
inst.objdump = fmt.Sprintf("%.4x\tsbic\t0x%.2x, %d\n", b2u16big(b), inst.ioaddr, inst.registerBit)
return inst
case INSN_SBIS:
// 1001 1011 AAAA Abbb
inst.ioaddr = (b[0] & 0xf8) >> 3
inst.registerBit = b[0] & 0x07
inst.objdump = fmt.Sprintf("%.4x\tsbis\t0x%.2x, %d\n", b2u16big(b), inst.ioaddr, inst.registerBit)
return inst
case INSN_BLD:
// 1111 100d dddd 0bbb
inst.dest = ((b[1] & 0x01) << 4) | ((b[0] & 0xf0) >> 4)
inst.registerBit = b[0] & 0x07
inst.objdump = fmt.Sprintf("%.4x\tbld\tr%d, %d\n", b2u16big(b), inst.dest, inst.registerBit)
return inst
case INSN_BST:
// 1111 101d dddd 0bbb
inst.dest = ((b[1] & 0x01) << 4) | ((b[0] & 0xf0) >> 4)
inst.registerBit = b[0] & 0x07
inst.objdump = fmt.Sprintf("%.4x\tbst\tr%d, %d\n", b2u16big(b), inst.dest, inst.registerBit)
return inst
case INSN_SBRC:
// 1111 110r rrrr 0bbb
inst.source = ((b[1] & 0x01) << 4) | ((b[0] & 0xf0) >> 4)
inst.registerBit = b[0] & 0x07
inst.objdump = fmt.Sprintf("%.4x\tsbrc\tr%d, %d\n", b2u16big(b), inst.source, inst.registerBit)
return inst
case INSN_SBRS:
// 1111 111r rrrr 0bbb
inst.source = ((b[1] & 0x01) << 4) | ((b[0] & 0xf0) >> 4)
inst.registerBit = b[0] & 0x07
inst.objdump = fmt.Sprintf("%.4x\tsbrs\tr%d, %d\n", b2u16big(b), inst.source, inst.registerBit)
return inst
case INSN_STS:
// 1001 001d dddd 0000 kkkk kkkk kkkk kkkk
inst.source = ((b[1]&0x01)<<4 | ((b[0] & 0xf0) >> 4))
cpu.imem.Fetch()
inst.k16 = b2i16little(current)
inst.objdump = fmt.Sprintf("%.4x\tsts\t0x%.4x, r%d\n", b2u16big(b), inst.k16, inst.source)
return inst
case INSN_LDS:
inst.dest = ((b[1]&0x01)<<4 | ((b[0] & 0xf0) >> 4))
cpu.imem.Fetch()
inst.k16 = b2i16little(current)
inst.objdump = fmt.Sprintf("%.4x\tlds\tr%d, 0x%.4x\n", b2u16big(b), inst.dest, inst.k16)
return inst
case INSN_RJMP:
// 1100 kkkk kkkk kkkk
k := (uint32(b[1]&0x0f)<<8 | uint32(b[0]))
if ((k & 0x800) >> 11) == 1 {
inst.k16 = int16((k + 0xf000) << 1)
inst.objdump = fmt.Sprintf("%.4x\trjmp\t.%d\n", b2u16big(b), inst.k16)
} else {
inst.k16 = int16(k << 1)
inst.objdump = fmt.Sprintf("%.4x\trjmp\t.+%d\n", b2u16big(b), inst.k16)
}
return inst
case INSN_IJMP:
// 1001 0100 0000 1001
inst.objdump = fmt.Sprintf("%.4x\tijmp\n", b2u16big(b))
return inst
case INSN_JMP:
// 1001 010k kkkk 110k kkkk kkkk kkkk kkkk
var k1, k2, k3 uint32
k1 = uint32(b[1]&0x01) << 20
k2 = uint32(b[0]&0xf0) << 12
cpu.imem.Fetch()
k3 = uint32(current[1])<<8 | uint32(current[0])
inst.k32 = (k1 | k2 | k3) << 1
inst.objdump = fmt.Sprintf("%.4x\tjmp\t0x%.8x\t;%d\n", b2u16big(b), inst.k32, inst.k32)
return inst
case INSN_CALL:
// 1001 010k kkkk 111k kkkk kkkk kkkk kkkk
var k1, k2, k3 uint32
k1 = uint32(b[1]&0x01) << 20
k2 = uint32(b[0]&0xf0) << 12
cpu.imem.Fetch()
k3 = uint32(current[1])<<8 | uint32(current[0])
inst.k32 = (k1 | k2 | k3) << 1
inst.objdump = fmt.Sprintf("%.4x\tcall\t0x%.8x\n", b2u16big(b), inst.k32)
return inst
case INSN_ADD:
// 0000 11rd dddd rrrr
inst.source = (((b[1]&0x02)>>1)<<4 | (b[0] & 0x0f))
inst.dest = ((b[1]&0x01)<<4 | ((b[0] & 0xf0) >> 4))
inst.objdump = fmt.Sprintf("%.4x\tadd\tr%d, r%d\n", b2u16big(b), inst.dest, inst.source)
return inst
case INSN_ADIW:
// 1001 0110 KKdd KKKK
// 24,26,28,30
inst.kdata = ((b[0] & 0xc0) >> 2) | (b[0] & 0x0f)
Rd := (b[0] & 0x30) >> 4
inst.dest = dm[Rd]
inst.objdump = fmt.Sprintf("%.4x\tadiw\tr%d, 0x%.2x\n", b2u16big(b), inst.dest, inst.kdata)
return inst
case INSN_SBIW:
// 1001 0111 KKdd KKKK
inst.kdata = ((b[0] & 0xc0) >> 2) | (b[0] & 0x0f)
Rd := (b[0] & 0x30) >> 4
inst.dest = dm[Rd]
inst.objdump = fmt.Sprintf("%.4x\tsbiw\tr%d, 0x%.2x\n", b2u16big(b), inst.dest, inst.kdata)
return inst
case INSN_AND:
// 0010 00rd dddd rrrr
inst.source = (((b[1]&0x02)>>1)<<4 | (b[0] & 0x0f))
inst.dest = ((b[1]&0x01)<<4 | ((b[0] & 0xf0) >> 4))
inst.objdump = fmt.Sprintf("%.4x\tand\tr%d, r%d\n", b2u16big(b), inst.dest, inst.source)
return inst
case INSN_ANDI:
//0111 KKKK dddd KKKK
inst.kdata = ((b[1] & 0x0f) << 4) | (b[0] & 0x0f)
inst.dest = ((b[0] & 0xf0) >> 4) + 0x10
inst.objdump = fmt.Sprintf("%.4x\tandi\tr%d, 0x%.2x\n", b2u16big(b), inst.dest, inst.kdata)
return inst
case INSN_BRCC:
//1111 01kk kkkk k000
// 64 ≤ k ≤ +63
k := (b2u16little(b) & 0x03f8) >> 3
if ((k & 0x40) >> 6) == 1 {
inst.k16 = int16((k + 0xff80) << 1)
} else {
inst.k16 = int16(k << 1)
}
inst.objdump = fmt.Sprintf("%.4x\tbrcc\t.+%d\n", b2u16big(b), inst.k16)
return inst
case INSN_BRCS:
// Supposed to be -64<k<+63, but avr-objdump doesn't display
// these values this way.
// 1111 00kk kkkk k000
k := (b2u16little(b) & 0x03f8) >> 3
if ((k & 0x40) >> 6) == 1 {
inst.k16 = int16((k + 0xff80) << 1)
inst.objdump = fmt.Sprintf("%.4x\tbrcs\t.%d\n", b2u16big(b), inst.k16)
} else {
inst.k16 = int16(k << 1)
inst.objdump = fmt.Sprintf("%.4x\tbrcs\t.+%d\n", b2u16big(b), inst.k16)
}
return inst
case INSN_BRMI:
// 1111 00kk kkkk k010
k := (b2u16little(b) & 0x03f8) >> 3
if ((k & 0x40) >> 6) == 1 {
inst.k16 = int16((k + 0xff80) << 1)
inst.objdump = fmt.Sprintf("%.4x\tbrmi\t.%d\n", b2u16big(b), inst.k16)
} else {
inst.k16 = int16(k << 1)
inst.objdump = fmt.Sprintf("%.4x\tbrmi\t.+%d\n", b2u16big(b), inst.k16)
}
return inst
case INSN_BRGE:
// 1111 01kk kkkk k100
k := (b2u16little(b) & 0x03f8) >> 3
if ((k & 0x40) >> 6) == 1 {
inst.k16 = int16((k + 0xff80) << 1)
inst.objdump = fmt.Sprintf("%.4x\tbrge\t.%d\n", b2u16big(b), inst.k16)
} else {
inst.k16 = int16(k << 1)
inst.objdump = fmt.Sprintf("%.4x\tbrge\t.+%d\n", b2u16big(b), inst.k16)
}
return inst
case INSN_BRNE:
// 1111 01kk kkkk k001
k := (b2u16little(b) & 0x03f8) >> 3
// check to see if msb of k is 1
// if it is, the result is negative.
if ((k & 0x40) >> 6) == 1 {
inst.k16 = int16((k + 0xff80) << 1)
inst.objdump = fmt.Sprintf("%.4x\tbrne\t.%d\n", b2u16big(b), inst.k16)
} else {
inst.k16 = int16(k << 1)
inst.objdump = fmt.Sprintf("%.4x\tbrne\t.+%d\n", b2u16big(b), inst.k16)
}
return inst
case INSN_BRLT:
// 1111 00kk kkkk k100
k := (b2u16little(b) & 0x03f8) >> 3
// check to see if msb of k is 1
// if it is, the result is negative.
if ((k & 0x40) >> 6) == 1 {
inst.k16 = int16((k + 0xff80) << 1)
inst.objdump = fmt.Sprintf("%.4x\tbrlt\t.%d\n", b2u16big(b), inst.k16)
} else {
inst.k16 = int16(k << 1)
inst.objdump = fmt.Sprintf("%.4x\tbrlt\t.+%d\n", b2u16big(b), inst.k16)
}
return inst
case INSN_BREQ:
// Supposed to be -64<k<+63, but avr-objdump doesn't display
// these values this way.
// 1111 00kk kkkk k001
k := (b2u16little(b) & 0x03f8) >> 3
if ((k & 0x40) >> 6) == 1 {
inst.k16 = int16((k + 0xff80) << 1)
inst.objdump = fmt.Sprintf("%.4x\tbreq\t.%d\n", b2u16big(b), inst.k16)
} else {
inst.k16 = int16(k << 1)
inst.objdump = fmt.Sprintf("%.4x\tbreq\t.+%d\n", b2u16big(b), inst.k16)
}
return inst
case INSN_BRPL:
// 1111 01kk kkkk k010
k := (b2u16little(b) & 0x03f8) >> 3
if ((k & 0x40) >> 6) == 1 {
inst.k16 = int16((k + 0xff80) << 1)
inst.objdump = fmt.Sprintf("%.4x\tbrpl\t.%d\n", b2u16big(b), inst.k16)
} else {
inst.k16 = int16(k << 1)
inst.objdump = fmt.Sprintf("%.4x\tbrpl\t.+%d\n", b2u16big(b), inst.k16)
}
return inst
case INSN_BRTC:
// 1111 01kk kkkk k110
k := (b2u16little(b) & 0x03f8) >> 3
if ((k & 0x40) >> 6) == 1 {
inst.k16 = int16((k + 0xff80) << 1)
inst.objdump = fmt.Sprintf("%.4x\tbrtc\t.%d\n", b2u16big(b), inst.k16)
} else {
inst.k16 = int16(k << 1)
inst.objdump = fmt.Sprintf("%.4x\tbrtc\t.+%d\n", b2u16big(b), inst.k16)
}
return inst
case INSN_BRTS:
// 1111 00kk kkkk k110
k := (b2u16little(b) & 0x03f8) >> 3
if ((k & 0x40) >> 6) == 1 {
inst.k16 = int16((k + 0xff80) << 1)
inst.objdump = fmt.Sprintf("%.4x\tbrts\t.%d\n", b2u16big(b), inst.k16)
} else {
inst.k16 = int16(k << 1)
inst.objdump = fmt.Sprintf("%.4x\tbrts\t.+%d\n", b2u16big(b), inst.k16)
}
return inst
case INSN_COM:
// 1001 010d dddd 0000
inst.dest = ((b[1] & 0x01) << 4) | ((b[0] & 0xf0) >> 4)
inst.objdump = fmt.Sprintf("%.4x\tcom\tr%d\n", b2u16big(b), inst.dest)
return inst
case INSN_CP:
// 0001 01rd dddd rrrr
inst.source = (((b[1]&0x02)>>1)<<4 | (b[0] & 0x0f))
inst.dest = ((b[1]&0x01)<<4 | ((b[0] & 0xf0) >> 4))
inst.objdump = fmt.Sprintf("%.4x\tcp\tr%d, r%d\n", b2u16big(b), inst.dest, inst.source)
return inst
case INSN_CPC:
// 0000 01rd dddd rrrr
inst.source = ((b[1] & 0x02) << 3) | b[0]&0x0f
inst.dest = ((b[1] & 0x01) << 4) | ((b[0] & 0xf0) >> 4)
inst.objdump = fmt.Sprintf("%.4x\tcpc\tr%d, r%d\n", b2u16big(b), inst.dest, inst.source)
return inst
case INSN_CPI:
// 0011 KKKK dddd KKKK
inst.kdata = ((b[1] & 0x0f) << 4) | (b[0] & 0x0f)
inst.dest = ((b[0] & 0xf0) >> 4) + 0x10
inst.objdump = fmt.Sprintf("%.4x\tcpi\tr%d, 0x%.2x\t;%d\n", b2u16big(b), inst.dest, inst.kdata, inst.kdata)
return inst
case INSN_CPSE:
// 0001 00rd dddd rrrr
inst.source = (((b[1]&0x02)>>1)<<4 | (b[0] & 0x0f))
inst.dest = ((b[1]&0x01)<<4 | ((b[0] & 0xf0) >> 4))
inst.objdump = fmt.Sprintf("%.4x\tcpse\tr%d, r%d\n", b2u16big(b), inst.dest, inst.source)
return inst
case INSN_DEC:
// 1001 010d dddd 1010
inst.dest = ((b[1] & 0x01) << 4) | ((b[0] & 0xf0) >> 4)
inst.objdump = fmt.Sprintf("%.4x\tdec\tr%d\n", b2u16big(b), inst.dest)
return inst
case INSN_INC:
// 1001 010d dddd 1011
inst.dest = ((b[1] & 0x01) << 4) | ((b[0] & 0xf0) >> 4)
inst.objdump = fmt.Sprintf("%.4x\tdec\tr%d\n", b2u16big(b), inst.dest)
return inst
case INSN_IN:
// 1011 0AAd dddd AAAA
inst.ioaddr = ((b[1] & 0x06) << 3) | (b[0] & 0x0f)
inst.dest = ((b[1] & 0xf1) << 4) | ((b[0] & 0xf0) >> 4)
inst.objdump = fmt.Sprintf("%.4x\tin\tr%d, 0x%.2x\n", b2u16big(b), inst.dest, inst.ioaddr)
return inst
case INSN_LDDY:
// 1001 000d dddd 1001
inst.dest = ((b[1] & 0x01) << 4) | ((b[0] & 0xf0) >> 4)
inst.offset = m.offset
inst.objdump = fmt.Sprintf("%.4x\tldd\tY+%d, r%d\n", b2u16big(b), inst.offset, inst.dest)
return inst
case INSN_LDDZ:
// 1001 000d dddd 0001
inst.dest = ((b[1] & 0x01) << 4) | ((b[0] & 0xf0) >> 4)
inst.offset = m.offset
inst.objdump = fmt.Sprintf("%.4x\tldd\tr%d, Z+%d\n", b2u16big(b), inst.dest, inst.offset)
return inst
case INSN_LDX:
// 1001 000d dddd 1100
inst.dest = ((b[1] & 0x01) << 4) | ((b[0] & 0xf0) >> 4)
inst.objdump = fmt.Sprintf("%.4x\tld\tr%d, X\n", b2u16big(b), inst.dest)
return inst
case INSN_LDXP:
//1001 000d dddd 1101
inst.dest = ((b[1] & 0x01) << 4) | ((b[0] & 0xf0) >> 4)
inst.objdump = fmt.Sprintf("%.4x\tld\tr%d, X+\n", b2u16big(b), inst.dest)
return inst
case INSN_LDY:
// 1000 000d dddd 1000
inst.dest = ((b[1] & 0x01) << 4) | ((b[0] & 0xf0) >> 4)
inst.objdump = fmt.Sprintf("%.4x\tld\tr%d, Y\n", b2u16big(b), inst.dest)
return inst
case INSN_LDYP:
// 1001 000d dddd 1001
inst.dest = ((b[1] & 0x01) << 4) | ((b[0] & 0xf0) >> 4)
inst.objdump = fmt.Sprintf("%.4x\tld\tr%d, Y+\n", b2u16big(b), inst.dest)
return inst
case INSN_LDYM:
// 1001 000d dddd 1010
inst.dest = ((b[1] & 0x01) << 4) | ((b[0] & 0xf0) >> 4)
inst.objdump = fmt.Sprintf("%.4x\tld\tr%d, -Y\n", b2u16big(b), inst.dest)
return inst
case INSN_LDZ:
// 1000 000d dddd 0000
inst.dest = ((b[1] & 0x01) << 4) | ((b[0] & 0xf0) >> 4)
inst.objdump = fmt.Sprintf("%.4x\tld\tr%d, Z\n", b2u16big(b), inst.dest)
return inst
case INSN_LDZP:
// 1001 000d dddd 0001
inst.dest = ((b[1] & 0x01) << 4) | ((b[0] & 0xf0) >> 4)
inst.objdump = fmt.Sprintf("%.4x\tld\tr%d, Z+\n", b2u16big(b), inst.dest)
return inst
case INSN_LDZM:
// 1001 000d dddd 0010
inst.dest = ((b[1] & 0x01) << 4) | ((b[0] & 0xf0) >> 4)
inst.objdump = fmt.Sprintf("%.4x\tld\tr%d, -Z\n", b2u16big(b), inst.dest)
return inst
case INSN_LPMZ:
//z 1001 000d dddd 0100
// XXX ToDo not tested
// XXX ToDo(Erin) Not sure this works.
inst.dest = ((b[1] & 0x01) << 4) | ((b[0] & 0xf0) >> 4)
inst.objdump = fmt.Sprintf("%.4x\tlpm\tr%d, Z\n", b2u16big(b), inst.dest)
return inst
case INSN_LPMZP:
//z+ 1001 000d dddd 0101
inst.dest = ((b[1] & 0x01) << 4) | ((b[0] & 0xf0) >> 4)
inst.objdump = fmt.Sprintf("%.4x\tlpm\tr%d, Z+\n", b2u16big(b), inst.dest)
return inst
case INSN_LPM:
// 1001 0101 1100 1000
// XXX ToDo: not tested
inst.dest = 0
inst.objdump = fmt.Sprintf("%.4x\tlpm\n", b2u16big(b))
return inst
case INSN_LSR:
//1001 010d dddd 0110
inst.dest = ((b[1] & 0x01) << 4) | ((b[0] & 0xf0) >> 4)
inst.objdump = fmt.Sprintf("%.4x\tlsr\tr%d\n", b2u16big(b), inst.dest)
return inst
case INSN_ASR:
//1001 010d dddd 0101
inst.dest = ((b[1] & 0x01) << 4) | ((b[0] & 0xf0) >> 4)
inst.objdump = fmt.Sprintf("%.4x\tasr\tr%d\n", b2u16big(b), inst.dest)
return inst
case INSN_LSL:
//1001 11dd dddd 0110
inst.dest = ((b[1] & 0x03) << 4) | ((b[0] & 0xf0) >> 4)
inst.objdump = fmt.Sprintf("%.4x\tlsl\tr%d\n", b2u16big(b), inst.dest)
return inst
case INSN_MOV:
// 0010 11rd dddd rrrr
inst.source = (((b[1] & 0x02) >> 1) << 4) | (b[0] & 0x0f)
inst.dest = ((b[1]&0x01)<<4 | ((b[0] & 0xf0) >> 4))
inst.objdump = fmt.Sprintf("%.4x\tmov\tr%d, r%d\n", b2u16big(b), inst.dest, inst.source)
return inst
case INSN_MOVW:
// 0000 0001 dddd rrrr
inst.dest = (b[0] & 0xf0) >> 3
inst.source = (b[0] & 0x0f) << 1
inst.objdump = fmt.Sprintf("%.4x\tmovw\tr%d, r%d\n", b2u16big(b), inst.dest, inst.source)
return inst
case INSN_MUL:
// 1001 11rd dddd rrrr
inst.source = (((b[1] & 0x02) >> 1) << 4) | (b[0] & 0x0f)
inst.dest = ((b[1]&0x01)<<4 | ((b[0] & 0xf0) >> 4))
inst.objdump = fmt.Sprintf("%.4x\tmul\tr%d, r%d\n", b2u16big(b), inst.dest, inst.source)
return inst
case INSN_NEG:
// 1001 010d dddd s0001
inst.dest = ((b[1] & 0x01) << 4) | ((b[0] & 0xf0) >> 4)
inst.objdump = fmt.Sprintf("%.4x\tneg\tr%d\n", b2u16big(b), inst.dest)
return inst
case INSN_OR:
// 0010 10rd dddd rrrr
inst.source = (((b[1] & 0x02) >> 1) << 4) | (b[0] & 0x0f)
inst.dest = ((b[1]&0x01)<<4 | ((b[0] & 0xf0) >> 4))
inst.objdump = fmt.Sprintf("%.4x\tor\tr%d, r%d\n", b2u16big(b), inst.dest, inst.source)
return inst
case INSN_ORI:
// 0110 KKKK dddd KKKK
inst.kdata = ((b[1] & 0x0f) << 4) | (b[0] & 0x0f)
inst.dest = ((b[0] & 0xf0) >> 4) + 0x10
inst.objdump = fmt.Sprintf("%.4x\tori\tr%d, %.2x\n", b2u16big(b), inst.dest, inst.kdata)
return inst
case INSN_POP:
// 1001 000d dddd 1111
inst.dest = ((b[1] & 0x01) << 4) | ((b[0] & 0xf0) >> 4)
inst.objdump = fmt.Sprintf("%.4x\tpop\t r%d\n", b2u16big(b), inst.dest)
return inst
case INSN_PUSH:
//1001 001d dddd 1111
inst.source = ((b[1] & 0x01) << 4) | ((b[0] & 0xf0) >> 4)
inst.objdump = fmt.Sprintf("%.4x\tpush\tr%d\n", b2u16big(b), inst.source)
return inst
case INSN_ICALL:
inst.objdump = fmt.Sprintf("%.4x\ticall\n", b2u16big(b))
return inst
case INSN_RET:
inst.objdump = fmt.Sprintf("%.4x\tret\n", b2u16big(b))
return inst
case INSN_RETI:
inst.objdump = fmt.Sprintf("%.4x\treti\n", b2u16big(b))
return inst
case INSN_ROR:
// 1001 010d dddd 0111
inst.dest = ((b[1] & 0x01) << 4) | ((b[0] & 0xf0) >> 4)
inst.objdump = fmt.Sprintf("%.4x\tror\tr%d\n", b2u16big(b), inst.dest)
return inst
case INSN_SBC:
// 0000 10rd rrrr dddd
inst.source = (((b[1]&0x02)>>1)<<4 | (b[0] & 0x0f))
inst.dest = ((b[1]&0x01)<<4 | ((b[0] & 0xf0) >> 4))
inst.objdump = fmt.Sprintf("%.4x\tsbc\tr%d, r%d\n", b2u16big(b), inst.dest, inst.source)
return inst
case INSN_SBCI:
// 0100 KKKK dddd KKKK
inst.dest = ((b[0] & 0xf0) >> 4) + 0x10
inst.kdata = (b[1]&0x0f)<<4 | (b[0] & 0x0f)
inst.objdump = fmt.Sprintf("%.4x\tsbci\tr%d, 0x%x\n", b2u16big(b), inst.dest, inst.kdata)
return inst
case INSN_SEI:
// 1001 0100 0111 1000
inst.objdump = fmt.Sprintf("%.4x\tsei\n", b2u16big(b))
return inst
case INSN_STDY:
// 1001 001r rrrr 1001
inst.source = ((b[1] & 0x01) << 4) | ((b[0] & 0xf0) >> 4)
inst.offset = m.offset
inst.objdump = fmt.Sprintf("%.4x\tstd\tY+%d, r%d\n", b2u16big(b), inst.offset, inst.source)
return inst
case INSN_STDZ:
// 10q0 qq1r rrrr 0qqq
inst.source = ((b[1] & 0x01) << 4) | ((b[0] & 0xf0) >> 4)
inst.offset = m.offset
inst.objdump = fmt.Sprintf("%.4x\tstd\tZ+%d, r%d\n", b2u16big(b), inst.offset, inst.source)
return inst
case INSN_STX:
// 1001 001r rrrr 1100
inst.source = ((b[1] & 0x01) << 4) | ((b[0] & 0xf0) >> 4)
inst.objdump = fmt.Sprintf("%.4x\tst\tX, r%d\n", b2u16big(b), inst.source)
return inst
case INSN_STXP:
// 1001 001r rrrr 1101
//inst.source = (b2u16little(b) & 0x01f0) >> 4
inst.source = ((b[1] & 0x01) << 4) | ((b[0] & 0xf0) >> 4)
inst.objdump = fmt.Sprintf("%.4x\tst\tX+, r%d\n", b2u16big(b), inst.source)
return inst
case INSN_STXM:
// 1001 001r rrrr 1110
inst.source = ((b[1] & 0x01) << 4) | ((b[0] & 0xf0) >> 4)
inst.objdump = fmt.Sprintf("%.4x\tst\t-X, r%d\n", b2u16big(b), inst.source)
return inst
case INSN_STY:
//1001 001r rrrr 1000
inst.source = ((b[1] & 0x01) << 4) | ((b[0] & 0xf0) >> 4)
inst.objdump = fmt.Sprintf("%.4x\tst\tY, r%d\n", b2u16big(b), inst.source)
return inst
case INSN_STYP:
//1001 001r rrrr 1001
inst.source = ((b[1] & 0x01) << 4) | ((b[0] & 0xf0) >> 4)
inst.objdump = fmt.Sprintf("%.4x\tst\tY+, r%d\n", b2u16big(b), inst.source)
return inst
case INSN_STYM:
//1001 001r rrrr 1010
inst.source = ((b[1] & 0x01) << 4) | ((b[0] & 0xf0) >> 4)
inst.objdump = fmt.Sprintf("%.4x\tst\t-Y, r%d\n", b2u16big(b), inst.source)
return inst
case INSN_STZ:
// 1000 001r rrrr 0000
inst.source = ((b[1] & 0x01) << 4) | ((b[0] & 0xf0) >> 4)
inst.objdump = fmt.Sprintf("%.4x\tst\tZ, r%d\n", b2u16big(b), inst.source)
return inst
case INSN_STZP:
// 1001 001r rrrr 0001
inst.source = ((b[1] & 0x01) << 4) | ((b[0] & 0xf0) >> 4)
inst.objdump = fmt.Sprintf("%.4x\tst\tZ+, r%d\n", b2u16big(b), inst.source)
return inst
case INSN_STZM:
// 1001 001r rrrr 0010
inst.source = ((b[1] & 0x01) << 4) | ((b[0] & 0xf0) >> 4)
inst.objdump = fmt.Sprintf("%.4x\tst\t-Z, r%d\n", b2u16big(b), inst.source)
return inst
case INSN_SUB:
// 0001 10rd dddd rrrr
inst.source = (((b[1] & 0x02) >> 1) << 4) | (b[0] & 0x0f)
inst.dest = ((b[1]&0x01)<<4 | ((b[0] & 0xf0) >> 4))
inst.objdump = fmt.Sprintf("%.4x\tsub\tr%d, r%d\n", b2u16big(b), inst.dest, inst.source)
return inst
case INSN_SUBI:
// 0101 KKKK dddd KKKK
inst.dest = ((b[0] & 0xf0) >> 4) + 0x10
inst.kdata = (b[1]&0x0f)<<4 | (b[0] & 0x0f)
inst.objdump = fmt.Sprintf("%.4x\tsubi\tr%d, 0x%x\n", b2u16big(b), inst.dest, inst.kdata)
return inst
default:
inst.objdump = fmt.Sprintf("None of the above. Got %s (0x%.4x)\n", m.mnemonic, b2u16big(b))
return inst
}
}
func lookUp(raw []byte) OpCode {
var op OpCode
b := b2u16little(raw)
for _, entry := range OpCodeLookUpTable {
v := b & entry.mask
if v == entry.value {
op = entry
switch entry.mnemonic {
case "std":
return deConvoluter(raw, op)
case "ldd":
return deConvoluter(raw, op)
}
return op
} else {
op = OpCode{mnemonic: "unknown", value: b, label: INSN_UNK}
}
}
return op
}
func getOffset(b []byte) uint16 {
o0 := b[1] & 0x20
o1 := (b[1] & 0x0c) << 1
o2 := b[0] & 0x07
o := o0 | o1 | o2
return uint16(o)
}
func deConvoluter(b []byte, op OpCode) OpCode {
x := b2u16little(b) & uint16(0xd208)
offset := getOffset(b)
switch x {
case 0x8000:
if offset == 0 {
op.mnemonic = "ldz"
op.label = INSN_LDZ
} else {
op.mnemonic = "lddz"
op.offset = offset
op.label = INSN_LDDZ
}
case 0x8008:
if offset == 0 {
op.mnemonic = "ldy"
op.label = INSN_LDY
} else {
op.mnemonic = "lddy"
op.offset = offset
op.label = INSN_LDDY
}
case 0x8200:
if offset == 0 {
op.mnemonic = "stz"
op.label = INSN_STZ
} else {
op.mnemonic = "stdz"
op.offset = offset
op.label = INSN_STDZ
}
case 0x8208:
if offset == 0 {
op.mnemonic = "sty"
op.label = INSN_STY
} else {
op.mnemonic = "stdy"
op.offset = offset
op.label = INSN_STDY
}
default:
op.mnemonic = "Unknown"
op.value = b2u16little(b)
}
return op
}