From 670ac5643a7cda55b7c5d68c99495ade0d14e6e4 Mon Sep 17 00:00:00 2001
From: bunnei <ericbunnie@gmail.com>
Date: Fri, 4 Apr 2014 23:02:59 -0400
Subject: [PATCH] added hw module to interface h/w register reads/writes

---
 src/core/core.vcxproj          |  2 +
 src/core/core.vcxproj.filters  |  9 +++++
 src/core/src/hw/hw.cpp         | 70 ++++++++++++++++++++++++++++++++++
 src/core/src/hw/hw.h           | 35 +++++++++++++++++
 src/core/src/mem_map_funcs.cpp |  5 +--
 5 files changed, 118 insertions(+), 3 deletions(-)
 create mode 100644 src/core/src/hw/hw.cpp
 create mode 100644 src/core/src/hw/hw.h

diff --git a/src/core/core.vcxproj b/src/core/core.vcxproj
index bee9f30467..eb1272b23c 100644
--- a/src/core/core.vcxproj
+++ b/src/core/core.vcxproj
@@ -152,6 +152,7 @@
     <ClCompile Include="src\elf\elf_reader.cpp" />
     <ClCompile Include="src\file_sys\directory_file_system.cpp" />
     <ClCompile Include="src\file_sys\meta_file_system.cpp" />
+    <ClCompile Include="src\hw\hardware.cpp" />
     <ClCompile Include="src\loader.cpp" />
     <ClCompile Include="src\mem_map.cpp" />
     <ClCompile Include="src\mem_map_funcs.cpp" />
@@ -180,6 +181,7 @@
     <ClInclude Include="src\file_sys\directory_file_system.h" />
     <ClInclude Include="src\file_sys\file_sys.h" />
     <ClInclude Include="src\file_sys\meta_file_system.h" />
+    <ClInclude Include="src\hw\hw.h" />
     <ClInclude Include="src\loader.h" />
     <ClInclude Include="src\mem_map.h" />
     <ClInclude Include="src\system.h" />
diff --git a/src/core/core.vcxproj.filters b/src/core/core.vcxproj.filters
index 45ddf8cf64..f3237ed05f 100644
--- a/src/core/core.vcxproj.filters
+++ b/src/core/core.vcxproj.filters
@@ -46,6 +46,9 @@
     <ClCompile Include="src\arm\interpreter\arm_interpreter.cpp">
       <Filter>arm\interpreter</Filter>
     </ClCompile>
+    <ClCompile Include="src\hw\hardware.cpp">
+      <Filter>hw</Filter>
+    </ClCompile>
   </ItemGroup>
   <ItemGroup>
     <Filter Include="arm">
@@ -69,6 +72,9 @@
     <Filter Include="arm\interpreter">
       <UniqueIdentifier>{cca8b763-8a80-4478-9bcc-3c979293c357}</UniqueIdentifier>
     </Filter>
+    <Filter Include="hw">
+      <UniqueIdentifier>{d1158fc4-3e0f-431f-9d3b-f30bbfeb4ad5}</UniqueIdentifier>
+    </Filter>
   </ItemGroup>
   <ItemGroup>
     <ClInclude Include="src\arm\disassembler\arm_disasm.h">
@@ -136,6 +142,9 @@
     <ClInclude Include="src\arm\interpreter\arm_interpreter.h">
       <Filter>arm\interpreter</Filter>
     </ClInclude>
+    <ClInclude Include="src\hw\hw.h">
+      <Filter>hw</Filter>
+    </ClInclude>
   </ItemGroup>
   <ItemGroup>
     <None Include="CMakeLists.txt" />
diff --git a/src/core/src/hw/hw.cpp b/src/core/src/hw/hw.cpp
new file mode 100644
index 0000000000..7250bc237d
--- /dev/null
+++ b/src/core/src/hw/hw.cpp
@@ -0,0 +1,70 @@
+/**
+ * Copyright (C) 2013 Citrus Emulator
+ *
+ * @file    hw.cpp
+ * @author  bunnei
+ * @date    2014-04-04
+ * @brief   Hardware 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/
+ */
+
+#include "log.h"
+#include "hw/hw.h"
+
+namespace HW {
+
+template <typename T>
+inline void Read(T &var, const u32 addr) {
+    // TODO: Figure out the fastest order of tests for both read and write (they are probably different).
+    // TODO: Make sure this represents the mirrors in a correct way.
+
+    // Could just do a base-relative read, too.... TODO
+
+    //if ((addr & 0x3E000000) == 0x08000000) {
+    //    var = *((const T*)&g_fcram[addr & MEM_FCRAM_MASK]);
+
+    //} else {
+    //    _assert_msg_(HW, false, "unknown hardware read");
+    //}
+}
+
+template <typename T>
+inline void Write(u32 addr, const T data) {
+    //// ExeFS:/.code is loaded here:
+    //if ((addr & 0xFFF00000) == 0x00100000) {
+    //    // TODO(ShizZy): This is dumb... handle correctly. From 3DBrew:
+    //    // http://3dbrew.org/wiki/Memory_layout#ARM11_User-land_memory_regions
+    //    // The ExeFS:/.code is loaded here, executables must be loaded to the 0x00100000 region when
+    //    // the exheader "special memory" flag is clear. The 0x03F00000-byte size restriction only 
+    //    // applies when this flag is clear. Executables are usually loaded to 0x14000000 when the 
+    //    // exheader "special memory" flag is set, however this address can be arbitrary.
+    //    *(T*)&g_fcram[addr & MEM_FCRAM_MASK] = data;
+
+    //// Error out...
+    //} else {
+    //    _assert_msg_(HW, false, "unknown hardware write");
+    //}
+}
+
+
+void Init() {
+}
+
+void Shutdown() {
+}
+
+}
\ No newline at end of file
diff --git a/src/core/src/hw/hw.h b/src/core/src/hw/hw.h
new file mode 100644
index 0000000000..dacad49246
--- /dev/null
+++ b/src/core/src/hw/hw.h
@@ -0,0 +1,35 @@
+/**
+ * Copyright (C) 2013 Citrus Emulator
+ *
+ * @file    hw.h
+ * @author  bunnei
+ * @date    2014-04-04
+ * @brief   Hardware 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/
+ */
+
+#include "common_types.h"
+
+namespace HW {
+
+template <typename T>
+inline void Read(T &var, const u32 addr);
+
+template <typename T>
+inline void Write(u32 addr, const T data);
+
+} // namespace
diff --git a/src/core/src/mem_map_funcs.cpp b/src/core/src/mem_map_funcs.cpp
index 446d3ac971..b000571e5c 100644
--- a/src/core/src/mem_map_funcs.cpp
+++ b/src/core/src/mem_map_funcs.cpp
@@ -54,8 +54,7 @@ inline void _Read(T &var, const u32 addr) {
         var = *((const T*)&g_fcram[addr & MEM_FCRAM_MASK]);
 
     } else {
-        _assert_msg_(MEMMAP, false, "unknown hardware read");
-        // WARN_LOG(MEMMAP, "_Read: Invalid addr %08x PC %08x LR %08x", addr, currentMIPS->pc, currentMIPS->r[MIPS_REG_RA]);
+        _assert_msg_(MEMMAP, false, "unknown memory read");
     }
 }
 
@@ -104,7 +103,7 @@ inline void _Write(u32 addr, const T data) {
 
     // Error out...
     } else {
-        _assert_msg_(MEMMAP, false, "unknown hardware write");
+        _assert_msg_(MEMMAP, false, "unknown memory write");
     }
 }