aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.Graphics.Gpu/Engine/DeviceStateWithShadow.cs
blob: 74a9aa0493833bb673a57b49ec587fc063dd4bf1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using Ryujinx.Graphics.Device;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;

namespace Ryujinx.Graphics.Gpu.Engine
{
    /// <summary>
    /// State interface with a shadow memory control register.
    /// </summary>
    interface IShadowState
    {
        /// <summary>
        /// MME shadow ram control mode.
        /// </summary>
        SetMmeShadowRamControlMode SetMmeShadowRamControlMode { get; }
    }

    /// <summary>
    /// Represents a device's state, with a additional shadow state.
    /// </summary>
    /// <typeparam name="TState">Type of the state</typeparam>
    class DeviceStateWithShadow<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)] TState> : IDeviceState where TState : unmanaged, IShadowState
    {
        private readonly DeviceState<TState> _state;
        private readonly DeviceState<TState> _shadowState;

        /// <summary>
        /// Current device state.
        /// </summary>
        public ref TState State => ref _state.State;

        /// <summary>
        /// Creates a new instance of the device state, with shadow state.
        /// </summary>
        /// <param name="callbacks">Optional that will be called if a register specified by name is read or written</param>
        /// <param name="debugLogCallback">Optional callback to be used for debug log messages</param>
        public DeviceStateWithShadow(IReadOnlyDictionary<string, RwCallback> callbacks = null, Action<string> debugLogCallback = null)
        {
            _state = new DeviceState<TState>(callbacks, debugLogCallback);
            _shadowState = new DeviceState<TState>();
        }

        /// <summary>
        /// Reads a value from a register.
        /// </summary>
        /// <param name="offset">Register offset in bytes</param>
        /// <returns>Value stored on the register</returns>
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public int Read(int offset)
        {
            return _state.Read(offset);
        }

        /// <summary>
        /// Writes a value to a register.
        /// </summary>
        /// <param name="offset">Register offset in bytes</param>
        /// <param name="value">Value to be written</param>
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public void Write(int offset, int value)
        {
            WriteWithRedundancyCheck(offset, value, out _);
        }

        /// <summary>
        /// Writes a value to a register, returning a value indicating if <paramref name="value"/>
        /// is different from the current value on the register.
        /// </summary>
        /// <param name="offset">Register offset in bytes</param>
        /// <param name="value">Value to be written</param>
        /// <param name="changed">True if the value was changed, false otherwise</param>
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public void WriteWithRedundancyCheck(int offset, int value, out bool changed)
        {
            var shadowRamControl = _state.State.SetMmeShadowRamControlMode;
            if (shadowRamControl == SetMmeShadowRamControlMode.MethodPassthrough || offset < 0x200)
            {
                _state.WriteWithRedundancyCheck(offset, value, out changed);
            }
            else if (shadowRamControl == SetMmeShadowRamControlMode.MethodTrack ||
                     shadowRamControl == SetMmeShadowRamControlMode.MethodTrackWithFilter)
            {
                _shadowState.Write(offset, value);
                _state.WriteWithRedundancyCheck(offset, value, out changed);
            }
            else /* if (shadowRamControl == SetMmeShadowRamControlMode.MethodReplay) */
            {
                Debug.Assert(shadowRamControl == SetMmeShadowRamControlMode.MethodReplay);
                _state.WriteWithRedundancyCheck(offset, _shadowState.Read(offset), out changed);
            }
        }
    }
}