26 lua_State* luactx = NULL;
27 VSM_PIN device_pins[32];
29 IDSIMMODEL_vtable VSM_DEVICE_vtable =
31 .isdigital = vsm_isdigital,
33 .runctrl = vsm_runctrl,
34 .actuate = vsm_actuate,
35 .indicate = vsm_indicate,
36 .simulate = vsm_simulate,
37 .callback = vsm_callback,
40 IDSIMMODEL VSM_DEVICE =
42 .vtable = &VSM_DEVICE_vtable,
45 ICPU_vtable ICPU_DEVICE_vtable =
47 .vdmhlr = icpu_vdmhlr,
48 .loaddata = icpu_loaddata,
49 .disassemble = icpu_disassemble,
50 .getvardata = icpu_getvardata,
55 .vtable = &ICPU_DEVICE_vtable,
64 bool global_device_init =
false;
65 bool global_device_simulate =
false;
66 bool global_timer_callback =
false;
67 bool global_on_stop =
false;
68 bool global_on_suspend =
false;
72 {.func_name=
"device_init", .exist=&global_device_init},
73 {.func_name=
"device_simulate", .exist=&global_device_simulate},
74 {.func_name=
"timer_callback", .exist=&global_timer_callback},
75 {.func_name=
"on_stop", .exist=&global_on_stop},
76 {.func_name=
"on_suspend", .exist=&global_on_suspend},
80 static void check_global_functions (
void )
82 for (
int i=0; lua_global_func_list[i].func_name; i++ )
84 lua_getglobal ( luactx,lua_global_func_list[i].func_name );
85 if ( lua_isfunction ( luactx,lua_gettop ( luactx ) ) )
86 *lua_global_func_list[i].exist =
true;
91 createdsimmodel (
char* device, ILICENCESERVER* ils )
99 luactx = luaL_newstate();
101 luaL_openlibs ( luactx );
102 register_functions ( luactx );
108 deletedsimmodel ( IDSIMMODEL* model )
112 lua_close ( luactx );
116 vsm_isdigital ( IDSIMMODEL* this, uint32_t edx,
char* pinname )
125 vsm_setup ( IDSIMMODEL* this, uint32_t edx, IINSTANCE* instance, IDSIMCKT* dsimckt )
129 model_instance = instance;
130 model_dsim = dsimckt;
133 char luascript[MAX_PATH]= {0};
134 snprintf ( luascript,
sizeof luascript,
"%s.lua", moddll );
135 lua_load_script ( luascript );
139 lua_getglobal ( luactx,
"device_pins" );
140 if ( 0 == lua_istable ( luactx, -1 ) )
142 out_error (
"No device model found, it is fatal error" );
146 lua_len ( luactx, -1 );
147 int32_t pin_number = lua_tointeger ( luactx, -1 );
148 lua_pop ( luactx, 1 );
150 for (
int i=1; i<=pin_number; i++ )
152 lua_rawgeti ( luactx,-1, i );
156 lua_getfield ( luactx,-1, PIN_NAME );
157 const char* pin_name = lua_tostring ( luactx,-1 );
158 device_pins[i].pin =
get_pin ( (
char* ) pin_name );
159 lua_pop ( luactx, 1 );
163 lua_getfield ( luactx,-1, PIN_ON_TIME );
164 device_pins[i].on_time = lua_tonumber ( luactx,-1 );
165 lua_pop ( luactx, 1 );
169 lua_getfield ( luactx,-1, PIN_OFF_TIME );
170 device_pins[i].off_time = lua_tonumber ( luactx,-1 );
171 lua_pop ( luactx, 1 );
175 lua_pushinteger ( luactx, i );
176 lua_setglobal ( luactx, pin_name );
177 lua_pop ( luactx, 1 );
180 check_global_functions();
181 if ( global_device_init )
182 lua_run_function (
"device_init" );
186 vsm_runctrl ( IDSIMMODEL* this, uint32_t edx, RUNMODES mode )
201 if ( global_on_stop )
202 lua_run_function (
"on_stop" );
205 if ( global_on_suspend )
206 lua_run_function (
"on_suspend" );
236 vsm_actuate ( IDSIMMODEL* this, uint32_t edx, REALTIME atime, ACTIVESTATE newstate )
255 vsm_indicate ( IDSIMMODEL* this, uint32_t edx, REALTIME atime, ACTIVEDATA* newstate )
274 vsm_simulate ( IDSIMMODEL* this, uint32_t edx, ABSTIME atime, DSIMMODES mode )
281 if ( global_device_simulate )
282 lua_run_function (
"device_simulate" );
295 vsm_callback ( IDSIMMODEL* this, uint32_t edx, ABSTIME atime, EVENTID eventid )
300 if (
false == global_timer_callback )
303 lua_getglobal ( luactx,
"timer_callback" );
304 lua_pushunsigned ( luactx, atime );
305 lua_pushunsigned ( luactx, eventid );
306 lua_pcall ( luactx, 2, 0, 0 );
319 DllMain ( HINSTANCE hInstDLL, uint32_t fdwReason, LPVOID lpvReserved )
323 ( void ) lpvReserved;
328 LRESULT
__attribute__ ( ( fastcall ) ) icpu_vdmhlr ( ICPU* this, uint32_t edx, VDM_COMMAND* cmd, uint8_t* data )
337 void __attribute__ ( ( fastcall ) ) icpu_loaddata ( ICPU* this, uint32_t edx, int32_t format, int32_t seg, ADDRESS address, uint8_t* data, int32_t numbytes )
348 void __attribute__ ( ( fastcall ) ) icpu_disassemble ( ICPU* this, uint32_t edx, ADDRESS address, int32_t numbytes )
356 bool __attribute__ ( ( fastcall ) ) icpu_getvardata ( ICPU* this, uint32_t edx, VARITEM* vip, VARDATA* vdp )
bool vsm_register(ILICENCESERVER *ils)
void out_error(const char *format,...)
bool APIENTRY DllMain(HINSTANCE hInstDLL, uint32_t fdwReason, LPVOID lpvReserved)
[brief description]
char * get_string_param(char *field_name)
IDSIMPIN * get_pin(char *pin_name)
int32_t __attribute__((fastcall))
[brief description]