From cb26b0ff77b3d9c6d2317c595641b40f98ed2810 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20L=C3=B6ffler?= Date: Thu, 26 Jan 2023 04:16:02 +0100 Subject: [PATCH] compare instructions --- src/emulator/instructions/arithmetic.rs | 10 ++++++++++ src/emulator/main.rs | 2 ++ 2 files changed, 12 insertions(+) diff --git a/src/emulator/instructions/arithmetic.rs b/src/emulator/instructions/arithmetic.rs index f376bd2..6a5f77f 100644 --- a/src/emulator/instructions/arithmetic.rs +++ b/src/emulator/instructions/arithmetic.rs @@ -57,6 +57,16 @@ bitwise_op!(and, and_reg, &); bitwise_op!(or, or_reg, |); bitwise_op!(xor, xor_reg, ^); +pub fn cmp_reg(register: Register, state: &mut EmulatorState) { + cmp(get_register(register, state), state); +} + +pub fn cmp(byte: u8, state: &mut EmulatorState) { + let (result, carry) = state.a.overflowing_sub(byte); + state.cc.c = carry; + set_cc(state, result); +} + /// Double precision add - Add B&C, D&E or H&L to H&L pub fn dad(register: Register, state: &mut EmulatorState) { let num = get_register_pair(register, state); diff --git a/src/emulator/main.rs b/src/emulator/main.rs index d41dcc5..5cc4584 100644 --- a/src/emulator/main.rs +++ b/src/emulator/main.rs @@ -124,9 +124,11 @@ fn tick(state: &mut EmulatorState) { 0xa0..=0xa7 => arithmetic::and_reg(register_from_num(instruction & 0xf), state), // ANA 0xa8..=0xaf => arithmetic::xor_reg(register_from_num(instruction & 0xf), state), // XRA 0xb0..=0xb7 => arithmetic::or_reg(register_from_num(instruction & 0xf), state), // ORA + 0xb8..=0xbf => arithmetic::cmp_reg(register_from_num(instruction & 0xf), state), // CMP 0xe6 => arithmetic::and(next_byte(), state), // ANI 0xee => arithmetic::xor(next_byte(), state), // XRI 0xf6 => arithmetic::or(next_byte(), state), // ORI + 0xfe => arithmetic::cmp(next_byte(), state), // CPI _ => not_implemented(state), }