aboutsummaryrefslogtreecommitdiff
path: root/src/cpu.rs
blob: 124d39130485e2368c05494a30a365e346a04cf3 (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
use crate::memory::{self, Memory};

#[derive(Default)]
#[derive(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 inc_pc(&mut self) {
        self.pc += 1;
    }

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

    pub fn mov(&mut self, mem: &mut 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,
            _ => {}
        }
    }

    pub fn add(&mut self, mem:&mut 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) {
            // What the fuck do these tuples mean?
            // so basically they are the numbers assigned to register
            // 0 => A, 1 => B ....
            // so when it is (0, 0), it basically says add the
            // value of register B into register A,
            // thats exactly whats replicated in the code below
            (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: &mut 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) {
            // What the fuck do these tuples mean?
            // so basically they are the numbers assigned to register
            // 0 => A, 1 => B ....
            // so when it is (0, 0), it basically says add the
            // value of register B into register A,
            // thats exactly whats replicated in the code below
            (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: &mut 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: &mut 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: &mut 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;
        }

    }


}