From 69223d007ef70cbcbe4b0db47c00b2f4837f5d23 Mon Sep 17 00:00:00 2001
From: bunnei <ericbunnie@gmail.com>
Date: Fri, 4 Apr 2014 22:26:06 -0400
Subject: [PATCH] - added an interface layer for ARM cores

- cleaned up core.cpp a bit
---
 src/core/core.vcxproj                         |  3 +
 src/core/core.vcxproj.filters                 |  9 ++
 src/core/src/arm/arm_interface.h              | 47 ++++++++++
 .../src/arm/interpreter/arm_interpreter.cpp   | 86 ++++++++++++++++++
 .../src/arm/interpreter/arm_interpreter.h     | 50 +++++++++++
 src/core/src/core.cpp                         | 87 +++----------------
 src/core/src/core.h                           |  8 +-
 7 files changed, 213 insertions(+), 77 deletions(-)
 create mode 100644 src/core/src/arm/arm_interface.h
 create mode 100644 src/core/src/arm/interpreter/arm_interpreter.cpp
 create mode 100644 src/core/src/arm/interpreter/arm_interpreter.h

diff --git a/src/core/core.vcxproj b/src/core/core.vcxproj
index 4cd55c5758..bee9f30467 100644
--- a/src/core/core.vcxproj
+++ b/src/core/core.vcxproj
@@ -144,6 +144,7 @@
     <ClCompile Include="src\arm\interpreter\armos.cpp" />
     <ClCompile Include="src\arm\interpreter\armsupp.cpp" />
     <ClCompile Include="src\arm\interpreter\armvirt.cpp" />
+    <ClCompile Include="src\arm\interpreter\arm_interpreter.cpp" />
     <ClCompile Include="src\arm\interpreter\thumbemu.cpp" />
     <ClCompile Include="src\arm\mmu\arm1176jzf_s_mmu.cpp" />
     <ClCompile Include="src\core.cpp" />
@@ -157,12 +158,14 @@
     <ClCompile Include="src\system.cpp" />
   </ItemGroup>
   <ItemGroup>
+    <ClInclude Include="src\arm\arm_interface.h" />
     <ClInclude Include="src\arm\disassembler\arm_disasm.h" />
     <ClInclude Include="src\arm\interpreter\armcpu.h" />
     <ClInclude Include="src\arm\interpreter\armdefs.h" />
     <ClInclude Include="src\arm\interpreter\armemu.h" />
     <ClInclude Include="src\arm\interpreter\armmmu.h" />
     <ClInclude Include="src\arm\interpreter\armos.h" />
+    <ClInclude Include="src\arm\interpreter\arm_interpreter.h" />
     <ClInclude Include="src\arm\interpreter\arm_regformat.h" />
     <ClInclude Include="src\arm\interpreter\skyeye_defs.h" />
     <ClInclude Include="src\arm\mmu\arm1176jzf_s_mmu.h" />
diff --git a/src/core/core.vcxproj.filters b/src/core/core.vcxproj.filters
index fe583127ab..45ddf8cf64 100644
--- a/src/core/core.vcxproj.filters
+++ b/src/core/core.vcxproj.filters
@@ -43,6 +43,9 @@
     <ClCompile Include="src\arm\interpreter\thumbemu.cpp">
       <Filter>arm\interpreter</Filter>
     </ClCompile>
+    <ClCompile Include="src\arm\interpreter\arm_interpreter.cpp">
+      <Filter>arm\interpreter</Filter>
+    </ClCompile>
   </ItemGroup>
   <ItemGroup>
     <Filter Include="arm">
@@ -127,6 +130,12 @@
     <ClInclude Include="src\arm\interpreter\skyeye_defs.h">
       <Filter>arm\interpreter</Filter>
     </ClInclude>
+    <ClInclude Include="src\arm\arm_interface.h">
+      <Filter>arm</Filter>
+    </ClInclude>
+    <ClInclude Include="src\arm\interpreter\arm_interpreter.h">
+      <Filter>arm\interpreter</Filter>
+    </ClInclude>
   </ItemGroup>
   <ItemGroup>
     <None Include="CMakeLists.txt" />
