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"
|
|
|
|
#include "x64Emitter.h"
|
|
|
|
#include "ABI.h"
|
|
|
|
|
2010-05-29 18:22:50 +00:00
|
|
|
#define MAX_BLOCK_SIZE 250
|
2010-04-07 15:04:45 +00:00
|
|
|
|
|
|
|
using namespace Gen;
|
|
|
|
|
2010-04-19 13:29:05 +00:00
|
|
|
DSPEmitter::DSPEmitter() : storeIndex(-1)
|
2010-04-07 15:04:45 +00:00
|
|
|
{
|
|
|
|
m_compiledCode = NULL;
|
|
|
|
|
|
|
|
AllocCodeSpace(COMPILED_CODE_SIZE);
|
|
|
|
|
|
|
|
blocks = new CompiledCode[MAX_BLOCKS];
|
2010-05-29 18:22:50 +00:00
|
|
|
blockSize = new u16[0x10000];
|
|
|
|
|
|
|
|
ClearIRAM();
|
2010-04-07 15:04:45 +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-04-07 15:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DSPEmitter::~DSPEmitter()
|
|
|
|
{
|
|
|
|
delete[] blocks;
|
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-05-29 18:22:50 +00:00
|
|
|
blocks[i] = NULL;
|
2010-04-07 15:04:45 +00:00
|
|
|
blockSize[i] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-17 20:42:39 +00:00
|
|
|
|
2010-04-16 10:50:52 +00:00
|
|
|
// Must go out of block if exception is detected
|
|
|
|
void DSPEmitter::checkExceptions() {
|
2010-04-17 20:42:39 +00:00
|
|
|
/*
|
|
|
|
// check if there is an external interrupt
|
|
|
|
if (! dsp_SR_is_flag_set(SR_EXT_INT_ENABLE))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (! (g_dsp.cr & CR_EXTERNAL_INT))
|
|
|
|
return;
|
|
|
|
|
|
|
|
g_dsp.cr &= ~CR_EXTERNAL_INT;
|
|
|
|
|
|
|
|
// Check for other exceptions
|
|
|
|
if (dsp_SR_is_flag_set(SR_INT_ENABLE))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (g_dsp.exceptions == 0)
|
|
|
|
return;
|
|
|
|
*/
|
2010-04-16 10:50:52 +00:00
|
|
|
ABI_CallFunction((void *)&DSPCore_CheckExternalInterrupt);
|
|
|
|
// 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));
|
|
|
|
TEST(8, MDisp(RAX,0), Imm8(0xff));
|
|
|
|
#endif
|
2010-04-16 10:50:52 +00:00
|
|
|
FixupBranch skipCheck = J_CC(CC_Z);
|
|
|
|
|
|
|
|
ABI_CallFunction((void *)&DSPCore_CheckExceptions);
|
|
|
|
|
2010-04-23 08:15:00 +00:00
|
|
|
// ABI_RestoreStack(0);
|
|
|
|
ABI_PopAllCalleeSavedRegsAndAdjustStack();
|
2010-04-16 10:50:52 +00:00
|
|
|
RET();
|
|
|
|
|
|
|
|
SetJumpTarget(skipCheck);
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
// Call extended
|
|
|
|
if (tinst->extended) {
|
|
|
|
if ((inst >> 12) == 0x3) {
|
|
|
|
if (! extOpTable[inst & 0x7F]->jitFunc) {
|
2010-05-29 18:22:50 +00:00
|
|
|
// Fall back to interpreter
|
2010-04-07 15:04:45 +00:00
|
|
|
ABI_CallFunctionC16((void*)extOpTable[inst & 0x7F]->intFunc, inst);
|
|
|
|
} else {
|
|
|
|
(this->*extOpTable[inst & 0x7F]->jitFunc)(inst);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!extOpTable[inst & 0xFF]->jitFunc) {
|
2010-05-29 18:22:50 +00:00
|
|
|
// Fall back to interpreter
|
2010-04-07 15:04:45 +00:00
|
|
|
ABI_CallFunctionC16((void*)extOpTable[inst & 0xFF]->intFunc, inst);
|
|
|
|
} else {
|
|
|
|
(this->*extOpTable[inst & 0xFF]->jitFunc)(inst);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Main instruction
|
2010-05-29 18:22:50 +00:00
|
|
|
if (!opTable[inst]->jitFunc) {
|
|
|
|
// Fall back to interpreter
|
2010-04-07 15:04:45 +00:00
|
|
|
ABI_CallFunctionC16((void*)opTable[inst]->intFunc, 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-04-19 13:02:24 +00:00
|
|
|
if (! extOpTable[inst & 0x7F]->jitFunc) {
|
|
|
|
ABI_CallFunction((void*)applyWriteBackLog);
|
|
|
|
} else {
|
|
|
|
popExtValueToReg();
|
|
|
|
}
|
2010-04-07 15:04:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DSPEmitter::unknown_instruction(UDSPInstruction inst)
|
|
|
|
{
|
|
|
|
PanicAlert("unknown_instruction %04x - Fix me ;)", inst);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DSPEmitter::Default(UDSPInstruction _inst)
|
|
|
|
{
|
2010-05-29 18:22:50 +00:00
|
|
|
EmitInstruction(_inst);
|
2010-04-07 15:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const u8 *DSPEmitter::Compile(int start_addr) {
|
|
|
|
AlignCode16();
|
|
|
|
const u8 *entryPoint = GetCodePtr();
|
2010-04-23 08:15:00 +00:00
|
|
|
ABI_PushAllCalleeSavedRegsAndAdjustStack();
|
|
|
|
// ABI_AlignStack(0);
|
2010-04-07 15:04:45 +00:00
|
|
|
|
|
|
|
int addr = start_addr;
|
2010-04-16 10:50:52 +00:00
|
|
|
checkExceptions();
|
2010-05-29 21:43:46 +00:00
|
|
|
blockSize[start_addr] = 0;
|
2010-05-29 18:22:50 +00:00
|
|
|
while (addr < start_addr + MAX_BLOCK_SIZE)
|
2010-04-07 15:04:45 +00:00
|
|
|
{
|
|
|
|
UDSPInstruction inst = dsp_imem_read(addr);
|
|
|
|
const DSPOPCTemplate *opcode = GetOpTemplate(inst);
|
|
|
|
|
2010-05-29 18:22:50 +00:00
|
|
|
// Increment PC - we shouldn't need to do this for every instruction. only for branches and end of block.
|
2010-10-01 23:23:13 +00:00
|
|
|
#ifdef _M_IX86 // All32
|
2010-04-07 15:04:45 +00:00
|
|
|
ADD(16, M(&(g_dsp.pc)), Imm16(1));
|
2010-10-01 23:23:13 +00:00
|
|
|
#else
|
|
|
|
MOV(64, R(RAX), ImmPtr(&(g_dsp.pc)));
|
|
|
|
ADD(16, MDisp(RAX,0), Imm16(1));
|
|
|
|
#endif
|
2010-04-07 15:04:45 +00:00
|
|
|
|
2010-05-29 18:22:50 +00:00
|
|
|
EmitInstruction(inst);
|
2010-04-07 15:04:45 +00:00
|
|
|
|
2010-05-29 18:22:50 +00:00
|
|
|
// Handle loop condition, only if current instruction was flagged as a loop destination
|
|
|
|
// by the analyzer. COMMENTED OUT - this breaks Zelda TP. Bah.
|
2010-04-07 15:04:45 +00:00
|
|
|
|
2010-05-29 18:22:50 +00:00
|
|
|
// if (DSPAnalyzer::code_flags[addr] & DSPAnalyzer::CODE_LOOP_END)
|
|
|
|
{
|
|
|
|
// TODO: Change to TEST for some reason (who added this comment?)
|
2010-10-01 23:23:13 +00:00
|
|
|
#ifdef _M_IX86 // All32
|
2010-05-29 18:22:50 +00:00
|
|
|
MOVZX(32, 16, EAX, M(&(g_dsp.r[DSP_REG_ST2])));
|
2010-10-01 23:23:13 +00:00
|
|
|
#else
|
|
|
|
MOV(64, R(R11), ImmPtr(&g_dsp.r));
|
|
|
|
MOVZX(32, 16, EAX, MDisp(R11,DSP_REG_ST2*2));
|
|
|
|
#endif
|
2010-05-29 18:22:50 +00:00
|
|
|
CMP(32, R(EAX), Imm32(0));
|
|
|
|
FixupBranch rLoopAddressExit = J_CC(CC_LE);
|
2010-04-07 15:04:45 +00:00
|
|
|
|
2010-10-01 23:23:13 +00:00
|
|
|
#ifdef _M_IX86 // All32
|
2010-05-29 18:22:50 +00:00
|
|
|
MOVZX(32, 16, EAX, M(&(g_dsp.r[DSP_REG_ST3])));
|
2010-10-01 23:23:13 +00:00
|
|
|
#else
|
|
|
|
MOVZX(32, 16, EAX, MDisp(R11,DSP_REG_ST3*2));
|
|
|
|
#endif
|
2010-05-29 18:22:50 +00:00
|
|
|
CMP(32, R(EAX), Imm32(0));
|
|
|
|
FixupBranch rLoopCounterExit = J_CC(CC_LE);
|
|
|
|
|
|
|
|
// These functions branch and therefore only need to be called in the
|
|
|
|
// end of each block and in this order
|
|
|
|
ABI_CallFunction((void *)&DSPInterpreter::HandleLoop);
|
|
|
|
// ABI_RestoreStack(0);
|
|
|
|
ABI_PopAllCalleeSavedRegsAndAdjustStack();
|
|
|
|
RET();
|
|
|
|
|
|
|
|
SetJumpTarget(rLoopAddressExit);
|
|
|
|
SetJumpTarget(rLoopCounterExit);
|
2010-04-07 15:04:45 +00:00
|
|
|
}
|
|
|
|
|
2010-06-30 16:17:20 +00:00
|
|
|
blockSize[start_addr]++;
|
|
|
|
|
2010-05-29 18:22:50 +00:00
|
|
|
// End the block if we're at a loop end.
|
|
|
|
if (opcode->branch ||
|
|
|
|
(DSPAnalyzer::code_flags[addr] & DSPAnalyzer::CODE_LOOP_END) ||
|
|
|
|
(DSPAnalyzer::code_flags[addr] & DSPAnalyzer::CODE_IDLE_SKIP)) {
|
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
|
|
|
addr += opcode->size;
|
|
|
|
}
|
|
|
|
|
2010-04-23 08:15:00 +00:00
|
|
|
// ABI_RestoreStack(0);
|
|
|
|
ABI_PopAllCalleeSavedRegsAndAdjustStack();
|
2010-04-07 15:04:45 +00:00
|
|
|
RET();
|
|
|
|
|
|
|
|
blocks[start_addr] = (CompiledCode)entryPoint;
|
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
|
|
|
|
|
|
|
return entryPoint;
|
|
|
|
}
|
|
|
|
|
2010-05-29 18:22:50 +00:00
|
|
|
void STACKALIGN DSPEmitter::CompileDispatcher()
|
|
|
|
{
|
2010-05-29 21:34:34 +00:00
|
|
|
/*
|
2010-05-29 18:22:50 +00:00
|
|
|
// TODO
|
2010-05-29 21:34:34 +00:00
|
|
|
enterDispatcher = GetCodePtr();
|
|
|
|
AlignCode16();
|
|
|
|
ABI_PushAllCalleeSavedRegsAndAdjustStack();
|
|
|
|
|
|
|
|
const u8 *outer_loop = GetCodePtr();
|
|
|
|
|
|
|
|
|
|
|
|
//Landing pad for drec space
|
|
|
|
ABI_PopAllCalleeSavedRegsAndAdjustStack();
|
|
|
|
RET();*/
|
2010-05-29 18:22:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Don't use the % operator in the inner loop. It's slow.
|
2010-05-29 21:34:34 +00:00
|
|
|
int STACKALIGN DSPEmitter::RunForCycles(int cycles)
|
2010-04-07 15:04:45 +00:00
|
|
|
{
|
2010-05-29 21:34:34 +00:00
|
|
|
const int idle_cycles = 1000;
|
2010-04-07 15:04:45 +00:00
|
|
|
|
|
|
|
while (!(g_dsp.cr & CR_HALT))
|
|
|
|
{
|
|
|
|
// Compile the block if needed
|
2010-05-29 21:34:34 +00:00
|
|
|
u16 block_addr = g_dsp.pc;
|
|
|
|
if (!blocks[block_addr])
|
2010-04-07 15:04:45 +00:00
|
|
|
{
|
2010-05-29 18:22:50 +00:00
|
|
|
CompileCurrent();
|
2010-04-07 15:04:45 +00:00
|
|
|
}
|
2010-05-29 21:34:34 +00:00
|
|
|
int block_size = blockSize[block_addr];
|
2010-04-07 15:04:45 +00:00
|
|
|
// Execute the block if we have enough cycles
|
2010-05-29 21:34:34 +00:00
|
|
|
if (cycles > block_size)
|
2010-04-07 15:04:45 +00:00
|
|
|
{
|
2010-05-29 21:34:34 +00:00
|
|
|
blocks[block_addr]();
|
|
|
|
if (DSPAnalyzer::code_flags[block_addr] & DSPAnalyzer::CODE_IDLE_SKIP) {
|
|
|
|
if (cycles > idle_cycles)
|
|
|
|
cycles -= idle_cycles;
|
|
|
|
else
|
|
|
|
cycles = 0;
|
2010-05-29 18:22:50 +00:00
|
|
|
} else {
|
2010-05-29 21:34:34 +00:00
|
|
|
cycles -= block_size;
|
2010-05-29 18:22:50 +00:00
|
|
|
}
|
2010-04-07 15:04:45 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2010-05-29 21:34:34 +00:00
|
|
|
return cycles;
|
2010-04-07 15:04:45 +00:00
|
|
|
}
|