summaryrefslogtreecommitdiff
path: root/src/lib.rs
blob: c700094bdf2f69473d57d2ac329bd31190500a70 (plain)
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
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
#[cfg(feature="use-serde")]
#[macro_use] extern crate serde_derive;
#[cfg(feature="use-serde")]
extern crate serde;
//#[cfg(feature="use-serde")]
//use serde::{Serialize, Deserialize};

extern crate yaxpeax_arch;

use yaxpeax_arch::{Arch, AddressDiff, Decoder, LengthedInstruction, Reader, StandardDecodeError, U16le};
use yaxpeax_arch::annotation::{AnnotatingDecoder, DescriptionSink, NullSink};

mod display;
pub use display::NoContext;

#[cfg_attr(feature="use-serde", derive(Serialize, Deserialize))]
#[derive(Debug)]
pub struct MSP430;

impl Arch for MSP430 {
    type Address = u16;
    type Word = U16le;
    type Instruction = Instruction;
    type DecodeError = StandardDecodeError;
    type Decoder = InstDecoder;
    type Operand = Operand;
}

#[derive(Debug, Copy, Clone)]
pub struct Instruction {
    pub opcode: Opcode,
    pub op_width: Width,
    pub operands: [Operand; 2]
}

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum Width {
    W, B
}

impl fmt::Display for Width {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Width::W => {
                f.write_str("w")
            }
            Width::B => {
                f.write_str("b")
            }
        }
    }
}

impl Default for Instruction {
    fn default() -> Instruction {
        Instruction {
            opcode: Opcode::Invalid(0xffff),
            op_width: Width::W,
            operands: [Operand::Nothing, Operand::Nothing]
        }
    }
}

impl LengthedInstruction for Instruction {
    type Unit = AddressDiff<<MSP430 as Arch>::Address>;
    fn min_size() -> Self::Unit {
        AddressDiff::from_const(2)
    }
    fn len(&self) -> Self::Unit {
        let mut size = 2;
        match self.operands[0] {
            Operand::Indexed(_, _) |
            Operand::Symbolic(_) |
            Operand::Immediate(_) |
            Operand::Absolute(_) => { size += 2; },
            _ => {}
        };
        match self.operands[1] {
            Operand::Indexed(_, _) |
            Operand::Symbolic(_) |
            Operand::Immediate(_) |
            Operand::Absolute(_) => { size += 2; },
            _ => {}
        };
        AddressDiff::from_const(size)
    }
}

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum Opcode {
    Invalid(u16),
    RRC,
    SWPB,
    RRA,
    SXT,
    PUSH,
    CALL,
    RETI,
    JNE,
    JEQ,
    JNC,
    JC,
    JN,
    JGE,
    JL,
    JMP,
    MOV,
    ADD,
    ADDC,
    SUBC,
    SUB,
    CMP,
    DADD,
    BIT,
    BIC,
    BIS,
    XOR,
    AND
}

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum Operand {
    Register(u8),
    Indexed(u8, u16),
    RegisterIndirect(u8),
    IndirectAutoinc(u8),
    Symbolic(u16),
    Immediate(u16),
    Absolute(u16),
    Offset(i16),
    Const4,
    Const8,
    Const0,
    Const1,
    Const2,
    ConstNeg1,
    Nothing
}

impl yaxpeax_arch::Instruction for Instruction {
    // TODO: this is wrong!!
    fn well_defined(&self) -> bool { true }
}

#[derive(Debug)]
pub struct InstDecoder {
    flags: u8
}

impl InstDecoder {
    pub fn minimal() -> Self {
        InstDecoder {
            flags: 0
        }
    }

    pub fn with_microcorruption(mut self) -> Self {
        self.flags |= 1;
        self
    }

    pub fn microcorruption_quirks(&self) -> bool {
        (self.flags & 1) != 0
    }
}

