add code formatting
This commit is contained in:
parent
6e317ee063
commit
d05f2980db
63
src/cpu.rs
63
src/cpu.rs
|
|
@ -1,8 +1,6 @@
|
||||||
use crate::memory::{self, Memory};
|
use crate::memory::Memory;
|
||||||
|
#[derive(Default, Debug)]
|
||||||
#[derive(Default)]
|
pub struct CPU {
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct CPU{
|
|
||||||
pub a: u8,
|
pub a: u8,
|
||||||
pub b: u8,
|
pub b: u8,
|
||||||
pub c: u8,
|
pub c: u8,
|
||||||
|
|
@ -27,8 +25,10 @@ impl CPU {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn mov(&mut self, mem: &mut Memory) {
|
pub fn mov(&mut self, mem: &mut Memory) {
|
||||||
let reg = mem.read(self.pc); self.inc_pc();
|
let reg = mem.read(self.pc);
|
||||||
let val = mem.read(self.pc); self.inc_pc();
|
self.inc_pc();
|
||||||
|
let val = mem.read(self.pc);
|
||||||
|
self.inc_pc();
|
||||||
|
|
||||||
match reg {
|
match reg {
|
||||||
0 => self.a = val,
|
0 => self.a = val,
|
||||||
|
|
@ -41,18 +41,13 @@ impl CPU {
|
||||||
self.zero = val == 0;
|
self.zero = val == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add(&mut self, mem:&mut Memory) {
|
pub fn add(&mut self, mem: &mut Memory) {
|
||||||
|
let dest = mem.read(self.pc);
|
||||||
let dest = mem.read(self.pc); self.pc += 1;
|
self.pc += 1;
|
||||||
let src = mem.read(self.pc); self.pc += 1;
|
let src = mem.read(self.pc);
|
||||||
|
self.pc += 1;
|
||||||
|
|
||||||
let (result, carry) = match (dest, src) {
|
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, 0) => self.a.overflowing_add(self.a),
|
||||||
(0, 1) => self.a.overflowing_add(self.b),
|
(0, 1) => self.a.overflowing_add(self.b),
|
||||||
(0, 2) => self.a.overflowing_add(self.c),
|
(0, 2) => self.a.overflowing_add(self.c),
|
||||||
|
|
@ -89,16 +84,12 @@ impl CPU {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn sub(&mut self, mem: &mut Memory) {
|
pub fn sub(&mut self, mem: &mut Memory) {
|
||||||
let dest = mem.read(self.pc); self.pc += 1;
|
let dest = mem.read(self.pc);
|
||||||
let src = mem.read(self.pc); self.pc += 1;
|
self.pc += 1;
|
||||||
|
let src = mem.read(self.pc);
|
||||||
|
self.pc += 1;
|
||||||
|
|
||||||
let (result, borrow) = match (dest, src) {
|
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, 0) => self.a.overflowing_sub(self.a),
|
||||||
(0, 1) => self.a.overflowing_sub(self.b),
|
(0, 1) => self.a.overflowing_sub(self.b),
|
||||||
(0, 2) => self.a.overflowing_sub(self.c),
|
(0, 2) => self.a.overflowing_sub(self.c),
|
||||||
|
|
@ -135,8 +126,10 @@ impl CPU {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn jmp(&mut self, mem: &mut Memory) {
|
pub fn jmp(&mut self, mem: &mut Memory) {
|
||||||
let low = mem.read(self.pc) as u16; self.inc_pc();
|
let low = mem.read(self.pc) as u16;
|
||||||
let high = mem.read(self.pc) as u16; self.inc_pc();
|
self.inc_pc();
|
||||||
|
let high = mem.read(self.pc) as u16;
|
||||||
|
self.inc_pc();
|
||||||
|
|
||||||
let addrs = (high << 8) | low;
|
let addrs = (high << 8) | low;
|
||||||
|
|
||||||
|
|
@ -144,8 +137,10 @@ impl CPU {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn jz(&mut self, mem: &mut Memory) {
|
pub fn jz(&mut self, mem: &mut Memory) {
|
||||||
let low = mem.read(self.pc) as u16; self.inc_pc();
|
let low = mem.read(self.pc) as u16;
|
||||||
let high = mem.read(self.pc) as u16; self.inc_pc();
|
self.inc_pc();
|
||||||
|
let high = mem.read(self.pc) as u16;
|
||||||
|
self.inc_pc();
|
||||||
|
|
||||||
let addrs = (high << 8) | low;
|
let addrs = (high << 8) | low;
|
||||||
|
|
||||||
|
|
@ -154,9 +149,11 @@ impl CPU {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn jnz(&mut self, mem: &mut Memory) {
|
pub fn jnz(&mut self, mem: &mut Memory) {
|
||||||
let low = mem.read(self.pc) as u16; self.inc_pc();
|
let low = mem.read(self.pc) as u16;
|
||||||
let high = mem.read(self.pc) as u16; self.inc_pc();
|
self.inc_pc();
|
||||||
|
let high = mem.read(self.pc) as u16;
|
||||||
|
self.inc_pc();
|
||||||
|
|
||||||
let addrs = (high << 8) | low;
|
let addrs = (high << 8) | low;
|
||||||
|
|
||||||
|
|
@ -164,6 +161,4 @@ impl CPU {
|
||||||
self.pc = addrs;
|
self.pc = addrs;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
11
src/main.rs
11
src/main.rs
|
|
@ -3,15 +3,13 @@ mod instructions;
|
||||||
mod memory;
|
mod memory;
|
||||||
|
|
||||||
use cpu::CPU;
|
use cpu::CPU;
|
||||||
use memory::Memory;
|
|
||||||
use instructions::Instruction;
|
use instructions::Instruction;
|
||||||
|
use memory::Memory;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let mut cpu = CPU::default();
|
let mut cpu = CPU::default();
|
||||||
let mut mem = Memory::new();
|
let mut mem = Memory::new();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// a = 10
|
// a = 10
|
||||||
mem.write(0x0000, Instruction::MOV as u8);
|
mem.write(0x0000, Instruction::MOV as u8);
|
||||||
mem.write(0x0001, 0);
|
mem.write(0x0001, 0);
|
||||||
|
|
@ -45,17 +43,16 @@ fn main() {
|
||||||
cpu.inc_pc();
|
cpu.inc_pc();
|
||||||
|
|
||||||
match opcode {
|
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::ADD as u8 => cpu.add(&mut mem),
|
||||||
x if x == Instruction::SUB as u8 => cpu.sub(&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::JMP as u8 => cpu.jmp(&mut mem),
|
||||||
x if x == Instruction::JZ as u8 => cpu.jz(&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::JNZ as u8 => cpu.jnz(&mut mem),
|
||||||
x if x == Instruction::HLT as u8 => cpu.halt(),
|
x if x == Instruction::HLT as u8 => cpu.halt(),
|
||||||
_ => panic!("Unknown opcode {:02X}", opcode),
|
_ => panic!("Unknown opcode {:02X}", opcode),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
println!("{:#?}", cpu);
|
println!("{:#?}", cpu);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue