From 5601aa554e96924b5c9496ea586f8348a2bb5f90 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Thu, 25 Feb 2021 10:29:19 -0500 Subject: [PATCH] thumb32: Implement STRB/STRH/STR (register) --- src/CMakeLists.txt | 1 + src/frontend/A32/decoder/thumb32.inc | 6 +-- .../impl/thumb32_store_single_data_item.cpp | 50 +++++++++++++++++++ .../A32/translate/impl/translate_thumb.h | 5 ++ 4 files changed, 59 insertions(+), 3 deletions(-) create mode 100644 src/frontend/A32/translate/impl/thumb32_store_single_data_item.cpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index fa00cbae..6e36979d 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -162,6 +162,7 @@ if ("A32" IN_LIST DYNARMIC_FRONTENDS) frontend/A32/translate/impl/thumb32_misc.cpp frontend/A32/translate/impl/thumb32_multiply.cpp frontend/A32/translate/impl/thumb32_parallel.cpp + frontend/A32/translate/impl/thumb32_store_single_data_item.cpp frontend/A32/translate/impl/translate_arm.h frontend/A32/translate/impl/translate_thumb.h frontend/A32/translate/impl/vfp.cpp diff --git a/src/frontend/A32/decoder/thumb32.inc b/src/frontend/A32/decoder/thumb32.inc index 71e3e4ef..90d8c3ae 100644 --- a/src/frontend/A32/decoder/thumb32.inc +++ b/src/frontend/A32/decoder/thumb32.inc @@ -133,17 +133,17 @@ INST(thumb32_BLX_imm, "BLX (imm)", "11110Svvvvvvvvvv11j0jv //INST(thumb32_STRB_imm_2, "STRB (imm)", "111110000000--------1100--------") //INST(thumb32_STRB_imm_3, "STRB (imm)", "111110001000--------------------") //INST(thumb32_STRBT, "STRBT", "111110000000--------1110--------") -//INST(thumb32_STRB, "STRB (reg)", "111110000000--------000000------") +INST(thumb32_STRB, "STRB (reg)", "111110000000nnnntttt000000iimmmm") //INST(thumb32_STRH_imm_1, "STRH (imm)", "111110000010--------1--1--------") //INST(thumb32_STRH_imm_2, "STRH (imm)", "111110000010--------1100--------") //INST(thumb32_STRH_imm_3, "STRH (imm)", "111110001010--------------------") //INST(thumb32_STRHT, "STRHT", "111110000010--------1110--------") -//INST(thumb32_STRH, "STRH (reg)", "111110000010--------000000------") +INST(thumb32_STRH, "STRH (reg)", "111110000010nnnntttt000000iimmmm") //INST(thumb32_STR_imm_1, "STR (imm)", "111110000100--------1--1--------") //INST(thumb32_STR_imm_2, "STR (imm)", "111110000100--------1100--------") //INST(thumb32_STR_imm_3, "STR (imm)", "111110001100--------------------") //INST(thumb32_STRT, "STRT", "111110000100--------1110--------") -//INST(thumb32_STR_reg, "STR (reg)", "111110000100--------000000------") +INST(thumb32_STR_reg, "STR (reg)", "111110000100nnnntttt000000iimmmm") // Load Byte and Memory Hints //INST(thumb32_PLD_lit, "PLD (lit)", "11111000-00111111111------------") diff --git a/src/frontend/A32/translate/impl/thumb32_store_single_data_item.cpp b/src/frontend/A32/translate/impl/thumb32_store_single_data_item.cpp new file mode 100644 index 00000000..0b776e75 --- /dev/null +++ b/src/frontend/A32/translate/impl/thumb32_store_single_data_item.cpp @@ -0,0 +1,50 @@ +/* This file is part of the dynarmic project. + * Copyright (c) 2021 MerryMage + * SPDX-License-Identifier: 0BSD + */ + +#include "frontend/A32/translate/impl/translate_thumb.h" + +namespace Dynarmic::A32 { + +template +static bool StoreRegister(ThumbTranslatorVisitor& v, Reg n, Reg t, Imm<2> imm2, Reg m, StoreRegFn store_fn) { + if (n == Reg::PC) { + return v.UndefinedInstruction(); + } + + if (t == Reg::PC || m == Reg::PC) { + return v.UnpredictableInstruction(); + } + + const auto reg_m = v.ir.GetRegister(m); + const auto reg_n = v.ir.GetRegister(n); + const auto reg_t = v.ir.GetRegister(t); + + const auto shift_amount = v.ir.Imm8(static_cast(imm2.ZeroExtend())); + const auto offset = v.ir.LogicalShiftLeft(reg_m, shift_amount); + const auto offset_address = v.ir.Add(reg_n, offset); + + store_fn(offset_address, reg_t); + return true; +} + +bool ThumbTranslatorVisitor::thumb32_STRB(Reg n, Reg t, Imm<2> imm2, Reg m) { + return StoreRegister(*this, n, t, imm2, m, [this](const IR::U32& offset_address, const IR::U32& data) { + ir.WriteMemory8(offset_address, ir.LeastSignificantByte(data)); + }); +} + +bool ThumbTranslatorVisitor::thumb32_STRH(Reg n, Reg t, Imm<2> imm2, Reg m) { + return StoreRegister(*this, n, t, imm2, m, [this](const IR::U32& offset_address, const IR::U32& data) { + ir.WriteMemory16(offset_address, ir.LeastSignificantHalf(data)); + }); +} + +bool ThumbTranslatorVisitor::thumb32_STR_reg(Reg n, Reg t, Imm<2> imm2, Reg m) { + return StoreRegister(*this, n, t, imm2, m, [this](const IR::U32& offset_address, const IR::U32& data) { + ir.WriteMemory32(offset_address, data); + }); +} + +} // namespace Dynarmic::A32 diff --git a/src/frontend/A32/translate/impl/translate_thumb.h b/src/frontend/A32/translate/impl/translate_thumb.h index 5ffecd9d..e22699da 100644 --- a/src/frontend/A32/translate/impl/translate_thumb.h +++ b/src/frontend/A32/translate/impl/translate_thumb.h @@ -181,6 +181,11 @@ struct ThumbTranslatorVisitor final { bool thumb32_BL_imm(Imm<1> S, Imm<10> hi, Imm<1> j1, Imm<1> j2, Imm<11> lo); bool thumb32_BLX_imm(Imm<1> S, Imm<10> hi, Imm<1> j1, Imm<1> j2, Imm<11> lo); + // thumb32 store single data item instructions + bool thumb32_STRB(Reg n, Reg t, Imm<2> imm2, Reg m); + bool thumb32_STRH(Reg n, Reg t, Imm<2> imm2, Reg m); + bool thumb32_STR_reg(Reg n, Reg t, Imm<2> imm2, Reg m); + // thumb32 data processing (register) instructions bool thumb32_SXTB(Reg d, SignExtendRotation rotate, Reg m); bool thumb32_SXTB16(Reg d, SignExtendRotation rotate, Reg m);