From a9d2a9ef5dd4bc3ca1374c50ab12db56e61183c7 Mon Sep 17 00:00:00 2001 From: Melefo <42809472+Melefo@users.noreply.github.com> Date: Thu, 26 Mar 2020 22:03:02 +0100 Subject: [PATCH 1/8] Fixing APU's padding cycles --- sources/APU/APU.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/sources/APU/APU.cpp b/sources/APU/APU.cpp index c2cacd9..cc56ec6 100644 --- a/sources/APU/APU.cpp +++ b/sources/APU/APU.cpp @@ -706,6 +706,10 @@ namespace ComSquare::APU { unsigned total = 0; + if (this->_paddingCycles > cycles) { + this->_paddingCycles -= cycles; + return; + } cycles -= this->_paddingCycles; while (total < cycles && this->_state == Running) total += this->_executeInstruction(); From 62daa00390282ae648313e4695e45703e7c7becc Mon Sep 17 00:00:00 2001 From: Melefo <42809472+Melefo@users.noreply.github.com> Date: Thu, 9 Apr 2020 17:29:39 +0200 Subject: [PATCH 2/8] APU's start boot is working ! --- sources/APU/APU.cpp | 58 +++++++++---------- sources/APU/APU.hpp | 28 ++++----- .../APU/Instructions/8bitDataTransmission.cpp | 4 +- sources/APU/Instructions/ProgramFlow.cpp | 52 ++++++++--------- sources/Debugger/APUDebug.cpp | 13 +++-- 5 files changed, 78 insertions(+), 77 deletions(-) diff --git a/sources/APU/APU.cpp b/sources/APU/APU.cpp index cc56ec6..a41d04c 100644 --- a/sources/APU/APU.cpp +++ b/sources/APU/APU.cpp @@ -192,7 +192,7 @@ namespace ComSquare::APU case 0x02: return this->SET1(this->_getDirectAddr(), 0); case 0x03: - return this->BBS(this->_getDirectAddr(), 0); + return this->BBS(this->_getDirectAddr(), this->_getImmediateData(), 0); case 0x04: return this->ORacc(this->_getDirectAddr(), 3); case 0x05: @@ -218,13 +218,13 @@ namespace ComSquare::APU case 0x0F: return this->BRK(); case 0x10: - return this->BPL(); + return this->BPL(this->_getImmediateData()); case 0x11: return this->TCALL(1); case 0x12: return this->CLR1(this->_getDirectAddr(), 0); case 0x13: - return this->BBC(this->_getDirectAddr(), 0); + return this->BBC(this->_getDirectAddr(), this->_getImmediateData(), 0); case 0x14: return this->ORacc(this->_getDirectAddrByX(), 4); case 0x15: @@ -256,7 +256,7 @@ namespace ComSquare::APU case 0x22: return this->SET1(this->_getDirectAddr(), 1); case 0x23: - return this->BBS(this->_getDirectAddr(), 1); + return this->BBS(this->_getDirectAddr(), this->_getImmediateData(), 1); case 0x24: return this->ANDacc(this->_getDirectAddr(), 3); case 0x25: @@ -278,17 +278,17 @@ namespace ComSquare::APU case 0x2D: return this->PUSH(this->_internalRegisters.a); case 0x2E: - return this->CBNE(this->_getImmediateData()); + return this->CBNE(this->_getImmediateData(), this->_getImmediateData()); case 0x2F: - return this->BRA(); + return this->BRA(this->_getImmediateData()); case 0x30: - return this->BMI(); + return this->BMI(this->_getImmediateData()); case 0x31: return this->TCALL(3); case 0x32: return this->CLR1(this->_getDirectAddr(), 1); case 0x33: - return this->BBC(this->_getDirectAddr(), 1); + return this->BBC(this->_getDirectAddr(), this->_getImmediateData(), 1); case 0x34: return this->ANDacc(this->_getDirectAddrByX(), 4); case 0x35: @@ -320,7 +320,7 @@ namespace ComSquare::APU case 0x42: return this->SET1(this->_getDirectAddr(), 2); case 0x43: - return this->BBS(this->_getDirectAddr(), 2); + return this->BBS(this->_getDirectAddr(), this->_getImmediateData(), 2); case 0x44: return this->EORacc(this->_getDirectAddr(), 3); case 0x45: @@ -346,13 +346,13 @@ namespace ComSquare::APU case 0x4F: return this->PCALL(); case 0x50: - return this->BVC(); + return this->BVC(this->_getImmediateData()); case 0x51: return this->TCALL(5); case 0x52: return this->CLR1(this->_getDirectAddr(), 2); case 0x53: - return this->BBC(this->_getDirectAddr(), 2); + return this->BBC(this->_getDirectAddr(), this->_getImmediateData(), 2); case 0x54: return this->EORacc(this->_getDirectAddrByX(), 4); case 0x55: @@ -384,7 +384,7 @@ namespace ComSquare::APU case 0x62: return this->SET1(this->_getDirectAddr(), 3); case 0x63: - return this->BBS(this->_getDirectAddr(), 3); + return this->BBS(this->_getDirectAddr(), this->_getImmediateData(), 3); case 0x64: return this->CMPreg(this->_internalRegisters.a, this->_getDirectAddr(), 3); case 0x65: @@ -406,17 +406,17 @@ namespace ComSquare::APU case 0x6D: return this->PUSH(this->_internalRegisters.y); case 0x6E: - return this->DBNZ(true); + return this->DBNZ(this->_getImmediateData(), true); case 0x6F: return this->RET(); case 0x70: - return this->BVS(); + return this->BVS(this->_getImmediateData()); case 0x71: return this->TCALL(7); case 0x72: return this->CLR1(this->_getDirectAddr(), 3); case 0x73: - return this->BBC(this->_getDirectAddr(), 3); + return this->BBC(this->_getDirectAddr(), this->_getImmediateData(), 3); case 0x74: return this->CMPreg(this->_internalRegisters.a, this->_getDirectAddrByX(), 4); case 0x75: @@ -448,7 +448,7 @@ namespace ComSquare::APU case 0x82: return this->SET1(this->_getDirectAddr(), 4); case 0x83: - return this->BBS(this->_getDirectAddr(), 4); + return this->BBS(this->_getDirectAddr(), this->_getImmediateData(), 4); case 0x84: return this->ADCacc(this->_getDirectAddr(), 3); case 0x85: @@ -474,13 +474,13 @@ namespace ComSquare::APU case 0x8F: return this->MOV(this->_getImmediateData(), this->_getDirectAddr()); case 0x90: - return this->BCC(); + return this->BCC(this->_getImmediateData()); case 0x91: return this->TCALL(9); case 0x92: return this->CLR1(this->_getDirectAddr(), 4); case 0x93: - return this->BBC(this->_getDirectAddr(), 4); + return this->BBC(this->_getDirectAddr(), this->_getImmediateData(), 4); case 0x94: return this->ADCacc(this->_getDirectAddrByX(), 4); case 0x95: @@ -512,7 +512,7 @@ namespace ComSquare::APU case 0xA2: return this->SET1(this->_getDirectAddr(), 5); case 0xA3: - return this->BBS(this->_getDirectAddr(), 5); + return this->BBS(this->_getDirectAddr(), this->_getImmediateData(), 5); case 0xA4: return this->SBCacc(this->_getDirectAddr(), 3); case 0xA5: @@ -538,13 +538,13 @@ namespace ComSquare::APU case 0xAF: return this->MOV(this->_internalRegisters.a, this->_getIndexXAddr(), 4, true); case 0xB0: - return this->BCS(); + return this->BCS(this->_getImmediateData()); case 0xB1: return this->TCALL(11); case 0xB2: return this->CLR1(this->_getDirectAddr(), 5); case 0xB3: - return this->BBC(this->_getDirectAddr(), 5); + return this->BBC(this->_getDirectAddr(), this->_getImmediateData(), 5); case 0xB4: return this->SBCacc(this->_getDirectAddrByX(), 4); case 0xB5: @@ -576,7 +576,7 @@ namespace ComSquare::APU case 0xC2: return this->SET1(this->_getDirectAddr(), 6); case 0xC3: - return this->BBS(this->_getDirectAddr(), 6); + return this->BBS(this->_getDirectAddr(), this->_getImmediateData(), 6); case 0xC4: return this->MOV(this->_internalRegisters.a, this->_getDirectAddr(), 4); case 0xC5: @@ -602,13 +602,13 @@ namespace ComSquare::APU case 0xCF: return this->MUL(); case 0xD0: - return this->BNE(); + return this->BNE(this->_getImmediateData()); case 0xD1: return this->TCALL(13); case 0xD2: return this->CLR1(this->_getDirectAddr(), 6); case 0xD3: - return this->BBC(this->_getDirectAddr(), 6); + return this->BBC(this->_getDirectAddr(), this->_getImmediateData(), 6); case 0xD4: return this->MOV(this->_internalRegisters.a, this->_getDirectAddrByX(), 5); case 0xD5: @@ -630,7 +630,7 @@ namespace ComSquare::APU case 0xDD: return this->MOV(this->_internalRegisters.y, this->_internalRegisters.a); case 0xDE: - return this->CBNE(this->_getDirectAddrByX(), true); + return this->CBNE(this->_getDirectAddrByX(), this->_getImmediateData(), true); case 0xDF: return this->DAA(); case 0xE0: @@ -640,7 +640,7 @@ namespace ComSquare::APU case 0xE2: return this->SET1(this->_getDirectAddr(), 7); case 0xE3: - return this->BBS(this->_getDirectAddr(), 7); + return this->BBS(this->_getDirectAddr(), this->_getImmediateData(), 7); case 0xE4: return this->MOV(this->_getDirectAddr(), this->_internalRegisters.a, 3); case 0xE5: @@ -666,13 +666,13 @@ namespace ComSquare::APU case 0xEF: return this->SLEEP(); case 0xF0: - return BEQ(); + return BEQ(this->_getImmediateData()); case 0xF1: return this->TCALL(15); case 0xF2: return this->CLR1(this->_getDirectAddr(), 7); case 0xF3: - return this->BBC(this->_getDirectAddr(), 7); + return this->BBC(this->_getDirectAddr(), this->_getImmediateData(), 7); case 0xF4: return this->MOV(this->_getDirectAddrByX(), this->_internalRegisters.a, 4); case 0xF5: @@ -694,7 +694,7 @@ namespace ComSquare::APU case 0xFD: return this->MOV(this->_internalRegisters.a, this->_internalRegisters.y); case 0xFE: - return this->DBNZ(); + return this->DBNZ(this->_getImmediateData()); case 0xFF: return this->STOP(); default: diff --git a/sources/APU/APU.hpp b/sources/APU/APU.hpp index 5dbc43f..b692c6e 100644 --- a/sources/APU/APU.hpp +++ b/sources/APU/APU.hpp @@ -264,31 +264,31 @@ namespace ComSquare::APU int RETI(); //! @brief Branch Always, go to the specified location from the next instruction. - int BRA(); + int BRA(int8_t offset); //! @brief Branch if Zero Flag is set. - int BEQ(); + int BEQ(int8_t offset); //! @brief Branch if Zero Flag is clear. - int BNE(); + int BNE(int8_t offset); //! @brief Branch if Carry Flag is set. - int BCS(); + int BCS(int8_t offset); //! @brief Branch if Carry Flag is clear. - int BCC(); + int BCC(int8_t offset); //! @brief Branch if Overflow Flag is set. - int BVS(); + int BVS(int8_t offset); //! @brief Branch if Overflow Flag is set. - int BVC(); + int BVC(int8_t offset); //! @brief Branch if Negative Flag is set. - int BMI(); + int BMI(int8_t offset); //! @brief Branch if Negative Flag is clear. - int BPL(); + int BPL(int8_t offset); //! @brief Branch if the specified is set in the address, go to the specified location from the next instruction. - int BBS(uint24_t addr, uint8_t bit); + int BBS(uint24_t addr, uint8_t bit, int8_t offset); //! @brief Branch if the specified is clear in the address, go to the specified location from the next instruction. - int BBC(uint24_t addr, uint8_t bit); + int BBC(uint24_t addr, uint8_t bit, int8_t offset); //! @brief Branch if the value at the specified address is not equal to the Accumulator Flag. - int CBNE(uint24_t addr, bool by_x = false); + int CBNE(uint24_t addr, int8_t offset, bool by_x = false); //! @brief Decrement a value then branch to the specified location if the value is not zero. - int DBNZ(bool direct_addr = false); + int DBNZ(int8_t offset, bool direct_addr = false); //! @brief Jump to the specified location. int JMP(uint24_t addr, bool by_x = false); @@ -363,9 +363,9 @@ namespace ComSquare::APU int CMPreg(uint8_t ®, uint24_t addr, int cycles); int MOV(uint8_t ®From, uint8_t ®To, bool setFlags = true); - int MOV(uint24_t memFrom, uint24_t memTo); int MOV(uint8_t ®From, uint24_t memTo, int cycles, bool incrementX = false); int MOV(uint24_t memFrom, uint8_t ®To, int cycles, bool incrementX = false); + int MOV(uint24_t memTo, uint24_t memFrom); public: explicit APU(std::shared_ptr &map); APU(const APU &) = default; diff --git a/sources/APU/Instructions/8bitDataTransmission.cpp b/sources/APU/Instructions/8bitDataTransmission.cpp index f75e83d..7e08122 100644 --- a/sources/APU/Instructions/8bitDataTransmission.cpp +++ b/sources/APU/Instructions/8bitDataTransmission.cpp @@ -32,9 +32,9 @@ namespace ComSquare::APU return 2; } - int APU::MOV(uint24_t memFrom, uint24_t memTo) + int APU::MOV(uint24_t memTo, uint24_t memFrom) { - this->_internalWrite(memTo, this->_internalRead(memFrom)); + this->_internalWrite(memTo, memFrom); return 5; } } \ No newline at end of file diff --git a/sources/APU/Instructions/ProgramFlow.cpp b/sources/APU/Instructions/ProgramFlow.cpp index e8b6f39..f22bde2 100644 --- a/sources/APU/Instructions/ProgramFlow.cpp +++ b/sources/APU/Instructions/ProgramFlow.cpp @@ -6,109 +6,107 @@ namespace ComSquare::APU { - int APU::BRA() + int APU::BRA(int8_t offset) { - int8_t offset = this->_getImmediateData(); - this->_internalRegisters.pc += offset; return 4; } - int APU::BEQ() + int APU::BEQ(int8_t offset) { if (!this->_internalRegisters.z) return 2; - this->BRA(); + this->BRA(offset); return 4; } - int APU::BNE() + int APU::BNE(int8_t offset) { if (this->_internalRegisters.z) return 2; - this->BRA(); + this->BRA(offset); return 4; } - int APU::BCS() + int APU::BCS(int8_t offset) { if (!this->_internalRegisters.c) return 2; - this->BRA(); + this->BRA(offset); return 4; } - int APU::BCC() + int APU::BCC(int8_t offset) { if (this->_internalRegisters.c) return 2; - this->BRA(); + this->BRA(offset); return 4; } - int APU::BVS() + int APU::BVS(int8_t offset) { if (!this->_internalRegisters.v) return 2; - this->BRA(); + this->BRA(offset); return 4; } - int APU::BVC() + int APU::BVC(int8_t offset) { if (this->_internalRegisters.v) return 2; - this->BRA(); + this->BRA(offset); return 4; } - int APU::BMI() + int APU::BMI(int8_t offset) { if (!this->_internalRegisters.n) return 2; - this->BRA(); + this->BRA(offset); return 4; } - int APU::BPL() + int APU::BPL(int8_t offset) { if (this->_internalRegisters.n) return 2; - this->BRA(); + this->BRA(offset); return 4; } - int APU::BBS(uint24_t addr, uint8_t bit) + int APU::BBS(uint24_t addr, uint8_t bit, int8_t offset) { uint8_t data = this->_internalRead(addr); if (!(data & (1u << bit))) return 5; - this->BRA(); + this->BRA(offset); return 7; } - int APU::BBC(uint24_t addr, uint8_t bit) + int APU::BBC(uint24_t addr, uint8_t bit, int8_t offset) { uint8_t data = this->_internalRead(addr); if (data & (1u << bit)) return 5; - this->BRA(); + this->BRA(offset); return 7; } - int APU::CBNE(uint24_t addr, bool by_x) + int APU::CBNE(uint24_t addr, int8_t offset, bool by_x) { uint8_t data = this->_internalRead(addr); if (this->_internalRegisters.a == data) return 5 + by_x; - this->BRA(); + this->BRA(offset); return 7 + by_x; } - int APU::DBNZ(bool direct_addr) + int APU::DBNZ(int8_t offset, bool direct_addr) { uint8_t data; @@ -122,7 +120,7 @@ namespace ComSquare::APU data = --this->_internalRegisters.y; if (!data) return 4 + direct_addr; - this->BRA(); + this->BRA(offset); return 6 + direct_addr; } diff --git a/sources/Debugger/APUDebug.cpp b/sources/Debugger/APUDebug.cpp index d0521a3..21bcdfa 100644 --- a/sources/Debugger/APUDebug.cpp +++ b/sources/Debugger/APUDebug.cpp @@ -35,11 +35,11 @@ namespace ComSquare::Debugger this->_ui.port1hexaLineEdit->setText(Utility::to_hex(this->_registers.port1).c_str()); this->_ui.port1LineEdit->setText(Utility::to_binary(this->_registers.port1).c_str()); - this->_ui.port2hexaLineEdit->setText(Utility::to_hex(this->_registers.port1).c_str()); - this->_ui.port2LineEdit->setText(Utility::to_binary(this->_registers.port1).c_str()); + this->_ui.port2hexaLineEdit->setText(Utility::to_hex(this->_registers.port2).c_str()); + this->_ui.port2LineEdit->setText(Utility::to_binary(this->_registers.port2).c_str()); - this->_ui.port3hexaLineEdit->setText(Utility::to_hex(this->_registers.port1).c_str()); - this->_ui.port3LineEdit->setText(Utility::to_binary(this->_registers.port1).c_str()); + this->_ui.port3hexaLineEdit->setText(Utility::to_hex(this->_registers.port2).c_str()); + this->_ui.port3LineEdit->setText(Utility::to_binary(this->_registers.port2).c_str()); this->_ui.controlhexaLineEdit->setText(Utility::to_hex(this->_registers.ctrlreg).c_str()); this->_ui.controlLineEdit->setText(Utility::to_binary(this->_registers.ctrlreg).c_str()); @@ -475,6 +475,8 @@ namespace ComSquare::Debugger int APUDebug::_executeInstruction() { + int cycles = 0; + if (this->_isPaused) return 0xFF; if (this->_isStepping) { @@ -482,8 +484,9 @@ namespace ComSquare::Debugger this->_isPaused = true; } this->_ui.logger->append(APUDebug::_getInstructionString().c_str()); + cycles = APU::_executeInstruction(); this->_updatePanel(); - return APU::_executeInstruction(); + return cycles; } void APUDebug::update(unsigned cycles) From c58c5088b7963c4c18a27c0bb0df9aa50ddc3bcb Mon Sep 17 00:00:00 2001 From: Melefo <42809472+Melefo@users.noreply.github.com> Date: Thu, 9 Apr 2020 19:26:08 +0200 Subject: [PATCH 3/8] Fixing APU tests --- sources/APU/APU.cpp | 2 +- sources/APU/APU.hpp | 4 +- sources/APU/Instructions/ProgramFlow.cpp | 4 +- tests/APU/testAPUInstructions.cpp | 139 ++++++++++++----------- 4 files changed, 75 insertions(+), 74 deletions(-) diff --git a/sources/APU/APU.cpp b/sources/APU/APU.cpp index a41d04c..dacb563 100644 --- a/sources/APU/APU.cpp +++ b/sources/APU/APU.cpp @@ -472,7 +472,7 @@ namespace ComSquare::APU case 0x8E: return this->POP(this->_internalRegisters.psw); case 0x8F: - return this->MOV(this->_getImmediateData(), this->_getDirectAddr()); + return this->MOV(this->_getDirectAddr(), this->_getImmediateData()); case 0x90: return this->BCC(this->_getImmediateData()); case 0x91: diff --git a/sources/APU/APU.hpp b/sources/APU/APU.hpp index b692c6e..4ff1acf 100644 --- a/sources/APU/APU.hpp +++ b/sources/APU/APU.hpp @@ -282,9 +282,9 @@ namespace ComSquare::APU //! @brief Branch if Negative Flag is clear. int BPL(int8_t offset); //! @brief Branch if the specified is set in the address, go to the specified location from the next instruction. - int BBS(uint24_t addr, uint8_t bit, int8_t offset); + int BBS(uint24_t addr, int8_t offset, uint8_t bit); //! @brief Branch if the specified is clear in the address, go to the specified location from the next instruction. - int BBC(uint24_t addr, uint8_t bit, int8_t offset); + int BBC(uint24_t addr, int8_t offset, uint8_t bit); //! @brief Branch if the value at the specified address is not equal to the Accumulator Flag. int CBNE(uint24_t addr, int8_t offset, bool by_x = false); //! @brief Decrement a value then branch to the specified location if the value is not zero. diff --git a/sources/APU/Instructions/ProgramFlow.cpp b/sources/APU/Instructions/ProgramFlow.cpp index f22bde2..343b30d 100644 --- a/sources/APU/Instructions/ProgramFlow.cpp +++ b/sources/APU/Instructions/ProgramFlow.cpp @@ -76,7 +76,7 @@ namespace ComSquare::APU return 4; } - int APU::BBS(uint24_t addr, uint8_t bit, int8_t offset) + int APU::BBS(uint24_t addr, int8_t offset, uint8_t bit) { uint8_t data = this->_internalRead(addr); @@ -86,7 +86,7 @@ namespace ComSquare::APU return 7; } - int APU::BBC(uint24_t addr, uint8_t bit, int8_t offset) + int APU::BBC(uint24_t addr, int8_t offset, uint8_t bit) { uint8_t data = this->_internalRead(addr); diff --git a/tests/APU/testAPUInstructions.cpp b/tests/APU/testAPUInstructions.cpp index 4b853eb..4ee153b 100644 --- a/tests/APU/testAPUInstructions.cpp +++ b/tests/APU/testAPUInstructions.cpp @@ -506,11 +506,11 @@ Test(ProgramFlow, BRA) auto apu = snes.apu; int result = 0; + apu->_internalRegisters.pc = 0; apu->_internalWrite(apu->_internalRegisters.pc, 23); - apu->_internalWrite(apu->_internalRegisters.pc + 24, 101); - result = apu->BRA(); + result = apu->BRA(apu->_getImmediateData()); cr_assert_eq(result, 4); - cr_assert_eq(apu->_internalRead(apu->_internalRegisters.pc), 101); + cr_assert_eq(apu->_internalRegisters.pc, 24); } Test(ProgramFlow, BEQ) @@ -519,14 +519,14 @@ Test(ProgramFlow, BEQ) auto apu = snes.apu; int result = 0; - apu->_internalWrite(apu->_internalRegisters.pc, 23); - apu->_internalWrite(apu->_internalRegisters.pc + 24, 101); - result = apu->BEQ(); + apu->_internalRegisters.pc = 0; + apu->_internalWrite(apu->_internalRegisters.pc + 1, 23); + result = apu->BEQ(apu->_getImmediateData()); cr_assert_eq(result, 2); apu->_internalRegisters.z = true; - result = apu->BEQ(); + result = apu->BEQ(apu->_getImmediateData()); cr_assert_eq(result, 4); - cr_assert_eq(apu->_internalRead(apu->_internalRegisters.pc), 101); + cr_assert_eq(apu->_internalRegisters.pc, 25); } Test(ProgramFlow, BNE) @@ -535,15 +535,15 @@ Test(ProgramFlow, BNE) auto apu = snes.apu; int result = 0; - apu->_internalWrite(apu->_internalRegisters.pc, 23); - apu->_internalWrite(apu->_internalRegisters.pc + 24, 101); + apu->_internalRegisters.pc = 0; + apu->_internalWrite(apu->_internalRegisters.pc + 1, 23); apu->_internalRegisters.z = true; - result = apu->BNE(); + result = apu->BNE(apu->_getImmediateData()); cr_assert_eq(result, 2); apu->_internalRegisters.z = false; - result = apu->BNE(); + result = apu->BNE(apu->_getImmediateData()); cr_assert_eq(result, 4); - cr_assert_eq(apu->_internalRead(apu->_internalRegisters.pc), 101); + cr_assert_eq(apu->_internalRegisters.pc, 25); } Test(ProgramFlow, BCS) @@ -552,14 +552,14 @@ Test(ProgramFlow, BCS) auto apu = snes.apu; int result = 0; - apu->_internalWrite(apu->_internalRegisters.pc, 23); - apu->_internalWrite(apu->_internalRegisters.pc + 24, 101); - result = apu->BCS(); + apu->_internalRegisters.pc = 0; + apu->_internalWrite(apu->_internalRegisters.pc + 1, 23); + result = apu->BCS(apu->_getImmediateData()); cr_assert_eq(result, 2); apu->_internalRegisters.c = true; - result = apu->BCS(); + result = apu->BCS(apu->_getImmediateData()); cr_assert_eq(result, 4); - cr_assert_eq(apu->_internalRead(apu->_internalRegisters.pc), 101); + cr_assert_eq(apu->_internalRegisters.pc, 25); } Test(ProgramFlow, BCC) @@ -568,15 +568,15 @@ Test(ProgramFlow, BCC) auto apu = snes.apu; int result = 0; - apu->_internalWrite(apu->_internalRegisters.pc, 23); - apu->_internalWrite(apu->_internalRegisters.pc + 24, 101); + apu->_internalRegisters.pc = 0; + apu->_internalWrite(apu->_internalRegisters.pc + 1, 23); apu->_internalRegisters.c = true; - result = apu->BCC(); + result = apu->BCC(apu->_getImmediateData()); cr_assert_eq(result, 2); apu->_internalRegisters.c = false; - result = apu->BCC(); + result = apu->BCC(apu->_getImmediateData()); cr_assert_eq(result, 4); - cr_assert_eq(apu->_internalRead(apu->_internalRegisters.pc), 101); + cr_assert_eq(apu->_internalRegisters.pc, 25); } Test(ProgramFlow, BVS) @@ -585,14 +585,14 @@ Test(ProgramFlow, BVS) auto apu = snes.apu; int result = 0; - apu->_internalWrite(apu->_internalRegisters.pc, 23); - apu->_internalWrite(apu->_internalRegisters.pc + 24, 101); - result = apu->BVS(); + apu->_internalRegisters.pc = 0; + apu->_internalWrite(apu->_internalRegisters.pc + 1, 23); + result = apu->BVS(apu->_getImmediateData()); cr_assert_eq(result, 2); apu->_internalRegisters.v = true; - result = apu->BVS(); + result = apu->BVS(apu->_getImmediateData()); cr_assert_eq(result, 4); - cr_assert_eq(apu->_internalRead(apu->_internalRegisters.pc), 101); + cr_assert_eq(apu->_internalRegisters.pc, 25); } Test(ProgramFlow, BVC) @@ -601,15 +601,15 @@ Test(ProgramFlow, BVC) auto apu = snes.apu; int result = 0; - apu->_internalWrite(apu->_internalRegisters.pc, 23); - apu->_internalWrite(apu->_internalRegisters.pc + 24, 101); + apu->_internalRegisters.pc = 0; + apu->_internalWrite(apu->_internalRegisters.pc + 1, 23); apu->_internalRegisters.v = true; - result = apu->BVC(); + result = apu->BVC(apu->_getImmediateData()); cr_assert_eq(result, 2); apu->_internalRegisters.v = false; - result = apu->BVC(); + result = apu->BVC(apu->_getImmediateData()); cr_assert_eq(result, 4); - cr_assert_eq(apu->_internalRead(apu->_internalRegisters.pc), 101); + cr_assert_eq(apu->_internalRegisters.pc, 25); } Test(ProgramFlow, BMI) @@ -618,14 +618,14 @@ Test(ProgramFlow, BMI) auto apu = snes.apu; int result = 0; - apu->_internalWrite(apu->_internalRegisters.pc, 23); - apu->_internalWrite(apu->_internalRegisters.pc + 24, 101); - result = apu->BMI(); + apu->_internalRegisters.pc = 0; + apu->_internalWrite(apu->_internalRegisters.pc + 1, 23); + result = apu->BMI(apu->_getImmediateData()); cr_assert_eq(result, 2); apu->_internalRegisters.n = true; - result = apu->BMI(); + result = apu->BMI(apu->_getImmediateData()); cr_assert_eq(result, 4); - cr_assert_eq(apu->_internalRead(apu->_internalRegisters.pc), 101); + cr_assert_eq(apu->_internalRegisters.pc, 25); } Test(ProgramFlow, BPL) @@ -634,15 +634,15 @@ Test(ProgramFlow, BPL) auto apu = snes.apu; int result = 0; - apu->_internalWrite(apu->_internalRegisters.pc, 23); - apu->_internalWrite(apu->_internalRegisters.pc + 24, 101); + apu->_internalRegisters.pc = 0; + apu->_internalWrite(apu->_internalRegisters.pc + 1, 23); apu->_internalRegisters.n = true; - result = apu->BPL(); + result = apu->BPL(apu->_getImmediateData()); cr_assert_eq(result, 2); apu->_internalRegisters.n = false; - result = apu->BPL(); + result = apu->BPL(apu->_getImmediateData()); cr_assert_eq(result, 4); - cr_assert_eq(apu->_internalRead(apu->_internalRegisters.pc), 101); + cr_assert_eq(apu->_internalRegisters.pc, 25); } Test(ProgramFlow, BBS) @@ -652,14 +652,14 @@ Test(ProgramFlow, BBS) int result = 0; apu->_internalRegisters.pc = 0; - apu->_internalWrite(apu->_internalRegisters.pc, 23); + apu->_internalWrite(apu->_internalRegisters.pc + 1, 23); apu->_internalWrite(23, 0); - result = apu->BBS(apu->_getDirectAddr(), 2); + result = apu->BBS(apu->_getDirectAddr(), apu->_getImmediateData(), 2); cr_assert_eq(result, 5); apu->_internalRegisters.pc = 0; - apu->_internalWrite(apu->_internalRegisters.pc + 1, 10); - apu->_internalWrite(23, 255); - result = apu->BBS(apu->_getDirectAddr(), 2); + apu->_internalWrite(apu->_internalRegisters.pc, 10); + apu->_internalWrite(23, 100); + result = apu->BBS(apu->_getDirectAddr(), apu->_getImmediateData(), 2); cr_assert_eq(result, 7); cr_assert_eq(apu->_internalRegisters.pc, 12); } @@ -671,16 +671,16 @@ Test(ProgramFlow, BBC) int result = 0; apu->_internalRegisters.pc = 0; - apu->_internalWrite(apu->_internalRegisters.pc, 10); - apu->_internalWrite( 10, 255); - apu->_internalWrite(apu->_internalRegisters.pc + 1, 23); - result = apu->BBC(apu->_getDirectAddr(), 2); + apu->_internalWrite(apu->_internalRegisters.pc + 1, 10); + apu->_internalWrite(10, 255); + result = apu->BBC(apu->_getDirectAddr(), apu->_getImmediateData(), 2); cr_assert_eq(result, 5); apu->_internalRegisters.pc = 0; - apu->_internalWrite( 10, 0); - result = apu->BBC(apu->_getDirectAddr(), 2); + apu->_internalWrite(apu->_internalRegisters.pc, 10); + apu->_internalWrite(apu->_internalRegisters.pc + 1, 0); + result = apu->BBC(apu->_getDirectAddr(), apu->_getImmediateData(), 2); cr_assert_eq(result, 7); - cr_assert_eq(apu->_internalRegisters.pc, 25); + cr_assert_eq(apu->_internalRegisters.pc, 12); } Test(ProgramFlow, CBNE) @@ -691,16 +691,16 @@ Test(ProgramFlow, CBNE) apu->_internalRegisters.pc = 0; apu->_internalRegisters.a = 4; - apu->_internalWrite(apu->_internalRegisters.pc, 10); - apu->_internalWrite( 10, 4); - apu->_internalWrite(apu->_internalRegisters.pc + 1, 23); - result = apu->CBNE(apu->_getDirectAddr()); + apu->_internalWrite(apu->_internalRegisters.pc + 1, 10); + apu->_internalWrite(10, 4); + result = apu->CBNE(apu->_getDirectAddr(), apu->_getImmediateData()); cr_assert_eq(result, 5); apu->_internalRegisters.pc = 0; - apu->_internalWrite( 10, 0); - result = apu->CBNE(apu->_getDirectAddrByX(), true); + apu->_internalRegisters.a = 0; + apu->_internalWrite(apu->_internalRegisters.pc, 10); + result = apu->CBNE(apu->_getDirectAddrByX(), apu->_getImmediateData(), true); cr_assert_eq(result, 8); - cr_assert_eq(apu->_internalRegisters.pc, 25); + cr_assert_eq(apu->_internalRegisters.pc, 12); } Test(ProgramFlow, DBNZ) @@ -711,14 +711,15 @@ Test(ProgramFlow, DBNZ) apu->_internalRegisters.pc = 0; apu->_internalRegisters.y = 1; - result = apu->DBNZ(); + result = apu->DBNZ(apu->_getImmediateData()); cr_assert_eq(result, 4); apu->_internalWrite(apu->_internalRegisters.pc, 10); - apu->_internalWrite(apu->_internalRegisters.pc + 1, 23); - apu->_internalWrite( 10, 0); - result = apu->DBNZ(true); + apu->_internalWrite(apu->_internalRegisters.pc + 1, 5); + apu->_internalWrite(5, 55); + result = apu->DBNZ(apu->_getImmediateData(), true); cr_assert_eq(result, 7); - cr_assert_eq(apu->_internalRegisters.pc, 25); + cr_assert_eq( apu->_internalRead(5), 54); + cr_assert_eq(apu->_internalRegisters.pc, 13); } Test(ProgramFlow, JMP) @@ -1308,9 +1309,9 @@ Test(VIIIDataTransmission, MovMemToMem) apu->_internalWrite(0x24, 0x33); apu->_internalWrite(0x56, 99); apu->_internalWrite(0x33, 66); - result = apu->MOV(apu->_getDirectAddr(), apu->_getDirectAddr()); + result = apu->MOV(apu->_getDirectAddr(), apu->_getImmediateData()); cr_assert_eq(result, 5); - cr_assert_eq(apu->_internalRead(0x56), 66); + cr_assert_eq(apu->_internalRead(0x33), 0x56); } Test(VIIIDataTransmission, MovRegToMem) From 7733ec212994c884c4959c0da2c2e676606bd91f Mon Sep 17 00:00:00 2001 From: Melefo <42809472+Melefo@users.noreply.github.com> Date: Thu, 7 May 2020 15:35:50 +0200 Subject: [PATCH 4/8] Fixing weird compilation errors --- sources/Memory/AMemory.hpp | 1 + sources/Ram/Ram.hpp | 1 + 2 files changed, 2 insertions(+) diff --git a/sources/Memory/AMemory.hpp b/sources/Memory/AMemory.hpp index 5215436..1450261 100644 --- a/sources/Memory/AMemory.hpp +++ b/sources/Memory/AMemory.hpp @@ -9,6 +9,7 @@ #include #include #include +#include #include "../Models/Int24.hpp" #include "../Models/Components.hpp" diff --git a/sources/Ram/Ram.hpp b/sources/Ram/Ram.hpp index d9297e7..207709f 100644 --- a/sources/Ram/Ram.hpp +++ b/sources/Ram/Ram.hpp @@ -6,6 +6,7 @@ #define COMSQUARE_RAM_HPP #include "../Memory/ARectangleMemory.hpp" +#include namespace ComSquare::Ram { From 5c94bdb27ad065cad6de0393657a4306d2f57dcc Mon Sep 17 00:00:00 2001 From: Melefo <42809472+Melefo@users.noreply.github.com> Date: Thu, 7 May 2020 16:42:23 +0200 Subject: [PATCH 5/8] Fix APU CMP & Debugger --- sources/APU/Instructions/8bitArithmetic.cpp | 5 ++--- sources/Debugger/APUDebug.cpp | 6 +++--- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/sources/APU/Instructions/8bitArithmetic.cpp b/sources/APU/Instructions/8bitArithmetic.cpp index c09c58f..2010d99 100644 --- a/sources/APU/Instructions/8bitArithmetic.cpp +++ b/sources/APU/Instructions/8bitArithmetic.cpp @@ -63,10 +63,9 @@ namespace ComSquare::APU int APU::CMP(uint24_t operand1, uint24_t operand2, int cycles) { uint8_t data1 = this->_internalRead(operand1); - uint8_t data2 = this->_internalRead(operand2); - this->_internalRegisters.c = data1 >= data2; - this->_setNZflags(data1 - data2); + this->_internalRegisters.c = data1 >= operand2; + this->_setNZflags(data1 - operand2); return cycles; } diff --git a/sources/Debugger/APUDebug.cpp b/sources/Debugger/APUDebug.cpp index 21bcdfa..6e11913 100644 --- a/sources/Debugger/APUDebug.cpp +++ b/sources/Debugger/APUDebug.cpp @@ -38,8 +38,8 @@ namespace ComSquare::Debugger this->_ui.port2hexaLineEdit->setText(Utility::to_hex(this->_registers.port2).c_str()); this->_ui.port2LineEdit->setText(Utility::to_binary(this->_registers.port2).c_str()); - this->_ui.port3hexaLineEdit->setText(Utility::to_hex(this->_registers.port2).c_str()); - this->_ui.port3LineEdit->setText(Utility::to_binary(this->_registers.port2).c_str()); + this->_ui.port3hexaLineEdit->setText(Utility::to_hex(this->_registers.port3).c_str()); + this->_ui.port3LineEdit->setText(Utility::to_binary(this->_registers.port3).c_str()); this->_ui.controlhexaLineEdit->setText(Utility::to_hex(this->_registers.ctrlreg).c_str()); this->_ui.controlLineEdit->setText(Utility::to_binary(this->_registers.ctrlreg).c_str()); @@ -81,7 +81,7 @@ namespace ComSquare::Debugger this->_ui.xIndexLineEdit->setText(Utility::to_hex(this->_internalRegisters.x).c_str()); this->_ui.yIndexLineEdit->setText(Utility::to_hex(this->_internalRegisters.y).c_str()); this->_ui.accumlatorLineEdit->setText(Utility::to_hex(this->_internalRegisters.a).c_str()); - this->_ui.programCounterLineEdit->setText(Utility::to_hex(this->_internalRegisters.pc).c_str()); + this->_ui.programCounterLineEdit->setText(Utility::to_hex(this->_internalRegisters.pc + 0x0001u).c_str()); this->_ui.programStatusWordLineEdit->setText(this->_getPSWString().c_str()); this->_ui.mvolLprogressBar->setValue(this->_dsp->getRegisters().mvolL); From c661dfa6418a65873351864eb0b358ab1516b5a8 Mon Sep 17 00:00:00 2001 From: Anonymus Raccoon Date: Thu, 14 May 2020 19:00:13 +0200 Subject: [PATCH 6/8] Fixing ??? message on the bus's debugger --- sources/Debugger/MemoryBusDebug.cpp | 4 ++-- sources/Debugger/MemoryBusDebug.hpp | 6 +++++- sources/SNES.cpp | 2 -- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/sources/Debugger/MemoryBusDebug.cpp b/sources/Debugger/MemoryBusDebug.cpp index 0ddc6c8..17d77fc 100644 --- a/sources/Debugger/MemoryBusDebug.cpp +++ b/sources/Debugger/MemoryBusDebug.cpp @@ -165,12 +165,12 @@ namespace ComSquare::Debugger void MemoryBusDebug::write(uint24_t addr, uint8_t data) { auto accessor = this->getAccessor(addr); - uint8_t value = 0; + std::optional value = std::nullopt; try { if (accessor) value = accessor->read(addr - accessor->getStart()); } catch (InvalidAddress &) { - value = 0; + value = std::nullopt; } if (!forceSilence) this->_model.log(BusLog(true, addr, accessor, value, data)); diff --git a/sources/Debugger/MemoryBusDebug.hpp b/sources/Debugger/MemoryBusDebug.hpp index 9e6cbd8..c477d2d 100644 --- a/sources/Debugger/MemoryBusDebug.hpp +++ b/sources/Debugger/MemoryBusDebug.hpp @@ -15,7 +15,11 @@ namespace ComSquare::Debugger { //! @brief The struct used to represent memory bus logs. struct BusLog { - BusLog(bool write, uint24_t addr, std::shared_ptr &accessor, std::optional oldData, uint8_t newData); + BusLog(bool write, + uint24_t addr, + std::shared_ptr &accessor, + std::optional oldData, + uint8_t newData); bool write; uint24_t addr; diff --git a/sources/SNES.cpp b/sources/SNES.cpp index 1ae1b61..153c1e8 100644 --- a/sources/SNES.cpp +++ b/sources/SNES.cpp @@ -10,8 +10,6 @@ #include "Debugger/APUDebug.hpp" #include "Debugger/MemoryBusDebug.hpp" #include "Debugger/CGramDebug.hpp" - - #endif namespace ComSquare From 7e2c8d99f1de7a8d9da7d40611bc2fca0eb87053 Mon Sep 17 00:00:00 2001 From: Melefo <42809472+Melefo@users.noreply.github.com> Date: Mon, 18 May 2020 11:18:05 +0200 Subject: [PATCH 7/8] Temporary better synchronisation between CPU & APU by decreasing the number of instructions and therefor the number of cycles --- sources/CPU/CPU.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sources/CPU/CPU.cpp b/sources/CPU/CPU.cpp index 88ce592..dc36ed4 100644 --- a/sources/CPU/CPU.cpp +++ b/sources/CPU/CPU.cpp @@ -205,7 +205,7 @@ namespace ComSquare::CPU { unsigned cycles = 0; - for (int i = 0; i < 0xFF; i++) { + for (int i = 0; i < 0x18; i++) { if (this->_isStopped) { cycles += 1; continue; From ea553923077147d9a48fd1db3b3ff4d7ea637673 Mon Sep 17 00:00:00 2001 From: Melefo <42809472+Melefo@users.noreply.github.com> Date: Wed, 20 May 2020 16:03:34 +0200 Subject: [PATCH 8/8] Merging with master to PR --- sources/CPU/CPU.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sources/CPU/CPU.cpp b/sources/CPU/CPU.cpp index dc36ed4..9b9debf 100644 --- a/sources/CPU/CPU.cpp +++ b/sources/CPU/CPU.cpp @@ -205,7 +205,7 @@ namespace ComSquare::CPU { unsigned cycles = 0; - for (int i = 0; i < 0x18; i++) { + for (int i = 0; i < 0x17; i++) { if (this->_isStopped) { cycles += 1; continue;