summaryrefslogtreecommitdiff
path: root/src/core.rs
blob: a4c0ce8d642c58af8b28777fe545969d0cd8f25e (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
126
127
128
129
130
131
132
133
134
135
136
use crate::ram::Ram;
use std::fmt;

#[derive(Debug, Clone)]
pub enum Opcode {
    Nop,
    Mov,
    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_MOV_A_DATA: u8 = 0x74;
const OPCODE_MOV_DIR_DATA: u8 = 0x75;
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: Ram,
}

impl Core {
    pub fn with_ram_from_hex(hex: String) -> Result<Self, String> {
        let ram = match Ram::from_hex(hex) {
            Ok(value) => value,
            Err(err_string) => return Err(err_string),
        };
        Ok(Self { pc: 0, ram })
    }

    pub fn step(&mut self) -> u16 {
        let op = self.fetch();
        self.exec(op);
        self.pc
    }

    pub fn op(&self) -> Opcode {
        self.ram.array[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.array[self.pc as usize].opcode();
        let size = self.ram.array[self.pc as usize].opsize();
        if size == 2 {
            operand1 = Some(self.ram.array[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::Mov => (),
            Opcode::Illegal => (),
            Opcode::Push => (),
            Opcode::Pop => (),
            Opcode::Sjmp => self.sjmp(op.operand1.expect("SJMP has no operand given") 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_MOV_A_DATA => (Opcode::Mov,        2, None),
            OPCODE_MOV_DIR_DATA => (Opcode::Mov,        3, 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
    }
}