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
use core::fmt::{Display, Formatter};
use anyhow::bail;
use crate::regs::Reg;
/// Instruction kinds.
#[derive(Copy, Clone, Debug)]
pub enum InstructionKind {
/// Add immediate.
Add(Reg),
/// Add register.
AddReg,
/// And immediate.
And(Reg),
/// And register.
AndReg,
/// Arithmetic shift right immediate.
Asr,
/// Arithmetic shift right register.
AsrReg,
/// 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 negative flag is set.
BranchMi,
/// Branch if the zero flag is clear.
BranchNe,
/// Branch if the negative flag is clear.
BranchPl,
/// Call a subroutine.
Call,
/// Clear carry flag.
Clc,
/// Clear interrupt disable flag.
Cli,
/// Compare immediate.
Cmp(Reg),
/// Compare register.
CmpReg,
/// CPU identification.
Cpuid,
/// 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.
Ld(Reg),
/// Load a register from an indirect address in another register, indexed by an
/// immediate value.
LdIndexed,
/// Load a register from an indirect address in another register.
LdIndirect,
/// Load a register from another register.
LdReg,
/// Logical shift right immediate.
Lsr,
/// Logical shift right register.
LsrReg,
/// No operation.
Nop,
/// Or immediate.
Or(Reg),
/// Or register.
OrReg,
/// Pop a register value from the stack.
Pop(Reg),
/// Pop the status register from the stack.
PopPS,
/// Push a register value to the stack.
Push(Reg),
/// Push the status register to the stack.
PushPS,
/// Return from a subroutine call.
Ret,
/// Rotate left immediate.
Rol,
/// Rotate left register.
RolReg,
/// Rotate right immediate.
Ror,
/// Rotate right register.
RorReg,
/// Return from a trap.
Rti,
/// Set carry flag.
Sec,
/// Set interrupt disable flag.
Sei,
/// Shift left immediate.
Shl,
/// Shift left register.
ShlReg,
/// Store a register value at an immediate address.
Store(Reg),
/// Store a register at an indirect address in another register, indexed by an
/// immediate value.
StoreIndexed,
/// Store a register value at an indirect address in another register.
StoreIndirect,
/// Store an immediate value at an indirect address in a register.
StoreIndirectImm,
/// Subtract immediate.
Sub(Reg),
/// Subtract register.
SubReg,
/// Test bits immediate.
Test(Reg),
/// Test bits register.
TestReg,
/// Trap with a specified code.
Trap,
/// Trap on overflow.
TrapV,
/// Exclusive or immediate.
Xor(Reg),
/// Exclusive or register.
XorReg,
}
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"),
AddReg => "add R1, R2".to_owned(),
And(reg) => format!("and {reg}, N"),
AndReg => "and R1, R2".to_owned(),
Asr => "asr R, S".to_owned(),
AsrReg => "asr R1, R2".to_owned(),
BranchAlways => "bra D".to_owned(),
BranchCc => "bcc D".to_owned(),
BranchCs => "bcs D".to_owned(),
BranchEq => "beq D".to_owned(),
BranchMi => "bmi D".to_owned(),
BranchNe => "bne D".to_owned(),
BranchPl => "bpl D".to_owned(),
Call => "call NN".to_owned(),
Clc => "clc".to_owned(),
Cli => "cli".to_owned(),
Cmp(reg) => format!("cmp {reg}, N"),
CmpReg => "cmp R1, R2".to_owned(),
Cpuid => "cpuid".to_owned(),
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(),
LdIndexed => "ld R, (RR+N)".to_owned(),
Ld(reg) => format!("ld {reg}, {}", if reg.is16() { "NN" } else { "N" }),
LdIndirect => "ld R, (RR)".to_owned(),
LdReg => "ld R1, R2".to_owned(),
Lsr => "lsr R, S".to_owned(),
LsrReg => "lsr R1, R2".to_owned(),
Nop => "nop".to_owned(),
Or(reg) => format!("or {reg}, N"),
OrReg => "or R1, R2".to_owned(),
Pop(reg) => format!("pop {reg}"),
PopPS => "pop ps".to_owned(),
Push(reg) => format!("push {reg}"),
PushPS => "push ps".to_owned(),
Ret => "ret".to_owned(),
Rol => "rol R, S".to_owned(),
RolReg => "rol R1, R2".to_owned(),
Ror => "ror R, S".to_owned(),
RorReg => "ror R1, R2".to_owned(),
Rti => "rti".to_owned(),
Sec => "sec".to_owned(),
Sei => "sei".to_owned(),
Shl => "shl R, S".to_owned(),
ShlReg => "shl R1, R2".to_owned(),
Store(reg) => format!("ld NN, {reg}"),
StoreIndexed => "ld (RR+N), R".to_owned(),
StoreIndirect => "ld (RR), R".to_owned(),
StoreIndirectImm => "ld (RR), N".to_owned(),
Sub(reg) => format!("sub {reg}, N"),
SubReg => "sub R1, R2".to_owned(),
Test(reg) => format!("test {reg}, N"),
TestReg => "test R1, R2".to_owned(),
Trap => "trap T".to_owned(),
TrapV => "trapv".to_owned(),
Xor(reg) => format!("xor {reg}, N"),
XorReg => "xor R1, R2".to_owned(),
}
)
}
}
impl TryFrom<u8> for InstructionKind {
type Error = anyhow::Error;
fn try_from(opcode: u8) -> Result<Self, Self::Error> {
let reg = Reg::try_from(opcode & 0x0F);
Ok(match opcode {
0x00 => Halt,
0x01 => Nop,
0x02 => TrapV,
0x03 => Sec,
0x04 => Clc,
0x05 => Sei,
0x06 => Cli,
0x07 => Ret,
0x08 => Rti,
0x10..=0x1C => Ld(reg?),
0x1D => LdIndirect,
0x1E => LdReg,
0x1F => LdIndexed,
0x20..=0x27 => Store(reg?),
0x28 => StoreIndirect,
0x29 => StoreIndirectImm,
0x2F => StoreIndexed,
0x30..=0x3C => Inc(reg?),
0x3D => IncIndirect,
0x3E => IncMem,
0x40..=0x4C => Dec(reg?),
0x4D => DecIndirect,
0x4E => DecMem,
0x50..=0x5C => Add(reg?),
0x5F => AddReg,
0x60..=0x6C => Sub(reg?),
0x6F => SubReg,
0x70..=0x7C => Cmp(reg?),
0x7F => CmpReg,
0x80..=0x8C => And(reg?),
0x8F => AndReg,
0x90..=0x9C => Test(reg?),
0x9F => TestReg,
0xA0..=0xAC => Or(reg?),
0xAF => OrReg,
0xB0..=0xBC => Xor(reg?),
0xBF => XorReg,
0xC0 => Shl,
0xC1 => ShlReg,
0xC2 => Lsr,
0xC3 => LsrReg,
0xC4 => Asr,
0xC5 => AsrReg,
0xC6 => Rol,
0xC7 => RolReg,
0xC8 => Ror,
0xC9 => RorReg,
0xD0..=0xDB => Push(reg?),
0xDC => PushPS,
0xE0..=0xEB => Pop(reg?),
0xEC => PopPS,
0xF0 => BranchAlways,
0xF1 => BranchEq,
0xF2 => BranchNe,
0xF3 => BranchCs,
0xF4 => BranchCc,
0xF5 => BranchMi,
0xF6 => BranchPl,
0xF7 => Jmp,
0xF8 => Call,
0xF9 => Trap,
0xFA => Cpuid,
_ => bail!("invalid opcode {opcode}"),
})
}
}
impl From<InstructionKind> for u8 {
fn from(ins: InstructionKind) -> Self {
match ins {
Add(reg) => 0x50 | u8::from(reg),
AddReg => 0x5F,
And(reg) => 0x80 | u8::from(reg),
AndReg => 0x8F,
Asr => 0xC4,
AsrReg => 0xC5,
BranchAlways => 0xF0,
BranchCc => 0xF4,
BranchCs => 0xF3,
BranchEq => 0xF1,
BranchMi => 0xF5,
BranchNe => 0xF2,
BranchPl => 0xF6,
Call => 0xF8,
Clc => 0x04,
Cli => 0x06,
Cmp(reg) => 0x70 | u8::from(reg),
CmpReg => 0x7F,
Cpuid => 0xFA,
Dec(reg) => 0x40 | u8::from(reg),
DecIndirect => 0x4D,
DecMem => 0x4E,
Halt => 0x00,
Jmp => 0xF7,
Inc(reg) => 0x30 | u8::from(reg),
IncIndirect => 0x3D,
IncMem => 0x3E,
LdIndexed => 0x1F,
Ld(reg) => 0x10 | u8::from(reg),
LdIndirect => 0x1D,
LdReg => 0x1E,
Lsr => 0xC2,
LsrReg => 0xC3,
Nop => 0x01,
Or(reg) => 0xA0 | u8::from(reg),
OrReg => 0xAF,
Pop(reg) => 0xE0 | u8::from(reg),
PopPS => 0xEC,
Push(reg) => 0xD0 | u8::from(reg),
PushPS => 0xDC,
Ret => 0x07,
Rol => 0xC6,
RolReg => 0xC7,
Ror => 0xC8,
RorReg => 0xC9,
Rti => 0x08,
Sec => 0x03,
Sei => 0x05,
Shl => 0xC0,
ShlReg => 0xC1,
Store(reg) => 0x20 | u8::from(reg),
StoreIndexed => 0x2F,
StoreIndirect => 0x28,
StoreIndirectImm => 0x29,
Sub(reg) => 0x60 | u8::from(reg),
SubReg => 0x6F,
Test(reg) => 0x90 | u8::from(reg),
TestReg => 0x9F,
Trap => 0xF9,
TrapV => 0x02,
Xor(reg) => 0xB0 | u8::from(reg),
XorReg => 0xBF,
}
}
}
impl InstructionKind {
/// Returns the number of operands this instruction takes.
#[must_use]
pub fn operands(&self) -> Operands {
use Operands::*;
match *self {
Clc | Cli | Dec(_) | Halt | Inc(_) | Nop | Pop(_) | PopPS | Push(_) | PushPS | Ret
| Rti | Sec | Sei | TrapV => Zero,
AddReg | AndReg | Asr | AsrReg | BranchAlways | BranchCc | BranchCs | BranchEq
| BranchMi | BranchNe | BranchPl | CmpReg | Cpuid | DecIndirect | IncIndirect
| LdIndirect | LdReg | Lsr | LsrReg | OrReg | Rol | RolReg | Ror | RorReg | Shl
| ShlReg | StoreIndirect | SubReg | TestReg | Trap | XorReg => One,
Add(reg) | And(reg) | Cmp(reg) | Ld(reg) | Or(reg) | Sub(reg) | Test(reg)
| Xor(reg) => {
if reg.is16() {
Two
} else {
One
}
}
Call | DecMem | IncMem | Jmp | LdIndexed | Store(_) | StoreIndexed
| StoreIndirectImm => 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,
}