From 2bde8f28561ea9436d13d990f6b129a0e80a325e Mon Sep 17 00:00:00 2001
From: bunnei <ericbunnie@gmail.com>
Date: Thu, 10 Apr 2014 23:26:12 -0400
Subject: [PATCH] base code to call a syscall from ARM11 appcore

---
 src/core/arm/interpreter/armemu.cpp |  3 ++
 src/core/hle.cpp                    | 28 +++++++++++++--
 src/core/hle/hle.h                  |  6 ++--
 src/core/hle/hle_syscall.cpp        | 55 ++++++++++++++++++++++++++---
 src/core/hle/hle_syscall.h          |  2 +-
 5 files changed, 85 insertions(+), 9 deletions(-)

diff --git a/src/core/arm/interpreter/armemu.cpp b/src/core/arm/interpreter/armemu.cpp
index 46c51fbe8..6074ff480 100644
--- a/src/core/arm/interpreter/armemu.cpp
+++ b/src/core/arm/interpreter/armemu.cpp
@@ -16,6 +16,8 @@
     along with this program; if not, write to the Free Software
     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
 
+#include "core/hle/hle.h"
+
 #include "arm_regformat.h"
 #include "armdefs.h"
 #include "armemu.h"
@@ -4558,6 +4560,7 @@ ARMul_Emulate26 (ARMul_State * state)
                 //    ARMul_OSHandleSWI (state, BITS (0, 23));
                 //    break;
                 //}
+                HLE::CallSyscall(instr);
                 ARMul_Abort (state, ARMul_SWIV);
                 break;
             }
diff --git a/src/core/hle.cpp b/src/core/hle.cpp
index 8dad7695b..d62d2d0ce 100644
--- a/src/core/hle.cpp
+++ b/src/core/hle.cpp
@@ -13,21 +13,45 @@ namespace HLE {
 
 static std::vector<ModuleDef> g_module_db;
 
+const FunctionDef* GetSyscallInfo(u32 opcode) {
+    u32 func_num = opcode & 0xFFFFFF; // 8 bits
+    if (func_num > 0xFF) {
+        ERROR_LOG(HLE,"Unknown syscall: 0x%02X", func_num); 
+        return NULL;
+    }
+    return &g_module_db[0].func_table[func_num];
+}
+
+void CallSyscall(u32 opcode) {
+    const FunctionDef *info = GetSyscallInfo(opcode);
+
+    if (!info) {
+        return;
+    }
+    if (info->func) {
+        info->func();
+    } else {
+        ERROR_LOG(HLE, "Unimplemented HLE function %s", info->name);
+    }
+}
+
 void RegisterModule(std::string name, int num_functions, const FunctionDef* func_table) {
     ModuleDef module = {name, num_functions, func_table};
     g_module_db.push_back(module);
 }
 
 void RegisterAllModules() {
-    Register_SysCall();
+    Register_Syscall();
 }
 
 void Init() {
     RegisterAllModules();
+    NOTICE_LOG(HLE, "initialized OK");
 }
 
 void Shutdown() {
-	g_module_db.clear();
+    g_module_db.clear();
+    NOTICE_LOG(HLE, "shutdown OK");
 }
 
 } // namespace
diff --git a/src/core/hle/hle.h b/src/core/hle/hle.h
index 35c8a4621..e3b8d483a 100644
--- a/src/core/hle/hle.h
+++ b/src/core/hle/hle.h
@@ -30,10 +30,12 @@ struct ModuleDef {
 	const FunctionDef*  func_table;
 };
 
+void RegisterModule(std::string name, int num_functions, const FunctionDef *func_table);
+
+void CallSyscall(u32 opcode);
+
 void Init();
 
 void Shutdown();
 
-void RegisterModule(std::string name, int num_functions, const FunctionDef *func_table);
-
 } // namespace
diff --git a/src/core/hle/hle_syscall.cpp b/src/core/hle/hle_syscall.cpp
index fdcaa914f..53d721275 100644
--- a/src/core/hle/hle_syscall.cpp
+++ b/src/core/hle/hle_syscall.cpp
@@ -10,15 +10,62 @@
 typedef u32 Handle;
 typedef s32 Result;
 
+////////////////////////////////////////////////////////////////////////////////////////////////////
+
 Result SVC_ConnectToPort(void* out, const char* port_name) {
     NOTICE_LOG(OSHLE, "svcConnectToPort called, port_name: %s", port_name);
     return 0;
 }
 
