From a2e40eb922ddcad8126752992d0efa2ee1c95d38 Mon Sep 17 00:00:00 2001
From: MerryMage <MerryMage@users.noreply.github.com>
Date: Fri, 8 Jul 2016 19:15:30 +0800
Subject: [PATCH] Implement thumb1_ADD_imm

---
 src/frontend/arm_types.h            |  1 +
 src/frontend/decoder/thumb1.h       |  4 ++--
 src/frontend/disassembler_thumb.cpp |  4 ++++
 src/frontend/translate_thumb.cpp    | 12 ++++++++++++
 4 files changed, 19 insertions(+), 2 deletions(-)

diff --git a/src/frontend/arm_types.h b/src/frontend/arm_types.h
index e7999a3a..c09209e5 100644
--- a/src/frontend/arm_types.h
+++ b/src/frontend/arm_types.h
@@ -36,6 +36,7 @@ inline Reg operator+(Reg reg, int number) {
     return static_cast<Reg>(new_reg);
 }
 
+using Imm3 = u32;
 using Imm4 = u32;
 using Imm5 = u32;
 using Imm8 = u32;
diff --git a/src/frontend/decoder/thumb1.h b/src/frontend/decoder/thumb1.h
index d1814109..1e150417 100644
--- a/src/frontend/decoder/thumb1.h
+++ b/src/frontend/decoder/thumb1.h
@@ -56,7 +56,7 @@ private:
 };
 
 template <typename V>
-static const std::array<Thumb1Matcher<V>, 12> g_thumb1_instruction_table {{
+static const std::array<Thumb1Matcher<V>, 13> g_thumb1_instruction_table {{
 
 #define INST(fn, name, bitstring) detail::detail<Thumb1Matcher, u16, 16>::GetMatcher<decltype(fn), fn>(name, bitstring)
 
@@ -66,7 +66,7 @@ static const std::array<Thumb1Matcher<V>, 12> g_thumb1_instruction_table {{
     { INST(&V::thumb1_ASR_imm,        "ASR (imm)",                "00010vvvvvmmmddd") },
     { INST(&V::thumb1_ADD_reg_t1,     "ADD (reg, T1)",            "0001100mmmnnnddd") },
     { INST(&V::thumb1_SUB_reg,        "SUB (reg)",                "0001101mmmnnnddd") },
-    //{ INST(&V::thumb1_ADD_rri,        "ADD (rri)",                "0001110mmmnnnddd") },
+    { INST(&V::thumb1_ADD_imm,        "ADD (imm)",                "0001110vvvnnnddd") },
     //{ INST(&V::thumb1_SUB_rri,        "SUB (rri)",                "0001111mmmnnnddd") },
     //{ INST(&V::thumb1_MOV_ri,         "MOV (ri)",                 "00100dddvvvvvvvv") },
     //{ INST(&V::thumb1_CMP_ri,         "CMP (ri)",                 "00101dddvvvvvvvv") },
diff --git a/src/frontend/disassembler_thumb.cpp b/src/frontend/disassembler_thumb.cpp
index 7a464455..4395d3ef 100644
--- a/src/frontend/disassembler_thumb.cpp
+++ b/src/frontend/disassembler_thumb.cpp
@@ -122,6 +122,10 @@ public:
         return Common::StringFromFormat("subs %s, %s, %s", RegStr(d), RegStr(n), RegStr(m));
     }
 
+    std::string thumb1_ADD_imm(Imm3 imm3, Reg n, Reg d) {
+        return Common::StringFromFormat("adds %s, %s, #%u", RegStr(d), RegStr(n), imm3);
+    }
+
     std::string thumb1_AND_reg(Reg m, Reg d_n) {
         return Common::StringFromFormat("ands %s, %s", RegStr(d_n), RegStr(m));
     }
diff --git a/src/frontend/translate_thumb.cpp b/src/frontend/translate_thumb.cpp
index be83fa40..501dbd31 100644
--- a/src/frontend/translate_thumb.cpp
+++ b/src/frontend/translate_thumb.cpp
@@ -86,6 +86,18 @@ struct TranslatorVisitor final {
         ir.SetVFlag(result.overflow);
         return true;
     }
+    bool thumb1_ADD_imm(Imm3 imm3, Reg n, Reg d) {
+        u32 imm32 = imm3 & 0x7;
+        // ADDS <Rd>, <Rn>, #<imm3>
+        // Rd can never encode R15.
+        auto result = ir.AddWithCarry(ir.GetRegister(n), ir.Imm32(imm32), ir.Imm1(0));
+        ir.SetRegister(d, result.result);
+        ir.SetNFlag(ir.MostSignificantBit(result.result));
+        ir.SetZFlag(ir.IsZero(result.result));
+        ir.SetCFlag(result.carry);
+        ir.SetVFlag(result.overflow);
+        return true;
+    }
 
     bool thumb1_AND_reg(Reg m, Reg d_n) {
         const Reg d = d_n, n = d_n;