From ecff2351a180e76967de7322ceb2d8e9064ae7a6 Mon Sep 17 00:00:00 2001
From: Yuri Kunde Schlesner <yuriks@yuriks.net>
Date: Wed, 6 May 2015 00:04:25 -0300
Subject: [PATCH] HLE: Clean up SVC dispatch mechanism

---
 .../arm/dyncom/arm_dyncom_interpreter.cpp     |  4 +-
 src/core/hle/hle.cpp                          | 49 ++-----------------
 src/core/hle/hle.h                            | 27 ----------
 src/core/hle/svc.cpp                          | 37 ++++++++++++--
 src/core/hle/svc.h                            |  2 +-
 5 files changed, 40 insertions(+), 79 deletions(-)

diff --git a/src/core/arm/dyncom/arm_dyncom_interpreter.cpp b/src/core/arm/dyncom/arm_dyncom_interpreter.cpp
index 991da740b..5ee99e93a 100644
--- a/src/core/arm/dyncom/arm_dyncom_interpreter.cpp
+++ b/src/core/arm/dyncom/arm_dyncom_interpreter.cpp
@@ -11,7 +11,7 @@
 #include "common/profiler.h"
 
 #include "core/mem_map.h"
-#include "core/hle/hle.h"
+#include "core/hle/svc.h"
 #include "core/arm/disassembler/arm_disasm.h"
 #include "core/arm/dyncom/arm_dyncom_interpreter.h"
 #include "core/arm/dyncom/arm_dyncom_thumb.h"
