From bc808bd4247e9c1821a4d29fddddf7b42c6d8042 Mon Sep 17 00:00:00 2001
From: AnonymusRaccoon
Date: Tue, 11 Feb 2020 16:30:06 +0100
Subject: [PATCH] Handling the m flag
---
CMakeLists.txt | 2 +-
sources/CPU/CPU.cpp | 5 ++++-
sources/CPU/CPU.hpp | 3 ++-
.../Instructions/MathematicalOperations.cpp | 2 ++
tests/CPU/{testMath.cpp => Math/testADC.cpp} | 22 ++++++++++++++++---
tests/CPU/testAddressingMode.cpp | 9 ++++++++
6 files changed, 37 insertions(+), 6 deletions(-)
rename tests/CPU/{testMath.cpp => Math/testADC.cpp} (82%)
diff --git a/CMakeLists.txt b/CMakeLists.txt
index d8246de..9766372 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -52,7 +52,7 @@ add_executable(unit_tests
sources/CPU/Instructions/CommonInstructions.hpp
sources/Exceptions/InvalidOpcode.hpp
sources/CPU/Instructions/Interrupts.cpp
- sources/CPU/Instructions/MathematicalOperations.cpp tests/CPU/testMath.cpp)
+ sources/CPU/Instructions/MathematicalOperations.cpp tests/CPU/Math/testADC.cpp)
# include criterion & coverage
target_link_libraries(unit_tests criterion -lgcov)
diff --git a/sources/CPU/CPU.cpp b/sources/CPU/CPU.cpp
index e40669f..ab07040 100644
--- a/sources/CPU/CPU.cpp
+++ b/sources/CPU/CPU.cpp
@@ -229,7 +229,10 @@ namespace ComSquare::CPU
uint24_t CPU::_getImmediateAddr()
{
- return this->_registers.pac++;
+ uint24_t effective = this->_registers.pac++;
+ if (this->_registers.p.m)
+ this->_registers.pac++;
+ return effective;
}
uint24_t CPU::_getDirectAddr()
diff --git a/sources/CPU/CPU.hpp b/sources/CPU/CPU.hpp
index 7cd63b2..c28382a 100644
--- a/sources/CPU/CPU.hpp
+++ b/sources/CPU/CPU.hpp
@@ -187,6 +187,7 @@ namespace ComSquare::CPU
enum Instructions
{
BRK = 0x00,
+
ADC_DPXi = 0x61,
ADC_SR = 0x63,
ADC_DP = 0x65,
@@ -201,7 +202,7 @@ namespace ComSquare::CPU
ADC_DPYil = 0x77,
ADC_ABSY = 0x79,
ADC_ABSX = 0x7D,
- ADC_ABSXl = 0x7F
+ ADC_ABSXl = 0x7F,
};
//! @brief The main CPU
diff --git a/sources/CPU/Instructions/MathematicalOperations.cpp b/sources/CPU/Instructions/MathematicalOperations.cpp
index ee1b6c7..364d741 100644
--- a/sources/CPU/Instructions/MathematicalOperations.cpp
+++ b/sources/CPU/Instructions/MathematicalOperations.cpp
@@ -10,6 +10,8 @@ namespace ComSquare::CPU
int CPU::ADC(uint24_t valueAddr)
{
unsigned value = this->_bus->read(valueAddr) + this->_registers.p.c;
+ if (this->_registers.p.m)
+ value += this->_bus->read(valueAddr + 1) << 8u;
unsigned negativeMask = this->_isEmulationMode ? 0xF0u : 0xF000u;
unsigned maxValue = this->_isEmulationMode ? UINT8_MAX : UINT16_MAX;
diff --git a/tests/CPU/testMath.cpp b/tests/CPU/Math/testADC.cpp
similarity index 82%
rename from tests/CPU/testMath.cpp
rename to tests/CPU/Math/testADC.cpp
index 79d5723..6cef599 100644
--- a/tests/CPU/testMath.cpp
+++ b/tests/CPU/Math/testADC.cpp
@@ -5,9 +5,9 @@
#include
#include
#include
-#include "../tests.hpp"
-#include "../../sources/SNES.hpp"
-#include "../../sources/Memory/MemoryBus.hpp"
+#include "../../tests.hpp"
+#include "../../../sources/SNES.hpp"
+#include "../../../sources/Memory/MemoryBus.hpp"
using namespace ComSquare;
Test(ADC, addingOne)
@@ -93,4 +93,20 @@ Test(ADC, signedOverflowEmulated)
cr_assert_eq(pair.second.cpu->_registers.p.v, true, "The overflow flags should 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(ADC, memoryTwoBytes)
+{
+ auto pair = Init();
+ pair.second.cpu->_isEmulationMode = false;
+ pair.second.cpu->_registers.p.m = true;
+ pair.second.cpu->_registers.a = 0x000F;
+ pair.second.wram->_data[0] = 0x01;
+ pair.second.wram->_data[1] = 0x04;
+ pair.second.cpu->ADC(0x0);
+ cr_assert_eq(pair.second.cpu->_registers.a, 0x0410, "The accumulator's value should be 0x0410 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 not be set.");
}
\ No newline at end of file
diff --git a/tests/CPU/testAddressingMode.cpp b/tests/CPU/testAddressingMode.cpp
index a9c6326..e52b695 100644
--- a/tests/CPU/testAddressingMode.cpp
+++ b/tests/CPU/testAddressingMode.cpp
@@ -24,6 +24,15 @@ Test(AddrMode, Immediate)
cr_assert_eq(pair.second.cpu->_registers.pac, 0x000016);
}
+Test(AddrMode, ImmediateMemoryFlag)
+{
+ auto pair = Init();
+ pair.second.cpu->_registers.pac = 0x000015;
+ pair.second.cpu->_registers.p.m = true;
+ cr_assert_eq(pair.second.cpu->_getImmediateAddr(), 0x000015, "Got %x, Expected 0x000015");
+ cr_assert_eq(pair.second.cpu->_registers.pac, 0x000017);
+}
+
Test(AddrMode, ImmediateBankChange)
{
auto pair = Init();