From c95ed9c31da9d390ba4b8052f7e5a198cd572abc Mon Sep 17 00:00:00 2001 From: Luca Bruno Date: Sat, 17 Jan 2015 23:29:07 +0100 Subject: [PATCH 1/7] core: link state entries to parent state interface In device state, link a single device_state_entry to its parent device_state_interface and expose a parent_state() getter. Signed-off-by: Luca Bruno --- src/emu/distate.c | 14 ++++++++------ src/emu/distate.h | 6 ++++-- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/emu/distate.c b/src/emu/distate.c index cee1f0a6c26..54e24b4d35a 100644 --- a/src/emu/distate.c +++ b/src/emu/distate.c @@ -49,8 +49,9 @@ const UINT64 device_state_entry::k_decimal_divisor[] = // device_state_entry - constructor //------------------------------------------------- -device_state_entry::device_state_entry(int index, const char *symbol, void *dataptr, UINT8 size) - : m_next(NULL), +device_state_entry::device_state_entry(int index, const char *symbol, void *dataptr, UINT8 size, device_state_interface *dev) + : m_device_state(dev), + m_next(NULL), m_index(index), m_dataptr(dataptr), m_datamask(0), @@ -86,8 +87,9 @@ device_state_entry::device_state_entry(int index, const char *symbol, void *data m_symbol.cpy("CURFLAGS"); } -device_state_entry::device_state_entry(int index) - : m_next(NULL), +device_state_entry::device_state_entry(int index, device_state_interface *dev) + : m_device_state(dev), + m_next(NULL), m_index(index), m_dataptr(NULL), m_datamask(0), @@ -523,7 +525,7 @@ device_state_entry &device_state_interface::state_add(int index, const char *sym assert(symbol != NULL); // allocate new entry - device_state_entry *entry = global_alloc(device_state_entry(index, symbol, data, size)); + device_state_entry *entry = global_alloc(device_state_entry(index, symbol, data, size, this)); // append to the end of the list m_state_list.append(*entry); @@ -543,7 +545,7 @@ device_state_entry &device_state_interface::state_add(int index, const char *sym device_state_entry &device_state_interface::state_add_divider(int index) { // allocate new entry - device_state_entry *entry = global_alloc(device_state_entry(index)); + device_state_entry *entry = global_alloc(device_state_entry(index, this)); // append to the end of the list m_state_list.append(*entry); diff --git a/src/emu/distate.h b/src/emu/distate.h index 7b5117e19e2..d3679b755e6 100644 --- a/src/emu/distate.h +++ b/src/emu/distate.h @@ -49,8 +49,8 @@ class device_state_entry private: // construction/destruction - device_state_entry(int index, const char *symbol, void *dataptr, UINT8 size); - device_state_entry(int index); + device_state_entry(int index, const char *symbol, void *dataptr, UINT8 size, device_state_interface *dev); + device_state_entry(int index, device_state_interface *dev); public: // post-construction modifiers @@ -70,6 +70,7 @@ public: const char *symbol() const { return m_symbol; } bool visible() const { return ((m_flags & DSF_NOSHOW) == 0); } bool divider() const { return m_flags & DSF_DIVIDER; } + device_state_interface *parent_state() const {return m_device_state;} protected: // device state flags @@ -98,6 +99,7 @@ protected: static const UINT64 k_decimal_divisor[20]; // divisors for outputting decimal values // public state description + device_state_interface *m_device_state; // link to parent device state device_state_entry * m_next; // link to next item UINT32 m_index; // index by which this item is referred generic_ptr m_dataptr; // pointer to where the data lives From 39788873b0b99c85bb6d8c6b5da9ead619b38411 Mon Sep 17 00:00:00 2001 From: Luca Bruno Date: Sun, 18 Jan 2015 16:28:09 +0100 Subject: [PATCH 2/7] save: factor-out presave/postload dispatchers Signed-off-by: Luca Bruno --- src/emu/save.c | 28 ++++++++++++++++++++++++---- src/emu/save.h | 4 ++++ 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/emu/save.c b/src/emu/save.c index d7d8ba57ab5..98c23caf4a0 100644 --- a/src/emu/save.c +++ b/src/emu/save.c @@ -213,6 +213,17 @@ save_error save_manager::check_file(running_machine &machine, emu_file &file, co return validate_header(header, gamename, sig, errormsg, ""); } +//------------------------------------------------- +// dispatch_postload - invoke all registered +// postload callbacks for updates +//------------------------------------------------- + + +void save_manager::dispatch_postload() +{ + for (state_callback *func = m_postload_list.first(); func != NULL; func = func->next()) + func->m_func(); +} //------------------------------------------------- // read_file - read the data from a file @@ -253,12 +264,22 @@ save_error save_manager::read_file(emu_file &file) } // call the post-load functions - for (state_callback *func = m_postload_list.first(); func != NULL; func = func->next()) - func->m_func(); + dispatch_postload(); return STATERR_NONE; } +//------------------------------------------------- +// dispatch_presave - invoke all registered +// presave callbacks for updates +//------------------------------------------------- + + +void save_manager::dispatch_presave() +{ + for (state_callback *func = m_presave_list.first(); func != NULL; func = func->next()) + func->m_func(); +} //------------------------------------------------- // write_file - writes the data to a file @@ -287,8 +308,7 @@ save_error save_manager::write_file(emu_file &file) file.compress(FCOMPRESS_MEDIUM); // call the pre-save functions - for (state_callback *func = m_presave_list.first(); func != NULL; func = func->next()) - func->m_func(); + dispatch_presave(); // then write all the data for (state_entry *entry = m_entry_list.first(); entry != NULL; entry = entry->next()) diff --git a/src/emu/save.h b/src/emu/save.h index 8fc537e1289..7e00daf3fd1 100644 --- a/src/emu/save.h +++ b/src/emu/save.h @@ -99,6 +99,10 @@ public: void register_presave(save_prepost_delegate func); void register_postload(save_prepost_delegate func); + // callback dispatching + void dispatch_presave(); + void dispatch_postload(); + // generic memory registration void save_memory(const char *module, const char *tag, UINT32 index, const char *name, void *val, UINT32 valsize, UINT32 valcount = 1); From 37cd9a2d98e4d07f394ec6f8d030bbfda3d1c4ef Mon Sep 17 00:00:00 2001 From: Luca Bruno Date: Sat, 17 Jan 2015 23:32:57 +0100 Subject: [PATCH 3/7] luaengine: rework state getter/setter for saves Improve state_get_value and state_set_value by using the parent device_state_interface and triggering callbacks for updates. While at it, also remove the hackish friend relationship. Signed-off-by: Luca Bruno --- src/emu/distate.h | 1 - src/emu/luaengine.c | 18 ++++++++++++++---- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/emu/distate.h b/src/emu/distate.h index d3679b755e6..1d16e81f793 100644 --- a/src/emu/distate.h +++ b/src/emu/distate.h @@ -45,7 +45,6 @@ class device_state_entry { friend class device_state_interface; friend class simple_list; - friend class lua_engine; private: // construction/destruction diff --git a/src/emu/luaengine.c b/src/emu/luaengine.c index f38a7386442..1f1e9b9db91 100644 --- a/src/emu/luaengine.c +++ b/src/emu/luaengine.c @@ -456,23 +456,33 @@ luabridge::LuaRef lua_engine::l_dev_get_states(const device_t *d) } //------------------------------------------------- -// state_get_value - return value of a devices state +// state_get_value - return value of a device state entry // -> manager:machine().devices[":maincpu"].state["PC"].value //------------------------------------------------- UINT64 lua_engine::l_state_get_value(const device_state_entry *d) { - return d->value(); + device_state_interface *state = d->parent_state(); + if(state) { + luaThis->machine().save().dispatch_presave(); + return state->state_int(d->index()); + } else { + return 0; + } } //------------------------------------------------- -// state_set_value - set value of a devices state +// state_set_value - set value of a device state entry // -> manager:machine().devices[":maincpu"].state["D0"].value = 0x0c00 //------------------------------------------------- void lua_engine::l_state_set_value(device_state_entry *d, UINT64 val) { - d->set_value(val); + device_state_interface *state = d->parent_state(); + if(state) { + state->set_state_int(d->index(), val); + luaThis->machine().save().dispatch_presave(); + } } //------------------------------------------------- From 8f2be7dbe72da72c52e5c07b8ef877768e0bc3b8 Mon Sep 17 00:00:00 2001 From: Luca Bruno Date: Thu, 15 Jan 2015 19:41:51 +0100 Subject: [PATCH 4/7] luaengine: use visible_area for drawing Drawing and scripts should use the actual visible_area, not the maximum declared screen size. Signed-off-by: Luca Bruno --- src/emu/luaengine.c | 56 +++++++++++++++++++++++++++++++++++---------- src/emu/luaengine.h | 2 ++ 2 files changed, 46 insertions(+), 12 deletions(-) diff --git a/src/emu/luaengine.c b/src/emu/luaengine.c index 1f1e9b9db91..0c037902899 100644 --- a/src/emu/luaengine.c +++ b/src/emu/luaengine.c @@ -536,6 +536,38 @@ int lua_engine::lua_addr_space::l_mem_read(lua_State *L) } +//------------------------------------------------- +// screen_height - return screen visible height +// -> manager:machine().screens[":screen"]:height() +//------------------------------------------------- + +int lua_engine::lua_screen::l_height(lua_State *L) +{ + screen_device *sc = luabridge::Stack::get(L, 1); + if(!sc) { + return 0; + } + + lua_pushunsigned(L, sc->visible_area().height()); + return 1; +} + +//------------------------------------------------- +// screen_width - return screen visible width +// -> manager:machine().screens[":screen"]:width() +//------------------------------------------------- + +int lua_engine::lua_screen::l_width(lua_State *L) +{ + screen_device *sc = luabridge::Stack::get(L, 1); + if(!sc) { + return 0; + } + + lua_pushunsigned(L, sc->visible_area().width()); + return 1; +} + //------------------------------------------------- // draw_box - draw a box on a screen container // -> manager:machine().screens[":screen"]:draw_box(x1, y1, x2, y2, bgcolor, linecolor) @@ -558,10 +590,10 @@ int lua_engine::lua_screen::l_draw_box(lua_State *L) // retrieve all parameters float x1, y1, x2, y2; - x1 = MIN(lua_tounsigned(L, 2) / static_cast(sc->width()) , 1.0f); - y1 = MIN(lua_tounsigned(L, 3) / static_cast(sc->height()), 1.0f); - x2 = MIN(lua_tounsigned(L, 4) / static_cast(sc->width()) , 1.0f); - y2 = MIN(lua_tounsigned(L, 5) / static_cast(sc->height()), 1.0f); + x1 = MIN(lua_tounsigned(L, 2) / static_cast(sc->visible_area().width()) , 1.0f); + y1 = MIN(lua_tounsigned(L, 3) / static_cast(sc->visible_area().height()), 1.0f); + x2 = MIN(lua_tounsigned(L, 4) / static_cast(sc->visible_area().width()) , 1.0f); + y2 = MIN(lua_tounsigned(L, 5) / static_cast(sc->visible_area().height()), 1.0f); UINT32 bgcolor = lua_tounsigned(L, 6); UINT32 fgcolor = lua_tounsigned(L, 7); @@ -594,10 +626,10 @@ int lua_engine::lua_screen::l_draw_line(lua_State *L) // retrieve all parameters float x1, y1, x2, y2; - x1 = MIN(lua_tounsigned(L, 2) / static_cast(sc->width()) , 1.0f); - y1 = MIN(lua_tounsigned(L, 3) / static_cast(sc->height()), 1.0f); - x2 = MIN(lua_tounsigned(L, 4) / static_cast(sc->width()) , 1.0f); - y2 = MIN(lua_tounsigned(L, 5) / static_cast(sc->height()), 1.0f); + x1 = MIN(lua_tounsigned(L, 2) / static_cast(sc->visible_area().width()) , 1.0f); + y1 = MIN(lua_tounsigned(L, 3) / static_cast(sc->visible_area().height()), 1.0f); + x2 = MIN(lua_tounsigned(L, 4) / static_cast(sc->visible_area().width()) , 1.0f); + y2 = MIN(lua_tounsigned(L, 5) / static_cast(sc->visible_area().height()), 1.0f); UINT32 color = lua_tounsigned(L, 6); // draw the line @@ -623,8 +655,8 @@ int lua_engine::lua_screen::l_draw_text(lua_State *L) luaL_argcheck(L, lua_isstring(L, 4), 4, "message (string) expected"); // retrieve all parameters - float x = MIN(lua_tounsigned(L, 2) / static_cast(sc->width()) , 1.0f); - float y = MIN(lua_tounsigned(L, 3) / static_cast(sc->height()), 1.0f); + float x = MIN(lua_tounsigned(L, 2) / static_cast(sc->visible_area().width()) , 1.0f); + float y = MIN(lua_tounsigned(L, 3) / static_cast(sc->visible_area().height()), 1.0f); const char *msg = luaL_checkstring(L,4); // TODO: add optional parameters (colors, etc.) @@ -900,13 +932,13 @@ void lua_engine::initialize() .addCFunction ("draw_box", &lua_screen::l_draw_box) .addCFunction ("draw_line", &lua_screen::l_draw_line) .addCFunction ("draw_text", &lua_screen::l_draw_text) + .addCFunction ("height", &lua_screen::l_height) + .addCFunction ("width", &lua_screen::l_width) .endClass() .deriveClass ("screen_dev") .addFunction ("name", &screen_device::name) .addFunction ("shortname", &screen_device::shortname) .addFunction ("tag", &screen_device::tag) - .addFunction ("height", &screen_device::height) - .addFunction ("width", &screen_device::width) .endClass() .beginClass ("dev_space") .addFunction ("name", &device_state_entry::symbol) diff --git a/src/emu/luaengine.h b/src/emu/luaengine.h index f274119e918..8578b7cd880 100644 --- a/src/emu/luaengine.h +++ b/src/emu/luaengine.h @@ -115,6 +115,8 @@ private: }; static luabridge::LuaRef l_machine_get_screens(const running_machine *r); struct lua_screen { + int l_height(lua_State *L); + int l_width(lua_State *L); int l_draw_box(lua_State *L); int l_draw_line(lua_State *L); int l_draw_text(lua_State *L); From 8c5c9a4b6600adbd136835f0427a062089d21b0e Mon Sep 17 00:00:00 2001 From: Luca Bruno Date: Thu, 15 Jan 2015 19:45:23 +0100 Subject: [PATCH 5/7] luaengine: update copyright Signed-off-by: Luca Bruno --- src/emu/luaengine.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/emu/luaengine.c b/src/emu/luaengine.c index 0c037902899..5eb30524dbf 100644 --- a/src/emu/luaengine.c +++ b/src/emu/luaengine.c @@ -1,5 +1,5 @@ // license:BSD-3-Clause -// copyright-holders:Miodrag Milanovic +// copyright-holders:Miodrag Milanovic,Luca Bruno /*************************************************************************** luaengine.c From cda9c13fb3a82f62254aa6f076472923d5846762 Mon Sep 17 00:00:00 2001 From: Luca Bruno Date: Fri, 16 Jan 2015 16:59:20 +0100 Subject: [PATCH 6/7] luaengine: add memory writers Add methods for memory writing, similarly to existing readers. Signed-off-by: Luca Bruno --- src/emu/luaengine.c | 54 +++++++++++++++++++++++++++++++++++++++++++++ src/emu/luaengine.h | 1 + 2 files changed, 55 insertions(+) diff --git a/src/emu/luaengine.c b/src/emu/luaengine.c index 5eb30524dbf..0cb4a7ef29c 100644 --- a/src/emu/luaengine.c +++ b/src/emu/luaengine.c @@ -536,6 +536,52 @@ int lua_engine::lua_addr_space::l_mem_read(lua_State *L) } +//------------------------------------------------- +// mem_write - templated memory writer for , +// -> manager:machine().devices[":maincpu"].spaces["program"]:write_u16(0xC000, 0xF00D) +//------------------------------------------------- + +template +int lua_engine::lua_addr_space::l_mem_write(lua_State *L) +{ + address_space &sp = 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); + + switch(sizeof(val) * 8) { + case 8: + sp.write_byte(address, val); + break; + case 16: + if ((address & 1) == 0) { + sp.write_word(address, val); + } else { + sp.read_word_unaligned(address, val); + } + break; + case 32: + if ((address & 3) == 0) { + sp.write_dword(address, val); + } else { + sp.write_dword_unaligned(address, val); + } + break; + case 64: + if ((address & 7) == 0) { + sp.write_qword(address, val); + } else { + sp.write_qword_unaligned(address, val); + } + break; + default: + break; + } + + return 0; +} + //------------------------------------------------- // screen_height - return screen visible height // -> manager:machine().screens[":screen"]:height() @@ -924,6 +970,14 @@ void lua_engine::initialize() .addCFunction ("read_u32", &lua_addr_space::l_mem_read) .addCFunction ("read_i64", &lua_addr_space::l_mem_read) .addCFunction ("read_u64", &lua_addr_space::l_mem_read) + .addCFunction ("write_i8", &lua_addr_space::l_mem_write) + .addCFunction ("write_u8", &lua_addr_space::l_mem_write) + .addCFunction ("write_i16", &lua_addr_space::l_mem_write) + .addCFunction ("write_u16", &lua_addr_space::l_mem_write) + .addCFunction ("write_i32", &lua_addr_space::l_mem_write) + .addCFunction ("write_u32", &lua_addr_space::l_mem_write) + .addCFunction ("write_i64", &lua_addr_space::l_mem_write) + .addCFunction ("write_u64", &lua_addr_space::l_mem_write) .endClass() .deriveClass ("addr_space") .addFunction("name", &address_space::name) diff --git a/src/emu/luaengine.h b/src/emu/luaengine.h index 8578b7cd880..5f54b68d2fd 100644 --- a/src/emu/luaengine.h +++ b/src/emu/luaengine.h @@ -112,6 +112,7 @@ private: static luabridge::LuaRef l_dev_get_memspaces(const device_t *d); struct lua_addr_space { template int l_mem_read(lua_State *L); + template int l_mem_write(lua_State *L); }; static luabridge::LuaRef l_machine_get_screens(const running_machine *r); struct lua_screen { From f63811d659883579de57258cee0b6b5d62ceffea Mon Sep 17 00:00:00 2001 From: Luca Bruno Date: Sat, 17 Jan 2015 23:05:33 +0100 Subject: [PATCH 7/7] luaengine: add per-screen frame_number() getter Add method to retrieve frame counter for each machine screen. Signed-off-by: Luca Bruno --- src/emu/luaengine.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/emu/luaengine.c b/src/emu/luaengine.c index 0cb4a7ef29c..5431e28c341 100644 --- a/src/emu/luaengine.c +++ b/src/emu/luaengine.c @@ -990,6 +990,7 @@ void lua_engine::initialize() .addCFunction ("width", &lua_screen::l_width) .endClass() .deriveClass ("screen_dev") + .addFunction ("frame_number", &screen_device::frame_number) .addFunction ("name", &screen_device::name) .addFunction ("shortname", &screen_device::shortname) .addFunction ("tag", &screen_device::tag)