From 40d7ef1c39dfc510eba5260ba22dabe26c32b57d Mon Sep 17 00:00:00 2001
From: AnonymusRaccoon
Date: Tue, 11 Feb 2020 13:52:44 +0100
Subject: [PATCH] Finishing addressing modes
---
sources/CPU/CPU.cpp | 12 ++++++++++++
sources/CPU/CPU.hpp | 4 ++++
tests/CPU/testAddressingMode.cpp | 24 ++++++++++++++++++++++++
3 files changed, 40 insertions(+)
diff --git a/sources/CPU/CPU.cpp b/sources/CPU/CPU.cpp
index ddb3ea2..d91dc7b 100644
--- a/sources/CPU/CPU.cpp
+++ b/sources/CPU/CPU.cpp
@@ -371,4 +371,16 @@ namespace ComSquare::CPU
effective += this->_bus->read(++dp) << 16u;
return effective;
}
+
+ uint24_t CPU::_getStackRelativeAddr()
+ {
+ return this->_bus->read(this->_registers.pac++) + this->_registers.s;
+ }
+
+ uint24_t CPU::_getStackRelativeIndirectIndexedAddr()
+ {
+ uint24_t base = this->_bus->read(this->_registers.pac++) + this->_registers.s;
+ base += this->_registers.dbr << 16u;
+ return base + this->_registers.y;
+ }
}
\ No newline at end of file
diff --git a/sources/CPU/CPU.hpp b/sources/CPU/CPU.hpp
index d606f8e..8a3c9b8 100644
--- a/sources/CPU/CPU.hpp
+++ b/sources/CPU/CPU.hpp
@@ -230,6 +230,10 @@ namespace ComSquare::CPU
uint24_t _getDirectIndirectAddr();
//! @brief 3 bytes are pulled from the direct page address to form an effective address.
uint24_t _getDirectIndirectLongAddr();
+ //! @brief The stack register is added to the <8-bit exp> to form the effective address.
+ uint24_t _getStackRelativeAddr();
+ //! @brief The <8-bit exp> is added to S and combined with DBR to form the base address. Y is added to the base address to form the effective address.
+ uint24_t _getStackRelativeIndirectIndexedAddr();
//! @brief Execute a single instruction.
diff --git a/tests/CPU/testAddressingMode.cpp b/tests/CPU/testAddressingMode.cpp
index 48ebe3b..b7a5491 100644
--- a/tests/CPU/testAddressingMode.cpp
+++ b/tests/CPU/testAddressingMode.cpp
@@ -257,4 +257,28 @@ Test(AddrMode, DirectIndirectLong)
auto addr = pair.second.cpu->_getDirectIndirectLongAddr();
cr_assert_eq(addr, 0x8801EF, "Returned address was %x but was expecting 0x8801EF.", addr);
cr_assert_eq(pair.second.cpu->_registers.pac, 0x808001);
+}
+
+Test(AddrMode, StackRelative)
+{
+ auto pair = Init();
+ pair.second.cpu->_registers.pac = 0x808000;
+ pair.second.cartridge->_data[0] = 0x06;
+ pair.second.cpu->_registers.s = 0x1010;
+ auto addr = pair.second.cpu->_getStackRelativeAddr();
+ cr_assert_eq(addr, 0x1016, "Returned address was %x but was expecting 0x1016.", addr);
+ cr_assert_eq(pair.second.cpu->_registers.pac, 0x808001);
+}
+
+Test(AddrMode, StackRelativeIndirectIndexed)
+{
+ auto pair = Init();
+ pair.second.cpu->_registers.pac = 0x808000;
+ pair.second.cartridge->_data[0] = 0x06;
+ pair.second.cpu->_registers.s = 0x1010;
+ pair.second.cpu->_registers.y = 0x5;
+ pair.second.cpu->_registers.dbr = 0x88;
+ auto addr = pair.second.cpu->_getStackRelativeIndirectIndexedAddr();
+ cr_assert_eq(addr, 0x88101B, "Returned address was %x but was expecting 0x88101B.", addr);
+ cr_assert_eq(pair.second.cpu->_registers.pac, 0x808001);
}
\ No newline at end of file