Skip to content

Commit

Permalink
Fixes #155 - Add a Lua API to access memory
Browse files Browse the repository at this point in the history
  • Loading branch information
st3fan committed Sep 20, 2017
1 parent a88c4a7 commit 97d415a
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions src/cpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,14 @@ static int cpu_lua_index(lua_State *state) {
return 1;
}

if (strcmp(name, "memory") == 0) {
void *cpu_data = lua_newuserdata(state, sizeof(struct cpu_t*));
*((struct cpu_t**) cpu_data) = cpu;
luaL_getmetatable(state, "mem_meta_table");
lua_setmetatable(state, -2);
return 1;
}

luaL_getmetatable(state, "cpu_methods_meta_table");
lua_pushvalue(state, 2);
lua_rawget(state, -2);
Expand Down Expand Up @@ -605,6 +613,46 @@ static int cpu_lua_newindex(lua_State *state) {
return 0;
}

// mem

static int cpu_lua_mem_index(lua_State *state) {
void *cpu_data = luaL_checkudata(state, 1, "mem_meta_table");
struct cpu_t *cpu = *((struct cpu_t**) cpu_data);

if (lua_type(state, 2) != LUA_TNUMBER) {
printf("First arg fail\n");
return 0;
}

uint16_t addr = lua_tointeger(state, 2);
lua_pushinteger(state, mem_get_byte(cpu, addr));

return 1;
}

static int cpu_lua_mem_newindex(lua_State *state) {
printf("mem_newindex()\n");

void *cpu_data = luaL_checkudata(state, 1, "mem_meta_table");
struct cpu_t *cpu = *((struct cpu_t**) cpu_data);

if (lua_type(state, 2) != LUA_TNUMBER) {
printf("First arg fail\n");
return 0;
}
uint16_t addr = lua_tointeger(state, 2);

if (lua_type(state, 3) != LUA_TNUMBER) {
printf("First arg fail\n");
return 0;
}
uint8_t value = lua_tointeger(state, 3);

mem_set_byte(cpu, addr, value);

return 0;
}

static int cpu_lua_reset(lua_State *state) {
void *cpu_data = luaL_checkudata(state, 1, "cpu_meta_table");
struct cpu_t *cpu = *((struct cpu_t**) cpu_data);
Expand Down Expand Up @@ -701,5 +749,14 @@ int ewm_cpu_init_lua(struct cpu_t *cpu, struct ewm_lua_t *lua) {
lua_setmetatable(lua->state, -2);
lua_setglobal(lua->state, "cpu");

// Register cpu.memory

luaL_Reg mem_functions[] = {
{"__index", cpu_lua_mem_index},
{"__newindex", cpu_lua_mem_newindex},
{NULL, NULL}
};
ewm_lua_register_component(lua, "mem", mem_functions);

return 0;
}

0 comments on commit 97d415a

Please sign in to comment.