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
use std::fmt;
use crate::bit_utils::BitField;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum OperandSize {
EightBit,
SixteenBit,
ThirtyTwoBit,
Unsized,
}
impl OperandSize {
pub const fn sized(&self) -> bool {
!matches!(self, OperandSize::Unsized)
}
pub const fn value(&self) -> u8 {
match self {
OperandSize::EightBit => 0b00,
OperandSize::SixteenBit => 0b01,
OperandSize::ThirtyTwoBit => 0b10,
OperandSize::Unsized => 0b11,
}
}
}
impl From<u8> for OperandSize {
fn from(opcode: u8) -> Self {
match opcode >> 6 {
0b00 => OperandSize::EightBit,
0b01 => OperandSize::SixteenBit,
0b10 => OperandSize::ThirtyTwoBit,
_ => OperandSize::Unsized,
}
}
}
impl fmt::Display for OperandSize {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mnemonic = match self {
OperandSize::EightBit => ".b",
OperandSize::SixteenBit => ".h",
OperandSize::ThirtyTwoBit => ".w",
OperandSize::Unsized => "",
};
write!(f, "{}", mnemonic)
}
}
pub const fn get_opcode_form(opcode: u8) -> (u8, u8) {
(opcode >> 4 & 0x3, opcode & 0xF)
}
pub const fn build_opcode_form(a: u8, b: u8) -> u8 {
(a & 0x3) << 4 | b & 0xF
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum SubopcodeLocation {
OH,
O1,
O2,
OL,
O3,
O5,
}
impl SubopcodeLocation {
pub(crate) const fn field(&self) -> &BitField<u8> {
match self {
SubopcodeLocation::OH => &bitfields::OH,
SubopcodeLocation::O1 => &bitfields::O1,
SubopcodeLocation::O2 => &bitfields::O2,
SubopcodeLocation::OL => &bitfields::OL,
SubopcodeLocation::O3 => &bitfields::O3,
SubopcodeLocation::O5 => &bitfields::O5,
}
}
}
pub const fn get_subopcode_location(size: u8, a: u8, b: u8) -> Option<SubopcodeLocation> {
match (size, a, b) {
(0x0..=0x2, 0x0, _) => Some(SubopcodeLocation::OH),
(0x0..=0x2, 0x1..=0x2, _) => Some(SubopcodeLocation::O1),
(0x0..=0x2, 0x3, 0x0..=0x1) => Some(SubopcodeLocation::O2),
(0x0..=0x2, 0x3, 0x2) => Some(SubopcodeLocation::O1),
(0x0..=0x2, 0x3, 0x3..=0x4) => Some(SubopcodeLocation::O2),
(0x0..=0x2, 0x3, 0x5) => Some(SubopcodeLocation::O1),
(0x0..=0x2, 0x3, 0x6..=0x7) => Some(SubopcodeLocation::O2),
(0x0..=0x2, 0x3, 0x8) => Some(SubopcodeLocation::O5),
(0x0..=0x2, 0x3, 0x9..=0xC) => Some(SubopcodeLocation::O3),
(0x0..=0x2, 0x3, 0xD) => Some(SubopcodeLocation::O2),
(0x0..=0x2, 0x3, 0xE) => Some(SubopcodeLocation::OH),
(0x0..=0x2, 0x3, 0xF) => Some(SubopcodeLocation::O1),
(0x3, 0x0..=0x2, _) => Some(SubopcodeLocation::O1),
(0x3, 0x3, 0x0..=0x2) => Some(SubopcodeLocation::O2),
(0x3, 0x3, 0x3) => Some(SubopcodeLocation::O1),
(0x3, 0x3, 0x4..=0x5) => Some(SubopcodeLocation::OL),
(0x3, 0x3, 0x6..=0x7) => Some(SubopcodeLocation::O1),
(0x3, 0x3, 0x8..=0x9) => Some(SubopcodeLocation::O2),
(0x3, 0x3, 0xA) => Some(SubopcodeLocation::O3),
(0x3, 0x3, 0xB..=0xC) => Some(SubopcodeLocation::O2),
(0x3, 0x3, 0xD..=0xF) => Some(SubopcodeLocation::O3),
_ => None,
}
}
mod bitfields {
use crate::bit_utils::BitField;
pub const OH: BitField<u8> = BitField::new(6..8, None);
pub const O1: BitField<u8> = BitField::new(0..4, None);
pub const O2: BitField<u8> = BitField::new(8..12, None);
pub const OL: BitField<u8> = BitField::new(8..14, None);
pub const O3: BitField<u8> = BitField::new(16..20, None);
pub const O5: BitField<u8> = BitField::new(32..36, None);
}