BizHawk/BizHawk.Client.Common/lua/EmuLuaLibrary.Memory.cs

352 lines
9.5 KiB
C#
Raw Normal View History

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 setcurrentmemorydomain() 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)
public MemoryLuaLibrary(Lua lua)
: base(lua)
{
if (MemoryDomainCore != null)
{
var domains = MemoryDomainCore.MemoryDomains;
_currentMemoryDomain = domains.IndexOf(domains.MainMemory);
}
}
public MemoryLuaLibrary(Lua lua, Action<string> logOutputCallback)
: base(lua, logOutputCallback)
{
if (MemoryDomainCore != null)
{
var domains = MemoryDomainCore.MemoryDomains;
_currentMemoryDomain = domains.IndexOf(domains.MainMemory);
}
}
public override string Name { get { return "memory"; } }
protected override MemoryDomain Domain
{
get
{
if (MemoryDomainCore != null)
{
return MemoryDomainCore.MemoryDomains[_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
2014-01-26 02:43:28 +00:00
[LuaMethodAttributes(
"getmemorydomainlist",
"Returns a string of the memory domains for the loaded platform core. List will be a single string delimited by line feeds"
2014-01-26 02:43:28 +00:00
)]
public LuaTable GetMemoryDomainList()
{
var table = Lua.NewTable();
for (int i = 0; i < DomainList.Count; i++)
{
table[i] = DomainList[i].Name;
}
return table;
}
2014-01-26 02:43:28 +00:00
[LuaMethodAttributes(
"getcurrentmemorydomain",
"Returns a string name of the current memory domain selected by Lua. The default is Main memory"
2014-01-26 02:43:28 +00:00
)]
public string GetCurrentMemoryDomain()
{
return Domain.Name;
}
2014-01-26 02:43:28 +00:00
[LuaMethodAttributes(
"getcurrentmemorydomainsize",
"Returns the number of bytes of the current memory domain selected by Lua. The default is Main memory"
2014-01-26 02:43:28 +00:00
)]
public int GetCurrentMemoryDomainSize()
{
return Domain.Size;
}
2014-01-26 02:43:28 +00:00
[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"
2014-01-26 02:43:28 +00:00
)]
public bool UseMemoryDomain(string domain)
{
for (var i = 0; i < DomainList.Count; i++)
{
if (DomainList[i].Name == domain)
{
_currentMemoryDomain = i;
return true;
}
}
return false;
}
#endregion
#region Common Special and Legacy Methods
2014-01-26 02:43:28 +00:00
[LuaMethodAttributes(
"readbyte",
"gets the value from the given address as an unsigned byte"
2014-01-26 02:43:28 +00:00
)]
public uint ReadByte(int addr)
{
return ReadUnsignedByte(addr);
}
2014-01-26 02:43:28 +00:00
[LuaMethodAttributes(
"writebyte",
"Writes the given value to the given address as an unsigned byte"
2014-01-26 02:43:28 +00:00
)]
public void WriteByte(int addr, uint value)
{
WriteUnsignedByte(addr, value);
}
2014-01-26 02:43:28 +00:00
[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)."
2014-01-26 02:43:28 +00:00
)]
public new LuaTable ReadByteRange(int addr, int length)
{
return base.ReadByteRange(addr, length);
}
2014-01-26 02:43:28 +00:00
[LuaMethodAttributes(
"writebyterange",
"Writes the given values to the given addresses as unsigned bytes"
2014-01-26 02:43:28 +00:00
)]
public new void WriteByteRange(LuaTable memoryblock)
{
base.WriteByteRange(memoryblock);
}
2014-01-26 02:43:28 +00:00
[LuaMethodAttributes(
"readfloat",
"Reads the given address as a 32-bit float value from the main memory domain with th e given endian"
2014-01-26 02:43:28 +00:00
)]
public new float ReadFloat(int addr, bool bigendian)
{
return base.ReadFloat(addr, bigendian);
}
2014-01-26 02:43:28 +00:00
[LuaMethodAttributes(
"writefloat",
"Writes the given 32-bit float value to the given address and endian"
2014-01-26 02:43:28 +00:00
)]
public new void WriteFloat(int addr, double value, bool bigendian)
{
base.WriteFloat(addr, value, bigendian);
}
#endregion
#region 1 Byte
[LuaMethodAttributes("read_s8", "read signed byte")]
public int ReadS8(int addr)
{
return (sbyte)ReadUnsignedByte(addr);
}
[LuaMethodAttributes("write_s8", "write signed byte")]
public void WriteS8(int addr, uint value)
{
WriteUnsignedByte(addr, value);
}
[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);
}
#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("read_s16_be", "read signed 2 byte value, big endian")]
public int ReadS16Big(int addr)
{
return ReadSignedBig(addr, 2);
}
[LuaMethodAttributes("write_s16_be", "write signed 2 byte value, big endian")]
public void WriteS16Big(int addr, int value)
{
WriteSignedBig(addr, value, 2);
}
[LuaMethodAttributes("read_u16_le", "read unsigned 2 byte value, little endian")]
public uint ReadU16Little(int addr)
{
return ReadUnsignedLittle(addr, 2);
}
[LuaMethodAttributes("write_u16_le", "write unsigned 2 byte value, little endian")]
public void WriteU16Little(int addr, uint value)
{
WriteUnsignedLittle(addr, value, 2);
}
[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);
}
#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);
}
#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)
2014-01-26 02:43:28 +00:00
{
WriteSignedLittle(addr, value, 4);
2014-01-26 02:43:28 +00:00
}
[LuaMethodAttributes("read_s32_be", "read signed 4 byte value, big endian")]
public int ReadS32Big(int addr)
2014-01-26 02:43:28 +00:00
{
return ReadSignedBig(addr, 4);
2014-01-26 02:43:28 +00:00
}
[LuaMethodAttributes("write_s32_be", "write signed 4 byte value, big endian")]
public void WriteS32Big(int addr, int value)
2014-01-26 02:43:28 +00:00
{
WriteSignedBig(addr, value, 4);
2014-01-26 02:43:28 +00:00
}
[LuaMethodAttributes("read_u32_le", "read unsigned 4 byte value, little endian")]
public uint ReadU32Little(int addr)
2014-01-26 02:43:28 +00:00
{
return ReadUnsignedLittle(addr, 4);
2014-01-26 02:43:28 +00:00
}
[LuaMethodAttributes("write_u32_le", "write unsigned 4 byte value, little endian")]
public void WriteU32Little(int addr, uint value)
2014-01-26 02:43:28 +00:00
{
WriteUnsignedLittle(addr, value, 4);
2014-01-26 02:43:28 +00:00
}
[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)
2014-01-26 02:43:28 +00:00
{
WriteUnsignedBig(addr, value, 4);
}
#endregion
}
}