impl Default for InstDecoder {
    fn default() -> Self {
        InstDecoder {
            flags: 0xff
        }
    }
}

/*
        let mut bytes_iter = bytes.into_iter();
        let word: Vec<u8> = bytes_iter.by_ref().take(2).collect();

        let fullword = match word[..] {
            [] | [_] => { return Err(DecodeError::ExhaustedInput); },
            [low, high] => (high as u16) << 8 | (low as u16),
            _ => unreachable!()
        };
        */

impl Decoder<MSP430> for InstDecoder {
    fn decode_into<T: Reader<<MSP430 as Arch>::Address, <MSP430 as Arch>::Word>>(&self, inst: &mut Instruction, words: &mut T) -> Result<(), <MSP430 as Arch>::DecodeError> {
        self.decode_with_annotation(inst, words, &mut NullSink)
    }
}

#[derive(Clone, PartialEq, Eq)]
pub struct MSP430FieldDescription {
    desc: Option<&'static str>,
    value: MSP430Field,
    // an opaque identifier to link different parts of FieldDescription at different offsets.
    id: u32,
}

impl yaxpeax_arch::annotation::FieldDescription for MSP430FieldDescription {
    fn id(&self) -> u32 {
        self.id
    }
    fn is_separator(&self) -> bool {
        false
    }
}

#[derive(Clone, PartialEq, Eq)]
pub enum MSP430Field {
    Opcode(Opcode),
    Operand(Operand),
    Width(Width),
    None,
}

use std::fmt;
impl fmt::Display for MSP430FieldDescription {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match &self.value {
            MSP430Field::Opcode(opc) => {
                write!(f, "opcode: {}", opc)?;
            }
            MSP430Field::Operand(operand) => {
                write!(f, "operand: {}", operand)?;
            }
            MSP430Field::Width(width) => {
                write!(f, "width: {}", width)?;
            }
            MSP430Field::None => {
            }
        }

        if let Some(desc) = self.desc {
            if let MSP430Field::None = &self.value {
                // no preceeding text, we can get right into the description
                write!(f, "{}", desc)?;
            } else {
                // we printed something, add a separator for the description
                write!(f, ", {}", desc)?;
            }
        } else {
            // all done, no description to follow
        }

        Ok(())
    }
}

impl AnnotatingDecoder<MSP430> for InstDecoder {
    type FieldDescription = MSP430FieldDescription;

