Make opus_packet_tick_count a common function
[lsnes.git] / src / lua / core.cpp
blob3b2327b11e5384dcf17ab31c69bddad3865c3e9a
1 #include "core/command.hpp"
2 #include "lua/internal.hpp"
3 #include "core/framerate.hpp"
4 #include "core/moviefile.hpp"
5 #include "core/moviedata.hpp"
6 #include "core/window.hpp"
7 #include "interface/romtype.hpp"
9 namespace
11 function_ptr_luafun lua_print(LS, "print", [](lua_state& L, const std::string& fname) -> int {
12 int stacksize = 0;
13 while(!L.isnone(stacksize + 1))
14 stacksize++;
15 std::string toprint;
16 bool first = true;
17 for(int i = 0; i < stacksize; i++) {
18 size_t len;
19 const char* tmp = NULL;
20 if(L.isnil(i + 1)) {
21 tmp = "nil";
22 len = 3;
23 } else if(L.isboolean(i + 1) && L.toboolean(i + 1)) {
24 tmp = "true";
25 len = 4;
26 } else if(L.isboolean(i + 1) && !L.toboolean(i + 1)) {
27 tmp = "false";
28 len = 5;
29 } else {
30 tmp = L.tolstring(i + 1, &len);
31 if(!tmp) {
32 tmp = "(unprintable)";
33 len = 13;
36 std::string localmsg(tmp, tmp + len);
37 if(first)
38 toprint = localmsg;
39 else
40 toprint = toprint + "\t" + localmsg;
41 first = false;
43 platform::message(toprint);
44 return 0;
45 });
47 function_ptr_luafun lua_exec(LS, "exec", [](lua_state& L, const std::string& fname) -> int {
48 std::string text = L.get_string(1, fname.c_str());
49 lsnes_cmd.invoke(text);
50 return 0;
51 });
53 function_ptr_luafun lua_booted(LS, "emulator_ready", [](lua_state& L, const std::string& fname) -> int {
54 L.pushboolean(lua_booted_flag ? 1 : 0);
55 return 1;
56 });
58 function_ptr_luafun lua_utime(LS, "utime", [](lua_state& L, const std::string& fname) -> int {
59 uint64_t t = get_utime();
60 L.pushnumber(t / 1000000);
61 L.pushnumber(t % 1000000);
62 return 2;
63 });
65 function_ptr_luafun lua_idle_time(LS, "set_idle_timeout", [](lua_state& L, const std::string& fname) -> int {
66 lua_idle_hook_time = get_utime() + L.get_numeric_argument<uint64_t>(1, fname.c_str());
67 return 0;
68 });
70 function_ptr_luafun lua_timer_time(LS, "set_timer_timeout", [](lua_state& L, const std::string& fname) ->
71 int {
72 lua_timer_hook_time = get_utime() + L.get_numeric_argument<uint64_t>(1, fname.c_str());
73 return 0;
74 });
76 function_ptr_luafun lua_busaddr(LS, "bus_address", [](lua_state& L, const std::string& fname) -> int {
77 uint64_t addr = L.get_numeric_argument<uint64_t>(1, fname.c_str());
78 auto busrange = our_rom->rtype->get_bus_map();
79 if(!busrange.second) {
80 L.pushstring("This platform does not have bus mapping");
81 L.error();
82 return 0;
84 L.pushnumber(busrange.first + (addr % busrange.second));
85 return 1;
86 });