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
|
use std::fmt;
#[derive(Debug, Clone)]
pub enum Opcode {
Nop,
Push,
Pop,
Illegal,
}
#[derive(Debug, Clone)]
pub struct Op {
opcode: Opcode,
size: usize,
operand1: Option<u8>,
operand2: Option<u8>,
operand3: Option<u8>,
}
const OPCODE_NOP: u8 = 0x00;
const OPCODE_PUSH: u8 = 0xC0;
const OPCODE_POP: u8 = 0xD0;
impl fmt::Display for Opcode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:?}", self)
}
}
impl fmt::Display for Op {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:?}", self)
}
}
pub struct Core {
pc: u16,
ram: [u8; u16::max_value() as usize + 1],
}
impl Core {
pub fn new() -> Self {
Core { pc: 0, ram: [0; u16::max_value() as usize + 1]}
}
pub fn step(&mut self) -> u16 {
let op = self.fetch();
self.exec(op);
self.pc
}
pub fn op(&self) -> Opcode {
self.ram[self.pc as usize].op().0
}
fn fetch(&mut self) -> Op {
let (opcode, size) = self.ram[self.pc as usize].op();
self.pc += size as u16;
Op {
opcode,
size,
operand1: None,
operand2: None,
operand3: None,
}
}
fn exec(&mut self, op: Op) -> () {
match op.opcode {
Opcode::Nop => (),
Opcode::Illegal => (),
Opcode::Push => (),
Opcode::Pop => (),
}
}
}
pub trait Isa {
fn op(&self) -> (Opcode, usize);
}
impl Isa for u8 {
fn op(&self) -> (Opcode, usize) {
match *self {
OPCODE_NOP => (Opcode::Nop, 1),
OPCODE_PUSH => (Opcode::Push, 2),
OPCODE_POP => (Opcode::Pop, 2),
_ => (Opcode::Illegal, 1),
}
}
}
|