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

#[derive(Debug)]
pub enum Op {
    Nop,
    Illegal,
}

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 {
        self.pc += self.ram[self.pc as usize].instruction_size();
        self.pc
    }

    pub fn op(&self) -> Op {
        self.ram[self.pc as usize].op()
    }
}

pub trait Isa {
    fn op(&self) -> Op;
    fn instruction_size(&self) -> u16;
}

impl Isa for u8 {
    fn op(&self) -> Op {
        match *self {
            0 => Op::Nop,
            _ => Op::Illegal
        }
    }

    fn instruction_size(&self) -> u16 {
        1
    }
}