Lua - combine common code in memory and mainmemory libraries into a base class, apply range checking to all methods, add memory.getsize() to match mainmemory method, fix a name bug in a memory 32bit method, clean up a bunch of things too

This commit is contained in:
adelikat 2014-05-23 23:19:20 +00:00
parent 52bceeeee5
commit 667a96a277
4 changed files with 551 additions and 693 deletions

View File

@ -127,6 +127,7 @@
<Compile Include="lua\LuaFileList.cs" />
<Compile Include="lua\LuaFunctionList.cs" />
<Compile Include="lua\LuaLibraryBase.cs" />
<Compile Include="lua\LuaMemoryBase.cs" />
<Compile Include="lua\NamedLuaFunction.cs" />
<Compile Include="movie\HeaderKeys.cs" />
<Compile Include="movie\IMovie.cs" />

View File

@ -1,10 +1,10 @@
using System;
using LuaInterface;
using BizHawk.Emulation.Common;
namespace BizHawk.Client.Common
{
// TODO: this needs a major refactor, as well as MemoryLuaLibrary, and this shoudl inherit memorylua library and extend it
public class MainMemoryLuaLibrary : LuaLibraryBase
public class MainMemoryLuaLibrary : LuaMemoryBase
{
public MainMemoryLuaLibrary(Lua lua)
: base(lua) { }
@ -14,85 +14,12 @@ namespace BizHawk.Client.Common
public override string Name { get { return "mainmemory"; } }
#region Main Memory Library Helpers
private static int U2S(uint u, int size)
protected override MemoryDomain Domain
{
var s = (int)u;
s <<= 8 * (4 - size);
s >>= 8 * (4 - size);
return s;
get { return Global.Emulator.MemoryDomains.MainMemory; }
}
private static int ReadSignedLittleCore(int addr, int size)
{
return U2S(ReadSignedLittle(addr, size), size);
}
private static uint ReadSignedLittle(int addr, int size)
{
uint v = 0;
for (var i = 0; i < size; ++i)
{
v |= ReadUnsignedByte(addr + i) << (8 * i);
}
return v;
}
private static int ReadSignedBig(int addr, int size)
{
return U2S(ReadUnsignedBig(addr, size), size);
}
private static uint ReadUnsignedBig(int addr, int size)
{
uint v = 0;
for (var i = 0; i < size; ++i)
{
v |= ReadUnsignedByte(addr + i) << (8 * (size - 1 - i));
}
return v;
}
private static void WriteSignedLittle(int addr, int v, int size)
{
WriteUnsignedLittle(addr, (uint)v, size);
}
private static void WriteUnsignedLittle(int addr, uint v, int size)
{
for (var i = 0; i < size; ++i)
{
WriteUnsignedByte(addr + i, (v >> (8 * i)) & 0xFF);
}
}
private static void WriteSignedBig(int addr, int v, int size)
{
WriteUnsignedBig(addr, (uint)v, size);
}
private static void WriteUnsignedBig(int addr, uint v, int size)
{
for (var i = 0; i < size; ++i)
{
WriteUnsignedByte(addr + i, (v >> (8 * (size - 1 - i))) & 0xFF);
}
}
private static uint ReadUnsignedByte(int addr)
{
return Global.Emulator.MemoryDomains.MainMemory.PeekByte(addr);
}
private static void WriteUnsignedByte(int addr, uint v)
{
Global.Emulator.MemoryDomains.MainMemory.PokeByte(addr, (byte)v);
}
#endregion
#region Unique Library Methods
[LuaMethodAttributes(
"getname",
@ -100,9 +27,22 @@ namespace BizHawk.Client.Common
)]
public string GetName()
{
return Global.Emulator.MemoryDomains.MainMemory.Name;
return Domain.Name;
}
[LuaMethodAttributes(
"getcurrentmemorydomainsize",
"Returns the number of bytes of the domain defined as main memory"
)]
public int GetSize()
{
return Domain.Size;
}
#endregion
#region Common Special and Legacy Methods
[LuaMethodAttributes(
"readbyte", "gets the value from the given address as an unsigned byte"
)]
@ -111,45 +51,6 @@ namespace BizHawk.Client.Common
return ReadUnsignedByte(addr);
}
[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 LuaTable ReadByteRange(int addr, int length)
{
var lastAddr = length + addr;
var table = Lua.NewTable();
if (lastAddr < Global.Emulator.MemoryDomains.MainMemory.Size)
{
for (var i = addr; i <= lastAddr; i++)
{
var a = string.Format("{0:X2}", i);
var v = Global.Emulator.MemoryDomains.MainMemory.PeekByte(i);
var vs = string.Format("{0:X2}", (int)v);
table[a] = vs;
}
}
else
{
Log("Warning: Attempted read " + lastAddr + " outside memory domain size of " +
Global.Emulator.MemoryDomains.MainMemory.Size +
" in mainmemory.readbyterange()");
}
return table;
}
[LuaMethodAttributes(
"readfloat",
"Reads the given address as a 32-bit float value from the main memory domain with th e given endian"
)]
public float ReadFloat(int addr, bool bigendian)
{
var val = Global.Emulator.MemoryDomains.MainMemory.PeekDWord(addr, bigendian);
var bytes = BitConverter.GetBytes(val);
return BitConverter.ToSingle(bytes, 0);
}
[LuaMethodAttributes(
"writebyte",
"Writes the given value to the given address as an unsigned byte"
@ -159,292 +60,226 @@ namespace BizHawk.Client.Common
WriteUnsignedByte(addr, value);
}
[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)
{
return base.ReadByteRange(addr, length);
}
[LuaMethodAttributes(
"writebyterange",
"Writes the given values to the given addresses as unsigned bytes"
)]
public void WriteByteRange(LuaTable memoryblock)
public new void WriteByteRange(LuaTable memoryblock)
{
foreach (var address in memoryblock.Keys)
{
var addr = LuaInt(address);
if (addr < Global.Emulator.MemoryDomains.MainMemory.Size)
{
Global.Emulator.MemoryDomains.MainMemory.PokeByte(
addr,
(byte)LuaInt(memoryblock[address]));
}
else
{
Log("Warning: Attempted read " + addr + " outside memory domain size of " +
Global.Emulator.MemoryDomains.MainMemory.Size +
" in mainmemory.writebyterange()");
}
}
base.WriteByteRange(memoryblock);
}
[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)
{
return base.ReadFloat(addr, bigendian);
}
[LuaMethodAttributes(
"writefloat",
"Writes the given 32-bit float value to the given address and endian"
)]
public void WriteFloat(int address, double value, bool bigendian)
public new void WriteFloat(int addr, double value, bool bigendian)
{
var dv = (float)value;
var bytes = BitConverter.GetBytes(dv);
var v = BitConverter.ToUInt32(bytes, 0);
Global.Emulator.MemoryDomains.MainMemory.PokeDWord(address, v, bigendian);
base.WriteFloat(addr, value, bigendian);
}
[LuaMethodAttributes(
"read_s8",
"read signed byte"
)]
#endregion
#region 1 Byte
[LuaMethodAttributes("read_s8", "read signed byte")]
public int ReadS8(int addr)
{
return (sbyte)ReadUnsignedByte(addr);
}
[LuaMethodAttributes(
"read_u8",
"read unsigned byte"
)]
public uint ReadU8(int addr)
{
return ReadUnsignedByte(addr);
}
[LuaMethodAttributes(
"read_s16_le",
"read signed 2 byte value, little endian"
)]
public int ReadS16Little(int addr)
{
return ReadSignedLittleCore(addr, 2);
}
[LuaMethodAttributes(
"read_s24_le",
"read signed 24 bit value, little endian"
)]
public int ReadS24Little(int addr)
{
return ReadSignedLittleCore(addr, 3);
}
[LuaMethodAttributes(
"read_s32_le",
"read signed 4 byte value, little endian"
)]
public int ReadS32Little(int addr)
{
return ReadSignedLittleCore(addr, 4);
}
[LuaMethodAttributes(
"read_u16_le",
"read unsigned 2 byte value, little endian"
)]
public uint ReadU16Little(int addr)
{
return ReadSignedLittle(addr, 2);
}
[LuaMethodAttributes(
"read_u24_le",
"read unsigned 24 bit value, little endian"
)]
public uint ReadU24Little(int addr)
{
return ReadSignedLittle(addr, 3);
}
[LuaMethodAttributes(
"read_u32_le",
"read unsigned 4 byte value, little endian"
)]
public uint ReadU32Little(int addr)
{
return ReadSignedLittle(addr, 4);
}
[LuaMethodAttributes(
"read_s16_be",
"read signed 2 byte value, big endian"
)]
public int ReadS16Big(int addr)
{
return ReadSignedBig(addr, 2);
}
[LuaMethodAttributes(
"read_s24_be",
"read signed 24 bit value, big endian"
)]
public int ReadS24Big(int addr)
{
return ReadSignedBig(addr, 3);
}
[LuaMethodAttributes(
"read_s32_be",
"read signed 4 byte value, big endian"
)]
public int ReadS32Big(int addr)
{
return ReadSignedBig(addr, 4);
}
[LuaMethodAttributes(
"read_u16_be",
"read unsigned 2 byte value, big endian"
)]
public uint ReadU16Big(int addr)
{
return ReadUnsignedBig(addr, 2);
}
[LuaMethodAttributes(
"read_u24_be",
"read unsigned 24 bit value, big endian"
)]
public uint ReadU24Big(int addr)
{
return ReadUnsignedBig(addr, 3);
}
[LuaMethodAttributes(
"read_u32_be",
"read unsigned 4 byte value, big endian"
)]
public uint ReadU32Big(int addr)
{
return ReadUnsignedBig(addr, 4);
}
[LuaMethodAttributes(
"write_s8",
"write signed byte"
)]
[LuaMethodAttributes("write_s8", "write signed byte")]
public void WriteS8(int addr, uint value)
{
WriteUnsignedByte(addr, value);
}
[LuaMethodAttributes(
"write_u8",
"write unsigned byte"
)]
[LuaMethodAttributes("read_u8", "read unsigned byte")]
public uint ReadU8(int addr)
{
return ReadUnsignedByte(addr);
}
[LuaMethodAttributes("write_u8", "write unsigned byte")]
public void WriteU8(int addr, uint value)
{
WriteUnsignedByte(addr, value);
}
[LuaMethodAttributes(
"write_s16_le",
"write signed 2 byte value, little endian"
)]
#endregion
#region 2 Byte
[LuaMethodAttributes("read_s16_le", "read signed 2 byte value, little endian")]
public int ReadS16Little(int addr)
{
return ReadSignedLittleCore(addr, 2);
}
[LuaMethodAttributes("write_s16_le", "write signed 2 byte value, little endian")]
public void WriteS16Little(int addr, int value)
{
WriteSignedLittle(addr, value, 2);
}
[LuaMethodAttributes(
"write_s24_le",
"write signed 24 bit value, little endian"
)]
public void WriteS24Little(int addr, int value)
[LuaMethodAttributes("read_s16_be", "read signed 2 byte value, big endian")]
public int ReadS16Big(int addr)
{
WriteSignedLittle(addr, value, 3);
return ReadSignedBig(addr, 2);
}
[LuaMethodAttributes(
"write_s32_le",
"write signed 4 byte value, little endian"
)]
public void WriteS32Little(int addr, int value)
{
WriteSignedLittle(addr, value, 4);
}
[LuaMethodAttributes(
"write_u16_le",
"write unsigned 2 byte value, little endian"
)]
public void WriteU16Little(int addr, uint value)
{
WriteUnsignedLittle(addr, value, 2);
}
[LuaMethodAttributes(
"write_u24_le",
"write unsigned 24 bit value, little endian"
)]
public void WriteU24Little(int addr, uint value)
{
WriteUnsignedLittle(addr, value, 3);
}
[LuaMethodAttributes(
"write_u32_le",
"write unsigned 4 byte value, little endian"
)]
public void WriteU32Little(int addr, uint value)
{
WriteUnsignedLittle(addr, value, 4);
}
[LuaMethodAttributes(
"write_s16_be",
"write signed 2 byte value, big endian"
)]
[LuaMethodAttributes("write_s16_be", "write signed 2 byte value, big endian")]
public void WriteS16Big(int addr, int value)
{
WriteSignedBig(addr, value, 2);
}
[LuaMethodAttributes(
"write_s24_be",
"write signed 24 bit value, big endian"
)]
public void WriteS24Big(int addr, int value)
[LuaMethodAttributes("read_u16_le", "read unsigned 2 byte value, little endian")]
public uint ReadU16Little(int addr)
{
WriteSignedBig(addr, value, 3);
return ReadSignedLittle(addr, 2);
}
[LuaMethodAttributes(
"write_s32_be",
"write signed 4 byte value, big endian"
)]
public void WriteS32Big(int addr, int value)
[LuaMethodAttributes("write_u16_le", "write unsigned 2 byte value, little endian")]
public void WriteU16Little(int addr, uint value)
{
WriteSignedBig(addr, value, 4);
WriteUnsignedLittle(addr, value, 2);
}
[LuaMethodAttributes(
"write_u16_be",
"write unsigned 2 byte value, big endian"
)]
[LuaMethodAttributes("read_u16_be", "read unsigned 2 byte value, big endian")]
public uint ReadU16Big(int addr)
{
return ReadUnsignedBig(addr, 2);
}
[LuaMethodAttributes("write_u16_be", "write unsigned 2 byte value, big endian")]
public void WriteU16Big(int addr, uint value)
{
WriteUnsignedBig(addr, value, 2);
}
[LuaMethodAttributes(
"write_u24_be",
"write unsigned 24 bit value, big endian"
)]
#endregion
#region 3 Byte
[LuaMethodAttributes("read_s24_le", "read signed 24 bit value, little endian")]
public int ReadS24Little(int addr)
{
return ReadSignedLittleCore(addr, 3);
}
[LuaMethodAttributes("write_s24_le", "write signed 24 bit value, little endian")]
public void WriteS24Little(int addr, int value)
{
WriteSignedLittle(addr, value, 3);
}
[LuaMethodAttributes("read_s24_be", "read signed 24 bit value, big endian")]
public int ReadS24Big(int addr)
{
return ReadSignedBig(addr, 3);
}
[LuaMethodAttributes("write_s24_be", "write signed 24 bit value, big endian")]
public void WriteS24Big(int addr, int value)
{
WriteSignedBig(addr, value, 3);
}
[LuaMethodAttributes("read_u24_le", "read unsigned 24 bit value, little endian")]
public uint ReadU24Little(int addr)
{
return ReadSignedLittle(addr, 3);
}
[LuaMethodAttributes("write_u24_le", "write unsigned 24 bit value, little endian")]
public void WriteU24Little(int addr, uint value)
{
WriteUnsignedLittle(addr, value, 3);
}
[LuaMethodAttributes("read_u24_be", "read unsigned 24 bit value, big endian")]
public uint ReadU24Big(int addr)
{
return ReadUnsignedBig(addr, 3);
}
[LuaMethodAttributes("write_u24_be", "write unsigned 24 bit value, big endian")]
public void WriteU24Big(int addr, uint value)
{
WriteUnsignedBig(addr, value, 3);
}
[LuaMethodAttributes(
"write_u32_be",
"write unsigned 4 byte value, big endian"
)]
#endregion
#region 4 Byte
[LuaMethodAttributes("read_s32_le", "read signed 4 byte value, little endian")]
public int ReadS32Little(int addr)
{
return ReadSignedLittleCore(addr, 4);
}
[LuaMethodAttributes("write_s32_le", "write signed 4 byte value, little endian")]
public void WriteS32Little(int addr, int value)
{
WriteSignedLittle(addr, value, 4);
}
[LuaMethodAttributes("read_s32_be", "read signed 4 byte value, big endian")]
public int ReadS32Big(int addr)
{
return ReadSignedBig(addr, 4);
}
[LuaMethodAttributes("write_s32_be", "write signed 4 byte value, big endian")]
public void WriteS32Big(int addr, int value)
{
WriteSignedBig(addr, value, 4);
}
[LuaMethodAttributes("read_u32_le", "read unsigned 4 byte value, little endian")]
public uint ReadU32Little(int addr)
{
return ReadSignedLittle(addr, 4);
}
[LuaMethodAttributes("write_u32_le", "write unsigned 4 byte value, little endian")]
public void WriteU32Little(int addr, uint value)
{
WriteUnsignedLittle(addr, value, 4);
}
[LuaMethodAttributes("read_u32_be", "read unsigned 4 byte value, big endian")]
public uint ReadU32Big(int addr)
{
return ReadUnsignedBig(addr, 4);
}
[LuaMethodAttributes("write_u32_be", "write unsigned 4 byte value, big endian")]
public void WriteU32Big(int addr, uint value)
{
WriteUnsignedBig(addr, value, 4);
}
#endregion
}
}

View File

@ -1,14 +1,12 @@
using System;
using System.Linq;
using LuaInterface;
using BizHawk.Emulation.Common;
namespace BizHawk.Client.Common
{
public class MemoryLuaLibrary : LuaLibraryBase
public class MemoryLuaLibrary : LuaMemoryBase
{
// TODO: when is this ever set by default?
private int _currentMemoryDomain; // Main memory by default
private int _currentMemoryDomain; // Main memory by default probably (index 0 is currently always main memory but may never be)
public MemoryLuaLibrary(Lua lua)
: base(lua) { }
@ -18,85 +16,12 @@ namespace BizHawk.Client.Common
public override string Name { get { return "memory"; } }
#region Memory Library Helpers
private static int U2S(uint u, int size)
protected override MemoryDomain Domain
{
var s = (int)u;
s <<= 8 * (4 - size);
s >>= 8 * (4 - size);
return s;
get { return Global.Emulator.MemoryDomains[_currentMemoryDomain]; }
}
private int ReadSignedLittleCore(int addr, int size)
{
return U2S(ReadUnsignedLittle(addr, size), size);
}
private uint ReadUnsignedLittle(int addr, int size)
{
uint v = 0;
for (var i = 0; i < size; ++i)
{
v |= ReadUnsignedByte(addr + i) << (8 * i);
}
return v;
}
private int ReadSignedBig(int addr, int size)
{
return U2S(ReadUnsignedBig(addr, size), size);
}
private uint ReadUnsignedBig(int addr, int size)
{
uint v = 0;
for (var i = 0; i < size; ++i)
{
v |= ReadUnsignedByte(addr + i) << (8 * (size - 1 - i));
}
return v;
}
private void WriteSignedLittle(int addr, int v, int size)
{
WriteUnsignedLittle(addr, (uint)v, size);
}
private void WriteUnsignedLittle(int addr, uint v, int size)
{
for (var i = 0; i < size; ++i)
{
WriteUnsignedByte(addr + i, (v >> (8 * i)) & 0xFF);
}
}
private void WriteSignedBig(int addr, int v, int size)
{
WriteUnsignedBig(addr, (uint)v, size);
}
private void WriteUnsignedBig(int addr, uint v, int size)
{
for (var i = 0; i < size; ++i)
{
WriteUnsignedByte(addr + i, (v >> (8 * (size - 1 - i))) & 0xFF);
}
}
private uint ReadUnsignedByte(int addr)
{
return Global.Emulator.MemoryDomains[_currentMemoryDomain].PeekByte(addr);
}
private void WriteUnsignedByte(int addr, uint v)
{
Global.Emulator.MemoryDomains[_currentMemoryDomain].PokeByte(addr, (byte)v);
}
#endregion
#region Unique Library Methods
[LuaMethodAttributes(
"getmemorydomainlist",
@ -113,66 +38,13 @@ namespace BizHawk.Client.Common
return table;
}
[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 LuaTable ReadByteRange(int addr, int length)
{
var lastAddr = length + addr;
var table = Lua.NewTable();
if (lastAddr < Global.Emulator.MemoryDomains[_currentMemoryDomain].Size)
{
for (var i = addr; i <= lastAddr; i++)
{
var a = string.Format("{0:X2}", i);
var v = Global.Emulator.MemoryDomains[_currentMemoryDomain].PeekByte(i);
var vs = string.Format("{0:X2}", (int)v);
table[a] = vs;
}
}
else
{
Log("Warning: Attempted read " + lastAddr + " outside memory domain size of " +
Global.Emulator.MemoryDomains[_currentMemoryDomain].Size +
" in memory.readbyterange()");
}
return table;
}
[LuaMethodAttributes(
"writebyterange",
"Writes the given values to the given addresses as unsigned bytes"
)]
public void WriteByteRange(LuaTable memoryblock)
{
foreach (var address in memoryblock.Keys)
{
var addr = LuaInt(address);
if (addr < Global.Emulator.MemoryDomains[_currentMemoryDomain].Size)
{
Global.Emulator.MemoryDomains[_currentMemoryDomain].PokeByte(
addr,
(byte)LuaInt(memoryblock[address]));
}
else
{
Log("Warning: Attempted read " + addr + " outside memory domain size of " +
Global.Emulator.MemoryDomains[_currentMemoryDomain].Size +
" in memory.writebyterange()");
}
}
}
[LuaMethodAttributes(
"getcurrentmemorydomain",
"Returns a string name of the current memory domain selected by Lua. The default is Main memory"
)]
public string GetCurrentMemoryDomain()
{
return Global.Emulator.MemoryDomains[_currentMemoryDomain].Name;
return Domain.Name;
}
[LuaMethodAttributes(
@ -181,48 +53,7 @@ namespace BizHawk.Client.Common
)]
public int GetCurrentMemoryDomainSize()
{
return Global.Emulator.MemoryDomains[_currentMemoryDomain].Size;
}
[LuaMethodAttributes(
"readbyte",
"gets the value from the given address as an unsigned byte"
)]
public uint ReadByte(int addr)
{
return ReadUnsignedByte(addr);
}
[LuaMethodAttributes(
"readfloat",
"Reads the given address as a 32-bit float value from the main memory domain with th e given endian"
)]
public float ReadFloat(int addr, bool bigendian)
{
var val = Global.Emulator.MemoryDomains[_currentMemoryDomain].PeekDWord(addr, bigendian);
var bytes = BitConverter.GetBytes(val);
return BitConverter.ToSingle(bytes, 0);
}
[LuaMethodAttributes(
"writebyte",
"Writes the given value to the given address as an unsigned byte"
)]
public void WriteByte(int addr, uint value)
{
WriteUnsignedByte(addr, value);
}
[LuaMethodAttributes(
"writefloat",
"Writes the given 32-bit float value to the given address and endian"
)]
public void WriteFloat(int addr, double value, bool bigendian)
{
var dv = (float)value;
var bytes = BitConverter.GetBytes(dv);
var v = BitConverter.ToUInt32(bytes, 0);
Global.Emulator.MemoryDomains[_currentMemoryDomain].PokeDWord(addr, v, bigendian);
return Domain.Size;
}
[LuaMethodAttributes(
@ -243,256 +74,248 @@ namespace BizHawk.Client.Common
return false;
}
[LuaMethodAttributes(
"read_s8",
"read signed byte"
)]
public int ReadS8(int addr)
{
return (sbyte)ReadUnsignedByte(addr);
}
#endregion
#region Common Special and Legacy Methods
[LuaMethodAttributes(
"read_u8",
"read unsigned byte"
"readbyte",
"gets the value from the given address as an unsigned byte"
)]
public uint ReadU8(int addr)
public uint ReadByte(int addr)
{
return ReadUnsignedByte(addr);
}
[LuaMethodAttributes(
"read_s16_le",
"read signed 2 byte value, little endian"
"writebyte",
"Writes the given value to the given address as an unsigned byte"
)]
public int ReadS16Little(int addr)
public void WriteByte(int addr, uint value)
{
return ReadSignedLittleCore(addr, 2);
WriteUnsignedByte(addr, value);
}
[LuaMethodAttributes(
"read_s24_le",
"read signed 24 bit value, little endian"
"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 int ReadS24Little(int addr)
public new LuaTable ReadByteRange(int addr, int length)
{
return ReadSignedLittleCore(addr, 3);
return base.ReadByteRange(addr, length);
}
[LuaMethodAttributes(
"read_s32_le",
"read signed 4 byte value, little endian"
"writebyterange",
"Writes the given values to the given addresses as unsigned bytes"
)]
public int ReadS32Little(int addr)
public new void WriteByteRange(LuaTable memoryblock)
{
return ReadSignedLittleCore(addr, 4);
base.WriteByteRange(memoryblock);
}
[LuaMethodAttributes(
"read_u16_le",
"read unsigned 2 byte value, little endian"
"readfloat",
"Reads the given address as a 32-bit float value from the main memory domain with th e given endian"
)]
public uint ReadU16Little(int addr)
public new float ReadFloat(int addr, bool bigendian)
{
return ReadUnsignedLittle(addr, 2);
return base.ReadFloat(addr, bigendian);
}
[LuaMethodAttributes(
"read_u24_le",
"read unsigned 24 bit value, little endian"
"writefloat",
"Writes the given 32-bit float value to the given address and endian"
)]
public uint ReadU24Little(int addr)
public new void WriteFloat(int addr, double value, bool bigendian)
{
return ReadUnsignedLittle(addr, 3);
base.WriteFloat(addr, value, bigendian);
}
[LuaMethodAttributes(
"read_u32_le",
"read unsigned 4 byte value, little endian"
)]
public uint ReadU32Little(int addr)
#endregion
#region 1 Byte
[LuaMethodAttributes("read_s8", "read signed byte")]
public int ReadS8(int addr)
{
return ReadUnsignedLittle(addr, 4);
return (sbyte)ReadUnsignedByte(addr);
}
[LuaMethodAttributes(
"read_s16_be",
"read signed 2 byte value, big endian"
)]
public int ReadS16Big(int addr)
{
return ReadSignedBig(addr, 2);
}
[LuaMethodAttributes(
"read_s24_be",
"read signed 24 bit value, big endian"
)]
public int ReadS24Big(int addr)
{
return ReadSignedBig(addr, 3);
}
[LuaMethodAttributes(
"read_s32_be",
"read signed 4 byte value, big endian"
)]
public int ReadS32Big(int addr)
{
return ReadSignedBig(addr, 4);
}
[LuaMethodAttributes(
"read_u16_be",
"read unsigned 2 byte value, big endian"
)]
public uint ReadU16Big(int addr)
{
return ReadUnsignedBig(addr, 2);
}
[LuaMethodAttributes(
"read_u24_be",
"read unsigned 24 bit value, big endian"
)]
public uint ReadU24Big(int addr)
{
return ReadUnsignedBig(addr, 3);
}
[LuaMethodAttributes(
"u32_be",
"read unsigned 4 byte value, big endian"
)]
public uint ReadU32Big(int addr)
{
return ReadUnsignedBig(addr, 4);
}
[LuaMethodAttributes(
"write_s8",
"write signed byte"
)]
[LuaMethodAttributes("write_s8", "write signed byte")]
public void WriteS8(int addr, uint value)
{
WriteUnsignedByte(addr, value);
}
[LuaMethodAttributes(
"write_u8",
"write unsigned byte"
)]
[LuaMethodAttributes("read_u8", "read unsigned byte")]
public uint ReadU8(int addr)
{
return ReadUnsignedByte(addr);
}
[LuaMethodAttributes("write_u8", "write unsigned byte")]
public void WriteU8(int addr, uint value)
{
WriteUnsignedByte(addr, value);
}
[LuaMethodAttributes(
"write_s16_le",
"write signed 2 byte value, little endian"
)]
#endregion
#region 2 Byte
[LuaMethodAttributes("read_s16_le", "read signed 2 byte value, little endian")]
public int ReadS16Little(int addr)
{
return ReadSignedLittleCore(addr, 2);
}
[LuaMethodAttributes("write_s16_le", "write signed 2 byte value, little endian")]
public void WriteS16Little(int addr, int value)
{
WriteSignedLittle(addr, value, 2);
}
[LuaMethodAttributes(
"write_s24_le",
"write signed 24 bit value, little endian"
)]
public void WriteS24Little(int addr, int value)
[LuaMethodAttributes("read_s16_be", "read signed 2 byte value, big endian")]
public int ReadS16Big(int addr)
{
WriteSignedLittle(addr, value, 3);
return ReadSignedBig(addr, 2);
}
[LuaMethodAttributes(
"write_s32_le",
"write signed 4 byte value, little endian"
)]
public void WriteS32Little(int addr, int value)
{
WriteSignedLittle(addr, value, 4);
}
[LuaMethodAttributes(
"write_u16_le",
"write unsigned 2 byte value, little endian"
)]
public void WriteU16Little(int addr, uint value)
{
WriteUnsignedLittle(addr, value, 2);
}
[LuaMethodAttributes(
"write_u24_le",
"write unsigned 24 bit value, little endian"
)]
public void WriteU24Little(int addr, uint value)
{
WriteUnsignedLittle(addr, value, 3);
}
[LuaMethodAttributes(
"write_u32_le",
"write unsigned 4 byte value, little endian"
)]
public void WriteU32Little(int addr, uint value)
{
WriteUnsignedLittle(addr, value, 4);
}
[LuaMethodAttributes(
"write_s16_be",
"write signed 2 byte value, big endian"
)]
[LuaMethodAttributes("write_s16_be", "write signed 2 byte value, big endian")]
public void WriteS16Big(int addr, int value)
{
WriteSignedBig(addr, value, 2);
}
[LuaMethodAttributes(
"write_s24_be",
"write signed 24 bit value, big endian"
)]
public void WriteS24Big(int addr, int value)
[LuaMethodAttributes("read_u16_le", "read unsigned 2 byte value, little endian")]
public uint ReadU16Little(int addr)
{
WriteSignedBig(addr, value, 3);
return ReadUnsignedLittle(addr, 2);
}
[LuaMethodAttributes(
"write_s32_be",
"write signed 4 byte value, big endian"
)]
public void WriteS32Big(int addr, int value)
[LuaMethodAttributes("write_u16_le", "write unsigned 2 byte value, little endian")]
public void WriteU16Little(int addr, uint value)
{
WriteSignedBig(addr, value, 4);
WriteUnsignedLittle(addr, value, 2);
}
[LuaMethodAttributes(
"write_u16_be",
"write unsigned 2 byte value, big endian"
)]
[LuaMethodAttributes("read_u16_be", "read unsigned 2 byte value, big endian")]
public uint ReadU16Big(int addr)
{
return ReadUnsignedBig(addr, 2);
}
[LuaMethodAttributes("write_u16_be", "write unsigned 2 byte value, big endian")]
public void WriteU16Big(int addr, uint value)
{
WriteUnsignedBig(addr, value, 2);
}
[LuaMethodAttributes(
"write_u24_be",
"write unsigned 24 bit value, big endian"
)]
#endregion
#region 3 Byte
[LuaMethodAttributes("read_s24_le", "read signed 24 bit value, little endian")]
public int ReadS24Little(int addr)
{
return ReadSignedLittleCore(addr, 3);
}
[LuaMethodAttributes("write_s24_le", "write signed 24 bit value, little endian")]
public void WriteS24Little(int addr, int value)
{
WriteSignedLittle(addr, value, 3);
}
[LuaMethodAttributes("read_s24_be", "read signed 24 bit value, big endian")]
public int ReadS24Big(int addr)
{
return ReadSignedBig(addr, 3);
}
[LuaMethodAttributes("write_s24_be", "write signed 24 bit value, big endian")]
public void WriteS24Big(int addr, int value)
{
WriteSignedBig(addr, value, 3);
}
[LuaMethodAttributes("read_u24_le", "read unsigned 24 bit value, little endian")]
public uint ReadU24Little(int addr)
{
return ReadUnsignedLittle(addr, 3);
}
[LuaMethodAttributes("write_u24_le", "write unsigned 24 bit value, little endian")]
public void WriteU24Little(int addr, uint value)
{
WriteUnsignedLittle(addr, value, 3);
}
[LuaMethodAttributes("read_u24_be", "read unsigned 24 bit value, big endian")]
public uint ReadU24Big(int addr)
{
return ReadUnsignedBig(addr, 3);
}
[LuaMethodAttributes("write_u24_be", "write unsigned 24 bit value, big endian")]
public void WriteU24Big(int addr, uint value)
{
WriteUnsignedBig(addr, value, 3);
}
[LuaMethodAttributes(
"write_u32_be",
"write unsigned 4 byte value, big endian"
)]
#endregion
#region 4 Byte
[LuaMethodAttributes("read_s32_le", "read signed 4 byte value, little endian")]
public int ReadS32Little(int addr)
{
return ReadSignedLittleCore(addr, 4);
}
[LuaMethodAttributes("write_s32_le", "write signed 4 byte value, little endian")]
public void WriteS32Little(int addr, int value)
{
WriteSignedLittle(addr, value, 4);
}
[LuaMethodAttributes("read_s32_be", "read signed 4 byte value, big endian")]
public int ReadS32Big(int addr)
{
return ReadSignedBig(addr, 4);
}
[LuaMethodAttributes("write_s32_be", "write signed 4 byte value, big endian")]
public void WriteS32Big(int addr, int value)
{
WriteSignedBig(addr, value, 4);
}
[LuaMethodAttributes("read_u32_le", "read unsigned 4 byte value, little endian")]
public uint ReadU32Little(int addr)
{
return ReadUnsignedLittle(addr, 4);
}
[LuaMethodAttributes("write_u32_le", "write unsigned 4 byte value, little endian")]
public void WriteU32Little(int addr, uint value)
{
WriteUnsignedLittle(addr, value, 4);
}
[LuaMethodAttributes("read_u32_be", "read unsigned 4 byte value, big endian")]
public uint ReadU32Big(int addr)
{
return ReadUnsignedBig(addr, 4);
}
[LuaMethodAttributes("write_u32_be", "write unsigned 4 byte value, big endian")]
public void WriteU32Big(int addr, uint value)
{
WriteUnsignedBig(addr, value, 4);
}
#endregion
}
}

View File

@ -0,0 +1,199 @@
using System;
using LuaInterface;
using BizHawk.Emulation.Common;
namespace BizHawk.Client.Common
{
/// <summary>
/// Base class for the Memory and MainMemory lua libraries
/// </summary>
public abstract class LuaMemoryBase : LuaLibraryBase
{
public LuaMemoryBase(Lua lua)
: base(lua) { }
public LuaMemoryBase(Lua lua, Action<string> logOutputCallback)
: base(lua, logOutputCallback) { }
protected abstract MemoryDomain Domain { get; }
protected uint ReadUnsignedByte(int addr)
{
if (addr < Domain.Size)
{
return Domain.PeekByte(addr);
}
Log("Warning: attempted read of " + addr +
" outside the memory size of " + Domain.Size);
return 0;
}
protected void WriteUnsignedByte(int addr, uint v)
{
if (addr < Domain.Size)
{
Domain.PokeByte(addr, (byte)v);
}
else
{
Log("Warning: attempted write to " + addr +
" outside the memory size of " + Domain.Size);
}
}
protected static int U2S(uint u, int size)
{
var s = (int)u;
s <<= 8 * (4 - size);
s >>= 8 * (4 - size);
return s;
}
protected int ReadSignedLittleCore(int addr, int size)
{
return U2S(ReadUnsignedLittle(addr, size), size);
}
protected uint ReadUnsignedLittle(int addr, int size)
{
uint v = 0;
for (var i = 0; i < size; ++i)
{
v |= ReadUnsignedByte(addr + i) << (8 * i);
}
return v;
}
protected int ReadSignedBig(int addr, int size)
{
return U2S(ReadUnsignedBig(addr, size), size);
}
protected uint ReadUnsignedBig(int addr, int size)
{
uint v = 0;
for (var i = 0; i < size; ++i)
{
v |= ReadUnsignedByte(addr + i) << (8 * (size - 1 - i));
}
return v;
}
protected void WriteSignedLittle(int addr, int v, int size)
{
WriteUnsignedLittle(addr, (uint)v, size);
}
protected void WriteUnsignedLittle(int addr, uint v, int size)
{
for (var i = 0; i < size; ++i)
{
WriteUnsignedByte(addr + i, (v >> (8 * i)) & 0xFF);
}
}
protected void WriteSignedBig(int addr, int v, int size)
{
WriteUnsignedBig(addr, (uint)v, size);
}
protected void WriteUnsignedBig(int addr, uint v, int size)
{
for (var i = 0; i < size; ++i)
{
WriteUnsignedByte(addr + i, (v >> (8 * (size - 1 - i))) & 0xFF);
}
}
protected uint ReadSignedLittle(int addr, int size)
{
uint v = 0;
for (var i = 0; i < size; ++i)
{
v |= ReadUnsignedByte(addr + i) << (8 * i);
}
return v;
}
#region public Library implementations
protected LuaTable ReadByteRange(int addr, int length)
{
var lastAddr = length + addr;
var table = Lua.NewTable();
if (lastAddr < Domain.Size)
{
for (var i = addr; i <= lastAddr; i++)
{
var a = string.Format("{0:X2}", i);
var v = Domain.PeekByte(i);
var vs = string.Format("{0:X2}", (int)v);
table[a] = vs;
}
}
else
{
Log("Warning: Attempted read " + lastAddr + " outside memory domain size of " +
Domain.Size + " in readbyterange()");
}
return table;
}
protected void WriteByteRange(LuaTable memoryblock)
{
foreach (var address in memoryblock.Keys)
{
var addr = LuaInt(address);
if (addr < Domain.Size)
{
Domain.PokeByte(addr, (byte)LuaInt(memoryblock[address]));
}
else
{
Log("Warning: Attempted write " + addr + " outside memory domain size of " +
Domain.Size + " in writebyterange()");
}
}
}
protected float ReadFloat(int addr, bool bigendian)
{
if (addr < Domain.Size)
{
var val = Domain.PeekDWord(addr, bigendian);
var bytes = BitConverter.GetBytes(val);
return BitConverter.ToSingle(bytes, 0);
}
else
{
Log("Warning: Attempted read " + addr +
" outside memory size of " + Domain.Size);
return 0;
}
}
protected void WriteFloat(int addr, double value, bool bigendian)
{
if (addr < Domain.Size)
{
var dv = (float)value;
var bytes = BitConverter.GetBytes(dv);
var v = BitConverter.ToUInt32(bytes, 0);
Domain.PokeDWord(addr, v, bigendian);
}
else
{
Log("Warning: Attempted write " + addr +
" outside memory size of " + Domain.Size);
}
}
#endregion
}
}