26 static int lua_state_to_string ( lua_State* L );
27 static int lua_set_pin_state ( lua_State* L );
28 static int lua_set_pin_bool ( lua_State* L );
29 static int lua_get_pin_bool ( lua_State* L );
30 static int lua_get_pin_state ( lua_State* L );
31 static int lua_is_pin_active ( lua_State* L );
32 static int lua_is_pin_edge ( lua_State* L );
33 static int lua_is_pin_posedge ( lua_State* L );
34 static int lua_is_pin_negedge ( lua_State* L );
35 static int lua_is_pin_low ( lua_State* L );
36 static int lua_is_pin_high ( lua_State* L );
37 static int lua_is_pin_floating ( lua_State* L );
38 static int lua_toggle_pin_state ( lua_State* L );
39 static int lua_out_log ( lua_State* L );
40 static int lua_out_message ( lua_State* L );
41 static int lua_out_warning ( lua_State* L );
42 static int lua_out_error ( lua_State* L );
43 static int lua_set_callback ( lua_State* L );
44 static int lua_create_debug_popup ( lua_State* L );
45 static int lua_print_to_debug_popup ( lua_State* L );
46 static int lua_dump_to_debug_popup ( lua_State* L );
47 static int lua_create_memory_popup ( lua_State* L );
48 static int lua_create_source_popup ( lua_State* L );
49 static int lua_create_status_popup ( lua_State* L );
50 static int lua_create_var_popup ( lua_State* L );
51 static int lua_delete_popup ( lua_State* L );
52 static int lua_set_memory_popup ( lua_State* L );
53 static int lua_repaint_memory_popup ( lua_State* L );
54 static int lua_get_string_param ( lua_State* L );
55 static int lua_get_bool_param ( lua_State* L );
56 static int lua_get_num_param ( lua_State* L );
57 static int lua_get_hex_param ( lua_State* L );
58 static int lua_get_init_param ( lua_State* L );
59 static int lua_add_source_file ( lua_State* L );
61 static int lua_get_bit ( lua_State* L );
62 static int lua_set_bit ( lua_State* L );
63 static int lua_toggle_bit ( lua_State* L );
64 static int lua_clear_bit ( lua_State* L );
66 static int lua_get_systime ( lua_State* L );
68 static const lua_bind_var lua_var_api_list[]=
70 {.var_name=
"SHI", .var_value=SHI},
71 {.var_name=
"SLO", .var_value=SLO},
72 {.var_name=
"FLT", .var_value=FLT},
73 {.var_name=
"PLO", .var_value=PLO},
74 {.var_name=
"ILO", .var_value=ILO},
75 {.var_name=
"WLO", .var_value=WLO},
76 {.var_name=
"WHI", .var_value=WHI},
77 {.var_name=
"IHI", .var_value=IHI},
78 {.var_name=
"PHI", .var_value=PHI},
79 {.var_name=
"WUD", .var_value=WUD},
80 {.var_name=
"SUD", .var_value=SUD},
81 {.var_name=
"TSTATE", .var_value=TSTATE},
82 {.var_name=
"FSTATE", .var_value=FSTATE},
83 {.var_name=
"UNDEFINED", .var_value=UNDEFINED},
84 {.var_name=
"MSEC", .var_value=1000000000L},
85 {.var_name=
"NSEC", .var_value=100000000L},
86 {.var_name=
"SEC", .var_value=1000000000000L},
87 {.var_name=
"NOW", .var_value=0L},
91 static const lua_bind_func lua_c_api_list[] =
93 {.lua_func_name=
"state_to_string", .lua_c_api=&lua_state_to_string},
94 {.lua_func_name=
"is_pin_active", .lua_c_api=&lua_is_pin_active},
95 {.lua_func_name=
"is_pin_edge", .lua_c_api=&lua_is_pin_edge},
96 {.lua_func_name=
"is_pin_posedge", .lua_c_api=&lua_is_pin_posedge},
97 {.lua_func_name=
"is_pin_negedge", .lua_c_api=&lua_is_pin_negedge},
98 {.lua_func_name=
"set_pin_state", .lua_c_api=&lua_set_pin_state},
99 {.lua_func_name=
"set_pin_bool", .lua_c_api=&lua_set_pin_bool},
100 {.lua_func_name=
"get_pin_bool", .lua_c_api=&lua_get_pin_bool},
101 {.lua_func_name=
"get_pin_state", .lua_c_api=&lua_get_pin_state},
102 {.lua_func_name=
"is_pin_low", .lua_c_api=&lua_is_pin_low},
103 {.lua_func_name=
"is_pin_high", .lua_c_api=&lua_is_pin_high},
104 {.lua_func_name=
"is_pin_floating", .lua_c_api=&lua_is_pin_floating},
105 {.lua_func_name=
"toggle_pin_state", .lua_c_api=&lua_toggle_pin_state},
106 {.lua_func_name=
"out_log", .lua_c_api=&lua_out_log},
107 {.lua_func_name=
"out_message", .lua_c_api=&lua_out_message},
108 {.lua_func_name=
"out_warning", .lua_c_api=&lua_out_warning},
109 {.lua_func_name=
"out_error", .lua_c_api=&lua_out_error},
110 {.lua_func_name=
"set_callback", .lua_c_api=&lua_set_callback},
111 {.lua_func_name=
"create_debug_popup", .lua_c_api=&lua_create_debug_popup},
112 {.lua_func_name=
"create_memory_popup", .lua_c_api=&lua_create_memory_popup},
113 {.lua_func_name=
"create_source_popup", .lua_c_api=&lua_create_source_popup},
114 {.lua_func_name=
"create_status_popup", .lua_c_api=&lua_create_status_popup},
115 {.lua_func_name=
"create_var_popup", .lua_c_api=&lua_create_var_popup},
116 {.lua_func_name=
"delete_popup", .lua_c_api=&lua_delete_popup},
117 {.lua_func_name=
"set_memory_popup", .lua_c_api=&lua_set_memory_popup},
118 {.lua_func_name=
"repaint_memory_popup", .lua_c_api=&lua_repaint_memory_popup},
119 {.lua_func_name=
"print_to_debug_popup", .lua_c_api=&lua_print_to_debug_popup},
120 {.lua_func_name=
"dump_to_debug_popup", .lua_c_api=&lua_dump_to_debug_popup},
121 {.lua_func_name=
"get_string_param", .lua_c_api=&lua_get_string_param},
122 {.lua_func_name=
"get_num_param", .lua_c_api=&lua_get_num_param},
123 {.lua_func_name=
"get_bool_param", .lua_c_api=&lua_get_bool_param},
124 {.lua_func_name=
"get_init_param", .lua_c_api=&lua_get_init_param},
125 {.lua_func_name=
"get_hex_param", .lua_c_api=&lua_get_hex_param},
126 {.lua_func_name=
"add_source_file", .lua_c_api=&lua_add_source_file},
127 {.lua_func_name=
"get_bit", .lua_c_api=&lua_get_bit},
128 {.lua_func_name=
"set_bit", .lua_c_api=&lua_set_bit},
129 {.lua_func_name=
"clear_bit", .lua_c_api=&lua_clear_bit},
130 {.lua_func_name=
"toggle_bit", .lua_c_api=&lua_toggle_bit},
131 {.lua_func_name=
"systime", .lua_c_api=&lua_get_systime},
136 register_functions ( lua_State* L )
139 for ( int32_t i=0; lua_c_api_list[i].lua_func_name; i++ )
141 lua_pushcfunction ( L, lua_c_api_list[i].lua_c_api );
142 lua_setglobal ( L, lua_c_api_list[i].lua_func_name );
145 for ( int32_t i=0; lua_var_api_list[i].var_name ; i++ )
147 lua_pushinteger ( L, lua_var_api_list[i].var_value );
148 lua_setglobal ( L, lua_var_api_list[i].var_name );
151 for (
int i=0; device_pins[i].name; i++ )
153 lua_pushinteger ( L, i );
154 lua_setglobal ( L, device_pins[i].name );
159 lua_load_script (
const char* device_name )
161 char spath[512] = {0};
162 if ( 0 == GetEnvironmentVariable (
"LUAVSM", spath,
sizeof spath ) )
164 out_error (
"LUAVSM env variable was not set" );
166 char script[512]= {0};
167 sprintf ( script,
"%s\\%s", spath, device_name );
169 int32_t lua_err = luaL_loadfile ( luactx, script );
172 const char* mess = NULL;
176 mess = lua_tostring(luactx, -1);
177 out_error (
"Syntax error in Lua script\n%s", mess );
180 out_error (
"Not enough memory to load script" );
183 out_error (
"Error loading script file" );
186 out_error (
"Unknown error, shouldn't happen" );
191 if ( 0 != lua_pcall ( luactx, 0, 0, 0 ) )
193 out_error (
"Failed to load the script" );
197 out_log (
"Successfully loaded Lua script" );
201 lua_run_function (
const char* func_name )
204 lua_getglobal ( luactx, func_name );
206 lua_pcall ( luactx, 0, 0, 0 );
210 lua_get_string_param ( lua_State* L )
212 lua_pushstring ( L,
get_string_param ( (
char* ) lua_tostring ( L, -1 ) ) );
217 lua_get_bool_param ( lua_State* L )
219 lua_pushboolean ( L,
get_bool_param ( (
char* ) lua_tostring ( L, -1 ) ) );
224 lua_get_num_param ( lua_State* L )
226 lua_pushnumber ( L,
get_num_param ( (
char* ) lua_tostring ( L, -1 ) ) );
231 lua_get_hex_param ( lua_State* L )
233 lua_pushinteger ( L,
get_hex_param ( (
char* ) lua_tostring ( L, -1 ) ) );
238 lua_get_init_param ( lua_State* L )
240 lua_pushinteger ( L,
get_init_param ( (
char* ) lua_tostring ( L, -1 ) ) );
245 lua_delete_popup ( lua_State* L )
247 int id = lua_tonumber ( L, -1 );
253 lua_create_debug_popup ( lua_State* L )
255 const char* text = lua_tostring ( L, -1 );
267 lua_print_to_debug_popup ( lua_State* L )
269 lua_Number argnum = lua_gettop ( L );
272 out_error (
"Function %s expects 2 arguments got %d\n", __PRETTY_FUNCTION__, argnum );
288 lua_dump_to_debug_popup ( lua_State* L )
290 lua_Number argnum = lua_gettop ( L );
293 out_error (
"Function %s expects 4 arguments got %d\n", __PRETTY_FUNCTION__, argnum );
297 lua_Number offset = luaL_checknumber ( L,-1 );
298 lua_Number size = luaL_checknumber ( L,-2 );
299 const char* buf = luaL_checkstring ( L,-3 );
305 lua_create_source_popup ( lua_State* L )
307 const char* text = lua_tostring ( L, -1 );
313 lua_create_status_popup ( lua_State* L )
315 const char* text = lua_tostring ( L, -1 );
321 lua_create_var_popup ( lua_State* L )
323 const char* text = lua_tostring ( L, -1 );
330 lua_create_memory_popup ( lua_State* L )
332 const char* text = lua_tostring ( L, -1 );
339 lua_set_memory_popup ( lua_State* L )
342 lua_Number argnum = lua_gettop ( L );
345 out_error (
"Function %s expects 3 arguments got %d\n", __PRETTY_FUNCTION__, argnum );
349 lua_Number size = luaL_checknumber ( L,-1 );
350 const char* buf = luaL_checkstring ( L,-2 );
358 lua_add_source_file ( lua_State* L )
361 lua_Number argnum = lua_gettop ( L );
364 out_error (
"Function %s expects 2 arguments got %d\n", __PRETTY_FUNCTION__, argnum );
368 if (
false ==
add_source_file ( lua_touserdata ( L, -3 ), (
char* ) lua_tostring ( L, -2 ), lua_toboolean ( L, -1 ) ) )
377 lua_repaint_memory_popup ( lua_State* L )
379 lua_Number argnum = lua_gettop ( L );
382 out_error (
"Function %s expects 1 argument got %d\n", __PRETTY_FUNCTION__, argnum );
385 if ( 0 == lua_isuserdata ( L, -1 ) )
396 lua_set_pin_state ( lua_State* L )
398 lua_Number argnum = lua_gettop ( L );
401 out_error (
"Function %s expects 2 arguments got %d\n", __PRETTY_FUNCTION__, argnum );
404 int32_t pin_num = lua_tonumber ( L, -2 );
405 int32_t pin_state = lua_tonumber ( L, -1 );
411 lua_set_pin_bool ( lua_State* L )
413 lua_Number argnum = lua_gettop ( L );
416 out_error (
"Function %s expects 2 arguments got %d\n", __PRETTY_FUNCTION__, argnum );
419 int32_t pin_num = lua_tonumber ( L, -2 );
420 bool pin_level = lua_toboolean ( L, -1 );
426 lua_get_pin_bool ( lua_State* L )
428 lua_Number argnum = lua_gettop ( L );
431 out_error (
"Function %s expects 1 arguments got %d\n", __PRETTY_FUNCTION__, argnum );
434 int32_t pin_num = lua_tonumber ( L, -1 );
441 lua_pushboolean ( L, state);
446 lua_state_to_string ( lua_State* L )
448 lua_Number argnum = lua_gettop ( L );
451 out_error (
"Function %s expects 1 arguments got %d\n", __PRETTY_FUNCTION__, argnum );
454 int32_t state = lua_tonumber ( L, -1 );
461 lua_get_pin_state ( lua_State* L )
463 lua_Number argnum = lua_gettop ( L );
466 out_error (
"Function %s expects 1 arguments got %d\n", __PRETTY_FUNCTION__, argnum );
469 int32_t pin_num = lua_tonumber ( L, -1 );
471 if ( TRUE ==
is_pin_high ( device_pins[pin_num].pin ) )
473 lua_pushnumber ( L, SHI );
476 else if ( TRUE ==
is_pin_low ( device_pins[pin_num].pin ) )
478 lua_pushnumber ( L, SLO );
483 lua_pushnumber ( L, FLT );
488 lua_pushnumber ( L, UNDEFINED );
494 lua_is_pin_low ( lua_State* L )
496 lua_Number argnum = lua_gettop ( L );
499 out_error (
"Function %s expects 1 arguments got %d\n", __PRETTY_FUNCTION__, argnum );
502 int32_t pin_num = lua_tonumber ( L, -1 );
503 lua_pushboolean ( L,
is_pin_low ( device_pins[pin_num].pin ) );
508 lua_is_pin_high ( lua_State* L )
510 lua_Number argnum = lua_gettop ( L );
513 out_error (
"Function %s expects 1 arguments got %d\n", __PRETTY_FUNCTION__, argnum );
516 int32_t pin_num = lua_tonumber ( L, -1 );
517 lua_pushboolean ( L,
is_pin_high ( device_pins[pin_num].pin ) );
522 lua_is_pin_edge ( lua_State* L )
524 lua_Number argnum = lua_gettop ( L );
527 out_error (
"Function %s expects 1 arguments got %d\n", __PRETTY_FUNCTION__, argnum );
530 int32_t pin_num = lua_tonumber ( L, -1 );
531 lua_pushboolean ( L,
is_pin_edge ( device_pins[pin_num].pin ) );
536 lua_is_pin_posedge ( lua_State* L )
538 lua_Number argnum = lua_gettop ( L );
541 out_error (
"Function %s expects 1 arguments got %d\n", __PRETTY_FUNCTION__, argnum );
544 int32_t pin_num = lua_tonumber ( L, -1 );
545 lua_pushboolean ( L,
is_pin_posedge ( device_pins[pin_num].pin ) );
550 lua_is_pin_negedge ( lua_State* L )
552 lua_Number argnum = lua_gettop ( L );
555 out_error (
"Function %s expects 1 arguments got %d\n", __PRETTY_FUNCTION__, argnum );
558 int32_t pin_num = lua_tonumber ( L, -1 );
559 lua_pushboolean ( L,
is_pin_negedge ( device_pins[pin_num].pin ) );
564 lua_is_pin_active ( lua_State* L )
566 lua_Number argnum = lua_gettop ( L );
569 out_error (
"Function %s expects 1 arguments got %d\n", __PRETTY_FUNCTION__, argnum );
572 int32_t pin_num = lua_tonumber ( L, -1 );
573 lua_pushboolean ( L,
is_pin_active ( device_pins[pin_num].pin ) );
578 lua_toggle_pin_state ( lua_State* L )
580 lua_Number argnum = lua_gettop ( L );
583 out_error (
"Function %s expects 1 arguments got %d\n", __PRETTY_FUNCTION__, argnum );
586 int32_t pin_num = lua_tonumber ( L, -1 );
592 lua_is_pin_floating ( lua_State* L )
594 lua_Number argnum = lua_gettop ( L );
597 out_error (
"Function %s expects 1 arguments got %d\n", __PRETTY_FUNCTION__, argnum );
600 int32_t pin_num = lua_tonumber ( L, -1 );
606 lua_out_log ( lua_State* L )
608 lua_Number argnum = lua_gettop ( L );
611 out_error (
"Function %s expects 1 arguments got %d\n", __PRETTY_FUNCTION__, argnum );
614 const char* text = lua_tostring ( L, -1 );
620 lua_out_message ( lua_State* L )
622 lua_Number argnum = lua_gettop ( L );
625 out_error (
"Function %s expects 1 arguments got %d\n", __PRETTY_FUNCTION__, argnum );
628 const char* text = lua_tostring ( L, -1 );
634 lua_out_warning ( lua_State* L )
636 lua_Number argnum = lua_gettop ( L );
639 out_error (
"Function %s expects 1 arguments got %d\n", __PRETTY_FUNCTION__, argnum );
642 const char* text = lua_tostring ( L, -1 );
648 lua_out_error ( lua_State* L )
650 lua_Number argnum = lua_gettop ( L );
653 out_error (
"Function %s expects 1 arguments got %d\n", __PRETTY_FUNCTION__, argnum );
656 const char* text = lua_tostring ( L, -1 );
662 lua_set_callback ( lua_State* L )
664 lua_Number argnum = lua_gettop ( L );
667 out_error (
"Function %s expects 2 arguments got %d\n", __PRETTY_FUNCTION__, argnum );
671 lua_Number picotime = lua_tonumber ( L, -2 );
672 lua_Number eventid = lua_tonumber ( L, -1 );
679 lua_get_systime ( lua_State* L )
683 lua_pushnumber ( L, curtime );
688 lua_get_bit ( lua_State* L )
690 lua_Number argnum = lua_gettop ( L );
693 out_error (
"Function %s expects 2 arguments got %d\n", __PRETTY_FUNCTION__, argnum );
697 size_t byte = lua_tointeger ( L, -2 );
698 size_t bit = lua_tointeger ( L, -1 );
699 lua_pushboolean ( L, ( byte >> bit & 0x01 ) );
704 lua_clear_bit ( lua_State* L )
706 lua_Number argnum = lua_gettop ( L );
709 out_error (
"Function %s expects 2 arguments got %d\n", __PRETTY_FUNCTION__, argnum );
713 size_t byte = lua_tointeger ( L, -2 );
714 size_t bit = lua_tointeger ( L, -1 );
715 byte &= ~ ( 1 << bit );
716 lua_pushnumber ( L, byte );
721 lua_set_bit ( lua_State* L )
723 lua_Number argnum = lua_gettop ( L );
726 out_error (
"Function %s expects 2 arguments got %d\n", __PRETTY_FUNCTION__, argnum );
730 size_t byte = lua_tointeger ( L, -2 );
731 size_t bit = lua_tointeger ( L, -1 );
732 byte |= ( 1 << bit );
733 lua_pushnumber ( L, byte );
738 lua_toggle_bit ( lua_State* L )
740 lua_Number argnum = lua_gettop ( L );
743 out_error (
"Function %s expects 2 arguments got %d\n", __PRETTY_FUNCTION__, argnum );
747 size_t byte = lua_tointeger ( L, -2 );
748 size_t bit = lua_tointeger ( L, -1 );
750 lua_pushnumber ( L, byte );
bool is_pin_posedge(IDSIMPIN *pin)
void toggle_pin_state(VSM_PIN pin)
IDEBUGPOPUP * create_source_popup(const char *title, const int32_t id)
int64_t get_init_param(char *field_name)
bool is_pin_edge(IDSIMPIN *pin)
bool is_pin_active(IDSIMPIN *pin)
void set_memory_popup(IMEMORYPOPUP *popup, size_t offset, void *buffer, size_t size)
bool is_pin_low(IDSIMPIN *pin)
void set_pin_state(VSM_PIN pin, STATE state)
void out_message(const char *format,...)
void out_error(const char *format,...)
void print_to_debug_popup(IDEBUGPOPUP *popup, const char *message)
void out_warning(const char *format,...)
int32_t get_pin_bool(VSM_PIN pin)
int32_t get_hex_param(char *field_name)
void delete_popup(POPUPID id)
bool get_bool_param(char *field_name)
IDEBUGPOPUP * create_debug_popup(const char *title, const int32_t id)
char * get_string_param(char *field_name)
void dump_to_debug_popup(IDEBUGPOPUP *popup, const uint8_t *buf, uint32_t offset, uint32_t size)
bool add_source_file(ISOURCEPOPUP *popup, char *filename, bool lowlevel)
IDEBUGPOPUP * create_var_popup(const char *title, const int32_t id)
bool is_pin_floating(IDSIMPIN *pin)
IDEBUGPOPUP * create_status_popup(const char *title, const int32_t id)
void systime(ABSTIME *at)
void set_callback(RELTIME picotime, EVENTID id)
bool is_pin_high(IDSIMPIN *pin)
IMEMORYPOPUP * create_memory_popup(const char *title, const int32_t id)
void set_pin_bool(VSM_PIN pin, bool level)
const char * state_to_string(STATE pinstate)
[Convert pin state to string]
void repaint_memory_popup(IMEMORYPOPUP *popup)
double get_num_param(char *field_name)
int popup_id
Global pop identificator. Should be unique.
void out_log(const char *format,...)
bool is_pin_negedge(IDSIMPIN *pin)