2013-04-17 22:43:11 -04:00
|
|
|
// Copyright 2013 Dolphin Emulator Project
|
|
|
|
// Licensed under GPLv2
|
|
|
|
// Refer to the license.txt file included.
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2014-02-17 05:18:15 -05:00
|
|
|
#include "Common/Atomic.h"
|
|
|
|
#include "Common/ChunkFile.h"
|
2014-09-07 20:06:58 -05:00
|
|
|
#include "Common/CommonTypes.h"
|
2014-02-20 04:11:52 +01:00
|
|
|
#include "Common/FPURoundMode.h"
|
2014-02-17 05:18:15 -05:00
|
|
|
#include "Common/MathUtil.h"
|
|
|
|
|
|
|
|
#include "Core/Core.h"
|
|
|
|
#include "Core/CoreTiming.h"
|
|
|
|
#include "Core/Host.h"
|
|
|
|
#include "Core/HW/CPU.h"
|
|
|
|
#include "Core/HW/EXI.h"
|
|
|
|
#include "Core/HW/Memmap.h"
|
|
|
|
#include "Core/HW/SystemTimers.h"
|
|
|
|
|
|
|
|
#include "Core/PowerPC/CPUCoreBase.h"
|
|
|
|
#include "Core/PowerPC/JitInterface.h"
|
|
|
|
#include "Core/PowerPC/PowerPC.h"
|
|
|
|
#include "Core/PowerPC/PPCTables.h"
|
|
|
|
#include "Core/PowerPC/Interpreter/Interpreter.h"
|
|
|
|
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2010-08-26 17:44:13 +00:00
|
|
|
CPUCoreBase *cpu_core_base;
|
|
|
|
|
2008-12-08 05:30:24 +00:00
|
|
|
namespace PowerPC
|
|
|
|
{
|
|
|
|
|
2009-03-23 21:19:43 +00:00
|
|
|
// STATE_TO_SAVE
|
|
|
|
PowerPCState GC_ALIGNED16(ppcState);
|
2014-07-19 19:43:46 -04:00
|
|
|
static volatile CPUState state = CPU_POWERDOWN;
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2010-08-26 17:44:13 +00:00
|
|
|
Interpreter * const interpreter = Interpreter::getInstance();
|
2014-07-08 15:58:25 +02:00
|
|
|
static CoreMode mode;
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2009-06-28 11:47:39 +00:00
|
|
|
BreakPoints breakpoints;
|
|
|
|
MemChecks memchecks;
|
2009-06-28 12:15:31 +00:00
|
|
|
PPCDebugInterface debug_interface;
|
2009-06-28 11:47:39 +00:00
|
|
|
|
2014-05-25 22:57:51 +02:00
|
|
|
u32 CompactCR()
|
2010-07-25 17:54:03 +00:00
|
|
|
{
|
Optimize PPC CR emulation by using magic 64 bit values
PowerPC has a 32 bit CR register, which is used to store flags for results of
computations. Most instructions have an optional bit that tells the CPU whether
the flags should be updated. This 32 bit register actually contains 8 sets of 4
flags: Summary Overflow (SO), Equals (EQ), Greater Than (GT), Less Than (LT).
These 8 sets are usually called CR0-CR7 and accessed independently. In the most
common operations, the flags are computed from the result of the operation in
the following fashion:
* EQ is set iff result == 0
* LT is set iff result < 0
* GT is set iff result > 0
* (Dolphin does not emulate SO)
While X86 architectures have a similar concept of flags, it is very difficult
to access the FLAGS register directly to translate its value to an equivalent
PowerPC value. With the current Dolphin implementation, updating a PPC CR
register requires CPU branching, which has a few performance issues: it uses
space in the BTB, and in the worst case (!GT, !LT, EQ) requires 2 branches not
taken.
After some brainstorming on IRC about how this could be improved, calc84maniac
figured out a neat trick that makes common CR operations way more efficient to
JIT on 64 bit X86 architectures. It relies on emulating each CRn bitfield with
a 64 bit register internally, whose value is the result of the operation from
which flags are updated, sign extended to 64 bits. Then, checking if a CR bit
is set can be done in the following way:
* EQ is set iff LOWER_32_BITS(cr_64b_val) == 0
* GT is set iff (s64)cr_64b_val > 0
* LT is set iff bit 62 of cr_64b_val is set
To take a few examples, if the result of an operation is:
* -1 (0xFFFFFFFFFFFFFFFF) -> lower 32 bits not 0 => !EQ
-> (s64)val (-1) is not > 0 => !GT
-> bit 62 is set => LT
!EQ, !GT, LT
* 0 (0x0000000000000000) -> lower 32 bits are 0 => EQ
-> (s64)val (0) is not > 0 => !GT
-> bit 62 is not set => !LT
EQ, !GT, !LT
* 1 (0x0000000000000001) -> lower 32 bits not 0 => !EQ
-> (s64)val (1) is > 0 => GT
-> bit 62 is not set => !LT
!EQ, GT, !LT
Sometimes we need to convert PPC CR values to these 64 bit values. The
following convention is used in this case:
* Bit 0 (LSB) is set iff !EQ
* Bit 62 is set iff LT
* Bit 63 is set iff !GT
* Bit 32 always set to disambiguize between EQ and GT
Some more examples:
* !EQ, GT, LT -> 0x4000000100000001 (!B63, B62, B32, B0)
-> lower 32 bits not 0 => !EQ
-> (s64)val is > 0 => GT
-> bit 62 is set => LT
* EQ, GT, !LT -> 0x0000000100000000
-> lower 32 bits are 0 => EQ
-> (s64)val is > 0 (note: B32) => GT
-> bit 62 is not set => !LT
2014-05-31 00:43:52 +02:00
|
|
|
u32 new_cr = 0;
|
|
|
|
for (int i = 0; i < 8; i++)
|
2010-07-25 17:54:03 +00:00
|
|
|
{
|
Optimize PPC CR emulation by using magic 64 bit values
PowerPC has a 32 bit CR register, which is used to store flags for results of
computations. Most instructions have an optional bit that tells the CPU whether
the flags should be updated. This 32 bit register actually contains 8 sets of 4
flags: Summary Overflow (SO), Equals (EQ), Greater Than (GT), Less Than (LT).
These 8 sets are usually called CR0-CR7 and accessed independently. In the most
common operations, the flags are computed from the result of the operation in
the following fashion:
* EQ is set iff result == 0
* LT is set iff result < 0
* GT is set iff result > 0
* (Dolphin does not emulate SO)
While X86 architectures have a similar concept of flags, it is very difficult
to access the FLAGS register directly to translate its value to an equivalent
PowerPC value. With the current Dolphin implementation, updating a PPC CR
register requires CPU branching, which has a few performance issues: it uses
space in the BTB, and in the worst case (!GT, !LT, EQ) requires 2 branches not
taken.
After some brainstorming on IRC about how this could be improved, calc84maniac
figured out a neat trick that makes common CR operations way more efficient to
JIT on 64 bit X86 architectures. It relies on emulating each CRn bitfield with
a 64 bit register internally, whose value is the result of the operation from
which flags are updated, sign extended to 64 bits. Then, checking if a CR bit
is set can be done in the following way:
* EQ is set iff LOWER_32_BITS(cr_64b_val) == 0
* GT is set iff (s64)cr_64b_val > 0
* LT is set iff bit 62 of cr_64b_val is set
To take a few examples, if the result of an operation is:
* -1 (0xFFFFFFFFFFFFFFFF) -> lower 32 bits not 0 => !EQ
-> (s64)val (-1) is not > 0 => !GT
-> bit 62 is set => LT
!EQ, !GT, LT
* 0 (0x0000000000000000) -> lower 32 bits are 0 => EQ
-> (s64)val (0) is not > 0 => !GT
-> bit 62 is not set => !LT
EQ, !GT, !LT
* 1 (0x0000000000000001) -> lower 32 bits not 0 => !EQ
-> (s64)val (1) is > 0 => GT
-> bit 62 is not set => !LT
!EQ, GT, !LT
Sometimes we need to convert PPC CR values to these 64 bit values. The
following convention is used in this case:
* Bit 0 (LSB) is set iff !EQ
* Bit 62 is set iff LT
* Bit 63 is set iff !GT
* Bit 32 always set to disambiguize between EQ and GT
Some more examples:
* !EQ, GT, LT -> 0x4000000100000001 (!B63, B62, B32, B0)
-> lower 32 bits not 0 => !EQ
-> (s64)val is > 0 => GT
-> bit 62 is set => LT
* EQ, GT, !LT -> 0x0000000100000000
-> lower 32 bits are 0 => EQ
-> (s64)val is > 0 (note: B32) => GT
-> bit 62 is not set => !LT
2014-05-31 00:43:52 +02:00
|
|
|
new_cr |= GetCRField(i) << (28 - i * 4);
|
2010-07-25 17:54:03 +00:00
|
|
|
}
|
2014-05-25 22:57:51 +02:00
|
|
|
return new_cr;
|
2010-07-25 17:54:03 +00:00
|
|
|
}
|
|
|
|
|
2014-05-25 22:57:51 +02:00
|
|
|
void ExpandCR(u32 cr)
|
2010-07-25 17:54:03 +00:00
|
|
|
{
|
|
|
|
for (int i = 0; i < 8; i++)
|
|
|
|
{
|
Optimize PPC CR emulation by using magic 64 bit values
PowerPC has a 32 bit CR register, which is used to store flags for results of
computations. Most instructions have an optional bit that tells the CPU whether
the flags should be updated. This 32 bit register actually contains 8 sets of 4
flags: Summary Overflow (SO), Equals (EQ), Greater Than (GT), Less Than (LT).
These 8 sets are usually called CR0-CR7 and accessed independently. In the most
common operations, the flags are computed from the result of the operation in
the following fashion:
* EQ is set iff result == 0
* LT is set iff result < 0
* GT is set iff result > 0
* (Dolphin does not emulate SO)
While X86 architectures have a similar concept of flags, it is very difficult
to access the FLAGS register directly to translate its value to an equivalent
PowerPC value. With the current Dolphin implementation, updating a PPC CR
register requires CPU branching, which has a few performance issues: it uses
space in the BTB, and in the worst case (!GT, !LT, EQ) requires 2 branches not
taken.
After some brainstorming on IRC about how this could be improved, calc84maniac
figured out a neat trick that makes common CR operations way more efficient to
JIT on 64 bit X86 architectures. It relies on emulating each CRn bitfield with
a 64 bit register internally, whose value is the result of the operation from
which flags are updated, sign extended to 64 bits. Then, checking if a CR bit
is set can be done in the following way:
* EQ is set iff LOWER_32_BITS(cr_64b_val) == 0
* GT is set iff (s64)cr_64b_val > 0
* LT is set iff bit 62 of cr_64b_val is set
To take a few examples, if the result of an operation is:
* -1 (0xFFFFFFFFFFFFFFFF) -> lower 32 bits not 0 => !EQ
-> (s64)val (-1) is not > 0 => !GT
-> bit 62 is set => LT
!EQ, !GT, LT
* 0 (0x0000000000000000) -> lower 32 bits are 0 => EQ
-> (s64)val (0) is not > 0 => !GT
-> bit 62 is not set => !LT
EQ, !GT, !LT
* 1 (0x0000000000000001) -> lower 32 bits not 0 => !EQ
-> (s64)val (1) is > 0 => GT
-> bit 62 is not set => !LT
!EQ, GT, !LT
Sometimes we need to convert PPC CR values to these 64 bit values. The
following convention is used in this case:
* Bit 0 (LSB) is set iff !EQ
* Bit 62 is set iff LT
* Bit 63 is set iff !GT
* Bit 32 always set to disambiguize between EQ and GT
Some more examples:
* !EQ, GT, LT -> 0x4000000100000001 (!B63, B62, B32, B0)
-> lower 32 bits not 0 => !EQ
-> (s64)val is > 0 => GT
-> bit 62 is set => LT
* EQ, GT, !LT -> 0x0000000100000000
-> lower 32 bits are 0 => EQ
-> (s64)val is > 0 (note: B32) => GT
-> bit 62 is not set => !LT
2014-05-31 00:43:52 +02:00
|
|
|
SetCRField(i, (cr >> (28 - i * 4)) & 0xF);
|
2010-07-25 17:54:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-23 21:19:43 +00:00
|
|
|
void DoState(PointerWrap &p)
|
|
|
|
{
|
2011-12-17 17:26:38 -08:00
|
|
|
// some of this code has been disabled, because
|
|
|
|
// it changes registers even in MODE_MEASURE (which is suspicious and seems like it could cause desyncs)
|
|
|
|
// and because the values it's changing have been added to CoreTiming::DoState, so it might conflict to mess with them here.
|
|
|
|
|
2014-02-16 23:51:41 -05:00
|
|
|
// rSPR(SPR_DEC) = SystemTimers::GetFakeDecrementer();
|
|
|
|
// *((u64 *)&TL) = SystemTimers::GetFakeTimeBase(); //works since we are little endian and TL comes first :)
|
2010-09-01 23:44:03 +00:00
|
|
|
|
2013-04-09 18:57:39 -05:00
|
|
|
p.DoPOD(ppcState);
|
2010-09-01 23:44:03 +00:00
|
|
|
|
2014-02-16 23:51:41 -05:00
|
|
|
// SystemTimers::DecrementerSet();
|
|
|
|
// SystemTimers::TimeBaseSet();
|
2011-10-15 22:19:42 +11:00
|
|
|
|
2013-02-26 13:49:00 -06:00
|
|
|
JitInterface::DoState(p);
|
2009-03-23 21:19:43 +00:00
|
|
|
}
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2014-07-08 14:29:26 +02:00
|
|
|
static void ResetRegisters()
|
2009-03-23 21:19:43 +00:00
|
|
|
{
|
2011-05-25 18:14:29 +00:00
|
|
|
memset(ppcState.ps, 0, sizeof(ppcState.ps));
|
|
|
|
memset(ppcState.gpr, 0, sizeof(ppcState.gpr));
|
2009-03-23 21:19:43 +00:00
|
|
|
memset(ppcState.spr, 0, sizeof(ppcState.spr));
|
2009-12-16 08:59:18 +00:00
|
|
|
/*
|
|
|
|
0x00080200 = lonestar 2.0
|
|
|
|
0x00088202 = lonestar 2.2
|
|
|
|
0x70000100 = gekko 1.0
|
|
|
|
0x00080100 = gekko 2.0
|
|
|
|
0x00083203 = gekko 2.3a
|
|
|
|
0x00083213 = gekko 2.3b
|
|
|
|
0x00083204 = gekko 2.4
|
|
|
|
0x00083214 = gekko 2.4e (8SE) - retail HW2
|
|
|
|
*/
|
|
|
|
ppcState.spr[SPR_PVR] = 0x00083214;
|
|
|
|
ppcState.spr[SPR_HID1] = 0x80000000; // We're running at 3x the bus clock
|
|
|
|
ppcState.spr[SPR_ECID_U] = 0x0d96e200;
|
|
|
|
ppcState.spr[SPR_ECID_M] = 0x1840c00d;
|
|
|
|
ppcState.spr[SPR_ECID_L] = 0x82bb08e8;
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2009-03-23 21:19:43 +00:00
|
|
|
ppcState.fpscr = 0;
|
|
|
|
ppcState.pc = 0;
|
|
|
|
ppcState.npc = 0;
|
|
|
|
ppcState.Exceptions = 0;
|
Optimize PPC CR emulation by using magic 64 bit values
PowerPC has a 32 bit CR register, which is used to store flags for results of
computations. Most instructions have an optional bit that tells the CPU whether
the flags should be updated. This 32 bit register actually contains 8 sets of 4
flags: Summary Overflow (SO), Equals (EQ), Greater Than (GT), Less Than (LT).
These 8 sets are usually called CR0-CR7 and accessed independently. In the most
common operations, the flags are computed from the result of the operation in
the following fashion:
* EQ is set iff result == 0
* LT is set iff result < 0
* GT is set iff result > 0
* (Dolphin does not emulate SO)
While X86 architectures have a similar concept of flags, it is very difficult
to access the FLAGS register directly to translate its value to an equivalent
PowerPC value. With the current Dolphin implementation, updating a PPC CR
register requires CPU branching, which has a few performance issues: it uses
space in the BTB, and in the worst case (!GT, !LT, EQ) requires 2 branches not
taken.
After some brainstorming on IRC about how this could be improved, calc84maniac
figured out a neat trick that makes common CR operations way more efficient to
JIT on 64 bit X86 architectures. It relies on emulating each CRn bitfield with
a 64 bit register internally, whose value is the result of the operation from
which flags are updated, sign extended to 64 bits. Then, checking if a CR bit
is set can be done in the following way:
* EQ is set iff LOWER_32_BITS(cr_64b_val) == 0
* GT is set iff (s64)cr_64b_val > 0
* LT is set iff bit 62 of cr_64b_val is set
To take a few examples, if the result of an operation is:
* -1 (0xFFFFFFFFFFFFFFFF) -> lower 32 bits not 0 => !EQ
-> (s64)val (-1) is not > 0 => !GT
-> bit 62 is set => LT
!EQ, !GT, LT
* 0 (0x0000000000000000) -> lower 32 bits are 0 => EQ
-> (s64)val (0) is not > 0 => !GT
-> bit 62 is not set => !LT
EQ, !GT, !LT
* 1 (0x0000000000000001) -> lower 32 bits not 0 => !EQ
-> (s64)val (1) is > 0 => GT
-> bit 62 is not set => !LT
!EQ, GT, !LT
Sometimes we need to convert PPC CR values to these 64 bit values. The
following convention is used in this case:
* Bit 0 (LSB) is set iff !EQ
* Bit 62 is set iff LT
* Bit 63 is set iff !GT
* Bit 32 always set to disambiguize between EQ and GT
Some more examples:
* !EQ, GT, LT -> 0x4000000100000001 (!B63, B62, B32, B0)
-> lower 32 bits not 0 => !EQ
-> (s64)val is > 0 => GT
-> bit 62 is set => LT
* EQ, GT, !LT -> 0x0000000100000000
-> lower 32 bits are 0 => EQ
-> (s64)val is > 0 (note: B32) => GT
-> bit 62 is not set => !LT
2014-05-31 00:43:52 +02:00
|
|
|
for (auto& v : ppcState.cr_val)
|
|
|
|
v = 0x8000000000000001;
|
2011-03-03 11:37:12 +00:00
|
|
|
|
2009-03-23 21:19:43 +00:00
|
|
|
TL = 0;
|
|
|
|
TU = 0;
|
2010-09-01 23:44:03 +00:00
|
|
|
SystemTimers::TimeBaseSet();
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2009-12-16 08:59:18 +00:00
|
|
|
// MSR should be 0x40, but we don't emulate BS1, so it would never be turned off :}
|
2009-03-23 21:19:43 +00:00
|
|
|
ppcState.msr = 0;
|
|
|
|
rDEC = 0xFFFFFFFF;
|
2010-09-01 23:44:03 +00:00
|
|
|
SystemTimers::DecrementerSet();
|
2009-03-23 21:19:43 +00:00
|
|
|
}
|
2009-03-23 08:49:17 +00:00
|
|
|
|
2010-01-19 19:28:27 +00:00
|
|
|
void Init(int cpu_core)
|
2009-03-23 21:19:43 +00:00
|
|
|
{
|
2013-02-26 13:49:00 -06:00
|
|
|
FPURoundMode::SetPrecisionMode(FPURoundMode::PREC_53);
|
|
|
|
|
|
|
|
memset(ppcState.sr, 0, sizeof(ppcState.sr));
|
|
|
|
ppcState.dtlb_last = 0;
|
|
|
|
memset(ppcState.dtlb_va, 0, sizeof(ppcState.dtlb_va));
|
|
|
|
memset(ppcState.dtlb_pa, 0, sizeof(ppcState.dtlb_pa));
|
|
|
|
ppcState.itlb_last = 0;
|
2012-01-02 02:20:22 -08:00
|
|
|
memset(ppcState.itlb_va, 0, sizeof(ppcState.itlb_va));
|
|
|
|
memset(ppcState.itlb_pa, 0, sizeof(ppcState.itlb_pa));
|
2013-05-19 17:14:29 +02:00
|
|
|
ppcState.pagetable_base = 0;
|
|
|
|
ppcState.pagetable_hashmask = 0;
|
2012-01-02 02:20:22 -08:00
|
|
|
|
2009-03-23 21:19:43 +00:00
|
|
|
ResetRegisters();
|
2010-08-26 17:44:13 +00:00
|
|
|
PPCTables::InitTables(cpu_core);
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2010-08-26 17:44:13 +00:00
|
|
|
// We initialize the interpreter because
|
|
|
|
// it is used on boot and code window independently.
|
|
|
|
interpreter->Init();
|
2010-01-19 19:28:27 +00:00
|
|
|
|
2010-08-26 17:44:13 +00:00
|
|
|
switch (cpu_core)
|
|
|
|
{
|
2014-07-07 05:30:06 +02:00
|
|
|
case 0:
|
2010-08-26 17:44:13 +00:00
|
|
|
{
|
|
|
|
cpu_core_base = interpreter;
|
|
|
|
break;
|
|
|
|
}
|
2013-02-26 13:49:00 -06:00
|
|
|
default:
|
|
|
|
cpu_core_base = JitInterface::InitJitCore(cpu_core);
|
2013-03-08 10:52:04 -06:00
|
|
|
if (!cpu_core_base) // Handle Situations where JIT core isn't available
|
|
|
|
{
|
|
|
|
WARN_LOG(POWERPC, "Jit core %d not available. Defaulting to interpreter.", cpu_core);
|
|
|
|
cpu_core_base = interpreter;
|
|
|
|
}
|
2013-02-26 13:49:00 -06:00
|
|
|
break;
|
2010-08-26 17:44:13 +00:00
|
|
|
}
|
2010-08-26 11:06:47 +00:00
|
|
|
|
2010-08-26 17:44:13 +00:00
|
|
|
if (cpu_core_base != interpreter)
|
|
|
|
{
|
|
|
|
mode = MODE_JIT;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mode = MODE_INTERPRETER;
|
|
|
|
}
|
2014-07-07 05:30:06 +02:00
|
|
|
state = CPU_STEPPING;
|
2009-10-03 12:29:27 +00:00
|
|
|
|
2012-01-02 02:20:22 -08:00
|
|
|
ppcState.iCache.Init();
|
2009-03-23 21:19:43 +00:00
|
|
|
}
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2009-03-23 21:19:43 +00:00
|
|
|
void Shutdown()
|
|
|
|
{
|
2013-02-26 13:49:00 -06:00
|
|
|
JitInterface::Shutdown();
|
2010-08-26 17:44:13 +00:00
|
|
|
interpreter->Shutdown();
|
2014-03-09 21:14:26 +01:00
|
|
|
cpu_core_base = nullptr;
|
2014-07-07 05:30:06 +02:00
|
|
|
state = CPU_POWERDOWN;
|
2009-03-23 21:19:43 +00:00
|
|
|
}
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2011-12-31 15:18:48 +11:00
|
|
|
CoreMode GetMode()
|
|
|
|
{
|
|
|
|
return mode;
|
|
|
|
}
|
|
|
|
|
2009-03-23 21:19:43 +00:00
|
|
|
void SetMode(CoreMode new_mode)
|
|
|
|
{
|
|
|
|
if (new_mode == mode)
|
|
|
|
return; // We don't need to do anything.
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2009-03-23 21:19:43 +00:00
|
|
|
mode = new_mode;
|
2010-08-26 17:44:13 +00:00
|
|
|
|
2009-03-23 21:19:43 +00:00
|
|
|
switch (mode)
|
|
|
|
{
|
|
|
|
case MODE_INTERPRETER: // Switching from JIT to interpreter
|
2010-08-26 17:44:13 +00:00
|
|
|
cpu_core_base = interpreter;
|
2009-03-23 21:19:43 +00:00
|
|
|
break;
|
2009-03-23 08:49:17 +00:00
|
|
|
|
2009-03-23 21:19:43 +00:00
|
|
|
case MODE_JIT: // Switching from interpreter to JIT.
|
|
|
|
// Don't really need to do much. It'll work, the cache will refill itself.
|
2013-02-26 13:49:00 -06:00
|
|
|
cpu_core_base = JitInterface::GetCore();
|
2013-03-08 10:52:04 -06:00
|
|
|
if (!cpu_core_base) // Has a chance to not get a working JIT core if one isn't active on host
|
|
|
|
cpu_core_base = interpreter;
|
2009-03-23 21:19:43 +00:00
|
|
|
break;
|
2008-12-08 05:30:24 +00:00
|
|
|
}
|
2009-03-23 21:19:43 +00:00
|
|
|
}
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2013-10-29 01:23:17 -04:00
|
|
|
void SingleStep()
|
2009-03-23 21:19:43 +00:00
|
|
|
{
|
2010-08-26 17:44:13 +00:00
|
|
|
cpu_core_base->SingleStep();
|
2009-03-23 21:19:43 +00:00
|
|
|
}
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2009-03-23 21:19:43 +00:00
|
|
|
void RunLoop()
|
|
|
|
{
|
2014-07-07 05:30:06 +02:00
|
|
|
state = CPU_RUNNING;
|
2010-08-26 17:44:13 +00:00
|
|
|
cpu_core_base->Run();
|
2009-03-23 21:19:43 +00:00
|
|
|
Host_UpdateDisasmDialog();
|
|
|
|
}
|
2009-02-17 22:48:16 +00:00
|
|
|
|
2009-03-23 21:19:43 +00:00
|
|
|
CPUState GetState()
|
|
|
|
{
|
|
|
|
return state;
|
|
|
|
}
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2009-03-23 21:19:43 +00:00
|
|
|
volatile CPUState *GetStatePtr()
|
|
|
|
{
|
|
|
|
return &state;
|
|
|
|
}
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2009-03-23 21:19:43 +00:00
|
|
|
void Start()
|
|
|
|
{
|
2014-07-07 05:30:06 +02:00
|
|
|
state = CPU_RUNNING;
|
2009-03-23 21:19:43 +00:00
|
|
|
Host_UpdateDisasmDialog();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Pause()
|
|
|
|
{
|
2014-07-07 05:30:06 +02:00
|
|
|
state = CPU_STEPPING;
|
2009-03-23 21:19:43 +00:00
|
|
|
Host_UpdateDisasmDialog();
|
|
|
|
}
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2009-03-23 21:19:43 +00:00
|
|
|
void Stop()
|
|
|
|
{
|
2014-07-07 05:30:06 +02:00
|
|
|
state = CPU_POWERDOWN;
|
2009-03-23 21:19:43 +00:00
|
|
|
Host_UpdateDisasmDialog();
|
|
|
|
}
|
|
|
|
|
2012-04-28 20:42:45 +10:00
|
|
|
void UpdatePerformanceMonitor(u32 cycles, u32 num_load_stores, u32 num_fp_inst)
|
|
|
|
{
|
|
|
|
switch (MMCR0.PMC1SELECT)
|
|
|
|
{
|
|
|
|
case 0: // No change
|
|
|
|
break;
|
|
|
|
case 1: // Processor cycles
|
|
|
|
PowerPC::ppcState.spr[SPR_PMC1] += cycles;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (MMCR0.PMC2SELECT)
|
|
|
|
{
|
|
|
|
case 0: // No change
|
|
|
|
break;
|
|
|
|
case 1: // Processor cycles
|
|
|
|
PowerPC::ppcState.spr[SPR_PMC2] += cycles;
|
|
|
|
break;
|
|
|
|
case 11: // Number of loads and stores completed
|
|
|
|
PowerPC::ppcState.spr[SPR_PMC2] += num_load_stores;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (MMCR1.PMC3SELECT)
|
|
|
|
{
|
|
|
|
case 0: // No change
|
|
|
|
break;
|
|
|
|
case 1: // Processor cycles
|
|
|
|
PowerPC::ppcState.spr[SPR_PMC3] += cycles;
|
|
|
|
break;
|
|
|
|
case 11: // Number of FPU instructions completed
|
|
|
|
PowerPC::ppcState.spr[SPR_PMC3] += num_fp_inst;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (MMCR1.PMC4SELECT)
|
|
|
|
{
|
|
|
|
case 0: // No change
|
|
|
|
break;
|
|
|
|
case 1: // Processor cycles
|
|
|
|
PowerPC::ppcState.spr[SPR_PMC4] += cycles;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-05-01 20:52:35 +10:00
|
|
|
if ((MMCR0.PMC1INTCONTROL && (PowerPC::ppcState.spr[SPR_PMC1] & 0x80000000) != 0) ||
|
|
|
|
(MMCR0.PMCINTCONTROL && (PowerPC::ppcState.spr[SPR_PMC2] & 0x80000000) != 0) ||
|
|
|
|
(MMCR0.PMCINTCONTROL && (PowerPC::ppcState.spr[SPR_PMC3] & 0x80000000) != 0) ||
|
|
|
|
(MMCR0.PMCINTCONTROL && (PowerPC::ppcState.spr[SPR_PMC4] & 0x80000000) != 0))
|
2012-04-28 20:42:45 +10:00
|
|
|
PowerPC::ppcState.Exceptions |= EXCEPTION_PERFORMANCE_MONITOR;
|
|
|
|
}
|
|
|
|
|
2009-03-23 21:19:43 +00:00
|
|
|
void CheckExceptions()
|
|
|
|
{
|
2011-10-03 23:30:29 -07:00
|
|
|
// Make sure we are checking against the latest EXI status. This is required
|
|
|
|
// for devices which interrupt frequently, such as the gc mic
|
|
|
|
ExpansionInterface::UpdateInterrupts();
|
|
|
|
|
2010-09-01 22:03:51 +00:00
|
|
|
// Read volatile data once
|
|
|
|
u32 exceptions = ppcState.Exceptions;
|
|
|
|
|
2009-04-06 14:15:02 +00:00
|
|
|
// Example procedure:
|
|
|
|
// set SRR0 to either PC or NPC
|
|
|
|
//SRR0 = NPC;
|
|
|
|
// save specified MSR bits
|
|
|
|
//SRR1 = MSR & 0x87C0FFFF;
|
|
|
|
// copy ILE bit to LE
|
2010-09-11 02:34:51 +00:00
|
|
|
//MSR |= (MSR >> 16) & 1;
|
2009-04-06 14:15:02 +00:00
|
|
|
// clear MSR as specified
|
|
|
|
//MSR &= ~0x04EF36; // 0x04FF36 also clears ME (only for machine check exception)
|
|
|
|
// set to exception type entry point
|
2012-05-01 20:26:05 +10:00
|
|
|
//NPC = 0x00000x00;
|
2009-04-06 14:15:02 +00:00
|
|
|
|
2014-05-18 00:56:18 +02:00
|
|
|
// TODO(delroth): Exception priority is completely wrong here: depending on
|
|
|
|
// the instruction class, exceptions should be executed in a given order,
|
|
|
|
// which is very different from the one arbitrarily chosen here. See §6.1.5
|
|
|
|
// in 6xx_pem.pdf.
|
|
|
|
|
2010-09-01 22:03:51 +00:00
|
|
|
if (exceptions & EXCEPTION_ISI)
|
2009-04-06 14:15:02 +00:00
|
|
|
{
|
|
|
|
SRR0 = NPC;
|
2010-09-11 02:34:51 +00:00
|
|
|
// Page fault occurred
|
|
|
|
SRR1 = (MSR & 0x87C0FFFF) | (1 << 30);
|
|
|
|
MSR |= (MSR >> 16) & 1;
|
2009-04-06 14:15:02 +00:00
|
|
|
MSR &= ~0x04EF36;
|
2012-05-01 20:26:05 +10:00
|
|
|
PC = NPC = 0x00000400;
|
2009-04-06 14:15:02 +00:00
|
|
|
|
2009-05-01 15:17:03 +00:00
|
|
|
INFO_LOG(POWERPC, "EXCEPTION_ISI");
|
2010-09-01 22:03:51 +00:00
|
|
|
Common::AtomicAnd(ppcState.Exceptions, ~EXCEPTION_ISI);
|
2009-04-06 14:15:02 +00:00
|
|
|
}
|
2010-09-01 22:03:51 +00:00
|
|
|
else if (exceptions & EXCEPTION_PROGRAM)
|
2009-04-06 14:15:02 +00:00
|
|
|
{
|
|
|
|
SRR0 = PC;
|
|
|
|
// say that it's a trap exception
|
2011-10-23 02:53:31 -07:00
|
|
|
SRR1 = (MSR & 0x87C0FFFF) | 0x20000;
|
2010-09-11 02:34:51 +00:00
|
|
|
MSR |= (MSR >> 16) & 1;
|
2009-04-06 14:15:02 +00:00
|
|
|
MSR &= ~0x04EF36;
|
2012-05-01 20:26:05 +10:00
|
|
|
PC = NPC = 0x00000700;
|
2009-04-06 14:15:02 +00:00
|
|
|
|
2009-05-01 15:17:03 +00:00
|
|
|
INFO_LOG(POWERPC, "EXCEPTION_PROGRAM");
|
2010-09-01 22:03:51 +00:00
|
|
|
Common::AtomicAnd(ppcState.Exceptions, ~EXCEPTION_PROGRAM);
|
2013-10-29 01:23:17 -04:00
|
|
|
}
|
2010-09-01 22:03:51 +00:00
|
|
|
else if (exceptions & EXCEPTION_SYSCALL)
|
2009-04-06 14:15:02 +00:00
|
|
|
{
|
|
|
|
SRR0 = NPC;
|
|
|
|
SRR1 = MSR & 0x87C0FFFF;
|
2010-09-11 02:34:51 +00:00
|
|
|
MSR |= (MSR >> 16) & 1;
|
2009-04-06 14:15:02 +00:00
|
|
|
MSR &= ~0x04EF36;
|
2012-05-01 20:26:05 +10:00
|
|
|
PC = NPC = 0x00000C00;
|
2009-03-23 21:19:43 +00:00
|
|
|
|
2009-05-01 15:17:03 +00:00
|
|
|
INFO_LOG(POWERPC, "EXCEPTION_SYSCALL (PC=%08x)", PC);
|
2010-09-01 22:03:51 +00:00
|
|
|
Common::AtomicAnd(ppcState.Exceptions, ~EXCEPTION_SYSCALL);
|
2009-04-06 14:15:02 +00:00
|
|
|
}
|
2010-09-01 22:03:51 +00:00
|
|
|
else if (exceptions & EXCEPTION_FPU_UNAVAILABLE)
|
2013-10-29 01:23:17 -04:00
|
|
|
{
|
2014-06-07 11:30:39 +09:00
|
|
|
//This happens a lot - GameCube OS uses deferred FPU context switching
|
2014-02-16 23:51:41 -05:00
|
|
|
SRR0 = PC; // re-execute the instruction
|
2009-04-06 14:15:02 +00:00
|
|
|
SRR1 = MSR & 0x87C0FFFF;
|
2010-09-11 02:34:51 +00:00
|
|
|
MSR |= (MSR >> 16) & 1;
|
2009-04-06 14:15:02 +00:00
|
|
|
MSR &= ~0x04EF36;
|
2012-05-01 20:26:05 +10:00
|
|
|
PC = NPC = 0x00000800;
|
2009-03-23 21:19:43 +00:00
|
|
|
|
2009-05-01 15:17:03 +00:00
|
|
|
INFO_LOG(POWERPC, "EXCEPTION_FPU_UNAVAILABLE");
|
2010-09-01 22:03:51 +00:00
|
|
|
Common::AtomicAnd(ppcState.Exceptions, ~EXCEPTION_FPU_UNAVAILABLE);
|
2009-03-23 21:19:43 +00:00
|
|
|
}
|
2010-09-01 22:03:51 +00:00
|
|
|
else if (exceptions & EXCEPTION_DSI)
|
2009-03-23 08:49:17 +00:00
|
|
|
{
|
2009-04-06 14:15:02 +00:00
|
|
|
SRR0 = PC;
|
|
|
|
SRR1 = MSR & 0x87C0FFFF;
|
2010-09-11 02:34:51 +00:00
|
|
|
MSR |= (MSR >> 16) & 1;
|
2009-04-06 14:15:02 +00:00
|
|
|
MSR &= ~0x04EF36;
|
2012-05-01 20:26:05 +10:00
|
|
|
PC = NPC = 0x00000300;
|
2009-04-06 14:15:02 +00:00
|
|
|
//DSISR and DAR regs are changed in GenerateDSIException()
|
2009-03-23 21:19:43 +00:00
|
|
|
|
2009-05-01 15:17:03 +00:00
|
|
|
INFO_LOG(POWERPC, "EXCEPTION_DSI");
|
2010-09-01 22:03:51 +00:00
|
|
|
Common::AtomicAnd(ppcState.Exceptions, ~EXCEPTION_DSI);
|
2013-10-29 01:23:17 -04:00
|
|
|
}
|
2010-09-01 22:03:51 +00:00
|
|
|
else if (exceptions & EXCEPTION_ALIGNMENT)
|
2009-03-23 08:49:17 +00:00
|
|
|
{
|
2009-03-23 21:19:43 +00:00
|
|
|
//This never happens ATM
|
2009-04-06 14:15:02 +00:00
|
|
|
// perhaps we can get dcb* instructions to use this :p
|
|
|
|
SRR0 = PC;
|
|
|
|
SRR1 = MSR & 0x87C0FFFF;
|
2010-09-11 02:34:51 +00:00
|
|
|
MSR |= (MSR >> 16) & 1;
|
2009-04-06 14:15:02 +00:00
|
|
|
MSR &= ~0x04EF36;
|
2012-05-01 20:26:05 +10:00
|
|
|
PC = NPC = 0x00000600;
|
2009-03-23 21:19:43 +00:00
|
|
|
|
2009-04-06 14:15:02 +00:00
|
|
|
//TODO crazy amount of DSISR options to check out
|
|
|
|
|
2009-05-01 15:17:03 +00:00
|
|
|
INFO_LOG(POWERPC, "EXCEPTION_ALIGNMENT");
|
2010-09-01 22:03:51 +00:00
|
|
|
Common::AtomicAnd(ppcState.Exceptions, ~EXCEPTION_ALIGNMENT);
|
2009-03-23 08:49:17 +00:00
|
|
|
}
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2009-06-13 14:10:10 +00:00
|
|
|
// EXTERNAL INTERRUPT
|
2014-05-18 00:56:18 +02:00
|
|
|
else if (MSR & 0x0008000) // Handling is delayed until MSR.EE=1.
|
2009-03-23 07:58:59 +00:00
|
|
|
{
|
2010-09-01 22:03:51 +00:00
|
|
|
if (exceptions & EXCEPTION_EXTERNAL_INT)
|
2009-03-23 07:58:59 +00:00
|
|
|
{
|
2009-03-23 21:19:43 +00:00
|
|
|
// Pokemon gets this "too early", it hasn't a handler yet
|
|
|
|
SRR0 = NPC;
|
2009-04-06 14:15:02 +00:00
|
|
|
SRR1 = MSR & 0x87C0FFFF;
|
2010-09-11 02:34:51 +00:00
|
|
|
MSR |= (MSR >> 16) & 1;
|
2009-04-06 14:15:02 +00:00
|
|
|
MSR &= ~0x04EF36;
|
2012-05-01 20:26:05 +10:00
|
|
|
PC = NPC = 0x00000500;
|
2012-04-15 21:34:15 +10:00
|
|
|
|
|
|
|
INFO_LOG(POWERPC, "EXCEPTION_EXTERNAL_INT");
|
|
|
|
Common::AtomicAnd(ppcState.Exceptions, ~EXCEPTION_EXTERNAL_INT);
|
|
|
|
|
|
|
|
_dbg_assert_msg_(POWERPC, (SRR1 & 0x02) != 0, "EXTERNAL_INT unrecoverable???");
|
|
|
|
}
|
2012-05-01 21:00:57 +10:00
|
|
|
else if (exceptions & EXCEPTION_PERFORMANCE_MONITOR)
|
|
|
|
{
|
|
|
|
SRR0 = NPC;
|
|
|
|
SRR1 = MSR & 0x87C0FFFF;
|
|
|
|
MSR |= (MSR >> 16) & 1;
|
|
|
|
MSR &= ~0x04EF36;
|
|
|
|
PC = NPC = 0x00000F00;
|
|
|
|
|
|
|
|
INFO_LOG(POWERPC, "EXCEPTION_PERFORMANCE_MONITOR");
|
|
|
|
Common::AtomicAnd(ppcState.Exceptions, ~EXCEPTION_PERFORMANCE_MONITOR);
|
|
|
|
}
|
2012-04-15 21:34:15 +10:00
|
|
|
else if (exceptions & EXCEPTION_DECREMENTER)
|
|
|
|
{
|
|
|
|
SRR0 = NPC;
|
|
|
|
SRR1 = MSR & 0x87C0FFFF;
|
|
|
|
MSR |= (MSR >> 16) & 1;
|
|
|
|
MSR &= ~0x04EF36;
|
2012-05-01 20:26:05 +10:00
|
|
|
PC = NPC = 0x00000900;
|
2012-04-15 21:34:15 +10:00
|
|
|
|
|
|
|
INFO_LOG(POWERPC, "EXCEPTION_DECREMENTER");
|
|
|
|
Common::AtomicAnd(ppcState.Exceptions, ~EXCEPTION_DECREMENTER);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CheckExternalExceptions()
|
|
|
|
{
|
|
|
|
// Read volatile data once
|
|
|
|
u32 exceptions = ppcState.Exceptions;
|
|
|
|
|
|
|
|
// EXTERNAL INTERRUPT
|
2014-10-24 13:23:21 +11:00
|
|
|
if (exceptions && (MSR & 0x0008000)) // Handling is delayed until MSR.EE=1.
|
2012-04-15 21:34:15 +10:00
|
|
|
{
|
|
|
|
if (exceptions & EXCEPTION_EXTERNAL_INT)
|
|
|
|
{
|
|
|
|
// Pokemon gets this "too early", it hasn't a handler yet
|
|
|
|
SRR0 = NPC;
|
|
|
|
SRR1 = MSR & 0x87C0FFFF;
|
|
|
|
MSR |= (MSR >> 16) & 1;
|
|
|
|
MSR &= ~0x04EF36;
|
2012-05-01 20:26:05 +10:00
|
|
|
PC = NPC = 0x00000500;
|
2009-03-23 21:19:43 +00:00
|
|
|
|
2009-05-01 15:17:03 +00:00
|
|
|
INFO_LOG(POWERPC, "EXCEPTION_EXTERNAL_INT");
|
2010-09-01 22:03:51 +00:00
|
|
|
Common::AtomicAnd(ppcState.Exceptions, ~EXCEPTION_EXTERNAL_INT);
|
2009-03-23 07:58:59 +00:00
|
|
|
|
2010-12-05 15:59:11 +00:00
|
|
|
_dbg_assert_msg_(POWERPC, (SRR1 & 0x02) != 0, "EXTERNAL_INT unrecoverable???");
|
2009-03-23 07:58:59 +00:00
|
|
|
}
|
2012-04-28 20:42:45 +10:00
|
|
|
else if (exceptions & EXCEPTION_PERFORMANCE_MONITOR)
|
|
|
|
{
|
|
|
|
SRR0 = NPC;
|
|
|
|
SRR1 = MSR & 0x87C0FFFF;
|
|
|
|
MSR |= (MSR >> 16) & 1;
|
|
|
|
MSR &= ~0x04EF36;
|
2012-05-01 21:00:57 +10:00
|
|
|
PC = NPC = 0x00000F00;
|
2012-04-28 20:42:45 +10:00
|
|
|
|
|
|
|
INFO_LOG(POWERPC, "EXCEPTION_PERFORMANCE_MONITOR");
|
|
|
|
Common::AtomicAnd(ppcState.Exceptions, ~EXCEPTION_PERFORMANCE_MONITOR);
|
|
|
|
}
|
2010-09-01 22:03:51 +00:00
|
|
|
else if (exceptions & EXCEPTION_DECREMENTER)
|
2009-03-23 07:58:59 +00:00
|
|
|
{
|
|
|
|
SRR0 = NPC;
|
2009-04-06 14:15:02 +00:00
|
|
|
SRR1 = MSR & 0x87C0FFFF;
|
2010-09-11 02:34:51 +00:00
|
|
|
MSR |= (MSR >> 16) & 1;
|
2009-04-06 14:15:02 +00:00
|
|
|
MSR &= ~0x04EF36;
|
2012-05-01 20:26:05 +10:00
|
|
|
PC = NPC = 0x00000900;
|
2009-03-23 07:58:59 +00:00
|
|
|
|
2009-05-01 15:17:03 +00:00
|
|
|
INFO_LOG(POWERPC, "EXCEPTION_DECREMENTER");
|
2010-09-01 22:03:51 +00:00
|
|
|
Common::AtomicAnd(ppcState.Exceptions, ~EXCEPTION_DECREMENTER);
|
2009-03-23 21:19:43 +00:00
|
|
|
}
|
|
|
|
else
|
2009-03-23 07:58:59 +00:00
|
|
|
{
|
2010-09-01 22:03:51 +00:00
|
|
|
_dbg_assert_msg_(POWERPC, 0, "Unknown EXT interrupt: Exceptions == %08x", exceptions);
|
|
|
|
ERROR_LOG(POWERPC, "Unknown EXTERNAL INTERRUPT exception: Exceptions == %08x", exceptions);
|
2009-03-23 07:58:59 +00:00
|
|
|
}
|
|
|
|
}
|
2009-03-23 21:19:43 +00:00
|
|
|
}
|
2009-03-23 07:58:59 +00:00
|
|
|
|
2010-07-05 02:05:47 +00:00
|
|
|
void CheckBreakPoints()
|
|
|
|
{
|
|
|
|
if (PowerPC::breakpoints.IsAddressBreakPoint(PC))
|
|
|
|
{
|
|
|
|
PowerPC::Pause();
|
|
|
|
if (PowerPC::breakpoints.IsTempBreakPoint(PC))
|
|
|
|
PowerPC::breakpoints.Remove(PC);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-23 21:19:43 +00:00
|
|
|
void OnIdle(u32 _uThreadAddr)
|
|
|
|
{
|
|
|
|
u32 nextThread = Memory::Read_U32(_uThreadAddr);
|
|
|
|
//do idle skipping
|
|
|
|
if (nextThread == 0)
|
|
|
|
CoreTiming::Idle();
|
|
|
|
}
|
2008-12-08 05:30:24 +00:00
|
|
|
|
2009-05-31 09:22:29 +00:00
|
|
|
void OnIdleIL()
|
|
|
|
{
|
|
|
|
CoreTiming::Idle();
|
|
|
|
}
|
|
|
|
|
2009-06-13 14:10:10 +00:00
|
|
|
} // namespace
|
|
|
|
|
2009-06-13 22:08:01 +00:00
|
|
|
|
|
|
|
// FPSCR update functions
|
|
|
|
|
2009-06-13 14:10:10 +00:00
|
|
|
void UpdateFPRF(double dvalue)
|
|
|
|
{
|
2009-06-15 21:10:11 +00:00
|
|
|
FPSCR.FPRF = MathUtil::ClassifyDouble(dvalue);
|
2009-06-14 11:30:33 +00:00
|
|
|
//if (FPSCR.FPRF == 0x11)
|
|
|
|
// PanicAlert("QNAN alert");
|
2009-06-13 20:55:25 +00:00
|
|
|
}
|