2015-08-26 04:54:06 +02:00
|
|
|
#include "stdafx.h"
|
|
|
|
|
#include "Emu/IdManager.h"
|
|
|
|
|
#include "Emu/Memory/Memory.h"
|
|
|
|
|
|
|
|
|
|
#include "SPUThread.h"
|
|
|
|
|
#include "SPURecompiler.h"
|
|
|
|
|
#include "SPUASMJITRecompiler.h"
|
2017-07-20 16:20:28 +02:00
|
|
|
#include <algorithm>
|
2015-08-26 04:54:06 +02:00
|
|
|
|
|
|
|
|
extern u64 get_system_time();
|
|
|
|
|
|
2017-01-25 00:22:19 +01:00
|
|
|
spu_recompiler_base::~spu_recompiler_base()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-14 01:09:41 +02:00
|
|
|
void spu_recompiler_base::enter(SPUThread& spu)
|
2015-08-26 04:54:06 +02:00
|
|
|
{
|
2016-04-14 01:09:41 +02:00
|
|
|
if (spu.pc >= 0x40000 || spu.pc % 4)
|
2015-08-26 04:54:06 +02:00
|
|
|
{
|
2016-08-08 18:01:06 +02:00
|
|
|
fmt::throw_exception("Invalid PC: 0x%05x", spu.pc);
|
2015-08-26 04:54:06 +02:00
|
|
|
}
|
|
|
|
|
|
2016-04-14 01:09:41 +02:00
|
|
|
// Get SPU LS pointer
|
2018-02-09 15:49:37 +01:00
|
|
|
const auto _ls = vm::_ptr<u32>(spu.offset);
|
2015-08-26 04:54:06 +02:00
|
|
|
|
2017-07-19 18:35:05 +02:00
|
|
|
// Search if cached data matches
|
|
|
|
|
auto func = spu.compiled_cache[spu.pc / 4];
|
|
|
|
|
|
|
|
|
|
// Check shared db if we dont have a match
|
2017-07-20 16:20:28 +02:00
|
|
|
if (!func || !std::equal(func->data.begin(), func->data.end(), _ls + spu.pc / 4, [](const be_t<u32>& l, const be_t<u32>& r) { return *(u32*)(u8*)&l == *(u32*)(u8*)&r; }))
|
2017-07-19 18:35:05 +02:00
|
|
|
{
|
2017-07-29 15:07:03 +02:00
|
|
|
func = spu.spu_db->analyse(_ls, spu.pc);
|
2017-07-19 18:35:05 +02:00
|
|
|
spu.compiled_cache[spu.pc / 4] = func;
|
|
|
|
|
}
|
2015-08-26 04:54:06 +02:00
|
|
|
|
2016-04-14 01:09:41 +02:00
|
|
|
// Reset callstack if necessary
|
2017-02-22 11:10:55 +01:00
|
|
|
if ((func->does_reset_stack && spu.recursion_level) || spu.recursion_level >= 128)
|
2015-08-26 04:54:06 +02:00
|
|
|
{
|
2016-08-09 16:14:41 +02:00
|
|
|
spu.state += cpu_flag::ret;
|
2016-04-14 01:09:41 +02:00
|
|
|
return;
|
2015-09-04 01:23:31 +02:00
|
|
|
}
|
2015-08-26 04:54:06 +02:00
|
|
|
|
2017-07-19 18:35:05 +02:00
|
|
|
// Compile if needed
|
2015-09-04 01:23:31 +02:00
|
|
|
if (!func->compiled)
|
|
|
|
|
{
|
2016-04-14 01:09:41 +02:00
|
|
|
if (!spu.spu_rec)
|
|
|
|
|
{
|
|
|
|
|
spu.spu_rec = fxm::get_always<spu_recompiler>();
|
|
|
|
|
}
|
2015-08-26 04:54:06 +02:00
|
|
|
|
2016-04-14 01:09:41 +02:00
|
|
|
spu.spu_rec->compile(*func);
|
|
|
|
|
|
2016-08-08 18:01:06 +02:00
|
|
|
if (!func->compiled) fmt::throw_exception("Compilation failed" HERE);
|
2015-08-26 04:54:06 +02:00
|
|
|
}
|
|
|
|
|
|
2015-09-04 01:23:31 +02:00
|
|
|
const u32 res = func->compiled(&spu, _ls);
|
2015-08-26 04:54:06 +02:00
|
|
|
|
|
|
|
|
if (const auto exception = spu.pending_exception)
|
|
|
|
|
{
|
|
|
|
|
spu.pending_exception = nullptr;
|
|
|
|
|
std::rethrow_exception(exception);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (res & 0x1000000)
|
|
|
|
|
{
|
|
|
|
|
spu.halt();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (res & 0x2000000)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (res & 0x4000000)
|
|
|
|
|
{
|
|
|
|
|
if (res & 0x8000000)
|
|
|
|
|
{
|
2016-08-08 18:01:06 +02:00
|
|
|
fmt::throw_exception("Invalid interrupt status set (0x%x)" HERE, res);
|
2015-08-26 04:54:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
spu.set_interrupt_status(true);
|
|
|
|
|
}
|
|
|
|
|
else if (res & 0x8000000)
|
|
|
|
|
{
|
|
|
|
|
spu.set_interrupt_status(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
spu.pc = res & 0x3fffc;
|
2017-02-13 14:12:24 +01:00
|
|
|
|
2017-12-01 03:50:01 +01:00
|
|
|
if (spu.interrupts_enabled && (spu.ch_event_mask & spu.ch_event_stat & SPU_EVENT_INTR_IMPLEMENTED) > 0)
|
2017-02-13 14:12:24 +01:00
|
|
|
{
|
2017-12-01 21:11:06 +01:00
|
|
|
spu.interrupts_enabled = false;
|
2017-02-13 14:12:24 +01:00
|
|
|
spu.srr0 = std::exchange(spu.pc, 0);
|
|
|
|
|
}
|
2015-08-26 04:54:06 +02:00
|
|
|
}
|