-
Notifications
You must be signed in to change notification settings - Fork 375
/
EmuLuaLibrary.Memory.cs
369 lines (307 loc) · 10.7 KB
/
EmuLuaLibrary.Memory.cs
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
using System;
using System.ComponentModel;
using LuaInterface;
using BizHawk.Emulation.Common;
using BizHawk.Emulation.Common.IEmulatorExtensions;
namespace BizHawk.Client.Common
{
[Description("These functions behavior identically to the mainmemory functions but the user can set the memory domain to read and write from. The default domain is main memory. Use getcurrentmemorydomain(), and usememorydomain() to control which domain is used. Each core has its own set of valid memory domains. Use getmemorydomainlist() to get a list of memory domains for the current core loaded.")]
public sealed class MemoryLuaLibrary : LuaMemoryBase
{
//private int _currentMemoryDomain; // Main memory by default probably (index 0 is currently always main memory but may never be)
private MemoryDomain _currentMemoryDomain;
public MemoryLuaLibrary(Lua lua)
: base(lua)
{
if (MemoryDomainCore != null)
{
_currentMemoryDomain = MemoryDomainCore.MainMemory;
}
}
public MemoryLuaLibrary(Lua lua, Action<string> logOutputCallback)
: base(lua, logOutputCallback)
{
if (MemoryDomainCore != null)
{
_currentMemoryDomain = MemoryDomainCore.MainMemory;
}
}
public override string Name { get { return "memory"; } }
protected override MemoryDomain Domain
{
get
{
if (MemoryDomainCore != null)
{
if (_currentMemoryDomain == null)
{
_currentMemoryDomain = MemoryDomainCore.MainMemory;
}
return _currentMemoryDomain;
}
else
{
var error = string.Format("Error: {0} does not implement memory domains", Emulator.Attributes().CoreName);
Log(error);
throw new NotImplementedException(error);
}
}
}
#region Unique Library Methods
[LuaMethodAttributes(
"getmemorydomainlist",
"Returns a string of the memory domains for the loaded platform core. List will be a single string delimited by line feeds"
)]
public LuaTable GetMemoryDomainList()
{
var table = Lua.NewTable();
int i = 0;
foreach (var domain in DomainList)
{
table[i] = domain.Name;
i++;
}
return table;
}
[LuaMethodAttributes(
"getcurrentmemorydomain",
"Returns a string name of the current memory domain selected by Lua. The default is Main memory"
)]
public string GetCurrentMemoryDomain()
{
return Domain.Name;
}
[LuaMethodAttributes(
"getcurrentmemorydomainsize",
"Returns the number of bytes of the current memory domain selected by Lua. The default is Main memory"
)]
public uint GetCurrentMemoryDomainSize()
{
return (uint)Domain.Size;
}
[LuaMethodAttributes(
"usememorydomain",
"Attempts to set the current memory domain to the given domain. If the name does not match a valid memory domain, the function returns false, else it returns true"
)]
public bool UseMemoryDomain(string domain)
{
try
{
if (DomainList[domain] != null)
{
_currentMemoryDomain = DomainList[domain];
return true;
}
else
{
Log(string.Format("Unable to find domain: {0}", domain));
return false;
}
}
catch // Just in case
{
Log(string.Format("Unable to find domain: {0}", domain));
}
return false;
}
#endregion
#region Common Special and Legacy Methods
[LuaMethodAttributes(
"readbyte",
"gets the value from the given address as an unsigned byte"
)]
public uint ReadByte(int addr, string domain = null)
{
return ReadUnsignedByte(addr, domain);
}
[LuaMethodAttributes(
"writebyte",
"Writes the given value to the given address as an unsigned byte"
)]
public void WriteByte(int addr, uint value, string domain = null)
{
WriteUnsignedByte(addr, value, domain);
}
[LuaMethodAttributes(
"readbyterange",
"Reads the address range that starts from address, and is length long. Returns the result into a table of key value pairs (where the address is the key)."
)]
public new LuaTable ReadByteRange(int addr, int length, string domain = null)
{
return base.ReadByteRange(addr, length, domain);
}
[LuaMethodAttributes(
"writebyterange",
"Writes the given values to the given addresses as unsigned bytes"
)]
public new void WriteByteRange(LuaTable memoryblock, string domain = null)
{
base.WriteByteRange(memoryblock, domain);
}
[LuaMethodAttributes(
"readfloat",
"Reads the given address as a 32-bit float value from the main memory domain with th e given endian"
)]
public new float ReadFloat(int addr, bool bigendian, string domain = null)
{
return base.ReadFloat(addr, bigendian, domain);
}
[LuaMethodAttributes(
"writefloat",
"Writes the given 32-bit float value to the given address and endian"
)]
public new void WriteFloat(int addr, double value, bool bigendian, string domain = null)
{
base.WriteFloat(addr, value, bigendian, domain);
}
#endregion
#region 1 Byte
[LuaMethodAttributes("read_s8", "read signed byte")]
public int ReadS8(int addr, string domain = null)
{
return (sbyte)ReadUnsignedByte(addr, domain);
}
[LuaMethodAttributes("write_s8", "write signed byte")]
public void WriteS8(int addr, uint value, string domain = null)
{
WriteUnsignedByte(addr, value, domain);
}
[LuaMethodAttributes("read_u8", "read unsigned byte")]
public uint ReadU8(int addr, string domain = null)
{
return ReadUnsignedByte(addr, domain);
}
[LuaMethodAttributes("write_u8", "write unsigned byte")]
public void WriteU8(int addr, uint value, string domain = null)
{
WriteUnsignedByte(addr, value, domain);
}
#endregion
#region 2 Byte
[LuaMethodAttributes("read_s16_le", "read signed 2 byte value, little endian")]
public int ReadS16Little(int addr, string domain = null)
{
return ReadSignedLittleCore(addr, 2, domain);
}
[LuaMethodAttributes("write_s16_le", "write signed 2 byte value, little endian")]
public void WriteS16Little(int addr, int value, string domain = null)
{
WriteSignedLittle(addr, value, 2, domain);
}
[LuaMethodAttributes("read_s16_be", "read signed 2 byte value, big endian")]
public int ReadS16Big(int addr, string domain = null)
{
return ReadSignedBig(addr, 2, domain);
}
[LuaMethodAttributes("write_s16_be", "write signed 2 byte value, big endian")]
public void WriteS16Big(int addr, int value, string domain = null)
{
WriteSignedBig(addr, value, 2, domain);
}
[LuaMethodAttributes("read_u16_le", "read unsigned 2 byte value, little endian")]
public uint ReadU16Little(int addr, string domain = null)
{
return ReadUnsignedLittle(addr, 2, domain);
}
[LuaMethodAttributes("write_u16_le", "write unsigned 2 byte value, little endian")]
public void WriteU16Little(int addr, uint value, string domain = null)
{
WriteUnsignedLittle(addr, value, 2, domain);
}
[LuaMethodAttributes("read_u16_be", "read unsigned 2 byte value, big endian")]
public uint ReadU16Big(int addr, string domain = null)
{
return ReadUnsignedBig(addr, 2, domain);
}
[LuaMethodAttributes("write_u16_be", "write unsigned 2 byte value, big endian")]
public void WriteU16Big(int addr, uint value, string domain = null)
{
WriteUnsignedBig(addr, value, 2, domain);
}
#endregion
#region 3 Byte
[LuaMethodAttributes("read_s24_le", "read signed 24 bit value, little endian")]
public int ReadS24Little(int addr, string domain = null)
{
return ReadSignedLittleCore(addr, 3, domain);
}
[LuaMethodAttributes("write_s24_le", "write signed 24 bit value, little endian")]
public void WriteS24Little(int addr, int value, string domain = null)
{
WriteSignedLittle(addr, value, 3, domain);
}
[LuaMethodAttributes("read_s24_be", "read signed 24 bit value, big endian")]
public int ReadS24Big(int addr, string domain = null)
{
return ReadSignedBig(addr, 3, domain);
}
[LuaMethodAttributes("write_s24_be", "write signed 24 bit value, big endian")]
public void WriteS24Big(int addr, int value, string domain = null)
{
WriteSignedBig(addr, value, 3, domain);
}
[LuaMethodAttributes("read_u24_le", "read unsigned 24 bit value, little endian")]
public uint ReadU24Little(int addr, string domain = null)
{
return ReadUnsignedLittle(addr, 3, domain);
}
[LuaMethodAttributes("write_u24_le", "write unsigned 24 bit value, little endian")]
public void WriteU24Little(int addr, uint value, string domain = null)
{
WriteUnsignedLittle(addr, value, 3, domain);
}
[LuaMethodAttributes("read_u24_be", "read unsigned 24 bit value, big endian")]
public uint ReadU24Big(int addr, string domain = null)
{
return ReadUnsignedBig(addr, 3, domain);
}
[LuaMethodAttributes("write_u24_be", "write unsigned 24 bit value, big endian")]
public void WriteU24Big(int addr, uint value, string domain = null)
{
WriteUnsignedBig(addr, value, 3, domain);
}
#endregion
#region 4 Byte
[LuaMethodAttributes("read_s32_le", "read signed 4 byte value, little endian")]
public int ReadS32Little(int addr, string domain = null)
{
return ReadSignedLittleCore(addr, 4, domain);
}
[LuaMethodAttributes("write_s32_le", "write signed 4 byte value, little endian")]
public void WriteS32Little(int addr, int value, string domain = null)
{
WriteSignedLittle(addr, value, 4, domain);
}
[LuaMethodAttributes("read_s32_be", "read signed 4 byte value, big endian")]
public int ReadS32Big(int addr, string domain = null)
{
return ReadSignedBig(addr, 4, domain);
}
[LuaMethodAttributes("write_s32_be", "write signed 4 byte value, big endian")]
public void WriteS32Big(int addr, int value, string domain = null)
{
WriteSignedBig(addr, value, 4, domain);
}
[LuaMethodAttributes("read_u32_le", "read unsigned 4 byte value, little endian")]
public uint ReadU32Little(int addr, string domain = null)
{
return ReadUnsignedLittle(addr, 4, domain);
}
[LuaMethodAttributes("write_u32_le", "write unsigned 4 byte value, little endian")]
public void WriteU32Little(int addr, uint value, string domain = null)
{
WriteUnsignedLittle(addr, value, 4, domain);
}
[LuaMethodAttributes("read_u32_be", "read unsigned 4 byte value, big endian")]
public uint ReadU32Big(int addr, string domain = null)
{
return ReadUnsignedBig(addr, 4, domain);
}
[LuaMethodAttributes("write_u32_be", "write unsigned 4 byte value, big endian")]
public void WriteU32Big(int addr, uint value, string domain = null)
{
WriteUnsignedBig(addr, value, 4, domain);
}
#endregion
}
}