From e927bbda0a928583d61adb2fd1b52e085002417d Mon Sep 17 00:00:00 2001 From: ogamespec Date: Sun, 13 Aug 2023 19:38:36 +0300 Subject: [PATCH] DMEM/IMEM moved to DspCore as it should --- src/dsp.cpp | 88 +++++------------------------------------------- src/dsp.h | 20 ----------- src/dspcore.cpp | 80 ++++++++++++++++++++++++++++++++++++++++--- src/dspcore.h | 22 ++++++++++++ src/dspdebug.cpp | 18 +++++----- src/flipper.cpp | 4 +-- src/main.h | 4 +-- src/pad.h | 4 +-- src/tx.h | 12 +++---- 9 files changed, 127 insertions(+), 125 deletions(-) diff --git a/src/dsp.cpp b/src/dsp.cpp index c1fb42b6..e3d57f46 100644 --- a/src/dsp.cpp +++ b/src/dsp.cpp @@ -27,34 +27,6 @@ namespace DSP JDI::Hub.RemoveNode(DSP_JDI_JSON); } - bool Dsp16::LoadIrom(std::vector& iromImage) - { - if (iromImage.empty() || iromImage.size() != IROM_SIZE) - { - return false; - } - else - { - memcpy(irom, iromImage.data(), IROM_SIZE); - } - - return true; - } - - bool Dsp16::LoadDrom(std::vector& dromImage) - { - if (dromImage.empty() || dromImage.size() != DROM_SIZE) - { - return false; - } - else - { - memcpy(drom, dromImage.data(), DROM_SIZE); - } - - return true; - } - void Dsp16::DspThreadProc(void* Parameter) { Dsp16* dsp = (Dsp16*)Parameter; @@ -116,50 +88,6 @@ namespace DSP #pragma region "Memory Engine" - uint8_t* Dsp16::TranslateIMem(DspAddress addr) - { - if (addr < (IRAM_SIZE / 2)) - { - return &iram[addr << 1]; - } - else if (addr >= IROM_START_ADDRESS && addr < (IROM_START_ADDRESS + (IROM_SIZE / 2))) - { - return &irom[(addr - IROM_START_ADDRESS) << 1]; - } - else - { - return nullptr; - } - } - - uint8_t* Dsp16::TranslateDMem(DspAddress addr) - { - if (addr < (DRAM_SIZE / 2)) - { - return &dram[addr << 1]; - } - else if (addr >= DROM_START_ADDRESS && addr < (DROM_START_ADDRESS + (DROM_SIZE / 2))) - { - return &drom[(addr - DROM_START_ADDRESS) << 1]; - } - else - { - return nullptr; - } - } - - uint16_t Dsp16::ReadIMem(DspAddress addr) - { - uint8_t* ptr = TranslateIMem(addr); - - if (ptr) - { - return _BYTESWAP_UINT16(*(uint16_t*)ptr); - } - - return 0; - } - uint16_t Dsp16::ReadDMem(DspAddress addr) { if (core->TestWatch(addr)) @@ -167,7 +95,7 @@ namespace DSP Report(Channel::DSP, "ReadDMem 0x%04X, pc: 0x%04X\n", addr, core->regs.pc); } - if (addr >= IFX_START_ADDRESS) + if (addr >= core->IFX_START_ADDRESS) { switch (addr) { @@ -231,7 +159,7 @@ namespace DSP return 0; } - uint8_t* ptr = TranslateDMem(addr); + uint8_t* ptr = core->TranslateDMem(addr); if (ptr) { @@ -256,7 +184,7 @@ namespace DSP Report(Channel::DSP, "WriteDMem 0x%04X = 0x%04X, pc: 0x%04X\n", addr, value, core->regs.pc); } - if (addr >= IFX_START_ADDRESS) + if (addr >= core->IFX_START_ADDRESS) { switch (addr) { @@ -471,9 +399,9 @@ namespace DSP return; } - if (addr < (DRAM_SIZE / 2)) + if (addr < (core->DRAM_SIZE / 2)) { - uint8_t* ptr = TranslateDMem(addr); + uint8_t* ptr = core->TranslateDMem(addr); if (ptr) { @@ -742,11 +670,11 @@ namespace DSP if (DmaRegs.control.Imem) { - ptr = TranslateIMem(DmaRegs.dspAddr); + ptr = core->TranslateIMem(DmaRegs.dspAddr); } else { - ptr = TranslateDMem(DmaRegs.dspAddr); + ptr = core->TranslateDMem(DmaRegs.dspAddr); } if (ptr == nullptr) @@ -795,7 +723,7 @@ namespace DSP void Dsp16::SpecialAramImemDma(uint8_t* ptr, size_t byteCount) { - memcpy(iram, ptr, byteCount); + memcpy(core->iram, ptr, byteCount); if (logDspDma) { diff --git a/src/dsp.h b/src/dsp.h index 2a98de9a..df471662 100644 --- a/src/dsp.h +++ b/src/dsp.h @@ -253,20 +253,6 @@ namespace DSP friend DspInterpreter; friend DspUnitTest::DspUnitTest; - static const size_t IRAM_SIZE = (8 * 1024); - static const size_t IROM_SIZE = (8 * 1024); - static const size_t DRAM_SIZE = (8 * 1024); - static const size_t DROM_SIZE = (4 * 1024); - - static const size_t IROM_START_ADDRESS = 0x8000; - static const size_t DROM_START_ADDRESS = 0x1000; - static const size_t IFX_START_ADDRESS = 0xFF00; // Internal dsp "hardware" - - uint8_t iram[IRAM_SIZE] = { 0 }; - uint8_t irom[IROM_SIZE] = { 0 }; - uint8_t dram[DRAM_SIZE] = { 0 }; - uint8_t drom[DROM_SIZE] = { 0 }; - Thread* dspThread = nullptr; static void DspThreadProc(void* Parameter); uint64_t savedGekkoTicks = 0; @@ -308,18 +294,12 @@ namespace DSP Dsp16(); ~Dsp16(); - bool LoadIrom(std::vector& iromImage); - bool LoadDrom(std::vector& dromImage); - void Run(); bool IsRunning() { return dspThread->IsRunning(); } void Suspend(); // Memory engine - uint8_t* TranslateIMem(DspAddress addr); - uint8_t* TranslateDMem(DspAddress addr); - uint16_t ReadIMem(DspAddress addr); uint16_t ReadDMem(DspAddress addr); void WriteDMem(DspAddress addr, uint16_t value); diff --git a/src/dspcore.cpp b/src/dspcore.cpp index 1df13b6a..12789db2 100644 --- a/src/dspcore.cpp +++ b/src/dspcore.cpp @@ -323,7 +323,7 @@ namespace DSP regs.eas->clear(); regs.lcs->clear(); - regs.pc = Flipper::DSPGetResetModifier() ? dsp->IROM_START_ADDRESS : 0; // IROM start / 0 + regs.pc = Flipper::DSPGetResetModifier() ? IROM_START_ADDRESS : 0; // IROM start / 0 } else { @@ -451,6 +451,78 @@ namespace DSP regs.pc = pc; } + bool DspCore::LoadIrom(std::vector& iromImage) + { + if (iromImage.empty() || iromImage.size() != IROM_SIZE) + { + return false; + } + else + { + memcpy(irom, iromImage.data(), IROM_SIZE); + } + + return true; + } + + bool DspCore::LoadDrom(std::vector& dromImage) + { + if (dromImage.empty() || dromImage.size() != DROM_SIZE) + { + return false; + } + else + { + memcpy(drom, dromImage.data(), DROM_SIZE); + } + + return true; + } + + uint8_t* DspCore::TranslateIMem(DspAddress addr) + { + if (addr < (IRAM_SIZE / 2)) + { + return &iram[addr << 1]; + } + else if (addr >= IROM_START_ADDRESS && addr < (IROM_START_ADDRESS + (IROM_SIZE / 2))) + { + return &irom[(addr - IROM_START_ADDRESS) << 1]; + } + else + { + return nullptr; + } + } + + uint8_t* DspCore::TranslateDMem(DspAddress addr) + { + if (addr < (DRAM_SIZE / 2)) + { + return &dram[addr << 1]; + } + else if (addr >= DROM_START_ADDRESS && addr < (DROM_START_ADDRESS + (DROM_SIZE / 2))) + { + return &drom[(addr - DROM_START_ADDRESS) << 1]; + } + else + { + return nullptr; + } + } + + uint16_t DspCore::ReadIMem(DspAddress addr) + { + uint8_t* ptr = TranslateIMem(addr); + + if (ptr) + { + return _BYTESWAP_UINT16(*(uint16_t*)ptr); + } + + return 0; + } + void DspCore::HardReset() { Report(Channel::DSP, "DspCore::HardReset\n"); @@ -497,7 +569,7 @@ namespace DSP intr.pendingSomething = false; // Hard Reset always from IROM start - regs.pc = dsp->IROM_START_ADDRESS; + regs.pc = IROM_START_ADDRESS; Report(Channel::DSP, "Hard Reset pc = 0x%04X\n", regs.pc); dsp->ResetIfx(); @@ -1388,7 +1460,7 @@ namespace DSP DspAddress imemAddr = core->regs.pc; - uint8_t* imemPtr = core->dsp->TranslateIMem(imemAddr); + uint8_t* imemPtr = core->TranslateIMem(imemAddr); if (imemPtr == nullptr) { Halt("DSP TranslateIMem failed on dsp addr: 0x%04X\n", imemAddr); @@ -2785,7 +2857,7 @@ namespace DSP void DspInterpreter::pld() { int r = (int)info.params[1]; - core->MoveToReg((int)info.params[0], core->dsp->ReadIMem(core->regs.r[r]) ); + core->MoveToReg((int)info.params[0], core->ReadIMem(core->regs.r[r]) ); AdvanceAddress(r, info.params[2]); } diff --git a/src/dspcore.h b/src/dspcore.h index 39684d6d..eabac9b5 100644 --- a/src/dspcore.h +++ b/src/dspcore.h @@ -381,9 +381,24 @@ namespace DSP /// class DspCore { + friend Dsp16; friend DspInterpreter; friend DspUnitTest::DspUnitTest; + static const size_t IRAM_SIZE = (8 * 1024); + static const size_t IROM_SIZE = (8 * 1024); + static const size_t DRAM_SIZE = (8 * 1024); + static const size_t DROM_SIZE = (4 * 1024); + + static const size_t IROM_START_ADDRESS = 0x8000; + static const size_t DROM_START_ADDRESS = 0x1000; + static const size_t IFX_START_ADDRESS = 0xFF00; // Internal dsp "hardware" + + uint8_t iram[IRAM_SIZE] = { 0 }; + uint8_t irom[IROM_SIZE] = { 0 }; + uint8_t dram[DRAM_SIZE] = { 0 }; + uint8_t drom[DROM_SIZE] = { 0 }; + std::list breakpoints; // IMEM breakpoints SpinLock breakPointsSpinLock; DspAddress oneShotBreakpoint = 0xffff; @@ -420,6 +435,13 @@ namespace DSP DspCore(Dsp16* parent); ~DspCore(); + bool LoadIrom(std::vector& iromImage); + bool LoadDrom(std::vector& dromImage); + + uint8_t* TranslateIMem(DspAddress addr); + uint8_t* TranslateDMem(DspAddress addr); + uint16_t ReadIMem(DspAddress addr); + void AssertInterrupt(DspInterrupt id); bool IsInterruptPending(DspInterrupt id); void ReturnFromInterrupt(); diff --git a/src/dspdebug.cpp b/src/dspdebug.cpp index 0bb6cda7..6539eacc 100644 --- a/src/dspdebug.cpp +++ b/src/dspdebug.cpp @@ -102,7 +102,7 @@ namespace DSP while (bytes != 0) { - uint8_t* ptr = Flipper::DSP->TranslateDMem(dsp_addr); + uint8_t* ptr = Flipper::DSP->core->TranslateDMem(dsp_addr); if (ptr == nullptr) { Report(Channel::DSP, "TranslateDMem failed on dsp addr: 0x%04X\n", dsp_addr); @@ -141,7 +141,7 @@ namespace DSP while (bytes != 0) { - uint8_t* ptr = Flipper::DSP->TranslateIMem(dsp_addr); + uint8_t* ptr = Flipper::DSP->core->TranslateIMem(dsp_addr); if (ptr == nullptr) { Report(Channel::DSP, "TranslateIMem failed on dsp addr: 0x%04X\n", dsp_addr); @@ -199,7 +199,7 @@ namespace DSP DSP::DspAddress pcAddr = Flipper::DSP->core->regs.pc; - uint8_t* imemPtr = Flipper::DSP->TranslateIMem(pcAddr); + uint8_t* imemPtr = Flipper::DSP->core->TranslateIMem(pcAddr); if (imemPtr == nullptr) { Report(Channel::DSP, "TranslateIMem failed on dsp addr: 0x%04X\n", pcAddr); @@ -327,7 +327,7 @@ namespace DSP while (instrCount--) { - uint8_t* imemPtr = Flipper::DSP->TranslateIMem(addr); + uint8_t* imemPtr = Flipper::DSP->core->TranslateIMem(addr); if (imemPtr == nullptr) { Report(Channel::DSP, "TranslateIMem failed on dsp addr: 0x%04X\n", addr); @@ -569,7 +569,7 @@ namespace DSP { DspAddress addr = strtoul(args[1].c_str(), nullptr, 0); - uint8_t* ptr = Flipper::DSP->TranslateDMem(addr); + uint8_t* ptr = Flipper::DSP->core->TranslateDMem(addr); Json::Value* output = new Json::Value(); output->type = Json::ValueType::Int; @@ -583,7 +583,7 @@ namespace DSP { DspAddress addr = strtoul(args[1].c_str(), nullptr, 0); - uint8_t* ptr = Flipper::DSP->TranslateIMem(addr); + uint8_t* ptr = Flipper::DSP->core->TranslateIMem(addr); Json::Value* output = new Json::Value(); output->type = Json::ValueType::Int; @@ -629,7 +629,7 @@ namespace DSP targetAddress = 0; - uint8_t* ptr = (uint8_t*)Flipper::DSP->TranslateIMem(address); + uint8_t* ptr = (uint8_t*)Flipper::DSP->core->TranslateIMem(address); if (!ptr) { return false; @@ -672,7 +672,7 @@ namespace DSP targetAddress = 0; - uint8_t* ptr = Flipper::DSP->TranslateIMem(address); + uint8_t* ptr = Flipper::DSP->core->TranslateIMem(address); if (!ptr) { return false; @@ -723,7 +723,7 @@ namespace DSP std::string text = ""; - uint8_t* ptr = (uint8_t*)Flipper::DSP->TranslateIMem(address); + uint8_t* ptr = (uint8_t*)Flipper::DSP->core->TranslateIMem(address); if (ptr) { DSP::Decoder::Decode(ptr, DSP::DspCore::MaxInstructionSizeInBytes, info); diff --git a/src/flipper.cpp b/src/flipper.cpp index e959493a..032181b8 100644 --- a/src/flipper.cpp +++ b/src/flipper.cpp @@ -53,7 +53,7 @@ namespace Flipper auto iromImage = Util::FileLoad(config->DspIromFilename); - if (DSP->LoadIrom(iromImage)) + if (DSP->core->LoadIrom(iromImage)) { Report(Channel::DSP, "Loaded DSP IROM: %s\n", Util::WstringToString(config->DspIromFilename).c_str()); } @@ -66,7 +66,7 @@ namespace Flipper auto dromImage = Util::FileLoad(config->DspDromFilename); - if (DSP->LoadDrom(dromImage)) + if (DSP->core->LoadDrom(dromImage)) { Report(Channel::DSP, "Loaded DSP DROM: %s\n", Util::WstringToString(config->DspDromFilename).c_str()); } diff --git a/src/main.h b/src/main.h index df2afcd8..d5d851a8 100644 --- a/src/main.h +++ b/src/main.h @@ -18,13 +18,13 @@ Thread* EMUCreateThread(ThreadProc threadProc, bool suspended, void* context, co void EMUJoinThread(Thread* thread); // all important data is placed here -typedef struct Emulator +struct Emulator { bool init; bool loaded; // file loaded std::wstring lastLoaded; bool bootrom; // The emulator is running in Bootrom runtime mode -} Emulator; +}; extern Emulator emu; diff --git a/src/pad.h b/src/pad.h index f2dc9a54..dd1a1540 100644 --- a/src/pad.h +++ b/src/pad.h @@ -31,11 +31,11 @@ enum VKEY_FOR_MAX }; -typedef struct +struct PADCONF { bool plugged; int vkeys[VKEY_FOR_MAX]; // -1 - undefined -} PADCONF; +}; // PAD (input) interface // (padnum = 0...3) diff --git a/src/tx.h b/src/tx.h index 8ad91326..192f23ae 100644 --- a/src/tx.h +++ b/src/tx.h @@ -188,7 +188,7 @@ namespace GX // TODO: Old implementation, will be redone nicely. // texture entry -typedef struct +struct TexEntry { uint32_t ramAddr; uint8_t* rawData; @@ -197,19 +197,19 @@ typedef struct int w, h, dw, dh; float ds, dt; uint32_t bind; -} TexEntry; +}; -typedef struct +struct S3TC_TEX { unsigned t : 2; -} S3TC_TEX; +}; -typedef struct +struct S3TC_BLK { uint16_t rgb0; // color 2 uint16_t rgb1; // color 1 uint8_t row[4]; -} S3TC_BLK; +}; // interface to application