-const HLE::FunctionDef SysCall_Table[] = {
-    {0x2D, WrapI_VC<SVC_ConnectToPort>, "svcConnectToPort"},
+const HLE::FunctionDef Syscall_Table[] = {
+    {0x00,  NULL,                           "Unknown"},
+    {0x01,  NULL,                           "svcControlMemory"},
+    {0x02,  NULL,                           "svcQueryMemory"},
+    {0x03,  NULL,                           "svcExitProcess"},
+    {0x04,  NULL,                           "svcGetProcessAffinityMask"},
+    {0x05,  NULL,                           "svcSetProcessAffinityMask"},
+    {0x06,  NULL,                           "svcGetProcessIdealProcessor"},
+    {0x07,  NULL,                           "svcSetProcessIdealProcessor"},
+    {0x08,  NULL,                           "svcCreateThread"},
+    {0x09,  NULL,                           "svcExitThread"},
+    {0x0A,  NULL,                           "svcSleepThread"},
+    {0x0B,  NULL,                           "svcGetThreadPriority"},
+    {0x0C,  NULL,                           "svcSetThreadPriority"},
+    {0x0D,  NULL,                           "svcGetThreadAffinityMask"},
+    {0x0E,  NULL,                           "svcSetThreadAffinityMask"},
+    {0x0F,  NULL,                           "svcGetThreadIdealProcessor"},
+    {0x10,  NULL,                           "svcSetThreadIdealProcessor"},
+    {0x11,  NULL,                           "svcGetCurrentProcessorNumber"},
+    {0x12,  NULL,                           "svcRun"},
+    {0x13,  NULL,                           "svcCreateMutex"},
+    {0x14,  NULL,                           "svcReleaseMutex"},
+    {0x15,  NULL,                           "svcCreateSemaphore"},
+    {0x16,  NULL,                           "svcReleaseSemaphore"},
+    {0x17,  NULL,                           "svcCreateEvent"},
+    {0x18,  NULL,                           "svcSignalEvent"},
+    {0x19,  NULL,                           "svcClearEvent"},
+    {0x1A,  NULL,                           "svcCreateTimer"},
+    {0x1B,  NULL,                           "svcSetTimer"},
+    {0x1C,  NULL,                           "svcCancelTimer"},
+    {0x1D,  NULL,                           "svcClearTimer"},
+    {0x1E,  NULL,                           "svcCreateMemoryBlock"},
+    {0x1F,  NULL,                           "svcMapMemoryBlock"},
+    {0x20,  NULL,                           "svcUnmapMemoryBlock"},
+    {0x21,  NULL,                           "svcCreateAddressArbiter"},
+    {0x22,  NULL,                           "svcArbitrateAddress"},
+    {0x23,  NULL,                           "svcCloseHandle"},
+    {0x24,  NULL,                           "svcWaitSynchronization1"},
+    {0x25,  NULL,                           "svcWaitSynchronizationN"},
+    {0x26,  NULL,                           "svcSignalAndWait"},
+    {0x27,  NULL,                           "svcDuplicateHandle"},
+    {0x28,  NULL,                           "svcGetSystemTick"},
+    {0x29,  NULL,                           "svcGetHandleInfo"},
+    {0x2A,  NULL,                           "svcGetSystemInfo"},
+    {0x2B,  NULL,                           "svcGetProcessInfo"},
+    {0x2C,  NULL,                           "svcGetThreadInfo"},
+    {0x2D,  WrapI_VC<SVC_ConnectToPort>,    "svcConnectToPort"},
 };
 
-void Register_SysCall() {
-    HLE::RegisterModule("SysCallTable", ARRAY_SIZE(SysCall_Table), SysCall_Table);
+void Register_Syscall() {
+    HLE::RegisterModule("SyscallTable", ARRAY_SIZE(Syscall_Table), Syscall_Table);
 }
diff --git a/src/core/hle/hle_syscall.h b/src/core/hle/hle_syscall.h
index 4faa14535..80b20c358 100644
--- a/src/core/hle/hle_syscall.h
+++ b/src/core/hle/hle_syscall.h
@@ -34,4 +34,4 @@
 //    }
 //};
 
-void Register_SysCall();
+void Register_Syscall();