Upload UI
[lsnes.git] / src / lua / disassemble.cpp
blob227451dbac5df1ad717e85e3d021f817eb15f737
1 #include "lua/internal.hpp"
2 #include "interface/disassembler.hpp"
3 #include "interface/romtype.hpp"
4 #include "library/bintohex.hpp"
5 #include "core/memorymanip.hpp"
6 #include "core/moviedata.hpp"
8 namespace
10 function_ptr_luafun memdisass(lua_func_misc, "memory.disassemble", [](lua_state& L, const std::string& fname)
11 -> int {
12 uint64_t count = 1;
13 std::string kind = L.get_string(1, fname.c_str());
14 uint64_t addr = L.get_numeric_argument<uint64_t>(2, fname.c_str());
15 L.get_numeric_argument<uint64_t>(3, count, fname.c_str());
16 disassembler* d;
17 d = &disassembler::byname(kind);
18 L.newtable();
19 uint64_t laddr = addr;
20 for(uint64_t i = 1; i <= count; i++) {
21 uint64_t bytes = 0;
22 L.pushnumber(i);
23 L.newtable();
24 L.pushstring("addr");
25 L.pushnumber(laddr);
26 L.settable(-3);
28 L.pushstring("disasm");
29 L.pushlstring(d->disassemble(laddr, [&bytes, laddr]() -> unsigned char {
30 return lsnes_memory.read<uint8_t>(laddr + bytes++);
31 }));
32 L.settable(-3);
34 std::vector<unsigned char> tmp;
35 tmp.resize(bytes);
36 lsnes_memory.read_range(laddr, &tmp[0], bytes);
37 L.pushstring("bytes");
38 L.pushlstring(binary_to_hex(&tmp[0], bytes));
39 L.settable(-3);
40 L.settable(-3);
41 laddr += bytes;
43 return 1;
44 });
46 function_ptr_luafun getreg(lua_func_misc, "memory.getregister", [](lua_state& L, const std::string& fname)
47 -> int {
48 std::string r = L.get_string(1, fname.c_str());
49 const interface_device_reg* regs = our_rom.rtype->get_registers();
50 if(!regs) {
51 L.pushnil();
52 return 1;
54 for(size_t i = 0; regs[i].name; i++) {
55 if(r != regs[i].name)
56 continue;
57 if(regs[i].boolean)
58 L.pushboolean(regs[i].read() != 0);
59 else
60 L.pushnumber(regs[i].read());
61 return 1;
63 L.pushnil();
64 return 1;
65 });
67 function_ptr_luafun setreg(lua_func_misc, "memory.setregister", [](lua_state& L, const std::string& fname)
68 -> int {
69 std::string r = L.get_string(1, fname.c_str());
70 const interface_device_reg* regs = our_rom.rtype->get_registers();
71 if(!regs) {
72 return 0;
74 for(size_t i = 0; regs[i].name; i++) {
75 if(r != regs[i].name)
76 continue;
77 if(!regs[i].write)
78 break;
79 if(regs[i].boolean)
80 regs[i].write(L.get_bool(2, fname.c_str()) ? 1 : 0);
81 else
82 regs[i].write(L.get_numeric_argument<uint64_t>(2, fname.c_str()));
83 return 0;
85 return 0;
86 });