Krolyxon
Commit be61e5a

Branches

Tags

Refs →

bench51srccpu.rs

169 lines · 4.8 KB · Rust

1use crate::memory::{self, Memory};
2
3#[derive(Default)]
4#[derive(Debug)]
5pub struct CPU{
6 pub a: u8,
7 pub b: u8,
8 pub c: u8,
9 pub d: u8,
10
11 pub pc: u16,
12 pub sp: u16,
13
14 pub zero: bool,
15 pub carry: bool,
16
17 pub halted: bool,
18}
19
20impl CPU {
21 pub fn inc_pc(&mut self) {
22 self.pc += 1;
23 }
24
25 pub fn halt(&mut self) {
26 self.halted = true;
27 }
28
29 pub fn mov(&mut self, mem: &mut Memory) {
30 let reg = mem.read(self.pc); self.inc_pc();
31 let val = mem.read(self.pc); self.inc_pc();
32
33 match reg {
34 0 => self.a = val,
35 1 => self.b = val,
36 2 => self.c = val,
37 3 => self.d = val,
38 _ => {}
39 }
40 }
41
42 pub fn add(&mut self, mem:&mut Memory) {
43
44 let dest = mem.read(self.pc); self.pc += 1;
45 let src = mem.read(self.pc); self.pc += 1;
46
47 let (result, carry) = match (dest, src) {
48 // What the fuck do these tuples mean?
49 // so basically they are the numbers assigned to register
50 // 0 => A, 1 => B ....
51 // so when it is (0, 0), it basically says add the
52 // value of register B into register A,
53 // thats exactly whats replicated in the code below
54 (0, 0) => self.a.overflowing_add(self.a),
55 (0, 1) => self.a.overflowing_add(self.b),
56 (0, 2) => self.a.overflowing_add(self.c),
57 (0, 3) => self.a.overflowing_add(self.d),
58
59 (1, 0) => self.b.overflowing_add(self.a),
60 (1, 1) => self.b.overflowing_add(self.b),
61 (1, 2) => self.b.overflowing_add(self.c),
62 (1, 3) => self.b.overflowing_add(self.d),
63
64 (2, 0) => self.c.overflowing_add(self.a),
65 (2, 1) => self.c.overflowing_add(self.b),
66 (2, 2) => self.c.overflowing_add(self.c),
67 (2, 3) => self.c.overflowing_add(self.d),
68
69 (3, 0) => self.d.overflowing_add(self.a),
70 (3, 1) => self.d.overflowing_add(self.b),
71 (3, 2) => self.d.overflowing_add(self.c),
72 (3, 3) => self.d.overflowing_add(self.d),
73
74 _ => (0, false),
75 };
76
77 match dest {
78 0 => self.a = result,
79 1 => self.b = result,
80 2 => self.c = result,
81 3 => self.d = result,
82 _ => {}
83 }
84
85 self.zero = result == 0;
86 self.carry = carry;
87 }
88
89 pub fn sub(&mut self, mem: &mut Memory) {
90 let dest = mem.read(self.pc); self.pc += 1;
91 let src = mem.read(self.pc); self.pc += 1;
92
93 let (result, borrow) = match (dest, src) {
94 // What the fuck do these tuples mean?
95 // so basically they are the numbers assigned to register
96 // 0 => A, 1 => B ....
97 // so when it is (0, 0), it basically says add the
98 // value of register B into register A,
99 // thats exactly whats replicated in the code below
100 (0, 0) => self.a.overflowing_sub(self.a),
101 (0, 1) => self.a.overflowing_sub(self.b),
102 (0, 2) => self.a.overflowing_sub(self.c),
103 (0, 3) => self.a.overflowing_sub(self.d),
104
105 (1, 0) => self.b.overflowing_sub(self.a),
106 (1, 1) => self.b.overflowing_sub(self.b),
107 (1, 2) => self.b.overflowing_sub(self.c),
108 (1, 3) => self.b.overflowing_sub(self.d),
109
110 (2, 0) => self.c.overflowing_sub(self.a),
111 (2, 1) => self.c.overflowing_sub(self.b),
112 (2, 2) => self.c.overflowing_sub(self.c),
113 (2, 3) => self.c.overflowing_sub(self.d),
114
115 (3, 0) => self.d.overflowing_sub(self.a),
116 (3, 1) => self.d.overflowing_sub(self.b),
117 (3, 2) => self.d.overflowing_sub(self.c),
118 (3, 3) => self.d.overflowing_sub(self.d),
119
120 _ => (0, false),
121 };
122
123 match dest {
124 0 => self.a = result,
125 1 => self.b = result,
126 2 => self.c = result,
127 3 => self.d = result,
128 _ => {}
129 }
130
131 self.zero = result == 0;
132 self.carry = borrow;
133 }
134
135 pub fn jmp(&mut self, mem: &mut Memory) {
136 let low = mem.read(self.pc) as u16; self.inc_pc();
137 let high = mem.read(self.pc) as u16; self.inc_pc();
138
139 let addrs = (high << 8) | low;
140
141 self.pc = addrs;
142 }
143
144 pub fn jz(&mut self, mem: &mut Memory) {
145 let low = mem.read(self.pc) as u16; self.inc_pc();
146 let high = mem.read(self.pc) as u16; self.inc_pc();
147
148 let addrs = (high << 8) | low;
149
150 if self.zero {
151 self.pc = addrs;
152 }
153
154 }
155
156 pub fn jnz(&mut self, mem: &mut Memory) {
157 let low = mem.read(self.pc) as u16; self.inc_pc();
158 let high = mem.read(self.pc) as u16; self.inc_pc();
159
160 let addrs = (high << 8) | low;
161
162 if !self.zero {
163 self.pc = addrs;
164 }
165
166 }
167
168
169}