diff --git a/src/frontend/A32/decoder/thumb32.inc b/src/frontend/A32/decoder/thumb32.inc index 71e3e4ef..7796e730 100644 --- a/src/frontend/A32/decoder/thumb32.inc +++ b/src/frontend/A32/decoder/thumb32.inc @@ -78,13 +78,13 @@ INST(thumb32_MOVW_imm, "MOVW (imm)", "11110i100100iiii0iiidd //INST(thumb32_SUB_imm_2, "SUB (imm)", "11110-101010----0---------------") INST(thumb32_MOVT, "MOVT", "11110i101100iiii0iiiddddiiiiiiii") //INST(thumb32_SSAT, "SSAT", "11110-110000----0---------------") -//INST(thumb32_SSAT16, "SSAT16", "11110-110010----0000----00------") +INST(thumb32_SSAT16, "SSAT16", "111100110010nnnn0000dddd0000iiii") //INST(thumb32_SSAT, "SSAT", "11110-110010----0---------------") //INST(thumb32_SBFX, "SBFX", "11110-110100----0---------------") //INST(thumb32_BFC, "BFC", "11110-11011011110---------------") //INST(thumb32_BFI, "BFI", "11110-110110----0---------------") //INST(thumb32_USAT, "USAT", "11110-111000----0---------------") -//INST(thumb32_USAT16, "USAT16", "11110-111010----0000----00------") +INST(thumb32_USAT16, "USAT16", "111100111010nnnn0000dddd0000iiii") //INST(thumb32_USAT, "USAT", "11110-111010----0---------------") //INST(thumb32_UBFX, "UBFX", "11110-111100----0---------------") diff --git a/src/frontend/A32/translate/impl/thumb32_data_processing_plain_binary_immediate.cpp b/src/frontend/A32/translate/impl/thumb32_data_processing_plain_binary_immediate.cpp index 8244ddc2..dcfd289d 100644 --- a/src/frontend/A32/translate/impl/thumb32_data_processing_plain_binary_immediate.cpp +++ b/src/frontend/A32/translate/impl/thumb32_data_processing_plain_binary_immediate.cpp @@ -6,6 +6,33 @@ #include "frontend/A32/translate/impl/translate_thumb.h" namespace Dynarmic::A32 { +static IR::U32 Pack2x16To1x32(A32::IREmitter& ir, IR::U32 lo, IR::U32 hi) { + return ir.Or(ir.And(lo, ir.Imm32(0xFFFF)), ir.LogicalShiftLeft(hi, ir.Imm8(16), ir.Imm1(0)).result); +} + +static IR::U16 MostSignificantHalf(A32::IREmitter& ir, IR::U32 value) { + return ir.LeastSignificantHalf(ir.LogicalShiftRight(value, ir.Imm8(16), ir.Imm1(0)).result); +} + +using SaturationFunction = IR::ResultAndOverflow (IREmitter::*)(const IR::U32&, size_t); + +static bool Saturation16(ThumbTranslatorVisitor& v, Reg n, Reg d, size_t saturate_to, SaturationFunction sat_fn) { + if (d == Reg::PC || n == Reg::PC) { + return v.UnpredictableInstruction(); + } + + const auto reg_n = v.ir.GetRegister(n); + + const auto lo_operand = v.ir.SignExtendHalfToWord(v.ir.LeastSignificantHalf(reg_n)); + const auto hi_operand = v.ir.SignExtendHalfToWord(MostSignificantHalf(v.ir, reg_n)); + const auto lo_result = (v.ir.*sat_fn)(lo_operand, saturate_to); + const auto hi_result = (v.ir.*sat_fn)(hi_operand, saturate_to); + + v.ir.SetRegister(d, Pack2x16To1x32(v.ir, lo_result.result, hi_result.result)); + v.ir.OrQFlag(lo_result.overflow); + v.ir.OrQFlag(hi_result.overflow); + return true; +} bool ThumbTranslatorVisitor::thumb32_MOVT(Imm<1> imm1, Imm<4> imm4, Imm<3> imm3, Reg d, Imm<8> imm8) { if (d == Reg::PC) { @@ -31,4 +58,12 @@ bool ThumbTranslatorVisitor::thumb32_MOVW_imm(Imm<1> imm1, Imm<4> imm4, Imm<3> i return true; } +bool ThumbTranslatorVisitor::thumb32_SSAT16(Reg n, Reg d, Imm<4> sat_imm) { + return Saturation16(*this, n, d, sat_imm.ZeroExtend() + 1, &IREmitter::SignedSaturation); +} + +bool ThumbTranslatorVisitor::thumb32_USAT16(Reg n, Reg d, Imm<4> sat_imm) { + return Saturation16(*this, n, d, sat_imm.ZeroExtend(), &IREmitter::UnsignedSaturation); +} + } // 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..2abf2008 100644 --- a/src/frontend/A32/translate/impl/translate_thumb.h +++ b/src/frontend/A32/translate/impl/translate_thumb.h @@ -162,6 +162,8 @@ struct ThumbTranslatorVisitor final { // thumb32 data processing (plain binary immediate) instructions. bool thumb32_MOVT(Imm<1> imm1, Imm<4> imm4, Imm<3> imm3, Reg d, Imm<8> imm8); bool thumb32_MOVW_imm(Imm<1> imm1, Imm<4> imm4, Imm<3> imm3, Reg d, Imm<8> imm8); + bool thumb32_SSAT16(Reg n, Reg d, Imm<4> sat_imm); + bool thumb32_USAT16(Reg n, Reg d, Imm<4> sat_imm); // thumb32 miscellaneous control instructions bool thumb32_BXJ(Reg m);