StoreConstantToAddress.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. namespace Ryujinx.HLE.HOS.Tamper.CodeEmitters
  2. {
  3. /// <summary>
  4. /// Code type 0 allows writing a static value to a memory address.
  5. /// </summary>
  6. class StoreConstantToAddress
  7. {
  8. private const int OperationWidthIndex = 1;
  9. private const int MemoryRegionIndex = 2;
  10. private const int OffsetRegisterIndex = 3;
  11. private const int OffsetImmediateIndex = 6;
  12. private const int ValueImmediateIndex = 16;
  13. private const int OffsetImmediateSize = 10;
  14. private const int ValueImmediateSize8 = 8;
  15. private const int ValueImmediateSize16 = 16;
  16. public static void Emit(byte[] instruction, CompilationContext context)
  17. {
  18. // 0TMR00AA AAAAAAAA VVVVVVVV (VVVVVVVV)
  19. // T: Width of memory write(1, 2, 4, or 8 bytes).
  20. // M: Memory region to write to(0 = Main NSO, 1 = Heap).
  21. // R: Register to use as an offset from memory region base.
  22. // A: Immediate offset to use from memory region base.
  23. // V: Value to write.
  24. byte operationWidth = instruction[OperationWidthIndex];
  25. MemoryRegion memoryRegion = (MemoryRegion)instruction[MemoryRegionIndex];
  26. Register offsetRegister = context.GetRegister(instruction[OffsetRegisterIndex]);
  27. ulong offsetImmediate = InstructionHelper.GetImmediate(instruction, OffsetImmediateIndex, OffsetImmediateSize);
  28. Pointer dstMem = MemoryHelper.EmitPointer(memoryRegion, offsetRegister, offsetImmediate, context);
  29. int valueImmediateSize = operationWidth <= 4 ? ValueImmediateSize8 : ValueImmediateSize16;
  30. ulong valueImmediate = InstructionHelper.GetImmediate(instruction, ValueImmediateIndex, valueImmediateSize);
  31. Value<ulong> storeValue = new Value<ulong>(valueImmediate);
  32. InstructionHelper.EmitMov(operationWidth, context, dstMem, storeValue);
  33. }
  34. }
  35. }