From c15ec0a9cb57ec578f60aa44a9cbc4067dbea16c Mon Sep 17 00:00:00 2001 From: AnonymusRaccoon Date: Fri, 28 Feb 2020 20:25:34 +0100 Subject: [PATCH] Starting the jump --- sources/CPU/CPU.cpp | 7 +++++++ sources/CPU/CPU.hpp | 13 +++++++++++- .../CPU/Instructions/InternalInstruction.cpp | 17 +++++++++++++++ sources/Debugger/CPUDebug.cpp | 7 +++++++ sources/PPU/PPU.cpp | 5 ----- tests/CPU/testInternal.cpp | 21 +++++++++++++++++++ 6 files changed, 64 insertions(+), 6 deletions(-) diff --git a/sources/CPU/CPU.cpp b/sources/CPU/CPU.cpp index ca1a0b3..e7f0903 100644 --- a/sources/CPU/CPU.cpp +++ b/sources/CPU/CPU.cpp @@ -370,6 +370,13 @@ namespace ComSquare::CPU case Instructions::BRA: this->BRA(this->_registers.pc++); return 3 + this->_isEmulationMode; case Instructions::BRL: this->BRL(this->_registers.pc); this->_registers.pc += 2; return 4; + case Instructions::JMP_ABS: this->JMP(this->_getAbsoluteAddr()); return 3; + case Instructions::JMP_ABSi: this->JMP(this->_getAbsoluteIndirectAddr()); return 3; + case Instructions::JMP_ABSXi: this->JMP(this->_getAbsoluteIndexedByXAddr()); return 3; + + case Instructions::JML_ABSl: this->JML(this->_getAbsoluteLongAddr()); return 3; + //case Instructions::JML_ABSil: this->JML(this->_getAbsoluteLong()); return 3; + default: throw InvalidOpcode("CPU", opcode); } diff --git a/sources/CPU/CPU.hpp b/sources/CPU/CPU.hpp index ab2d913..b3cc6cf 100644 --- a/sources/CPU/CPU.hpp +++ b/sources/CPU/CPU.hpp @@ -352,7 +352,14 @@ namespace ComSquare::CPU BVC = 0x50, BVS = 0x70, BRA = 0x80, - BRL = 0x82 + BRL = 0x82, + + JMP_ABS = 0x4C, + JMP_ABSi = 0x6C, + JMP_ABSXi = 0x7C, + + JML_ABSl = 0x5C, + JML_ABSil = 0xDC }; //! @brief The main CPU @@ -540,6 +547,10 @@ namespace ComSquare::CPU bool BRA(uint24_t valueAddr); //! @brief Branch always long bool BRL(uint24_t valueAddr); + //! @brief Jump. + void JMP(uint24_t valueAddr); + //! @brief Long jump. + void JML(uint24_t valueAddr); 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 abc1d6b..16b426b 100644 --- a/sources/CPU/Instructions/InternalInstruction.cpp +++ b/sources/CPU/Instructions/InternalInstruction.cpp @@ -297,4 +297,21 @@ namespace ComSquare::CPU this->_registers.pac += static_cast(this->_bus->read(valueAddr)); return this->_registers.p.v; } + + void CPU::JMP(uint24_t valueAddr) + { + unsigned value = this->_bus->read(valueAddr); + value += this->_bus->read(valueAddr + 1) << 8u; + + this->_registers.pc = value; + } + + void CPU::JML(uint24_t valueAddr) + { + unsigned value = this->_bus->read(valueAddr); + value += this->_bus->read(valueAddr + 1) << 8u; + value += this->_bus->read(valueAddr + 2) << 16u; + + this->_registers.pac = value; + } } \ No newline at end of file diff --git a/sources/Debugger/CPUDebug.cpp b/sources/Debugger/CPUDebug.cpp index 613e0df..bb80fe6 100644 --- a/sources/Debugger/CPUDebug.cpp +++ b/sources/Debugger/CPUDebug.cpp @@ -366,6 +366,13 @@ namespace ComSquare::Debugger case Instructions::BRA: return "BRA " + this->_getImmediateValue8Bits(pc); case Instructions::BRL: return "BRL " + this->_getImmediateValue16Bits(pc); + case Instructions::JMP_ABS: return "JMP " + this->_getAbsoluteValue(pc); + case Instructions::JMP_ABSi: return "JMP "; //+ this->_getAbsoluteIndire(pc); + case Instructions::JMP_ABSXi: return "JMP "; //+ this->_getAbsoluteValue(pc); + + case Instructions::JML_ABSl: return "JML"; + case Instructions::JML_ABSil: return "JML"; + default: return "Unknown"; } } diff --git a/sources/PPU/PPU.cpp b/sources/PPU/PPU.cpp index 1a212dc..d029ca0 100644 --- a/sources/PPU/PPU.cpp +++ b/sources/PPU/PPU.cpp @@ -206,11 +206,6 @@ namespace ComSquare::PPU void PPU::update(unsigned cycles) { (void)cycles; - this->_bus->write(0x2121, 0); - for (uint16_t value = 0; value <= 256; value++) { - this->_bus->write(0x2122, 0b11100000); - this->_bus->write(0x2122, 0b00000011); - } uint16_t tmp; uint8_t red; uint8_t green; diff --git a/tests/CPU/testInternal.cpp b/tests/CPU/testInternal.cpp index b704172..e4b7bf6 100644 --- a/tests/CPU/testInternal.cpp +++ b/tests/CPU/testInternal.cpp @@ -883,4 +883,25 @@ Test(BVS, noJump) pair.second.wram->_data[0] = 0x90; pair.second.cpu->BVS(0x0); cr_assert_eq(pair.second.cpu->_registers.pc, 0x80, "The program counter should be equal to 0x80 but it was 0x%x.", pair.second.cpu->_registers.pc); +} + +Test(JMP, simpleJump) +{ + auto pair = Init(); + pair.second.cpu->_registers.pc = 0x8000; + pair.second.wram->_data[0] = 0x00; + pair.second.wram->_data[1] = 0x10; + pair.second.cpu->JMP(0x0); + cr_assert_eq(pair.second.cpu->_registers.pc, 0x1000, "The program counter should be equal to 0x9000 but it was 0x%x.", pair.second.cpu->_registers.pc); +} + +Test(JML, simpleJump) +{ + auto pair = Init(); + pair.second.cpu->_registers.pc = 0x8000; + pair.second.wram->_data[0] = 0x00; + pair.second.wram->_data[1] = 0xAB; + pair.second.wram->_data[2] = 0x10; + pair.second.cpu->JML(0x0); + cr_assert_eq(pair.second.cpu->_registers.pac, 0x10AB00, "The program counter should be equal to 0x10AB00 but it was 0x%x.", pair.second.cpu->_registers.pac); } \ No newline at end of file