DeviceStateWithShadow.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using Ryujinx.Graphics.Device;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.Diagnostics.CodeAnalysis;
  6. using System.Runtime.CompilerServices;
  7. namespace Ryujinx.Graphics.Gpu.Engine
  8. {
  9. /// <summary>
  10. /// State interface with a shadow memory control register.
  11. /// </summary>
  12. interface IShadowState
  13. {
  14. /// <summary>
  15. /// MME shadow ram control mode.
  16. /// </summary>
  17. SetMmeShadowRamControlMode SetMmeShadowRamControlMode { get; }
  18. }
  19. /// <summary>
  20. /// Represents a device's state, with a additional shadow state.
  21. /// </summary>
  22. /// <typeparam name="TState">Type of the state</typeparam>
  23. class DeviceStateWithShadow<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)] TState> : IDeviceState where TState : unmanaged, IShadowState
  24. {
  25. private readonly DeviceState<TState> _state;
  26. private readonly DeviceState<TState> _shadowState;
  27. /// <summary>
  28. /// Current device state.
  29. /// </summary>
  30. public ref TState State => ref _state.State;
  31. /// <summary>
  32. /// Creates a new instance of the device state, with shadow state.
  33. /// </summary>
  34. /// <param name="callbacks">Optional that will be called if a register specified by name is read or written</param>
  35. /// <param name="debugLogCallback">Optional callback to be used for debug log messages</param>
  36. public DeviceStateWithShadow(IReadOnlyDictionary<string, RwCallback> callbacks = null, Action<string> debugLogCallback = null)
  37. {
  38. _state = new DeviceState<TState>(callbacks, debugLogCallback);
  39. _shadowState = new DeviceState<TState>();
  40. }
  41. /// <summary>
  42. /// Reads a value from a register.
  43. /// </summary>
  44. /// <param name="offset">Register offset in bytes</param>
  45. /// <returns>Value stored on the register</returns>
  46. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  47. public int Read(int offset)
  48. {
  49. return _state.Read(offset);
  50. }
  51. /// <summary>
  52. /// Writes a value to a register.
  53. /// </summary>
  54. /// <param name="offset">Register offset in bytes</param>
  55. /// <param name="value">Value to be written</param>
  56. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  57. public void Write(int offset, int value)
  58. {
  59. WriteWithRedundancyCheck(offset, value, out _);
  60. }
  61. /// <summary>
  62. /// Writes a value to a register, returning a value indicating if <paramref name="value"/>
  63. /// is different from the current value on the register.
  64. /// </summary>
  65. /// <param name="offset">Register offset in bytes</param>
  66. /// <param name="value">Value to be written</param>
  67. /// <param name="changed">True if the value was changed, false otherwise</param>
  68. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  69. public void WriteWithRedundancyCheck(int offset, int value, out bool changed)
  70. {
  71. var shadowRamControl = _state.State.SetMmeShadowRamControlMode;
  72. if (shadowRamControl == SetMmeShadowRamControlMode.MethodPassthrough || offset < 0x200)
  73. {
  74. _state.WriteWithRedundancyCheck(offset, value, out changed);
  75. }
  76. else if (shadowRamControl == SetMmeShadowRamControlMode.MethodTrack ||
  77. shadowRamControl == SetMmeShadowRamControlMode.MethodTrackWithFilter)
  78. {
  79. _shadowState.Write(offset, value);
  80. _state.WriteWithRedundancyCheck(offset, value, out changed);
  81. }
  82. else /* if (shadowRamControl == SetMmeShadowRamControlMode.MethodReplay) */
  83. {
  84. Debug.Assert(shadowRamControl == SetMmeShadowRamControlMode.MethodReplay);
  85. _state.WriteWithRedundancyCheck(offset, _shadowState.Read(offset), out changed);
  86. }
  87. }
  88. }
  89. }