~ubuntu-branches/ubuntu/vivid/fceux/vivid

« back to all changes in this revision

Viewing changes to src/lua-engine.cpp

  • Committer: Package Import Robot
  • Author(s): Joe Nahmias
  • Date: 2014-03-02 19:22:04 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20140302192204-9f0aehi5stfnhn7d
Tags: 2.2.2+dfsg0-1
* Imported Upstream version 2.2.2
  + remove patches merged upstream; refresh remaining
  + remove windows compiled help files and non-free Visual C files
* Use C++11 standard static assertion functionality
* fix upstream installation of support files
* New patch 0004-ignore-missing-windows-help-CHM-file.patch
* update d/copyright for new, renamed, deleted files
* d/control: bump std-ver to 3.9.5, no changes needed

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#include <stdio.h>
2
 
#include <stdlib.h>
3
 
#include <string.h>
4
 
#include <ctype.h>
5
 
#include <zlib.h>
6
 
#include <assert.h>
7
 
#include <vector>
8
 
#include <map>
9
 
#include <string>
10
 
#include <algorithm>
11
 
#include <stdlib.h>
12
 
#include <math.h>
13
 
 
14
1
#ifdef __linux
15
2
#include <unistd.h>
16
3
#define SetCurrentDir chdir
26
13
#include "types.h"
27
14
#include "fceu.h"
28
15
#include "video.h"
 
16
#include "debug.h"
29
17
#include "sound.h"
30
18
#include "drawing.h"
31
19
#include "state.h"
33
21
#include "driver.h"
34
22
#include "cheat.h"
35
23
#include "x6502.h"
36
 
#include "x6502abbrev.h"
37
24
#include "utils/xstring.h"
38
25
#include "utils/memory.h"
39
26
#include "fceulua.h"
48
35
extern TASEDITOR_LUA taseditor_lua;
49
36
#endif
50
37
 
 
38
#include <cstdio>
 
39
#include <cstdlib>
 
40
#include <cstring>
 
41
#include <cctype>
 
42
#include <cassert>
 
43
#include <cstdlib>
 
44
#include <cmath>
 
45
#include <zlib.h>
 
46
 
 
47
#include <vector>
 
48
#include <map>
 
49
#include <string>
 
50
#include <algorithm>
 
51
#include <bitset>
 
52
 
 
53
#include "x6502abbrev.h"
 
54
 
 
55
bool CheckLua()
 
56
{
 
57
#ifdef WIN32
 
58
        HMODULE mod = LoadLibrary("lua51.dll");
 
59
        if(!mod)
 
60
        {
 
61
                return false;
 
62
        }
 
63
        FreeLibrary(mod);
 
64
        return true;
 
65
#else
 
66
        return true;
 
67
#endif
 
68
}
 
69
 
 
70
bool DemandLua()
 
71
{
 
72
#ifdef WIN32
 
73
        if(!CheckLua())
 
74
        {
 
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);
 
76
                return false;
 
77
        }
 
78
        return true;
 
79
#else
 
80
        return true;
 
81
#endif
 
82
}
 
83
 
51
84
extern "C"
52
85
{
53
86
#include <lua.h>
54
87
#include <lauxlib.h>
55
88
#include <lualib.h>
56
89
#ifdef WIN32
 
90
#include <lstate.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);
207
241
};
208
242
 
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");
211
245
 
212
246
static const char* luaMemHookTypeStrings [] =
213
247
{
221
255
};
222
256
 
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");
225
259
 
226
260
static char* rawToCString(lua_State* L, int idx=0);
227
261
static const char* toCString(lua_State* L, int idx=0);
1156
1190
 
1157
1191
                if (lua_isfunction(L, -1))
1158
1192
                {
 
1193
#ifdef WIN32
 
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
 
1200
 
 
1201
                        int prevGarbage = lua_gc(L, LUA_GCCOUNT, 0);
 
1202
 
 
1203
                        lua_pushinteger(L, savestateNumber);
 
1204
                        saveData.LoadRecord(L, LUA_DATARECORDKEY, numParamsExpected);
 
1205
#else
1159
1206
                        int prevGarbage = lua_gc(L, LUA_GCCOUNT, 0);
1160
1207
 
1161
1208
                        lua_pushinteger(L, savestateNumber);
1162
1209
                        saveData.LoadRecord(L, LUA_DATARECORDKEY, (unsigned int) -1);
 
1210
#endif
 
1211
 
1163
1212
                        int n = lua_gettop(L) - 1;
1164
1213
 
1165
1214
                        int ret = lua_pcall(L, n, 0, 0);
1192
1241
}
1193
1242
 
