48
35
extern TASEDITOR_LUA taseditor_lua;
53
#include "x6502abbrev.h"
58
HMODULE mod = LoadLibrary("lua51.dll");
75
MessageBox(NULL, "lua51.dll was not found. Please get it into your PATH or in the same directory as fceux.exe", "FCEUX", MB_OK | MB_ICONERROR);
54
87
#include <lauxlib.h>
55
88
#include <lualib.h>
57
91
int iuplua_open(lua_State * L);
58
92
int iupcontrolslua_open(lua_State * L);
59
93
int luaopen_winapi(lua_State * L);
209
243
//make sure we have the right number of strings
210
CTASSERT(sizeof(luaCallIDStrings)/sizeof(*luaCallIDStrings) == LUACALL_COUNT)
244
CTASSERT(sizeof(luaCallIDStrings)/sizeof(*luaCallIDStrings) == LUACALL_COUNT, "luaCallIDStrings doesn't have the correct number of strings");
212
246
static const char* luaMemHookTypeStrings [] =
223
257
//make sure we have the right number of strings
224
CTASSERT(sizeof(luaMemHookTypeStrings)/sizeof(*luaMemHookTypeStrings) == LUAMEMHOOK_COUNT)
258
CTASSERT(sizeof(luaMemHookTypeStrings)/sizeof(*luaMemHookTypeStrings) == LUAMEMHOOK_COUNT, "luaMemHookTypeStrings doesn't have the correct number of strings");
226
260
static char* rawToCString(lua_State* L, int idx=0);
227
261
static const char* toCString(lua_State* L, int idx=0);
1157
1191
if (lua_isfunction(L, -1))
1194
// since the scriptdata can be very expensive to load
1195
// (e.g. the registered save function returned some huge tables)
1196
// check the number of parameters the registered load function expects
1197
// and don't bother loading the parameters it wouldn't receive anyway
1198
int numParamsExpected = (L->top - 1)->value.gc->cl.l.p->numparams; // NOTE: if this line crashes, that means your Lua headers are out of sync with your Lua lib
1199
if(numParamsExpected) numParamsExpected--; // minus one for the savestate number we always pass in
1201
int prevGarbage = lua_gc(L, LUA_GCCOUNT, 0);
1203
lua_pushinteger(L, savestateNumber);
1204
saveData.LoadRecord(L, LUA_DATARECORDKEY, numParamsExpected);
1159
1206
int prevGarbage = lua_gc(L, LUA_GCCOUNT, 0);
1161
1208
lua_pushinteger(L, savestateNumber);
1162
1209
saveData.LoadRecord(L, LUA_DATARECORDKEY, (unsigned int) -1);
1163
1212
int n = lua_gettop(L) - 1;
1165
1214
int ret = lua_pcall(L, n, 0, 0);
1195
static int rom_readbyte(lua_State *L) { lua_pushinteger(L, FCEU_ReadRomByte(luaL_checkinteger(L,1))); return 1; }
1196
static int rom_readbytesigned(lua_State *L) { lua_pushinteger(L, (signed char)FCEU_ReadRomByte(luaL_checkinteger(L,1))); return 1; }
1244
static int rom_readbyte(lua_State *L) {
1245
lua_pushinteger(L, FCEU_ReadRomByte(luaL_checkinteger(L,1)));
1249
static int rom_readbytesigned(lua_State *L) {
1250
lua_pushinteger(L, (signed char)FCEU_ReadRomByte(luaL_checkinteger(L,1)));
1197
1254
static int rom_gethash(lua_State *L) {
1198
1255
const char *type = luaL_checkstring(L, 1);
1199
1256
if(!type) lua_pushstring(L, "");
1201
1258
else lua_pushstring(L, "");
1204
static int memory_readbyte(lua_State *L) { lua_pushinteger(L, FCEU_CheatGetByte(luaL_checkinteger(L,1))); return 1; }
1205
static int memory_writebyte(lua_State *L) { FCEU_CheatSetByte(luaL_checkinteger(L,1), luaL_checkinteger(L,2)); return 0; }
1262
static int memory_readbyte(lua_State *L) {
1263
lua_pushinteger(L, FCEU_CheatGetByte(luaL_checkinteger(L,1)));
1267
static int memory_readbytesigned(lua_State *L) {
1268
signed char c = (signed char) FCEU_CheatGetByte(luaL_checkinteger(L,1));
1269
lua_pushinteger(L, c);
1273
static int GetWord(lua_State *L, bool isSigned)
1275
// little endian, unless the high byte address is specified as a 2nd parameter
1276
uint16 addressLow = luaL_checkinteger(L, 1);
1277
uint16 addressHigh = addressLow + 1;
1278
if (lua_type(L, 2) == LUA_TNUMBER)
1279
addressHigh = luaL_checkinteger(L, 2);
1280
uint16 result = FCEU_CheatGetByte(addressLow) | (FCEU_CheatGetByte(addressHigh) << 8);
1281
return isSigned ? (int16)result : result;
1284
static int memory_readword(lua_State *L)
1286
lua_pushinteger(L, GetWord(L, false));
1290
static int memory_readwordsigned(lua_State *L) {
1291
lua_pushinteger(L, GetWord(L, true));
1295
static int memory_writebyte(lua_State *L) {
1296
FCEU_CheatSetByte(luaL_checkinteger(L,1), luaL_checkinteger(L,2));
1206
1300
static int memory_readbyterange(lua_State *L) {
1208
1302
int range_start = luaL_checkinteger(L,1);
1524
// tobitstring(int value)
1526
// Converts byte to binary string
1527
static int tobitstring(lua_State *L)
1529
std::bitset<8> bits (luaL_checkinteger(L, 1));
1530
std::string temp = bits.to_string().insert(4, " ");
1531
const char * result = temp.c_str();
1532
lua_pushstring(L,result);
1430
1536
// like rawToCString, but will check if the global Lua function tostring()
1431
1537
// has been replaced with a custom function, and call that instead if so
1432
1538
static const char* toCString(lua_State* L, int idx)
1896
// Not for the signed versions though
1897
static int memory_readbytesigned(lua_State *L) {
1898
signed char c = (signed char) FCEU_CheatGetByte(luaL_checkinteger(L,1));
1899
lua_pushinteger(L, c);
1903
2002
static int memory_registerHook(lua_State* L, LuaMemHookType hookType, int defaultSize)
1905
2004
// get first argument: address
2619
//int emu.lagcount()
2718
// int emu.lagcount()
2621
// Gets the current lag count
2720
// Gets the current lag count
2622
2721
int emu_lagcount(lua_State *L) {
2624
2723
lua_pushinteger(L, FCEUI_GetLagCount());
2630
//Returns true if the game is currently on a lag frame
2729
// Returns true if the game is currently on a lag frame
2631
2730
int emu_lagged (lua_State *L) {
2633
2732
bool Lag_Frame = FCEUI_GetLagged();
2638
//emu_setlagflag(bool value)
2737
// emu.setlagflag(bool value)
2640
//Returns true if the game is currently on a lag frame
2739
// Returns true if the game is currently on a lag frame
2641
2740
int emu_setlagflag(lua_State *L)
2643
2742
FCEUI_SetLagFlag(lua_toboolean(L, 1) == 1);
2653
2752
// string movie.mode()
2655
// Returns "taseditor", "record", "playback", "finished" or nil
2754
// Returns "taseditor", "record", "playback", "finished" or nil
2656
2755
int movie_mode(lua_State *L)
2658
2757
if (FCEUMOV_Mode(MOVIEMODE_TASEDITOR))
4203
4302
lua_pushnumber(L, nesVolumes[0]);
4204
4303
lua_setfield(L, -2, "volume");
4205
freq = (39375000.0/352.0) / curfreq[0];
4304
freq = (39375000.0/352.0) / (curfreq[0] + 1);
4206
4305
lua_pushnumber(L, freq);
4207
4306
lua_setfield(L, -2, "frequency");
4208
4307
lua_pushnumber(L, (log(freq / 440.0) * 12 / log(2.0)) + 69);
4224
4323
lua_pushnumber(L, nesVolumes[1]);
4225
4324
lua_setfield(L, -2, "volume");
4226
freq = (39375000.0/352.0) / curfreq[1];
4325
freq = (39375000.0/352.0) / (curfreq[1] + 1);
4227
4326
lua_pushnumber(L, freq);
4228
4327
lua_setfield(L, -2, "frequency");
4229
4328
lua_pushnumber(L, (log(freq / 440.0) * 12 / log(2.0)) + 69);
4243
4342
lua_pushnumber(L, 1.0);
4244
4343
lua_setfield(L, -2, "volume");
4245
4344
freqReg = PSG[0xa] | ((PSG[0xb] & 7) << 8);
4246
freq = (39375000.0/704.0) / freqReg;
4345
freq = (39375000.0/704.0) / (freqReg + 1);
4247
4346
lua_pushnumber(L, freq);
4248
4347
lua_setfield(L, -2, "frequency");
4249
4348
lua_pushnumber(L, (log(freq / 440.0) * 12 / log(2.0)) + 69);
4406
// Debugger functions library
4408
// debugger.hitbreakpoint()
4409
static int debugger_hitbreakpoint(lua_State *L)
4415
// debugger.getcyclescount()
4416
static int debugger_getcyclescount(lua_State *L)
4418
int64 counter_value = timestampbase + (uint64)timestamp - total_cycles_base;
4419
if (counter_value < 0) // sanity check
4421
ResetDebugStatisticsCounters();
4424
lua_pushinteger(L, counter_value);
4428
// debugger.getinstructionscount()
4429
static int debugger_getinstructionscount(lua_State *L)
4431
lua_pushinteger(L, total_instructions);
4435
// debugger.resetcyclescount()
4436
static int debugger_resetcyclescount(lua_State *L)
4438
ResetCyclesCounter();
4442
// debugger.resetinstructionscount()
4443
static int debugger_resetinstructionscount(lua_State *L)
4445
ResetInstructionsCounter();
4307
4449
// TAS Editor functions library
4309
4451
// bool taseditor.registerauto()
5233
5375
{"readbyte", memory_readbyte},
5234
5376
{"readbyterange", memory_readbyterange},
5235
{"readbytesigned", memory_readbytesigned},
5236
// alternate naming scheme for unsigned
5237
{"readbyteunsigned", memory_readbyte},
5377
{"readbytesigned", memory_readbytesigned},
5378
{"readbyteunsigned", memory_readbyte}, // alternate naming scheme for unsigned
5379
{"readword", memory_readword},
5380
{"readwordsigned", memory_readwordsigned},
5381
{"readwordunsigned", memory_readword}, // alternate naming scheme for unsigned
5238
5382
{"writebyte", memory_writebyte},
5239
5383
{"getregister", memory_getregister},
5240
5384
{"setregister", memory_setregister},
5514
static const struct luaL_reg debuggerlib[] = {
5516
{"hitbreakpoint", debugger_hitbreakpoint},
5517
{"getcyclescount", debugger_getcyclescount},
5518
{"getinstructionscount", debugger_getinstructionscount},
5519
{"resetcyclescount", debugger_resetcyclescount},
5520
{"resetinstructionscount", debugger_resetinstructionscount},
5370
5524
static const struct luaL_reg taseditorlib[] = {
5372
5526
{"registerauto", taseditor_registerauto},
5474
5628
* Returns true on success, false on failure.
5476
5630
int FCEU_LoadLuaCode(const char *filename, const char *arg) {
5477
5636
if (filename != luaScriptName)
5479
5638
if (luaScriptName) free(luaScriptName);
5523
5682
luaL_register(L, "movie", movielib);
5524
5683
luaL_register(L, "gui", guilib);
5525
5684
luaL_register(L, "sound", soundlib);
5685
luaL_register(L, "debugger", debuggerlib);
5526
5686
luaL_register(L, "taseditor", taseditorlib);
5527
5687
luaL_register(L, "bit", bit_funcs); // LuaBitOp library
5528
5688
lua_settop(L, 0); // clean the stack, because each call to luaL_register leaves a table on top
5530
5690
// register a few utility functions outside of libraries (in the global namespace)
5531
5691
lua_register(L, "print", print);
5532
5692
lua_register(L, "tostring", tostring);
5693
lua_register(L, "tobitstring", tobitstring);
5533
5694
lua_register(L, "addressof", addressof);
5534
5695
lua_register(L, "copytable", copytable);
5538
5699
lua_register(L, "OR", bit_bor);
5539
5700
lua_register(L, "XOR", bit_bxor);
5540
5701
lua_register(L, "SHIFT", bit_bshift_emulua);
5541
lua_register(L, "BIT", bitbit);
5702
lua_register(L, "BIT", bitbit);
5631
5792
void FCEU_ReloadLuaCode()
5633
5794
if (!luaScriptName)
5634
FCEU_DispMessage("There's no script to reload.",0);
5797
// no script currently running, then try loading the most recent
5798
extern char *recent_lua[];
5799
char*& fname = recent_lua[0];
5800
extern void UpdateLuaConsole(const char* fname);
5803
UpdateLuaConsole(fname);
5804
FCEU_LoadLuaCode(fname);
5807
FCEU_DispMessage("There's no script to reload.", 0);
5810
FCEU_DispMessage("There's no script to reload.", 0);
5636
5814
FCEU_LoadLuaCode(luaScriptName);