-
Notifications
You must be signed in to change notification settings - Fork 449
Expand file tree
/
Copy pathMemoryDomain.cs
More file actions
191 lines (160 loc) · 5.3 KB
/
MemoryDomain.cs
File metadata and controls
191 lines (160 loc) · 5.3 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#nullable disable
using System.Buffers.Binary;
using BizHawk.Common;
namespace BizHawk.Emulation.Common
{
/// <summary>
/// A memory region and the functionality to read/write from it
/// as required by the IMemoryDomains service.
/// </summary>
/// <seealso cref="IMemoryDomains" />
public abstract class MemoryDomain : IMonitor
{
public enum Endian
{
Big,
Little,
Unknown,
}
private const string ERR_MSG_BUFFER_WRONG_SIZE = "Invalid length of values array";
public string Name { get; protected set; }
public long Size { get; protected set; }
public int WordSize { get; protected set; }
public Endian EndianType { get; protected set; }
public bool Writable { get; protected set; }
public abstract byte PeekByte(long addr);
public abstract void PokeByte(long addr, byte val);
public override string ToString() => Name;
public virtual ushort PeekUshort(long addr, bool bigEndian)
{
if (bigEndian)
{
return (ushort)((PeekByte(addr) << 8) | PeekByte(addr + 1));
}
return (ushort)(PeekByte(addr) | (PeekByte(addr + 1) << 8));
}
public virtual uint PeekUint(long addr, bool bigEndian)
{
ReadOnlySpan<byte> scratch = stackalloc byte[]
{
PeekByte(addr),
PeekByte(addr + 1),
PeekByte(addr + 2),
PeekByte(addr + 3),
};
return bigEndian
? BinaryPrimitives.ReadUInt32BigEndian(scratch)
: BinaryPrimitives.ReadUInt32LittleEndian(scratch);
}
public virtual void PokeUshort(long addr, ushort val, bool bigEndian)
{
if (bigEndian)
{
PokeByte(addr + 0, (byte)(val >> 8));
PokeByte(addr + 1, (byte)val);
}
else
{
PokeByte(addr + 0, (byte)val);
PokeByte(addr + 1, (byte)(val >> 8));
}
}
public virtual void PokeUint(long addr, uint val, bool bigEndian)
{
Span<byte> scratch = stackalloc byte[4];
if (bigEndian) BinaryPrimitives.WriteUInt32BigEndian(scratch, val);
else BinaryPrimitives.WriteUInt32LittleEndian(scratch, val);
PokeByte(addr, scratch[0]);
PokeByte(addr + 1, scratch[1]);
PokeByte(addr + 2, scratch[2]);
PokeByte(addr + 3, scratch[3]);
}
public virtual void BulkPeekByte(Range<long> addresses, byte[] values)
{
if (addresses is null) throw new ArgumentNullException(paramName: nameof(addresses));
if (values is null) throw new ArgumentNullException(paramName: nameof(values));
if ((long) addresses.Count() > values.Length) throw new InvalidOperationException(ERR_MSG_BUFFER_WRONG_SIZE);
using (this.EnterExit())
{
for (var i = addresses.Start; i <= addresses.EndInclusive; i++)
{
values[i - addresses.Start] = PeekByte(i);
}
}
}
public virtual void BulkPokeByte(long startAddress, Span<byte> values)
{
using (this.EnterExit())
{
long address = startAddress;
for (var i = 0; i < values.Length; i++, address++)
{
PokeByte(address, values[i]);
}
}
}
public virtual void BulkPeekUshort(Range<long> addresses, bool bigEndian, ushort[] values)
{
if (addresses is null) throw new ArgumentNullException(paramName: nameof(addresses));
if (values is null) throw new ArgumentNullException(paramName: nameof(values));
var start = addresses.Start;
var end = addresses.EndInclusive + 1;
if (values.LongLength * sizeof(ushort) < end - start) throw new InvalidOperationException(ERR_MSG_BUFFER_WRONG_SIZE);
using (this.EnterExit())
{
for (var i = 0; i < values.Length; i++, start += sizeof(ushort))
{
values[i] = PeekUshort(start, bigEndian);
}
}
}
public virtual void BulkPokeUshort(long startAddress, bool bigEndian, Span<ushort> values)
{
using (this.EnterExit())
{
long address = startAddress;
for (var i = 0; i < values.Length; i++, address += sizeof(ushort))
{
PokeUshort(address, values[i], bigEndian);
}
}
}
public virtual void BulkPeekUint(Range<long> addresses, bool bigEndian, uint[] values)
{
if (addresses is null) throw new ArgumentNullException(paramName: nameof(addresses));
if (values is null) throw new ArgumentNullException(paramName: nameof(values));
var start = addresses.Start;
var end = addresses.EndInclusive + 1;
if (values.LongLength * sizeof(uint) < end - start) throw new InvalidOperationException(ERR_MSG_BUFFER_WRONG_SIZE);
using (this.EnterExit())
{
for (var i = 0; i < values.Length; i++, start += sizeof(uint))
{
values[i] = PeekUint(start, bigEndian);
}
}
}
public virtual void BulkPokeUint(long startAddress, bool bigEndian, Span<uint> values)
{
using (this.EnterExit())
{
long address = startAddress;
for (var i = 0; i < values.Length; i++, address += sizeof(uint))
{
PokeUint(address, values[i], bigEndian);
}
}
}
public virtual void SendCheatToCore(int addr, byte value, int compare, int compare_type) { }
/// <summary>
/// only use this if you are expecting to do a lot of peeks/pokes
/// no-op if the domain has no monitor
/// </summary>
public virtual void Enter() { }
/// <summary>
/// only use this if you are expecting to do a lot of peeks/pokes
/// no-op if the domain has no monitor
/// </summary>
public virtual void Exit() { }
}
}