Skip to content

Commit

Permalink
sapemu: Extract sbc logic into separate function
Browse files Browse the repository at this point in the history
The same will soon be done with the similar adc logic.
  • Loading branch information
kleinesfilmroellchen committed Sep 18, 2024
1 parent d40e4c8 commit af097f3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
16 changes: 16 additions & 0 deletions sapemu/src/smp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,22 @@ impl Smp {
self.psw.set(ProgramStatusWord::HalfCarry, half_carry_result);
}

/// Sets all relevant flags of an 8-bit subtraction with carry (or borrow) in, and returns the result.
#[inline]
fn perform_sub_carry(&mut self, op1: u8, op2: u8) -> u8 {
let expanded_result = (op1 as u16).wrapping_add((!op2) as u16) + self.carry() as u16;

Check failure on line 474 in sapemu/src/smp.rs

View workflow job for this annotation

GitHub Actions / clippy

casts from `bool` to `u16` can be expressed infallibly using `From`

error: casts from `bool` to `u16` can be expressed infallibly using `From` --> sapemu/src/smp.rs:474:68 | 474 | let expanded_result = (op1 as u16).wrapping_add((!op2) as u16) + self.carry() as u16; | ^^^^^^^^^^^^^^^^^^^ | = help: an `as` cast can become silently lossy if the types change in the future = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cast_lossless help: use `u16::from` instead | 474 | let expanded_result = (op1 as u16).wrapping_add((!op2) as u16) + u16::from(self.carry()); | ~~~~~~~~~~~~~~~~~~~~~~~

Check failure on line 474 in sapemu/src/smp.rs

View workflow job for this annotation

GitHub Actions / clippy

casts from `u8` to `u16` can be expressed infallibly using `From`

error: casts from `u8` to `u16` can be expressed infallibly using `From` --> sapemu/src/smp.rs:474:51 | 474 | let expanded_result = (op1 as u16).wrapping_add((!op2) as u16) + self.carry() as u16; | ^^^^^^^^^^^^^ | = help: an `as` cast can become silently lossy if the types change in the future = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cast_lossless help: use `u16::from` instead | 474 | let expanded_result = (op1 as u16).wrapping_add(u16::from((!op2))) + self.carry() as u16; | ~~~~~~~~~~~~~~~~~

Check failure on line 474 in sapemu/src/smp.rs

View workflow job for this annotation

GitHub Actions / clippy

casts from `u8` to `u16` can be expressed infallibly using `From`

error: casts from `u8` to `u16` can be expressed infallibly using `From` --> sapemu/src/smp.rs:474:25 | 474 | let expanded_result = (op1 as u16).wrapping_add((!op2) as u16) + self.carry() as u16; | ^^^^^^^^^^^^ | = help: an `as` cast can become silently lossy if the types change in the future = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cast_lossless help: use `u16::from` instead | 474 | let expanded_result = u16::from(op1).wrapping_add((!op2) as u16) + self.carry() as u16; | ~~~~~~~~~~~~~~
trace!("{} + {} + {} = {}", op1, !op2, self.carry(), expanded_result);
let result = (expanded_result & 0xff) as u8;
self.psw.set(ProgramStatusWord::Sign, (result as i8) < 0);
self.psw.set(ProgramStatusWord::Zero, result == 0);
self.psw.set(ProgramStatusWord::Overflow, (op1 as i8).borrowing_sub(op2 as i8, !self.carry()).1);
let half_carry_result = (op1 & 0x0f) + ((!op2) & 0x0f) + self.carry() as u8 >= 0x10;

Check failure on line 480 in sapemu/src/smp.rs

View workflow job for this annotation

GitHub Actions / clippy

casts from `bool` to `u8` can be expressed infallibly using `From`

error: casts from `bool` to `u8` can be expressed infallibly using `From` --> sapemu/src/smp.rs:480:60 | 480 | let half_carry_result = (op1 & 0x0f) + ((!op2) & 0x0f) + self.carry() as u8 >= 0x10; | ^^^^^^^^^^^^^^^^^^ | = help: an `as` cast can become silently lossy if the types change in the future = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cast_lossless help: use `u8::from` instead | 480 | let half_carry_result = (op1 & 0x0f) + ((!op2) & 0x0f) + u8::from(self.carry()) >= 0x10; | ~~~~~~~~~~~~~~~~~~~~~~
self.psw.set(ProgramStatusWord::HalfCarry, half_carry_result);

self.psw.set(ProgramStatusWord::Carry, expanded_result >= 0x100);
result
}

/// Set the interrupt flag.
#[inline]
fn set_interrupt(&mut self, interrupt: bool) {
Expand Down
14 changes: 1 addition & 13 deletions sapemu/src/smp/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2020,19 +2020,7 @@ fn sbc_a_dp(cpu: &mut Smp, memory: &mut Memory, cycle: usize, state: Instruction
},
2 => {
let value = cpu.read(state.address, memory);

let expanded_result = (cpu.a as u16).wrapping_add((!value) as u16) + cpu.carry() as u16;
trace!("{} + {} + {} = {}", cpu.a, !value, cpu.carry(), expanded_result);
let result = (expanded_result & 0xff) as u8;
cpu.psw.set(ProgramStatusWord::Sign, (result as i8) < 0);
cpu.psw.set(ProgramStatusWord::Zero, result == 0);
cpu.psw.set(ProgramStatusWord::Overflow, (cpu.a as i8).borrowing_sub(value as i8, !cpu.carry()).1);
let half_carry_result = (cpu.a & 0x0f) + ((!value) & 0x0f) + cpu.carry() as u8 >= 0x10;
cpu.psw.set(ProgramStatusWord::HalfCarry, half_carry_result);

cpu.psw.set(ProgramStatusWord::Carry, expanded_result >= 0x100);

cpu.a = result;
cpu.a = cpu.perform_sub_carry(cpu.a, value);
MicroArchAction::Next
},
_ => unreachable!(),
Expand Down

0 comments on commit af097f3

Please sign in to comment.