@@ -6234,7 +6234,7 @@ unsigned InterpreterMainLoop(ARMul_State* state) {
     SWI_INST:
     {
         if (inst_base->cond == 0xE || CondPassed(cpu, inst_base->cond)) {
-            HLE::CallSVC(Memory::Read32(cpu->Reg[15]));
+            SVC::CallSVC(Memory::Read32(cpu->Reg[15]));
         }
 
         cpu->Reg[15] += GET_INST_SIZE(cpu);
diff --git a/src/core/hle/hle.cpp b/src/core/hle/hle.cpp
index 191d0411e..fdeb9a028 100644
--- a/src/core/hle/hle.cpp
+++ b/src/core/hle/hle.cpp
@@ -2,53 +2,23 @@
 // Licensed under GPLv2 or any later version
 // Refer to the license.txt file included.
 
-#include <vector>
-
-#include "common/profiler.h"
+#include "common/assert.h"
+#include "common/logging/log.h"
 
 #include "core/arm/arm_interface.h"
-#include "core/mem_map.h"
+#include "core/core.h"
 #include "core/hle/hle.h"
 #include "core/hle/config_mem.h"
 #include "core/hle/shared_page.h"
 #include "core/hle/kernel/thread.h"
 #include "core/hle/service/service.h"
-#include "core/hle/svc.h"
 
 ////////////////////////////////////////////////////////////////////////////////////////////////////
 
 namespace HLE {
 
-Common::Profiling::TimingCategory profiler_svc("SVC Calls");
-
-static std::vector<ModuleDef> g_module_db;
-
 bool g_reschedule; ///< If true, immediately reschedules the CPU to a new thread
 
-static const FunctionDef* GetSVCInfo(u32 opcode) {
-    u32 func_num = opcode & 0xFFFFFF; // 8 bits
-    if (func_num > 0xFF) {
-        LOG_ERROR(Kernel_SVC,"unknown svc=0x%02X", func_num);
-        return nullptr;
-    }
-    return &g_module_db[0].func_table[func_num];
-}
-
-void CallSVC(u32 opcode) {
-    Common::Profiling::ScopeTimer timer_svc(profiler_svc);
-
-    const FunctionDef *info = GetSVCInfo(opcode);
-
-    if (!info) {
-        return;
-    }
-    if (info->func) {
-        info->func();
-    } else {
-        LOG_ERROR(Kernel_SVC, "unimplemented SVC function %s(..)", info->name.c_str());
-    }
-}
-
 void Reschedule(const char *reason) {
     DEBUG_ASSERT_MSG(reason != nullptr && strlen(reason) < 256, "Reschedule: Invalid or too long reason.");
 
@@ -63,18 +33,7 @@ void Reschedule(const char *reason) {
     g_reschedule = true;
 }
 
-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);
-}
-
-static void RegisterAllModules() {
-    SVC::Register();
-}
-
 void Init() {
-    RegisterAllModules();
-
     Service::Init();
     ConfigMem::Init();
     SharedPage::Init();
@@ -89,8 +48,6 @@ void Shutdown() {
     SharedPage::Shutdown();
     Service::Shutdown();
 
-    g_module_db.clear();
-
     LOG_DEBUG(Kernel, "shutdown OK");
 }
 
diff --git a/src/core/hle/hle.h b/src/core/hle/hle.h
index 3f6f9a4b5..23de1aab7 100644
--- a/src/core/hle/hle.h
+++ b/src/core/hle/hle.h
@@ -4,40 +4,13 @@
 
 #pragma once
 
-#include <string>
-
-#include "common/common_types.h"
-#include "core/core.h"
-
-////////////////////////////////////////////////////////////////////////////////////////////////////
-
 namespace HLE {
 
 extern bool g_reschedule;   ///< If true, immediately reschedules the CPU to a new thread
 
-typedef u32 Addr;
-typedef void (*Func)();
-
-struct FunctionDef {
-    u32                 id;
-    Func                func;
-    std::string         name;
-};
-
-struct ModuleDef {
-    std::string         name;
-    int                 num_funcs;
-    const FunctionDef*  func_table;
-};
-
-void RegisterModule(std::string name, int num_functions, const FunctionDef *func_table);
-
-void CallSVC(u32 opcode);
-
 void Reschedule(const char *reason);
 
 void Init();
-
 void Shutdown();
 
 } // namespace
diff --git a/src/core/hle/svc.cpp b/src/core/hle/svc.cpp
index 76e9b171a..2da488d83 100644
--- a/src/core/hle/svc.cpp
+++ b/src/core/hle/svc.cpp
@@ -4,6 +4,7 @@
 
 #include <map>
 
+#include "common/profiler.h"
 #include "common/string_util.h"
 #include "common/symbols.h"
 
@@ -606,7 +607,17 @@ static ResultCode CreateMemoryBlock(Handle* out_handle, u32 addr, u32 size, u32
     return RESULT_SUCCESS;
 }
 
-const HLE::FunctionDef SVC_Table[] = {
+namespace {
+    struct FunctionDef {
+        using Func = void();
+
+        u32         id;
+        Func*       func;
+        const char* name;
+    };
+}
+
+static const FunctionDef SVC_Table[] = {
     {0x00, nullptr,                         "Unknown"},
     {0x01, HLE::Wrap<ControlMemory>,        "ControlMemory"},
     {0x02, HLE::Wrap<QueryMemory>,          "QueryMemory"},
@@ -735,8 +746,28 @@ const HLE::FunctionDef SVC_Table[] = {
     {0x7D, nullptr,                         "QueryProcessMemory"},
 };
 
-void Register() {
-    HLE::RegisterModule("SVC_Table", ARRAY_SIZE(SVC_Table), SVC_Table);
+Common::Profiling::TimingCategory profiler_svc("SVC Calls");
+
+static const FunctionDef* GetSVCInfo(u32 opcode) {
+    u32 func_num = opcode & 0xFFFFFF; // 8 bits
+    if (func_num >= ARRAY_SIZE(SVC_Table)) {
+        LOG_ERROR(Kernel_SVC, "unknown svc=0x%02X", func_num);
+        return nullptr;
+    }
+    return &SVC_Table[func_num];
+}
+
+void CallSVC(u32 opcode) {
+    Common::Profiling::ScopeTimer timer_svc(profiler_svc);
+
+    const FunctionDef *info = GetSVCInfo(opcode);
+    if (info) {
+        if (info->func) {
+            info->func();
+        } else {
+            LOG_ERROR(Kernel_SVC, "unimplemented SVC function %s(..)", info->name);
+        }
+    }
 }
 
 } // namespace
diff --git a/src/core/hle/svc.h b/src/core/hle/svc.h
index 5d020a5ba..4389aa73d 100644
--- a/src/core/hle/svc.h
+++ b/src/core/hle/svc.h
@@ -41,6 +41,6 @@ enum ArbitrationType {
 
 namespace SVC {
 
-void Register();
+void CallSVC(u32 opcode);
 
 } // namespace