aboutsummaryrefslogtreecommitdiff
path: root/src/cpu.rs
blob: 13d2fc8c4ce20a78d870dd0ce3ae4ed2c2fd9754 (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
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
use crate::instructions::Instruction;
use crate::memory::Memory;

#[derive(Default, Debug)]
pub struct CPU {
    pub a: u8,
    pub b: u8,
    pub c: u8,
    pub d: u8,

    pub pc: u16,
    pub sp: u16,

    pub zero: bool,
    pub carry: bool,

    pub halted: bool,
}

impl CPU {
    pub fn step(&mut self, mem: &mut Memory) {
        let opcode = mem.read(self.pc);
        self.inc_pc();

        match opcode {
            x if x == Instruction::MOV as u8 => self.mov(mem),
            x if x == Instruction::ADD as u8 => self.add(mem),
            x if x == Instruction::SUB as u8 => self.sub(mem),
            x if x == Instruction::JMP as u8 => self.jmp(mem),
            x if x == Instruction::JZ as u8 => self.jz(mem),
            x if x == Instruction::JNZ as u8 => self.jnz(mem),
            x if x == Instruction::HLT as u8 => self.halt(),
            _ => panic!("Unknown opcode {:02X}", opcode),
        }
    }

    pub fn inc_pc(&mut self) {
        self.pc += 1;
    }

    pub fn halt(&mut self) {
        self.halted = true;
    }

    pub fn mov(&mut self, mem: &Memory) {
        let reg = mem.read(self.pc);
        self.inc_pc();
        let val = mem.read(self.pc);
        self.inc_pc();

        match reg {
            0 => self.a = val,
            1 => self.b = val,
            2 => self.c = val,
            3 => self.d = val,
            _ => {}
        }

        self.zero = val == 0;
    }

    pub fn add(&mut self, mem: &Memory) {
        let dest = mem.read(self.pc);
        self.pc += 1;
        let src = mem.read(self.pc);
        self.pc += 1;

        let (result, carry) = match (dest, src) {
            (0, 0) => self.a.overflowing_add(self.a),
            (0, 1) => self.a.overflowing_add(self.b),
            (0, 2) => self.a.overflowing_add(self.c),
            (0, 3) => self.a.overflowing_add(self.d),

            (1, 0) => self.b.overflowing_add(self.a),
            (1, 1) => self.b.overflowing_add(self.b),
            (1, 2) => self.b.overflowing_add(self.c),
            (1, 3) => self.b.overflowing_add(self.d),

            (2, 0) => self.c.overflowing_add(self.a),
            (2, 1) => self.c.overflowing_add(self.b),
            (2, 2) => self.c.overflowing_add(self.c),
            (2, 3) => self.c.overflowing_add(self.d),

            (3, 0) => self.d.overflowing_add(self.a),
            (3, 1) => self.d.overflowing_add(self.b),
            (3, 2) => self.d.overflowing_add(self.c),
            (3, 3) => self.d.overflowing_add(self.d),

            _ => (0, false),
        };

        match dest {
            0 => self.a = result,
            1 => self.b = result,
            2 => self.c = result,
            3 => self.d = result,
            _ => {}
        }

        self.zero = result == 0;
        self.carry = carry;
    }

    pub fn sub(&mut self, mem: &Memory) {
        let dest = mem.read(self.pc);
        self.pc += 1;
        let src = mem.read(self.pc);
        self.pc += 1;

        let (result, borrow) = match (dest, src) {
            (0, 0) => self.a.overflowing_sub(self.a),
            (0, 1) => self.a.overflowing_sub(self.b),
            (0, 2) => self.a.overflowing_sub(self.c),
            (0, 3) => self.a.overflowing_sub(self.d),

            (1, 0) => self.b.overflowing_sub(self.a),
            (1, 1) => self.b.overflowing_sub(self.b),
            (1, 2) => self.b.overflowing_sub(self.c),
            (1, 3) => self.b.overflowing_sub(self.d),

            (2, 0) => self.c.overflowing_sub(self.a),
            (2, 1) => self.c.overflowing_sub(self.b),
            (2, 2) => self.c.overflowing_sub(self.c),
            (2, 3) => self.c.overflowing_sub(self.d),

            (3, 0) => self.d.overflowing_sub(self.a),
            (3, 1) => self.d.overflowing_sub(self.b),
            (3, 2) => self.d.overflowing_sub(self.c),
            (3, 3) => self.d.overflowing_sub(self.d),

            _ => (0, false),
        };

        match dest {
            0 => self.a = result,
            1 => self.b = result,
            2 => self.c = result,
            3 => self.d = result,
            _ => {}
        }

        self.zero = result == 0;
        self.carry = borrow;
    }

    pub fn jmp(&mut self, mem: &Memory) {
        let low = mem.read(self.pc) as u16;
        self.inc_pc();
        let high = mem.read(self.pc) as u16;
        self.inc_pc();

        let addrs = (high << 8) | low;

        self.pc = addrs;
    }

    pub fn jz(&mut self, mem: &Memory) {
        let low = mem.read(self.pc) as u16;
        self.inc_pc();
        let high = mem.read(self.pc) as u16;
        self.inc_pc();

        let addrs = (high << 8) | low;

        if self.zero {
            self.pc = addrs;
        }
    }

    pub fn jnz(&mut self, mem: &Memory) {
        let low = mem.read(self.pc) as u16;
        self.inc_pc();
        let high = mem.read(self.pc) as u16;
        self.inc_pc();

        let addrs = (high << 8) | low;

        if !self.zero {
            self.pc = addrs;
        }
    }
}