2013-12-07 06:57:16 +00:00
|
|
|
/**
|
|
|
|
******************************************************************************
|
|
|
|
* Xenia : Xbox 360 Emulator Research Project *
|
|
|
|
******************************************************************************
|
|
|
|
* Copyright 2013 Ben Vanik. All rights reserved. *
|
|
|
|
* Released under the BSD license - see LICENSE in the root for more details. *
|
|
|
|
******************************************************************************
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <alloy/alloy.h>
|
2014-07-14 04:15:37 +00:00
|
|
|
#include <alloy/backend/ivm/ivm_backend.h>
|
|
|
|
#include <alloy/backend/x64/x64_backend.h>
|
2014-07-30 05:44:56 +00:00
|
|
|
#include <alloy/frontend/ppc/ppc_context.h>
|
|
|
|
#include <alloy/frontend/ppc/ppc_frontend.h>
|
2013-12-07 06:57:16 +00:00
|
|
|
#include <alloy/runtime/raw_module.h>
|
2014-08-16 23:34:04 +00:00
|
|
|
#include <poly/main.h>
|
2014-07-30 03:29:50 +00:00
|
|
|
#include <poly/poly.h>
|
2013-12-07 06:57:16 +00:00
|
|
|
|
|
|
|
#include <gflags/gflags.h>
|
|
|
|
|
2014-07-30 05:44:56 +00:00
|
|
|
namespace alloy {
|
|
|
|
namespace sandbox {
|
2013-12-07 06:57:16 +00:00
|
|
|
|
2014-07-30 05:44:56 +00:00
|
|
|
using alloy::frontend::ppc::PPCContext;
|
|
|
|
using alloy::runtime::Runtime;
|
|
|
|
|
|
|
|
class ThreadState : public alloy::runtime::ThreadState {
|
|
|
|
public:
|
2014-08-22 04:36:34 +00:00
|
|
|
ThreadState(Runtime* runtime, uint32_t thread_id, uint64_t stack_address,
|
|
|
|
size_t stack_size, uint64_t thread_state_address)
|
2014-07-30 05:44:56 +00:00
|
|
|
: alloy::runtime::ThreadState(runtime, thread_id),
|
2014-08-22 04:36:34 +00:00
|
|
|
stack_address_(stack_address),
|
2014-07-30 05:44:56 +00:00
|
|
|
stack_size_(stack_size),
|
|
|
|
thread_state_address_(thread_state_address) {
|
2014-08-20 04:02:15 +00:00
|
|
|
memset(memory_->Translate(stack_address_), 0, stack_size_);
|
2014-07-30 05:44:56 +00:00
|
|
|
|
|
|
|
// Allocate with 64b alignment.
|
2014-08-21 05:22:47 +00:00
|
|
|
context_ = (PPCContext*)calloc(1, sizeof(PPCContext));
|
2014-07-30 05:44:56 +00:00
|
|
|
assert_true((reinterpret_cast<uint64_t>(context_) & 0xF) == 0);
|
|
|
|
|
|
|
|
// Stash pointers to common structures that callbacks may need.
|
|
|
|
context_->reserve_address = memory_->reserve_address();
|
|
|
|
context_->membase = memory_->membase();
|
|
|
|
context_->runtime = runtime;
|
|
|
|
context_->thread_state = this;
|
|
|
|
|
|
|
|
// Set initial registers.
|
|
|
|
context_->r[1] = stack_address_ + stack_size;
|
|
|
|
context_->r[13] = thread_state_address_;
|
|
|
|
|
|
|
|
// Pad out stack a bit, as some games seem to overwrite the caller by about
|
|
|
|
// 16 to 32b.
|
|
|
|
context_->r[1] -= 64;
|
|
|
|
|
|
|
|
raw_context_ = context_;
|
|
|
|
|
|
|
|
runtime_->debugger()->OnThreadCreated(this);
|
|
|
|
}
|
|
|
|
~ThreadState() override {
|
|
|
|
runtime_->debugger()->OnThreadDestroyed(this);
|
2014-08-21 05:22:47 +00:00
|
|
|
free(context_);
|
2014-07-30 05:44:56 +00:00
|
|
|
}
|
2014-05-28 05:54:40 +00:00
|
|
|
|
2014-07-30 05:44:56 +00:00
|
|
|
PPCContext* context() const { return context_; }
|
2013-12-07 06:57:16 +00:00
|
|
|
|
2014-07-30 05:44:56 +00:00
|
|
|
private:
|
|
|
|
uint64_t stack_address_;
|
|
|
|
size_t stack_size_;
|
|
|
|
uint64_t thread_state_address_;
|
2013-12-07 06:57:16 +00:00
|
|
|
|
2014-07-30 05:44:56 +00:00
|
|
|
// NOTE: must be 64b aligned for SSE ops.
|
|
|
|
PPCContext* context_;
|
|
|
|
};
|
2013-12-07 06:57:16 +00:00
|
|
|
|
2014-07-30 05:44:56 +00:00
|
|
|
// TODO(benvanik): simple memory? move more into core?
|
|
|
|
|
2014-08-16 23:34:04 +00:00
|
|
|
int main(std::vector<std::wstring>& args) {
|
2014-08-21 05:37:01 +00:00
|
|
|
#if XE_OPTION_PROFILING
|
2014-07-30 05:44:56 +00:00
|
|
|
xe::Profiler::Initialize();
|
|
|
|
xe::Profiler::ThreadEnter("main");
|
2014-08-21 05:37:01 +00:00
|
|
|
#endif // XE_OPTION_PROFILING
|
2014-07-30 05:44:56 +00:00
|
|
|
|
2014-08-20 04:02:15 +00:00
|
|
|
size_t memory_size = 16 * 1024 * 1024;
|
|
|
|
auto memory = std::make_unique<SimpleMemory>(memory_size);
|
2014-07-30 05:44:56 +00:00
|
|
|
auto runtime = std::make_unique<Runtime>(memory.get());
|
|
|
|
|
|
|
|
auto frontend =
|
|
|
|
std::make_unique<alloy::frontend::ppc::PPCFrontend>(runtime.get());
|
|
|
|
std::unique_ptr<alloy::backend::Backend> backend;
|
2014-08-22 04:36:34 +00:00
|
|
|
// backend =
|
|
|
|
// std::make_unique<alloy::backend::ivm::IVMBackend>(runtime.get());
|
|
|
|
// backend =
|
|
|
|
// std::make_unique<alloy::backend::x64::X64Backend>(runtime.get());
|
2014-07-30 05:44:56 +00:00
|
|
|
runtime->Initialize(std::move(frontend), std::move(backend));
|
|
|
|
|
|
|
|
auto module = std::make_unique<alloy::runtime::RawModule>(runtime.get());
|
2014-08-22 04:23:35 +00:00
|
|
|
module->LoadFile(0x00001000, "test\\codegen\\instr_add.bin");
|
2014-07-14 04:53:31 +00:00
|
|
|
runtime->AddModule(std::move(module));
|
2013-12-07 06:57:16 +00:00
|
|
|
|
2014-07-30 05:44:56 +00:00
|
|
|
{
|
2014-08-20 04:02:15 +00:00
|
|
|
uint64_t stack_size = 64 * 1024;
|
2014-08-22 04:36:34 +00:00
|
|
|
uint64_t stack_address = memory_size - stack_size;
|
|
|
|
uint64_t thread_state_address = stack_address - 0x1000;
|
2014-08-20 04:02:15 +00:00
|
|
|
auto thread_state = std::make_unique<ThreadState>(
|
|
|
|
runtime.get(), 100, stack_address, stack_size, thread_state_address);
|
2013-12-07 06:57:16 +00:00
|
|
|
|
2014-07-30 05:44:56 +00:00
|
|
|
alloy::runtime::Function* fn;
|
2014-08-20 04:02:15 +00:00
|
|
|
runtime->ResolveFunction(0x1000, &fn);
|
2014-07-30 05:44:56 +00:00
|
|
|
auto ctx = thread_state->context();
|
|
|
|
ctx->lr = 0xBEBEBEBE;
|
|
|
|
ctx->r[5] = 10;
|
|
|
|
ctx->r[25] = 25;
|
|
|
|
fn->Call(thread_state.get(), ctx->lr);
|
|
|
|
auto result = ctx->r[11];
|
|
|
|
printf("%llu", result);
|
|
|
|
}
|
2013-12-07 06:57:16 +00:00
|
|
|
|
2014-07-30 05:44:56 +00:00
|
|
|
runtime.reset();
|
|
|
|
memory.reset();
|
2013-12-07 06:57:16 +00:00
|
|
|
|
2014-08-21 05:37:01 +00:00
|
|
|
#if XE_OPTION_PROFILING
|
2014-05-28 05:54:40 +00:00
|
|
|
xe::Profiler::Dump();
|
|
|
|
xe::Profiler::ThreadExit();
|
2014-08-21 05:37:01 +00:00
|
|
|
#endif // XE_OPTION_PROFILING
|
2014-05-28 05:54:40 +00:00
|
|
|
|
2013-12-25 00:23:53 +00:00
|
|
|
return 0;
|
2013-12-07 06:57:16 +00:00
|
|
|
}
|
2014-07-30 05:44:56 +00:00
|
|
|
|
|
|
|
} // namespace sandbox
|
|
|
|
} // namespace alloy
|
|
|
|
|
2014-08-16 23:34:04 +00:00
|
|
|
DEFINE_ENTRY_POINT(L"alloy-sandbox", L"?", alloy::sandbox::main);
|