Skip to content

Commit

Permalink
Refactor Registers
Browse files Browse the repository at this point in the history
  • Loading branch information
choffmann committed Nov 16, 2023
1 parent 65cbc76 commit 6987ff5
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 30 deletions.
1 change: 0 additions & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 8 additions & 26 deletions src/cpu/cpu.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::cpu::instructions::{JumpCondition, Source16Bit, Target16Bit};
use crate::cpu::instructions::{JumpCondition, Target16Bit};
use crate::cpu::registers::{Register8BitName, Registers};
use crate::memory::Memory;

Expand Down Expand Up @@ -41,7 +41,8 @@ impl CPU {
return self.pc.wrapping_add(1);
}
Instruction::LDN(target, source) => {
return self.match_16bit_load(&target, source);
let value = self.register.get_16bit(source.into());
return self.match_16bit_load(&target, value);
}
}
}
Expand All @@ -61,23 +62,12 @@ impl CPU {
return self.pc.wrapping_add(1);
}

fn match_16bit_load(&mut self, target: &Target16Bit, source: Source16Bit) -> u16 {
fn match_16bit_load(&mut self, target: &Target16Bit, value: u16) -> u16 {
match target {
Target16Bit::BC | Target16Bit::DE | Target16Bit::HL | Target16Bit::SP => {
// let value = self.register.get_16bit(source.into());
let value = match source {
Source16Bit::BC |
Source16Bit::DE |
Source16Bit::HL |
Source16Bit::SP => self.register.get_16bit(source.into()),
Source16Bit::NN => {
let least_sign_byte = self.memory.read_byte(self.pc + 1) as u16;
let most_sign_byte = self.memory.read_byte(self.pc + 2) as u16;
(most_sign_byte << 8) | least_sign_byte
}
};
self.register.set_16bit(target.into(), value);
}
Target16Bit::BC => self.register.set_16bit(target.into(), value),
Target16Bit::DE => self.register.set_16bit(target.into(), value),
Target16Bit::HL => self.register.set_16bit(target.into(), value),
Target16Bit::SP => self.register.set_16bit(target.into(), value),
}

return self.pc.wrapping_add(3);
Expand Down Expand Up @@ -371,11 +361,3 @@ impl CPU {
}
}
}

fn print_missing_register(instruction: &str, target: Target8Bit) {
unimplemented!(
"Missing instruction {} for register {:?}",
instruction,
target
);
}
5 changes: 4 additions & 1 deletion src/cpu/instructions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub enum Instruction {
// Load Instructions
LD(Target8Bit, Target8Bit),
LDN(Target16Bit, Source16Bit),
LDD
}

#[derive(Debug)]
Expand All @@ -46,7 +47,7 @@ pub enum Target16Bit {
SP,
}

#[derive(Debug)]
#[derive(Debug, PartialEq, Copy, Clone)]
pub enum Source16Bit {
BC,
DE,
Expand Down Expand Up @@ -333,6 +334,8 @@ impl Instruction {
0x21 => Some(Instruction::LDN(Target16Bit::HL, Source16Bit::NN)),
0x31 => Some(Instruction::LDN(Target16Bit::SP, Source16Bit::NN)),

0x32 => Some(Instruction::LDD),

_ => {
eprintln!("[INS] Missing byte Instruction 0x{:x}", byte);
None
Expand Down
1 change: 1 addition & 0 deletions src/cpu/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod cpu;
pub mod instructions;
pub mod registers;
pub mod arithmetic;
5 changes: 3 additions & 2 deletions src/cpu/registers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ use super::registers::register::Register;
use super::registers::register_8bit::Register8Bit;
use super::registers::stack_pointer::StackPointer;

mod flag_register;
pub(crate) mod flag_register;
mod register;
mod register_8bit;
mod stack_pointer;

#[derive(Clone)]
pub struct Registers {
a: Register8Bit,
b: Register8Bit,
Expand Down Expand Up @@ -104,7 +105,7 @@ impl Registers {
self.a.set(value);
}

pub fn read_a(&mut self) -> u8 {
pub fn read_a(&self) -> u8 {
return self.a.get();
}

Expand Down
1 change: 1 addition & 0 deletions src/cpu/registers/register_8bit.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::register::Register;

#[derive(Clone)]
pub struct Register8Bit {
name: String,
value: u8,
Expand Down
1 change: 1 addition & 0 deletions src/cpu/registers/stack_pointer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::Register;

#[derive(Clone)]
pub struct StackPointer {
name: String,
value: u16
Expand Down
5 changes: 5 additions & 0 deletions src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pub const BOOT_ROM_END: usize = 0xFF;
pub const BOOT_ROM_SIZE: usize = BOOT_ROM_END - BOOT_ROM_BEGIN + 1;


#[derive(Clone)]
pub struct Memory {
pub memory: [u8; 0xFFFF],
}
Expand Down Expand Up @@ -30,4 +31,8 @@ impl Memory {
println!("[MEM] Reading memory: address: 0x{:x} value: 0x{:x}", address, mem_value);
return mem_value;
}

pub fn read_next_word(&self, pc: u16) -> u16 {
((self.read_byte(pc + 2) as u16) << 8) | (self.read_byte(pc + 1) as u16)
}
}

0 comments on commit 6987ff5

Please sign in to comment.