From 5be0da1a90232d81344d702217ec961e6911f801 Mon Sep 17 00:00:00 2001 From: Lea Date: Wed, 25 Jan 2023 19:08:00 +0100 Subject: [PATCH] turns out 8 bit processors process 8 bit numbers --- src/emulator/instructions/arithmetic.rs | 12 ++++++------ src/emulator/main.rs | 22 +++++++++++----------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/emulator/instructions/arithmetic.rs b/src/emulator/instructions/arithmetic.rs index 2baf4dd..6af8f5d 100644 --- a/src/emulator/instructions/arithmetic.rs +++ b/src/emulator/instructions/arithmetic.rs @@ -12,7 +12,7 @@ fn set_cc(state: &mut EmulatorState, result: u16, flags: u8) { /// Add values of `register` and `A` pub fn add(register: Register, state: &mut EmulatorState) { - let result = get_register(register, state) + state.a as u16; + let result = get_register(register, state) as u16 + state.a as u16; set_cc(state, result, 0b1111); state.a = (result & 0xff) as u8; } @@ -26,7 +26,7 @@ pub fn adi(byte: u8, state: &mut EmulatorState) { /// Add values of `register` and `A` and add +1 if carry bit is set pub fn adc(register: Register, state: &mut EmulatorState) { - let result = get_register(register, state) + state.a as u16 + if state.cc.c { 1 } else { 0 }; + let result = get_register(register, state) as u16 + state.a as u16 + if state.cc.c { 1 } else { 0 }; set_cc(state, result, 0b1111); state.a = (result & 0xff) as u8; } @@ -41,14 +41,14 @@ pub fn aci(byte: u8, state: &mut EmulatorState) { /// Double precision add - Add B&C, D&E or H&L to H&L pub fn dad(register: Register, state: &mut EmulatorState) { let num = match register { - Register::B => (state.b as u16) << 8 | state.c as u16, - Register::D => (state.d as u16) << 8 | state.e as u16, - Register::H => get_register(Register::M, state), + Register::B => u16::from_le_bytes([state.c, state.b]), + Register::D => u16::from_le_bytes([state.e, state.d]), + Register::H => u16::from_le_bytes([state.l, state.h]), Register::SP => state.sp, _ => panic!("Cannot perform DAD on register {:?}", register), }; - let result = num as u32 + get_register(Register::M, state) as u32; + let result = num as u32 + u16::from_le_bytes([state.l, state.h]) as u32; state.cc.c = result > 0xffff; state.h = (result >> 8) as u8; state.l = result as u8; diff --git a/src/emulator/main.rs b/src/emulator/main.rs index e3b3e43..f123e6c 100644 --- a/src/emulator/main.rs +++ b/src/emulator/main.rs @@ -58,17 +58,17 @@ fn register_from_num(b: u8) -> Register { } } -fn get_register(register: Register, state: &EmulatorState) -> u16 { +fn get_register(register: Register, state: &EmulatorState) -> u8 { match register { - Register::B => state.b as u16, - Register::C => state.c as u16, - Register::D => state.d as u16, - Register::E => state.e as u16, - Register::H => state.h as u16, - Register::L => state.l as u16, - Register::A => state.a as u16, - Register::M => (state.memory[(state.h as usize)] as u16) << 8 | (state.memory[state.l as usize] as u16), - Register::SP => state.sp, + Register::B => state.b as u8, + Register::C => state.c as u8, + Register::D => state.d as u8, + Register::E => state.e as u8, + Register::H => state.h as u8, + Register::L => state.l as u8, + Register::A => state.a as u8, + Register::M => state.memory[u16::from_le_bytes([state.l, state.h]) as usize], + Register::SP => unreachable!(), } } @@ -81,7 +81,7 @@ fn set_register(register: Register, value: u8, state: &mut EmulatorState) { Register::H => state.h = value, Register::L => state.l = value, Register::A => state.a = value, - Register::M => panic!("Cannot set pseudoregister 'M'"), + Register::M => state.memory[u16::from_le_bytes([state.l, state.h]) as usize] = value, Register::SP => panic!("Cannot set 'SP' through set_register()"), }; }