1194
1243
 
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)));
 
1246
        return 1;
 
1247
}
 
1248
 
 
1249
static int rom_readbytesigned(lua_State *L) {
 
1250
        lua_pushinteger(L, (signed char)FCEU_ReadRomByte(luaL_checkinteger(L,1)));
 
1251
        return 1;
 
1252
}
 
1253
 
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, "");
1202
1259
        return 1;
1203
1260
}
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; }
 
1261
 
 
1262
static int memory_readbyte(lua_State *L) {
 
1263
        lua_pushinteger(L, FCEU_CheatGetByte(luaL_checkinteger(L,1)));
 
1264
        return 1;
 
1265
}
 
1266
 
 
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);
 
1270
        return 1;
 
1271
}
 
1272
 
 
1273
static int GetWord(lua_State *L, bool isSigned)
 
1274
{
 
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;
 
1282
}
 
1283
 
 
1284
static int memory_readword(lua_State *L)
 
1285
{
 
1286
        lua_pushinteger(L, GetWord(L, false));
 
1287
        return 1;
 
1288
}
 
1289
 
 
1290
static int memory_readwordsigned(lua_State *L) {
 
1291
        lua_pushinteger(L, GetWord(L, true));
 
1292
        return 1;
 
1293
}
 
1294
 
 
1295
static int memory_writebyte(lua_State *L) {
 
1296
        FCEU_CheatSetByte(luaL_checkinteger(L,1), luaL_checkinteger(L,2));
 
1297
        return 0;
 
1298
}
 
1299
 
1206
1300
static int memory_readbyterange(lua_State *L) {
1207
1301
 
1208
1302
        int range_start = luaL_checkinteger(L,1);
1427
1521
        return 1;
1428
1522
}
1429
1523
 
 
1524
// tobitstring(int value)
 
1525
//
 
1526
//   Converts byte to binary string
 
1527
static int tobitstring(lua_State *L)
 
1528
{
 
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);
 
1533
        return 1;
 
1534
}
 
1535
 
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)
1893
1999
}
1894
2000
#endif
1895
2001
 
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);
1900
 
        return 1;
1901
 
}
1902
 
 
1903
2002
static int memory_registerHook(lua_State* L, LuaMemHookType hookType, int defaultSize)
1904
2003
{
1905
2004
        // get first argument: address
2616
2715
        return 1;
2617
2716
}
2618
2717
 
2619
 
//int emu.lagcount()
 
2718
// int emu.lagcount()
2620
2719
//
2621
 
// Gets the current lag count
 
2720
//   Gets the current lag count
2622
2721
int emu_lagcount(lua_State *L) {
2623
2722
 
2624
2723
        lua_pushinteger(L, FCEUI_GetLagCount());
2625
2724
        return 1;
2626
2725
}
2627
2726
 
2628
 
//emu_lagged()
 
2727
// emu.lagged()
2629
2728
//
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) {
2632
2731
 
2633
2732
        bool Lag_Frame = FCEUI_GetLagged();
2635
2734
        return 1;
2636
2735
}
2637
2736
 
2638
 
//emu_setlagflag(bool value)
 
2737
// emu.setlagflag(bool value)
2639
2738
//
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)
2642
2741
{
2643
2742
        FCEUI_SetLagFlag(lua_toboolean(L, 1) == 1);
2652
2751
 
2653
2752
// string movie.mode()
2654
2753
//
2655
 
// Returns "taseditor", "record", "playback", "finished" or nil
 
2754
//   Returns "taseditor", "record", "playback", "finished" or nil
2656
2755
int movie_mode(lua_State *L)
2657
2756
{
2658
2757
        if (FCEUMOV_Mode(MOVIEMODE_TASEDITOR))
3078
3177
static const struct ColorMapping
3079
3178
{
3080
3179
        const char* name;
3081
 
        int value;
 
3180
        unsigned int value;
3082
3181
}
3083
3182
s_colorMapping [] =
3084
3183
{
4202
4301
        else
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);
4223
4322
        else
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);
4304
4403
        return 1;
4305
4404
}
4306
4405
 
 
4406
// Debugger functions library
 
