MemoryConditional.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. using Ryujinx.HLE.HOS.Tamper.Conditions;
  2. namespace Ryujinx.HLE.HOS.Tamper.CodeEmitters
  3. {
  4. /// <summary>
  5. /// Code type 1 performs a comparison of the contents of memory to a static value.
  6. /// If the condition is not met, all instructions until the appropriate conditional block terminator
  7. /// are skipped.
  8. /// </summary>
  9. class MemoryConditional
  10. {
  11. private const int OperationWidthIndex = 1;
  12. private const int MemoryRegionIndex = 2;
  13. private const int ComparisonTypeIndex = 3;
  14. private const int OffsetImmediateIndex = 6;
  15. private const int ValueImmediateIndex = 16;
  16. private const int OffsetImmediateSize = 10;
  17. private const int ValueImmediateSize4 = 8;
  18. private const int ValueImmediateSize8 = 16;
  19. public static ICondition Emit(byte[] instruction, CompilationContext context)
  20. {
  21. // 1TMC00AA AAAAAAAA VVVVVVVV (VVVVVVVV)
  22. // T: Width of memory write (1, 2, 4, or 8 bytes).
  23. // M: Memory region to write to (0 = Main NSO, 1 = Heap).
  24. // C: Condition to use, see below.
  25. // A: Immediate offset to use from memory region base.
  26. // V: Value to compare to.
  27. byte operationWidth = instruction[OperationWidthIndex];
  28. MemoryRegion memoryRegion = (MemoryRegion)instruction[MemoryRegionIndex];
  29. Comparison comparison = (Comparison)instruction[ComparisonTypeIndex];
  30. ulong address = InstructionHelper.GetImmediate(instruction, OffsetImmediateIndex, OffsetImmediateSize);
  31. Pointer sourceMemory = MemoryHelper.EmitPointer(memoryRegion, address, context);
  32. int valueSize = operationWidth <= 4 ? ValueImmediateSize4 : ValueImmediateSize8;
  33. ulong value = InstructionHelper.GetImmediate(instruction, ValueImmediateIndex, valueSize);
  34. Value<ulong> compareToValue = new Value<ulong>(value);
  35. return InstructionHelper.CreateCondition(comparison, operationWidth, sourceMemory, compareToValue);
  36. }
  37. }
  38. }