2020-07-12 05:07:01 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.Runtime.CompilerServices;
|
2021-07-11 22:20:40 +02:00
|
|
|
|
using System.Runtime.InteropServices;
|
2020-07-12 05:07:01 +02:00
|
|
|
|
|
|
|
|
|
namespace Ryujinx.Graphics.Device
|
|
|
|
|
{
|
|
|
|
|
public class DeviceState<TState> : IDeviceState where TState : unmanaged
|
|
|
|
|
{
|
|
|
|
|
private const int RegisterSize = sizeof(int);
|
|
|
|
|
|
|
|
|
|
public TState State;
|
|
|
|
|
|
2021-07-11 22:20:40 +02:00
|
|
|
|
private uint Size => (uint)(Unsafe.SizeOf<TState>() + RegisterSize - 1) / RegisterSize;
|
2020-07-12 05:07:01 +02:00
|
|
|
|
|
2021-07-11 22:20:40 +02:00
|
|
|
|
private readonly Func<int>[] _readCallbacks;
|
|
|
|
|
private readonly Action<int>[] _writeCallbacks;
|
2020-07-12 05:07:01 +02:00
|
|
|
|
|
2021-07-11 22:20:40 +02:00
|
|
|
|
private readonly Dictionary<uint, string> _fieldNamesForDebug;
|
2021-07-08 01:56:06 +02:00
|
|
|
|
private readonly Action<string> _debugLogCallback;
|
|
|
|
|
|
|
|
|
|
public DeviceState(IReadOnlyDictionary<string, RwCallback> callbacks = null, Action<string> debugLogCallback = null)
|
2020-07-12 05:07:01 +02:00
|
|
|
|
{
|
2021-07-11 22:20:40 +02:00
|
|
|
|
_readCallbacks = new Func<int>[Size];
|
|
|
|
|
_writeCallbacks = new Action<int>[Size];
|
2020-07-12 05:07:01 +02:00
|
|
|
|
|
2021-07-08 01:56:06 +02:00
|
|
|
|
if (debugLogCallback != null)
|
|
|
|
|
{
|
2021-07-11 22:20:40 +02:00
|
|
|
|
_fieldNamesForDebug = new Dictionary<uint, string>();
|
2021-07-08 01:56:06 +02:00
|
|
|
|
_debugLogCallback = debugLogCallback;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-12 05:07:01 +02:00
|
|
|
|
var fields = typeof(TState).GetFields();
|
|
|
|
|
int offset = 0;
|
|
|
|
|
|
|
|
|
|
for (int fieldIndex = 0; fieldIndex < fields.Length; fieldIndex++)
|
|
|
|
|
{
|
|
|
|
|
var field = fields[fieldIndex];
|
|
|
|
|
|
|
|
|
|
int sizeOfField = SizeCalculator.SizeOf(field.FieldType);
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < ((sizeOfField + 3) & ~3); i += 4)
|
|
|
|
|
{
|
2021-07-11 22:20:40 +02:00
|
|
|
|
int index = (offset + i) / RegisterSize;
|
2020-07-12 05:07:01 +02:00
|
|
|
|
|
2021-07-11 22:20:40 +02:00
|
|
|
|
if (callbacks != null && callbacks.TryGetValue(field.Name, out var cb))
|
2020-07-12 05:07:01 +02:00
|
|
|
|
{
|
2021-07-11 22:20:40 +02:00
|
|
|
|
if (cb.Read != null)
|
|
|
|
|
{
|
|
|
|
|
_readCallbacks[index] = cb.Read;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (cb.Write != null)
|
|
|
|
|
{
|
|
|
|
|
_writeCallbacks[index] = cb.Write;
|
|
|
|
|
}
|
2020-07-12 05:07:01 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-08 01:56:06 +02:00
|
|
|
|
if (debugLogCallback != null)
|
|
|
|
|
{
|
2021-07-11 22:20:40 +02:00
|
|
|
|
_fieldNamesForDebug.Add((uint)offset, field.Name);
|
2021-07-08 01:56:06 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-12 05:07:01 +02:00
|
|
|
|
offset += sizeOfField;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Debug.Assert(offset == Unsafe.SizeOf<TState>());
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-11 22:20:40 +02:00
|
|
|
|
public int Read(int offset)
|
2020-07-12 05:07:01 +02:00
|
|
|
|
{
|
2021-07-11 22:20:40 +02:00
|
|
|
|
uint index = (uint)offset / RegisterSize;
|
|
|
|
|
|
|
|
|
|
if (index < Size)
|
2020-07-12 05:07:01 +02:00
|
|
|
|
{
|
2021-07-11 22:20:40 +02:00
|
|
|
|
uint alignedOffset = index * RegisterSize;
|
2020-07-12 05:07:01 +02:00
|
|
|
|
|
2021-07-11 22:20:40 +02:00
|
|
|
|
var readCallback = Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(_readCallbacks), (IntPtr)index);
|
|
|
|
|
if (readCallback != null)
|
2020-07-12 05:07:01 +02:00
|
|
|
|
{
|
2021-07-11 22:20:40 +02:00
|
|
|
|
return readCallback();
|
2020-07-12 05:07:01 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2021-07-11 22:20:40 +02:00
|
|
|
|
return GetRefUnchecked<int>(alignedOffset);
|
2020-07-12 05:07:01 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-11 22:20:40 +02:00
|
|
|
|
public void Write(int offset, int data)
|
2020-07-12 05:07:01 +02:00
|
|
|
|
{
|
2021-07-11 22:20:40 +02:00
|
|
|
|
uint index = (uint)offset / RegisterSize;
|
|
|
|
|
|
|
|
|
|
if (index < Size)
|
2020-07-12 05:07:01 +02:00
|
|
|
|
{
|
2021-07-11 22:20:40 +02:00
|
|
|
|
uint alignedOffset = index * RegisterSize;
|
|
|
|
|
DebugWrite(alignedOffset, data);
|
2020-07-12 05:07:01 +02:00
|
|
|
|
|
2021-07-11 22:20:40 +02:00
|
|
|
|
GetRefIntAlignedUncheck(index) = data;
|
2021-07-08 01:56:06 +02:00
|
|
|
|
|
2021-07-11 22:20:40 +02:00
|
|
|
|
Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(_writeCallbacks), (IntPtr)index)?.Invoke(data);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-07-24 04:53:25 +02:00
|
|
|
|
|
2021-07-11 22:20:40 +02:00
|
|
|
|
public void WriteWithRedundancyCheck(int offset, int data, out bool changed)
|
|
|
|
|
{
|
|
|
|
|
uint index = (uint)offset / RegisterSize;
|
|
|
|
|
|
|
|
|
|
if (index < Size)
|
|
|
|
|
{
|
|
|
|
|
uint alignedOffset = index * RegisterSize;
|
|
|
|
|
DebugWrite(alignedOffset, data);
|
|
|
|
|
|
|
|
|
|
ref var storage = ref GetRefIntAlignedUncheck(index);
|
|
|
|
|
changed = storage != data;
|
|
|
|
|
storage = data;
|
|
|
|
|
|
|
|
|
|
Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(_writeCallbacks), (IntPtr)index)?.Invoke(data);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
changed = false;
|
2020-07-12 05:07:01 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-11 22:20:40 +02:00
|
|
|
|
[Conditional("DEBUG")]
|
|
|
|
|
private void DebugWrite(uint alignedOffset, int data)
|
2020-07-12 05:07:01 +02:00
|
|
|
|
{
|
2021-07-11 22:20:40 +02:00
|
|
|
|
if (_fieldNamesForDebug != null && _fieldNamesForDebug.TryGetValue(alignedOffset, out string fieldName))
|
|
|
|
|
{
|
|
|
|
|
_debugLogCallback($"{typeof(TState).Name}.{fieldName} = 0x{data:X}");
|
|
|
|
|
}
|
2020-07-12 05:07:01 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ref T GetRef<T>(int offset) where T : unmanaged
|
|
|
|
|
{
|
|
|
|
|
if ((uint)(offset + Unsafe.SizeOf<T>()) > Unsafe.SizeOf<TState>())
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(offset));
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-11 22:20:40 +02:00
|
|
|
|
return ref GetRefUnchecked<T>((uint)offset);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
private ref T GetRefUnchecked<T>(uint offset) where T : unmanaged
|
|
|
|
|
{
|
2020-07-12 05:07:01 +02:00
|
|
|
|
return ref Unsafe.As<TState, T>(ref Unsafe.AddByteOffset(ref State, (IntPtr)offset));
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-11 22:20:40 +02:00
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
private ref int GetRefIntAlignedUncheck(ulong index)
|
2020-07-12 05:07:01 +02:00
|
|
|
|
{
|
2021-07-11 22:20:40 +02:00
|
|
|
|
return ref Unsafe.Add(ref Unsafe.As<TState, int>(ref State), (IntPtr)index);
|
2020-07-12 05:07:01 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|