    fn decode_with_annotation<
        T: Reader<<MSP430 as Arch>::Address, <MSP430 as Arch>::Word>,
        S: DescriptionSink<Self::FieldDescription>
    >(&self, inst: &mut Instruction, words: &mut T, sink: &mut S) -> Result<(), <MSP430 as Arch>::DecodeError> {
        let fullword = words.next()?.0;

        fn decode_operand<
            T: Reader<<MSP430 as Arch>::Address, <MSP430 as Arch>::Word>,
            S: DescriptionSink<MSP430FieldDescription>
        >(words: &mut T, sink: &mut S, reg: u8, reg_addr: (u32, u32), mode: u8, mode_addr: (u32, u32), oper: &mut Operand) -> Result<(), StandardDecodeError> {
            let id = reg_addr.0 + 256;
            *oper = match reg {
                0 => {
                    if mode == 0 {
                        sink.record(reg_addr.0, reg_addr.1, MSP430FieldDescription {
                            desc: Some("register number"),
                            value: MSP430Field::Operand(Operand::Register(reg)),
                            id
                        });
                        sink.record(mode_addr.0, mode_addr.1, MSP430FieldDescription {
                            desc: Some("operand mode (register)"),
                            value: MSP430Field::Operand(Operand::Register(reg)),
                            id
                        });
                        Operand::Register(reg)
                    } else if mode == 1 {
                        let disp_start = words.offset() as u32 * 16;
                        let disp = words.next()?.0;
                        sink.record(reg_addr.0, reg_addr.1, MSP430FieldDescription {
                            desc: Some("operand mode (symbolic)"),
                            value: MSP430Field::Operand(Operand::Symbolic(disp)),
                            id
                        });
                        sink.record(mode_addr.0, mode_addr.1, MSP430FieldDescription {
                            desc: Some("operand mode (symbolic)"),
                            value: MSP430Field::Operand(Operand::Symbolic(disp)),
                            id
                        });
                        sink.record(disp_start, disp_start + 15, MSP430FieldDescription {
                            desc: Some("pc-relative offst"),
                            value: MSP430Field::Operand(Operand::Symbolic(disp)),
                            id
                        });
                        Operand::Symbolic(disp)
                    } else if mode == 2 {
                        sink.record(reg_addr.0, reg_addr.1, MSP430FieldDescription {
                            desc: Some("register number"),
                            value: MSP430Field::Operand(Operand::Register(reg)),
                            id
                        });
                        sink.record(mode_addr.0, mode_addr.1, MSP430FieldDescription {
                            desc: Some("operand mode (register)"),
                            value: MSP430Field::Operand(Operand::Register(reg)),
                            id
                        });
                        Operand::RegisterIndirect(reg)
                    } else if mode == 3 {
                        let imm_start = words.offset() as u32 * 16;
                        let imm = words.next()?.0;
                        sink.record(reg_addr.0, reg_addr.1, MSP430FieldDescription {
                            desc: Some("operand mode (immediate)"),
                            value: MSP430Field::Operand(Operand::Immediate(imm)),
                            id
                        });
                        sink.record(mode_addr.0, mode_addr.1, MSP430FieldDescription {
                            desc: Some("operand mode (immediate)"),
                            value: MSP430Field::Operand(Operand::Immediate(imm)),
                            id
                        });
                        sink.record(imm_start, imm_start + 15, MSP430FieldDescription {
                            desc: Some("immediate"),
                            value: MSP430Field::Operand(Operand::Immediate(imm)),
                            id
                        });
                        Operand::Immediate(imm)
                    } else {
                        return Err(StandardDecodeError::InvalidOperand);
                    }
                },
                2 => {
                    match mode {
                        0 => {
                            sink.record(reg_addr.0, reg_addr.1, MSP430FieldDescription {
                                desc: Some("register number"),
                                value: MSP430Field::Operand(Operand::Register(reg)),
                                id
                            });
                            sink.record(mode_addr.0, mode_addr.1, MSP430FieldDescription {
                                desc: Some("operand mode (register)"),
                                value: MSP430Field::Operand(Operand::Register(reg)),
                                id
                            });
                            Operand::Register(reg)
                        },
                        1 => {
                            let abs_start = words.offset() as u32 * 16;
                            let abs = words.next()?.0;
                            sink.record(reg_addr.0, reg_addr.1, MSP430FieldDescription {
                                desc: Some("operand mode (absolute)"),
                                value: MSP430Field::Operand(Operand::Absolute(abs)),
                                id
                            });
                            sink.record(mode_addr.0, mode_addr.1, MSP430FieldDescription {
                                desc: Some("operand mode (absolute)"),
                                value: MSP430Field::Operand(Operand::Absolute(abs)),
                                id
                            });
                            sink.record(abs_start, abs_start + 15, MSP430FieldDescription {
                                desc: Some("absolute address"),
                                value: MSP430Field::Operand(Operand::Absolute(abs)),
                                id
                            });
                            Operand::Absolute(abs)
                        },
                        2 => {
                            sink.record(reg_addr.0, reg_addr.1, MSP430FieldDescription {
                                desc: Some("constant (8)"),
                                value: MSP430Field::Operand(Operand::Const8),
                                id
                            });
                            sink.record(mode_addr.0, mode_addr.1, MSP430FieldDescription {
                                desc: Some("constant (8)"),
                                value: MSP430Field::Operand(Operand::Const8),
                                id
                            });
                            Operand::Const8
                        },
                        3 => {
                            sink.record(reg_addr.0, reg_addr.1, MSP430FieldDescription {
                                desc: Some("constant (4)"),
                                value: MSP430Field::Operand(Operand::Const4),
                                id
                            });
                            sink.record(mode_addr.0, mode_addr.1, MSP430FieldDescription {
                                desc: Some("constant (4)"),
                                value: MSP430Field::Operand(Operand::Const4),
                                id
                            });
                            Operand::Const4
                        },
                        _ => { unreachable!() }
                    }
                },
                3 => {
                    match mode {
                        0 => {
                            sink.record(reg_addr.0, reg_addr.1, MSP430FieldDescription {
                                desc: Some("constant (0)"),
                                value: MSP430Field::Operand(Operand::Const0),
                                id
                            });
                            sink.record(mode_addr.0, mode_addr.1, MSP430FieldDescription {
                                desc: Some("constant (0)"),
                                value: MSP430Field::Operand(Operand::Const0),
                                id
                            });
                            Operand::Const0
                        },
                        1 => {
                            sink.record(reg_addr.0, reg_addr.1, MSP430FieldDescription {
                                desc: Some("constant (1)"),
                                value: MSP430Field::Operand(Operand::Const1),
                                id
                            });
                            sink.record(mode_addr.0, mode_addr.1, MSP430FieldDescription {
                                desc: Some("constant (1)"),
                                value: MSP430Field::Operand(Operand::Const1),
                                id
                            });
                            Operand::Const1
                        },
                        2 => {
                            sink.record(reg_addr.0, reg_addr.1, MSP430FieldDescription {
                                desc: Some("constant (2)"),
                                value: MSP430Field::Operand(Operand::Const2),
                                id
                            });
                            sink.record(mode_addr.0, mode_addr.1, MSP430FieldDescription {
                                desc: Some("constant (2)"),
                                value: MSP430Field::Operand(Operand::Const2),
                                id
                            });
                            Operand::Const2
                        },
                        3 => {
                            sink.record(reg_addr.0, reg_addr.1, MSP430FieldDescription {
                                desc: Some("constant (-1)"),
                                value: MSP430Field::Operand(Operand::ConstNeg1),
                                id
                            });
                            sink.record(mode_addr.0, mode_addr.1, MSP430FieldDescription {
                                desc: Some("constant (-1)"),
                                value: MSP430Field::Operand(Operand::ConstNeg1),
                                id
                            });
                            Operand::ConstNeg1
                        },
                        _ => { unreachable!() }
                    }
                },
                _ => {
                    match mode {
                        0 => {
                            sink.record(reg_addr.0, reg_addr.1, MSP430FieldDescription {
                                desc: Some("register number"),
                                value: MSP430Field::Operand(Operand::Register(reg)),
                                id
                            });
                            sink.record(mode_addr.0, mode_addr.1, MSP430FieldDescription {
                                desc: Some("operand mode (register)"),
                                value: MSP430Field::Operand(Operand::Register(reg)),
                                id
                            });
                            Operand::Register(reg)
                        },
                        1 => {
                            let offset_start = words.offset() as u32 * 16;
                            let offset = words.next()?.0;
                            sink.record(reg_addr.0, reg_addr.1, MSP430FieldDescription {
                                desc: Some("operand mode (indexed)"),
                                value: MSP430Field::Operand(Operand::Indexed(reg, offset)),
                                id
                            });
                            sink.record(mode_addr.0, mode_addr.1, MSP430FieldDescription {
                                desc: Some("operand mode (indexed)"),
                                value: MSP430Field::Operand(Operand::Indexed(reg, offset)),
                                id
                            });
                            sink.record(offset_start, offset_start + 15, MSP430FieldDescription {
                                desc: Some("reg-relative offset"),
                                value: MSP430Field::Operand(Operand::Indexed(reg, offset)),
                                id
                            });
                            Operand::Indexed(reg, offset)
                        },
                        2 => {
                            sink.record(reg_addr.0, reg_addr.1, MSP430FieldDescription {
                                desc: Some("register number"),
                                value: MSP430Field::Operand(Operand::Register(reg)),
                                id
                            });
                            sink.record(mode_addr.0, mode_addr.1, MSP430FieldDescription {
                                desc: Some("operand mode (register indirect)"),
                                value: MSP430Field::Operand(Operand::Register(reg)),
                                id
                            });
                            Operand::RegisterIndirect(reg)
                        },
                        3 => {
                            sink.record(reg_addr.0, reg_addr.1, MSP430FieldDescription {
                                desc: Some("register number"),
                                value: MSP430Field::Operand(Operand::Register(reg)),
                                id
                            });
                            sink.record(mode_addr.0, mode_addr.1, MSP430FieldDescription {
                                desc: Some("operand mode (register indirect, autoinc)"),
                                value: MSP430Field::Operand(Operand::Register(reg)),
                                id
                            });
                            Operand::IndirectAutoinc(reg)
                        },
                        _ => { unreachable!() }
                    }
                }
            };
            return Ok(());
        }

        inst.op_width = Width::W;

        match fullword {
            /*
            instrword if instrword < 0x1000 => {
                // MSP430X instructions go here
                inst.opcode = Opcode::Invalid(instrword);
                inst.operands[0] = Operand::Nothing;
                inst.operands[1] = Operand::Nothing;
                return None;
            }, */
            instrword if instrword < 0x2000 => {
                // microcorruption msp430 is non-standard and accepts invalid instructions..
                if !self.microcorruption_quirks() {
                    sink.record(13, 15, MSP430FieldDescription {
                        desc: Some("unallocated opcode space (error)"),
                        value: MSP430Field::None,
                        id: 0,
                    });
                    return Err(StandardDecodeError::InvalidOpcode);
                } else {
                    sink.record(13, 15, MSP430FieldDescription {
                        desc: Some("unallocated opcode space (microcorruption ignores)"),
                        value: MSP430Field::None,
                        id: 0,
                    });
                }

                let (opcode_idx, operands) = ((instrword & 0x0380) >> 7, instrword & 0x7f);
                match opcode_idx {
                    x if x < 6 => {
                        inst.opcode = [
                            Opcode::RRC,
                            Opcode::SWPB,
                            Opcode::RRA,
                            Opcode::SXT,
                            Opcode::PUSH,
                            Opcode::CALL
                        ][x as usize];
                        sink.record(7, 9, MSP430FieldDescription {
                            desc: None,
                            value: MSP430Field::Opcode(inst.opcode),
                            id: 0,
                        });
                        inst.op_width = if operands & 0b01000000 == 0 {
                            Width::W
                        } else {
                            Width::B
                        };
                        sink.record(6, 6, MSP430FieldDescription {
                            desc: None,
                            value: MSP430Field::Width(inst.op_width),
                            id: 1,
                        });
                        if inst.op_width == Width::B && (x == 1 || x == 3 || x == 5) {
                            sink.record(6, 9, MSP430FieldDescription {
                                desc: Some("swpb, sxt, and call all require width=W (error)"),
                                value: MSP430Field::None,
                                id: 0,
                            });
                            inst.opcode = Opcode::Invalid(instrword);
                            return Err(StandardDecodeError::InvalidOpcode);
                        }

                        #[allow(non_snake_case)]
                        let (As, source) = (
                            ((instrword & 0x0030) >> 4) as u8,
                            (instrword & 0x000f) as u8
                        );
                        decode_operand(words, sink, source, (0, 3), As, (4, 5), &mut inst.operands[0])?;
                        inst.operands[1] = Operand::Nothing;
                        Ok(())
                    },
                    6 => {
                        sink.record(7, 9, MSP430FieldDescription {
                            desc: None,
                            value: MSP430Field::Opcode(Opcode::RETI),
                            id: 0,
                        });
                        if operands == 0 {
                            inst.opcode = Opcode::RETI;
                            inst.operands[0] = Operand::Nothing;
                            inst.operands[1] = Operand::Nothing;
                            Ok(())
                        } else {
                            sink.record(0, 6, MSP430FieldDescription {
                                desc: Some("reti requires all-zero operands (error)"),
                                value: MSP430Field::None,
                                id: 0,
                            });
                            inst.opcode = Opcode::Invalid(instrword);
                            return Err(StandardDecodeError::InvalidOperand);
                        }
                    }
                    7 => {
                        sink.record(7, 9, MSP430FieldDescription {
                            desc: Some("invalid opcode"),
                            value: MSP430Field::None,
                            id: 0,
                        });
                        inst.opcode = Opcode::Invalid(instrword);
                        return Err(StandardDecodeError::InvalidOpcode);
                    }
                    _ => {
                        unreachable!();
                    }
                }
            },
            instrword if instrword < 0x4000 => {
                let (opcode_idx, offset) = ((instrword & 0x1c00) >> 10, instrword & 0x3ff);
                inst.opcode = [
                    Opcode::JNE,
                    Opcode::JEQ,
                    Opcode::JNC,
                    Opcode::JC,
                    Opcode::JN,
                    Opcode::JGE,
                    Opcode::JL,
                    Opcode::JMP
                ][opcode_idx as usize];
                sink.record(13, 15, MSP430FieldDescription {
                    desc: Some("Jcc or JMP"),
                    value: MSP430Field::Opcode(inst.opcode),
                    id: 0,
                });
                sink.record(10, 12, MSP430FieldDescription {
                    desc: None,
                    value: MSP430Field::Opcode(inst.opcode),
                    id: 0,
                });

                let offset = ((offset as i16) << 6) >> 6;
                sink.record(0, 9, MSP430FieldDescription {
                    desc: Some("relative offset (sign-extended to i16)"),
                    value: MSP430Field::Operand(Operand::Offset(offset)),
                    id: 2,
                });
                inst.operands[0] = Operand::Offset(offset);
                inst.operands[1] = Operand::Nothing;
                Ok(())
            },
            instrword @ _ => {
                let (opcode_idx, operands) = ((instrword & 0xf000) >> 12, instrword & 0x0fff);
                inst.opcode = [
                    Opcode::MOV,
                    Opcode::ADD,
                    Opcode::ADDC,
                    Opcode::SUBC,
                    Opcode::SUB,
                    Opcode::CMP,
                    Opcode::DADD,
                    Opcode::BIT,
                    Opcode::BIC,
                    Opcode::BIS,
                    Opcode::XOR,
                    Opcode::AND
                ][(opcode_idx - 4) as usize];
                sink.record(12, 15, MSP430FieldDescription {
                    desc: None,
                    value: MSP430Field::Opcode(inst.opcode),
                    id: 0,
                });

                inst.op_width = if operands & 0b01000000 == 0 { Width::W } else { Width:: B };
                sink.record(6, 6, MSP430FieldDescription {
                    desc: None,
                    value: MSP430Field::Width(inst.op_width),
                    id: 1,
                });

                #[allow(non_snake_case)]
                let (source, Ad, As, dest) = (
                    ((instrword & 0x0f00) >> 8) as u8,
                    ((instrword & 0x0080) >> 7) as u8,
                    ((instrword & 0x0030) >> 4) as u8,
                    (instrword & 0x000f) as u8
                );
                decode_operand(words, sink, source, (8, 11), As, (4, 5), &mut inst.operands[0])?;
                decode_operand(words, sink, dest, (0, 3), Ad, (7, 7), &mut inst.operands[1])?;
                Ok(())
            }
        }
    }
}