diff --git a/src/core/src/arm/arm_interface.h b/src/core/src/arm/arm_interface.h
new file mode 100644
index 0000000000..785234396a
--- /dev/null
+++ b/src/core/src/arm/arm_interface.h
@@ -0,0 +1,47 @@
+/**
+ * Copyright (C) 2013 Citrus Emulator
+ *
+ * @file    arm_interface.h
+ * @author  bunnei 
+ * @date    2014-04-04
+ * @brief   Generic ARM CPU core interface
+ *
+ * @section LICENSE
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details at
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * Official project repository can be found at:
+ * http://code.google.com/p/gekko-gc-emu/
+ */
+
+#pragma once
+
+#include "common_types.h"
+
+/// Generic ARM11 CPU interface
+class ARM_Interface {
+public:
+    ARM_Interface() {
+    }
+
+    ~ARM_Interface() {
+    }
+
+    virtual void ExecuteInstruction() = 0;
+    
+    virtual void SetPC(u32 pc) = 0;
+
+    virtual u32 PC() = 0;
+
+    virtual u32 Reg(int index) = 0;
+
+    virtual u32 CPSR() = 0;
+};
diff --git a/src/core/src/arm/interpreter/arm_interpreter.cpp b/src/core/src/arm/interpreter/arm_interpreter.cpp
new file mode 100644
index 0000000000..9305069634
--- /dev/null
+++ b/src/core/src/arm/interpreter/arm_interpreter.cpp
@@ -0,0 +1,86 @@
+/**
+* Copyright (C) 2013 Citrus Emulator
+*
+* @file    arm_interpreter.h
+* @author  bunnei
+* @date    2014-04-04
+* @brief   ARM interface instance for SkyEye interprerer
+*
+* @section LICENSE
+* This program is free software; you can redistribute it and/or
+* modify it under the terms of the GNU General Public License as
+* published by the Free Software Foundation; either version 2 of
+* the License, or (at your option) any later version.
+*
+* This program is distributed in the hope that it will be useful, but
+* WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* General Public License for more details at
+* http://www.gnu.org/copyleft/gpl.html
+*
+* Official project repository can be found at:
+* http://code.google.com/p/gekko-gc-emu/
+*/
+
+#include "arm_interpreter.h"
+
+const static cpu_config_t s_arm11_cpu_info = {
+    "armv6", "arm11", 0x0007b000, 0x0007f000, NONCACHE
+};
+
+ARM_Interpreter::ARM_Interpreter()  {
+
+    state = new ARMul_State;
+
+    ARMul_EmulateInit();
+    ARMul_NewState(state);
+
+    state->abort_model = 0;
+    state->cpu = (cpu_config_t*)&s_arm11_cpu_info;
+    state->bigendSig = LOW;
+
+    ARMul_SelectProcessor(state, ARM_v6_Prop | ARM_v5_Prop | ARM_v5e_Prop);
+    state->lateabtSig = LOW;
+    mmu_init(state);
+
+    // Reset the core to initial state
+    ARMul_Reset(state);
+    state->NextInstr = 0;
+    state->Emulate = 3;
+
+    state->pc = state->Reg[15] = 0x00000000;
+    state->Reg[13] = 0x10000000; // Set stack pointer to the top of the stack
+}
+
+void ARM_Interpreter::SetPC(u32 pc) {
+    state->pc = state->Reg[15] = pc;
+}
+
+u32 ARM_Interpreter::PC() {
+    return state->pc;
+}
+
+u32 ARM_Interpreter::Reg(int index){
+    return state->Reg[index];
+}
+
+u32 ARM_Interpreter::CPSR() {
+    return state->Cpsr;
+}
+
+ARM_Interpreter::~ARM_Interpreter() {
+    delete state;
+}
+
+void ARM_Interpreter::ExecuteInstruction() {
+    state->step++;
+    state->cycle++;
+    state->EndCondition = 0;
+    state->stop_simulator = 0;
+    state->NextInstr = RESUME;
+    state->last_pc = state->Reg[15];
+    state->Reg[15] = ARMul_DoInstr(state);
+    state->Cpsr = ((state->Cpsr & 0x0fffffdf) | (state->NFlag << 31) | (state->ZFlag << 30) | 
+                   (state->CFlag << 29) | (state->VFlag << 28) | (state->TFlag << 5));
+    FLUSHPIPE;
+}
diff --git a/src/core/src/arm/interpreter/arm_interpreter.h b/src/core/src/arm/interpreter/arm_interpreter.h
new file mode 100644
index 0000000000..89f871fa93
--- /dev/null
+++ b/src/core/src/arm/interpreter/arm_interpreter.h
@@ -0,0 +1,50 @@
+/**
+* Copyright (C) 2013 Citrus Emulator
+*
+* @file    arm_interpreter.h
+* @author  bunnei
+* @date    2014-04-04
+* @brief   ARM interface instance for SkyEye interprerer
+*
+* @section LICENSE
+* This program is free software; you can redistribute it and/or
+* modify it under the terms of the GNU General Public License as
+* published by the Free Software Foundation; either version 2 of
+* the License, or (at your option) any later version.
+*
+* This program is distributed in the hope that it will be useful, but
+* WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* General Public License for more details at
+* http://www.gnu.org/copyleft/gpl.html
+*
+* Official project repository can be found at:
+* http://code.google.com/p/gekko-gc-emu/
+*/
+
+#pragma once
+
+#include "common_types.h"
+#include "arm/arm_interface.h"
+
+#include "arm/interpreter/armdefs.h"
+#include "arm/interpreter/armemu.h"
+
+class ARM_Interpreter : virtual public ARM_Interface {
+public:
+    ARM_Interpreter();
+    ~ARM_Interpreter();
+
+    void ExecuteInstruction();
+
+    void SetPC(u32 pc);
+
+    u32 PC();
+
+    u32 Reg(int index);
+
+    u32 CPSR();
+
+private:
+    ARMul_State* state;
+};
diff --git a/src/core/src/core.cpp b/src/core/src/core.cpp
index edae66b9f4..5928051346 100644
--- a/src/core/src/core.cpp
+++ b/src/core/src/core.cpp
@@ -25,28 +25,14 @@
 #include "log.h"
 #include "core.h"
 #include "mem_map.h"
