Lua - implement memory.readfloat() and memory.writefloat() and equivlant mainmemory functions

This commit is contained in:
adelikat 2013-10-07 21:41:19 +00:00
parent bcfe4ff576
commit 7e0e8ed51d
1 changed files with 355 additions and 314 deletions

View File

@ -25,7 +25,7 @@ namespace BizHawk.MultiClient
private readonly LuaConsole Caller; private readonly LuaConsole Caller;
private int CurrentMemoryDomain; //Main memory by default private int CurrentMemoryDomain; //Main memory by default
private Lua currThread; private Lua currThread;
private LuaFunctionCollection lua_functions = new LuaFunctionCollection(); private LuaFunctionCollection lua_functions = new LuaFunctionCollection();
private readonly Dictionary<Color, SolidBrush> SolidBrushes = new Dictionary<Color, SolidBrush>(); private readonly Dictionary<Color, SolidBrush> SolidBrushes = new Dictionary<Color, SolidBrush>();
@ -141,7 +141,7 @@ namespace BizHawk.MultiClient
foreach (string t in ConsoleFunctions) foreach (string t in ConsoleFunctions)
{ {
lua.RegisterFunction("console." + t, this, lua.RegisterFunction("console." + t, this,
GetType().GetMethod("console_" + t)); GetType().GetMethod("console_" + t));
docs.Add("console", t, GetType().GetMethod("console_" + t)); docs.Add("console", t, GetType().GetMethod("console_" + t));
} }
@ -170,7 +170,7 @@ namespace BizHawk.MultiClient
foreach (string t in MainMemoryFunctions) foreach (string t in MainMemoryFunctions)
{ {
lua.RegisterFunction("mainmemory." + t, this, lua.RegisterFunction("mainmemory." + t, this,
GetType().GetMethod("mainmemory_" + t)); GetType().GetMethod("mainmemory_" + t));
docs.Add("mainmemory", t, GetType().GetMethod("mainmemory_" + t)); docs.Add("mainmemory", t, GetType().GetMethod("mainmemory_" + t));
} }
@ -178,7 +178,7 @@ namespace BizHawk.MultiClient
foreach (string t in SaveStateFunctions) foreach (string t in SaveStateFunctions)
{ {
lua.RegisterFunction("savestate." + t, this, lua.RegisterFunction("savestate." + t, this,
GetType().GetMethod("savestate_" + t)); GetType().GetMethod("savestate_" + t));
docs.Add("savestate", t, GetType().GetMethod("savestate_" + t)); docs.Add("savestate", t, GetType().GetMethod("savestate_" + t));
} }
@ -207,7 +207,7 @@ namespace BizHawk.MultiClient
foreach (string t in MultiClientFunctions) foreach (string t in MultiClientFunctions)
{ {
lua.RegisterFunction("client." + t, this, lua.RegisterFunction("client." + t, this,
GetType().GetMethod("client_" + t)); GetType().GetMethod("client_" + t));
docs.Add("client", t, GetType().GetMethod("client_" + t)); docs.Add("client", t, GetType().GetMethod("client_" + t));
} }
@ -361,273 +361,276 @@ namespace BizHawk.MultiClient
/****************************************************/ /****************************************************/
public static string[] ConsoleFunctions = new[] public static string[] ConsoleFunctions = new[]
{ {
"output", "output",
"log", "log",
"clear", "clear",
"getluafunctionslist" "getluafunctionslist"
}; };
public static string[] GuiFunctions = new[] public static string[] GuiFunctions = new[]
{ {
"text", "text",
"alert", "alert",
"cleartext", "cleartext",
"drawPixel", "drawPixel",
"drawLine", "drawLine",
"drawBox", "drawBox",
"drawRectangle", "drawRectangle",
"drawEllipse", "drawEllipse",
"drawPolygon", "drawPolygon",
"drawBezier", "drawBezier",
"drawPie", "drawPie",
"drawIcon", "drawIcon",
"drawImage", "drawImage",
"addmessage", "addmessage",
"drawText", "drawText",
"drawString" "drawString"
}; };
public static string[] EmuFunctions = new[] public static string[] EmuFunctions = new[]
{ {
"frameadvance", "frameadvance",
"yield", "yield",
"pause", "pause",
"unpause", "unpause",
"togglepause", "togglepause",
"ispaused", "ispaused",
"speedmode", "speedmode",
"framecount", "framecount",
"lagcount", "lagcount",
"islagged", "islagged",
"getsystemid", "getsystemid",
"setrenderplanes", "setrenderplanes",
"frameskip", "frameskip",
"minimizeframeskip", "minimizeframeskip",
"limitframerate", "limitframerate",
"displayvsync", "displayvsync",
"enablerewind", "enablerewind",
"on_snoop" "on_snoop"
}; };
public static string[] MemoryFunctions = new[] public static string[] MemoryFunctions = new[]
{ {
"usememorydomain", "usememorydomain",
"getmemorydomainlist", "getmemorydomainlist",
"getcurrentmemorydomain", "getcurrentmemorydomain",
"getcurrentmemorydomainsize", "getcurrentmemorydomainsize",
"read_s8", "read_s8",
"read_u8", "read_u8",
"read_s16_le", "read_s16_le",
"read_s24_le", "read_s24_le",
"read_s32_le", "read_s32_le",
"read_u16_le", "read_u16_le",
"read_u24_le", "read_u24_le",
"read_u32_le", "read_u32_le",
"read_s16_be", "read_s16_be",
"read_s24_be", "read_s24_be",
"read_s32_be", "read_s32_be",
"read_u16_be", "read_u16_be",
"read_u24_be", "read_u24_be",
"read_u32_be", "read_u32_be",
"write_s8", "write_s8",
"write_u8", "write_u8",
"write_s16_le", "write_s16_le",
"write_s24_le", "write_s24_le",
"write_s32_le", "write_s32_le",
"write_u16_le", "write_u16_le",
"write_u24_le", "write_u24_le",
"write_u32_le", "write_u32_le",
"write_s16_be", "write_s16_be",
"write_s24_be", "write_s24_be",
"write_s32_be", "write_s32_be",
"write_u16_be", "write_u16_be",
"write_u24_be", "write_u24_be",
"write_u32_be", "write_u32_be",
"readbyte", "readbyte",
"writebyte" "writebyte",
//"registerwrite", "readfloat",
//"registerread", "writefloat"
}; };
public static string[] MainMemoryFunctions = new[] public static string[] MainMemoryFunctions = new[]
{ {
"read_s8", "read_s8",
"read_u8", "read_u8",
"read_s16_le", "read_s16_le",
"read_s24_le", "read_s24_le",
"read_s32_le", "read_s32_le",
"read_u16_le", "read_u16_le",
"read_u24_le", "read_u24_le",
"read_u32_le", "read_u32_le",
"read_s16_be", "read_s16_be",
"read_s24_be", "read_s24_be",
"read_s32_be", "read_s32_be",
"read_u16_be", "read_u16_be",
"read_u24_be", "read_u24_be",
"read_u32_be", "read_u32_be",
"write_s8", "write_s8",
"write_u8", "write_u8",
"write_s16_le", "write_s16_le",
"write_s24_le", "write_s24_le",
"write_s32_le", "write_s32_le",
"write_u16_le", "write_u16_le",
"write_u24_le", "write_u24_le",
"write_u32_le", "write_u32_le",
"write_s16_be", "write_s16_be",
"write_s24_be", "write_s24_be",
"write_s32_be", "write_s32_be",
"write_u16_be", "write_u16_be",
"write_u24_be", "write_u24_be",
"write_u32_be", "write_u32_be",
"readbyterange", "readbyterange",
"writebyterange" "writebyterange",
}; "readfloat",
"writefloat"
};
public static string[] SaveStateFunctions = new[] public static string[] SaveStateFunctions = new[]
{ {
"saveslot", "saveslot",
"loadslot", "loadslot",
"save", "save",
"load", "load",
"registersave", "registersave",
"registerload" "registerload"
}; };
public static string[] MovieFunctions = new[] public static string[] MovieFunctions = new[]
{ {
"mode", "mode",
"isloaded", "isloaded",
"rerecordcount", "rerecordcount",
"length", "length",
"stop", "stop",
"filename", "filename",
"getreadonly", "getreadonly",
"setreadonly", "setreadonly",
"getrerecordcounting", "getrerecordcounting",
"setrerecordcounting", "setrerecordcounting",
"getinput" "getinput"
}; };
public static string[] InputFunctions = new[] public static string[] InputFunctions = new[]
{ {
"get", "get",
"getmouse" "getmouse"
}; };
public static string[] JoypadFunctions = new[] public static string[] JoypadFunctions = new[]
{ {
"set", "set",
"get", "get",
"getimmediate", "getimmediate",
"setanalog" "setanalog"
}; };
public static string[] MultiClientFunctions = new[] public static string[] MultiClientFunctions = new[]
{ {
"getwindowsize", "getwindowsize",
"setwindowsize", "setwindowsize",
"openrom", "openrom",
"closerom", "closerom",
"opentoolbox", "opentoolbox",
"openramwatch", "openramwatch",
"openramsearch", "openramsearch",
"openhexeditor", "openhexeditor",
"opentasstudio", "opentasstudio",
"opencheats", "opencheats",
"screenwidth", "screenwidth",
"xpos", "xpos",
"screenheight", "screenheight",
"ypos", "ypos",
"screenshot", "screenshot",
"screenshottoclipboard", "screenshottoclipboard",
"setscreenshotosd", "setscreenshotosd",
"pause_av", "pause_av",
"unpause_av", "unpause_av",
"reboot_core", "reboot_core",
}; };
public static string[] FormsFunctions = new[] public static string[] FormsFunctions = new[]
{ {
"newform", "newform",
"destroy", "destroy",
"destroyall", "destroyall",
"button", "button",
"label", "label",
"textbox", "textbox",
"setlocation", "setlocation",
"setsize", "setsize",
"settext", "settext",
"addclick", "addclick",
"clearclicks", "clearclicks",
"gettext", "gettext",
"setproperty", "setproperty",
"getproperty", "getproperty",
"openfile" "openfile"
}; };
public static string[] BitwiseFunctions = new[] public static string[] BitwiseFunctions = new[]
{ {
"band", "band",
"lshift", "lshift",
"rshift", "rshift",
"rol", "rol",
"ror", "ror",
"bor", "bor",
"bxor", "bxor",
"bnot" "bnot"
}; };
public static string[] NESFunctions = new[] public static string[] NESFunctions = new[]
{ {
"setscanlines", "setscanlines",
"gettopscanline", "gettopscanline",
"getbottomscanline", "getbottomscanline",
"getclipleftandright", "getclipleftandright",
"setclipleftandright", "setclipleftandright",
"getdispbackground", "getdispbackground",
"setdispbackground", "setdispbackground",
"getdispsprites", "getdispsprites",
"setdispsprites", "setdispsprites",
"getallowmorethaneightsprites", "getallowmorethaneightsprites",
"setallowmorethaneightsprites", "setallowmorethaneightsprites",
"addgamegenie", "addgamegenie",
"removegamegenie" "removegamegenie"
}; };
public static string[] SNESFunctions = new[] public static string[] SNESFunctions = new[]
{ {
"setlayer_bg_1", "setlayer_bg_1",
"setlayer_bg_2", "setlayer_bg_2",
"setlayer_bg_3", "setlayer_bg_3",
"setlayer_bg_4", "setlayer_bg_4",
"getlayer_bg_1", "getlayer_bg_1",
"getlayer_bg_2", "getlayer_bg_2",
"getlayer_bg_3", "getlayer_bg_3",
"getlayer_bg_4", "getlayer_bg_4",
"setlayer_obj_1", "setlayer_obj_1",
"setlayer_obj_2", "setlayer_obj_2",
"setlayer_obj_3", "setlayer_obj_3",
"setlayer_obj_4", "setlayer_obj_4",
"getlayer_obj_1", "getlayer_obj_1",
"getlayer_obj_2", "getlayer_obj_2",
"getlayer_obj_3", "getlayer_obj_3",
"getlayer_obj_4" "getlayer_obj_4"
}; };
public static string[] EventFunctions = new[] public static string[] EventFunctions = new[]
{ {
"onloadstate", "onloadstate",
"onsavestate", "onsavestate",
"onframestart", "onframestart",
"onframeend", "onframeend",
"onmemoryread", "onmemoryread",
"onmemorywrite", "onmemorywrite",
"oninputpoll", "oninputpoll",
"unregisterbyid", "unregisterbyid",
"unregisterbyname" "unregisterbyname"
}; };
/****************************************************/ /****************************************************/
/*************function definitions********************/ /*************function definitions********************/
/****************************************************/ /****************************************************/
@ -644,43 +647,43 @@ namespace BizHawk.MultiClient
} }
else else
{ {
if (lua_input is LuaTable) if (lua_input is LuaTable)
{ {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
var lti = (lua_input as LuaTable); var lti = (lua_input as LuaTable);
List<string> Keys = new List<string>(); List<string> Keys = new List<string>();
List<string> Values = new List<string>(); List<string> Values = new List<string>();
foreach (var key in lti.Keys) { Keys.Add(key.ToString()); } foreach (var key in lti.Keys) { Keys.Add(key.ToString()); }
foreach (var value in lti.Values) { Values.Add(value.ToString()); } foreach (var value in lti.Values) { Values.Add(value.ToString()); }
List<KeyValuePair<string, string>> KVPs = new List<KeyValuePair<string, string>>(); List<KeyValuePair<string, string>> KVPs = new List<KeyValuePair<string, string>>();
for (int i = 0; i < Keys.Count; i++) for (int i = 0; i < Keys.Count; i++)
{ {
if (i < Values.Count) if (i < Values.Count)
{ {
KeyValuePair<string, string> kvp = new KeyValuePair<string, string>(Keys[i], Values[i]); KeyValuePair<string, string> kvp = new KeyValuePair<string, string>(Keys[i], Values[i]);
KVPs.Add(kvp); KVPs.Add(kvp);
} }
} }
KVPs = KVPs.OrderBy(x => x.Key).ToList(); KVPs = KVPs.OrderBy(x => x.Key).ToList();
foreach(var kvp in KVPs) foreach (var kvp in KVPs)
{ {
sb sb
.Append("\"") .Append("\"")
.Append(kvp.Key) .Append(kvp.Key)
.Append("\": \"") .Append("\": \"")
.Append(kvp.Value) .Append(kvp.Value)
.Append("\"") .Append("\"")
.AppendLine(); .AppendLine();
} }
Global.MainForm.LuaConsole1.WriteToOutputWindow(sb.ToString()); Global.MainForm.LuaConsole1.WriteToOutputWindow(sb.ToString());
} }
else else
{ {
Global.MainForm.LuaConsole1.WriteToOutputWindow(lua_input.ToString()); Global.MainForm.LuaConsole1.WriteToOutputWindow(lua_input.ToString());
} }
} }
} }
@ -748,7 +751,7 @@ namespace BizHawk.MultiClient
dx *= client_getwindowsize(); dx *= client_getwindowsize();
dy *= client_getwindowsize(); dy *= client_getwindowsize();
Global.OSD.AddGUIText(luaStr.ToString(), dx, dy, alert, GetColor(background), GetColor(forecolor), a); Global.OSD.AddGUIText(luaStr.ToString(), dx, dy, alert, GetColor(background), GetColor(forecolor), a);
} }
public void gui_text(object luaX, object luaY, object luaStr, object background = null, object forecolor = null, public void gui_text(object luaX, object luaY, object luaStr, object background = null, object forecolor = null,
@ -1609,6 +1612,25 @@ namespace BizHawk.MultiClient
M_W_U_BE(addr, v, 4); M_W_U_BE(addr, v, 4);
} }
public float memory_readfloat(object lua_addr, bool bigendian)
{
int addr = LuaInt(lua_addr);
uint val = Global.Emulator.MemoryDomains[CurrentMemoryDomain].PeekDWord(addr, bigendian ? Endian.Big : Endian.Little);
byte[] bytes = BitConverter.GetBytes(val);
float _float = BitConverter.ToSingle(bytes, 0);
return _float;
}
public void memory_writefloat(object lua_addr, object lua_v, bool bigendian)
{
int addr = LuaInt(lua_addr);
float dv = (float)(double)lua_v;
byte[] bytes = BitConverter.GetBytes(dv);
uint v = BitConverter.ToUInt32(bytes, 0);
Global.Emulator.MemoryDomains[CurrentMemoryDomain].PokeDWord(addr, v, bigendian ? Endian.Big : Endian.Little);
}
private int M_R_S_LE(int addr, int size) private int M_R_S_LE(int addr, int size)
{ {
return U2S(M_R_U_LE(addr, size), size); return U2S(M_R_U_LE(addr, size), size);
@ -1959,6 +1981,25 @@ namespace BizHawk.MultiClient
return s; return s;
} }
public float mainmemory_readfloat(object lua_addr, bool bigendian)
{
int addr = LuaInt(lua_addr);
uint val = Global.Emulator.MainMemory.PeekDWord(addr, bigendian ? Endian.Big : Endian.Little);
byte[] bytes = BitConverter.GetBytes(val);
float _float = BitConverter.ToSingle(bytes, 0);
return _float;
}
public void mainmemory_writefloat(object lua_addr, object lua_v, bool bigendian)
{
int addr = LuaInt(lua_addr);
float dv = (float)(double)lua_v;
byte[] bytes = BitConverter.GetBytes(dv);
uint v = BitConverter.ToUInt32(bytes, 0);
Global.Emulator.MainMemory.PokeDWord(addr, v, bigendian ? Endian.Big : Endian.Little);
}
//---------------------------------------------------- //----------------------------------------------------
//Bitwise Operator library //Bitwise Operator library
//---------------------------------------------------- //----------------------------------------------------
@ -2154,7 +2195,7 @@ namespace BizHawk.MultiClient
LuaTable input = _lua.NewTable(); LuaTable input = _lua.NewTable();
string s = Global.MovieSession.Movie.GetInput(LuaInt(frame)); string s = Global.MovieSession.Movie.GetInput(LuaInt(frame));
MovieControllerAdapter m = new MovieControllerAdapter {Type = Global.MovieSession.MovieControllerAdapter.Type}; MovieControllerAdapter m = new MovieControllerAdapter { Type = Global.MovieSession.MovieControllerAdapter.Type };
m.SetControllersAsMnemonic(s); m.SetControllersAsMnemonic(s);
foreach (string button in m.Type.BoolButtons) foreach (string button in m.Type.BoolButtons)
input[button] = m[button]; input[button] = m[button];
@ -2194,29 +2235,29 @@ namespace BizHawk.MultiClient
public LuaTable joypad_get(object controller = null) public LuaTable joypad_get(object controller = null)
{ {
LuaTable buttons = _lua.NewTable(); LuaTable buttons = _lua.NewTable();
foreach (string button in Global.ControllerOutput.Source.Type.BoolButtons) foreach (string button in Global.ControllerOutput.Source.Type.BoolButtons)
{ {
if (controller == null) if (controller == null)
{ {
buttons[button] = Global.ControllerOutput[button]; buttons[button] = Global.ControllerOutput[button];
} }
else if (button.Length >= 3 && button.Substring(0, 2) == "P" + LuaInt(controller).ToString()) else if (button.Length >= 3 && button.Substring(0, 2) == "P" + LuaInt(controller).ToString())
{ {
buttons[button.Substring(3)] = Global.ControllerOutput["P" + LuaInt(controller) + " " + button.Substring(3)]; buttons[button.Substring(3)] = Global.ControllerOutput["P" + LuaInt(controller) + " " + button.Substring(3)];
} }
} }
foreach (string button in Global.ControllerOutput.Source.Type.FloatControls) foreach (string button in Global.ControllerOutput.Source.Type.FloatControls)
{ {
if (controller == null) if (controller == null)
{ {
buttons[button] = Global.ControllerOutput.GetFloat(button); buttons[button] = Global.ControllerOutput.GetFloat(button);
} }
else if (button.Length >= 3 && button.Substring(0, 2) == "P" + LuaInt(controller).ToString()) else if (button.Length >= 3 && button.Substring(0, 2) == "P" + LuaInt(controller).ToString())
{ {
buttons[button.Substring(3)] = Global.ControllerOutput.GetFloat("P" + LuaInt(controller) + " " + button.Substring(3)); buttons[button.Substring(3)] = Global.ControllerOutput.GetFloat("P" + LuaInt(controller) + " " + button.Substring(3));
} }
} }
buttons["clear"] = null; buttons["clear"] = null;
buttons["getluafunctionslist"] = null; buttons["getluafunctionslist"] = null;
@ -2242,7 +2283,7 @@ namespace BizHawk.MultiClient
bool invert = false; bool invert = false;
bool? theValue; bool? theValue;
string theValueStr = buttons[button].ToString(); string theValueStr = buttons[button].ToString();
if (!String.IsNullOrWhiteSpace(theValueStr)) if (!String.IsNullOrWhiteSpace(theValueStr))
{ {
if (theValueStr.ToLower() == "false") if (theValueStr.ToLower() == "false")
@ -2263,7 +2304,7 @@ namespace BizHawk.MultiClient
{ {
theValue = null; theValue = null;
} }
if (!invert) if (!invert)
{ {
@ -2319,7 +2360,7 @@ namespace BizHawk.MultiClient
} }
} }
} }
catch { /*Eat it*/ } catch { /*Eat it*/ }
} }
public void joypad_setanalog(LuaTable controls, object controller = null) public void joypad_setanalog(LuaTable controls, object controller = null)
@ -2380,19 +2421,19 @@ namespace BizHawk.MultiClient
{ {
return Global.RenderPanel.NativeSize.Width; return Global.RenderPanel.NativeSize.Width;
} }
public int client_xpos() public int client_xpos()
{ {
return Global.MainForm.DesktopLocation.X; return Global.MainForm.DesktopLocation.X;
} }
public int client_screenheight() public int client_screenheight()
{ {
return Global.RenderPanel.NativeSize.Height; return Global.RenderPanel.NativeSize.Height;
} }
public int client_ypos() public int client_ypos()
{ {
return Global.MainForm.DesktopLocation.Y; return Global.MainForm.DesktopLocation.Y;
} }
public void client_openrom(object lua_input) public void client_openrom(object lua_input)
{ {
@ -2844,13 +2885,13 @@ namespace BizHawk.MultiClient
OpenFileDialog openFileDialog1 = new OpenFileDialog(); OpenFileDialog openFileDialog1 = new OpenFileDialog();
if (InitialDirectory != null) if (InitialDirectory != null)
{ {
openFileDialog1.InitialDirectory = InitialDirectory; openFileDialog1.InitialDirectory = InitialDirectory;
} }
if (FileName != null) if (FileName != null)
{ {
openFileDialog1.FileName = FileName; openFileDialog1.FileName = FileName;
} }
if (Filter != null) if (Filter != null)
{ {
openFileDialog1.AddExtension = true; openFileDialog1.AddExtension = true;
openFileDialog1.Filter = Filter; openFileDialog1.Filter = Filter;