Lua: rom.writebyte() and gethash().

This commit is contained in:
feos-tas 2015-06-23 21:04:31 +00:00
parent 3be370f776
commit 954fc5d5dd
3 changed files with 45 additions and 9 deletions

View File

@ -1186,8 +1186,6 @@ void FCEUXGameInterface(GI command) {
} }
} }
bool FCEUXLoad(const char *name, FCEUFILE *fp) { bool FCEUXLoad(const char *name, FCEUFILE *fp) {
//read ines header //read ines header
iNES_HEADER head; iNES_HEADER head;
@ -1239,11 +1237,22 @@ bool FCEUXLoad(const char *name, FCEUFILE *fp) {
return true; return true;
} }
uint8 FCEU_ReadRomByte(uint32 i) { uint8 FCEU_ReadRomByte(uint32 i) {
extern iNES_HEADER head; extern iNES_HEADER head;
if (i < 16) return *((unsigned char*)&head + i); if (i < 16)
if (i < 16 + PRGsize[0]) return PRGptr[0][i - 16]; return *((unsigned char*)&head + i);
if (i < 16 + PRGsize[0] + CHRsize[0]) return CHRptr[0][i - 16 - PRGsize[0]]; 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; 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;
}

View File

@ -61,6 +61,7 @@ extern uint8 *RAM; //shared memory modifications
extern int EmulationPaused; extern int EmulationPaused;
uint8 FCEU_ReadRomByte(uint32 i); uint8 FCEU_ReadRomByte(uint32 i);
void FCEU_WriteRomByte(uint32 i, uint8 value);
extern readfunc ARead[0x10000]; extern readfunc ARead[0x10000];
extern writefunc BWrite[0x10000]; extern writefunc BWrite[0x10000];

View File

@ -23,6 +23,7 @@
#include "x6502.h" #include "x6502.h"
#include "utils/xstring.h" #include "utils/xstring.h"
#include "utils/memory.h" #include "utils/memory.h"
#include "utils/crc32.h"
#include "fceulua.h" #include "fceulua.h"
#ifdef WIN32 #ifdef WIN32
@ -1258,6 +1259,19 @@ static int rom_readbytesigned(lua_State *L) {
return 1; 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) { static int rom_gethash(lua_State *L) {
const char *type = luaL_checkstring(L, 1); const char *type = luaL_checkstring(L, 1);
if(!type) lua_pushstring(L, ""); if(!type) lua_pushstring(L, "");
@ -1590,6 +1604,17 @@ static int print(lua_State *L)
return 0; 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 // provides an easy way to copy a table from Lua
// (simple assignment only makes an alias, but sometimes an independent table is desired) // (simple assignment only makes an alias, but sometimes an independent table is desired)
// currently this function only performs a shallow copy, // currently this function only performs a shallow copy,
@ -3586,8 +3611,8 @@ static int gui_gdscreenshot(lua_State *L) {
*ptr++ = (65534 ) & 0xFF; *ptr++ = (65534 ) & 0xFF;
*ptr++ = (width >> 8) & 0xFF; *ptr++ = (width >> 8) & 0xFF;
*ptr++ = (width ) & 0xFF; *ptr++ = (width ) & 0xFF;
*ptr++ = (height >> 8) & 0xFF; *ptr++ = (height>> 8) & 0xFF;
*ptr++ = (height ) & 0xFF; *ptr++ = (height ) & 0xFF;
*ptr++ = 1; *ptr++ = 1;
*ptr++ = 255; *ptr++ = 255;
*ptr++ = 255; *ptr++ = 255;
@ -5371,7 +5396,7 @@ static const struct luaL_reg romlib [] = {
{"readbytesigned", rom_readbytesigned}, {"readbytesigned", rom_readbytesigned},
// alternate naming scheme for unsigned // alternate naming scheme for unsigned
{"readbyteunsigned", rom_readbyte}, {"readbyteunsigned", rom_readbyte},
{"writebyte", rom_writebyte},
{"gethash", rom_gethash}, {"gethash", rom_gethash},
{NULL,NULL} {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) // register a few utility functions outside of libraries (in the global namespace)
lua_register(L, "print", print); lua_register(L, "print", print);
lua_register(L, "gethash", gethash),
lua_register(L, "tostring", tostring); lua_register(L, "tostring", tostring);
lua_register(L, "tobitstring", tobitstring); lua_register(L, "tobitstring", tobitstring);
lua_register(L, "addressof", addressof); lua_register(L, "addressof", addressof);