From 8787b09546cfd11693d228c033fa040eb19e3378 Mon Sep 17 00:00:00 2001 From: AnonymusRaccoon Date: Fri, 14 Feb 2020 16:35:02 +0100 Subject: [PATCH] Implementing the CLC/CLI/CLD/CLV --- sources/CPU/CPU.cpp | 5 +++ sources/CPU/CPU.hpp | 15 ++++++++- .../CPU/Instructions/InternalInstruction.cpp | 20 ++++++++++++ tests/CPU/testInternal.cpp | 32 +++++++++++++++++++ 4 files changed, 71 insertions(+), 1 deletion(-) diff --git a/sources/CPU/CPU.cpp b/sources/CPU/CPU.cpp index b71549d..1a07507 100644 --- a/sources/CPU/CPU.cpp +++ b/sources/CPU/CPU.cpp @@ -301,6 +301,11 @@ namespace ComSquare::CPU case Instructions::JSL: this->JSR(this->_getAbsoluteLongAddr()); return 8; + case Instructions::CLC: this->CLC(); return 2; + case Instructions::CLI: this->CLI(); return 2; + case Instructions::CLD: this->CLD(); return 2; + case Instructions::CLV: this->CLV(); return 2; + default: throw InvalidOpcode("CPU", opcode); } diff --git a/sources/CPU/CPU.hpp b/sources/CPU/CPU.hpp index e992b33..248d812 100644 --- a/sources/CPU/CPU.hpp +++ b/sources/CPU/CPU.hpp @@ -282,7 +282,12 @@ namespace ComSquare::CPU JSR_ABS = 0x20, JSR_ABSXi = 0xFC, - JSL = 0x22 + JSL = 0x22, + + CLC = 0x18, + CLI = 0x58, + CLD = 0xD8, + CLV = 0xB8 }; //! @brief The main CPU @@ -415,6 +420,14 @@ namespace ComSquare::CPU void PLX(); //! @brief Pull the y index register to the stack. void PLY(); + //! @brief Clear the carry flag. + void CLC(); + //! @brief Clear the Interrupt Disable flag. + void CLI(); + //! @brief Clear the decimal flag. + void CLD(); + //! @brief Clear the overflow flag. + void CLV(); public: explicit CPU(std::shared_ptr bus, Cartridge::Header &cartridgeHeader); CPU(const CPU &) = default; diff --git a/sources/CPU/Instructions/InternalInstruction.cpp b/sources/CPU/Instructions/InternalInstruction.cpp index 28d342a..3f2db8b 100644 --- a/sources/CPU/Instructions/InternalInstruction.cpp +++ b/sources/CPU/Instructions/InternalInstruction.cpp @@ -112,4 +112,24 @@ namespace ComSquare::CPU this->_registers.p.z = this->_registers.y == 0; this->_registers.p.n = this->_registers.y & 0x8000u; } + + void CPU::CLC() + { + this->_registers.p.c = false; + } + + void CPU::CLI() + { + this->_registers.p.i = false; + } + + void CPU::CLD() + { + this->_registers.p.d = false; + } + + void CPU::CLV() + { + this->_registers.p.v = false; + } } \ No newline at end of file diff --git a/tests/CPU/testInternal.cpp b/tests/CPU/testInternal.cpp index 4098f47..6f01b28 100644 --- a/tests/CPU/testInternal.cpp +++ b/tests/CPU/testInternal.cpp @@ -405,4 +405,36 @@ Test(PLP, emulation) auto data = pair.second.cpu->_registers.p.flags; cr_assert_eq(data, 0b00110000, "The flags should be 0b00110000 but it was %x", data); cr_assert_eq(pair.second.cpu->_registers.s, 0x1, "The Stack pointer should be equal to 0x1 but it was %x", pair.second.cpu->_registers.s); +} + +Test(CLC, clear) +{ + auto pair = Init(); + pair.second.cpu->_registers.p.flags = 0xFF; + pair.second.cpu->CLC(); + cr_assert_eq(pair.second.cpu->_registers.p.c, false, "The carry flag should not be set"); +} + +Test(CLI, clear) +{ + auto pair = Init(); + pair.second.cpu->_registers.p.flags = 0xFF; + pair.second.cpu->CLI(); + cr_assert_eq(pair.second.cpu->_registers.p.i, false, "The interrupt flag should not be set"); +} + +Test(CLD, clear) +{ + auto pair = Init(); + pair.second.cpu->_registers.p.flags = 0xFF; + pair.second.cpu->CLD(); + cr_assert_eq(pair.second.cpu->_registers.p.d, false, "The decimal flag should not be set"); +} + +Test(CLV, clear) +{ + auto pair = Init(); + pair.second.cpu->_registers.p.flags = 0xFF; + pair.second.cpu->CLV(); + cr_assert_eq(pair.second.cpu->_registers.p.v, false, "The overflow flag should not be set"); } \ No newline at end of file