-#include "arm/armdefs.h"
-#include "arm/armemu.h"
 #include "arm/disassembler/arm_disasm.h"
+#include "arm/interpreter/arm_interpreter.h"
 
 namespace Core {
 
-typedef struct arm11_core{
-    conf_object_t* obj;
-    ARMul_State* state;
-    memory_space_intf* space;
-}arm11_core_t;
-
-arm11_core* core = NULL;
-
-Arm* disasm = NULL;
-
-//ARMul_State* g_arm_state = NULL;
-
-/// Start the core
-void Start() {
-    // TODO(ShizZy): ImplementMe
-}
+ARM_Disasm*     g_disasm    = NULL; ///< ARM disassembler
+ARM_Interface*  g_app_core  = NULL; ///< ARM11 application core
+ARM_Interface*  g_sys_core  = NULL; ///< ARM11 system (OS) core
 
 /// Run the core CPU loop
 void RunLoop() {
@@ -55,23 +41,7 @@ void RunLoop() {
 
 /// Step the CPU one instruction
 void SingleStep() {
-    ARMul_State *state = core->state;
-
-    state->step++;
-    state->cycle++;
-    state->EndCondition = 0;
-    state->stop_simulator = 0;
-    state->NextInstr = RESUME;      /* treat as PC change */
-    state->last_pc = state->Reg[15];
-    state->Reg[15] = ARMul_DoInstr(state);
-    state->Cpsr = (state->Cpsr & 0x0fffffdf) | \
-        (state->NFlag << 31) | \
-        (state->ZFlag << 30) | \
-        (state->CFlag << 29) | \
-        (state->VFlag << 28);// | \
-        //(state->TFlag << 5);
-
-    FLUSHPIPE;
+    g_app_core->ExecuteInstruction();
 }
 
 /// Halt the core
@@ -85,55 +55,20 @@ void Stop() {
 }
 
 /// Initialize the core
-const static cpu_config_t arm11_cpu_info = { "armv6", "arm11", 0x0007b000, 0x0007f000, NONCACHE };
 int Init() {
     NOTICE_LOG(MASTER_LOG, "Core initialized OK");
 
-    disasm = new Arm();
-    core = (arm11_core_t*)malloc(sizeof(arm11_core_t));
-    //core->obj = new_conf_object(obj_name, core);
-    ARMul_EmulateInit();
-    ARMul_State* state = new ARMul_State;
-    ARMul_NewState(state);
-    state->abort_model = 0;
-    state->cpu = (cpu_config_t*)&arm11_cpu_info;
-    state->bigendSig = LOW;
+    g_disasm = new ARM_Disasm();
+    g_app_core = new ARM_Interpreter();
+    g_sys_core = new ARM_Interpreter();
 
-    ARMul_SelectProcessor(state, ARM_v6_Prop | ARM_v5_Prop | ARM_v5e_Prop);
-    state->lateabtSig = LOW;
-    mmu_init(state);
-    /* reset the core to initial state */
-    ARMul_Reset(state);
-    state->NextInstr = 0;
-    state->Emulate = 3;
-#if 0
-    state->mmu.ops.read_byte = arm11_read_byte;
-    state->mmu.ops.read_halfword = arm11_read_halfword;
-    state->mmu.ops.read_word = arm11_read_word;
-    state->mmu.ops.write_byte = arm11_write_byte;
-    state->mmu.ops.write_halfword = arm11_write_halfword;
-    state->mmu.ops.write_word = arm11_write_word;
-#endif
-    core->state = state;
-
-    state->pc = state->Reg[15] = 0x080c3ee0; // Hardcoded set PC to start address of a homebrew ROM
-                                             // this is where most launcher.dat code loads /bunnei
-
-    state->Reg[13] = 0x10000000; // Set stack pointer to the top of the stack, not sure if this is 
-                                    // right? /bunnei
-
-    //state->s
     return 0;
 }
 
-ARMul_State* GetState()
-{
-    return core->state;
-}
-
 void Shutdown() {
-    //delete g_arm_state;
-    //g_arm_state = NULL;
+    delete g_disasm;
+    delete g_app_core;
+    delete g_sys_core;
 }
 
 } // namespace
diff --git a/src/core/src/core.h b/src/core/src/core.h
index f4a3ca05d4..f41daca6ab 100644
--- a/src/core/src/core.h
+++ b/src/core/src/core.h
@@ -25,12 +25,18 @@
 #ifndef CORE_CORE_H_
 #define CORE_CORE_H_
 
-#include "arm/armdefs.h"
+#include "arm/arm_interface.h"
+#include "arm/interpreter/armdefs.h"
 
 ////////////////////////////////////////////////////////////////////////////////////////////////////
 
 namespace Core {
 
+extern ARM_Interface*   g_app_core;     ///< ARM11 application core
+extern ARM_Interface*   g_sys_core;     ///< ARM11 system (OS) core
+
+////////////////////////////////////////////////////////////////////////////////////////////////////
+
 /// Start the core
 void Start();