add code formatting

This commit is contained in:
krolxon 2026-01-04 19:18:53 +05:30
parent 6e317ee063
commit d05f2980db
2 changed files with 33 additions and 41 deletions

View File

@ -1,8 +1,6 @@
use crate::memory::{self, Memory};
#[derive(Default)]
#[derive(Debug)]
pub struct CPU{
use crate::memory::Memory;
#[derive(Default, Debug)]
pub struct CPU {
pub a: u8,
pub b: u8,
pub c: u8,
@ -27,8 +25,10 @@ impl CPU {
}
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();
let reg = mem.read(self.pc);
self.inc_pc();
let val = mem.read(self.pc);
self.inc_pc();
match reg {
0 => self.a = val,
@ -41,18 +41,13 @@ impl CPU {
self.zero = val == 0;
}
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;
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),
@ -89,16 +84,12 @@ impl CPU {
}
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 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),
@ -135,8 +126,10 @@ impl CPU {
}
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 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;
@ -144,8 +137,10 @@ impl CPU {
}
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 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;
@ -154,9 +149,11 @@ impl CPU {
}
}
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();
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;
@ -164,6 +161,4 @@ impl CPU {
self.pc = addrs;
}
}
}

View File

@ -3,15 +3,13 @@ mod instructions;
mod memory;
use cpu::CPU;
use memory::Memory;
use instructions::Instruction;
use memory::Memory;
fn main() {
let mut cpu = CPU::default();
let mut mem = Memory::new();
// a = 10
mem.write(0x0000, Instruction::MOV as u8);
mem.write(0x0001, 0);
@ -45,17 +43,16 @@ fn main() {
cpu.inc_pc();
match opcode {
x if x == Instruction::MOV as u8 => cpu.mov(&mut mem),
x if x == Instruction::MOV as u8 => cpu.mov(&mut mem),
x if x == Instruction::ADD as u8 => cpu.add(&mut mem),
x if x == Instruction::SUB as u8 => cpu.sub(&mut mem),
x if x == Instruction::JMP as u8 => cpu.jmp(&mut mem),
x if x == Instruction::JZ as u8 => cpu.jz(&mut mem),
x if x == Instruction::JNZ as u8 => cpu.jnz(&mut mem),
x if x == Instruction::JZ as u8 => cpu.jz(&mut mem),
x if x == Instruction::JNZ as u8 => cpu.jnz(&mut mem),
x if x == Instruction::HLT as u8 => cpu.halt(),
_ => panic!("Unknown opcode {:02X}", opcode),
}
}
println!("{:#?}", cpu);
}