From 7e4fb4db193433c77ffd707b62d11e58e46c684e Mon Sep 17 00:00:00 2001
From: Lioncash <mathew1800@gmail.com>
Date: Mon, 27 Jul 2015 22:22:00 -0400
Subject: [PATCH] dyncom: Remove code duplication regarding thumb instructions

---
 src/core/arm/dyncom/arm_dyncom_interpreter.cpp | 12 +++---------
 src/core/arm/dyncom/arm_dyncom_thumb.cpp       |  8 +-------
 src/core/arm/dyncom/arm_dyncom_thumb.h         | 15 ++++++++-------
 3 files changed, 12 insertions(+), 23 deletions(-)

diff --git a/src/core/arm/dyncom/arm_dyncom_interpreter.cpp b/src/core/arm/dyncom/arm_dyncom_interpreter.cpp
index 69228180e7..cdef72be66 100644
--- a/src/core/arm/dyncom/arm_dyncom_interpreter.cpp
+++ b/src/core/arm/dyncom/arm_dyncom_interpreter.cpp
@@ -3471,18 +3471,12 @@ enum {
 static tdstate decode_thumb_instr(u32 inst, u32 addr, u32* arm_inst, u32* inst_size, ARM_INST_PTR* ptr_inst_base) {
     // Check if in Thumb mode
     tdstate ret = thumb_translate (addr, inst, arm_inst, inst_size);
-    if(ret == t_branch){
-        // TODO: FIXME, endian should be judged
-        u32 tinstr;
-        if((addr & 0x3) != 0)
-            tinstr = inst >> 16;
-        else
-            tinstr = inst & 0xFFFF;
-
+    if (ret == t_branch) {
         int inst_index;
         int table_length = sizeof(arm_instruction_trans) / sizeof(transop_fp_t);
+        u32 tinstr = GetThumbInstruction(inst, addr);
 
-        switch((tinstr & 0xF800) >> 11){
+        switch ((tinstr & 0xF800) >> 11) {
         case 26:
         case 27:
             if (((tinstr & 0x0F00) != 0x0E00) && ((tinstr & 0x0F00) != 0x0F00)){
diff --git a/src/core/arm/dyncom/arm_dyncom_thumb.cpp b/src/core/arm/dyncom/arm_dyncom_thumb.cpp
index 2860af3767..6eb03fb5a6 100644
--- a/src/core/arm/dyncom/arm_dyncom_thumb.cpp
+++ b/src/core/arm/dyncom/arm_dyncom_thumb.cpp
@@ -14,13 +14,7 @@
 
 tdstate thumb_translate(u32 addr, u32 instr, u32* ainstr, u32* inst_size) {
     tdstate valid = t_uninitialized;
-    u32 tinstr = instr;
-
-    // The endian should be judge here
-    if((addr & 0x3) != 0)
-        tinstr = instr >> 16;
-    else
-        tinstr &= 0xFFFF;
+    u32 tinstr = GetThumbInstruction(instr, addr);
 
     *ainstr = 0xDEADC0DE; // Debugging to catch non updates
 
diff --git a/src/core/arm/dyncom/arm_dyncom_thumb.h b/src/core/arm/dyncom/arm_dyncom_thumb.h
index c06f095803..74e69e13a5 100644
--- a/src/core/arm/dyncom/arm_dyncom_thumb.h
+++ b/src/core/arm/dyncom/arm_dyncom_thumb.h
@@ -37,11 +37,12 @@ enum tdstate {
 
 tdstate thumb_translate(u32 addr, u32 instr, u32* ainstr, u32* inst_size);
 
-static inline u32 get_thumb_instr(u32 instr, u32 pc) {
-    u32 tinstr;
-    if ((pc & 0x3) != 0)
-        tinstr = instr >> 16;
-    else
-        tinstr = instr & 0xFFFF;
-    return tinstr;
+static inline u32 GetThumbInstruction(u32 instr, u32 address) {
+    // Normally you would need to handle instruction endianness,
+    // however, it is fixed to little-endian on the MPCore, so
+    // there's no need to check for this beforehand.
+    if ((address & 0x3) != 0)
+        return instr >> 16;
+
+    return instr & 0xFFFF;
 }