diff --git a/src/frontend/A32/decoder/thumb32.inc b/src/frontend/A32/decoder/thumb32.inc index 8aabfc88..20fed89a 100644 --- a/src/frontend/A32/decoder/thumb32.inc +++ b/src/frontend/A32/decoder/thumb32.inc @@ -3,7 +3,7 @@ //INST(thumb32_RFE_2, "RFE", "1110100000-1--------------------") INST(thumb32_STMIA, "STMIA/STMEA", "1110100010W0nnnn0iiiiiiiiiiiiiii") //INST(thumb32_POP, "POP", "1110100010111101----------------") -//INST(thumb32_LDMIA, "LDMIA/LDMFD", "1110100010-1--------------------") +INST(thumb32_LDMIA, "LDMIA/LDMFD", "1110100010W1nnnniiiiiiiiiiiiiiii") //INST(thumb32_PUSH, "PUSH", "1110100100101101----------------") INST(thumb32_STMDB, "STMDB/STMFD", "1110100100W0nnnn0iiiiiiiiiiiiiii") //INST(thumb32_LDMDB, "LDMDB/LDMEA", "1110100100-1--------------------") diff --git a/src/frontend/A32/translate/impl/thumb32_load_store_multiple.cpp b/src/frontend/A32/translate/impl/thumb32_load_store_multiple.cpp index f6fb751d..528bec7f 100644 --- a/src/frontend/A32/translate/impl/thumb32_load_store_multiple.cpp +++ b/src/frontend/A32/translate/impl/thumb32_load_store_multiple.cpp @@ -7,6 +7,59 @@ #include "frontend/A32/translate/impl/translate_thumb.h" namespace Dynarmic::A32 { +static bool ITBlockCheck(const A32::IREmitter& ir) { + return ir.current_location.IT().IsInITBlock() && !ir.current_location.IT().IsLastInITBlock(); +} + +static bool LDMHelper(A32::IREmitter& ir, bool W, Reg n, u32 list, + const IR::U32& start_address, const IR::U32& writeback_address) { + auto address = start_address; + for (size_t i = 0; i <= 14; i++) { + if (Common::Bit(i, list)) { + ir.SetRegister(static_cast(i), ir.ReadMemory32(address)); + address = ir.Add(address, ir.Imm32(4)); + } + } + if (W && !Common::Bit(RegNumber(n), list)) { + ir.SetRegister(n, writeback_address); + } + if (Common::Bit<15>(list)) { + ir.UpdateUpperLocationDescriptor(); + ir.LoadWritePC(ir.ReadMemory32(address)); + if (n == Reg::R13) { + ir.SetTerm(IR::Term::PopRSBHint{}); + } else { + ir.SetTerm(IR::Term::FastDispatchHint{}); + } + return false; + } + return true; +} + +bool ThumbTranslatorVisitor::thumb32_LDMIA(bool W, Reg n, Imm<16> reg_list) { + const auto regs_imm = reg_list.ZeroExtend(); + const auto num_regs = static_cast(Common::BitCount(regs_imm)); + + if (n == Reg::PC || num_regs < 2) { + return UnpredictableInstruction(); + } + if (reg_list.Bit<15>() && reg_list.Bit<14>()) { + return UnpredictableInstruction(); + } + if (W && Common::Bit(static_cast(n), regs_imm)) { + return UnpredictableInstruction(); + } + if (reg_list.Bit<13>()) { + return UnpredictableInstruction(); + } + if (reg_list.Bit<15>() && ITBlockCheck(ir)) { + return UnpredictableInstruction(); + } + + const auto start_address = ir.GetRegister(n); + const auto writeback_address = ir.Add(start_address, ir.Imm32(num_regs * 4)); + return LDMHelper(ir, W, n, regs_imm, start_address, writeback_address); +} bool ThumbTranslatorVisitor::thumb32_STMIA(bool W, Reg n, Imm<15> reg_list) { const auto regs_imm = reg_list.ZeroExtend(); diff --git a/src/frontend/A32/translate/impl/translate_thumb.h b/src/frontend/A32/translate/impl/translate_thumb.h index 33fa6147..a4728a16 100644 --- a/src/frontend/A32/translate/impl/translate_thumb.h +++ b/src/frontend/A32/translate/impl/translate_thumb.h @@ -172,6 +172,7 @@ struct ThumbTranslatorVisitor final { bool thumb16_B_t2(Imm<11> imm11); // thumb32 load/store multiple instructions + bool thumb32_LDMIA(bool W, Reg n, Imm<16> reg_list); bool thumb32_STMIA(bool W, Reg n, Imm<15> reg_list); bool thumb32_STMDB(bool W, Reg n, Imm<15> reg_list);