diff --git a/BizHawk.Client.Common/lua/EmuLuaLibrary.MainMemory.cs b/BizHawk.Client.Common/lua/EmuLuaLibrary.MainMemory.cs index e3cf5ba28b..4b5d9e5a8d 100644 --- a/BizHawk.Client.Common/lua/EmuLuaLibrary.MainMemory.cs +++ b/BizHawk.Client.Common/lua/EmuLuaLibrary.MainMemory.cs @@ -10,7 +10,7 @@ using NLua; namespace BizHawk.Client.Common { [Description("Main memory library reads and writes from the Main memory domain (the default memory domain set by any given core)")] - public sealed class MainMemoryLuaLibrary : LuaLibraryBase + public sealed class MainMemoryLuaLibrary : DelegatingLuaLibrary { [RequiredService] private IEmulator Emulator { get; set; } @@ -43,115 +43,6 @@ namespace BizHawk.Client.Common } } - private uint ReadUnsignedByte(int addr) - { - var d = Domain; - if (addr < d.Size) - { - return d.PeekByte(addr); - } - - Log($"Warning: attempted read of {addr} outside the memory size of {d.Size}"); - return 0; - } - - private void WriteUnsignedByte(int addr, uint v) - { - var d = Domain; - if (d.CanPoke()) - { - if (addr < Domain.Size) - { - d.PokeByte(addr, (byte)v); - } - else - { - Log($"Warning: attempted write to {addr} outside the memory size of {d.Size}"); - } - } - else - { - Log($"Error: the domain {d.Name} is not writable"); - } - } - - private static int U2S(uint u, int size) - { - var s = (int)u; - s <<= 8 * (4 - size); - s >>= 8 * (4 - size); - return s; - } - - 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 ReadSignedLittle(int addr, int size) - { - uint v = 0; - for (var i = 0; i < size; ++i) - { - v |= ReadUnsignedByte(addr + i) << (8 * i); - } - - return v; - } - #region Unique Library Methods [LuaMethodExample("local stmaiget = mainmemory.getname( );")] @@ -174,46 +65,31 @@ namespace BizHawk.Client.Common [LuaMethodExample("local uimairea = mainmemory.readbyte( 0x100 );")] [LuaMethod("readbyte", "gets the value from the given address as an unsigned byte")] - public uint ReadByte(int addr) - { - return ReadUnsignedByte(addr); - } + public uint ReadByte(int addr) => APIs.Mem.ReadByte(addr, Domain.Name); [LuaMethodExample("mainmemory.writebyte( 0x100, 1000 );")] [LuaMethod("writebyte", "Writes the given value to the given address as an unsigned byte")] - public void WriteByte(int addr, uint value) - { - WriteUnsignedByte(addr, value); - } + public void WriteByte(int addr, uint value) => APIs.Mem.WriteByte(addr, value, Domain.Name); [LuaMethodExample("local nlmairea = mainmemory.readbyterange( 0x100, 64 );")] [LuaMethod("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 d = Domain; - var lastAddr = length + addr; + var result = APIs.Mem.ReadByteRange(addr, length, Domain.Name); var table = Lua.NewTable(); - if (lastAddr <= d.Size) - { - for (var i = 0; i < length; i++) - { - int a = addr + i; - var v = d.PeekByte(a); - table[i] = v; - } - } - else - { - Log($"Warning: Attempted read {lastAddr} outside memory domain size of {d.Size} in readbyterange()"); - } - + var count = result.Count; + for (var i = 0; i != count; i++) table[i] = result[i]; return table; } + /// TODO C# version requires a contiguous address range [LuaMethodExample("")] [LuaMethod("writebyterange", "Writes the given values to the given addresses as unsigned bytes")] public void WriteByteRange(LuaTable memoryblock) { +#if true + foreach (var addr in memoryblock.Keys) APIs.Mem.WriteByte(LuaInt(addr), (uint) memoryblock[addr], Domain.Name); +#else var d = Domain; if (d.CanPoke()) { @@ -234,48 +110,23 @@ namespace BizHawk.Client.Common { Log($"Error: the domain {d.Name} is not writable"); } +#endif } [LuaMethodExample("local simairea = mainmemory.readfloat(0x100, false);")] [LuaMethod("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 d = Domain; - if (addr < d.Size) - { - var val = d.PeekUint(addr, bigendian); - var bytes = BitConverter.GetBytes(val); - return BitConverter.ToSingle(bytes, 0); - } - - Log($"Warning: Attempted read {addr} outside memory size of {d.Size}"); - - return 0; + APIs.Mem.SetBigEndian(bigendian); + return APIs.Mem.ReadFloat(addr, Domain.Name); } [LuaMethodExample("mainmemory.writefloat( 0x100, 10.0, false );")] [LuaMethod("writefloat", "Writes the given 32-bit float value to the given address and endian")] public void WriteFloat(int addr, double value, bool bigendian) { - var d = Domain; - if (d.CanPoke()) - { - if (addr < d.Size) - { - var dv = (float)value; - var bytes = BitConverter.GetBytes(dv); - var v = BitConverter.ToUInt32(bytes, 0); - d.PokeUint(addr, v, bigendian); - } - else - { - Log($"Warning: Attempted write {addr} outside memory size of {d.Size}"); - } - } - else - { - Log($"Error: the domain {Domain.Name} is not writable"); - } + APIs.Mem.SetBigEndian(bigendian); + APIs.Mem.WriteFloat(addr, value, Domain.Name); } #endregion @@ -284,31 +135,19 @@ namespace BizHawk.Client.Common [LuaMethodExample("local inmairea = mainmemory.read_s8( 0x100 );")] [LuaMethod("read_s8", "read signed byte")] - public int ReadS8(int addr) - { - return (sbyte)ReadUnsignedByte(addr); - } + public int ReadS8(int addr) => APIs.Mem.ReadS8(addr, Domain.Name); [LuaMethodExample("mainmemory.write_s8( 0x100, 1000 );")] [LuaMethod("write_s8", "write signed byte")] - public void WriteS8(int addr, uint value) - { - WriteUnsignedByte(addr, value); - } + public void WriteS8(int addr, uint value) => APIs.Mem.WriteS8(addr, unchecked((int) value), Domain.Name); [LuaMethodExample("local uimairea = mainmemory.read_u8( 0x100 );")] [LuaMethod("read_u8", "read unsigned byte")] - public uint ReadU8(int addr) - { - return ReadUnsignedByte(addr); - } + public uint ReadU8(int addr) => APIs.Mem.ReadU8(addr, Domain.Name); [LuaMethodExample("mainmemory.write_u8( 0x100, 1000 );")] [LuaMethod("write_u8", "write unsigned byte")] - public void WriteU8(int addr, uint value) - { - WriteUnsignedByte(addr, value); - } + public void WriteU8(int addr, uint value) => APIs.Mem.WriteU8(addr, value, Domain.Name); #endregion @@ -318,56 +157,64 @@ namespace BizHawk.Client.Common [LuaMethod("read_s16_le", "read signed 2 byte value, little endian")] public int ReadS16Little(int addr) { - return ReadSignedLittleCore(addr, 2); + APIs.Mem.SetBigEndian(false); + return APIs.Mem.ReadS16(addr, Domain.Name); } [LuaMethodExample("mainmemory.write_s16_le( 0x100, -1000 );")] [LuaMethod("write_s16_le", "write signed 2 byte value, little endian")] public void WriteS16Little(int addr, int value) { - WriteSignedLittle(addr, value, 2); + APIs.Mem.SetBigEndian(false); + APIs.Mem.WriteS16(addr, value, Domain.Name); } [LuaMethodExample("local inmairea = mainmemory.read_s16_be( 0x100 );")] [LuaMethod("read_s16_be", "read signed 2 byte value, big endian")] public int ReadS16Big(int addr) { - return ReadSignedBig(addr, 2); + APIs.Mem.SetBigEndian(); + return APIs.Mem.ReadS16(addr, Domain.Name); } [LuaMethodExample("mainmemory.write_s16_be( 0x100, -1000 );")] [LuaMethod("write_s16_be", "write signed 2 byte value, big endian")] public void WriteS16Big(int addr, int value) { - WriteSignedBig(addr, value, 2); + APIs.Mem.SetBigEndian(); + APIs.Mem.WriteS16(addr, value, Domain.Name); } [LuaMethodExample("local uimairea = mainmemory.read_u16_le( 0x100 );")] [LuaMethod("read_u16_le", "read unsigned 2 byte value, little endian")] public uint ReadU16Little(int addr) { - return ReadSignedLittle(addr, 2); + APIs.Mem.SetBigEndian(false); + return APIs.Mem.ReadU16(addr, Domain.Name); } [LuaMethodExample("mainmemory.write_u16_le( 0x100, 1000 );")] [LuaMethod("write_u16_le", "write unsigned 2 byte value, little endian")] public void WriteU16Little(int addr, uint value) { - WriteUnsignedLittle(addr, value, 2); + APIs.Mem.SetBigEndian(false); + APIs.Mem.WriteU16(addr, value, Domain.Name); } [LuaMethodExample("local uimairea = mainmemory.read_u16_be( 0x100 );")] [LuaMethod("read_u16_be", "read unsigned 2 byte value, big endian")] public uint ReadU16Big(int addr) { - return ReadUnsignedBig(addr, 2); + APIs.Mem.SetBigEndian(); + return APIs.Mem.ReadU16(addr, Domain.Name); } [LuaMethodExample("mainmemory.write_u16_be( 0x100, 1000 );")] [LuaMethod("write_u16_be", "write unsigned 2 byte value, big endian")] public void WriteU16Big(int addr, uint value) { - WriteUnsignedBig(addr, value, 2); + APIs.Mem.SetBigEndian(); + APIs.Mem.WriteU16(addr, value, Domain.Name); } #endregion @@ -378,56 +225,64 @@ namespace BizHawk.Client.Common [LuaMethod("read_s24_le", "read signed 24 bit value, little endian")] public int ReadS24Little(int addr) { - return ReadSignedLittleCore(addr, 3); + APIs.Mem.SetBigEndian(false); + return APIs.Mem.ReadS24(addr, Domain.Name); } [LuaMethodExample("mainmemory.write_s24_le( 0x100, -1000 );")] [LuaMethod("write_s24_le", "write signed 24 bit value, little endian")] public void WriteS24Little(int addr, int value) { - WriteSignedLittle(addr, value, 3); + APIs.Mem.SetBigEndian(false); + APIs.Mem.WriteS24(addr, value, Domain.Name); } [LuaMethodExample("local inmairea = mainmemory.read_s24_be( 0x100 );")] [LuaMethod("read_s24_be", "read signed 24 bit value, big endian")] public int ReadS24Big(int addr) { - return ReadSignedBig(addr, 3); + APIs.Mem.SetBigEndian(); + return APIs.Mem.ReadS24(addr, Domain.Name); } [LuaMethodExample("mainmemory.write_s24_be( 0x100, -1000 );")] [LuaMethod("write_s24_be", "write signed 24 bit value, big endian")] public void WriteS24Big(int addr, int value) { - WriteSignedBig(addr, value, 3); + APIs.Mem.SetBigEndian(); + APIs.Mem.WriteS24(addr, value, Domain.Name); } [LuaMethodExample("local uimairea = mainmemory.read_u24_le( 0x100 );")] [LuaMethod("read_u24_le", "read unsigned 24 bit value, little endian")] public uint ReadU24Little(int addr) { - return ReadSignedLittle(addr, 3); + APIs.Mem.SetBigEndian(false); + return APIs.Mem.ReadU24(addr, Domain.Name); } [LuaMethodExample("mainmemory.write_u24_le( 0x100, 1000 );")] [LuaMethod("write_u24_le", "write unsigned 24 bit value, little endian")] public void WriteU24Little(int addr, uint value) { - WriteUnsignedLittle(addr, value, 3); + APIs.Mem.SetBigEndian(false); + APIs.Mem.WriteU24(addr, value, Domain.Name); } [LuaMethodExample("local uimairea = mainmemory.read_u24_be( 0x100 );")] [LuaMethod("read_u24_be", "read unsigned 24 bit value, big endian")] public uint ReadU24Big(int addr) { - return ReadUnsignedBig(addr, 3); + APIs.Mem.SetBigEndian(); + return APIs.Mem.ReadU24(addr, Domain.Name); } [LuaMethodExample("mainmemory.write_u24_be( 0x100, 1000 );")] [LuaMethod("write_u24_be", "write unsigned 24 bit value, big endian")] public void WriteU24Big(int addr, uint value) { - WriteUnsignedBig(addr, value, 3); + APIs.Mem.SetBigEndian(); + APIs.Mem.WriteU24(addr, value, Domain.Name); } #endregion @@ -438,56 +293,64 @@ namespace BizHawk.Client.Common [LuaMethod("read_s32_le", "read signed 4 byte value, little endian")] public int ReadS32Little(int addr) { - return ReadSignedLittleCore(addr, 4); + APIs.Mem.SetBigEndian(false); + return APIs.Mem.ReadS32(addr, Domain.Name); } [LuaMethodExample("mainmemory.write_s32_le( 0x100, -1000 );")] [LuaMethod("write_s32_le", "write signed 4 byte value, little endian")] public void WriteS32Little(int addr, int value) { - WriteSignedLittle(addr, value, 4); + APIs.Mem.SetBigEndian(false); + APIs.Mem.WriteS32(addr, value, Domain.Name); } [LuaMethodExample("local inmairea = mainmemory.read_s32_be( 0x100 );")] [LuaMethod("read_s32_be", "read signed 4 byte value, big endian")] public int ReadS32Big(int addr) { - return ReadSignedBig(addr, 4); + APIs.Mem.SetBigEndian(); + return APIs.Mem.ReadS32(addr, Domain.Name); } [LuaMethodExample("mainmemory.write_s32_be( 0x100, -1000 );")] [LuaMethod("write_s32_be", "write signed 4 byte value, big endian")] public void WriteS32Big(int addr, int value) { - WriteSignedBig(addr, value, 4); + APIs.Mem.SetBigEndian(); + APIs.Mem.WriteS32(addr, value, Domain.Name); } [LuaMethodExample("local uimairea = mainmemory.read_u32_le( 0x100 );")] [LuaMethod("read_u32_le", "read unsigned 4 byte value, little endian")] public uint ReadU32Little(int addr) { - return ReadSignedLittle(addr, 4); + APIs.Mem.SetBigEndian(false); + return APIs.Mem.ReadU32(addr, Domain.Name); } [LuaMethodExample("mainmemory.write_u32_le( 0x100, 1000 );")] [LuaMethod("write_u32_le", "write unsigned 4 byte value, little endian")] public void WriteU32Little(int addr, uint value) { - WriteUnsignedLittle(addr, value, 4); + APIs.Mem.SetBigEndian(false); + APIs.Mem.WriteU32(addr, value, Domain.Name); } [LuaMethodExample("local uimairea = mainmemory.read_u32_be( 0x100 );")] [LuaMethod("read_u32_be", "read unsigned 4 byte value, big endian")] public uint ReadU32Big(int addr) { - return ReadUnsignedBig(addr, 4); + APIs.Mem.SetBigEndian(); + return APIs.Mem.ReadU32(addr, Domain.Name); } [LuaMethodExample("mainmemory.write_u32_be( 0x100, 1000 );")] [LuaMethod("write_u32_be", "write unsigned 4 byte value, big endian")] public void WriteU32Big(int addr, uint value) { - WriteUnsignedBig(addr, value, 4); + APIs.Mem.SetBigEndian(); + APIs.Mem.WriteU32(addr, value, Domain.Name); } #endregion