2010-04-07 15:04:45 +00:00
|
|
|
// Copyright (C) 2010 Dolphin Project.
|
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, version 2.0.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License 2.0 for more details.
|
|
|
|
|
|
|
|
// A copy of the GPL 2.0 should have been included with the program.
|
|
|
|
// If not, see http://www.gnu.org/licenses/
|
|
|
|
|
|
|
|
// Official SVN repository and contact information can be found at
|
|
|
|
// http://code.google.com/p/dolphin-emu/
|
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
#include "DSPEmitter.h"
|
|
|
|
#include "DSPMemoryMap.h"
|
|
|
|
#include "DSPCore.h"
|
|
|
|
#include "DSPInterpreter.h"
|
|
|
|
#include "DSPAnalyzer.h"
|
2011-01-14 18:00:25 +00:00
|
|
|
#include "Jit/DSPJitUtil.h"
|
2010-04-07 15:04:45 +00:00
|
|
|
#include "x64Emitter.h"
|
|
|
|
#include "ABI.h"
|
|
|
|
|
2010-05-29 18:22:50 +00:00
|
|
|
#define MAX_BLOCK_SIZE 250
|
2010-12-29 04:34:33 +00:00
|
|
|
#define DSP_IDLE_SKIP_CYCLES 0x1000
|
2010-04-07 15:04:45 +00:00
|
|
|
|
|
|
|
using namespace Gen;
|
|
|
|
|
2011-01-14 18:00:25 +00:00
|
|
|
DSPEmitter::DSPEmitter() : gpr(*this), storeIndex(-1), storeIndex2(-1)
|
2010-04-07 15:04:45 +00:00
|
|
|
{
|
|
|
|
m_compiledCode = NULL;
|
|
|
|
|
|
|
|
AllocCodeSpace(COMPILED_CODE_SIZE);
|
|
|
|
|
|
|
|
blocks = new CompiledCode[MAX_BLOCKS];
|
2011-01-14 18:00:25 +00:00
|
|
|
blockLinks = new Block[MAX_BLOCKS];
|
|
|
|
blockSize = new u16[MAX_BLOCKS];
|
|
|
|
unresolvedJumps = new std::list<u16>[MAX_BLOCKS];
|
2010-05-29 18:22:50 +00:00
|
|
|
|
2010-04-11 18:06:29 +00:00
|
|
|
compileSR = 0;
|
|
|
|
compileSR |= SR_INT_ENABLE;
|
|
|
|
compileSR |= SR_EXT_INT_ENABLE;
|
2010-05-29 18:22:50 +00:00
|
|
|
|
|
|
|
CompileDispatcher();
|
2010-11-28 13:06:52 +00:00
|
|
|
stubEntryPoint = CompileStub();
|
|
|
|
|
|
|
|
//clear all of the block references
|
|
|
|
for(int i = 0x0000; i < MAX_BLOCKS; i++)
|
|
|
|
{
|
|
|
|
blocks[i] = (CompiledCode)stubEntryPoint;
|
2010-12-15 07:08:36 +00:00
|
|
|
blockLinks[i] = 0;
|
2010-11-28 13:06:52 +00:00
|
|
|
blockSize[i] = 0;
|
|
|
|
}
|
2010-04-07 15:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DSPEmitter::~DSPEmitter()
|
|
|
|
{
|
|
|
|
delete[] blocks;
|
2010-12-15 07:08:36 +00:00
|
|
|
delete[] blockLinks;
|
2010-05-29 18:22:50 +00:00
|
|
|
delete[] blockSize;
|
2010-04-07 15:04:45 +00:00
|
|
|
FreeCodeSpace();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DSPEmitter::ClearIRAM() {
|
2010-05-29 18:22:50 +00:00
|
|
|
// ClearCodeSpace();
|
2010-04-07 15:04:45 +00:00
|
|
|
for(int i = 0x0000; i < 0x1000; i++)
|
|
|
|
{
|
2010-11-28 13:06:52 +00:00
|
|
|
blocks[i] = (CompiledCode)stubEntryPoint;
|
2010-12-15 07:08:36 +00:00
|
|
|
blockLinks[i] = 0;
|
2010-04-07 15:04:45 +00:00
|
|
|
blockSize[i] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-16 10:50:52 +00:00
|
|
|
// Must go out of block if exception is detected
|
2010-12-15 01:42:32 +00:00
|
|
|
void DSPEmitter::checkExceptions(u32 retval)
|
|
|
|
{
|
2010-04-16 10:50:52 +00:00
|
|
|
// Check for interrupts and exceptions
|
2010-10-01 23:23:13 +00:00
|
|
|
#ifdef _M_IX86 // All32
|
2010-04-16 10:50:52 +00:00
|
|
|
TEST(8, M(&g_dsp.exceptions), Imm8(0xff));
|
2010-10-01 23:23:13 +00:00
|
|
|
#else
|
|
|
|
MOV(64, R(RAX), ImmPtr(&g_dsp.exceptions));
|
2010-12-21 14:48:05 +00:00
|
|
|
TEST(8, MatR(RAX), Imm8(0xff));
|
2010-10-01 23:23:13 +00:00
|
|
|
#endif
|
2010-04-16 10:50:52 +00:00
|
|
|
FixupBranch skipCheck = J_CC(CC_Z);
|
2010-12-15 01:42:32 +00:00
|
|
|
|
|
|
|
#ifdef _M_IX86 // All32
|
|
|
|
MOV(16, M(&(g_dsp.pc)), Imm16(compilePC));
|
|
|
|
#else
|
|
|
|
MOV(64, R(RAX), ImmPtr(&(g_dsp.pc)));
|
2010-12-27 23:23:53 +00:00
|
|
|
MOV(16, MatR(RAX), Imm16(compilePC));
|
2010-12-15 01:42:32 +00:00
|
|
|
#endif
|
|
|
|
|
2011-01-14 18:00:25 +00:00
|
|
|
DSPJitRegCache c(gpr);
|
|
|
|
SaveDSPRegs();
|
2010-04-16 10:50:52 +00:00
|
|
|
ABI_CallFunction((void *)&DSPCore_CheckExceptions);
|
2010-12-30 00:10:18 +00:00
|
|
|
MOV(32, R(EAX), Imm32(retval));
|
2011-01-16 06:24:48 +00:00
|
|
|
JMP(returnDispatcher, true);
|
2011-01-14 18:00:25 +00:00
|
|
|
gpr.flushRegs(c,false);
|
|
|
|
|
2010-04-16 10:50:52 +00:00
|
|
|
SetJumpTarget(skipCheck);
|
|
|
|
}
|
|
|
|
|
2010-12-16 23:27:38 +00:00
|
|
|
void DSPEmitter::Default(UDSPInstruction inst)
|
2010-12-15 01:42:32 +00:00
|
|
|
{
|
|
|
|
if (opTable[inst]->reads_pc)
|
|
|
|
{
|
|
|
|
// Increment PC - we shouldn't need to do this for every instruction. only for branches and end of block.
|
|
|
|
// Fallbacks to interpreter need this for fetching immediate values
|
|
|
|
|
|
|
|
#ifdef _M_IX86 // All32
|
|
|
|
MOV(16, M(&(g_dsp.pc)), Imm16(compilePC + 1));
|
|
|
|
#else
|
|
|
|
MOV(64, R(RAX), ImmPtr(&(g_dsp.pc)));
|
2010-12-21 14:48:05 +00:00
|
|
|
MOV(16, MatR(RAX), Imm16(compilePC + 1));
|
2010-12-15 01:42:32 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fall back to interpreter
|
2010-12-30 00:10:18 +00:00
|
|
|
SaveDSPRegs();
|
2010-12-15 01:42:32 +00:00
|
|
|
ABI_CallFunctionC16((void*)opTable[inst]->intFunc, inst);
|
2010-12-30 00:10:18 +00:00
|
|
|
LoadDSPRegs();
|
2010-12-15 01:42:32 +00:00
|
|
|
}
|
|
|
|
|
2010-05-29 18:22:50 +00:00
|
|
|
void DSPEmitter::EmitInstruction(UDSPInstruction inst)
|
2010-04-07 15:04:45 +00:00
|
|
|
{
|
|
|
|
const DSPOPCTemplate *tinst = GetOpTemplate(inst);
|
2010-10-31 23:26:18 +00:00
|
|
|
bool ext_is_jit = false;
|
2010-04-07 15:04:45 +00:00
|
|
|
|
|
|
|
// Call extended
|
|
|
|
if (tinst->extended) {
|
|
|
|
if ((inst >> 12) == 0x3) {
|
2010-12-29 20:20:52 +00:00
|
|
|
if (! extOpTable[inst & 0x7F]->jitFunc) {
|
2010-05-29 18:22:50 +00:00
|
|
|
// Fall back to interpreter
|
2010-12-30 00:10:18 +00:00
|
|
|
SaveDSPRegs();
|
2010-04-07 15:04:45 +00:00
|
|
|
ABI_CallFunctionC16((void*)extOpTable[inst & 0x7F]->intFunc, inst);
|
2010-12-30 00:10:18 +00:00
|
|
|
LoadDSPRegs();
|
|
|
|
INFO_LOG(DSPLLE, "Instruction not JITed(ext part): %04x\n", inst);
|
2010-10-31 23:26:18 +00:00
|
|
|
ext_is_jit = false;
|
2010-04-07 15:04:45 +00:00
|
|
|
} else {
|
|
|
|
(this->*extOpTable[inst & 0x7F]->jitFunc)(inst);
|
2010-10-31 23:26:18 +00:00
|
|
|
ext_is_jit = true;
|
2010-04-07 15:04:45 +00:00
|
|
|
}
|
|
|
|
} else {
|
2010-12-29 20:20:52 +00:00
|
|
|
if (!extOpTable[inst & 0xFF]->jitFunc) {
|
2010-05-29 18:22:50 +00:00
|
|
|
// Fall back to interpreter
|
2010-12-30 00:10:18 +00:00
|
|
|
SaveDSPRegs();
|
2010-04-07 15:04:45 +00:00
|
|
|
ABI_CallFunctionC16((void*)extOpTable[inst & 0xFF]->intFunc, inst);
|
2010-12-30 00:10:18 +00:00
|
|
|
LoadDSPRegs();
|
|
|
|
INFO_LOG(DSPLLE, "Instruction not JITed(ext part): %04x\n", inst);
|
2010-10-31 23:26:18 +00:00
|
|
|
ext_is_jit = false;
|
2010-04-07 15:04:45 +00:00
|
|
|
} else {
|
|
|
|
(this->*extOpTable[inst & 0xFF]->jitFunc)(inst);
|
2010-10-31 23:26:18 +00:00
|
|
|
ext_is_jit = true;
|
2010-04-07 15:04:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Main instruction
|
2010-12-29 20:20:52 +00:00
|
|
|
if (!opTable[inst]->jitFunc) {
|
2010-12-16 23:27:38 +00:00
|
|
|
Default(inst);
|
2010-12-30 00:10:18 +00:00
|
|
|
INFO_LOG(DSPLLE, "Instruction not JITed(main part): %04x\n", inst);
|
2010-05-29 18:22:50 +00:00
|
|
|
}
|
2010-04-07 15:04:45 +00:00
|
|
|
else
|
2010-05-29 18:22:50 +00:00
|
|
|
{
|
2010-04-07 15:04:45 +00:00
|
|
|
(this->*opTable[inst]->jitFunc)(inst);
|
2010-05-29 18:22:50 +00:00
|
|
|
}
|
2010-04-07 15:04:45 +00:00
|
|
|
|
|
|
|
// Backlog
|
|
|
|
if (tinst->extended) {
|
2010-10-31 23:26:18 +00:00
|
|
|
if (!ext_is_jit) {
|
|
|
|
//need to call the online cleanup function because
|
|
|
|
//the writeBackLog gets populated at runtime
|
2010-12-30 00:10:18 +00:00
|
|
|
SaveDSPRegs();
|
2010-10-31 23:26:18 +00:00
|
|
|
ABI_CallFunction((void*)::applyWriteBackLog);
|
2010-12-30 00:10:18 +00:00
|
|
|
LoadDSPRegs();
|
2010-04-19 13:02:24 +00:00
|
|
|
} else {
|
|
|
|
popExtValueToReg();
|
|
|
|
}
|
2010-04-07 15:04:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DSPEmitter::unknown_instruction(UDSPInstruction inst)
|
|
|
|
{
|
|
|
|
PanicAlert("unknown_instruction %04x - Fix me ;)", inst);
|
|
|
|
}
|
|
|
|
|
2011-01-03 10:39:48 +00:00
|
|
|
void DSPEmitter::Compile(u16 start_addr)
|
2010-11-28 05:28:21 +00:00
|
|
|
{
|
2010-12-15 22:13:31 +00:00
|
|
|
// Remember the current block address for later
|
|
|
|
startAddr = start_addr;
|
2011-01-03 10:39:48 +00:00
|
|
|
unresolvedJumps[start_addr].clear();
|
2010-12-15 22:13:31 +00:00
|
|
|
|
2010-11-28 05:28:21 +00:00
|
|
|
const u8 *entryPoint = AlignCode16();
|
2010-04-07 15:04:45 +00:00
|
|
|
|
2010-10-14 11:37:59 +00:00
|
|
|
/*
|
|
|
|
// Check for other exceptions
|
|
|
|
if (dsp_SR_is_flag_set(SR_INT_ENABLE))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (g_dsp.exceptions == 0)
|
|
|
|
return;
|
|
|
|
*/
|
2010-12-15 07:08:36 +00:00
|
|
|
|
2011-01-14 18:00:25 +00:00
|
|
|
LoadDSPRegs();
|
|
|
|
|
2010-12-15 07:08:36 +00:00
|
|
|
blockLinkEntry = GetCodePtr();
|
|
|
|
|
2010-12-15 01:42:32 +00:00
|
|
|
compilePC = start_addr;
|
|
|
|
bool fixup_pc = false;
|
2010-05-29 21:43:46 +00:00
|
|
|
blockSize[start_addr] = 0;
|
2010-12-15 01:42:32 +00:00
|
|
|
|
|
|
|
while (compilePC < start_addr + MAX_BLOCK_SIZE)
|
2010-04-07 15:04:45 +00:00
|
|
|
{
|
2011-01-03 14:19:11 +00:00
|
|
|
if (DSPAnalyzer::code_flags[compilePC] & DSPAnalyzer::CODE_CHECK_INT)
|
|
|
|
checkExceptions(blockSize[start_addr]);
|
2010-10-14 11:37:59 +00:00
|
|
|
|
2010-12-15 01:42:32 +00:00
|
|
|
UDSPInstruction inst = dsp_imem_read(compilePC);
|
2010-04-07 15:04:45 +00:00
|
|
|
const DSPOPCTemplate *opcode = GetOpTemplate(inst);
|
2010-10-09 21:43:57 +00:00
|
|
|
|
2010-05-29 18:22:50 +00:00
|
|
|
EmitInstruction(inst);
|
2010-10-09 21:43:57 +00:00
|
|
|
|
|
|
|
blockSize[start_addr]++;
|
2010-12-15 01:42:32 +00:00
|
|
|
compilePC += opcode->size;
|
|
|
|
|
2011-01-03 10:39:48 +00:00
|
|
|
// If the block was trying to link into itself, remove the link
|
|
|
|
unresolvedJumps[start_addr].remove(compilePC);
|
|
|
|
|
2010-12-15 01:42:32 +00:00
|
|
|
fixup_pc = true;
|
2010-10-09 21:43:57 +00:00
|
|
|
|
2010-05-29 18:22:50 +00:00
|
|
|
// Handle loop condition, only if current instruction was flagged as a loop destination
|
2010-10-14 11:37:59 +00:00
|
|
|
// by the analyzer.
|
2010-12-15 01:42:32 +00:00
|
|
|
if (DSPAnalyzer::code_flags[compilePC-1] & DSPAnalyzer::CODE_LOOP_END)
|
2010-05-29 18:22:50 +00:00
|
|
|
{
|
2010-10-01 23:23:13 +00:00
|
|
|
#ifdef _M_IX86 // All32
|
2010-12-29 20:20:52 +00:00
|
|
|
MOVZX(32, 16, EAX, M(&(g_dsp.r.st[2])));
|
2010-10-01 23:23:13 +00:00
|
|
|
#else
|
2010-12-29 20:20:52 +00:00
|
|
|
MOV(64, R(R11), ImmPtr(&g_dsp.r));
|
2010-12-30 00:10:18 +00:00
|
|
|
MOVZX(32, 16, EAX, MDisp(R11, STRUCT_OFFSET(g_dsp.r, st[2])));
|
2010-10-01 23:23:13 +00:00
|
|
|
#endif
|
2010-05-29 18:22:50 +00:00
|
|
|
CMP(32, R(EAX), Imm32(0));
|
2010-12-26 12:34:38 +00:00
|
|
|
FixupBranch rLoopAddressExit = J_CC(CC_LE, true);
|
2010-12-30 00:10:18 +00:00
|
|
|
|
2010-10-01 23:23:13 +00:00
|
|
|
#ifdef _M_IX86 // All32
|
2010-12-29 20:20:52 +00:00
|
|
|
MOVZX(32, 16, EAX, M(&g_dsp.r.st[3]));
|
2010-10-01 23:23:13 +00:00
|
|
|
#else
|
2010-12-30 00:10:18 +00:00
|
|
|
MOVZX(32, 16, EAX, MDisp(R11, STRUCT_OFFSET(g_dsp.r, st[3])));
|
2010-10-01 23:23:13 +00:00
|
|
|
#endif
|
2010-05-29 18:22:50 +00:00
|
|
|
CMP(32, R(EAX), Imm32(0));
|
2010-12-26 12:34:38 +00:00
|
|
|
FixupBranch rLoopCounterExit = J_CC(CC_LE, true);
|
2010-05-29 18:22:50 +00:00
|
|
|
|
2010-12-15 01:42:32 +00:00
|
|
|
if (!opcode->branch)
|
|
|
|
{
|
|
|
|
//branch insns update the g_dsp.pc
|
|
|
|
#ifdef _M_IX86 // All32
|
|
|
|
MOV(16, M(&(g_dsp.pc)), Imm16(compilePC));
|
|
|
|
#else
|
|
|
|
MOV(64, R(RAX), ImmPtr(&(g_dsp.pc)));
|
2010-12-27 23:23:53 +00:00
|
|
|
MOV(16, MatR(RAX), Imm16(compilePC));
|
2010-12-15 01:42:32 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2010-05-29 18:22:50 +00:00
|
|
|
// These functions branch and therefore only need to be called in the
|
|
|
|
// end of each block and in this order
|
2011-01-14 18:00:25 +00:00
|
|
|
DSPJitRegCache c(gpr);
|
2010-12-26 12:34:38 +00:00
|
|
|
HandleLoop();
|
2011-01-14 18:00:25 +00:00
|
|
|
SaveDSPRegs();
|
2010-11-28 11:04:50 +00:00
|
|
|
if (DSPAnalyzer::code_flags[start_addr] & DSPAnalyzer::CODE_IDLE_SKIP)
|
|
|
|
{
|
2010-12-29 04:34:33 +00:00
|
|
|
MOV(16, R(EAX), Imm16(DSP_IDLE_SKIP_CYCLES));
|
2010-11-28 11:04:50 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-12-29 04:34:33 +00:00
|
|
|
MOV(16, R(EAX), Imm16(blockSize[start_addr]));
|
2011-01-14 18:00:25 +00:00
|
|
|
}
|
2011-01-16 06:24:48 +00:00
|
|
|
JMP(returnDispatcher, true);
|
2011-01-14 18:00:25 +00:00
|
|
|
gpr.flushRegs(c,false);
|
2010-05-29 18:22:50 +00:00
|
|
|
|
|
|
|
SetJumpTarget(rLoopAddressExit);
|
|
|
|
SetJumpTarget(rLoopCounterExit);
|
2010-04-07 15:04:45 +00:00
|
|
|
}
|
|
|
|
|
2010-11-28 05:28:21 +00:00
|
|
|
if (opcode->branch)
|
|
|
|
{
|
2010-12-15 01:42:32 +00:00
|
|
|
//don't update g_dsp.pc -- the branch insn already did
|
|
|
|
fixup_pc = false;
|
2010-11-28 05:28:21 +00:00
|
|
|
if (opcode->uncond_branch)
|
|
|
|
{
|
2010-10-14 13:17:38 +00:00
|
|
|
break;
|
2010-11-28 05:28:21 +00:00
|
|
|
}
|
2010-12-29 04:34:33 +00:00
|
|
|
else if (!opcode->jitFunc)
|
2010-11-28 05:28:21 +00:00
|
|
|
{
|
2010-10-14 13:17:38 +00:00
|
|
|
//look at g_dsp.pc if we actually branched
|
|
|
|
#ifdef _M_IX86 // All32
|
2010-11-28 05:28:21 +00:00
|
|
|
MOV(16, R(AX), M(&g_dsp.pc));
|
2010-10-14 13:17:38 +00:00
|
|
|
#else
|
2010-11-28 16:29:58 +00:00
|
|
|
MOV(64, R(RAX), ImmPtr(&(g_dsp.pc)));
|
2010-12-27 23:23:53 +00:00
|
|
|
MOV(16, R(AX), MatR(RAX));
|
2010-10-14 13:17:38 +00:00
|
|
|
#endif
|
2010-12-15 01:42:32 +00:00
|
|
|
CMP(16, R(AX), Imm16(compilePC));
|
2010-10-14 13:17:38 +00:00
|
|
|
FixupBranch rNoBranch = J_CC(CC_Z);
|
|
|
|
|
2011-01-14 18:00:25 +00:00
|
|
|
DSPJitRegCache c(gpr);
|
2010-12-15 01:42:32 +00:00
|
|
|
//don't update g_dsp.pc -- the branch insn already did
|
2011-01-14 18:00:25 +00:00
|
|
|
SaveDSPRegs();
|
2010-11-28 11:04:50 +00:00
|
|
|
if (DSPAnalyzer::code_flags[start_addr] & DSPAnalyzer::CODE_IDLE_SKIP)
|
|
|
|
{
|
2010-12-29 04:34:33 +00:00
|
|
|
MOV(16, R(EAX), Imm16(DSP_IDLE_SKIP_CYCLES));
|
2010-11-28 11:04:50 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-12-29 04:34:33 +00:00
|
|
|
MOV(16, R(EAX), Imm16(blockSize[start_addr]));
|
2011-01-14 18:00:25 +00:00
|
|
|
}
|
2011-01-16 06:24:48 +00:00
|
|
|
JMP(returnDispatcher, true);
|
2011-01-14 18:00:25 +00:00
|
|
|
gpr.flushRegs(c,false);
|
2010-10-14 13:17:38 +00:00
|
|
|
|
|
|
|
SetJumpTarget(rNoBranch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// End the block if we're before an idle skip address
|
2010-12-15 01:42:32 +00:00
|
|
|
if (DSPAnalyzer::code_flags[compilePC] & DSPAnalyzer::CODE_IDLE_SKIP)
|
2010-11-28 05:28:21 +00:00
|
|
|
{
|
2010-04-07 15:04:45 +00:00
|
|
|
break;
|
2010-04-09 15:11:02 +00:00
|
|
|
}
|
2010-04-07 15:04:45 +00:00
|
|
|
}
|
|
|
|
|
2010-12-15 01:42:32 +00:00
|
|
|
if (fixup_pc) {
|
|
|
|
#ifdef _M_IX86 // All32
|
|
|
|
MOV(16, M(&(g_dsp.pc)), Imm16(compilePC));
|
|
|
|
#else
|
|
|
|
MOV(64, R(RAX), ImmPtr(&(g_dsp.pc)));
|
2010-12-27 23:23:53 +00:00
|
|
|
MOV(16, MatR(RAX), Imm16(compilePC));
|
2010-12-15 01:42:32 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2010-04-07 15:04:45 +00:00
|
|
|
blocks[start_addr] = (CompiledCode)entryPoint;
|
2010-12-15 22:13:31 +00:00
|
|
|
|
|
|
|
// Mark this block as a linkable destination if it does not contain
|
|
|
|
// any unresolved CALL's
|
2011-01-03 10:39:48 +00:00
|
|
|
if (unresolvedJumps[start_addr].empty())
|
2010-12-30 00:10:18 +00:00
|
|
|
{
|
2011-01-14 18:00:25 +00:00
|
|
|
blockLinks[start_addr] = blockLinkEntry;
|
2011-01-03 10:39:48 +00:00
|
|
|
|
|
|
|
for(u16 i = 0x0000; i < 0xffff; ++i)
|
|
|
|
{
|
|
|
|
if (!unresolvedJumps[i].empty())
|
|
|
|
{
|
|
|
|
// Check if there were any blocks waiting for this block to be linkable
|
2011-01-04 02:07:17 +00:00
|
|
|
unsigned int size = unresolvedJumps[i].size();
|
2011-01-03 10:39:48 +00:00
|
|
|
unresolvedJumps[i].remove(start_addr);
|
|
|
|
if (unresolvedJumps[i].size() < size)
|
|
|
|
{
|
|
|
|
// Mark the block to be recompiled again
|
|
|
|
blocks[i] = (CompiledCode)stubEntryPoint;
|
|
|
|
blockLinks[i] = 0;
|
|
|
|
blockSize[i] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-12-30 00:10:18 +00:00
|
|
|
}
|
2010-12-15 22:13:31 +00:00
|
|
|
|
2010-06-30 16:17:20 +00:00
|
|
|
if (blockSize[start_addr] == 0)
|
|
|
|
{
|
|
|
|
// just a safeguard, should never happen anymore.
|
|
|
|
// if it does we might get stuck over in RunForCycles.
|
|
|
|
ERROR_LOG(DSPLLE, "Block at 0x%04x has zero size", start_addr);
|
|
|
|
blockSize[start_addr] = 1;
|
|
|
|
}
|
2010-04-07 15:04:45 +00:00
|
|
|
|
2010-12-30 00:10:18 +00:00
|
|
|
SaveDSPRegs();
|
2010-11-28 11:04:50 +00:00
|
|
|
if (DSPAnalyzer::code_flags[start_addr] & DSPAnalyzer::CODE_IDLE_SKIP)
|
|
|
|
{
|
2010-12-29 04:34:33 +00:00
|
|
|
MOV(16, R(EAX), Imm16(DSP_IDLE_SKIP_CYCLES));
|
2010-11-28 11:04:50 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-12-29 04:34:33 +00:00
|
|
|
MOV(16, R(EAX), Imm16(blockSize[start_addr]));
|
2010-11-28 11:04:50 +00:00
|
|
|
}
|
2011-01-16 06:24:48 +00:00
|
|
|
JMP(returnDispatcher, true);
|
2010-04-07 15:04:45 +00:00
|
|
|
}
|
|
|
|
|
2010-11-28 13:06:52 +00:00
|
|
|
const u8 *DSPEmitter::CompileStub()
|
|
|
|
{
|
|
|
|
const u8 *entryPoint = AlignCode16();
|
2010-11-28 16:29:58 +00:00
|
|
|
ABI_CallFunction((void *)&CompileCurrent);
|
2010-11-28 13:06:52 +00:00
|
|
|
//MOVZX(32, 16, ECX, M(&g_dsp.pc));
|
|
|
|
XOR(32, R(EAX), R(EAX)); // Return 0 cycles executed
|
2011-01-16 06:24:48 +00:00
|
|
|
JMP(returnDispatcher);
|
2010-11-28 13:06:52 +00:00
|
|
|
return entryPoint;
|
|
|
|
}
|
|
|
|
|
2010-11-28 05:28:21 +00:00
|
|
|
void DSPEmitter::CompileDispatcher()
|
2010-05-29 18:22:50 +00:00
|
|
|
{
|
2010-11-28 05:28:21 +00:00
|
|
|
enterDispatcher = AlignCode16();
|
2010-05-29 21:34:34 +00:00
|
|
|
ABI_PushAllCalleeSavedRegsAndAdjustStack();
|
|
|
|
|
2010-11-28 05:28:21 +00:00
|
|
|
const u8 *dispatcherLoop = GetCodePtr();
|
|
|
|
|
|
|
|
// Check for DSP halt
|
2010-11-28 16:29:58 +00:00
|
|
|
#ifdef _M_IX86
|
2010-11-28 05:28:21 +00:00
|
|
|
TEST(8, M(&g_dsp.cr), Imm8(CR_HALT));
|
2010-11-28 16:29:58 +00:00
|
|
|
#else
|
2010-12-30 00:10:18 +00:00
|
|
|
MOV(64, R(RAX), ImmPtr(&g_dsp.cr));
|
|
|
|
TEST(8, MatR(RAX), Imm8(CR_HALT));
|
2010-11-28 16:29:58 +00:00
|
|
|
#endif
|
2011-01-01 20:00:03 +00:00
|
|
|
FixupBranch _halt = J_CC(CC_NE);
|
2010-11-28 05:28:21 +00:00
|
|
|
|
2010-11-28 16:29:58 +00:00
|
|
|
#ifdef _M_IX86
|
2010-11-28 05:28:21 +00:00
|
|
|
MOVZX(32, 16, ECX, M(&g_dsp.pc));
|
2010-11-28 16:29:58 +00:00
|
|
|
#else
|
|
|
|
MOV(64, R(RCX), ImmPtr(&g_dsp.pc));
|
2010-12-29 02:12:06 +00:00
|
|
|
MOVZX(64, 16, RCX, MatR(RCX));
|
2010-11-28 16:29:58 +00:00
|
|
|
#endif
|
2010-11-28 05:28:21 +00:00
|
|
|
|
|
|
|
// Execute block. Cycles executed returned in EAX.
|
|
|
|
#ifdef _M_IX86
|
2011-01-16 06:24:48 +00:00
|
|
|
MOV(32, R(EBX), ImmPtr(blocks));
|
|
|
|
JMPptr(MComplex(EBX, ECX, SCALE_4, 0));
|
2010-11-28 05:28:21 +00:00
|
|
|
#else
|
2011-01-16 06:24:48 +00:00
|
|
|
MOV(64, R(RBX), ImmPtr(blocks));
|
|
|
|
JMPptr(MComplex(RBX, RCX, SCALE_8, 0));
|
2010-11-28 05:28:21 +00:00
|
|
|
#endif
|
|
|
|
|
2011-01-16 06:24:48 +00:00
|
|
|
returnDispatcher = GetCodePtr();
|
|
|
|
|
2010-11-28 05:28:21 +00:00
|
|
|
// Decrement cyclesLeft
|
2010-11-28 16:29:58 +00:00
|
|
|
#ifdef _M_IX86
|
2011-01-16 06:24:48 +00:00
|
|
|
SUB(16, M(&cyclesLeft), R(EAX));
|
2010-11-28 16:29:58 +00:00
|
|
|
#else
|
2011-01-16 06:24:48 +00:00
|
|
|
MOV(64, R(R12), ImmPtr(&cyclesLeft));
|
|
|
|
SUB(16, MatR(R12), R(EAX));
|
2010-11-28 16:29:58 +00:00
|
|
|
#endif
|
2010-11-28 13:06:52 +00:00
|
|
|
|
2010-11-28 05:28:21 +00:00
|
|
|
J_CC(CC_A, dispatcherLoop);
|
|
|
|
|
|
|
|
// DSP gave up the remaining cycles.
|
2011-01-01 20:00:03 +00:00
|
|
|
SetJumpTarget(_halt);
|
2010-11-28 05:28:21 +00:00
|
|
|
//MOV(32, M(&cyclesLeft), Imm32(0));
|
2010-05-29 21:34:34 +00:00
|
|
|
ABI_PopAllCalleeSavedRegsAndAdjustStack();
|
2010-11-28 05:28:21 +00:00
|
|
|
RET();
|
2010-05-29 18:22:50 +00:00
|
|
|
}
|