Krolyxon
Commit 6e317ee

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 self.zero = val == 0;
42 }
43
44 pub fn add(&mut self, mem:&mut Memory) {
45
46 let dest = mem.read(self.pc); self.pc += 1;
47 let src = mem.read(self.pc); self.pc += 1;
48
49 let (result, carry) = match (dest, src) {
50 // What the fuck do these tuples mean?
51 // so basically they are the numbers assigned to register
52 // 0 => A, 1 => B ....
53 // so when it is (0, 0), it basically says add the
54 // value of register B into register A,
55 // thats exactly whats replicated in the code below
56 (0, 0) => self.a.overflowing_add(self.a),
57 (0, 1) => self.a.overflowing_add(self.b),
58 (0, 2) => self.a.overflowing_add(self.c),
59 (0, 3) => self.a.overflowing_add(self.d),
60
61 (1, 0) => self.b.overflowing_add(self.a),
62 (1, 1) => self.b.overflowing_add(self.b),
63 (1, 2) => self.b.overflowing_add(self.c),
64 (1, 3) => self.b.overflowing_add(self.d),
65
66 (2, 0) => self.c.overflowing_add(self.a),
67 (2, 1) => self.c.overflowing_add(self.b),
68 (2, 2) => self.c.overflowing_add(self.c),
69 (2, 3) => self.c.overflowing_add(self.d),
70
71 (3, 0) => self.d.overflowing_add(self.a),
72 (3, 1) => self.d.overflowing_add(self.b),
73 (3, 2) => self.d.overflowing_add(self.c),
74 (3, 3) => self.d.overflowing_add(self.d),
75
76 _ => (0, false),
77 };
78
79 match dest {
80 0 => self.a = result,
81 1 => self.b = result,
82 2 => self.c = result,
83 3 => self.d = result,
84 _ => {}
85 }
86
87 self.zero = result == 0;
88 self.carry = carry;
89 }
90
91 pub fn sub(&mut self, mem: &mut Memory) {
92 let dest = mem.read(self.pc); self.pc += 1;
93 let src = mem.read(self.pc); self.pc += 1;
94
95 let (result, borrow) = match (dest, src) {
96 // What the fuck do these tuples mean?
97 // so basically they are the numbers assigned to register
98 // 0 => A, 1 => B ....
99 // so when it is (0, 0), it basically says add the
100 // value of register B into register A,
101 // thats exactly whats replicated in the code below
102 (0, 0) => self.a.overflowing_sub(self.a),
103 (0, 1) => self.a.overflowing_sub(self.b),
104 (0, 2) => self.a.overflowing_sub(self.c),
105 (0, 3) => self.a.overflowing_sub(self.d),
106
107 (1, 0) => self.b.overflowing_sub(self.a),
108 (1, 1) => self.b.overflowing_sub(self.b),
109 (1, 2) => self.b.overflowing_sub(self.c),
110 (1, 3) => self.b.overflowing_sub(self.d),
111
112 (2, 0) => self.c.overflowing_sub(self.a),
113 (2, 1) => self.c.overflowing_sub(self.b),
114 (2, 2) => self.c.overflowing_sub(self.c),
115 (2, 3) => self.c.overflowing_sub(self.d),
116
117 (3, 0) => self.d.overflowing_sub(self.a),
118 (3, 1) => self.d.overflowing_sub(self.b),
119 (3, 2) => self.d.overflowing_sub(self.c),
120 (3, 3) => self.d.overflowing_sub(self.d),
121
122 _ => (0, false),
123 };
124
125 match dest {
126 0 => self.a = result,
127 1 => self.b = result,
128 2 => self.c = result,
129 3 => self.d = result,
130 _ => {}
131 }
132
133 self.zero = result == 0;
134 self.carry = borrow;
135 }
136
137 pub fn jmp(&mut self, mem: &mut Memory) {
138 let low = mem.read(self.pc) as u16; self.inc_pc();
139 let high = mem.read(self.pc) as u16; self.inc_pc();
140
141 let addrs = (high << 8) | low;
142
143 self.pc = addrs;
144 }
145
146 pub fn jz(&mut self, mem: &mut Memory) {
147 let low = mem.read(self.pc) as u16; self.inc_pc();
148 let high = mem.read(self.pc) as u16; self.inc_pc();
149
150 let addrs = (high << 8) | low;
151
152 if self.zero {
153 self.pc = addrs;
154 }
155 }
156
157 pub fn jnz(&mut self, mem: &mut Memory) {
158 let low = mem.read(self.pc) as u16; self.inc_pc();
159 let high = mem.read(self.pc) as u16; self.inc_pc();
160
161 let addrs = (high << 8) | low;
162
163 if !self.zero {
164 self.pc = addrs;
165 }
166 }
167
168
169}