DeviceStateWithShadow.cs 3.8 KB

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