Previous 199869 Revisions Next

r34047 Wednesday 24th December, 2014 at 10:37:52 UTC by Luca Bruno
luaengine: add access to devices and address spaces

This commit maps some more classes (device_t and address_space)
to retrieve devices and address spaces out of current running
machine.
Proxy methods are provided to enumerate/access devices and
address spaces, and to read memory content.

Signed-off-by: Luca Bruno <lucab@debian.org>
[src/emu]luaengine.c luaengine.h
[src/mame/drivers]cps1.c
[src/mame/includes]cps1.h

trunk/src/emu/luaengine.c
r242558r242559
88
99***************************************************************************/
1010
11#include <limits>
1112#include "lua/lua.hpp"
1213#include "lua/lib/lualibs.h"
1314#include "lua/bridge/LuaBridge.h"
r242558r242559
335336   return 0;
336337}
337338
339//-------------------------------------------------
340//  machine_get_devices - return table of available devices userdata
341//  -> manager:machine().devices[":maincpu"]
342//-------------------------------------------------
338343
344luabridge::LuaRef lua_engine::l_machine_get_devices(const running_machine *r)
345{
346   running_machine *m = const_cast<running_machine *>(r);
347   lua_State *L = luaThis->m_lua_state;
348   luabridge::LuaRef devs_table = luabridge::LuaRef::newTable(L);
349
350   device_t *root = &(m->root_device());
351    devs_table = devtree_dfs(root, devs_table);
352
353   return devs_table;
354}
355
356// private helper for get_devices - DFS visit all devices in a running machine
357luabridge::LuaRef lua_engine::devtree_dfs(device_t *root, luabridge::LuaRef devs_table)
358{
359   if (root) {
360      for (device_t *dev = root->first_subdevice(); dev != NULL; dev = dev->next()) {
361         if (dev && dev->configured() && dev->started()) {
362            devs_table[dev->tag()] = dev;
363            devtree_dfs(dev, devs_table);
364         }
365      }
366   }
367   return devs_table;
368}
369
370//-------------------------------------------------
371//  device_get_memspaces - return table of available address spaces userdata
372//  -> manager:machine().devices[":maincpu"].spaces["program"]
373//-------------------------------------------------
374
375luabridge::LuaRef lua_engine::l_dev_get_memspaces(const device_t *d)
376{
377   device_t *dev = const_cast<device_t *>(d);
378   lua_State *L = luaThis->m_lua_state;
379   luabridge::LuaRef sp_table = luabridge::LuaRef::newTable(L);
380
381   for (address_spacenum sp = AS_0; sp < ADDRESS_SPACES; sp++) {
382      if (dev->memory().has_space(sp)) {
383         sp_table[dev->memory().space(sp).name()] = &(dev->memory().space(sp));
384      }
385   }
386
387   return sp_table;
388}
389
390//-------------------------------------------------
391//  mem_read - templated memory readers for <sign>,<size>
392//  -> manager:machine().devices[":maincpu"].spaces["program"]:read_i8(0xC000)
393//-------------------------------------------------
394
395template <typename T>
396int lua_engine::lua_addr_space::l_mem_read(lua_State *L)
397{
398   address_space &sp = luabridge::Stack<address_space &>::get(L, 1);
399   luaL_argcheck(L, lua_isnumber(L, 2), 2, "address (integer) expected");
400   offs_t address = lua_tounsigned(L, 2);
401   T mem_content = 0;
402   switch(sizeof(mem_content) * 8) {
403      case 8:
404         mem_content = sp.read_byte(address);
405         break;
406      case 16:
407         if ((address & 1) == 0) {
408            mem_content = sp.read_word(address);
409         } else {
410            mem_content = sp.read_word_unaligned(address);
411         }
412         break;
413      case 32:
414         if ((address & 3) == 0) {
415            mem_content = sp.read_dword(address);
416         } else {
417            mem_content = sp.read_dword_unaligned(address);
418         }
419         break;
420      case 64:
421         if ((address & 7) == 0) {
422            mem_content = sp.read_qword(address);
423         } else {
424            mem_content = sp.read_qword_unaligned(address);
425         }
426         break;
427      default:
428         break;
429   }
430
431   if (std::numeric_limits<T>::is_signed) {
432      lua_pushinteger(L, mem_content);
433   } else {
434      lua_pushunsigned(L, mem_content);
435   }
436
437   return 1;
438
439}
440
339441void *lua_engine::checkparam(lua_State *L, int idx, const char *tname)
340442{
341443   const char *name;
r242558r242559
564666            .addFunction ("hard_reset", &running_machine::schedule_hard_reset)
565667            .addFunction ("soft_reset", &running_machine::schedule_soft_reset)
566668            .addFunction ("system", &running_machine::system)
669            .addProperty <luabridge::LuaRef, void> ("devices", &lua_engine::l_machine_get_devices)
567670         .endClass ()
568671         .beginClass <game_driver> ("game_driver")
569672            .addData ("name", &game_driver::name)
r242558r242559
571674            .addData ("year", &game_driver::year)
572675            .addData ("manufacturer", &game_driver::manufacturer)
573676         .endClass ()
677         .beginClass <device_t> ("device")
678            .addFunction("name", &device_t::tag)
679            .addProperty <luabridge::LuaRef, void> ("spaces", &lua_engine::l_dev_get_memspaces)
680         .endClass()
681         .beginClass <lua_addr_space> ("lua_addr_space")
682            .addCFunction ("read_i8", &lua_addr_space::l_mem_read<INT8>)
683            .addCFunction ("read_u8", &lua_addr_space::l_mem_read<UINT8>)
684            .addCFunction ("read_i16", &lua_addr_space::l_mem_read<INT16>)
685            .addCFunction ("read_u16", &lua_addr_space::l_mem_read<UINT16>)
686            .addCFunction ("read_i32", &lua_addr_space::l_mem_read<INT32>)
687            .addCFunction ("read_u32", &lua_addr_space::l_mem_read<UINT32>)
688            .addCFunction ("read_i64", &lua_addr_space::l_mem_read<INT64>)
689            .addCFunction ("read_u64", &lua_addr_space::l_mem_read<UINT64>)
690         .endClass()
691         .deriveClass <address_space, lua_addr_space> ("addr_space")
692            .addFunction("name", &address_space::name)
693         .endClass()
574694      .endNamespace ();
575695
576696   luabridge::push (m_lua_state, machine_manager::instance());
trunk/src/emu/luaengine.h
r242558r242559
1919
2020#include <map>
2121
22// None is typedef'd already in SDL/X11 libs
23#ifdef None
24#undef None
25#endif
26
27#include "lua/lua.hpp"
28#include "lua/lib/lualibs.h"
29#include "lua/bridge/LuaBridge.h"
30
2231struct lua_State;
2332
2433class lua_engine
r242558r242559
8998   static int l_emu_pause(lua_State *L);
9099   static int l_emu_unpause(lua_State *L);
91100
101   // "emu.machine" namespace
102   static luabridge::LuaRef l_machine_get_devices(const running_machine *r);
103   static luabridge::LuaRef l_dev_get_memspaces(const device_t *d);
104   static luabridge::LuaRef devtree_dfs(device_t *root, luabridge::LuaRef dev_table);
105   struct lua_addr_space {
106      template<typename T> int l_mem_read(lua_State *L);
107   };
108
92109   void resume(void *L, INT32 param);
93110   void report_errors(int status);
94111   void start();
trunk/src/mame/drivers/cps1.c
r242558r242559
1151811518   m_maincpu->space(AS_PROGRAM).install_readwrite_handler(0xff0000, 0xffffff, read16_delegate(FUNC(cps_state::ganbare_ram_r),this), write16_delegate(FUNC(cps_state::ganbare_ram_w),this));
1151911519}
1152011520
11521READ16_MEMBER(cps_state::dinohunt_sound_r)
11522{
11523   /*TODO: understand what's really going on here. According to MT05805;
11524   "I think that the values written are only qsound leftovers (after a lot of 0xFF values,
11525   there is the same qsound starting sequence, eg: 0x88, 0xFF, 0x0B, 0x00, 0x00, 0x00, ...)."*/
11526   return 0xff;
11527}
11528
1152911521DRIVER_INIT_MEMBER(cps_state,dinohunt)
1153011522{
1153111523   // is this shared with the new sound hw?
11532   m_maincpu->space(AS_PROGRAM).install_read_handler(0xf18000, 0xf19fff, read16_delegate(FUNC(cps_state::dinohunt_sound_r), this));
11533   m_maincpu->space(AS_PROGRAM).install_read_port(0xfc0000, 0xfc0001, "IN2"); ;
11534   // the ym2151 doesn't seem to be used. Is it actually on the PCB?
11535   
11524   UINT8* ram = (UINT8*)m_maincpu->space(AS_PROGRAM).install_ram(0xf18000, 0xf19fff);
11525   memset(ram,0xff,0x2000);
11526   m_maincpu->space(AS_PROGRAM).install_read_handler(0xfc0000, 0xfc0001, read16_delegate(FUNC(cps_state::cps1_in2_r), this));
1153611527   DRIVER_INIT_CALL(cps1);
1153711528}
1153811529
trunk/src/mame/includes/cps1.h
r242558r242559
242242   DECLARE_WRITE8_MEMBER(cps1_oki_pin7_w);
243243   DECLARE_WRITE16_MEMBER(sf2m1_layer_w);
244244   DECLARE_WRITE16_MEMBER(sf2m3_layer_w);
245   DECLARE_READ16_MEMBER(dinohunt_sound_r);
246245   DECLARE_DRIVER_INIT(sf2rb);
247246   DECLARE_DRIVER_INIT(sf2rb2);
248247   DECLARE_DRIVER_INIT(sf2thndr);


Previous 199869 Revisions Next


© 1997-2024 The MAME Team