summaryrefslogtreecommitdiff
path: root/src/core.rs
blob: b51cf80a0812721b5bcfa8f851d19a12c8403961 (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
use std::fmt;

#[derive(Debug, Clone)]
pub enum Opcode {
    Nop,
    Push,
    Pop,
    Illegal,
    Sjmp,
}

#[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;
const OPCODE_SJMP: u8 = 0xD2;

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 {
        Self { 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
    }

    ///
    /// Increment PC
    /// Return the whole operation
    ///
    fn fetch(&mut self) -> Op {
        let mut operand1 = None;
        let opcode = self.ram[self.pc as usize].opcode();
        let size = self.ram[self.pc as usize].opsize();
        if size == 2 {
            operand1 = Some(self.ram[self.pc as usize]);
        }
        self.pc += size as u16;
        Op {
            opcode,
            size,
            operand1,
            operand2: None,
            operand3: None,
        }
    }

    fn exec(&mut self, op: Op) {
        match op.opcode {
            Opcode::Nop => (),
            Opcode::Illegal => (),
            Opcode::Push => (),
            Opcode::Pop => (),
            Opcode::Sjmp => self.sjmp(op.operand1.unwrap() as i8),
        }
    }

    fn sjmp(&mut self, reladdr: i8) {
        self.pc = (self.pc as i16 + reladdr as i16) as u16;
    }
}

type Register = u8;

pub trait Isa8051 {
    fn op(&self) -> (Opcode, usize, Option<Register>);
    fn opcode(&self) -> Opcode;
    fn opsize(&self) -> usize;
    fn opreg(&self) -> Option<Register>;
}

impl Isa8051 for u8 {
    fn op(&self) -> (Opcode, usize, Option<Register>) {
        match *self {
            OPCODE_NOP  => (Opcode::Nop,        1, None),
            OPCODE_PUSH => (Opcode::Push,       2, None),
            OPCODE_POP  => (Opcode::Pop,        2, None),
            OPCODE_SJMP => (Opcode::Sjmp,       2, None),
            _           => (Opcode::Illegal,    1, None),
        }
    }

    fn opcode(&self) -> Opcode {
        self.op().0
    }

    fn opsize(&self) -> usize {
        self.op().1
    }

    fn opreg(&self) -> Option<Register> {
        self.op().2
    }
}