From c29389d3eb357ed3665406668ca465d60cf8fb47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20L=C3=B6ffler?= Date: Thu, 26 Jan 2023 03:04:12 +0100 Subject: [PATCH] subtraction --- src/emulator/instructions/arithmetic.rs | 13 +++++++++++++ src/emulator/main.rs | 5 +++++ 2 files changed, 18 insertions(+) diff --git a/src/emulator/instructions/arithmetic.rs b/src/emulator/instructions/arithmetic.rs index f3d5696..b3e001a 100644 --- a/src/emulator/instructions/arithmetic.rs +++ b/src/emulator/instructions/arithmetic.rs @@ -25,6 +25,19 @@ pub fn add(byte: u8, carry: bool, state: &mut EmulatorState) { state.a = result; } +pub fn sub_reg(register: Register, borrow: bool, state: &mut EmulatorState) { + sub(get_register(register, state), borrow, state); +} + +pub fn sub(byte: u8, borrow: bool, state: &mut EmulatorState) { + let (a, first) = state.a.overflowing_sub(byte); + let (result, second) = a.overflowing_sub(borrow as u8); + + state.cc.c = first != second; + set_cc(state, result); + state.a = 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 29ea48f..859423b 100644 --- a/src/emulator/main.rs +++ b/src/emulator/main.rs @@ -116,6 +116,11 @@ fn tick(state: &mut EmulatorState) { 0xc6 => arithmetic::add(next_byte(), false, state), // ADI 0xce => arithmetic::add(next_byte(), state.cc.c, state), // ACI + 0x90..=0x97 => arithmetic::sub_reg(register_from_num(instruction & 0xf), false, state), // SUB + 0x98..=0x9f => arithmetic::sub_reg(register_from_num(instruction & 0xf), state.cc.c, state), // SBB + 0xd6 => arithmetic::sub(next_byte(), false, state), // SUI + 0xde => arithmetic::sub(next_byte(), state.cc.c, state), // SBI + _ => not_implemented(state), }