diff --git a/plugins/cheatfind/init.lua b/plugins/cheatfind/init.lua index 55ef7beb00e..a7e850f5385 100644 --- a/plugins/cheatfind/init.lua +++ b/plugins/cheatfind/init.lua @@ -40,6 +40,15 @@ function cheatfind.startplugin() return ram end + -- return table of share regions + function cheat.getshares() + local shares = {} + for tag, share in pairs(manager:machine():memory().shares) do + shares[tag] = share + end + return shares + end + -- save data block function cheat.save(space, start, size) local data = { block = "", start = start, size = size, space = space } @@ -274,6 +283,10 @@ function cheatfind.startplugin() for tag, ram in pairs(space_table) do devtable[#devtable + 1] = { tag = tag, space = ram.dev, ram = {{ offset = 0, size = ram.size }} } end + space_table = cheat.getshares() + for tag, share in pairs(space_table) do + devtable[#devtable + 1] = { tag = tag, space = share, ram = {{ offset = 0, size = share.size }} } + end end emu.register_start(start) diff --git a/src/frontend/mame/luaengine.cpp b/src/frontend/mame/luaengine.cpp index dade6bc99f3..b5c698c0eb5 100644 --- a/src/frontend/mame/luaengine.cpp +++ b/src/frontend/mame/luaengine.cpp @@ -587,7 +587,25 @@ luabridge::LuaRef lua_engine::l_memory_get_regions(const memory_manager *m) luabridge::LuaRef table = luabridge::LuaRef::newTable(L); for (auto ®ion: mm->regions()) { - table[region.second->name()] = ®ion; + table[region.second->name()] = region.second.get(); + } + + return table; +} + +//------------------------------------------------- +// memory_shares - return memory_shares +// -> manager:machine():memory().share[":maincpu"] +//------------------------------------------------- + +luabridge::LuaRef lua_engine::l_memory_get_shares(const memory_manager *m) +{ + memory_manager *mm = const_cast(m); + lua_State *L = luaThis->m_lua_state; + luabridge::LuaRef table = luabridge::LuaRef::newTable(L); + + for (auto &share: mm->shares()) { + table[share.first] = share.second.get(); } return table; @@ -1063,7 +1081,7 @@ int lua_engine::lua_addr_space::l_direct_mem_write(lua_State *L) //------------------------------------------------- // region_read - templated region readers for , -// -> manager:machine():memory().region[":maincpu"]:read_i8(0xC000) +// -> manager:machine():memory().regions[":maincpu"]:read_i8(0xC000) //------------------------------------------------- template @@ -1097,7 +1115,7 @@ int lua_engine::lua_memory_region::l_region_read(lua_State *L) //------------------------------------------------- // region_write - templated region writer for , -// -> manager:machine():memory().region[":maincpu"]:write_u16(0xC000, 0xF00D) +// -> manager:machine():memory().regions[":maincpu"]:write_u16(0xC000, 0xF00D) //------------------------------------------------- template @@ -1124,6 +1142,71 @@ int lua_engine::lua_memory_region::l_region_write(lua_State *L) return 0; } +//------------------------------------------------- +// share_read - templated share readers for , +// -> manager:machine():memory().shares[":maincpu"]:read_i8(0xC000) +//------------------------------------------------- + +template +int lua_engine::lua_memory_share::l_share_read(lua_State *L) +{ + memory_share &share = luabridge::Stack::get(L, 1); + luaL_argcheck(L, lua_isnumber(L, 2), 2, "address (integer) expected"); + offs_t address = lua_tounsigned(L, 2); + T mem_content = 0; + offs_t lowmask = share.bytewidth() - 1; + UINT8* ptr = (UINT8*)share.ptr(); + for(int i = 0; i < sizeof(T); i++) + { + int addr = share.endianness() == ENDIANNESS_LITTLE ? address + sizeof(T) - 1 - i : address + i; + if(addr >= share.bytes()) + continue; + mem_content <<= 8; + if(share.endianness() == ENDIANNESS_BIG) + mem_content |= ptr[(BYTE8_XOR_BE(addr) & lowmask) | (addr & ~lowmask)]; + else + mem_content |= ptr[(BYTE8_XOR_LE(addr) & lowmask) | (addr & ~lowmask)]; + } + + if (std::numeric_limits::is_signed) { + lua_pushinteger(L, mem_content); + } else { + lua_pushunsigned(L, mem_content); + } + + return 1; +} + +//------------------------------------------------- +// share_write - templated share writer for , +// -> manager:machine():memory().shares[":maincpu"]:write_u16(0xC000, 0xF00D) +//------------------------------------------------- + +template +int lua_engine::lua_memory_share::l_share_write(lua_State *L) +{ + memory_share &share = luabridge::Stack::get(L, 1); + luaL_argcheck(L, lua_isnumber(L, 2), 2, "address (integer) expected"); + luaL_argcheck(L, lua_isnumber(L, 3), 3, "value (integer) expected"); + offs_t address = lua_tounsigned(L, 2); + T val = lua_tounsigned(L, 3); + offs_t lowmask = share.bytewidth() - 1; + UINT8* ptr = (UINT8*)share.ptr(); + for(int i = 0; i < sizeof(T); i++) + { + int addr = share.endianness() == ENDIANNESS_BIG ? address + sizeof(T) - 1 - i : address + i; + if(addr >= share.bytes()) + continue; + if(share.endianness() == ENDIANNESS_BIG) + ptr[(BYTE8_XOR_BE(addr) & lowmask) | (addr & ~lowmask)] = val & 0xff; + else + ptr[(BYTE8_XOR_BE(addr) & lowmask) | (addr & ~lowmask)] = val & 0xff; + val >>= 8; + } + + return 0; +} + luabridge::LuaRef lua_engine::l_addr_space_map(const address_space *space) { lua_State *L = luaThis->m_lua_state; @@ -2375,6 +2458,7 @@ void lua_engine::initialize() .beginClass ("memory") .addProperty ("banks", &lua_engine::l_memory_get_banks) .addProperty ("regions", &lua_engine::l_memory_get_regions) + .addProperty ("shares", &lua_engine::l_memory_get_shares) .endClass() .beginClass ("lua_region") .addCFunction ("read_i8", &lua_memory_region::l_region_read) @@ -2394,6 +2478,30 @@ void lua_engine::initialize() .addCFunction ("write_i64", &lua_memory_region::l_region_write) .addCFunction ("write_u64", &lua_memory_region::l_region_write) .endClass() + .deriveClass ("region") + .addProperty ("size", &memory_region::bytes) + .endClass() + .beginClass ("lua_share") + .addCFunction ("read_i8", &lua_memory_share::l_share_read) + .addCFunction ("read_u8", &lua_memory_share::l_share_read) + .addCFunction ("read_i16", &lua_memory_share::l_share_read) + .addCFunction ("read_u16", &lua_memory_share::l_share_read) + .addCFunction ("read_i32", &lua_memory_share::l_share_read) + .addCFunction ("read_u32", &lua_memory_share::l_share_read) + .addCFunction ("read_i64", &lua_memory_share::l_share_read) + .addCFunction ("read_u64", &lua_memory_share::l_share_read) + .addCFunction ("write_i8", &lua_memory_share::l_share_write) + .addCFunction ("write_u8", &lua_memory_share::l_share_write) + .addCFunction ("write_i16", &lua_memory_share::l_share_write) + .addCFunction ("write_u16", &lua_memory_share::l_share_write) + .addCFunction ("write_i32", &lua_memory_share::l_share_write) + .addCFunction ("write_u32", &lua_memory_share::l_share_write) + .addCFunction ("write_i64", &lua_memory_share::l_share_write) + .addCFunction ("write_u64", &lua_memory_share::l_share_write) + .endClass() + .deriveClass ("region") + .addProperty ("size", &memory_share::bytes) + .endClass() .beginClass ("output") .addFunction ("set_value", &output_manager::set_value) .addFunction ("set_indexed_value", &output_manager::set_indexed_value) @@ -2402,9 +2510,6 @@ void lua_engine::initialize() .addFunction ("name_to_id", &output_manager::name_to_id) .addFunction ("id_to_name", &output_manager::id_to_name) .endClass() - .deriveClass ("region") - .addProperty ("size", &memory_region::bytes) - .endClass() .beginClass ("image") .addFunction ("exists", &device_image_interface::exists) .addFunction ("filename", &device_image_interface::filename) diff --git a/src/frontend/mame/luaengine.h b/src/frontend/mame/luaengine.h index 8286916d4d1..a616fa1292f 100644 --- a/src/frontend/mame/luaengine.h +++ b/src/frontend/mame/luaengine.h @@ -198,6 +198,11 @@ private: }; static luabridge::LuaRef l_memory_get_banks(const memory_manager *m); + static luabridge::LuaRef l_memory_get_shares(const memory_manager *m); + struct lua_memory_share { + template int l_share_read(lua_State *L); + template int l_share_write(lua_State *L); + }; static luabridge::LuaRef l_memory_get_regions(const memory_manager *m); struct lua_memory_region { template int l_region_read(lua_State *L);