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
use core::fmt::{Display, Formatter};
use anyhow::bail;
use crate::regs::Reg;
/// Instruction kinds.
#[derive(Copy, Clone, Debug)]
pub enum InstructionKind {
/// Add with carry.
Add(Reg),
/// Bitwise immediate AND.
And(Reg),
/// Branch always.
BranchAlways,
/// Branch if the carry flag is clear.
BranchCc,
/// Branch if the carry flag is set.
BranchCs,
/// Branch if the zero flag is set.
BranchEq,
/// Branch if the zero flag is clear.
BranchNe,
/// Call a subroutine.
Call,
/// Clear carry flag.
Clc,
/// Compare a register with an immediate operand.
Cmp(Reg),
/// Decrement a register.
Dec(Reg),
/// Decrement a memory location in a register.
DecIndirect,
/// Decrement a memory location.
DecMem,
/// Halt the CPU.
Halt,
/// Increment a register.
Inc(Reg),
/// Increment a memory location in a register.
IncIndirect,
/// Increment a memory location.
IncMem,
/// Far jump.
Jmp,
/// Load a register with an immediate operand.
LdRegImm(Reg),
/// Load a register from an indirect address in another register.
LdRegIndirect,
/// Load a register from another register.
LdRegReg,
/// Logical shift right.
Lsr(Reg),
/// No operation.
Nop,
/// Pop a register value from the stack.
Pop(Reg),
/// Push a register value to the stack.
Push(Reg),
/// Return from a subroutine call.
Ret,
/// Return from a trap.
Rti,
/// Set carry flag.
Sec,
/// Store a register value at an immediate address.
StoreRegDirect(Reg),
/// Store a register value at an indirect address in another register.
StoreRegIndirect,
/// Subtract with carry.
Sub(Reg),
/// Trap with a specified code.
Trap,
}
use InstructionKind::*;
impl Display for InstructionKind {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
write!(
f,
"{}",
match *self {
Add(reg) => format!("add {reg}, N"),
And(reg) => format!("and {reg}, N"),
BranchAlways => "bra D".to_owned(),
BranchCc => "bcc D".to_owned(),
BranchCs => "bcs D".to_owned(),
BranchEq => "beq D".to_owned(),
BranchNe => "bne D".to_owned(),
Call => "call NN".to_owned(),
Clc => "clc".to_owned(),
Cmp(reg) => format!("cmp {reg}, N"),
Dec(reg) => format!("dec {reg}"),
DecIndirect => "dec (RR)".to_owned(),
DecMem => "dec (NN)".to_owned(),
Halt => "halt".to_owned(),
Inc(reg) => format!("inc {reg}"),
IncIndirect => "inc (RR)".to_owned(),
IncMem => "inc (NN)".to_owned(),
Jmp => "jmp NN".to_owned(),
LdRegImm(reg) => format!("ld {reg}, {}", if reg.is16() { "NN" } else { "N" }),
LdRegIndirect => "ld R, (RR)".to_owned(),
LdRegReg => "ld R, R".to_owned(),
Lsr(reg) => format!("lsr {reg}, S"),
Nop => "nop".to_owned(),
Pop(reg) => format!("pop {reg}"),
Push(reg) => format!("push {reg}"),
Ret => "ret".to_owned(),
Rti => "rti".to_owned(),
Sec => "sec".to_owned(),
StoreRegDirect(reg) => format!("ld NN, {reg}"),
StoreRegIndirect => "ld (RR), R".to_owned(),
Sub(reg) => format!("sub {reg}, N"),
Trap => "trap T".to_owned(),
}
)
}
}
impl TryFrom<u8> for InstructionKind {
type Error = anyhow::Error;
fn try_from(opcode: u8) -> Result<Self, Self::Error> {
// Register ID for instructions with X0.. opcodes.
let reg = Reg::try_from(opcode & 0x0F);
// Register ID for instructions with X8..XF opcodes.
let reg2 = Reg::try_from(opcode.wrapping_sub(8) & 0x0F);
Ok(match opcode {
0x00 => Halt,
0x01 => Nop,
0x03 => Sec,
0x04 => Clc,
0x08 => Ret,
0x09 => Rti,
0x10..=0x1C => LdRegImm(reg?),
0x1D => LdRegIndirect,
0x1F => LdRegReg,
0x20..=0x27 => StoreRegDirect(reg?),
0x28 => StoreRegIndirect,
0x30..=0x3C => Inc(reg?),
0x3D => IncIndirect,
0x3E => IncMem,
0x40..=0x4C => Dec(reg?),
0x4D => DecIndirect,
0x4E => DecMem,
0x50..=0x57 => Add(reg?),
0x60..=0x67 => Sub(reg?),
0x70..=0x7B => Cmp(reg?),
0x80..=0x87 => And(reg?),
0xA8..=0xAF => Lsr(reg2?),
0xD0..=0xDB => Push(reg?),
0xE0..=0xEB => Pop(reg?),
0xF0 => BranchAlways,
0xF1 => BranchEq,
0xF2 => BranchNe,
0xF3 => BranchCs,
0xF4 => BranchCc,
0xF7 => Jmp,
0xF8 => Call,
0xF9 => Trap,
_ => bail!("invalid opcode {opcode}"),
})
}
}
impl From<InstructionKind> for u8 {
fn from(ins: InstructionKind) -> Self {
match ins {
Add(reg) => 0x50 | u8::from(reg),
And(reg) => 0x80 | u8::from(reg),
BranchAlways => 0xF0,
BranchCc => 0xF4,
BranchCs => 0xF3,
BranchEq => 0xF1,
BranchNe => 0xF2,
Call => 0xF8,
Clc => 0x04,
Cmp(reg) => 0x70 | u8::from(reg),
Dec(reg) => 0x40 | u8::from(reg),
DecIndirect => 0x4D,
DecMem => 0x4E,
Halt => 0x00,
Jmp => 0xF7,
Inc(reg) => 0x30 | u8::from(reg),
IncIndirect => 0x3D,
IncMem => 0x3E,
LdRegImm(reg) => 0x10 | u8::from(reg),
LdRegIndirect => 0x1D,
LdRegReg => 0x1F,
Lsr(reg) => 0xA8 | u8::from(reg),
Nop => 0x01,
Pop(reg) => 0xE0 | u8::from(reg),
Push(reg) => 0xD0 | u8::from(reg),
Ret => 0x08,
Rti => 0x09,
Sec => 0x03,
StoreRegDirect(reg) => 0x20 | u8::from(reg),
StoreRegIndirect => 0x28,
Sub(reg) => 0x60 | u8::from(reg),
Trap => 0xF9,
}
}
}
impl InstructionKind {
/// Returns the number of operands this instruction takes.
#[must_use]
pub fn operands(&self) -> Operands {
use Operands::*;
match *self {
Clc | Dec(_) | Halt | Inc(_) | Nop | Push(_) | Pop(_) | Ret | Rti | Sec => Zero,
Add(_) | And(_) | BranchAlways | BranchCc | BranchCs | BranchEq | BranchNe
| DecIndirect | IncIndirect | LdRegIndirect | LdRegReg | Lsr(_) | StoreRegIndirect
| Sub(_) | Trap => One,
Cmp(reg) | LdRegImm(reg) => {
if reg.is16() {
Two
} else {
One
}
}
Call | DecMem | IncMem | Jmp | StoreRegDirect(_) => Two,
}
}
}
/// Specifies whether an instruction takes zero, one, or two operands.
#[derive(Clone, Debug, PartialEq)]
pub enum Operands {
/// One operand.
One,
/// Two operands.
Two,
/// Zero operands.
Zero,
}