From 4394c7625e371196691e8ab217cf64ec90151a6d Mon Sep 17 00:00:00 2001
From: Anonymus Raccoon
Date: Thu, 27 Feb 2020 23:37:52 +0100
Subject: [PATCH] Implementing a SBC without decimal mode
---
CMakeLists.txt | 2 +-
sources/CPU/CPU.cpp | 16 ++++
sources/CPU/CPU.hpp | 18 +++-
.../Instructions/MathematicalOperations.cpp | 15 +++
sources/Debugger/CPUDebug.cpp | 16 ++++
tests/CPU/Math/testADC.cpp | 2 -
tests/CPU/Math/testSBC.cpp | 92 +++++++++++++++++++
7 files changed, 157 insertions(+), 4 deletions(-)
create mode 100644 tests/CPU/Math/testSBC.cpp
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 6a0ca86..a24be7d 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -77,7 +77,7 @@ add_executable(unit_tests
sources/APU/Instructions/Stack.cpp
sources/APU/Instructions/Subroutine.cpp
sources/APU/Instructions/ProgramFlow.cpp
-)
+ tests/CPU/Math/testSBC.cpp)
# include criterion & coverage
target_link_libraries(unit_tests criterion -lgcov)
diff --git a/sources/CPU/CPU.cpp b/sources/CPU/CPU.cpp
index 718066a..07f57b5 100644
--- a/sources/CPU/CPU.cpp
+++ b/sources/CPU/CPU.cpp
@@ -328,6 +328,22 @@ namespace ComSquare::CPU
case Instructions::XCE: this->XCE(); return 2;
+ case Instructions::SBC_IM: this->SBC(this->_getImmediateAddr()); return 2 + !this->_registers.p.m;
+ case Instructions::SBC_ABS: this->SBC(this->_getAbsoluteAddr()); return 4 + !this->_registers.p.m;
+ case Instructions::SBC_ABSl: this->SBC(this->_getAbsoluteLongAddr()); return 5 + !this->_registers.p.m;
+ case Instructions::SBC_DP: this->SBC(this->_getDirectAddr()); return 3 + !this->_registers.p.m + this->_registers.dl != 0;
+ case Instructions::SBC_DPi: this->SBC(this->_getDirectIndirectAddr()); return 5 + !this->_registers.p.m + this->_registers.dl != 0;
+ case Instructions::SBC_DPil: this->SBC(this->_getDirectIndirectLongAddr()); return 6 + !this->_registers.p.m + this->_registers.dl != 0;
+ case Instructions::SBC_ABSX: this->SBC(this->_getAbsoluteIndexedByXAddr()); return 4 + !this->_registers.p.m + this->_hasIndexCrossedPageBoundary;
+ case Instructions::SBC_ABSXl:this->SBC(this->_getAbsoluteIndexedByXLongAddr()); return 5 + !this->_registers.p.m;
+ case Instructions::SBC_ABSY: this->SBC(this->_getAbsoluteIndexedByYAddr()); return 4 + !this->_registers.p.m + this->_hasIndexCrossedPageBoundary;
+ case Instructions::SBC_DPX: this->SBC(this->_getDirectIndexedByXAddr()); return 4 + !this->_registers.p.m + this->_registers.dl != 0;
+ case Instructions::SBC_DPXi: this->SBC(this->_getDirectIndirectIndexedXAddr()); return 6 + !this->_registers.p.m + this->_registers.dl != 0;
+ case Instructions::SBC_DPYi: this->SBC(this->_getDirectIndirectIndexedYAddr()); return 5 + !this->_registers.p.m + this->_registers.dl != 0 + this->_hasIndexCrossedPageBoundary;
+ case Instructions::SBC_DPYil:this->SBC(this->_getDirectIndirectIndexedYLongAddr()); return 6 + !this->_registers.p.m + this->_registers.dl != 0;
+ case Instructions::SBC_SR: this->SBC(this->_getStackRelativeAddr()); return 4 + !this->_registers.p.m;
+ case Instructions::SBC_SRYi: this->SBC(this->_getStackRelativeIndirectIndexedYAddr()); return 7 + !this->_registers.p.m;
+
default:
throw InvalidOpcode("CPU", opcode);
}
diff --git a/sources/CPU/CPU.hpp b/sources/CPU/CPU.hpp
index 23d1f83..710a05d 100644
--- a/sources/CPU/CPU.hpp
+++ b/sources/CPU/CPU.hpp
@@ -310,7 +310,23 @@ namespace ComSquare::CPU
AND_SR = 0x23,
AND_SRYi = 0x33,
- XCE = 0xFB
+ XCE = 0xFB,
+
+ SBC_IM = 0xE9,
+ SBC_ABS = 0xED,
+ SBC_ABSl = 0xEF,
+ SBC_DP = 0xE5,
+ SBC_DPi = 0xF2,
+ SBC_DPil = 0xE7,
+ SBC_ABSX = 0xFD,
+ SBC_ABSXl = 0xFF,
+ SBC_ABSY = 0xF9,
+ SBC_DPX = 0xF5,
+ SBC_DPXi = 0xE1,
+ SBC_DPYi = 0xF1,
+ SBC_DPYil = 0xF7,
+ SBC_SR = 0xE3,
+ SBC_SRYi = 0xF3,
};
//! @brief The main CPU
diff --git a/sources/CPU/Instructions/MathematicalOperations.cpp b/sources/CPU/Instructions/MathematicalOperations.cpp
index 8300f69..f4ec41c 100644
--- a/sources/CPU/Instructions/MathematicalOperations.cpp
+++ b/sources/CPU/Instructions/MathematicalOperations.cpp
@@ -29,6 +29,21 @@ namespace ComSquare::CPU
void CPU::SBC(uint24_t valueAddr)
{
+ unsigned negativeMask = this->_isEmulationMode ? 0x80u : 0x8000u;
+ unsigned value = this->_bus->read(valueAddr);
+ if (this->_registers.p.m)
+ value += this->_bus->read(valueAddr + 1) << 8u;
+ bool oldCarry = this->_registers.p.c;
+ this->_registers.p.c = this->_registers.a >= value;
+ if ((this->_registers.a & negativeMask) == (value & negativeMask))
+ this->_registers.p.v = (this->_registers.a & negativeMask) != ((this->_registers.a + value) & negativeMask);
+ else
+ this->_registers.p.v = false;
+ this->_registers.a += ~value + oldCarry;
+ if (this->_isEmulationMode)
+ this->_registers.a %= 0x100;
+ this->_registers.p.z = this->_registers.a == 0;
+ this->_registers.p.n = this->_registers.a & negativeMask;
}
}
\ No newline at end of file
diff --git a/sources/Debugger/CPUDebug.cpp b/sources/Debugger/CPUDebug.cpp
index 9b4f02f..8b97400 100644
--- a/sources/Debugger/CPUDebug.cpp
+++ b/sources/Debugger/CPUDebug.cpp
@@ -242,6 +242,22 @@ namespace ComSquare::Debugger
case Instructions::XCE: return "XCE";
+ case Instructions::SBC_IM: return "SBC";
+ case Instructions::SBC_ABS: return "SBC";
+ case Instructions::SBC_ABSl: return "SBC";
+ case Instructions::SBC_DP: return "SBC";
+ case Instructions::SBC_DPi: return "SBC";
+ case Instructions::SBC_DPil: return "SBC";
+ case Instructions::SBC_ABSX: return "SBC";
+ case Instructions::SBC_ABSXl:return "SBC";
+ case Instructions::SBC_ABSY: return "SBC";
+ case Instructions::SBC_DPX: return "SBC";
+ case Instructions::SBC_DPXi: return "SBC";
+ case Instructions::SBC_DPYi: return "SBC";
+ case Instructions::SBC_DPYil:return "SBC";
+ case Instructions::SBC_SR: return "SBC";
+ case Instructions::SBC_SRYi: return "SBC";
+
default: return "Unknown";
}
}
diff --git a/tests/CPU/Math/testADC.cpp b/tests/CPU/Math/testADC.cpp
index 4fcc3eb..8623d6d 100644
--- a/tests/CPU/Math/testADC.cpp
+++ b/tests/CPU/Math/testADC.cpp
@@ -3,11 +3,9 @@
//
#include
-#include
#include
#include "../../tests.hpp"
#include "../../../sources/SNES.hpp"
-#include "../../../sources/Memory/MemoryBus.hpp"
using namespace ComSquare;
Test(ADC, addingOne)
diff --git a/tests/CPU/Math/testSBC.cpp b/tests/CPU/Math/testSBC.cpp
new file mode 100644
index 0000000..e9360b0
--- /dev/null
+++ b/tests/CPU/Math/testSBC.cpp
@@ -0,0 +1,92 @@
+//
+// Created by anonymus-raccoon on 27/02/20.
+//
+
+#include
+#include
+#include "../../tests.hpp"
+#include "../../../sources/SNES.hpp"
+using namespace ComSquare;
+
+Test(SBC, removingOne)
+{
+ auto pair = Init();
+ pair.second.cpu->_isEmulationMode = false;
+ pair.second.cpu->_registers.p.c = true;
+ pair.second.cpu->_registers.a = 0x1;
+ pair.second.wram->_data[0] = 0x1;
+ pair.second.cpu->SBC(0x0);
+ cr_assert_eq(pair.second.cpu->_registers.a, 0, "The accumulator's value should be 0x0 but it was 0x%x.", pair.second.cpu->_registers.a);
+ cr_assert_eq(pair.second.cpu->_registers.p.c, true, "The carry flags should be set.");
+ cr_assert_eq(pair.second.cpu->_registers.p.v, false, "The overflow flags should not be set.");
+ cr_assert_eq(pair.second.cpu->_registers.p.n, false, "The negative flags should not be set.");
+ cr_assert_eq(pair.second.cpu->_registers.p.z, true, "The zero flags should be set.");
+}
+
+Test(SBC, legitOverflowWithCarry)
+{
+ auto pair = Init();
+ pair.second.cpu->_isEmulationMode = false;
+ pair.second.cpu->_registers.a = 0x1;
+ pair.second.cpu->_registers.p.m = true;
+ pair.second.cpu->_registers.p.c = true;
+ pair.second.wram->_data[0] = 0x03;
+ pair.second.wram->_data[1] = 0x20;
+ pair.second.cpu->SBC(0x0);
+ cr_assert_eq(pair.second.cpu->_registers.a, 0xDFFE, "The accumulator's value should be 0xDFFE but it was 0x%x.", pair.second.cpu->_registers.a);
+ cr_assert_eq(pair.second.cpu->_registers.p.c, false, "The carry flags should not be set.");
+ cr_assert_eq(pair.second.cpu->_registers.p.v, false, "The overflow flags should not be set.");
+ cr_assert_eq(pair.second.cpu->_registers.p.n, true, "The negative flags should be set.");
+ cr_assert_eq(pair.second.cpu->_registers.p.z, false, "The zero flags should not be set.");
+}
+
+Test(SBC, overflowWithCarry)
+{
+ auto pair = Init();
+ pair.second.cpu->_isEmulationMode = false;
+ pair.second.cpu->_registers.a = 0x1;
+ pair.second.cpu->_registers.p.m = true;
+ pair.second.cpu->_registers.p.c = true;
+ pair.second.wram->_data[0] = 0x03;
+ pair.second.wram->_data[1] = 0x20;
+ pair.second.cpu->SBC(0x0);
+ cr_assert_eq(pair.second.cpu->_registers.a, 0xDFFE, "The accumulator's value should be 0xDFFE but it was 0x%x.", pair.second.cpu->_registers.a);
+ cr_assert_eq(pair.second.cpu->_registers.p.c, false, "The carry flags should not be set.");
+ cr_assert_eq(pair.second.cpu->_registers.p.v, false, "The overflow flags should be not set.");
+ cr_assert_eq(pair.second.cpu->_registers.p.n, true, "The negative flags should be set.");
+ cr_assert_eq(pair.second.cpu->_registers.p.z, false, "The zero flags should not be set.");
+}
+
+Test(SBC, overflowEmulation)
+{
+ auto pair = Init();
+ pair.second.cpu->_isEmulationMode = true;
+ pair.second.cpu->_registers.a = 0x1;
+ pair.second.cpu->_registers.p.m = false;
+ pair.second.cpu->_registers.p.c = false;
+ pair.second.wram->_data[0] = 0x02;
+ pair.second.cpu->SBC(0x0);
+ cr_assert_eq(pair.second.cpu->_registers.a, 0xFE, "The accumulator's value should be 0xFE but it was 0x%x.", pair.second.cpu->_registers.a);
+ cr_assert_eq(pair.second.cpu->_registers.p.c, false, "The carry flags should not be set.");
+ cr_assert_eq(pair.second.cpu->_registers.p.v, false, "The overflow flags should not be set.");
+ cr_assert_eq(pair.second.cpu->_registers.p.n, true, "The negative flags should be set.");
+ cr_assert_eq(pair.second.cpu->_registers.p.z, false, "The zero flags should be not set.");
+}
+
+
+Test(SBC, decimal)
+{
+ auto pair = Init();
+ pair.second.cpu->_isEmulationMode = true;
+ pair.second.cpu->_registers.a = 0x1;
+ pair.second.cpu->_registers.p.d = true;
+ pair.second.cpu->_registers.p.m = true;
+ pair.second.wram->_data[0] = 0x03;
+ pair.second.wram->_data[1] = 0x20;
+ pair.second.cpu->SBC(0x0);
+ cr_assert_eq(pair.second.cpu->_registers.a, 0x7998, "The accumulator's value should be 0x7998 but it was 0x%x.", pair.second.cpu->_registers.a);
+ cr_assert_eq(pair.second.cpu->_registers.p.c, false, "The carry flags should not be set.");
+ cr_assert_eq(pair.second.cpu->_registers.p.v, false, "The overflow flags should not be set.");
+ cr_assert_eq(pair.second.cpu->_registers.p.n, false, "The negative flags should not be set.");
+ cr_assert_eq(pair.second.cpu->_registers.p.z, false, "The zero flags should be not set.");
+}