diff --git a/build/wiiu.vcxproj b/build/wiiu.vcxproj
index 36659fdf..9b73f8ec 100644
--- a/build/wiiu.vcxproj
+++ b/build/wiiu.vcxproj
@@ -160,8 +160,8 @@
+
-
@@ -207,6 +207,7 @@
+
@@ -247,7 +248,6 @@
-
diff --git a/build/wiiu.vcxproj.filters b/build/wiiu.vcxproj.filters
index bc1b7626..909fc363 100644
--- a/build/wiiu.vcxproj.filters
+++ b/build/wiiu.vcxproj.filters
@@ -213,9 +213,6 @@
Source Files\system
-
- Source Files\system
-
Source Files\ppc
@@ -318,6 +315,9 @@
Source Files\modules\nn_ac
+
+ Source Files\system
+
@@ -452,9 +452,6 @@
Header Files\system
-
- Header Files\system
-
Header Files\util
@@ -575,5 +572,8 @@
Header Files\modules\nn_ac
+
+ Header Files\system
+
\ No newline at end of file
diff --git a/src/codetests.cpp b/src/codetests.cpp
index 6af69682..9948fa39 100644
--- a/src/codetests.cpp
+++ b/src/codetests.cpp
@@ -573,14 +573,14 @@ executeCodeTests(const std::string &assembler, const std::string &directory)
// Run test with all state set to 0x00
gLog->debug(" Running with 0x00");
memset(&state, 0x00, sizeof(ThreadState));
- state.thread = nullptr;
+ state.fiber = nullptr;
state.tracer = nullptr;
result &= executeCodeTest(state, baseAddress, test.second);
// Run test with all state set to 0xFF
gLog->debug(" Running with 0xFF");
memset(&state, 0xFF, sizeof(ThreadState));
- state.thread = nullptr;
+ state.fiber = nullptr;
state.tracer = nullptr;
result &= executeCodeTest(state, baseAddress, test.second);
diff --git a/src/interpreter/interpreter_system.cpp b/src/interpreter/interpreter_system.cpp
index e44ae08c..c652103f 100644
--- a/src/interpreter/interpreter_system.cpp
+++ b/src/interpreter/interpreter_system.cpp
@@ -5,7 +5,6 @@
#include "log.h"
#include "system.h"
#include "kernelfunction.h"
-#include "thread.h"
#include "usermodule.h"
static SprEncoding
diff --git a/src/jit.cpp b/src/jit.cpp
index 5598cab1..d588843f 100644
--- a/src/jit.cpp
+++ b/src/jit.cpp
@@ -1,487 +1,487 @@
#include "jit.h"
-#include "log.h"
-#include "interpreter.h"
-#include "instructiondata.h"
-
-JitManager
-gJitManager;
-
-static std::vector
-sJitInstructionMap;
-
-void JitManager::RegisterFunctions()
-{
- static bool didInit = false;
-
- if (!didInit) {
- // Reserve instruction map
- sJitInstructionMap.resize(static_cast(InstructionID::InstructionCount), nullptr);
-
- // Register JIT instruction handlers
- registerBranchInstructions();
- registerConditionInstructions();
- registerFloatInstructions();
- registerIntegerInstructions();
- registerLoadStoreInstructions();
- registerPairedInstructions();
- registerSystemInstructions();
-
- didInit = true;
- }
-}
-
-void JitManager::registerInstruction(InstructionID id, jitinstrfptr_t fptr)
-{
- sJitInstructionMap[static_cast(id)] = fptr;
-}
-
-bool JitManager::initialise() {
- initStubs();
- return true;
-}
-
-void JitManager::initStubs() {
- PPCEmuAssembler a(mRuntime);
-
- asmjit::Label introLabel(a);
- asmjit::Label extroLabel(a);
-
- a.bind(introLabel);
- a.push(a.zbx);
- a.push(a.zsi);
- a.sub(a.zsp, 0x28);
- a.mov(a.zbx, a.zcx);
- a.mov(a.zsi, gMemory.base());
- a.jmp(a.zdx);
-
- a.bind(extroLabel);
- a.add(a.zsp, 0x28);
- a.pop(a.zsi);
- a.pop(a.zbx);
- a.ret();
-
- auto basePtr = a.make();
- mCallFn = asmjit_cast(basePtr, a.getLabelOffset(introLabel));
- mFinaleFn = asmjit_cast(basePtr, a.getLabelOffset(extroLabel));
-}
-
-enum BoBits
-{
- CtrValue = 1,
- NoCheckCtr = 2,
- CondValue = 3,
- NoCheckCond = 4
-};
-enum BcFlags
-{
- BcCheckCtr = 1 << 0,
- BcCheckCond = 1 << 1,
- BcBranchLR = 1 << 2,
- BcBranchCTR = 1 << 3
-};
-
-bool JitManager::jit_b(PPCEmuAssembler& a, Instruction instr, uint32_t cia, const JumpLabelMap& jumpLabels)
-{
- uint32_t nia = sign_extend<26>(instr.li << 2);
- if (!instr.aa) {
- nia += cia;
- }
-
- if (instr.lk) {
- a.mov(a.eax, cia + 4u);
- a.mov(a.ppclr, a.eax);
-
- a.mov(a.eax, nia);
- a.jmp(asmjit::Ptr(mFinaleFn));
- return true;
- }
-
- auto i = jumpLabels.find(nia);
- if (i != jumpLabels.end()) {
- a.jmp(i->second);
- } else {
- a.mov(a.eax, nia);
- a.jmp(asmjit::Ptr(mFinaleFn));
- }
-
- return true;
-}
-
-template
-static bool
-bcGeneric(PPCEmuAssembler& a, Instruction instr, uint32_t cia, const JumpLabelMap& jumpLabels, JitFinale finaleFn)
-{
- uint32_t bo = instr.bo;
- asmjit::Label doCondFailLbl(a);
-
- if (flags & BcCheckCtr) {
- if (!get_bit(bo)) {
- //state->ctr--;
- //ctr_ok = !!((state->ctr != 0) ^ (get_bit(bo)));
-
- a.dec(a.ppcctr);
-
- a.mov(a.eax, a.ppcctr);
- a.cmp(a.eax, 0);
- if (get_bit(bo)) {
- a.jne(doCondFailLbl);
- } else {
- a.je(doCondFailLbl);
- }
- }
- }
-
- if (flags & BcCheckCond) {
- if (!get_bit(bo)) {
- //auto crb = get_bit(state->cr.value, 31 - instr.bi);
- //auto crv = get_bit(bo);
- //cond_ok = (crb == crv);
-
- a.mov(a.eax, a.ppccr);
- a.and_(a.eax, 1 << (31 - instr.bi));
- a.cmp(a.eax, 0);
-
- if (get_bit(bo)) {
- a.je(doCondFailLbl);
- } else {
- a.jne(doCondFailLbl);
- }
- }
- }
-
- if (instr.lk) {
- a.mov(a.eax, cia + 4);
- a.mov(a.ppclr, a.eax);
- }
-
- // Make sure no JMP related instructions end up above
- // this if-block as we use a JMP instruction with
- // early exit in the else block...
- if (flags & BcBranchCTR) {
- a.mov(a.eax, a.ppcctr);
- a.and_(a.eax, ~0x3);
- a.jmp(asmjit::Ptr(finaleFn));
- } else if (flags & BcBranchLR) {
- a.mov(a.eax, a.ppclr);
- a.and_(a.eax, ~0x3);
- a.jmp(asmjit::Ptr(finaleFn));
- } else {
- uint32_t nia = cia + sign_extend<16>(instr.bd << 2);
- auto i = jumpLabels.find(nia);
- if (i != jumpLabels.end()) {
- a.jmp(i->second);
- } else {
- a.mov(a.eax, nia);
- a.jmp(asmjit::Ptr(finaleFn));
- }
- }
-
- a.bind(doCondFailLbl);
- return true;
-}
-
-bool JitManager::jit_bc(PPCEmuAssembler& a, Instruction instr, uint32_t cia, const JumpLabelMap& jumpLabels)
-{
- return bcGeneric(a, instr, cia, jumpLabels, mFinaleFn);
-}
-
-bool JitManager::jit_bcctr(PPCEmuAssembler& a, Instruction instr, uint32_t cia, const JumpLabelMap& jumpLabels)
-{
- return bcGeneric(a, instr, cia, jumpLabels, mFinaleFn);
-}
-
-bool JitManager::jit_bclr(PPCEmuAssembler& a, Instruction instr, uint32_t cia, const JumpLabelMap& jumpLabels)
-{
- return bcGeneric(a, instr, cia, jumpLabels, mFinaleFn);
-}
-
-JitManager::JitManager()
- : mRuntime(new asmjit::JitRuntime()) {
-}
-
-JitManager::~JitManager() {
- if (mRuntime) {
- delete mRuntime;
- mRuntime = nullptr;
- }
-}
-
-void JitManager::clearCache() {
- if (mRuntime) {
- delete mRuntime;
- mRuntime = nullptr;
- }
-
- mRuntime = new asmjit::JitRuntime();
- mBlocks.clear();
- mSingleBlocks.clear();
- initStubs();
-}
-
-bool JitManager::prepare(uint32_t addr) {
- return get(addr) != nullptr;
-}
-
-JitCode JitManager::get(uint32_t addr) {
- auto i = mBlocks.find(addr);
- if (i != mBlocks.end()) {
- return i->second;
- }
-
- // Set it to nullptr first so we don't
- // try to regenerate after a failed attempt.
- mBlocks[addr] = nullptr;
-
- JitBlock block(addr);
-
- gLog->debug("Attempting to JIT {:08x}", block.start);
-
- if (!identBlock(block)) {
- return nullptr;
- }
-
- gLog->debug("Found end at {:08x}", block.end);
-
- if (!gen(block)) {
- return nullptr;
- }
-
- mBlocks[block.start] = block.entry;
- for (auto i = block.targets.cbegin(); i != block.targets.cend(); ++i) {
- if (i->second) {
- mBlocks[i->first] = i->second;
- }
- }
- return block.entry;
-}
-
-JitCode JitManager::getSingle(uint32_t addr) {
- auto i = mSingleBlocks.find(addr);
- if (i != mSingleBlocks.end()) {
- return i->second;
- }
-
- mSingleBlocks[addr] = nullptr;
-
- JitBlock block(addr);
- block.end = block.start + 4;
-
- if (!gen(block)) {
- return nullptr;
- }
-
- mSingleBlocks[addr] = block.entry;
- return block.entry;
-}
-
-typedef std::vector JumpTargetList;
-
-bool JitManager::identBlock(JitBlock& block) {
- auto fnStart = block.start;
- auto fnMax = fnStart;
- auto fnEnd = fnStart;
- bool jitFailed = false;
- JumpTargetList jumpTargets;
-
- auto lclCia = fnStart;
- while (lclCia) {
- auto instr = gMemory.read(lclCia);
- auto data = gInstructionTable.decode(instr);
-
- if (!JIT_CONTINUE_ON_ERROR) {
- // These ifs should match the generator loop below...
- if (data->id == InstructionID::b) {
- } else if (data->id == InstructionID::bc) {
- } else if (data->id == InstructionID::bcctr) {
- } else if (data->id == InstructionID::bclr) {
- } else {
- auto fptr = sJitInstructionMap[static_cast(data->id)];
-
- if (!fptr) {
- gLog->debug("JIT bailing due to unimplemented instruction {}", data->name);
- jitFailed = true;
- break;
- }
- }
- }
-
- uint32_t nia;
- switch (data->id) {
- case InstructionID::b:
- if (instr.lk) {
- jumpTargets.push_back(lclCia + 4);
- }
-
- nia = sign_extend<26>(instr.li << 2);
- if (!instr.aa) {
- nia += lclCia;
- }
- if (!instr.lk) {
- jumpTargets.push_back(nia);
- if (nia > fnMax) {
- fnMax = nia;
- }
- }
- break;
- case InstructionID::bc:
- if (instr.lk) {
- jumpTargets.push_back(lclCia + 4);
- }
-
- nia = sign_extend<16>(instr.bd << 2);
- if (!instr.aa) {
- nia += lclCia;
- }
- if (!instr.lk) {
- jumpTargets.push_back(nia);
- if (nia > fnMax) {
- fnMax = nia;
- }
- }
- break;
- case InstructionID::bcctr:
- // Target is unknown (CTR)
- if (instr.lk) {
- jumpTargets.push_back(lclCia + 4);
- }
-
- break;
- case InstructionID::bclr:
- // Target is unknown (LR)
- if (instr.lk) {
- jumpTargets.push_back(lclCia + 4);
- }
-
- if (get_bit(instr.bo) && get_bit(instr.bo)) {
- if (lclCia > fnMax) {
- fnMax = lclCia;
- fnEnd = fnMax + 4;
- }
- }
- break;
- default:
- break;
- }
-
- if (jitFailed || fnEnd != fnStart) {
- // If we found an end, lets stop searching!
- break;
- }
-
- lclCia += 4;
-
- if (((lclCia - fnStart) >> 2) > JIT_MAX_INST) {
- gLog->debug("Bailing on JIT due to max instruction limit at {:08x}", lclCia);
- jitFailed = true;
- break;
- }
- }
-
- if (jitFailed) {
- return false;
- }
-
- block.end = fnEnd;
-
- for (auto i : jumpTargets) {
- block.targets[i] = nullptr;
- }
- return true;
-}
-
-bool JitManager::gen(JitBlock& block)
-{
- PPCEmuAssembler a(mRuntime);
- bool jitFailed = false;
-
- JumpLabelMap jumpLabels;
- for (auto i = block.targets.begin(); i != block.targets.end(); ++i) {
- if (i->first >= block.start && i->first < block.end) {
- jumpLabels[i->first] = asmjit::Label(a);
- }
- }
-
- // Fix VS debug viewer...
- for (int i = 0; i < 8; ++i) {
- a.nop();
- }
-
- asmjit::Label codeStart(a);
- a.bind(codeStart);
-
- auto lclCia = block.start;
- while (lclCia < block.end) {
- auto ciaLbl = jumpLabels.find(lclCia);
- if (ciaLbl != jumpLabels.end()) {
- a.bind(ciaLbl->second);
- }
-
- a.mov(a.cia, lclCia);
-
- auto instr = gMemory.read(lclCia);
- auto data = gInstructionTable.decode(instr);
-
- bool genSuccess = false;
- if (data->id == InstructionID::b) {
- genSuccess = jit_b(a, instr, lclCia, jumpLabels);
- } else if (data->id == InstructionID::bc) {
- genSuccess = jit_bc(a, instr, lclCia, jumpLabels);
- } else if (data->id == InstructionID::bcctr) {
- genSuccess = jit_bcctr(a, instr, lclCia, jumpLabels);
- } else if (data->id == InstructionID::bclr) {
- genSuccess = jit_bclr(a, instr, lclCia, jumpLabels);
- } else {
- auto fptr = sJitInstructionMap[static_cast(data->id)];
- if (fptr) {
- genSuccess = fptr(a, instr);
- }
- }
-
- if (!genSuccess) {
- gLog->debug("JIT bailed due to generation failure on {}", data->name);
-
- if (!JIT_CONTINUE_ON_ERROR) {
- jitFailed = true;
- break;
- } else {
- a.int3();
- }
- }
-
- a.nop();
-
- lclCia += 4;
- }
-
- if (jitFailed) {
- return false;
- }
-
- // Debug Check
- for (auto i = jumpLabels.begin(); i != jumpLabels.end(); ++i) {
- if (!a.isLabelValid(i->second) || !a.isLabelBound(i->second)) {
- gLog->debug("Jump target {:08x} was never initialized...", i->first);
- }
- }
-
- a.mov(a.eax, block.end);
- a.jmp(asmjit::Ptr(mFinaleFn));
-
- JitCode func = asmjit_cast(a.make());
- if (func == nullptr) {
- gLog->error("JIT failed due to asmjit make failure");
- return false;
- }
-
- auto baseAddr = asmjit_cast(func, a.getLabelOffset(codeStart));
- block.entry = baseAddr;
- for (auto i = jumpLabels.cbegin(); i != jumpLabels.cend(); ++i) {
- block.targets[i->first] = asmjit_cast(func, a.getLabelOffset(i->second));
- }
-
- return true;
-}
-
-uint32_t JitManager::execute(ThreadState *state, JitCode block) {
- return mCallFn(state, block);
+#include "log.h"
+#include "interpreter.h"
+#include "instructiondata.h"
+
+JitManager
+gJitManager;
+
+static std::vector
+sJitInstructionMap;
+
+void JitManager::RegisterFunctions()
+{
+ static bool didInit = false;
+
+ if (!didInit) {
+ // Reserve instruction map
+ sJitInstructionMap.resize(static_cast(InstructionID::InstructionCount), nullptr);
+
+ // Register JIT instruction handlers
+ registerBranchInstructions();
+ registerConditionInstructions();
+ registerFloatInstructions();
+ registerIntegerInstructions();
+ registerLoadStoreInstructions();
+ registerPairedInstructions();
+ registerSystemInstructions();
+
+ didInit = true;
+ }
+}
+
+void JitManager::registerInstruction(InstructionID id, jitinstrfptr_t fptr)
+{
+ sJitInstructionMap[static_cast(id)] = fptr;
+}
+
+bool JitManager::initialise() {
+ initStubs();
+ return true;
+}
+
+void JitManager::initStubs() {
+ PPCEmuAssembler a(mRuntime);
+
+ asmjit::Label introLabel(a);
+ asmjit::Label extroLabel(a);
+
+ a.bind(introLabel);
+ a.push(a.zbx);
+ a.push(a.zsi);
+ a.sub(a.zsp, 0x28);
+ a.mov(a.zbx, a.zcx);
+ a.mov(a.zsi, gMemory.base());
+ a.jmp(a.zdx);
+
+ a.bind(extroLabel);
+ a.add(a.zsp, 0x28);
+ a.pop(a.zsi);
+ a.pop(a.zbx);
+ a.ret();
+
+ auto basePtr = a.make();
+ mCallFn = asmjit_cast(basePtr, a.getLabelOffset(introLabel));
+ mFinaleFn = asmjit_cast(basePtr, a.getLabelOffset(extroLabel));
+}
+
+enum BoBits
+{
+ CtrValue = 1,
+ NoCheckCtr = 2,
+ CondValue = 3,
+ NoCheckCond = 4
+};
+enum BcFlags
+{
+ BcCheckCtr = 1 << 0,
+ BcCheckCond = 1 << 1,
+ BcBranchLR = 1 << 2,
+ BcBranchCTR = 1 << 3
+};
+
+bool JitManager::jit_b(PPCEmuAssembler& a, Instruction instr, uint32_t cia, const JumpLabelMap& jumpLabels)
+{
+ uint32_t nia = sign_extend<26>(instr.li << 2);
+ if (!instr.aa) {
+ nia += cia;
+ }
+
+ if (instr.lk) {
+ a.mov(a.eax, cia + 4u);
+ a.mov(a.ppclr, a.eax);
+
+ a.mov(a.eax, nia);
+ a.jmp(asmjit::Ptr(mFinaleFn));
+ return true;
+ }
+
+ auto i = jumpLabels.find(nia);
+ if (i != jumpLabels.end()) {
+ a.jmp(i->second);
+ } else {
+ a.mov(a.eax, nia);
+ a.jmp(asmjit::Ptr(mFinaleFn));
+ }
+
+ return true;
+}
+
+template
+static bool
+bcGeneric(PPCEmuAssembler& a, Instruction instr, uint32_t cia, const JumpLabelMap& jumpLabels, JitFinale finaleFn)
+{
+ uint32_t bo = instr.bo;
+ asmjit::Label doCondFailLbl(a);
+
+ if (flags & BcCheckCtr) {
+ if (!get_bit(bo)) {
+ //state->ctr--;
+ //ctr_ok = !!((state->ctr != 0) ^ (get_bit(bo)));
+
+ a.dec(a.ppcctr);
+
+ a.mov(a.eax, a.ppcctr);
+ a.cmp(a.eax, 0);
+ if (get_bit(bo)) {
+ a.jne(doCondFailLbl);
+ } else {
+ a.je(doCondFailLbl);
+ }
+ }
+ }
+
+ if (flags & BcCheckCond) {
+ if (!get_bit(bo)) {
+ //auto crb = get_bit(state->cr.value, 31 - instr.bi);
+ //auto crv = get_bit(bo);
+ //cond_ok = (crb == crv);
+
+ a.mov(a.eax, a.ppccr);
+ a.and_(a.eax, 1 << (31 - instr.bi));
+ a.cmp(a.eax, 0);
+
+ if (get_bit(bo)) {
+ a.je(doCondFailLbl);
+ } else {
+ a.jne(doCondFailLbl);
+ }
+ }
+ }
+
+ if (instr.lk) {
+ a.mov(a.eax, cia + 4);
+ a.mov(a.ppclr, a.eax);
+ }
+
+ // Make sure no JMP related instructions end up above
+ // this if-block as we use a JMP instruction with
+ // early exit in the else block...
+ if (flags & BcBranchCTR) {
+ a.mov(a.eax, a.ppcctr);
+ a.and_(a.eax, ~0x3);
+ a.jmp(asmjit::Ptr(finaleFn));
+ } else if (flags & BcBranchLR) {
+ a.mov(a.eax, a.ppclr);
+ a.and_(a.eax, ~0x3);
+ a.jmp(asmjit::Ptr(finaleFn));
+ } else {
+ uint32_t nia = cia + sign_extend<16>(instr.bd << 2);
+ auto i = jumpLabels.find(nia);
+ if (i != jumpLabels.end()) {
+ a.jmp(i->second);
+ } else {
+ a.mov(a.eax, nia);
+ a.jmp(asmjit::Ptr(finaleFn));
+ }
+ }
+
+ a.bind(doCondFailLbl);
+ return true;
+}
+
+bool JitManager::jit_bc(PPCEmuAssembler& a, Instruction instr, uint32_t cia, const JumpLabelMap& jumpLabels)
+{
+ return bcGeneric(a, instr, cia, jumpLabels, mFinaleFn);
+}
+
+bool JitManager::jit_bcctr(PPCEmuAssembler& a, Instruction instr, uint32_t cia, const JumpLabelMap& jumpLabels)
+{
+ return bcGeneric(a, instr, cia, jumpLabels, mFinaleFn);
+}
+
+bool JitManager::jit_bclr(PPCEmuAssembler& a, Instruction instr, uint32_t cia, const JumpLabelMap& jumpLabels)
+{
+ return bcGeneric(a, instr, cia, jumpLabels, mFinaleFn);
+}
+
+JitManager::JitManager()
+ : mRuntime(new asmjit::JitRuntime()) {
+}
+
+JitManager::~JitManager() {
+ if (mRuntime) {
+ delete mRuntime;
+ mRuntime = nullptr;
+ }
+}
+
+void JitManager::clearCache() {
+ if (mRuntime) {
+ delete mRuntime;
+ mRuntime = nullptr;
+ }
+
+ mRuntime = new asmjit::JitRuntime();
+ mBlocks.clear();
+ mSingleBlocks.clear();
+ initStubs();
+}
+
+bool JitManager::prepare(uint32_t addr) {
+ return get(addr) != nullptr;
+}
+
+JitCode JitManager::get(uint32_t addr) {
+ auto i = mBlocks.find(addr);
+ if (i != mBlocks.end()) {
+ return i->second;
+ }
+
+ // Set it to nullptr first so we don't
+ // try to regenerate after a failed attempt.
+ mBlocks[addr] = nullptr;
+
+ JitBlock block(addr);
+
+ gLog->debug("Attempting to JIT {:08x}", block.start);
+
+ if (!identBlock(block)) {
+ return nullptr;
+ }
+
+ gLog->debug("Found end at {:08x}", block.end);
+
+ if (!gen(block)) {
+ return nullptr;
+ }
+
+ mBlocks[block.start] = block.entry;
+ for (auto i = block.targets.cbegin(); i != block.targets.cend(); ++i) {
+ if (i->second) {
+ mBlocks[i->first] = i->second;
+ }
+ }
+ return block.entry;
+}
+
+JitCode JitManager::getSingle(uint32_t addr) {
+ auto i = mSingleBlocks.find(addr);
+ if (i != mSingleBlocks.end()) {
+ return i->second;
+ }
+
+ mSingleBlocks[addr] = nullptr;
+
+ JitBlock block(addr);
+ block.end = block.start + 4;
+
+ if (!gen(block)) {
+ return nullptr;
+ }
+
+ mSingleBlocks[addr] = block.entry;
+ return block.entry;
+}
+
+typedef std::vector JumpTargetList;
+
+bool JitManager::identBlock(JitBlock& block) {
+ auto fnStart = block.start;
+ auto fnMax = fnStart;
+ auto fnEnd = fnStart;
+ bool jitFailed = false;
+ JumpTargetList jumpTargets;
+
+ auto lclCia = fnStart;
+ while (lclCia) {
+ auto instr = gMemory.read(lclCia);
+ auto data = gInstructionTable.decode(instr);
+
+ if (!JIT_CONTINUE_ON_ERROR) {
+ // These ifs should match the generator loop below...
+ if (data->id == InstructionID::b) {
+ } else if (data->id == InstructionID::bc) {
+ } else if (data->id == InstructionID::bcctr) {
+ } else if (data->id == InstructionID::bclr) {
+ } else {
+ auto fptr = sJitInstructionMap[static_cast(data->id)];
+
+ if (!fptr) {
+ gLog->debug("JIT bailing due to unimplemented instruction {}", data->name);
+ jitFailed = true;
+ break;
+ }
+ }
+ }
+
+ uint32_t nia;
+ switch (data->id) {
+ case InstructionID::b:
+ if (instr.lk) {
+ jumpTargets.push_back(lclCia + 4);
+ }
+
+ nia = sign_extend<26>(instr.li << 2);
+ if (!instr.aa) {
+ nia += lclCia;
+ }
+ if (!instr.lk) {
+ jumpTargets.push_back(nia);
+ if (nia > fnMax) {
+ fnMax = nia;
+ }
+ }
+ break;
+ case InstructionID::bc:
+ if (instr.lk) {
+ jumpTargets.push_back(lclCia + 4);
+ }
+
+ nia = sign_extend<16>(instr.bd << 2);
+ if (!instr.aa) {
+ nia += lclCia;
+ }
+ if (!instr.lk) {
+ jumpTargets.push_back(nia);
+ if (nia > fnMax) {
+ fnMax = nia;
+ }
+ }
+ break;
+ case InstructionID::bcctr:
+ // Target is unknown (CTR)
+ if (instr.lk) {
+ jumpTargets.push_back(lclCia + 4);
+ }
+
+ break;
+ case InstructionID::bclr:
+ // Target is unknown (LR)
+ if (instr.lk) {
+ jumpTargets.push_back(lclCia + 4);
+ }
+
+ if (get_bit(instr.bo) && get_bit(instr.bo)) {
+ if (lclCia > fnMax) {
+ fnMax = lclCia;
+ fnEnd = fnMax + 4;
+ }
+ }
+ break;
+ default:
+ break;
+ }
+
+ if (jitFailed || fnEnd != fnStart) {
+ // If we found an end, lets stop searching!
+ break;
+ }
+
+ lclCia += 4;
+
+ if (((lclCia - fnStart) >> 2) > JIT_MAX_INST) {
+ gLog->debug("Bailing on JIT due to max instruction limit at {:08x}", lclCia);
+ jitFailed = true;
+ break;
+ }
+ }
+
+ if (jitFailed) {
+ return false;
+ }
+
+ block.end = fnEnd;
+
+ for (auto i : jumpTargets) {
+ block.targets[i] = nullptr;
+ }
+ return true;
+}
+
+bool JitManager::gen(JitBlock& block)
+{
+ PPCEmuAssembler a(mRuntime);
+ bool jitFailed = false;
+
+ JumpLabelMap jumpLabels;
+ for (auto i = block.targets.begin(); i != block.targets.end(); ++i) {
+ if (i->first >= block.start && i->first < block.end) {
+ jumpLabels[i->first] = asmjit::Label(a);
+ }
+ }
+
+ // Fix VS debug viewer...
+ for (int i = 0; i < 8; ++i) {
+ a.nop();
+ }
+
+ asmjit::Label codeStart(a);
+ a.bind(codeStart);
+
+ auto lclCia = block.start;
+ while (lclCia < block.end) {
+ auto ciaLbl = jumpLabels.find(lclCia);
+ if (ciaLbl != jumpLabels.end()) {
+ a.bind(ciaLbl->second);
+ }
+
+ a.mov(a.cia, lclCia);
+
+ auto instr = gMemory.read(lclCia);
+ auto data = gInstructionTable.decode(instr);
+
+ bool genSuccess = false;
+ if (data->id == InstructionID::b) {
+ genSuccess = jit_b(a, instr, lclCia, jumpLabels);
+ } else if (data->id == InstructionID::bc) {
+ genSuccess = jit_bc(a, instr, lclCia, jumpLabels);
+ } else if (data->id == InstructionID::bcctr) {
+ genSuccess = jit_bcctr(a, instr, lclCia, jumpLabels);
+ } else if (data->id == InstructionID::bclr) {
+ genSuccess = jit_bclr(a, instr, lclCia, jumpLabels);
+ } else {
+ auto fptr = sJitInstructionMap[static_cast(data->id)];
+ if (fptr) {
+ genSuccess = fptr(a, instr);
+ }
+ }
+
+ if (!genSuccess) {
+ gLog->debug("JIT bailed due to generation failure on {}", data->name);
+
+ if (!JIT_CONTINUE_ON_ERROR) {
+ jitFailed = true;
+ break;
+ } else {
+ a.int3();
+ }
+ }
+
+ a.nop();
+
+ lclCia += 4;
+ }
+
+ if (jitFailed) {
+ return false;
+ }
+
+ // Debug Check
+ for (auto i = jumpLabels.begin(); i != jumpLabels.end(); ++i) {
+ if (!a.isLabelValid(i->second) || !a.isLabelBound(i->second)) {
+ gLog->debug("Jump target {:08x} was never initialized...", i->first);
+ }
+ }
+
+ a.mov(a.eax, block.end);
+ a.jmp(asmjit::Ptr(mFinaleFn));
+
+ JitCode func = asmjit_cast(a.make());
+ if (func == nullptr) {
+ gLog->error("JIT failed due to asmjit make failure");
+ return false;
+ }
+
+ auto baseAddr = asmjit_cast(func, a.getLabelOffset(codeStart));
+ block.entry = baseAddr;
+ for (auto i = jumpLabels.cbegin(); i != jumpLabels.cend(); ++i) {
+ block.targets[i->first] = asmjit_cast(func, a.getLabelOffset(i->second));
+ }
+
+ return true;
+}
+
+uint32_t JitManager::execute(ThreadState *state, JitCode block) {
+ return mCallFn(state, block);
}
\ No newline at end of file
diff --git a/src/jit/jit_system.cpp b/src/jit/jit_system.cpp
index 0337c862..02096dc1 100644
--- a/src/jit/jit_system.cpp
+++ b/src/jit/jit_system.cpp
@@ -4,7 +4,6 @@
#include "log.h"
#include "system.h"
#include "kernelfunction.h"
-#include "thread.h"
#include "usermodule.h"
static SprEncoding
diff --git a/src/main.cpp b/src/main.cpp
index 8d544915..1ff93d5b 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -5,6 +5,7 @@
#include "filesystem.h"
#include "instructiondata.h"
#include "interpreter.h"
+#include "processor.h"
#include "jit.h"
#include "loader.h"
#include "log.h"
@@ -19,7 +20,6 @@
#include "modules/proc_ui/proc_ui.h"
#include "modules/padscore/padscore.h"
#include "system.h"
-#include "thread.h"
#include "usermodule.h"
std::shared_ptr
@@ -196,6 +196,9 @@ play(const std::string &path)
// TODO: Call __preinit_user
CoreInitDefaultHeap();
+ // Startup processor
+ gProcessor.start();
+
// Allocate OSThread structures and stacks
auto thread1 = OSAllocFromSystem();
auto stack1 = reinterpret_cast(OSAllocFromSystem(stackSize, 8));
diff --git a/src/modules/coreinit/coreinit_core.cpp b/src/modules/coreinit/coreinit_core.cpp
index c82c0d43..4d862430 100644
--- a/src/modules/coreinit/coreinit_core.cpp
+++ b/src/modules/coreinit/coreinit_core.cpp
@@ -1,17 +1,17 @@
#include "coreinit.h"
#include "coreinit_core.h"
-#include "thread.h"
+#include "processor.h"
uint32_t
OSGetCoreCount()
{
- return 3;
+ return gProcessor.getCoreCount();
}
uint32_t
OSGetCoreId()
{
- return Thread::getCurrentThread()->getCoreID();
+ return gProcessor.getCoreID();
}
uint32_t
diff --git a/src/modules/coreinit/coreinit_dynload.cpp b/src/modules/coreinit/coreinit_dynload.cpp
index fc04d84d..356e5053 100644
--- a/src/modules/coreinit/coreinit_dynload.cpp
+++ b/src/modules/coreinit/coreinit_dynload.cpp
@@ -4,7 +4,6 @@
#include "coreinit_memheap.h"
#include "coreinit_expheap.h"
#include "interpreter.h"
-#include "thread.h"
static wfunc_ptr*>
pOSDynLoad_MemAlloc;
@@ -16,7 +15,9 @@ template
static inline uint32_t
OSExecuteCallback(uint32_t addr, uint32_t (&args)[N])
{
- auto state = Thread::getCurrentThread()->getThreadState();
+ assert(false);
+ return 0;
+ /*auto state = Thread::getCurrentThread()->getThreadState();
uint32_t save[N];
uint32_t result;
@@ -33,11 +34,14 @@ OSExecuteCallback(uint32_t addr, uint32_t (&args)[N])
state->gpr[3 + i] = save[i];
}
- return result;
+ return result;*/
}
int MEM_DynLoad_DefaultAlloc(int size, int alignment, be_val *outPtr)
{
+ assert(false);
+ return 0;
+ /*
if (!outPtr) {
return 0xBAD10017;
}
@@ -54,16 +58,18 @@ int MEM_DynLoad_DefaultAlloc(int size, int alignment, be_val *outPtr)
};
*outPtr = OSExecuteCallback(static_cast(pMEMAllocFromDefaultHeapEx), args);
- return 0;
+ return 0;*/
}
void MEM_DynLoad_DefaultFree(p32 addr)
{
+ assert(false);
+ /*
uint32_t args[] = {
static_cast(addr)
};
- OSExecuteCallback(static_cast(pMEMFreeToDefaultHeap), args);
+ OSExecuteCallback(static_cast(pMEMFreeToDefaultHeap), args);*/
}
int
diff --git a/src/modules/coreinit/coreinit_event.cpp b/src/modules/coreinit/coreinit_event.cpp
index 706e30ff..2523f8aa 100644
--- a/src/modules/coreinit/coreinit_event.cpp
+++ b/src/modules/coreinit/coreinit_event.cpp
@@ -1,15 +1,17 @@
#include "coreinit.h"
#include "coreinit_event.h"
+#include "coreinit_thread.h"
#include "system.h"
+#include "processor.h"
void
-OSInitEvent(EventHandle handle, BOOL value, EventMode mode)
+OSInitEvent(EventHandle handle, bool value, EventMode mode)
{
OSInitEventEx(handle, value, mode, nullptr);
}
void
-OSInitEventEx(EventHandle handle, BOOL value, EventMode mode, char *name)
+OSInitEventEx(EventHandle handle, bool value, EventMode mode, char *name)
{
auto event = gSystem.addSystemObject(handle);
event->mode = mode;
@@ -21,12 +23,31 @@ void
OSSignalEvent(EventHandle handle)
{
auto event = gSystem.getSystemObject(handle);
+ std::unique_lock lock { event->mutex };
+
event->value = TRUE;
if (event->mode == EventMode::ManualReset) {
- event->condition.notify_all();
+ // Wake all waiting fibers
+ for (auto fiber : event->queue) {
+ gProcessor.queue(fiber);
+ }
+
+ event->queue.clear();
} else if (event->mode == EventMode::AutoReset) {
- event->condition.notify_one();
+ // Wake highest priority fiber
+ Fiber *highest = nullptr;
+
+ for (auto fiber : event->queue) {
+ if (!highest || fiber->thread->basePriority > highest->thread->basePriority) {
+ highest = fiber;
+ }
+ }
+
+ if (highest) {
+ gProcessor.queue(highest);
+ event->queue.erase(std::remove(event->queue.begin(), event->queue.end(), highest), event->queue.end());
+ }
}
}
@@ -34,14 +55,22 @@ void
OSSignalEventAll(EventHandle handle)
{
auto event = gSystem.getSystemObject(handle);
+ std::unique_lock lock { event->mutex };
event->value = TRUE;
- event->condition.notify_all();
+
+ // Wake all fibers
+ for (auto fiber : event->queue) {
+ gProcessor.queue(fiber);
+ }
+
+ event->queue.clear();
}
void
OSResetEvent(EventHandle handle)
{
auto event = gSystem.getSystemObject(handle);
+ std::unique_lock lock { event->mutex };
event->value = FALSE;
}
@@ -49,10 +78,17 @@ void
OSWaitEvent(EventHandle handle)
{
auto event = gSystem.getSystemObject(handle);
- auto lock = std::unique_lock { event->mutex };
+ auto fiber = gProcessor.getCurrentFiber();
- if (!event->value == TRUE) {
- event->condition.wait(lock, [event] { return event->value; });
+ while (!event->value) {
+ std::unique_lock lock { event->mutex };
+
+ if (event->value) {
+ break;
+ }
+
+ event->queue.push_back(fiber);
+ gProcessor.wait(lock);
}
if (event->mode == EventMode::AutoReset) {
@@ -64,12 +100,26 @@ BOOL
OSWaitEventWithTimeout(EventHandle handle, Time timeout)
{
auto event = gSystem.getSystemObject(handle);
- auto lock = std::unique_lock { event->mutex };
+ auto fiber = gProcessor.getCurrentFiber();
+ auto start = std::chrono::system_clock::now();
- if (!event->value == TRUE) {
- if (!event->condition.wait_for(lock, std::chrono::nanoseconds(timeout), [event] { return event->value; })) {
+ while (!event->value) {
+ std::unique_lock lock { event->mutex };
+
+ if (event->value) {
+ break;
+ }
+
+ // Check for timeout
+ auto diff = std::chrono::system_clock::now() - start;
+ auto ns = std::chrono::duration_cast(diff).count();
+
+ if (ns >= timeout) {
return FALSE;
}
+
+ event->queue.push_back(fiber);
+ gProcessor.wait(lock);
}
if (event->mode == EventMode::AutoReset) {
diff --git a/src/modules/coreinit/coreinit_event.h b/src/modules/coreinit/coreinit_event.h
index 912a75d1..3e67d57d 100644
--- a/src/modules/coreinit/coreinit_event.h
+++ b/src/modules/coreinit/coreinit_event.h
@@ -1,6 +1,4 @@
#pragma once
-#include
-#include
#include "coreinit_time.h"
#include "systemobject.h"
@@ -10,24 +8,26 @@ enum class EventMode
AutoReset = 1
};
+struct Fiber;
+
struct Event : public SystemObject
{
static const uint32_t Tag = 0x65566E54;
char *name;
EventMode mode;
- BOOL value;
+ std::atomic value;
std::mutex mutex;
- std::condition_variable condition;
+ std::vector queue;
};
-using EventHandle = p32;
+using EventHandle = SystemObjectHeader *;
void
-OSInitEvent(EventHandle handle, BOOL value, EventMode mode);
+OSInitEvent(EventHandle handle, bool value, EventMode mode);
void
-OSInitEventEx(EventHandle handle, BOOL value, EventMode mode, char *name);
+OSInitEventEx(EventHandle handle, bool value, EventMode mode, char *name);
void
OSSignalEvent(EventHandle handle);
diff --git a/src/modules/coreinit/coreinit_messagequeue.cpp b/src/modules/coreinit/coreinit_messagequeue.cpp
index f13d8a20..63ffa2dd 100644
--- a/src/modules/coreinit/coreinit_messagequeue.cpp
+++ b/src/modules/coreinit/coreinit_messagequeue.cpp
@@ -25,6 +25,14 @@ OSInitMessageQueueEx(MessageQueueHandle handle, OSMessage *messages, int32_t siz
queue->count = 0;
queue->size = size;
queue->messages = messages;
+
+ queue->mutex = OSAllocMutex();
+ queue->waitRead = OSAllocCondition();
+ queue->waitSend = OSAllocCondition();
+
+ OSInitMutex(queue->mutex);
+ OSInitCond(queue->waitRead);
+ OSInitCond(queue->waitSend);
}
// Insert at back of message queue
@@ -32,7 +40,7 @@ BOOL
OSSendMessage(MessageQueueHandle handle, OSMessage *message, MessageFlags::Flags flags)
{
auto queue = gSystem.getSystemObject(handle);
- std::unique_lock lock { queue->mutex };
+ OSLockMutex(queue->mutex);
if (!(flags & MessageFlags::Blocking) && queue->count == queue->size) {
// Do not wait for space
@@ -41,7 +49,7 @@ OSSendMessage(MessageQueueHandle handle, OSMessage *message, MessageFlags::Flags
// Wait for space
while (queue->count == queue->size) {
- queue->waitSend.wait(lock);
+ OSWaitCond(queue->waitSend, queue->mutex);
}
auto index = (queue->first + queue->count) % queue->size;
@@ -52,8 +60,9 @@ OSSendMessage(MessageQueueHandle handle, OSMessage *message, MessageFlags::Flags
queue->count++;
// Wake up anyone waiting for message to read
- lock.unlock();
- queue->waitRead.notify_one();
+ OSUnlockMutex(queue->mutex);
+ OSSignalCond(queue->waitRead);
+
return TRUE;
}
@@ -62,17 +71,18 @@ BOOL
OSJamMessage(MessageQueueHandle handle, OSMessage *message, MessageFlags::Flags flags)
{
auto queue = gSystem.getSystemObject(handle);
- std::unique_lock lock { queue->mutex };
uint32_t index;
+ OSLockMutex(queue->mutex);
if (!(flags & MessageFlags::Blocking) && queue->count == queue->size) {
// Do not wait for space
+ OSUnlockMutex(queue->mutex);
return FALSE;
}
// Wait for space
while (queue->count == queue->size) {
- queue->waitSend.wait(lock);
+ OSWaitCond(queue->waitSend, queue->mutex);
}
// Get index before front
@@ -91,8 +101,9 @@ OSJamMessage(MessageQueueHandle handle, OSMessage *message, MessageFlags::Flags
queue->count++;
// Wake up anyone waiting for message to read
- lock.unlock();
- queue->waitRead.notify_one();
+ OSUnlockMutex(queue->mutex);
+ OSSignalCond(queue->waitRead);
+
return TRUE;
}
@@ -101,14 +112,16 @@ BOOL
OSReceiveMessage(MessageQueueHandle handle, OSMessage *message, MessageFlags::Flags flags)
{
auto queue = gSystem.getSystemObject(handle);
- std::unique_lock lock { queue->mutex };
+ OSLockMutex(queue->mutex);
if (!(flags & MessageFlags::Blocking) && queue->count == 0) {
+ // Do not wait for space
+ OSUnlockMutex(queue->mutex);
return FALSE;
}
while (queue->count == 0) {
- queue->waitRead.wait(lock);
+ OSWaitCond(queue->waitRead, queue->mutex);
}
auto index = queue->first;
@@ -120,8 +133,9 @@ OSReceiveMessage(MessageQueueHandle handle, OSMessage *message, MessageFlags::Fl
queue->count--;
// Wake up anyone waiting for space to send
- lock.unlock();
- queue->waitSend.notify_one();
+ OSUnlockMutex(queue->mutex);
+ OSSignalCond(queue->waitSend);
+
return TRUE;
}
@@ -130,15 +144,18 @@ BOOL
OSPeekMessage(MessageQueueHandle handle, OSMessage *message)
{
auto queue = gSystem.getSystemObject(handle);
- std::unique_lock lock { queue->mutex };
+ OSLockMutex(queue->mutex);
if (queue->count == 0) {
+ OSUnlockMutex(queue->mutex);
return FALSE;
}
// Copy from message array
auto src = static_cast(queue->messages) + queue->first;
memcpy(message, src, sizeof(OSMessage));
+
+ OSUnlockMutex(queue->mutex);
return TRUE;
}
@@ -163,7 +180,7 @@ CoreInit::registerMessageQueueFunctions()
void
CoreInit::initialiseMessageQueues()
{
- gSystemMessageQueue = make_p32(OSAllocFromSystem());
+ gSystemMessageQueue = OSAllocFromSystem();
gSystemMessageArray = reinterpret_cast(OSAllocFromSystem(16 * sizeof(OSMessage)));
OSInitMessageQueue(gSystemMessageQueue, gSystemMessageArray, 16);
}
diff --git a/src/modules/coreinit/coreinit_messagequeue.h b/src/modules/coreinit/coreinit_messagequeue.h
index adc9fa78..f62096f0 100644
--- a/src/modules/coreinit/coreinit_messagequeue.h
+++ b/src/modules/coreinit/coreinit_messagequeue.h
@@ -1,8 +1,7 @@
#pragma once
-#include
-#include
#include "systemtypes.h"
#include "coreinit_thread.h"
+#include "coreinit_mutex.h"
#include "util.h"
#pragma pack(push, 1)
@@ -33,12 +32,12 @@ struct MessageQueue : public SystemObject
uint32_t count;
uint32_t size;
OSMessage *messages;
- std::mutex mutex;
- std::condition_variable waitSend;
- std::condition_variable waitRead;
+ MutexHandle mutex;
+ ConditionHandle waitSend;
+ ConditionHandle waitRead;
};
-using MessageQueueHandle = p32;
+using MessageQueueHandle = SystemObjectHeader *;
void
OSInitMessageQueue(MessageQueueHandle handle, OSMessage *messages, int32_t size);
diff --git a/src/modules/coreinit/coreinit_mutex.cpp b/src/modules/coreinit/coreinit_mutex.cpp
index 66438609..e6b4d1a9 100644
--- a/src/modules/coreinit/coreinit_mutex.cpp
+++ b/src/modules/coreinit/coreinit_mutex.cpp
@@ -1,6 +1,20 @@
#include "coreinit.h"
#include "coreinit_mutex.h"
+#include "coreinit_memheap.h"
#include "system.h"
+#include "processor.h"
+
+MutexHandle
+OSAllocMutex()
+{
+ return OSAllocFromSystem();
+}
+
+ConditionHandle
+OSAllocCondition()
+{
+ return OSAllocFromSystem();
+}
void
OSInitMutex(MutexHandle handle)
@@ -13,27 +27,73 @@ OSInitMutexEx(MutexHandle handle, char *name)
{
auto mutex = gSystem.addSystemObject(handle);
mutex->name = name;
+ mutex->count = 0;
+ mutex->owner = nullptr;
}
void
OSLockMutex(MutexHandle handle)
{
auto mutex = gSystem.getSystemObject(handle);
- mutex->mutex.lock();
+ auto fiber = gProcessor.getCurrentFiber();
+
+ while (true) {
+ std::unique_lock lock { mutex->mutex };
+
+ if (!mutex->owner || mutex->owner == fiber) {
+ break;
+ }
+
+ mutex->queue.push_back(fiber);
+ gProcessor.wait(lock);
+ }
+
+ mutex->count++;
+ mutex->owner = fiber;
}
BOOL
OSTryLockMutex(MutexHandle handle)
{
auto mutex = gSystem.getSystemObject(handle);
- return mutex->mutex.try_lock();
+ auto fiber = gProcessor.getCurrentFiber();
+
+ std::unique_lock lock { mutex->mutex };
+
+ if (mutex->owner == fiber) {
+ mutex->count++;
+ return TRUE;
+ } else if (!mutex->owner) {
+ mutex->owner = fiber;
+ mutex->count = 1;
+ return TRUE;
+ }
+
+ return FALSE;
}
void
OSUnlockMutex(MutexHandle handle)
{
auto mutex = gSystem.getSystemObject(handle);
- mutex->mutex.unlock();
+ auto fiber = gProcessor.getCurrentFiber();
+
+ std::unique_lock lock { mutex->mutex };
+ assert(mutex->owner == fiber);
+
+ mutex->count--;
+
+ if (mutex->count) {
+ return;
+ }
+
+ mutex->owner = nullptr;
+
+ for (auto fiber : mutex->queue) {
+ gProcessor.queue(fiber);
+ }
+
+ mutex->queue.clear();
}
void
@@ -47,22 +107,55 @@ OSInitCondEx(ConditionHandle handle, char *name)
{
auto condition = gSystem.addSystemObject(handle);
condition->name = name;
+ condition->value = false;
}
void
OSWaitCond(ConditionHandle conditionHandle, MutexHandle mutexHandle)
{
auto condition = gSystem.getSystemObject(conditionHandle);
+ auto fiber = gProcessor.getCurrentFiber();
+
+ // Unlock mutex
auto mutex = gSystem.getSystemObject(mutexHandle);
- auto lock = std::unique_lock { mutex->mutex };
- condition->condition.wait(lock);
+ auto count = mutex->count;
+ mutex->count = 1;
+ OSUnlockMutex(mutexHandle);
+
+ // Try acquire condition
+ while (true) {
+ std::unique_lock lock { condition->mutex };
+
+ if (condition->value) {
+ break;
+ }
+
+ condition->queue.push_back(fiber);
+ gProcessor.wait(lock);
+ }
+
+ condition->value = false;
+
+ // Lock mutex
+ OSLockMutex(mutexHandle);
+ mutex->count = count;
}
void
OSSignalCond(ConditionHandle handle)
{
auto condition = gSystem.getSystemObject(handle);
- condition->condition.notify_all();
+ std::unique_lock lock { condition->mutex };
+
+ // Set condition
+ condition->value = true;
+
+ // Wake all waiting fibers
+ for (auto fiber : condition->queue) {
+ gProcessor.queue(fiber);
+ }
+
+ condition->queue.clear();
}
void
diff --git a/src/modules/coreinit/coreinit_mutex.h b/src/modules/coreinit/coreinit_mutex.h
index b29ec764..820040da 100644
--- a/src/modules/coreinit/coreinit_mutex.h
+++ b/src/modules/coreinit/coreinit_mutex.h
@@ -4,12 +4,17 @@
#include "systemobject.h"
#include "coreinit_thread.h"
+struct Fiber;
+
struct Mutex : public SystemObject
{
static const uint32_t Tag = 0x6D557458;
char *name;
- std::recursive_mutex mutex;
+ uint32_t count;
+ std::mutex mutex;
+ Fiber *owner;
+ std::vector queue;
};
struct Condition : public SystemObject
@@ -17,11 +22,16 @@ struct Condition : public SystemObject
static const uint32_t Tag = 0x634E6456;
char *name;
- std::condition_variable_any condition;
+ bool value;
+ std::mutex mutex;
+ std::vector queue;
};
-using MutexHandle = p32;
-using ConditionHandle = p32;
+using MutexHandle = SystemObjectHeader *;
+using ConditionHandle = SystemObjectHeader *;
+
+MutexHandle
+OSAllocMutex();
void
OSInitMutex(MutexHandle handle);
@@ -38,11 +48,14 @@ OSUnlockMutex(MutexHandle handle);
BOOL
OSTryLockMutex(MutexHandle handle);
+ConditionHandle
+OSAllocCondition();
+
void
OSInitCond(ConditionHandle handle);
void
-OSInitCondEx(ConditionHandle handle, char *nname);
+OSInitCondEx(ConditionHandle handle, char *name);
void
OSWaitCond(ConditionHandle conditionHandle, MutexHandle mutexHandle);
diff --git a/src/modules/coreinit/coreinit_semaphore.cpp b/src/modules/coreinit/coreinit_semaphore.cpp
index 4f5a7499..cd0f3af2 100644
--- a/src/modules/coreinit/coreinit_semaphore.cpp
+++ b/src/modules/coreinit/coreinit_semaphore.cpp
@@ -1,5 +1,6 @@
#include "coreinit.h"
#include "coreinit_semaphore.h"
+#include "processor.h"
#include "system.h"
void
@@ -20,43 +21,51 @@ int32_t
OSWaitSemaphore(SemaphoreHandle handle)
{
auto semaphore = gSystem.getSystemObject(handle);
- auto lock = std::unique_lock { semaphore->mutex };
- auto previous = semaphore->count;
+ auto fiber = gProcessor.getCurrentFiber();
- while (semaphore->count <= 0) {
- semaphore->condition.wait(lock);
+ while (true) {
+ std::unique_lock lock { semaphore->mutex };
+
+ if (semaphore->count > 0) {
+ break;
+ }
+
+ semaphore->queue.push_back(fiber);
+ gProcessor.wait(lock);
}
- --semaphore->count;
- return previous;
+ return semaphore->count--;
}
int32_t
OSTryWaitSemaphore(SemaphoreHandle handle)
{
auto semaphore = gSystem.getSystemObject(handle);
- auto lock = std::unique_lock { semaphore->mutex };
- auto previous = semaphore->count;
+ auto fiber = gProcessor.getCurrentFiber();
+ std::unique_lock lock { semaphore->mutex };
- if (semaphore->count > 0) {
- --semaphore->count;
+ if (semaphore->count <= 0) {
+ return semaphore->count;
}
- return previous;
+ return semaphore->count--;
}
int32_t
OSSignalSemaphore(SemaphoreHandle handle)
{
auto semaphore = gSystem.getSystemObject(handle);
- auto lock = std::unique_lock { semaphore->mutex };
- auto previous = semaphore->count;
+ auto fiber = gProcessor.getCurrentFiber();
+ std::unique_lock lock { semaphore->mutex };
+ auto count = semaphore->count++;
- // Increase count, wake up waiting threads
- ++semaphore->count;
- semaphore->condition.notify_one();
+ // Wakeup fibers
+ for (auto fiber : semaphore->queue) {
+ gProcessor.queue(fiber);
+ }
- return previous;
+ semaphore->queue.clear();
+ return count;
}
int32_t
diff --git a/src/modules/coreinit/coreinit_semaphore.h b/src/modules/coreinit/coreinit_semaphore.h
index b8092aac..23c0814f 100644
--- a/src/modules/coreinit/coreinit_semaphore.h
+++ b/src/modules/coreinit/coreinit_semaphore.h
@@ -1,9 +1,10 @@
#pragma once
#include
-#include
#include "systemobject.h"
#include "systemtypes.h"
+struct Fiber;
+
struct Semaphore : public SystemObject
{
static const uint32_t Tag = 0x73506852;
@@ -11,10 +12,10 @@ struct Semaphore : public SystemObject
char *name;
int32_t count;
std::mutex mutex;
- std::condition_variable condition;
+ std::vector queue;
};
-using SemaphoreHandle = p32;
+using SemaphoreHandle = SystemObjectHeader *;
void
OSInitSemaphore(SemaphoreHandle handle, int32_t count);
diff --git a/src/modules/coreinit/coreinit_spinlock.cpp b/src/modules/coreinit/coreinit_spinlock.cpp
index 8a8f5aa8..7ff2ba8f 100644
--- a/src/modules/coreinit/coreinit_spinlock.cpp
+++ b/src/modules/coreinit/coreinit_spinlock.cpp
@@ -4,14 +4,14 @@
#include
void
-OSInitSpinLock(OSSpinLock * spinlock)
+OSInitSpinLock(OSSpinLock *spinlock)
{
spinlock->owner = 0;
spinlock->recursion = 0;
}
BOOL
-OSAcquireSpinLock(OSSpinLock * spinlock)
+OSAcquireSpinLock(OSSpinLock *spinlock)
{
auto owner = gMemory.untranslate(OSGetCurrentThread());
auto expected = 0u;
@@ -30,7 +30,7 @@ OSAcquireSpinLock(OSSpinLock * spinlock)
}
BOOL
-OSTryAcquireSpinLock(OSSpinLock * spinlock)
+OSTryAcquireSpinLock(OSSpinLock *spinlock)
{
auto owner = gMemory.untranslate(OSGetCurrentThread());
auto expected = 0u;
@@ -48,14 +48,14 @@ OSTryAcquireSpinLock(OSSpinLock * spinlock)
}
BOOL
-OSTryAcquireSpinLockWithTimeout(OSSpinLock * spinlock, int64_t timeout)
+OSTryAcquireSpinLockWithTimeout(OSSpinLock *spinlock, int64_t timeout)
{
gLog->error("TODO: OSTryAcquireSpinLockWithTimeout");
return FALSE;
}
BOOL
-OSReleaseSpinLock(OSSpinLock * spinlock)
+OSReleaseSpinLock(OSSpinLock *spinlock)
{
auto owner = gMemory.untranslate(OSGetCurrentThread());
@@ -73,25 +73,25 @@ OSReleaseSpinLock(OSSpinLock * spinlock)
}
BOOL
-OSUninterruptibleSpinLock_Acquire(OSSpinLock * spinlock)
+OSUninterruptibleSpinLock_Acquire(OSSpinLock *spinlock)
{
return OSAcquireSpinLock(spinlock);
}
BOOL
-OSUninterruptibleSpinLock_TryAcquire(OSSpinLock * spinlock)
+OSUninterruptibleSpinLock_TryAcquire(OSSpinLock *spinlock)
{
return OSTryAcquireSpinLock(spinlock);
}
BOOL
-OSUninterruptibleSpinLock_TryAcquireWithTimeout(OSSpinLock * spinlock, int64_t timeout)
+OSUninterruptibleSpinLock_TryAcquireWithTimeout(OSSpinLock *spinlock, int64_t timeout)
{
return OSTryAcquireSpinLockWithTimeout(spinlock, timeout);
}
BOOL
-OSUninterruptibleSpinLock_Release(OSSpinLock * spinlock)
+OSUninterruptibleSpinLock_Release(OSSpinLock *spinlock)
{
return OSReleaseSpinLock(spinlock);
}
diff --git a/src/modules/coreinit/coreinit_thread.cpp b/src/modules/coreinit/coreinit_thread.cpp
index 9539d7d2..d55442b4 100644
--- a/src/modules/coreinit/coreinit_thread.cpp
+++ b/src/modules/coreinit/coreinit_thread.cpp
@@ -2,19 +2,14 @@
#include "coreinit_thread.h"
#include "coreinit_systeminfo.h"
#include "memory.h"
-#include "thread.h"
+#include "processor.h"
+#include "trace.h"
#include "system.h"
#include
static OSThread *
gDefaultThreads[3];
-OSThread *
-OSGetCurrentThread()
-{
- return Thread::getCurrentThread()->getOSThread();
-}
-
void
OSInitThreadQueue(OSThreadQueue *queue)
{
@@ -31,10 +26,17 @@ OSInitThreadQueueEx(OSThreadQueue *queue, p32 parent)
queue->parent = parent;
}
+OSThread *
+OSGetCurrentThread()
+{
+ return gProcessor.getCurrentFiber()->thread;
+}
+
BOOL
OSSetThreadPriority(OSThread *thread, uint32_t priority)
{
thread->basePriority = priority;
+ gProcessor.reschedule();
return TRUE;
}
@@ -59,15 +61,16 @@ OSSetThreadSpecific(uint32_t id, uint32_t value)
BOOL
OSSetThreadAffinity(OSThread *thread, uint32_t affinity)
{
- thread->attr &= ~OSThreadAttributes::AffinityNone;
+ thread->attr &= ~OSThreadAttributes::AffinityAny;
thread->attr |= affinity;
+ gProcessor.reschedule();
return TRUE;
}
uint32_t
OSGetThreadAffinity(OSThread *thread)
{
- return thread->attr & OSThreadAttributes::AffinityNone;
+ return thread->attr & OSThreadAttributes::AffinityAny;
}
void
@@ -80,14 +83,41 @@ OSSleepTicks(Time ticks)
void
OSYieldThread()
{
- std::this_thread::yield();
+ gProcessor.reschedule();
}
+static uint32_t gThreadId = 1;
+
BOOL
-OSCreateThread(OSThread *osThread, ThreadEntryPoint entry, uint32_t argc, void *argv, uint8_t *stack, uint32_t stackSize, uint32_t priority, OSThreadAttributes::Flags attributes)
+OSCreateThread(OSThread *thread, ThreadEntryPoint entry, uint32_t argc, void *argv, uint8_t *stack, uint32_t stackSize, uint32_t priority, OSThreadAttributes::Flags attributes)
{
- auto thread = new Thread(osThread, entry, argc, argv, stack, stackSize, priority, attributes);
- gSystem.addThread(thread);
+ // Create new fiber
+ auto fiber = gProcessor.createFiber();
+
+ // Setup OSThread
+ thread->entryPoint = entry;
+ thread->stackEnd = gMemory.untranslate(stack);
+ thread->stackStart = thread->stackEnd - stackSize;
+ thread->basePriority = priority;
+ thread->attr = attributes;
+ thread->fiber = fiber;
+ thread->state = OSThreadState::Ready;
+ thread->id = gThreadId++;
+ thread->suspendCounter = 1;
+ fiber->thread = thread;
+
+ // Setup ThreadState
+ auto state = &fiber->state;
+ memset(state, 0, sizeof(ThreadState));
+ state->cia = thread->entryPoint;
+ state->nia = state->cia + 4;
+ state->gpr[1] = thread->stackEnd;
+ state->gpr[3] = argc;
+ state->gpr[4] = gMemory.untranslate(argv);
+
+ // Initialise tracer
+ traceInit(state, 128);
+
return TRUE;
}
@@ -131,7 +161,7 @@ OSResumeThread(OSThread *thread)
}
if (thread->suspendCounter == 0) {
- thread->thread->resume();
+ gProcessor.queue(thread->fiber);
} else {
assert(0);
}
@@ -142,19 +172,29 @@ OSResumeThread(OSThread *thread)
BOOL
OSRunThread(OSThread *thread, ThreadEntryPoint entry, uint32_t argc, p32 argv)
{
- return thread->thread->run(entry, argc, argv);
+ auto fiber = thread->fiber;
+ auto state = &fiber->state;
+ state->cia = thread->entryPoint;
+ state->nia = state->cia + 4;
+ state->gpr[1] = thread->stackEnd;
+ state->gpr[3] = argc;
+ state->gpr[4] = static_cast(argv);
+ gProcessor.queue(fiber);
+ return FALSE;
}
BOOL
OSJoinThread(OSThread *thread, be_val *exitValue)
{
- *exitValue = thread->thread->join();
+ auto fiber = thread->fiber;
+ *exitValue = gProcessor.join(fiber);
return TRUE;
}
uint32_t
OSSuspendThread(OSThread *thread)
{
+ assert(false);
auto old = thread->suspendCounter;
thread->suspendCounter++;
diff --git a/src/modules/coreinit/coreinit_thread.h b/src/modules/coreinit/coreinit_thread.h
index 66ff6a90..fc312953 100644
--- a/src/modules/coreinit/coreinit_thread.h
+++ b/src/modules/coreinit/coreinit_thread.h
@@ -6,7 +6,7 @@
#pragma pack(push, 1)
struct OSThread;
-class Thread;
+struct Fiber;
struct OSThreadQueue
{
@@ -103,10 +103,10 @@ namespace OSThreadAttributes
{
enum Flags : uint8_t
{
- AffinityNone = 7, // If affinity_none is 7 = 0b111 then lets assume its a bitmask for now
- AffinityCPU0 = 1,
- AffinityCPU1 = 2,
- AffinityCPU2 = 4,
+ AffinityCPU0 = 1 << 0,
+ AffinityCPU1 = 1 << 1,
+ AffinityCPU2 = 1 << 2,
+ AffinityAny = AffinityCPU0 | AffinityCPU1 | AffinityCPU2,
};
}
@@ -123,7 +123,7 @@ struct OSThread
be_val priority;
be_val basePriority; // "ba" in DumpActiveThreads and returned in OSGetThreadPriority
be_val exitValue; // OSExitThread / OSJoinThread
- Thread *thread; // THIS IS CUSTOM! squueeezed in, hopefully not override anything important.
+ Fiber *fiber; // THIS IS CUSTOM! squueeezed in, hopefully not override anything important.
UNKNOWN(0x394 - 0x340);
be_val stackStart; // __OSSaveUserStackPointer
be_val stackEnd; // __OSSaveUserStackPointer
diff --git a/src/ppc.h b/src/ppc.h
index b1998c2d..dba203ac 100644
--- a/src/ppc.h
+++ b/src/ppc.h
@@ -337,7 +337,7 @@ enum class SprEncoding
// TODO: Some system registers may not be thread-specific!
struct ThreadState
{
- class Thread *thread;
+ struct Fiber *fiber;
struct Tracer *tracer;
uint32_t cia; // Current execution address
diff --git a/src/processor.cpp b/src/processor.cpp
new file mode 100755
index 00000000..578d0584
--- /dev/null
+++ b/src/processor.cpp
@@ -0,0 +1,137 @@
+#include "interpreter.h"
+#include "processor.h"
+#include "ppc.h"
+#include "modules/coreinit/coreinit_thread.h"
+
+Processor
+gProcessor { 3 };
+
+__declspec(thread) Core *
+tCurrentCore = nullptr;
+
+void Fiber::fiberEntryPoint(void *param)
+{
+ gProcessor.fiberEntryPoint(reinterpret_cast(param));
+}
+
+Processor::Processor(size_t cores)
+{
+ for (auto i = 0u; i < cores; ++i) {
+ mCores.push_back(new Core { i });
+ }
+}
+
+void Processor::start()
+{
+ mRunning = true;
+
+ for (auto core : mCores) {
+ core->thread = std::thread(std::bind(&Processor::coreEntryPoint, this, core));
+ }
+}
+
+void Processor::join()
+{
+ mRunning = false;
+
+ for (auto core : mCores) {
+ core->thread.join();
+ }
+}
+
+uint32_t Processor::join(Fiber *fiber)
+{
+ std::unique_lock lock(fiber->mutex);
+
+ while (fiber->thread->state != OSThreadState::Moribund) {
+ fiber->condition.wait(lock);
+ }
+
+ // TODO: Delete fiber?
+
+ return fiber->state.gpr[3];
+}
+
+void Processor::coreEntryPoint(Core *core)
+{
+ tCurrentCore = core;
+ core->primaryFiber = ConvertThreadToFiber(NULL);
+
+ while (mRunning) {
+ std::unique_lock lock(mMutex);
+ Fiber *nextFiber = nullptr;
+
+ // Find highest priority fiber with matching affinity
+ for (auto fiber : mFiberQueue) {
+ if (fiber->thread->attr & (1 << core->id)) {
+ nextFiber = fiber;
+ break;
+ }
+ }
+
+ if (!nextFiber) {
+ // Wait for a fiber
+ mCondition.wait(lock);
+ } else {
+ // Switch to fiber
+ core->currentFiber = nextFiber;
+ core->currentFiber->parentFiber = core->primaryFiber;
+ core->currentFiber->thread->state = OSThreadState::Running;
+ lock.unlock();
+ SwitchToFiber(core->currentFiber->handle);
+ }
+ }
+}
+
+void Processor::fiberEntryPoint(Fiber *fiber)
+{
+ gInterpreter.execute(&fiber->state, fiber->state.cia);
+ fiber->thread->exitValue = fiber->state.gpr[3];
+
+ // Exit
+ SwitchToFiber(fiber->parentFiber);
+}
+
+void Processor::queue(Fiber *fiber)
+{
+ std::lock_guard lock(mMutex);
+
+ auto compare =
+ [](Fiber *lhs, Fiber *rhs) {
+ return lhs->thread->basePriority < rhs->thread->basePriority;
+ };
+
+ mFiberQueue.insert(std::upper_bound(mFiberQueue.begin(), mFiberQueue.end(), fiber, compare), fiber);
+ mCondition.notify_all();
+}
+
+void Processor::reschedule()
+{
+ auto core = tCurrentCore;
+ auto fiber = tCurrentCore->currentFiber;
+ fiber->thread->state = OSThreadState::Waiting;
+ queue(fiber);
+ SwitchToFiber(core->primaryFiber);
+}
+
+Fiber *Processor::createFiber()
+{
+ auto fiber = new Fiber();
+ mFiberList.push_back(fiber);
+ return fiber;
+}
+
+Fiber *Processor::getCurrentFiber()
+{
+ return tCurrentCore->currentFiber;
+}
+
+uint32_t Processor::getCoreID()
+{
+ return tCurrentCore->id;
+}
+
+uint32_t Processor::getCoreCount()
+{
+ return static_cast(mCores.size());
+}
diff --git a/src/processor.h b/src/processor.h
new file mode 100755
index 00000000..2e4c9acf
--- /dev/null
+++ b/src/processor.h
@@ -0,0 +1,90 @@
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+
+struct OSThread;
+struct ThreadState;
+
+struct Fiber
+{
+ Fiber()
+ {
+ handle = CreateFiber(0, &Fiber::fiberEntryPoint, this);
+ }
+
+ ~Fiber()
+ {
+ DeleteFiber(handle);
+ }
+
+ static void __stdcall fiberEntryPoint(void *param);
+
+ void *handle = nullptr;
+ void *parentFiber = nullptr;
+ OSThread *thread = nullptr;
+ ThreadState state;
+ std::mutex mutex;
+ std::condition_variable condition;
+};
+
+struct Core
+{
+ Core(uint32_t id) :
+ id(id)
+ {
+ }
+
+ uint32_t id;
+ Fiber *currentFiber = nullptr;
+ void *primaryFiber = nullptr;
+ std::thread thread;
+};
+
+class Processor
+{
+public:
+ Processor(size_t cores);
+
+ void start();
+
+ void join();
+ uint32_t join(Fiber *fiber);
+
+ void queue(Fiber *fiber);
+ void reschedule();
+
+ Fiber *createFiber();
+ Fiber *getCurrentFiber();
+
+ uint32_t getCoreID();
+ uint32_t getCoreCount();
+
+ template
+ void wait(LockType &lock)
+ {
+ lock.unlock();
+ reschedule();
+ }
+
+protected:
+ friend Core;
+ friend Fiber;
+
+ void coreEntryPoint(Core *core);
+ void fiberEntryPoint(Fiber *fiber);
+
+private:
+ std::atomic mRunning;
+ std::vector mCores;
+ std::mutex mMutex;
+ std::condition_variable mCondition;
+ std::vector mFiberQueue;
+ std::vector mFiberList;
+};
+
+extern Processor
+gProcessor;
diff --git a/src/system.cpp b/src/system.cpp
index bd2e23bd..7fa871e8 100644
--- a/src/system.cpp
+++ b/src/system.cpp
@@ -2,7 +2,6 @@
#include "instructiondata.h"
#include "system.h"
#include "kernelmodule.h"
-#include "thread.h"
#include "modules/coreinit/coreinit_memheap.h"
System gSystem;
@@ -65,20 +64,6 @@ System::findModule(const char *name) const
}
}
-void
-System::addThread(Thread *thread)
-{
- std::lock_guard lock(mThreadMutex);
- mThreads.push_back(thread);
-}
-
-void
-System::removeThread(Thread *thread)
-{
- std::lock_guard lock(mThreadMutex);
- mThreads.erase(std::remove(mThreads.begin(), mThreads.end(), thread), mThreads.end());
-}
-
KernelFunction *
System::getSyscall(uint32_t id)
{
diff --git a/src/system.h b/src/system.h
index cbfb8e1e..12bc2771 100644
--- a/src/system.h
+++ b/src/system.h
@@ -26,9 +26,6 @@ public:
void registerModule(const char *name, UserModule *module);
UserModule *getUserModule() const;
- void addThread(Thread *thread);
- void removeThread(Thread *thread);
-
KernelFunction *getSyscall(uint32_t id);
FileSystem *getFileSystem();
@@ -79,9 +76,6 @@ private:
p32 mSystemThunks;
std::vector mSystemCalls;
- std::mutex mThreadMutex;
- std::vector mThreads;
-
std::mutex mSystemObjectMutex;
std::map mSystemObjects;
diff --git a/src/thread.cpp b/src/thread.cpp
deleted file mode 100644
index 5d2d6f57..00000000
--- a/src/thread.cpp
+++ /dev/null
@@ -1,170 +0,0 @@
-#include
-#include "thread.h"
-#include "interpreter.h"
-#include "system.h"
-#include "trace.h"
-#include "modules/coreinit/coreinit_memheap.h"
-
-__declspec(thread) Thread *tCurrentThread = nullptr;
-static std::atomic gThreadId = 1;
-
-Thread::Thread(OSThread *thread, ThreadEntryPoint entry,
- uint32_t argc, void *argv,
- uint8_t *stack, uint32_t stackSize,
- uint32_t priority, OSThreadAttributes::Flags attributes) :
- mOSThread(thread)
-{
- // Setup stack
- mStackSize = stackSize;
- mStackBase = gMemory.untranslate(stack) - stackSize;
-
- // Setup thread state
- mState = reinterpret_cast(&mAlignedState);
- memset(mState, 0, sizeof(ThreadState));
- mState->thread = this;
- mState->cia = entry;
- mState->nia = mState->cia + 4;
-
- // Setup registers
- mState->gpr[1] = mStackBase + mStackSize;
- mState->gpr[3] = argc;
- mState->gpr[4] = gMemory.untranslate(argv);
-
- // Init tracer
- traceInit(mState, 128);
-
- // Fill out data of OSThread
- mOSThread->entryPoint = entry;
- mOSThread->stackStart = mStackBase;
- mOSThread->stackEnd = mStackBase + mStackSize;
- mOSThread->basePriority = priority;
- mOSThread->attr = attributes;
- mOSThread->thread = this;
- mOSThread->state = OSThreadState::Ready;
- mOSThread->id = gThreadId++;
- mOSThread->suspendCounter = 1;
-
- // Set CoreID
- if (attributes & OSThreadAttributes::AffinityCPU0) {
- mCoreID = 0;
- }
-
- if (attributes & OSThreadAttributes::AffinityCPU2) {
- mCoreID = 2;
- }
-
- if (attributes & OSThreadAttributes::AffinityCPU1) {
- mCoreID = 1;
- }
-}
-
-Thread::~Thread()
-{
- OSFreeToSystem(make_p32(mStackBase));
-}
-
-Thread *
-Thread::getCurrentThread()
-{
- return tCurrentThread;
-}
-
-uint32_t Thread::getCoreID() const
-{
- return mCoreID;
-}
-
-ThreadState *
-Thread::getThreadState() const
-{
- return mState;
-}
-
-OSThread *
-Thread::getOSThread() const
-{
- return mOSThread;
-}
-
-bool
-Thread::run(ThreadEntryPoint entry, uint32_t argc, p32 argv)
-{
- if (mOSThread->state != OSThreadState::Ready) {
- return false;
- }
-
- mState->cia = entry;
- mState->nia = mState->cia + 4;
- mState->gpr[3] = argc;
- mState->gpr[4] = static_cast(argv);
- mOSThread->suspendCounter = 0;
- resume();
- return true;
-}
-
-void
-Thread::resume()
-{
- assert(mOSThread->suspendCounter == 0);
- assert(mOSThread->state == OSThreadState::Ready);
- mOSThread->state = OSThreadState::Running;
- mThread = std::thread(std::bind(&Thread::entry, this));
-}
-
-uint32_t
-Thread::join()
-{
- mThread.join();
- return mOSThread->exitValue;
-}
-
-LONG exceptionFilter(_EXCEPTION_POINTERS *info)
-{
- printf("\n");
- printf("[EXCEPTION] %X\n", info->ExceptionRecord->ExceptionCode);
-
- for (auto i = 0u; i < info->ExceptionRecord->NumberParameters; ++i) {
- printf("info[%u] = %08X\n", i, info->ExceptionRecord->ExceptionInformation[i]);
- }
-
- return (info->ExceptionRecord->ExceptionCode == EXCEPTION_ACCESS_VIOLATION ? EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH);
-}
-
-void
-Thread::entry()
-{
- __try {
- tCurrentThread = this;
- mOSThread->exitValue = execute(mState->cia, 0, nullptr);
- }
- __except (exceptionFilter(GetExceptionInformation())) {
- auto state = tCurrentThread->getThreadState();
-
- printf("\nThread State:\n");
- printf("CIA: %08X\n", state->cia);
- printf("NIA: %08X\n", state->nia);
- printf("LR: %08X\n", state->lr);
-
- for (auto i = 0u; i < 32; ++i) {
- printf("gpr[% 2u]: %08X\n", i, state->gpr[i]);
- }
-
- printf("\nBacktrace:\n");
- tracePrint(state, 32);
- printf("\n");
-
- system("PAUSE");
- exit(-1);
- }
-}
-
-uint32_t
-Thread::execute(uint32_t address, size_t argc, uint32_t args[])
-{
- for (auto i = 0u; i < argc; ++i) {
- mState->gpr[3 + i] = args[i];
- }
-
- gInterpreter.execute(mState, mState->cia);
- return mState->gpr[3];
-}
diff --git a/src/thread.h b/src/thread.h
deleted file mode 100644
index 0728d74a..00000000
--- a/src/thread.h
+++ /dev/null
@@ -1,45 +0,0 @@
-#pragma once
-#include
-#include
-#include "ppc.h"
-#include "modules/coreinit/coreinit_thread.h"
-
-class Interpreter;
-class System;
-struct ThreadState;
-
-class Thread
-{
-public:
- Thread(OSThread *thread, ThreadEntryPoint entry,
- uint32_t argc, void *argv,
- uint8_t *stack, uint32_t stackSize,
- uint32_t priority, OSThreadAttributes::Flags attributes);
- ~Thread();
-
- uint32_t getCoreID() const;
- ThreadState *getThreadState() const;
- OSThread *getOSThread() const;
-
- uint32_t join();
- void resume();
- bool run(ThreadEntryPoint entry, uint32_t argc, p32 argv);
- uint32_t execute(uint32_t address, size_t argc, uint32_t args[]);
-
-private:
- void entry();
-
-public:
- static Thread *getCurrentThread();
-
-private:
- uint32_t mCoreID;
- std::string mName;
- uint32_t mStackBase;
- uint32_t mStackSize;
- OSThread *mOSThread;
-
- std::aligned_storage::type mAlignedState;
- ThreadState *mState;
- std::thread mThread;
-};
diff --git a/src/trace.cpp b/src/trace.cpp
index 1ff408df..cd4fcede 100755
--- a/src/trace.cpp
+++ b/src/trace.cpp
@@ -1,6 +1,7 @@
#include "disassembler.h"
#include "instruction.h"
#include "instructiondata.h"
+#include "log.h"
#include "memory.h"
#include "ppc.h"
#include "trace.h"
@@ -42,16 +43,16 @@ printFieldValue(Instruction instr, Field field, uint32_t value)
{
switch (field) {
case Field::rA:
- printf(" r%u = %08X\n", instr.rA, value);
+ gLog->emerg(" r{:02} = {:08x}", instr.rA, value);
break;
case Field::rB:
- printf(" r%u = %08X\n", instr.rB, value);
+ gLog->emerg(" r{:02} = {:08x}", instr.rB, value);
break;
case Field::rS:
- printf(" r%u = %08X\n", instr.rS, value);
+ gLog->emerg(" r{:02} = {:08x}", instr.rS, value);
break;
case Field::rD:
- printf(" r%u = %08X\n", instr.rD, value);
+ gLog->emerg(" r{:02} = {:08x}", instr.rD, value);
break;
}
}
@@ -123,7 +124,7 @@ tracePrint(ThreadState *state, size_t count)
printFieldValue(trace.instr, trace.data->write[j], trace.write[j]);
}
- printf(" %08X %s\n", trace.cia, dis.text.c_str());
+ gLog->emerg(" {:08x} {}", trace.cia, dis.text.c_str());
for (auto j = 0u; j < trace.data->read.size(); ++j) {
printFieldValue(trace.instr, trace.data->read[j], trace.read[j]);