diff --git a/src/emulator/instructions/arithmetic.rs b/src/emulator/instructions/arithmetic.rs index e0eaf4a..aa2df4e 100644 --- a/src/emulator/instructions/arithmetic.rs +++ b/src/emulator/instructions/arithmetic.rs @@ -1,21 +1,32 @@ use crate::{EmulatorState, Register, get_register}; -/// Sets the condition code flags according to `result` -fn set_cc(state: &mut EmulatorState, result: u16) { - state.cc.z = (result & 0xff) == 0; - state.cc.s = (result & 0x80) == 1; - state.cc.c = result > 0xff; - state.cc.p = (result & 0xff).count_ones() % 2 == 0; +/// Sets the condition code flags according to `result`. `flags` parameter +/// indicates which flags will be set, 0b1111 will set all (Z, S, C, P) +/// while 0b1000 will only set Z. +fn set_cc(state: &mut EmulatorState, result: u16, flags: u8) { + if flags & 0b1000 > 0 { state.cc.z = (result & 0xff) == 0; } + if flags & 0b0100 > 0 { state.cc.s = (result & 0x80) > 0; } + if flags & 0b0010 > 0 { state.cc.c = result > 0xff; } + if flags & 0b0001 > 0 { state.cc.p = (result & 0xff).count_ones() % 2 == 0; } } +/// Add values of `register` and `A` pub fn add(register: Register, state: &mut EmulatorState) { let result = get_register(register, state) + state.a as u16; - set_cc(state, result); + 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); + 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) + state.a as u16 + if state.cc.c { 1 } else { 0 }; + set_cc(state, result, 0b1111); + state.a = (result & 0xff) as u8; +} diff --git a/src/emulator/main.rs b/src/emulator/main.rs index 624adaf..eed3021 100644 --- a/src/emulator/main.rs +++ b/src/emulator/main.rs @@ -124,6 +124,7 @@ fn tick(state: &mut EmulatorState) { match instruction { 0x00 => {} // NOP 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 _ => not_implemented(state), }