/* This file is part of the dynarmic project. * Copyright (c) 2016 MerryMage * This software may be used and distributed according to the terms of the GNU * General Public License version 2 or any later version. */ #include "common/bit_util.h" #include "translate_arm.h" namespace Dynarmic::A32 { // BFC , #, # bool ArmTranslatorVisitor::arm_BFC(Cond cond, Imm5 msb, Reg d, Imm5 lsb) { if (d == Reg::PC) { return UnpredictableInstruction(); } if (msb < lsb) { return UnpredictableInstruction(); } if (!ConditionPassed(cond)) { return true; } const u32 mask = ~(Common::Ones(msb - lsb + 1) << lsb); const IR::U32 operand = ir.GetRegister(d); const IR::U32 result = ir.And(operand, ir.Imm32(mask)); ir.SetRegister(d, result); return true; } // BFI , , #, # bool ArmTranslatorVisitor::arm_BFI(Cond cond, Imm5 msb, Reg d, Imm5 lsb, Reg n) { if (d == Reg::PC) { return UnpredictableInstruction(); } if (msb < lsb) { return UnpredictableInstruction(); } if (!ConditionPassed(cond)) { return true; } const u32 inclusion_mask = Common::Ones(msb - lsb + 1) << lsb; const u32 exclusion_mask = ~inclusion_mask; const IR::U32 operand1 = ir.And(ir.GetRegister(d), ir.Imm32(exclusion_mask)); const IR::U32 operand2 = ir.And(ir.LogicalShiftLeft(ir.GetRegister(n), ir.Imm8(lsb)), ir.Imm32(inclusion_mask)); const IR::U32 result = ir.Or(operand1, operand2); ir.SetRegister(d, result); return true; } // CLZ , bool ArmTranslatorVisitor::arm_CLZ(Cond cond, Reg d, Reg m) { if (d == Reg::PC || m == Reg::PC) { return UnpredictableInstruction(); } if (!ConditionPassed(cond)) { return true; } ir.SetRegister(d, ir.CountLeadingZeros(ir.GetRegister(m))); return true; } // MOVT , # bool ArmTranslatorVisitor::arm_MOVT(Cond cond, Imm4 imm4, Reg d, Imm12 imm12) { if (d == Reg::PC) { return UnpredictableInstruction(); } if (!ConditionPassed(cond)) { return true; } const IR::U32 imm16 = ir.Imm32(((u32(imm4) << 12 | u32(imm12)) << 16)); const IR::U32 operand = ir.GetRegister(d); const IR::U32 result = ir.Or(ir.And(operand, ir.Imm32(0x0000FFFFU)), imm16); ir.SetRegister(d, result); return true; } // SBFX , , #, # bool ArmTranslatorVisitor::arm_SBFX(Cond cond, Imm5 widthm1, Reg d, Imm5 lsb, Reg n) { if (d == Reg::PC || n == Reg::PC) { return UnpredictableInstruction(); } const u32 msb = u32{lsb} + widthm1; if (msb >= Common::BitSize()) { return UnpredictableInstruction(); } if (!ConditionPassed(cond)) { return true; } constexpr size_t max_width = Common::BitSize(); const u8 width = widthm1 + 1; const u8 left_shift_amount = static_cast(max_width - width - lsb); const u8 right_shift_amount = static_cast(max_width - width); const IR::U32 operand = ir.GetRegister(n); const IR::U32 tmp = ir.LogicalShiftLeft(operand, ir.Imm8(left_shift_amount)); const IR::U32 result = ir.ArithmeticShiftRight(tmp, ir.Imm8(right_shift_amount)); ir.SetRegister(d, result); return true; } // SEL , , bool ArmTranslatorVisitor::arm_SEL(Cond cond, Reg n, Reg d, Reg m) { if (n == Reg::PC || d == Reg::PC || m == Reg::PC) { return UnpredictableInstruction(); } if (!ConditionPassed(cond)) { return true; } const auto to = ir.GetRegister(m); const auto from = ir.GetRegister(n); const auto result = ir.PackedSelect(ir.GetGEFlags(), to, from); ir.SetRegister(d, result); return true; } // UBFX , , #, # bool ArmTranslatorVisitor::arm_UBFX(Cond cond, Imm5 widthm1, Reg d, Imm5 lsb, Reg n) { if (d == Reg::PC || n == Reg::PC) { return UnpredictableInstruction(); } const u32 msb = u32{lsb} + widthm1; if (msb >= Common::BitSize()) { return UnpredictableInstruction(); } if (!ConditionPassed(cond)) { return true; } const IR::U32 operand = ir.GetRegister(n); const IR::U32 mask = ir.Imm32(Common::Ones(widthm1 + 1)); const IR::U32 result = ir.And(ir.LogicalShiftRight(operand, ir.Imm8(lsb)), mask); ir.SetRegister(d, result); return true; } } // namespace Dynarmic::A32