Krolyxon
Commit d05f298

Branches

Tags

Refs →

bench51d05f298

add code formatting

Author
krolxon
Date
2026-01-04 19:18 +0530
Parent
6e317ee
Commit
d05f2980db4a3bd6cb90dae478560fc15fccb128

+33 −41

2 files changed

  1. M src/cpu.rs +29 −34
  2. M src/main.rs +4 −7
M src/cpu.rs View
@@ -1,8 +1,6 @@
1−use crate::memory::{self, Memory};
2−
3−#[derive(Default)]
4−#[derive(Debug)]
5−pub struct CPU{
1+use crate::memory::Memory;
2+#[derive(Default, Debug)]
3+pub struct CPU {
64 pub a: u8,
75 pub b: u8,
86 pub c: u8,
@@ -27,8 +25,10 @@ impl CPU {
2725 }
2826
2927 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();
28+ let reg = mem.read(self.pc);
29+ self.inc_pc();
30+ let val = mem.read(self.pc);
31+ self.inc_pc();
3232
3333 match reg {
3434 0 => self.a = val,
@@ -41,18 +41,13 @@ impl CPU {
4141 self.zero = val == 0;
4242 }
4343
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;
44+ pub fn add(&mut self, mem: &mut Memory) {
45+ let dest = mem.read(self.pc);
46+ self.pc += 1;
47+ let src = mem.read(self.pc);
48+ self.pc += 1;
4849
4950 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
5651 (0, 0) => self.a.overflowing_add(self.a),
5752 (0, 1) => self.a.overflowing_add(self.b),
5853 (0, 2) => self.a.overflowing_add(self.c),
@@ -89,16 +84,12 @@ impl CPU {
8984 }
9085
9186 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;
87+ let dest = mem.read(self.pc);
88+ self.pc += 1;
89+ let src = mem.read(self.pc);
90+ self.pc += 1;
9491
9592 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
10293 (0, 0) => self.a.overflowing_sub(self.a),
10394 (0, 1) => self.a.overflowing_sub(self.b),
10495 (0, 2) => self.a.overflowing_sub(self.c),
@@ -135,8 +126,10 @@ impl CPU {
135126 }
136127
137128 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();
129+ let low = mem.read(self.pc) as u16;
130+ self.inc_pc();
131+ let high = mem.read(self.pc) as u16;
132+ self.inc_pc();
140133
141134 let addrs = (high << 8) | low;
142135
@@ -144,8 +137,10 @@ impl CPU {
144137 }
145138
146139 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();
140+ let low = mem.read(self.pc) as u16;
141+ self.inc_pc();
142+ let high = mem.read(self.pc) as u16;
143+ self.inc_pc();
149144
150145 let addrs = (high << 8) | low;
151146
@@ -154,9 +149,11 @@ impl CPU {
154149 }
155150 }
156151
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();
152+ pub fn jnz(&mut self, mem: &mut Memory) {
153+ let low = mem.read(self.pc) as u16;
154+ self.inc_pc();
155+ let high = mem.read(self.pc) as u16;
156+ self.inc_pc();
160157
161158 let addrs = (high << 8) | low;
162159
@@ -164,6 +161,4 @@ impl CPU {
164161 self.pc = addrs;
165162 }
166163 }
167−
168−
169164 }
M src/main.rs View
@@ -3,15 +3,13 @@ mod instructions;
33 mod memory;
44
55 use cpu::CPU;
6−use memory::Memory;
76 use instructions::Instruction;
7+use memory::Memory;
88
99 fn main() {
1010 let mut cpu = CPU::default();
1111 let mut mem = Memory::new();
1212
13−
14−
1513 // a = 10
1614 mem.write(0x0000, Instruction::MOV as u8);
1715 mem.write(0x0001, 0);
@@ -45,17 +43,16 @@ fn main() {
4543 cpu.inc_pc();
4644
4745 match opcode {
48− x if x == Instruction::MOV as u8 => cpu.mov(&mut mem),
46+ x if x == Instruction::MOV as u8 => cpu.mov(&mut mem),
4947 x if x == Instruction::ADD as u8 => cpu.add(&mut mem),
5048 x if x == Instruction::SUB as u8 => cpu.sub(&mut mem),
5149 x if x == Instruction::JMP as u8 => cpu.jmp(&mut mem),
52− x if x == Instruction::JZ as u8 => cpu.jz(&mut mem),
53− x if x == Instruction::JNZ as u8 => cpu.jnz(&mut mem),
50+ x if x == Instruction::JZ as u8 => cpu.jz(&mut mem),
51+ x if x == Instruction::JNZ as u8 => cpu.jnz(&mut mem),
5452 x if x == Instruction::HLT as u8 => cpu.halt(),
5553 _ => panic!("Unknown opcode {:02X}", opcode),
5654 }
5755 }
5856
5957 println!("{:#?}", cpu);
60−
6158 }