compact down add instructions

This commit is contained in:
Martin Löffler 2023-01-25 23:57:48 +01:00
parent d186950e99
commit b9c5f33d93
Signed by: FatalErrorCoded
GPG key ID: FFEF368AC076566A
2 changed files with 10 additions and 24 deletions

View file

@ -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;
}

View file

@ -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),
}