From 954fc5d5dd13ae5c4c622f40069230978e10c352 Mon Sep 17 00:00:00 2001 From: feos-tas Date: Tue, 23 Jun 2015 21:04:31 +0000 Subject: [PATCH] Lua: rom.writebyte() and gethash(). --- trunk/src/fceu.cpp | 21 +++++++++++++++------ trunk/src/fceu.h | 1 + trunk/src/lua-engine.cpp | 32 +++++++++++++++++++++++++++++--- 3 files changed, 45 insertions(+), 9 deletions(-) diff --git a/trunk/src/fceu.cpp b/trunk/src/fceu.cpp index 838f4947..8aa27371 100644 --- a/trunk/src/fceu.cpp +++ b/trunk/src/fceu.cpp @@ -1186,8 +1186,6 @@ void FCEUXGameInterface(GI command) { } } - - bool FCEUXLoad(const char *name, FCEUFILE *fp) { //read ines header iNES_HEADER head; @@ -1239,11 +1237,22 @@ bool FCEUXLoad(const char *name, FCEUFILE *fp) { return true; } - uint8 FCEU_ReadRomByte(uint32 i) { extern iNES_HEADER head; - if (i < 16) return *((unsigned char*)&head + i); - if (i < 16 + PRGsize[0]) return PRGptr[0][i - 16]; - if (i < 16 + PRGsize[0] + CHRsize[0]) return CHRptr[0][i - 16 - PRGsize[0]]; + if (i < 16) + return *((unsigned char*)&head + i); + if (i < 16 + PRGsize[0]) + return PRGptr[0][i - 16]; + if (i < 16 + PRGsize[0] + CHRsize[0]) + return CHRptr[0][i - 16 - PRGsize[0]]; return 0; } + +void FCEU_WriteRomByte(uint32 i, uint8 value) { + if (i < 16) + MessageBox(hMemView,"Sorry", "You can't edit the ROM header.", MB_OK); + if (i < 16 + PRGsize[0]) + PRGptr[0][i - 16] = value; + if (i < 16 + PRGsize[0] + CHRsize[0]) + CHRptr[0][i - 16 - PRGsize[0]] = value; +} diff --git a/trunk/src/fceu.h b/trunk/src/fceu.h index bc1af8d8..b8046a96 100644 --- a/trunk/src/fceu.h +++ b/trunk/src/fceu.h @@ -61,6 +61,7 @@ extern uint8 *RAM; //shared memory modifications extern int EmulationPaused; uint8 FCEU_ReadRomByte(uint32 i); +void FCEU_WriteRomByte(uint32 i, uint8 value); extern readfunc ARead[0x10000]; extern writefunc BWrite[0x10000]; diff --git a/trunk/src/lua-engine.cpp b/trunk/src/lua-engine.cpp index 584baba5..1faed825 100644 --- a/trunk/src/lua-engine.cpp +++ b/trunk/src/lua-engine.cpp @@ -23,6 +23,7 @@ #include "x6502.h" #include "utils/xstring.h" #include "utils/memory.h" +#include "utils/crc32.h" #include "fceulua.h" #ifdef WIN32 @@ -1258,6 +1259,19 @@ static int rom_readbytesigned(lua_State *L) { return 1; } +// doesn't keep backups to allow maximum speed (for automatic rom corruptors and stuff) +// keeping them might be an option though, just need to use memview's ApplyPatch() +// that'd also highlight the edits in hex editor +static int rom_writebyte(lua_State *L) +{ + uint32 address = luaL_checkinteger(L,1); + if (address < 16) + luaL_error(L,"rom.writebyte() can't edit the ROM header."); + else + FCEU_WriteRomByte(address, luaL_checkinteger(L,2)); + return 1; +} + static int rom_gethash(lua_State *L) { const char *type = luaL_checkstring(L, 1); if(!type) lua_pushstring(L, ""); @@ -1590,6 +1604,17 @@ static int print(lua_State *L) return 0; } +// gethash() +// +// Returns the crc32 hashsum of an arbitrary buffer +static int gethash(lua_State *L) { + uint8 *buffer = (uint8 *)luaL_checkstring(L, 1); + int size = luaL_checkinteger(L,2); + int hash = CalcCRC32(0, buffer, size); + lua_pushinteger(L, hash); + return 1; +} + // provides an easy way to copy a table from Lua // (simple assignment only makes an alias, but sometimes an independent table is desired) // currently this function only performs a shallow copy, @@ -3586,8 +3611,8 @@ static int gui_gdscreenshot(lua_State *L) { *ptr++ = (65534 ) & 0xFF; *ptr++ = (width >> 8) & 0xFF; *ptr++ = (width ) & 0xFF; - *ptr++ = (height >> 8) & 0xFF; - *ptr++ = (height ) & 0xFF; + *ptr++ = (height>> 8) & 0xFF; + *ptr++ = (height ) & 0xFF; *ptr++ = 1; *ptr++ = 255; *ptr++ = 255; @@ -5371,7 +5396,7 @@ static const struct luaL_reg romlib [] = { {"readbytesigned", rom_readbytesigned}, // alternate naming scheme for unsigned {"readbyteunsigned", rom_readbyte}, - + {"writebyte", rom_writebyte}, {"gethash", rom_gethash}, {NULL,NULL} }; @@ -5699,6 +5724,7 @@ int FCEU_LoadLuaCode(const char *filename, const char *arg) { // register a few utility functions outside of libraries (in the global namespace) lua_register(L, "print", print); + lua_register(L, "gethash", gethash), lua_register(L, "tostring", tostring); lua_register(L, "tobitstring", tobitstring); lua_register(L, "addressof", addressof);