Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DMEM/IMEM moved to DspCore as it should #289

Merged
merged 1 commit into from
Aug 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 8 additions & 80 deletions src/dsp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,34 +27,6 @@ namespace DSP
JDI::Hub.RemoveNode(DSP_JDI_JSON);
}

bool Dsp16::LoadIrom(std::vector<uint8_t>& iromImage)
{
if (iromImage.empty() || iromImage.size() != IROM_SIZE)
{
return false;
}
else
{
memcpy(irom, iromImage.data(), IROM_SIZE);
}

return true;
}

bool Dsp16::LoadDrom(std::vector<uint8_t>& 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;
Expand Down Expand Up @@ -116,58 +88,14 @@ 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))
{
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)
{
Expand Down Expand Up @@ -231,7 +159,7 @@ namespace DSP
return 0;
}

uint8_t* ptr = TranslateDMem(addr);
uint8_t* ptr = core->TranslateDMem(addr);

if (ptr)
{
Expand All @@ -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)
{
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
{
Expand Down
20 changes: 0 additions & 20 deletions src/dsp.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -308,18 +294,12 @@ namespace DSP
Dsp16();
~Dsp16();

bool LoadIrom(std::vector<uint8_t>& iromImage);
bool LoadDrom(std::vector<uint8_t>& 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);

Expand Down
80 changes: 76 additions & 4 deletions src/dspcore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -451,6 +451,78 @@ namespace DSP
regs.pc = pc;
}

bool DspCore::LoadIrom(std::vector<uint8_t>& iromImage)
{
if (iromImage.empty() || iromImage.size() != IROM_SIZE)
{
return false;
}
else
{
memcpy(irom, iromImage.data(), IROM_SIZE);
}

return true;
}

bool DspCore::LoadDrom(std::vector<uint8_t>& 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");
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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]);
}

Expand Down
22 changes: 22 additions & 0 deletions src/dspcore.h
Original file line number Diff line number Diff line change
Expand Up @@ -381,9 +381,24 @@ namespace DSP
/// </summary>
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<DspAddress> breakpoints; // IMEM breakpoints
SpinLock breakPointsSpinLock;
DspAddress oneShotBreakpoint = 0xffff;
Expand Down Expand Up @@ -420,6 +435,13 @@ namespace DSP
DspCore(Dsp16* parent);
~DspCore();

bool LoadIrom(std::vector<uint8_t>& iromImage);
bool LoadDrom(std::vector<uint8_t>& 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();
Expand Down
Loading