4407
 
 
4408
// debugger.hitbreakpoint()
 
4409
static int debugger_hitbreakpoint(lua_State *L)
 
4410
{
 
4411
        break_asap = true;
 
4412
        return 0;
 
4413
}
 
4414
 
 
4415
// debugger.getcyclescount()
 
4416
static int debugger_getcyclescount(lua_State *L)
 
4417
{
 
4418
        int64 counter_value = timestampbase + (uint64)timestamp - total_cycles_base;
 
4419
        if (counter_value < 0)  // sanity check
 
4420
        {
 
4421
                ResetDebugStatisticsCounters();
 
4422
                counter_value = 0;
 
4423
        }
 
4424
        lua_pushinteger(L, counter_value);
 
4425
        return 1;
 
4426
}
 
4427
 
 
4428
// debugger.getinstructionscount()
 
4429
static int debugger_getinstructionscount(lua_State *L)
 
4430
{
 
4431
        lua_pushinteger(L, total_instructions);
 
4432
        return 1;
 
4433
}
 
4434
 
 
4435
// debugger.resetcyclescount()
 
4436
static int debugger_resetcyclescount(lua_State *L)
 
4437
{
 
4438
        ResetCyclesCounter();
 
4439
        return 0;
 
4440
}
 
4441
 
 
4442
// debugger.resetinstructionscount()
 
4443
static int debugger_resetinstructionscount(lua_State *L)
 
4444
{
 
4445
        ResetInstructionsCounter();
 
4446
        return 0;
 
4447
}
 
4448
 
4307
4449
// TAS Editor functions library
4308
4450
 
4309
4451
// bool taseditor.registerauto()
5232
5374
 
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},
5367
5511
        {NULL,NULL}
5368
5512
};
5369
5513
 
 
5514
static const struct luaL_reg debuggerlib[] = {
 
5515
 
 
5516
        {"hitbreakpoint", debugger_hitbreakpoint},
 
5517
        {"getcyclescount", debugger_getcyclescount},
 
5518
        {"getinstructionscount", debugger_getinstructionscount},
 
5519
        {"resetcyclescount", debugger_resetcyclescount},
 
5520
        {"resetinstructionscount", debugger_resetinstructionscount},
 
5521
        {NULL,NULL}
 
5522
};
 
5523
 
5370
5524
static const struct luaL_reg taseditorlib[] = {
5371
5525
 
5372
5526
        {"registerauto", taseditor_registerauto},
5474
5628
 * Returns true on success, false on failure.
5475
5629
 */
5476
5630
int FCEU_LoadLuaCode(const char *filename, const char *arg) {
 
5631
        if (!DemandLua())
 
5632
        {
 
5633
                return 0;
 
5634
        }
 
5635
 
5477
5636
        if (filename != luaScriptName)
5478
5637
        {
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);
5535
5696
 
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);         
5542
5703
 
5543
5704
                if (arg)
5544
5705
                {
5631
5792
void FCEU_ReloadLuaCode()
5632
5793
{
5633
5794
        if (!luaScriptName)
5634
 
                FCEU_DispMessage("There's no script to reload.",0);
5635
 
        else
 
5795
        {
 
5796
#ifdef WIN32
 
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);
 
5801
                if (fname)
 
5802
                {
 
5803
                        UpdateLuaConsole(fname);
 
5804
                        FCEU_LoadLuaCode(fname);
 
5805
                } else
 
5806
                {
 
5807
                        FCEU_DispMessage("There's no script to reload.", 0);
 
5808
                }
 
5809
#else
 
5810
                FCEU_DispMessage("There's no script to reload.", 0);
 
5811
#endif
 
5812
        } else
 
5813
        {
5636
5814
                FCEU_LoadLuaCode(luaScriptName);
 
5815
        }
5637
5816
}
5638
5817
 
5639
5818
 
5645
5824
 */
5646
5825
void FCEU_LuaStop() {
5647
5826
 
 
5827
        if (!CheckLua())
 
5828
                return;
 
5829
 
5648
5830
        //already killed
5649
5831
        if (!L) return;
5650
5832