lua: optional domain for memory functions.

fixes #538.

I tested heavy memory reading, and it's not adding noticeable overhead.
This commit is contained in:
feos 2015-11-29 22:55:52 +03:00
parent eb41e8a8e0
commit 3c07f7e649
2 changed files with 141 additions and 113 deletions

View File

@ -130,54 +130,54 @@ namespace BizHawk.Client.Common
"readbyte", "readbyte",
"gets the value from the given address as an unsigned byte" "gets the value from the given address as an unsigned byte"
)] )]
public uint ReadByte(int addr) public uint ReadByte(int addr, string domain = null)
{ {
return ReadUnsignedByte(addr); return ReadUnsignedByte(addr, domain);
} }
[LuaMethodAttributes( [LuaMethodAttributes(
"writebyte", "writebyte",
"Writes the given value to the given address as an unsigned byte" "Writes the given value to the given address as an unsigned byte"
)] )]
public void WriteByte(int addr, uint value) public void WriteByte(int addr, uint value, string domain = null)
{ {
WriteUnsignedByte(addr, value); WriteUnsignedByte(addr, value, domain);
} }
[LuaMethodAttributes( [LuaMethodAttributes(
"readbyterange", "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)." "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) public new LuaTable ReadByteRange(int addr, int length, string domain = null)
{ {
return base.ReadByteRange(addr, length); return base.ReadByteRange(addr, length, domain);
} }
[LuaMethodAttributes( [LuaMethodAttributes(
"writebyterange", "writebyterange",
"Writes the given values to the given addresses as unsigned bytes" "Writes the given values to the given addresses as unsigned bytes"
)] )]
public new void WriteByteRange(LuaTable memoryblock) public new void WriteByteRange(LuaTable memoryblock, string domain = null)
{ {
base.WriteByteRange(memoryblock); base.WriteByteRange(memoryblock, domain);
} }
[LuaMethodAttributes( [LuaMethodAttributes(
"readfloat", "readfloat",
"Reads the given address as a 32-bit float value from the main memory domain with th e given endian" "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) public new float ReadFloat(int addr, bool bigendian, string domain = null)
{ {
return base.ReadFloat(addr, bigendian); return base.ReadFloat(addr, bigendian, domain);
} }
[LuaMethodAttributes( [LuaMethodAttributes(
"writefloat", "writefloat",
"Writes the given 32-bit float value to the given address and endian" "Writes the given 32-bit float value to the given address and endian"
)] )]
public new void WriteFloat(int addr, double value, bool bigendian) public new void WriteFloat(int addr, double value, bool bigendian, string domain = null)
{ {
base.WriteFloat(addr, value, bigendian); base.WriteFloat(addr, value, bigendian, domain);
} }
#endregion #endregion
@ -185,27 +185,27 @@ namespace BizHawk.Client.Common
#region 1 Byte #region 1 Byte
[LuaMethodAttributes("read_s8", "read signed byte")] [LuaMethodAttributes("read_s8", "read signed byte")]
public int ReadS8(int addr) public int ReadS8(int addr, string domain = null)
{ {
return (sbyte)ReadUnsignedByte(addr); return (sbyte)ReadUnsignedByte(addr, domain);
} }
[LuaMethodAttributes("write_s8", "write signed byte")] [LuaMethodAttributes("write_s8", "write signed byte")]
public void WriteS8(int addr, uint value) public void WriteS8(int addr, uint value, string domain = null)
{ {
WriteUnsignedByte(addr, value); WriteUnsignedByte(addr, value, domain);
} }
[LuaMethodAttributes("read_u8", "read unsigned byte")] [LuaMethodAttributes("read_u8", "read unsigned byte")]
public uint ReadU8(int addr) public uint ReadU8(int addr, string domain = null)
{ {
return ReadUnsignedByte(addr); return ReadUnsignedByte(addr, domain);
} }
[LuaMethodAttributes("write_u8", "write unsigned byte")] [LuaMethodAttributes("write_u8", "write unsigned byte")]
public void WriteU8(int addr, uint value) public void WriteU8(int addr, uint value, string domain = null)
{ {
WriteUnsignedByte(addr, value); WriteUnsignedByte(addr, value, domain);
} }
#endregion #endregion
@ -213,51 +213,51 @@ namespace BizHawk.Client.Common
#region 2 Byte #region 2 Byte
[LuaMethodAttributes("read_s16_le", "read signed 2 byte value, little endian")] [LuaMethodAttributes("read_s16_le", "read signed 2 byte value, little endian")]
public int ReadS16Little(int addr) public int ReadS16Little(int addr, string domain = null)
{ {
return ReadSignedLittleCore(addr, 2); return ReadSignedLittleCore(addr, 2, domain);
} }
[LuaMethodAttributes("write_s16_le", "write signed 2 byte value, little endian")] [LuaMethodAttributes("write_s16_le", "write signed 2 byte value, little endian")]
public void WriteS16Little(int addr, int value) public void WriteS16Little(int addr, int value, string domain = null)
{ {
WriteSignedLittle(addr, value, 2); WriteSignedLittle(addr, value, 2, domain);
} }
[LuaMethodAttributes("read_s16_be", "read signed 2 byte value, big endian")] [LuaMethodAttributes("read_s16_be", "read signed 2 byte value, big endian")]
public int ReadS16Big(int addr) public int ReadS16Big(int addr, string domain = null)
{ {
return ReadSignedBig(addr, 2); return ReadSignedBig(addr, 2, domain);
} }
[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) public void WriteS16Big(int addr, int value, string domain = null)
{ {
WriteSignedBig(addr, value, 2); WriteSignedBig(addr, value, 2, domain);
} }
[LuaMethodAttributes("read_u16_le", "read unsigned 2 byte value, little endian")] [LuaMethodAttributes("read_u16_le", "read unsigned 2 byte value, little endian")]
public uint ReadU16Little(int addr) public uint ReadU16Little(int addr, string domain = null)
{ {
return ReadUnsignedLittle(addr, 2); return ReadUnsignedLittle(addr, 2, domain);
} }
[LuaMethodAttributes("write_u16_le", "write unsigned 2 byte value, little endian")] [LuaMethodAttributes("write_u16_le", "write unsigned 2 byte value, little endian")]
public void WriteU16Little(int addr, uint value) public void WriteU16Little(int addr, uint value, string domain = null)
{ {
WriteUnsignedLittle(addr, value, 2); WriteUnsignedLittle(addr, value, 2, domain);
} }
[LuaMethodAttributes("read_u16_be", "read unsigned 2 byte value, big endian")] [LuaMethodAttributes("read_u16_be", "read unsigned 2 byte value, big endian")]
public uint ReadU16Big(int addr) public uint ReadU16Big(int addr, string domain = null)
{ {
return ReadUnsignedBig(addr, 2); return ReadUnsignedBig(addr, 2, domain);
} }
[LuaMethodAttributes("write_u16_be", "write unsigned 2 byte value, big endian")] [LuaMethodAttributes("write_u16_be", "write unsigned 2 byte value, big endian")]
public void WriteU16Big(int addr, uint value) public void WriteU16Big(int addr, uint value, string domain = null)
{ {
WriteUnsignedBig(addr, value, 2); WriteUnsignedBig(addr, value, 2, domain);
} }
#endregion #endregion
@ -265,51 +265,51 @@ namespace BizHawk.Client.Common
#region 3 Byte #region 3 Byte
[LuaMethodAttributes("read_s24_le", "read signed 24 bit value, little endian")] [LuaMethodAttributes("read_s24_le", "read signed 24 bit value, little endian")]
public int ReadS24Little(int addr) public int ReadS24Little(int addr, string domain = null)
{ {
return ReadSignedLittleCore(addr, 3); return ReadSignedLittleCore(addr, 3, domain);
} }
[LuaMethodAttributes("write_s24_le", "write signed 24 bit value, little endian")] [LuaMethodAttributes("write_s24_le", "write signed 24 bit value, little endian")]
public void WriteS24Little(int addr, int value) public void WriteS24Little(int addr, int value, string domain = null)
{ {
WriteSignedLittle(addr, value, 3); WriteSignedLittle(addr, value, 3, domain);
} }
[LuaMethodAttributes("read_s24_be", "read signed 24 bit value, big endian")] [LuaMethodAttributes("read_s24_be", "read signed 24 bit value, big endian")]
public int ReadS24Big(int addr) public int ReadS24Big(int addr, string domain = null)
{ {
return ReadSignedBig(addr, 3); return ReadSignedBig(addr, 3, domain);
} }
[LuaMethodAttributes("write_s24_be", "write signed 24 bit value, big endian")] [LuaMethodAttributes("write_s24_be", "write signed 24 bit value, big endian")]
public void WriteS24Big(int addr, int value) public void WriteS24Big(int addr, int value, string domain = null)
{ {
WriteSignedBig(addr, value, 3); WriteSignedBig(addr, value, 3, domain);
} }
[LuaMethodAttributes("read_u24_le", "read unsigned 24 bit value, little endian")] [LuaMethodAttributes("read_u24_le", "read unsigned 24 bit value, little endian")]
public uint ReadU24Little(int addr) public uint ReadU24Little(int addr, string domain = null)
{ {
return ReadUnsignedLittle(addr, 3); return ReadUnsignedLittle(addr, 3, domain);
} }
[LuaMethodAttributes("write_u24_le", "write unsigned 24 bit value, little endian")] [LuaMethodAttributes("write_u24_le", "write unsigned 24 bit value, little endian")]
public void WriteU24Little(int addr, uint value) public void WriteU24Little(int addr, uint value, string domain = null)
{ {
WriteUnsignedLittle(addr, value, 3); WriteUnsignedLittle(addr, value, 3, domain);
} }
[LuaMethodAttributes("read_u24_be", "read unsigned 24 bit value, big endian")] [LuaMethodAttributes("read_u24_be", "read unsigned 24 bit value, big endian")]
public uint ReadU24Big(int addr) public uint ReadU24Big(int addr, string domain = null)
{ {
return ReadUnsignedBig(addr, 3); return ReadUnsignedBig(addr, 3, domain);
} }
[LuaMethodAttributes("write_u24_be", "write unsigned 24 bit value, big endian")] [LuaMethodAttributes("write_u24_be", "write unsigned 24 bit value, big endian")]
public void WriteU24Big(int addr, uint value) public void WriteU24Big(int addr, uint value, string domain = null)
{ {
WriteUnsignedBig(addr, value, 3); WriteUnsignedBig(addr, value, 3, domain);
} }
#endregion #endregion
@ -317,51 +317,51 @@ namespace BizHawk.Client.Common
#region 4 Byte #region 4 Byte
[LuaMethodAttributes("read_s32_le", "read signed 4 byte value, little endian")] [LuaMethodAttributes("read_s32_le", "read signed 4 byte value, little endian")]
public int ReadS32Little(int addr) public int ReadS32Little(int addr, string domain = null)
{ {
return ReadSignedLittleCore(addr, 4); return ReadSignedLittleCore(addr, 4, domain);
} }
[LuaMethodAttributes("write_s32_le", "write signed 4 byte value, little endian")] [LuaMethodAttributes("write_s32_le", "write signed 4 byte value, little endian")]
public void WriteS32Little(int addr, int value) public void WriteS32Little(int addr, int value, string domain = null)
{ {
WriteSignedLittle(addr, value, 4); WriteSignedLittle(addr, value, 4, domain);
} }
[LuaMethodAttributes("read_s32_be", "read signed 4 byte value, big endian")] [LuaMethodAttributes("read_s32_be", "read signed 4 byte value, big endian")]
public int ReadS32Big(int addr) public int ReadS32Big(int addr, string domain = null)
{ {
return ReadSignedBig(addr, 4); return ReadSignedBig(addr, 4, domain);
} }
[LuaMethodAttributes("write_s32_be", "write signed 4 byte value, big endian")] [LuaMethodAttributes("write_s32_be", "write signed 4 byte value, big endian")]
public void WriteS32Big(int addr, int value) public void WriteS32Big(int addr, int value, string domain = null)
{ {
WriteSignedBig(addr, value, 4); WriteSignedBig(addr, value, 4, domain);
} }
[LuaMethodAttributes("read_u32_le", "read unsigned 4 byte value, little endian")] [LuaMethodAttributes("read_u32_le", "read unsigned 4 byte value, little endian")]
public uint ReadU32Little(int addr) public uint ReadU32Little(int addr, string domain = null)
{ {
return ReadUnsignedLittle(addr, 4); return ReadUnsignedLittle(addr, 4, domain);
} }
[LuaMethodAttributes("write_u32_le", "write unsigned 4 byte value, little endian")] [LuaMethodAttributes("write_u32_le", "write unsigned 4 byte value, little endian")]
public void WriteU32Little(int addr, uint value) public void WriteU32Little(int addr, uint value, string domain = null)
{ {
WriteUnsignedLittle(addr, value, 4); WriteUnsignedLittle(addr, value, 4, domain);
} }
[LuaMethodAttributes("read_u32_be", "read unsigned 4 byte value, big endian")] [LuaMethodAttributes("read_u32_be", "read unsigned 4 byte value, big endian")]
public uint ReadU32Big(int addr) public uint ReadU32Big(int addr, string domain = null)
{ {
return ReadUnsignedBig(addr, 4); return ReadUnsignedBig(addr, 4, domain);
} }
[LuaMethodAttributes("write_u32_be", "write unsigned 4 byte value, big endian")] [LuaMethodAttributes("write_u32_be", "write unsigned 4 byte value, big endian")]
public void WriteU32Big(int addr, uint value) public void WriteU32Big(int addr, uint value, string domain = null)
{ {
WriteUnsignedBig(addr, value, 4); WriteUnsignedBig(addr, value, 4, domain);
} }
#endregion #endregion

View File

@ -41,35 +41,59 @@ namespace BizHawk.Client.Common
} }
} }
protected uint ReadUnsignedByte(int addr) public string VerifyMemoryDomain(string domain)
{ {
if (addr < Domain.Size) try
{ {
return Domain.PeekByte(addr); if (DomainList[domain] == null)
{
Log(string.Format("Unable to find domain: {0}, fallung back to current", domain));
return Domain.Name;
}
else
{
return domain;
}
}
catch // Just in case
{
Log(string.Format("Unable to find domain: {0}, fallung back to current", domain));
}
return Domain.Name;
}
protected uint ReadUnsignedByte(int addr, string domain = null)
{
var d = (string.IsNullOrEmpty(domain)) ? Domain : DomainList[VerifyMemoryDomain(domain)];
if (addr < d.Size)
{
return d.PeekByte(addr);
} }
Log("Warning: attempted read of " + addr + Log("Warning: attempted read of " + addr +
" outside the memory size of " + Domain.Size); " outside the memory size of " + d.Size);
return 0; return 0;
} }
protected void WriteUnsignedByte(int addr, uint v) protected void WriteUnsignedByte(int addr, uint v, string domain = null)
{ {
if (Domain.CanPoke()) var d = (string.IsNullOrEmpty(domain)) ? Domain : DomainList[VerifyMemoryDomain(domain)];
if (d.CanPoke())
{ {
if (addr < Domain.Size) if (addr < Domain.Size)
{ {
Domain.PokeByte(addr, (byte)v); d.PokeByte(addr, (byte)v);
} }
else else
{ {
Log("Warning: attempted write to " + addr + Log("Warning: attempted write to " + addr +
" outside the memory size of " + Domain.Size); " outside the memory size of " + d.Size);
} }
} }
else else
{ {
Log(string.Format("Error: the domain {0} is not writable", Domain.Name)); Log(string.Format("Error: the domain {0} is not writable", d.Name));
} }
} }
@ -81,65 +105,65 @@ namespace BizHawk.Client.Common
return s; return s;
} }
protected int ReadSignedLittleCore(int addr, int size) protected int ReadSignedLittleCore(int addr, int size, string domain = null)
{ {
return U2S(ReadUnsignedLittle(addr, size), size); return U2S(ReadUnsignedLittle(addr, size, domain), size);
} }
protected uint ReadUnsignedLittle(int addr, int size) protected uint ReadUnsignedLittle(int addr, int size, string domain = null)
{ {
uint v = 0; uint v = 0;
for (var i = 0; i < size; ++i) for (var i = 0; i < size; ++i)
{ {
v |= ReadUnsignedByte(addr + i) << (8 * i); v |= ReadUnsignedByte(addr + i, domain) << (8 * i);
} }
return v; return v;
} }
protected int ReadSignedBig(int addr, int size) protected int ReadSignedBig(int addr, int size, string domain = null)
{ {
return U2S(ReadUnsignedBig(addr, size), size); return U2S(ReadUnsignedBig(addr, size, domain), size);
} }
protected uint ReadUnsignedBig(int addr, int size) protected uint ReadUnsignedBig(int addr, int size, string domain = null)
{ {
uint v = 0; uint v = 0;
for (var i = 0; i < size; ++i) for (var i = 0; i < size; ++i)
{ {
v |= ReadUnsignedByte(addr + i) << (8 * (size - 1 - i)); v |= ReadUnsignedByte(addr + i, domain) << (8 * (size - 1 - i));
} }
return v; return v;
} }
protected void WriteSignedLittle(int addr, int v, int size) protected void WriteSignedLittle(int addr, int v, int size, string domain = null)
{ {
WriteUnsignedLittle(addr, (uint)v, size); WriteUnsignedLittle(addr, (uint)v, size, domain);
} }
protected void WriteUnsignedLittle(int addr, uint v, int size) protected void WriteUnsignedLittle(int addr, uint v, int size, string domain = null)
{ {
for (var i = 0; i < size; ++i) for (var i = 0; i < size; ++i)
{ {
WriteUnsignedByte(addr + i, (v >> (8 * i)) & 0xFF); WriteUnsignedByte(addr + i, (v >> (8 * i)) & 0xFF, domain);
} }
} }
protected void WriteSignedBig(int addr, int v, int size) protected void WriteSignedBig(int addr, int v, int size, string domain = null)
{ {
WriteUnsignedBig(addr, (uint)v, size); WriteUnsignedBig(addr, (uint)v, size, domain);
} }
protected void WriteUnsignedBig(int addr, uint v, int size) protected void WriteUnsignedBig(int addr, uint v, int size, string domain = null)
{ {
for (var i = 0; i < size; ++i) for (var i = 0; i < size; ++i)
{ {
WriteUnsignedByte(addr + i, (v >> (8 * (size - 1 - i))) & 0xFF); WriteUnsignedByte(addr + i, (v >> (8 * (size - 1 - i))) & 0xFF, domain);
} }
} }
protected uint ReadSignedLittle(int addr, int size) protected uint ReadSignedLittle(int addr, int size) // only used by mainmemory, so no domain can be passed
{ {
uint v = 0; uint v = 0;
for (var i = 0; i < size; ++i) for (var i = 0; i < size; ++i)
@ -152,84 +176,88 @@ namespace BizHawk.Client.Common
#region public Library implementations #region public Library implementations
protected LuaTable ReadByteRange(int addr, int length) protected LuaTable ReadByteRange(int addr, int length, string domain = null)
{ {
var d = (string.IsNullOrEmpty(domain)) ? Domain : DomainList[VerifyMemoryDomain(domain)];
var lastAddr = length + addr; var lastAddr = length + addr;
var table = Lua.NewTable(); var table = Lua.NewTable();
if (lastAddr < Domain.Size) if (lastAddr < d.Size)
{ {
for (var i = 0; i <length ; i++) for (var i = 0; i <length ; i++)
{ {
int a = addr + i; int a = addr + i;
var v = Domain.PeekByte(a); var v = d.PeekByte(a);
table[i] = v; table[i] = v;
} }
} }
else else
{ {
Log("Warning: Attempted read " + lastAddr + " outside memory domain size of " + Log("Warning: Attempted read " + lastAddr + " outside memory domain size of " +
Domain.Size + " in readbyterange()"); d.Size + " in readbyterange()");
} }
return table; return table;
} }
protected void WriteByteRange(LuaTable memoryblock) protected void WriteByteRange(LuaTable memoryblock, string domain = null)
{ {
if (Domain.CanPoke()) var d = (string.IsNullOrEmpty(domain)) ? Domain : DomainList[VerifyMemoryDomain(domain)];
if (d.CanPoke())
{ {
foreach (var address in memoryblock.Keys) foreach (var address in memoryblock.Keys)
{ {
var addr = LuaInt(address); var addr = LuaInt(address);
if (addr < Domain.Size) if (addr < d.Size)
{ {
Domain.PokeByte(addr, (byte)LuaInt(memoryblock[address])); d.PokeByte(addr, (byte)LuaInt(memoryblock[address]));
} }
else else
{ {
Log("Warning: Attempted write " + addr + " outside memory domain size of " + Log("Warning: Attempted write " + addr + " outside memory domain size of " +
Domain.Size + " in writebyterange()"); d.Size + " in writebyterange()");
} }
} }
} }
else else
{ {
Log(string.Format("Error: the domain {0} is not writable", Domain.Name)); Log(string.Format("Error: the domain {0} is not writable", d.Name));
} }
} }
protected float ReadFloat(int addr, bool bigendian) protected float ReadFloat(int addr, bool bigendian, string domain = null)
{ {
if (addr < Domain.Size) var d = (string.IsNullOrEmpty(domain)) ? Domain : DomainList[VerifyMemoryDomain(domain)];
if (addr < d.Size)
{ {
var val = Domain.PeekDWord(addr, bigendian); var val = d.PeekDWord(addr, bigendian);
var bytes = BitConverter.GetBytes(val); var bytes = BitConverter.GetBytes(val);
return BitConverter.ToSingle(bytes, 0); return BitConverter.ToSingle(bytes, 0);
} }
else else
{ {
Log("Warning: Attempted read " + addr + Log("Warning: Attempted read " + addr +
" outside memory size of " + Domain.Size); " outside memory size of " + d.Size);
return 0; return 0;
} }
} }
protected void WriteFloat(int addr, double value, bool bigendian) protected void WriteFloat(int addr, double value, bool bigendian, string domain = null)
{ {
if (Domain.CanPoke()) var d = (string.IsNullOrEmpty(domain)) ? Domain : DomainList[VerifyMemoryDomain(domain)];
if (d.CanPoke())
{ {
if (addr < Domain.Size) if (addr < d.Size)
{ {
var dv = (float)value; var dv = (float)value;
var bytes = BitConverter.GetBytes(dv); var bytes = BitConverter.GetBytes(dv);
var v = BitConverter.ToUInt32(bytes, 0); var v = BitConverter.ToUInt32(bytes, 0);
Domain.PokeDWord(addr, v, bigendian); d.PokeDWord(addr, v, bigendian);
} }
else else
{ {
Log("Warning: Attempted write " + addr + Log("Warning: Attempted write " + addr +
" outside memory size of " + Domain.Size); " outside memory size of " + d.Size);
} }
} }
else else