openVSM  1.0
 All Data Structures Files Functions Variables Pages
vsm_api.c
Go to the documentation of this file.
1 
24 #include "vsm_api.h"
25 
26 lua_State* luactx = NULL;
27 VSM_PIN device_pins[32];
28 
29 IDSIMMODEL_vtable VSM_DEVICE_vtable =
30 {
31  .isdigital = vsm_isdigital,
32  .setup = vsm_setup,
33  .runctrl = vsm_runctrl,
34  .actuate = vsm_actuate,
35  .indicate = vsm_indicate,
36  .simulate = vsm_simulate,
37  .callback = vsm_callback,
38 };
39 
40 IDSIMMODEL VSM_DEVICE =
41 {
42  .vtable = &VSM_DEVICE_vtable,
43 };
44 
45 ICPU_vtable ICPU_DEVICE_vtable =
46 {
47  .vdmhlr = icpu_vdmhlr,
48  .loaddata = icpu_loaddata,
49  .disassemble = icpu_disassemble,
50  .getvardata = icpu_getvardata,
51 };
52 
53 ICPU ICPU_DEVICE =
54 {
55  .vtable = &ICPU_DEVICE_vtable,
56 };
57 
58 typedef struct lua_global_func
59 {
60  char* func_name;
61  bool* exist;
63 
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;
69 
70 static lua_global_func lua_global_func_list[] =
71 {
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},
77  {.func_name=0},
78 };
79 
80 static void check_global_functions ( void )
81 {
82  for ( int i=0; lua_global_func_list[i].func_name; i++ )
83  {
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;
87  }
88 }
89 
90 IDSIMMODEL* __cdecl
91 createdsimmodel ( char* device, ILICENCESERVER* ils )
92 {
93  ( void ) device;
94  if ( 0 == vsm_register ( ils ) )
95  {
96  return NULL;
97  }
98  /* Init Lua */
99  luactx = luaL_newstate();
100  /* Open libraries */
101  luaL_openlibs ( luactx );
102  register_functions ( luactx );
103 
104  return &VSM_DEVICE;
105 }
106 
107 void __cdecl
108 deletedsimmodel ( IDSIMMODEL* model )
109 {
110  ( void ) model;
111  /* Close Lua */
112  lua_close ( luactx );
113 }
114 
115 int32_t __attribute__ ( ( fastcall ) )
116 vsm_isdigital ( IDSIMMODEL* this, uint32_t edx, char* pinname )
117 {
118  ( void ) this;
119  ( void ) edx;
120  ( void ) pinname;
121  return 1;
122 }
123 
124 void __attribute__ ( ( fastcall ) )
125 vsm_setup ( IDSIMMODEL* this, uint32_t edx, IINSTANCE* instance, IDSIMCKT* dsimckt )
126 {
127  ( void ) this;
128  ( void ) edx;
129  model_instance = instance;
130  model_dsim = dsimckt;
131 
132  char* moddll = get_string_param ( "moddll" );
133  char luascript[MAX_PATH]= {0};
134  snprintf ( luascript, sizeof luascript, "%s.lua", moddll );
135  lua_load_script ( luascript );
136 
137  free ( moddll );
138 
139  lua_getglobal ( luactx, "device_pins" );
140  if ( 0 == lua_istable ( luactx, -1 ) )
141  {
142  out_error ( "No device model found, it is fatal error" );
143  return;
144  }
145 
146  lua_len ( luactx, -1 );
147  int32_t pin_number = lua_tointeger ( luactx, -1 );
148  lua_pop ( luactx, 1 );
149 
150  for ( int i=1; i<=pin_number; i++ )
151  {
152  lua_rawgeti ( luactx,-1, i );
154  //set pin //
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 );
161  //set pin on time //
163  lua_getfield ( luactx,-1, PIN_ON_TIME );
164  device_pins[i].on_time = lua_tonumber ( luactx,-1 );
165  lua_pop ( luactx, 1 );
167  //set pin off time //
169  lua_getfield ( luactx,-1, PIN_OFF_TIME );
170  device_pins[i].off_time = lua_tonumber ( luactx,-1 );
171  lua_pop ( luactx, 1 );
173  //Set global variable that holds pin name and its number //
175  lua_pushinteger ( luactx, i );
176  lua_setglobal ( luactx, pin_name );
177  lua_pop ( luactx, 1 );
178  }
179 
180  check_global_functions();
181  if ( global_device_init )
182  lua_run_function ( "device_init" );
183 }
184 
185 void __attribute__ ( ( fastcall ) )
186 vsm_runctrl ( IDSIMMODEL* this, uint32_t edx, RUNMODES mode )
187 {
188  ( void ) this;
189  ( void ) edx;
190  ( void ) mode;
191 
192  switch ( mode )
193  {
194  case RM_BATCH:
195 
196  break;
197  case RM_START:
198 
199  break;
200  case RM_STOP:
201  if ( global_on_stop )
202  lua_run_function ( "on_stop" );
203  break;
204  case RM_SUSPEND:
205  if ( global_on_suspend )
206  lua_run_function ( "on_suspend" );
207  break;
208  case RM_ANIMATE:
209  break;
210  case RM_STEPTIME:
211 
212  break;
213  case RM_STEPOVER:
214 
215  break;
216  case RM_STEPINTO:
217 
218  break;
219  case RM_STEPOUT:
220 
221  break;
222  case RM_STEPTO:
223 
224  break;
225  case RM_META:
226 
227  break;
228  case RM_DUMP:
229 
230  break;
231  }
232 
233 }
234 
235 void __attribute__ ( ( fastcall ) )
236 vsm_actuate ( IDSIMMODEL* this, uint32_t edx, REALTIME atime, ACTIVESTATE newstate )
237 {
238  ( void ) this;
239  ( void ) edx;
240  ( void ) atime;
241  ( void ) newstate;
242 }
243 
254 bool __attribute__ ( ( fastcall ) )
255 vsm_indicate ( IDSIMMODEL* this, uint32_t edx, REALTIME atime, ACTIVEDATA* newstate )
256 {
257  ( void ) this;
258  ( void ) edx;
259  ( void ) atime;
260  ( void ) newstate;
261  return false;
262 }
263 
273 void __attribute__ ( ( fastcall ) )
274 vsm_simulate ( IDSIMMODEL* this, uint32_t edx, ABSTIME atime, DSIMMODES mode )
275 {
276  ( void ) this;
277  ( void ) edx;
278  ( void ) atime;
279  ( void ) mode;
280 
281  if ( global_device_simulate )
282  lua_run_function ( "device_simulate" );
283 }
284 
294 void __attribute__ ( ( fastcall ) )
295 vsm_callback ( IDSIMMODEL* this, uint32_t edx, ABSTIME atime, EVENTID eventid )
296 {
297  ( void ) this;
298  ( void ) edx;
299 
300  if ( false == global_timer_callback )
301  return;
302 
303  lua_getglobal ( luactx, "timer_callback" );
304  lua_pushunsigned ( luactx, atime );
305  lua_pushunsigned ( luactx, eventid );
306  lua_pcall ( luactx, 2, 0, 0 );
307 }
308 
318 bool APIENTRY
319 DllMain ( HINSTANCE hInstDLL, uint32_t fdwReason, LPVOID lpvReserved )
320 {
321  ( void ) hInstDLL;
322  ( void ) fdwReason;
323  ( void ) lpvReserved;
324 
325  return true;
326 }
327 
328 LRESULT __attribute__ ( ( fastcall ) ) icpu_vdmhlr ( ICPU* this, uint32_t edx, VDM_COMMAND* cmd, uint8_t* data )
329 {
330  ( void ) this;
331  ( void ) edx;
332  ( void ) cmd;
333  ( void ) data;
334  return 0;
335 }
336 
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 )
338 {
339  ( void ) this;
340  ( void ) edx;
341  ( void ) format;
342  ( void ) seg;
343  ( void ) address;
344  ( void ) data;
345  ( void ) numbytes;
346 }
347 
348 void __attribute__ ( ( fastcall ) ) icpu_disassemble ( ICPU* this, uint32_t edx, ADDRESS address, int32_t numbytes )
349 {
350  ( void ) this;
351  ( void ) edx;
352  ( void ) address;
353  ( void ) numbytes;
354 }
355 
356 bool __attribute__ ( ( fastcall ) ) icpu_getvardata ( ICPU* this, uint32_t edx, VARITEM* vip, VARDATA* vdp )
357 {
358  ( void ) this;
359  ( void ) edx;
360  ( void ) vip;
361  ( void ) vdp;
362  return true;
363 }
bool vsm_register(ILICENCESERVER *ils)
Definition: c_bind.c:61
void out_error(const char *format,...)
Definition: c_bind.c:507
bool APIENTRY DllMain(HINSTANCE hInstDLL, uint32_t fdwReason, LPVOID lpvReserved)
[brief description]
Definition: vsm_api.c:319
char * get_string_param(char *field_name)
Definition: c_bind.c:109
IDSIMPIN * get_pin(char *pin_name)
Definition: c_bind.c:523
int32_t __attribute__((fastcall))
[brief description]
Definition: vsm_api.c:115