diff --git a/.gitignore b/.gitignore index 5468f3d..bdb7501 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ .idea/* cmake-build-debug/* -ui/*.h \ No newline at end of file +ui/*.h +out/ +.vs/ \ No newline at end of file diff --git a/sources/APU/APU.cpp b/sources/APU/APU.cpp index 8613883..7e2cf67 100644 --- a/sources/APU/APU.cpp +++ b/sources/APU/APU.cpp @@ -3,6 +3,7 @@ // #include +#include #include "APU.hpp" #include "../Exceptions/NotImplementedException.hpp" #include "../Exceptions/InvalidAddress.hpp" @@ -650,7 +651,7 @@ namespace ComSquare::APU case 0xE3: return this->BBS(this->_getDirectAddr(), this->_getImmediateData(), 7); case 0xE4: - return this->MOV(this->_getDirectAddr(), this->_internalRegisters.a, 3); + return this->MOV(this->_internalRead(this->_getDirectAddr()), this->_internalRegisters.a, 3); case 0xE5: return this->MOV(this->_getAbsoluteAddrByX(), this->_internalRegisters.a, 5); case 0xE6: @@ -682,7 +683,7 @@ namespace ComSquare::APU case 0xF3: return this->BBC(this->_getDirectAddr(), this->_getImmediateData(), 7); case 0xF4: - return this->MOV(this->_getDirectAddrByX(), this->_internalRegisters.a, 4); + return this->MOV(this->_internalRead(this->_getDirectAddrByX()), this->_internalRegisters.a, 4); case 0xF5: return this->MOV(this->_getAbsoluteAddrByX(), this->_internalRegisters.a, 5); case 0xF6: @@ -731,6 +732,74 @@ namespace ComSquare::APU this->_renderer.playAudio(std::span{this->_soundBuffer}, samples / 2); } + void APU::loadFromSPC(const std::shared_ptr& cartridge) + { + const uint8_t *data = cartridge->getData(); + + std::string song = std::string(reinterpret_cast(data + 0x2E), 0x20); + std::string game = std::string(reinterpret_cast(data + 0x4E), 0x20); + std::string dumper = std::string(reinterpret_cast(data + 0x6E), 0x10); + std::string comment = std::string(reinterpret_cast(data + 0x7E), 0x20); + std::string date = std::string(reinterpret_cast(data + 0x9E), 0x0B); + std::string artist = std::string(reinterpret_cast(data + 0xB1), 0x20); + + this->_internalRegisters.pcl = cartridge->read(0x25); + this->_internalRegisters.pch = cartridge->read(0x26); + this->_internalRegisters.a = cartridge->read(0x27); + this->_internalRegisters.x = cartridge->read(0x28); + this->_internalRegisters.y = cartridge->read(0x29); + this->_internalRegisters.psw = cartridge->read(0x2A); + this->_internalRegisters.sp = cartridge->read(0x2B); + + std::memcpy(this->_map->Page0.getData(), data + 0x100, this->_map->Page0.getSize()); + std::memcpy(this->_map->Page1.getData(), data + 0x200, this->_map->Page1.getSize()); + std::memcpy(this->_map->Memory.getData(), data + 0x300, this->_map->Memory.getSize()); + + this->_registers.unknown = cartridge->read(0x100 + 0xF0); + this->_registers.ctrlreg = cartridge->read(0x100 + 0xF1); + this->_registers.dspregAddr = cartridge->read(0x100 + 0xF2); + this->_dsp.write(this->_registers.dspregAddr, cartridge->read(0x100 + 0xF3)); + this->_registers.port0 = cartridge->read(0x100 + 0xF4); + this->_registers.port1 = cartridge->read(0x100 + 0xF5); + this->_registers.port2 = cartridge->read(0x100 + 0xF6); + this->_registers.port3 = cartridge->read(0x100 + 0xF7); + this->_registers.regmem1 = cartridge->read(0x100 + 0xF8); + this->_registers.regmem2 = cartridge->read(0x100 + 0xF9); + this->_registers.timer0 = cartridge->read(0x100 + 0xFA); + this->_registers.timer1 = cartridge->read(0x100 + 0xFB); + this->_registers.timer2 = cartridge->read(0x100 + 0xFC); + this->_registers.counter0 = cartridge->read(0x100 + 0xFD); + this->_registers.counter1 = cartridge->read(0x100 + 0xFE); + this->_registers.counter2 = cartridge->read(0x100 + 0xFF); + + for (int i = 0x00; i < 0x80; i += 0x10) + this->_dsp.write(i, cartridge->read(0x10100 + i)); + for (int i = 0x01; i < 0x80; i += 0x10) + this->_dsp.write(i, cartridge->read(0x10100 + i)); + for (int i = 0x02; i < 0x80; i += 0x10) + this->_dsp.write(i, cartridge->read(0x10100 + i)); + for (int i = 0x03; i < 0x80; i += 0x10) + this->_dsp.write(i, cartridge->read(0x10100 + i)); + for (int i = 0x04; i < 0x80; i += 0x10) + this->_dsp.write(i, cartridge->read(0x10100 + i)); + for (int i = 0x05; i < 0x80; i += 0x10) + this->_dsp.write(i, cartridge->read(0x10100 + i)); + for (int i = 0x06; i < 0x80; i += 0x10) + this->_dsp.write(i, cartridge->read(0x10100 + i)); + for (int i = 0x07; i < 0x80; i += 0x10) + this->_dsp.write(i, cartridge->read(0x10100 + i)); + for (int i = 0x08; i < 0x80; i += 0x10) + this->_dsp.write(i, cartridge->read(0x10100 + i)); + for (int i = 0x09; i < 0x80; i += 0x10) + this->_dsp.write(i, cartridge->read(0x10100 + i)); + for (int i = 0x0C; i < 0x80; i += 0x10) + this->_dsp.write(i, cartridge->read(0x10100 + i)); + for (int i = 0x0D; i < 0x80; i += 0x10) + this->_dsp.write(i, cartridge->read(0x10100 + i)); + for (int i = 0x0F; i < 0x80; i += 0x10) + this->_dsp.write(i, cartridge->read(0x10100 + i)); + } + void APU::_setNZflags(uint8_t value) { this->_internalRegisters.n = value & 0x80u; diff --git a/sources/APU/APU.hpp b/sources/APU/APU.hpp index a56a0d6..28c0608 100644 --- a/sources/APU/APU.hpp +++ b/sources/APU/APU.hpp @@ -11,6 +11,7 @@ #include "../Ram/Ram.hpp" #include "IPL/IPL.hpp" #include "../Renderer/IRenderer.hpp" +#include "../Cartridge/Cartridge.hpp" namespace ComSquare::APU { @@ -45,22 +46,22 @@ namespace ComSquare::APU //! @brief Program Status Word register union { struct { - //! @brief Negative flag - bool n : 1; - //! @brief Overflow flag - bool v : 1; - //! @brief Direct page flag - bool p : 1; - //! @brief Break flag - bool b : 1; - //! @brief Half carry flag - bool h : 1; - //! @brief Interrupt enabled flag - bool i : 1; - //! @brief Zero flag - bool z : 1; //! @brief Carry flag bool c : 1; + //! @brief Zero flag + bool z : 1; + //! @brief Interrupt enabled flag + bool i : 1; + //! @brief Half carry flag + bool h : 1; + //! @brief Break flag + bool b : 1; + //! @brief Direct page flag + bool p : 1; + //! @brief Overflow flag + bool v : 1; + //! @brief Negative flag + bool n : 1; }; uint8_t psw; }; @@ -393,6 +394,9 @@ namespace ComSquare::APU //! @return The number of bytes inside this memory. uint24_t getSize() const override; + //! @brief Parses rom data to uploads directly into RAM and corresponding registers + void loadFromSPC(const std::shared_ptr& cartridge); + //! @brief This function execute the instructions received until the maximum number of cycles is reached. //! @return The number of cycles that elapsed. virtual void update(unsigned cycles); diff --git a/sources/Debugger/APUDebug.cpp b/sources/Debugger/APUDebug.cpp index 3222f8d..4b2b521 100644 --- a/sources/Debugger/APUDebug.cpp +++ b/sources/Debugger/APUDebug.cpp @@ -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 + 0x0001u).c_str()); + this->_ui.programCounterLineEdit->setText(Utility::to_hex(this->_internalRegisters.pc).c_str()); this->_ui.programStatusWordLineEdit->setText(this->_getPSWString().c_str()); auto voices = this->_dsp.getVoices(); @@ -90,12 +90,13 @@ namespace ComSquare::Debugger auto noise = this->_dsp.getNoise(); auto brr = this->_dsp.getBrr(); auto latch = this->_dsp.getLatch(); + auto max = std::numeric_limits::max(); - this->_ui.mvolLprogressBar->setValue(master.volume[0]); - this->_ui.mvolRprogressBar->setValue(master.volume[1]); - this->_ui.evolLprogressBar->setValue(echo.volume[0]); - this->_ui.evolRprogressBar->setValue(echo.volume[1]); - this->_ui.echoprogressBar->setValue(echo.feedback); + this->_ui.mvolLprogressBar->setValue(master.volume[0] * 100 / max); + this->_ui.mvolRprogressBar->setValue(master.volume[1] * 100 / max); + this->_ui.evolLprogressBar->setValue(echo.volume[0] * 100 / max); + this->_ui.evolRprogressBar->setValue(echo.volume[1] * 100 / max); + this->_ui.echoprogressBar->setValue(echo.feedback * 100 / max); uint8_t flg = 0; flg += master.reset << 7; @@ -107,10 +108,10 @@ namespace ComSquare::Debugger this->_ui.echoBufferOffsetLineEdit->setText(Utility::to_hex(echo.data).c_str()); this->_ui.echoDelayLineEdit->setText(Utility::to_hex(echo.delay).c_str()); - this->_ui.VolumeLprogressBar->setValue(voices[0].volume[0]); - this->_ui.VolumeRprogressBar->setValue(voices[0].volume[1]); - this->_ui.WaveHeightprogressBar->setValue(latch.outx); - this->_ui.EchoFIRCoeffprogressBar->setValue(echo.FIR[0]); + this->_ui.VolumeLprogressBar->setValue(voices[0].volume[0] * 100 / max); + this->_ui.VolumeRprogressBar->setValue(voices[0].volume[1] * 100 / max); + this->_ui.WaveHeightprogressBar->setValue(latch.outx * 100 / max); + this->_ui.EchoFIRCoeffprogressBar->setValue(echo.FIR[0] * 100 / max); this->_ui.PitchlineEdit->setText(Utility::to_hex(voices[0].pitch).c_str()); this->_ui.sourceNumberLineEdit->setText(Utility::to_hex(voices[0].srcn).c_str()); this->_ui.GainlineEdit->setText(Utility::to_hex(voices[0].gain).c_str()); @@ -123,10 +124,10 @@ namespace ComSquare::Debugger this->_ui.SampleEndcheckBox->setChecked(voices[0].endx); this->_ui.PitchModulationcheckBox->setChecked(voices[0].pmon); - this->_ui.VolumeLprogressBar_2->setValue(voices[1].volume[0]); - this->_ui.VolumeRprogressBar_2->setValue(voices[1].volume[1]); - this->_ui.WaveHeightprogressBar_2->setValue(latch.outx); - this->_ui.EchoFIRCoeffprogressBar_2->setValue(echo.FIR[1]); + this->_ui.VolumeLprogressBar_2->setValue(voices[1].volume[0] * 100 / max); + this->_ui.VolumeRprogressBar_2->setValue(voices[1].volume[1] * 100 / max); + this->_ui.WaveHeightprogressBar_2->setValue(latch.outx * 100 / max); + this->_ui.EchoFIRCoeffprogressBar_2->setValue(echo.FIR[1] * 100 / max); this->_ui.PitchlineEdit_2->setText(Utility::to_hex(voices[1].pitch).c_str()); this->_ui.sourceNumberLineEdit_2->setText(Utility::to_hex(voices[1].srcn).c_str()); this->_ui.GainlineEdit_2->setText(Utility::to_hex(voices[1].gain).c_str()); @@ -139,10 +140,10 @@ namespace ComSquare::Debugger this->_ui.SampleEndcheckBox_2->setChecked(voices[1].endx); this->_ui.PitchModulationcheckBox_2->setChecked(voices[1].pmon); - this->_ui.VolumeLprogressBar_3->setValue(voices[2].volume[0]); - this->_ui.VolumeRprogressBar_3->setValue(voices[2].volume[1]); - this->_ui.WaveHeightprogressBar_3->setValue(latch.outx); - this->_ui.EchoFIRCoeffprogressBar_3->setValue(echo.FIR[2]); + this->_ui.VolumeLprogressBar_3->setValue(voices[2].volume[0] * 100 / max); + this->_ui.VolumeRprogressBar_3->setValue(voices[2].volume[1] * 100 / max); + this->_ui.WaveHeightprogressBar_3->setValue(latch.outx * 100 / max); + this->_ui.EchoFIRCoeffprogressBar_3->setValue(echo.FIR[2] * 100 / max); this->_ui.PitchlineEdit_3->setText(Utility::to_hex(voices[2].pitch).c_str()); this->_ui.sourceNumberLineEdit_3->setText(Utility::to_hex(voices[2].srcn).c_str()); this->_ui.GainlineEdit_3->setText(Utility::to_hex(voices[2].gain).c_str()); @@ -155,10 +156,10 @@ namespace ComSquare::Debugger this->_ui.SampleEndcheckBox_3->setChecked(voices[2].endx); this->_ui.PitchModulationcheckBox_3->setChecked(voices[2].pmon); - this->_ui.VolumeLprogressBar_4->setValue(voices[3].volume[0]); - this->_ui.VolumeRprogressBar_4->setValue(voices[3].volume[1]); - this->_ui.WaveHeightprogressBar_4->setValue(latch.outx); - this->_ui.EchoFIRCoeffprogressBar_4->setValue(echo.FIR[3]); + this->_ui.VolumeLprogressBar_4->setValue(voices[3].volume[0] * 100 / max); + this->_ui.VolumeRprogressBar_4->setValue(voices[3].volume[1] * 100 / max); + this->_ui.WaveHeightprogressBar_4->setValue(latch.outx * 100 / max); + this->_ui.EchoFIRCoeffprogressBar_4->setValue(echo.FIR[3] * 100 / max); this->_ui.PitchlineEdit_4->setText(Utility::to_hex(voices[3].pitch).c_str()); this->_ui.sourceNumberLineEdit_4->setText(Utility::to_hex(voices[3].srcn).c_str()); this->_ui.GainlineEdit_4->setText(Utility::to_hex(voices[3].gain).c_str()); @@ -171,10 +172,10 @@ namespace ComSquare::Debugger this->_ui.SampleEndcheckBox_4->setChecked(voices[3].endx); this->_ui.PitchModulationcheckBox_4->setChecked(voices[3].pmon); - this->_ui.VolumeLprogressBar_5->setValue(voices[4].volume[0]); - this->_ui.VolumeRprogressBar_5->setValue(voices[4].volume[1]); - this->_ui.WaveHeightprogressBar_5->setValue(latch.outx); - this->_ui.EchoFIRCoeffprogressBar_5->setValue(echo.FIR[4]); + this->_ui.VolumeLprogressBar_5->setValue(voices[4].volume[0] * 100 / max); + this->_ui.VolumeRprogressBar_5->setValue(voices[4].volume[1] * 100 / max); + this->_ui.WaveHeightprogressBar_5->setValue(latch.outx * 100 / max); + this->_ui.EchoFIRCoeffprogressBar_5->setValue(echo.FIR[4] * 100 / max); this->_ui.PitchlineEdit_5->setText(Utility::to_hex(voices[4].pitch).c_str()); this->_ui.sourceNumberLineEdit_5->setText(Utility::to_hex(voices[4].srcn).c_str()); this->_ui.GainlineEdit_5->setText(Utility::to_hex(voices[4].gain).c_str()); @@ -187,10 +188,10 @@ namespace ComSquare::Debugger this->_ui.SampleEndcheckBox_5->setChecked(voices[4].endx); this->_ui.PitchModulationcheckBox_5->setChecked(voices[4].pmon); - this->_ui.VolumeLprogressBar_6->setValue(voices[5].volume[0]); - this->_ui.VolumeRprogressBar_6->setValue(voices[5].volume[1]); - this->_ui.WaveHeightprogressBar_6->setValue(latch.outx); - this->_ui.EchoFIRCoeffprogressBar_6->setValue(echo.FIR[5]); + this->_ui.VolumeLprogressBar_6->setValue(voices[5].volume[0] * 100 / max); + this->_ui.VolumeRprogressBar_6->setValue(voices[5].volume[1] * 100 / max); + this->_ui.WaveHeightprogressBar_6->setValue(latch.outx * 100 / max); + this->_ui.EchoFIRCoeffprogressBar_6->setValue(echo.FIR[5] * 100 / max); this->_ui.PitchlineEdit_6->setText(Utility::to_hex(voices[5].pitch).c_str()); this->_ui.sourceNumberLineEdit_6->setText(Utility::to_hex(voices[5].srcn).c_str()); this->_ui.GainlineEdit_6->setText(Utility::to_hex(voices[5].gain).c_str()); @@ -203,10 +204,10 @@ namespace ComSquare::Debugger this->_ui.SampleEndcheckBox_6->setChecked(voices[5].endx); this->_ui.PitchModulationcheckBox_6->setChecked(voices[5].pmon); - this->_ui.VolumeLprogressBar_7->setValue(voices[6].volume[0]); - this->_ui.VolumeRprogressBar_7->setValue(voices[6].volume[1]); - this->_ui.WaveHeightprogressBar_7->setValue(latch.outx); - this->_ui.EchoFIRCoeffprogressBar_7->setValue(echo.FIR[6]); + this->_ui.VolumeLprogressBar_7->setValue(voices[6].volume[0] * 100 / max); + this->_ui.VolumeRprogressBar_7->setValue(voices[6].volume[1] * 100 / max); + this->_ui.WaveHeightprogressBar_7->setValue(latch.outx * 100 / max); + this->_ui.EchoFIRCoeffprogressBar_7->setValue(echo.FIR[6] * 100 / max); this->_ui.PitchlineEdit_7->setText(Utility::to_hex(voices[6].pitch).c_str()); this->_ui.sourceNumberLineEdit_7->setText(Utility::to_hex(voices[6].srcn).c_str()); this->_ui.GainlineEdit_7->setText(Utility::to_hex(voices[6].gain).c_str()); @@ -219,10 +220,10 @@ namespace ComSquare::Debugger this->_ui.SampleEndcheckBox_7->setChecked(voices[6].endx); this->_ui.PitchModulationcheckBox_7->setChecked(voices[6].pmon); - this->_ui.VolumeLprogressBar_8->setValue(voices[7].volume[0]); - this->_ui.VolumeRprogressBar_8->setValue(voices[7].volume[1]); - this->_ui.WaveHeightprogressBar_8->setValue(latch.outx); - this->_ui.EchoFIRCoeffprogressBar_8->setValue(echo.FIR[7]); + this->_ui.VolumeLprogressBar_8->setValue(voices[7].volume[0] * 100 / max); + this->_ui.VolumeRprogressBar_8->setValue(voices[7].volume[1] * 100 / max); + this->_ui.WaveHeightprogressBar_8->setValue(latch.outx * 100 / max); + this->_ui.EchoFIRCoeffprogressBar_8->setValue(echo.FIR[7] * 100 / max); this->_ui.PitchlineEdit_8->setText(Utility::to_hex(voices[7].pitch).c_str()); this->_ui.sourceNumberLineEdit_8->setText(Utility::to_hex(voices[7].srcn).c_str()); this->_ui.GainlineEdit_8->setText(Utility::to_hex(voices[7].gain).c_str()); diff --git a/sources/Ram/Ram.cpp b/sources/Ram/Ram.cpp index de8dc98..85951a7 100644 --- a/sources/Ram/Ram.cpp +++ b/sources/Ram/Ram.cpp @@ -56,4 +56,9 @@ namespace ComSquare::Ram { return this->_ramType; } + + uint8_t *Ram::getData() const + { + return this->_data; + } } diff --git a/sources/Ram/Ram.hpp b/sources/Ram/Ram.hpp index d3e4916..52c498c 100644 --- a/sources/Ram/Ram.hpp +++ b/sources/Ram/Ram.hpp @@ -50,6 +50,9 @@ namespace ComSquare::Ram //! @brief Get the size of the ram in bytes. uint24_t getSize() const override; + + //! @brief Get the raw data of the RAM + uint8_t *getData() const; }; } diff --git a/sources/SNES.cpp b/sources/SNES.cpp index 0d2b9c3..5323310 100644 --- a/sources/SNES.cpp +++ b/sources/SNES.cpp @@ -24,12 +24,17 @@ namespace ComSquare apu(new APU::APU(renderer)) { this->bus->mapComponents(*this); + if (this->cartridge->getType() == Cartridge::Audio) + this->apu->loadFromSPC(this->cartridge); } void SNES::update() { if (this->cartridge->getType() == Cartridge::Audio) + { + this->apu->update(0x01); return; + } unsigned cycleCount = this->cpu->update(); this->ppu->update(cycleCount);