diff --git a/src/emulator/instructions/arithmetic.rs b/src/emulator/instructions/arithmetic.rs index 58bde33..aaf88ac 100644 --- a/src/emulator/instructions/arithmetic.rs +++ b/src/emulator/instructions/arithmetic.rs @@ -19,30 +19,16 @@ 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) as u16 + state.a as u16; +/// Add values of `register` and `A`, add +1 if carry arg is set (either false or state.cc.c) +pub fn add(register: Register, carry: bool, state: &mut EmulatorState) { + let result = get_register(register, state) as u16 + state.a as u16 + u16::from(carry); set_cc(state, result, 0b1111); state.a = (result & 0xff) as u8; } -/// Add values of input byte and `A` -pub fn adi(byte: u8, state: &mut EmulatorState) { - let result = state.a as u16 + byte as u16; - set_cc(state, result, 0b1111); - state.a = result as u8; -} - -/// 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) as u16 + state.a as u16 + u16::from(state.cc.c); - set_cc(state, result, 0b1111); - state.a = (result & 0xff) as u8; -} - -/// Add values of input byte and `A` and add +1 if carry bit is set -pub fn aci(byte: u8, state: &mut EmulatorState) { - let result = state.a as u16 + byte as u16 + u16::from(state.cc.c); +/// Add values of input byte and `A`, add +1 if carry arg is set (either false or state.cc.c) +pub fn adi(byte: u8, carry: bool, state: &mut EmulatorState) { + let result = state.a as u16 + byte as u16 + u16::from(carry); set_cc(state, result, 0b1111); state.a = result as u8; } diff --git a/src/emulator/main.rs b/src/emulator/main.rs index 277ec20..014dd33 100644 --- a/src/emulator/main.rs +++ b/src/emulator/main.rs @@ -111,10 +111,10 @@ fn tick(state: &mut EmulatorState) { 0x2b => arithmetic::dcx(Register::H, state), 0x3b => arithmetic::dcx(Register::SP, state), - 0x80..=0x87 => arithmetic::add(register_from_num(instruction & 0xf), state), // ADD - 0x88..=0x8f => arithmetic::adc(register_from_num(instruction & 0xf), state), // ADC - 0xc6 => arithmetic::adi(next_byte(), state), // ADI - 0xce => arithmetic::aci(next_byte(), state), // ACI + 0x80..=0x87 => arithmetic::add(register_from_num(instruction & 0xf), false, state), // ADD + 0x88..=0x8f => arithmetic::add(register_from_num(instruction & 0xf), state.cc.c, state), // ADC + 0xc6 => arithmetic::adi(next_byte(), false, state), // ADI + 0xce => arithmetic::adi(next_byte(), state.cc.c, state), // ACI _ => not_implemented(state), }