From f3d3bf470f10a0d3789472df6321f5aae2001b47 Mon Sep 17 00:00:00 2001 From: omarandlorraine <64254276+omarandlorraine@users.noreply.github.com> Date: Thu, 11 Aug 2022 14:42:34 +0100 Subject: [PATCH] add option to statically disable decimal mode (#47) * add the decimal mode feature to `Cargo.toml` * add tests for disabled decimal mode to github workflows * don't run decimal mode tests if decimal mode disabled Co-authored-by: Sam M W --- .github/workflows/ci.yaml | 5 +++++ Cargo.toml | 4 ++++ src/cpu.rs | 12 ++++++++++-- 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index abe3df5..168f58e 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -33,3 +33,8 @@ jobs: uses: actions-rs/cargo@v1 with: command: test + - name: Run tests with disabled decimal mode + uses: actions-rs/cargo@v1 + with: + command: test + args: --no-default-features diff --git a/Cargo.toml b/Cargo.toml index 34ae3e4..f39ae2a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -54,3 +54,7 @@ skeptic = "0.13.7" [dev-dependencies] skeptic = "0.13.7" + +[features] +decimal_mode = [] +default = ["decimal_mode"] diff --git a/src/cpu.rs b/src/cpu.rs index 508f077..e78e698 100644 --- a/src/cpu.rs +++ b/src/cpu.rs @@ -546,12 +546,16 @@ impl CPU { 0x00 }; + #[cfg(feature = "decimal_mode")] let result: i8 = if self.registers.status.contains(Status::PS_DECIMAL_MODE) { a_after.wrapping_add(bcd1).wrapping_add(bcd2) } else { a_after }; + #[cfg(not(feature = "decimal_mode"))] + let result: i8 = a_after; + let did_carry = (result as u8) < (a_before as u8) || (c_before == 1 && value == -1); let did_overflow = (a_before < 0 && value < 0 && a_after >= 0) @@ -619,12 +623,16 @@ impl CPU { 0x00 }; + #[cfg(feature = "decimal_mode")] let result: i8 = if self.registers.status.contains(Status::PS_DECIMAL_MODE) { a_after.wrapping_sub(bcd1).wrapping_sub(bcd2) } else { a_after }; + #[cfg(not(feature = "decimal_mode"))] + let result: i8 = a_after; + // The carry flag is set on unsigned overflow. let did_carry = (result as u8) > (a_before as u8); @@ -794,7 +802,7 @@ mod tests { use super::*; use num::range_inclusive; - #[test] + #[cfg_attr(feature = "decimal_mode", test)] fn decimal_add_test() { let mut cpu = CPU::new(); cpu.registers.status.or(Status::PS_DECIMAL_MODE); @@ -821,7 +829,7 @@ mod tests { assert!(cpu.registers.status.contains(Status::PS_OVERFLOW)); } - #[test] + #[cfg_attr(feature = "decimal_mode", test)] fn decimal_subtract_test() { let mut cpu = CPU::new(); cpu.registers