mirror of
https://github.com/st3fan/ewm.git
synced 2024-06-11 15:29:33 +00:00
Merge a7ea27bfad
into c68fc356c6
This commit is contained in:
commit
dec2090e20
|
@ -24,7 +24,7 @@ os: osx
|
|||
language: c
|
||||
before_install:
|
||||
- brew update
|
||||
- brew install sdl2
|
||||
- brew install sdl2 lua
|
||||
before_script:
|
||||
- cd src
|
||||
script:
|
||||
|
|
22
src/Makefile
22
src/Makefile
|
@ -21,33 +21,33 @@
|
|||
# SOFTWARE.
|
||||
|
||||
CC?=cc
|
||||
CFLAGS=-std=gnu11 -O3 -Wall -Wextra -Werror -Wno-unused-parameter
|
||||
CFLAGS=-std=gnu11 -g -Wall -Wextra -Werror -Wno-unused-parameter
|
||||
LDFLAGS=-g -L/usr/local/lib
|
||||
|
||||
EWM_EXECUTABLE=ewm
|
||||
EWM_SOURCES=cpu.c ins.c pia.c mem.c ewm.c fmt.c two.c scr.c dsk.c chr.c alc.c one.c tty.c utl.c boo.c
|
||||
EWM_SOURCES=cpu.c ins.c pia.c mem.c ewm.c fmt.c two.c scr.c dsk.c chr.c alc.c one.c tty.c utl.c boo.c lua.c
|
||||
EWM_OBJECTS=$(EWM_SOURCES:.c=.o)
|
||||
EWM_LIBS=-lSDL2
|
||||
EWM_LIBS=-lSDL2 -llua
|
||||
|
||||
CPU_TEST_EXECUTABLE=cpu_test
|
||||
CPU_TEST_SOURCES=cpu.c ins.c mem.c fmt.c utl.c cpu_test.c
|
||||
CPU_TEST_SOURCES=cpu.c ins.c mem.c fmt.c utl.c cpu_test.c lua.c
|
||||
CPU_TEST_OBJECTS=$(CPU_TEST_SOURCES:.c=.o)
|
||||
CPU_TEST_LIBS=
|
||||
CPU_TEST_LIBS=-llua
|
||||
|
||||
SCR_TEST_EXECUTABLE=scr_test
|
||||
SCR_TEST_SOURCES=cpu.c ins.c mem.c fmt.c two.c scr.c dsk.c chr.c alc.c utl.c scr_test.c
|
||||
SCR_TEST_SOURCES=cpu.c ins.c mem.c fmt.c two.c scr.c dsk.c chr.c alc.c utl.c scr_test.c lua.c
|
||||
SCR_TEST_OBJECTS=$(SCR_TEST_SOURCES:.c=.o)
|
||||
SCR_TEST_LIBS=-lSDL2
|
||||
SCR_TEST_LIBS=-lSDL2 -llua
|
||||
|
||||
CPU_BENCH=cpu_bench
|
||||
CPU_BENCH_SOURCES=cpu.c ins.c mem.c fmt.c utl.c cpu_bench.c
|
||||
CPU_BENCH_SOURCES=cpu.c ins.c mem.c fmt.c utl.c cpu_bench.c lua.c
|
||||
CPU_BENCH_OBJECTS=$(CPU_BENCH_SOURCES:.c=.o)
|
||||
CPU_BENCH_LIBS=
|
||||
CPU_BENCH_LIBS=-llua
|
||||
|
||||
MEM_BENCH=mem_bench
|
||||
MEM_BENCH_SOURCES=cpu.c ins.c mem.c fmt.c utl.c mem_bench.c
|
||||
MEM_BENCH_SOURCES=cpu.c ins.c mem.c fmt.c utl.c mem_bench.c lua.c
|
||||
MEM_BENCH_OBJECTS=$(MEM_BENCH_SOURCES:.c=.o)
|
||||
MEM_BENCH_LIBS=
|
||||
MEM_BENCH_LIBS=-llua
|
||||
|
||||
all: $(EWM_SOURCES) $(EWM_EXECUTABLE) $(CPU_TEST_SOURCES) $(CPU_TEST_EXECUTABLE) $(SCR_TEST_EXECUTABLE) $(CPU_BENCH) $(MEM_BENCH)
|
||||
|
||||
|
|
216
src/cpu.c
216
src/cpu.c
|
@ -21,6 +21,7 @@
|
|||
// SOFTWARE.
|
||||
|
||||
#include <assert.h>
|
||||
#include <ctype.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
|
@ -36,6 +37,10 @@
|
|||
#include "ins.h"
|
||||
#include "mem.h"
|
||||
#include "fmt.h"
|
||||
#include "lua.h"
|
||||
|
||||
uint8_t xxx_op;
|
||||
int xxx_fn;
|
||||
|
||||
// Stack management.
|
||||
|
||||
|
@ -92,6 +97,12 @@ void _cpu_set_status(struct cpu_t *cpu, uint8_t status) {
|
|||
cpu->state.c = (status & (1 << 0));
|
||||
}
|
||||
|
||||
|
||||
// TODO This does not belong here I think
|
||||
|
||||
|
||||
|
||||
|
||||
static int cpu_execute_instruction(struct cpu_t *cpu) {
|
||||
/* Trace code - Refactor into its own function or module */
|
||||
char trace_instruction[256];
|
||||
|
@ -131,6 +142,34 @@ static int cpu_execute_instruction(struct cpu_t *cpu) {
|
|||
cpu->state.pc += i->bytes;
|
||||
}
|
||||
|
||||
if (cpu->lua != NULL) {
|
||||
if (i->opcode == xxx_op) {
|
||||
switch (i->bytes) {
|
||||
case 1:
|
||||
lua_rawgeti(cpu->lua->state, LUA_REGISTRYINDEX, xxx_fn);
|
||||
ewm_lua_push_cpu(cpu->lua, cpu);
|
||||
lua_pushinteger(cpu->lua->state, i->opcode);
|
||||
lua_pushinteger(cpu->lua->state, 0);
|
||||
lua_call(cpu->lua->state, 3, 0);
|
||||
break;
|
||||
case 2:
|
||||
lua_rawgeti(cpu->lua->state, LUA_REGISTRYINDEX, xxx_fn);
|
||||
ewm_lua_push_cpu(cpu->lua, cpu);
|
||||
lua_pushinteger(cpu->lua->state, i->opcode);
|
||||
lua_pushinteger(cpu->lua->state, mem_get_byte(cpu, pc+1));
|
||||
lua_call(cpu->lua->state, 3, 0);
|
||||
break;
|
||||
case 3:
|
||||
lua_rawgeti(cpu->lua->state, LUA_REGISTRYINDEX, xxx_fn);
|
||||
ewm_lua_push_cpu(cpu->lua, cpu);
|
||||
lua_pushinteger(cpu->lua->state, i->opcode);
|
||||
lua_pushinteger(cpu->lua->state, mem_get_word(cpu, pc+1));
|
||||
lua_call(cpu->lua->state, 3, 0);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Execute instruction */
|
||||
switch (i->bytes) {
|
||||
case 1:
|
||||
|
@ -442,3 +481,180 @@ int cpu_nmi(struct cpu_t *cpu) {
|
|||
int cpu_step(struct cpu_t *cpu) {
|
||||
return cpu_execute_instruction(cpu);
|
||||
}
|
||||
|
||||
// Lua support
|
||||
|
||||
// cpu state functions
|
||||
|
||||
#if 0
|
||||
static int lua_cpu_get_a(lua_State *state) {
|
||||
void *cpu_data = luaL_checkudata(state, 1, "cpu_meta_table");
|
||||
struct cpu_t *cpu = *((struct cpu_t**) cpu_data);
|
||||
|
||||
if (lua_gettop(state) == 2) {
|
||||
if(!lua_isnumber(state, lua_gettop(state))) {
|
||||
printf("First arg fail\n");
|
||||
return 0;
|
||||
}
|
||||
int n = lua_tointeger(state, lua_gettop(state));
|
||||
cpu->state.a = (uint8_t) n;
|
||||
return 0;
|
||||
} else {
|
||||
lua_pushnumber(state, cpu->state.a);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
static int lua_cpu_get_pc(lua_State *state) {
|
||||
void *cpu_data = luaL_checkudata(state, 1, "cpu_meta_table");
|
||||
struct cpu_t *cpu = *((struct cpu_t**) cpu_data);
|
||||
lua_pushnumber(state, cpu->state.pc);
|
||||
return 1;
|
||||
}
|
||||
#endif
|
||||
|
||||
static int lua_cpu_reset(lua_State *state) {
|
||||
printf("This is cpu.reset()\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int lua_cpu_index(lua_State *state) {
|
||||
printf("This is cpu.__index()\n");
|
||||
|
||||
void *cpu_data = luaL_checkudata(state, 1, "cpu_meta_table");
|
||||
struct cpu_t *cpu = *((struct cpu_t**) cpu_data);
|
||||
|
||||
if(!lua_isstring(state, 2)) {
|
||||
printf("TODO lua_cpu_index: arg 2 is not a string\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
const char *name = lua_tostring(state, 2);
|
||||
|
||||
if (strcmp(name, "a") == 0) {
|
||||
lua_pushnumber(state, cpu->state.a);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (strcmp(name, "x") == 0) {
|
||||
lua_pushnumber(state, cpu->state.x);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (strcmp(name, "y") == 0) {
|
||||
lua_pushnumber(state, cpu->state.y);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (strcmp(name, "pc") == 0) {
|
||||
lua_pushnumber(state, cpu->state.pc);
|
||||
return 1;
|
||||
}
|
||||
|
||||
printf("TODO lua_cpu_index: Unknown property so what do we do?\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int lua_cpu_new_index(lua_State *state) {
|
||||
printf("This is cpu.__newindex()\n");
|
||||
|
||||
void *cpu_data = luaL_checkudata(state, 1, "cpu_meta_table");
|
||||
struct cpu_t *cpu = *((struct cpu_t**) cpu_data);
|
||||
|
||||
if(!lua_isstring(state, 2)) {
|
||||
printf("TODO lua_cpu_new_index: arg 2 is not a string\n");
|
||||
return 0;
|
||||
}
|
||||
const char *name = lua_tostring(state, 2);
|
||||
|
||||
if(!lua_isnumber(state, 3)) {
|
||||
printf("TODO lua_cpu_new_index: arg 3 is not a string\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
int value = lua_tointeger(state, 3);
|
||||
|
||||
if (strcmp(name, "a") == 0) {
|
||||
cpu->state.a = (uint8_t) value;
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (strcmp(name, "x") == 0) {
|
||||
cpu->state.x = (uint8_t) value;
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (strcmp(name, "y") == 0) {
|
||||
cpu->state.y = (uint8_t) value;
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (strcmp(name, "pc") == 0) {
|
||||
cpu->state.pc = (uint16_t) value; // TODO Does this actually work?
|
||||
return 0;
|
||||
}
|
||||
|
||||
printf("TODO lua_cpu_newindex: Unknown property so what do we do?\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// cpu module functions
|
||||
|
||||
static int ewm_cpu_lua_hello(lua_State *lua) {
|
||||
printf("This is cpu.hello()\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
// onBeforeExecution(op, fn)
|
||||
static int ewm_cpu_lua_onBeforeExecuteInstruction(lua_State *state) {
|
||||
printf("This is cpu.onBeforeExecuteInstruction\n");
|
||||
|
||||
if(!lua_isnumber(state, lua_gettop(state) - 1)) {
|
||||
printf("First arg fail\n");
|
||||
return 0;
|
||||
}
|
||||
xxx_op = lua_tointeger(state, lua_gettop(state) - 1);
|
||||
|
||||
if(!lua_isfunction(state, lua_gettop(state) - 0)) {
|
||||
printf("Second arg fail\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
lua_pushvalue(state, lua_gettop(state) - 0);
|
||||
xxx_fn = luaL_ref(state, LUA_REGISTRYINDEX);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int ewm_cpu_luaopen(lua_State *state) {
|
||||
luaL_Reg cpu_functions[] = {
|
||||
{"hello", ewm_cpu_lua_hello},
|
||||
{"onBeforeExecuteInstruction", ewm_cpu_lua_onBeforeExecuteInstruction},
|
||||
{NULL, NULL}
|
||||
};
|
||||
luaL_newlib(state, cpu_functions);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int ewm_cpu_init_lua(struct cpu_t *cpu, struct ewm_lua_t *lua) {
|
||||
cpu->lua = lua; // TODO This needs to move to cpu_luaopen so that
|
||||
// we don't actually enable lua support until this
|
||||
// module is required in a script. Same for other
|
||||
// components.
|
||||
luaL_requiref(cpu->lua->state, "cpu", ewm_cpu_luaopen, 0);
|
||||
|
||||
luaL_Reg functions[] = {
|
||||
//{"a", lua_cpu_get_a},
|
||||
//{"pc", lua_cpu_get_pc},
|
||||
{"reset", lua_cpu_reset},
|
||||
{"__index", lua_cpu_index},
|
||||
{"__newindex", lua_cpu_new_index},
|
||||
{NULL, NULL}
|
||||
};
|
||||
ewm_lua_register_component(lua->state, "cpu", functions);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -39,6 +39,7 @@
|
|||
#define EWM_VECTOR_IRQ 0xfffe
|
||||
|
||||
struct cpu_instruction_t;
|
||||
struct ewm_lua_t;
|
||||
|
||||
struct cpu_state_t {
|
||||
uint8_t a, x, y, s, sp;
|
||||
|
@ -57,6 +58,8 @@ struct cpu_t {
|
|||
|
||||
uint8_t *page0;
|
||||
uint8_t *page1;
|
||||
|
||||
struct ewm_lua_t *lua;
|
||||
};
|
||||
|
||||
typedef void (*cpu_instruction_handler_t)(struct cpu_t *cpu);
|
||||
|
@ -122,4 +125,6 @@ uint8_t cpu_memory_get_byte(struct cpu_t *cpu, uint16_t addr);
|
|||
void cpu_memory_set_word(struct cpu_t *cpu, uint16_t addr, uint16_t v);
|
||||
void cpu_memory_set_byte(struct cpu_t *cpu, uint16_t addr, uint8_t v);
|
||||
|
||||
int ewm_cpu_init_lua(struct cpu_t *cpu, struct ewm_lua_t *lua);
|
||||
|
||||
#endif
|
||||
|
|
23
src/dsk.c
23
src/dsk.c
|
@ -30,6 +30,7 @@
|
|||
#include "mem.h"
|
||||
#include "cpu.h"
|
||||
#include "utl.h"
|
||||
#include "lua.h"
|
||||
#include "dsk.h"
|
||||
|
||||
//
|
||||
|
@ -553,3 +554,25 @@ int ewm_dsk_set_disk_file(struct ewm_dsk_t *dsk, uint8_t drive, bool readonly, c
|
|||
|
||||
return result;
|
||||
}
|
||||
|
||||
// Lua Support
|
||||
|
||||
static int ewm_dsk_lua_hello(lua_State *lua) {
|
||||
printf("This is dsk.hello()\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ewm_dsk_luaopen(lua_State *state) {
|
||||
luaL_Reg functions[] = {
|
||||
{"hello", ewm_dsk_lua_hello},
|
||||
{NULL, NULL}
|
||||
};
|
||||
luaL_newlib(state, functions);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int ewm_dsk_init_lua(struct ewm_dsk_t *dsk, struct ewm_lua_t *lua) {
|
||||
dsk->lua = lua;
|
||||
luaL_requiref(dsk->lua->state, "dsk", ewm_dsk_luaopen, 0);
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -28,6 +28,7 @@
|
|||
|
||||
struct cpu_t;
|
||||
struct mem_t;
|
||||
struct ewm_lua_t;
|
||||
|
||||
#define EWM_DSK_DRIVE1 (0)
|
||||
#define EWM_DSK_DRIVE2 (1)
|
||||
|
@ -61,6 +62,7 @@ struct ewm_dsk_t {
|
|||
struct ewm_dsk_drive_t drives[2];
|
||||
uint8_t drive; // 0 based
|
||||
int skip;
|
||||
struct ewm_lua_t *lua;
|
||||
};
|
||||
|
||||
#define EWM_DSK_TYPE_UNKNOWN (-1)
|
||||
|
@ -72,4 +74,6 @@ struct ewm_dsk_t *ewm_dsk_create(struct cpu_t *cpu);
|
|||
int ewm_dsk_set_disk_data(struct ewm_dsk_t *dsk, uint8_t index, bool readonly, void *data, size_t length, int type);
|
||||
int ewm_dsk_set_disk_file(struct ewm_dsk_t *dsk, uint8_t index, bool readonly, char *path);
|
||||
|
||||
int ewm_dsk_init_lua(struct ewm_dsk_t *dsk, struct ewm_lua_t *lua);
|
||||
|
||||
#endif
|
||||
|
|
96
src/lua.c
Normal file
96
src/lua.c
Normal file
|
@ -0,0 +1,96 @@
|
|||
// The MIT License (MIT)
|
||||
//
|
||||
// Copyright (c) 2015 Stefan Arentz - http://github.com/st3fan/ewm
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <lua.h>
|
||||
#include <lauxlib.h>
|
||||
#include <lualib.h>
|
||||
|
||||
#include "lua.h"
|
||||
#include "two.h"
|
||||
#include "cpu.h"
|
||||
#include "dsk.h"
|
||||
|
||||
static int ewm_lua_init(struct ewm_lua_t *lua) {
|
||||
memset(lua, 0x00, sizeof(struct ewm_lua_t));
|
||||
lua->state = luaL_newstate();
|
||||
luaL_openlibs(lua->state);
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct ewm_lua_t *ewm_lua_create() {
|
||||
struct ewm_lua_t *lua = malloc(sizeof(struct ewm_lua_t));
|
||||
if (ewm_lua_init(lua) != 0) {
|
||||
free(lua);
|
||||
lua = NULL;
|
||||
}
|
||||
return lua;
|
||||
}
|
||||
|
||||
int ewm_lua_load_script(struct ewm_lua_t *lua, char *script_path) {
|
||||
return luaL_dofile(lua->state, script_path);
|
||||
}
|
||||
|
||||
void ewm_lua_push_cpu(struct ewm_lua_t *lua, struct cpu_t *cpu) {
|
||||
void *cpu_data = lua_newuserdata(lua->state, sizeof(struct cpu_t*));
|
||||
*((struct cpu_t**) cpu_data) = cpu;
|
||||
luaL_getmetatable(lua->state, "cpu_meta_table");
|
||||
lua_setmetatable(lua->state, -2);
|
||||
}
|
||||
|
||||
void ewm_lua_push_two(struct ewm_lua_t *lua, struct ewm_two_t *two) {
|
||||
void *two_data = lua_newuserdata(lua->state, sizeof(struct ewm_two_t*));
|
||||
*((struct ewm_two_t**) two_data) = two;
|
||||
luaL_getmetatable(lua->state, "two_meta_table");
|
||||
lua_setmetatable(lua->state, -2);
|
||||
}
|
||||
|
||||
void ewm_lua_push_dsk(struct ewm_lua_t *lua, struct ewm_dsk_t *dsk) {
|
||||
void *dsk_data = lua_newuserdata(lua->state, sizeof(struct ewm_dsk_t*));
|
||||
*((struct ewm_dsk_t**) dsk_data) = dsk;
|
||||
luaL_getmetatable(lua->state, "dsk_meta_table");
|
||||
lua_setmetatable(lua->state, -2);
|
||||
}
|
||||
|
||||
void ewm_lua_register_component(lua_State *state, char *name, luaL_Reg *functions) {
|
||||
char table_name[64];
|
||||
strncpy(table_name, name, sizeof(table_name)-1);
|
||||
strncat(table_name, "_meta_table", sizeof(table_name)-1);
|
||||
|
||||
char global_name[64];
|
||||
strncpy(global_name, name, sizeof(global_name)-1);
|
||||
for (size_t i = 0; i < strlen(global_name); i++) {
|
||||
global_name[i] = toupper(global_name[i]);
|
||||
}
|
||||
|
||||
// Register the cpu meta table
|
||||
luaL_newmetatable(state, table_name);
|
||||
luaL_setfuncs(state, functions, 0);
|
||||
|
||||
//lua_pushvalue(state, -1);
|
||||
//lua_setfield(state, -1, "__index");
|
||||
|
||||
// Keep it around
|
||||
lua_setglobal(state, "CPU"); // TODO I don't think we actually need this
|
||||
}
|
47
src/lua.h
Normal file
47
src/lua.h
Normal file
|
@ -0,0 +1,47 @@
|
|||
// The MIT License (MIT)
|
||||
//
|
||||
// Copyright (c) 2015 Stefan Arentz - http://github.com/st3fan/ewm
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
|
||||
#ifndef LUA_H
|
||||
#define LUA_H
|
||||
|
||||
#include <lua.h>
|
||||
#include <lauxlib.h>
|
||||
#include <lualib.h>
|
||||
|
||||
struct cpu_t;
|
||||
struct ewm_two_t;
|
||||
struct ewm_dsk_t;
|
||||
|
||||
struct ewm_lua_t {
|
||||
lua_State *state;
|
||||
};
|
||||
|
||||
struct ewm_lua_t *ewm_lua_create();
|
||||
int ewm_lua_load_script(struct ewm_lua_t *lua, char *script_path);
|
||||
|
||||
void ewm_lua_push_cpu(struct ewm_lua_t *lua, struct cpu_t *cpu);
|
||||
void ewm_lua_push_two(struct ewm_lua_t *lua, struct ewm_two_t *two);
|
||||
void ewm_lua_push_dsk(struct ewm_lua_t *lua, struct ewm_dsk_t *dsk);
|
||||
|
||||
void ewm_lua_register_component(lua_State *state, char *name, luaL_Reg *functions);
|
||||
|
||||
#endif // LUA_H
|
53
src/test.lua
Normal file
53
src/test.lua
Normal file
|
@ -0,0 +1,53 @@
|
|||
-- EWM Meets Lua
|
||||
|
||||
local function myerrhandler(err)
|
||||
print(err)
|
||||
print(debug.traceback())
|
||||
return false
|
||||
end
|
||||
|
||||
xpcall(function()
|
||||
|
||||
local two = require 'two'
|
||||
two.hello()
|
||||
|
||||
local cpu = require 'cpu'
|
||||
cpu.hello()
|
||||
|
||||
local dsk = require 'dsk'
|
||||
dsk.hello()
|
||||
|
||||
-- Intercept JSR COUT calls
|
||||
-- cpu.onBeforeExecuteInstruction(0x20, function(cpu, opcode, operand)
|
||||
-- if operand == 0xfded then
|
||||
-- print(string.format("COUT was called from %.4x with A=%.2x", cpu:pc(), cpu:a()))
|
||||
-- if cpu:a() >= 0xc1 and cpu:a() <= 0xda then
|
||||
-- -- Lets inverse this character
|
||||
-- cpu:a(cpu:a() - 0xC0)
|
||||
-- end
|
||||
-- end
|
||||
-- end)
|
||||
|
||||
cpu.onBeforeExecuteInstruction(0x20, function(state, opcode, operand)
|
||||
if operand == 0xfded then
|
||||
print(string.format("COUT was called from %.4x with A=%.2x", state.a, state.pc))
|
||||
if state.a >= 0xc1 and state.a <= 0xda then
|
||||
-- Lets inverse this character
|
||||
state.a = state.a - 0xC0
|
||||
end
|
||||
end
|
||||
end)
|
||||
|
||||
-- Reset when the R key is pressed
|
||||
--two.onKeyDown('R', function(two, key)
|
||||
-- print('You pressed R')
|
||||
-- two.reset()
|
||||
--end)
|
||||
|
||||
-- Enter the Monitor when the M key is pressed
|
||||
--two.onKeyDown('M', function(two, key)
|
||||
-- print('You pressed B')
|
||||
-- two.monitor()
|
||||
--end)
|
||||
|
||||
end , myerrhandler )
|
54
src/two.c
54
src/two.c
|
@ -34,9 +34,9 @@
|
|||
#include "alc.h"
|
||||
#include "chr.h"
|
||||
#include "scr.h"
|
||||
#include "lua.h"
|
||||
#include "two.h"
|
||||
|
||||
|
||||
#define EWM_A2P_SS_KBD 0xc000
|
||||
#define EWM_A2P_SS_KBDSTRB 0xc010
|
||||
#define EWM_A2P_SS_SPKR 0xc030
|
||||
|
@ -350,12 +350,34 @@ void ewm_two_destroy(struct ewm_two_t *two) {
|
|||
// TODO
|
||||
}
|
||||
|
||||
// Lua support
|
||||
|
||||
static int ewm_two_lua_hello(lua_State *lua) {
|
||||
printf("This is two.hello()\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ewm_two_luaopen(lua_State *state) {
|
||||
luaL_Reg ewm_two_functions[] = {
|
||||
{"hello", ewm_two_lua_hello},
|
||||
{NULL, NULL}
|
||||
};
|
||||
luaL_newlib(state, ewm_two_functions);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int ewm_two_init_lua(struct ewm_two_t *two, struct ewm_lua_t *lua) {
|
||||
two->lua = lua;
|
||||
luaL_requiref(two->lua->state, "two", ewm_two_luaopen, 0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// External API
|
||||
|
||||
int ewm_two_load_disk(struct ewm_two_t *two, int drive, char *path) {
|
||||
return ewm_dsk_set_disk_file(two->dsk, drive, false, path);
|
||||
}
|
||||
|
||||
|
||||
|
||||
static bool ewm_two_poll_event(struct ewm_two_t *two, SDL_Window *window) { // TODO Should window be part of ewm_two_t?
|
||||
SDL_Event event;
|
||||
while (SDL_PollEvent(&event) != 0) {
|
||||
|
@ -537,6 +559,7 @@ static void ewm_two_update_status_bar(struct ewm_two_t *two, double mhz) {
|
|||
#define EWM_TWO_OPT_TRACE (6)
|
||||
#define EWM_TWO_OPT_STRICT (7)
|
||||
#define EWM_TWO_OPT_DEBUG (8)
|
||||
#define EWM_TWO_OPT_LUA (9)
|
||||
|
||||
static struct option one_options[] = {
|
||||
{ "help", no_argument, NULL, EWM_TWO_OPT_HELP },
|
||||
|
@ -548,6 +571,7 @@ static struct option one_options[] = {
|
|||
{ "trace", optional_argument, NULL, EWM_TWO_OPT_TRACE },
|
||||
{ "strict", no_argument, NULL, EWM_TWO_OPT_STRICT },
|
||||
{ "debug", no_argument, NULL, EWM_TWO_OPT_DEBUG },
|
||||
{ "lua", required_argument, NULL, EWM_TWO_OPT_LUA },
|
||||
{ NULL, 0, NULL, 0 }
|
||||
};
|
||||
|
||||
|
@ -561,6 +585,7 @@ static void usage() {
|
|||
fprintf(stderr, " --trace <file> trace cpu to file\n");
|
||||
fprintf(stderr, " --strict run emulator in strict mode\n");
|
||||
fprintf(stderr, " --debug print debug info\n");
|
||||
fprintf(stderr, " --lua <script> load lua script into the emulator\n");
|
||||
}
|
||||
|
||||
int ewm_two_main(int argc, char **argv) {
|
||||
|
@ -574,6 +599,7 @@ int ewm_two_main(int argc, char **argv) {
|
|||
char *trace_path = NULL;
|
||||
bool strict = false;
|
||||
bool debug = false;
|
||||
char *lua_script_path = NULL;
|
||||
|
||||
int ch;
|
||||
while ((ch = getopt_long_only(argc, argv, "", one_options, NULL)) != -1) {
|
||||
|
@ -612,6 +638,9 @@ int ewm_two_main(int argc, char **argv) {
|
|||
case EWM_TWO_OPT_DEBUG:
|
||||
debug = true;
|
||||
break;
|
||||
case EWM_TWO_OPT_LUA:
|
||||
lua_script_path = optarg;
|
||||
break;
|
||||
default: {
|
||||
usage();
|
||||
exit(1);
|
||||
|
@ -719,6 +748,25 @@ int ewm_two_main(int argc, char **argv) {
|
|||
cpu_strict(two->cpu, strict);
|
||||
cpu_trace(two->cpu, trace_path);
|
||||
|
||||
// Setup a Lua environment if we were asked to
|
||||
|
||||
if (lua_script_path != NULL) {
|
||||
two->lua = ewm_lua_create();
|
||||
|
||||
luaL_requiref(two->lua->state, "ewm", ewm_two_luaopen, 0); // ewm_two_init_lua? that calls it for all its components?
|
||||
|
||||
ewm_two_init_lua(two, two->lua);
|
||||
ewm_cpu_init_lua(two->cpu, two->lua);
|
||||
ewm_dsk_init_lua(two->dsk, two->lua);
|
||||
|
||||
if (ewm_lua_load_script(two->lua, lua_script_path) != 0) {
|
||||
printf("Lua script failed to load\n"); // TODO Move errors reporting into C code
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
// Reset things to a known state
|
||||
|
||||
cpu_reset(two->cpu);
|
||||
|
||||
//
|
||||
|
|
|
@ -56,11 +56,13 @@
|
|||
struct mem_t;
|
||||
struct ewm_dsk_t;
|
||||
struct scr;
|
||||
struct ewm_lua_t;
|
||||
|
||||
struct ewm_two_t {
|
||||
int type;
|
||||
struct cpu_t *cpu;
|
||||
struct scr_t *scr;
|
||||
struct ewm_lua_t *lua;
|
||||
struct ewm_dsk_t *dsk;
|
||||
struct ewm_alc_t *alc;
|
||||
|
||||
|
@ -104,6 +106,8 @@ struct ewm_two_t {
|
|||
struct ewm_two_t *ewm_two_create(int type, SDL_Renderer *renderer, SDL_Joystick *joystick);
|
||||
void ewm_two_destroy(struct ewm_two_t *two);
|
||||
|
||||
int ewm_two_init_lua(struct ewm_two_t *two, struct ewm_lua_t *lua);
|
||||
|
||||
int ewm_two_load_disk(struct ewm_two_t *two, int drive, char *path);
|
||||
|
||||
int ewm_two_main(int argc, char **argv);
|
||||
|
|
Loading…
Reference in New Issue
Block a user