Fixes #164 - Make Lua Optional (#165)

This commit is contained in:
Stefan Arentz 2017-10-02 20:32:43 -04:00 committed by GitHub
parent 9a517223e2
commit 839ce07e9c
8 changed files with 606 additions and 527 deletions

View File

@ -34,25 +34,34 @@ else
LDFLAGS += -g LDFLAGS += -g
endif endif
LUA_LIBS=-llua ifdef LUA
ifeq ($(UNAME), Linux) CFLAGS += -DEWM_LUA
LUA_LIBS = -llua5.1 ifeq ($(UNAME), Linux)
CFLAGS += -I/usr/include/lua5.1 LUA_LIBS = -llua5.1
LDFLAGS += -L/usr/lib/arm-linux-gnueabihf CFLAGS += -I/usr/include/lua5.1
LDFLAGS += -L/usr/lib/arm-linux-gnueabihf
else
LUA_LIBS=-llua
endif
endif
CPU_SOURCES=cpu.c mem.c fmt.c ins.c utl.c
ifdef LUA
CPU_SOURCES += lua.c
endif endif
EWM_EXECUTABLE=ewm 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 lua.c sdl.c EWM_SOURCES=$(CPU_SOURCES) pia.c ewm.c two.c scr.c dsk.c chr.c alc.c one.c tty.c boo.c sdl.c
EWM_OBJECTS=$(EWM_SOURCES:.c=.o) EWM_OBJECTS=$(EWM_SOURCES:.c=.o)
EWM_LIBS=-lSDL2 $(LUA_LIBS) EWM_LIBS=-lSDL2 $(LUA_LIBS)
CPU_TEST_EXECUTABLE=cpu_test CPU_TEST_EXECUTABLE=cpu_test
CPU_TEST_SOURCES=cpu.c ins.c mem.c fmt.c utl.c cpu_test.c lua.c CPU_TEST_SOURCES=$(CPU_SOURCES) cpu_test.c
CPU_TEST_OBJECTS=$(CPU_TEST_SOURCES:.c=.o) CPU_TEST_OBJECTS=$(CPU_TEST_SOURCES:.c=.o)
CPU_TEST_LIBS=$(LUA_LIBS) CPU_TEST_LIBS=$(LUA_LIBS)
SCR_TEST_EXECUTABLE=scr_test 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 lua.c sdl.c SCR_TEST_SOURCES=$(CPU_SOURCES) two.c scr.c dsk.c chr.c alc.c scr_test.c sdl.c
SCR_TEST_OBJECTS=$(SCR_TEST_SOURCES:.c=.o) SCR_TEST_OBJECTS=$(SCR_TEST_SOURCES:.c=.o)
SCR_TEST_LIBS=-lSDL2 $(LUA_LIBS) SCR_TEST_LIBS=-lSDL2 $(LUA_LIBS)
@ -62,12 +71,12 @@ TTY_TEST_OBJECTS=$(TTY_TEST_SOURCES:.c=.o)
TTY_TEST_LIBS=-lSDL2 $(LUA_LIBS) TTY_TEST_LIBS=-lSDL2 $(LUA_LIBS)
CPU_BENCH=cpu_bench CPU_BENCH=cpu_bench
CPU_BENCH_SOURCES=cpu.c ins.c mem.c fmt.c utl.c cpu_bench.c lua.c CPU_BENCH_SOURCES=$(CPU_SOURCES) cpu_bench.c
CPU_BENCH_OBJECTS=$(CPU_BENCH_SOURCES:.c=.o) CPU_BENCH_OBJECTS=$(CPU_BENCH_SOURCES:.c=.o)
CPU_BENCH_LIBS=$(LUA_LIBS) CPU_BENCH_LIBS=$(LUA_LIBS)
MEM_BENCH=mem_bench MEM_BENCH=mem_bench
MEM_BENCH_SOURCES=cpu.c ins.c mem.c fmt.c utl.c mem_bench.c lua.c MEM_BENCH_SOURCES=$(CPU_SOURCES) mem_bench.c
MEM_BENCH_OBJECTS=$(MEM_BENCH_SOURCES:.c=.o) MEM_BENCH_OBJECTS=$(MEM_BENCH_SOURCES:.c=.o)
MEM_BENCH_LIBS=$(LUA_LIBS) MEM_BENCH_LIBS=$(LUA_LIBS)

View File

@ -37,7 +37,10 @@
#include "ins.h" #include "ins.h"
#include "mem.h" #include "mem.h"
#include "fmt.h" #include "fmt.h"
#if defined(EWM_LUA)
#include "lua.h" #include "lua.h"
#endif
// Stack management. // Stack management.
@ -102,6 +105,7 @@ static int cpu_execute_instruction(struct cpu_t *cpu) {
uint16_t pc = cpu->state.pc; uint16_t pc = cpu->state.pc;
cpu->state.pc += i->bytes; cpu->state.pc += i->bytes;
#if defined(EWM_LUA)
if (i->lua_before_handler != LUA_NOREF) { if (i->lua_before_handler != LUA_NOREF) {
lua_rawgeti(cpu->lua->state, LUA_REGISTRYINDEX, i->lua_before_handler); lua_rawgeti(cpu->lua->state, LUA_REGISTRYINDEX, i->lua_before_handler);
ewm_lua_push_cpu(cpu->lua, cpu); ewm_lua_push_cpu(cpu->lua, cpu);
@ -121,6 +125,7 @@ static int cpu_execute_instruction(struct cpu_t *cpu) {
printf("cpu: script error: %s\n", lua_tostring(cpu->lua->state, -1)); printf("cpu: script error: %s\n", lua_tostring(cpu->lua->state, -1));
} }
} }
#endif
/* Execute instruction */ /* Execute instruction */
switch (i->bytes) { switch (i->bytes) {
@ -135,6 +140,7 @@ static int cpu_execute_instruction(struct cpu_t *cpu) {
break; break;
} }
#if defined(EWM_LUA)
if (i->lua_after_handler != LUA_NOREF) { if (i->lua_after_handler != LUA_NOREF) {
lua_rawgeti(cpu->lua->state, LUA_REGISTRYINDEX, i->lua_after_handler); lua_rawgeti(cpu->lua->state, LUA_REGISTRYINDEX, i->lua_after_handler);
ewm_lua_push_cpu(cpu->lua, cpu); ewm_lua_push_cpu(cpu->lua, cpu);
@ -154,6 +160,7 @@ static int cpu_execute_instruction(struct cpu_t *cpu) {
printf("cpu: script error: %s\n", lua_tostring(cpu->lua->state, -1)); printf("cpu: script error: %s\n", lua_tostring(cpu->lua->state, -1));
} }
} }
#endif
cpu->counter += i->cycles; cpu->counter += i->cycles;
@ -432,7 +439,11 @@ int cpu_step(struct cpu_t *cpu) {
return cpu_execute_instruction(cpu); return cpu_execute_instruction(cpu);
} }
#if defined(EWM_LUA)
//
// Lua support // Lua support
//
// cpu state functions // cpu state functions
@ -702,3 +713,5 @@ int ewm_cpu_init_lua(struct cpu_t *cpu, struct ewm_lua_t *lua) {
return 0; return 0;
} }
#endif

View File

@ -59,7 +59,9 @@ struct cpu_t {
uint8_t *ram; uint8_t *ram;
size_t ram_size; size_t ram_size;
#if defined(EWM_LUA)
struct ewm_lua_t *lua; struct ewm_lua_t *lua;
#endif
}; };
typedef void (*cpu_instruction_handler_t)(struct cpu_t *cpu); typedef void (*cpu_instruction_handler_t)(struct cpu_t *cpu);
@ -125,6 +127,8 @@ 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_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); void cpu_memory_set_byte(struct cpu_t *cpu, uint16_t addr, uint8_t v);
#if defined(EWM_LUA)
int ewm_cpu_init_lua(struct cpu_t *cpu, struct ewm_lua_t *lua); int ewm_cpu_init_lua(struct cpu_t *cpu, struct ewm_lua_t *lua);
#endif
#endif #endif

View File

@ -30,7 +30,9 @@
#include "cpu.h" #include "cpu.h"
#include "mem.h" #include "mem.h"
#include "utl.h" #include "utl.h"
#if defined(EWM_LUA)
#include "lua.h" #include "lua.h"
#endif
int test(int model, uint16_t start_addr, uint16_t success_addr, char *rom_path, int with_lua) { int test(int model, uint16_t start_addr, uint16_t success_addr, char *rom_path, int with_lua) {
struct cpu_t *cpu = cpu_create(model); struct cpu_t *cpu = cpu_create(model);
@ -38,6 +40,7 @@ int test(int model, uint16_t start_addr, uint16_t success_addr, char *rom_path,
cpu_reset(cpu); cpu_reset(cpu);
cpu->state.pc = start_addr; cpu->state.pc = start_addr;
#if defined(EWM_LUA)
if (with_lua) { if (with_lua) {
cpu->lua = ewm_lua_create(); cpu->lua = ewm_lua_create();
ewm_cpu_init_lua(cpu, cpu->lua); ewm_cpu_init_lua(cpu, cpu->lua);
@ -47,7 +50,8 @@ int test(int model, uint16_t start_addr, uint16_t success_addr, char *rom_path,
exit(1); exit(1);
} }
} }
#endif
uint16_t last_pc = cpu->state.pc; uint16_t last_pc = cpu->state.pc;
struct timespec start; struct timespec start;
@ -118,8 +122,10 @@ int main(int argc, char **argv) {
fprintf(stderr, "TEST Running 65C02 tests\n"); fprintf(stderr, "TEST Running 65C02 tests\n");
test(EWM_CPU_MODEL_65C02, 0x0400, 0x24a8, "rom/65C02_extended_opcodes_test.bin", 0); test(EWM_CPU_MODEL_65C02, 0x0400, 0x24a8, "rom/65C02_extended_opcodes_test.bin", 0);
#if defined(EWM_LUA)
fprintf(stderr, "TEST Running 6502 tests - With Lua\n"); fprintf(stderr, "TEST Running 6502 tests - With Lua\n");
test(EWM_CPU_MODEL_6502, 0x0400, 0x3399, "rom/6502_functional_test.bin", 1); test(EWM_CPU_MODEL_6502, 0x0400, 0x3399, "rom/6502_functional_test.bin", 1);
fprintf(stderr, "TEST Running 65C02 tests - With Lua\n"); fprintf(stderr, "TEST Running 65C02 tests - With Lua\n");
test(EWM_CPU_MODEL_65C02, 0x0400, 0x24a8, "rom/65C02_extended_opcodes_test.bin", 1); test(EWM_CPU_MODEL_65C02, 0x0400, 0x24a8, "rom/65C02_extended_opcodes_test.bin", 1);
#endif
} }

View File

@ -30,7 +30,9 @@
#include "mem.h" #include "mem.h"
#include "cpu.h" #include "cpu.h"
#include "utl.h" #include "utl.h"
#if defined(EWM_LUA)
#include "lua.h" #include "lua.h"
#endif
#include "dsk.h" #include "dsk.h"
// //
@ -555,9 +557,15 @@ int ewm_dsk_set_disk_file(struct ewm_dsk_t *dsk, uint8_t drive, bool readonly, c
return result; return result;
} }
#if defined(EWM_LUA)
//
// Lua Support // Lua Support
//
int ewm_dsk_init_lua(struct ewm_dsk_t *dsk, struct ewm_lua_t *lua) { int ewm_dsk_init_lua(struct ewm_dsk_t *dsk, struct ewm_lua_t *lua) {
dsk->lua = lua; dsk->lua = lua;
return 0; return 0;
} }
#endif

View File

@ -26,9 +26,12 @@
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h>
#if defined(EWM_LUA)
#include "lua.h"
#endif
struct cpu_t; struct cpu_t;
struct mem_t; struct mem_t;
struct ewm_lua_t;
#define EWM_DSK_DRIVE1 (0) #define EWM_DSK_DRIVE1 (0)
#define EWM_DSK_DRIVE2 (1) #define EWM_DSK_DRIVE2 (1)
@ -62,7 +65,9 @@ struct ewm_dsk_t {
struct ewm_dsk_drive_t drives[2]; struct ewm_dsk_drive_t drives[2];
uint8_t drive; // 0 based uint8_t drive; // 0 based
int skip; int skip;
#if defined(EWM_LUA)
struct ewm_lua_t *lua; struct ewm_lua_t *lua;
#endif
}; };
#define EWM_DSK_TYPE_UNKNOWN (-1) #define EWM_DSK_TYPE_UNKNOWN (-1)
@ -74,6 +79,8 @@ 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_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_set_disk_file(struct ewm_dsk_t *dsk, uint8_t index, bool readonly, char *path);
#if defined(EWM_LUA)
int ewm_dsk_init_lua(struct ewm_dsk_t *dsk, struct ewm_lua_t *lua); int ewm_dsk_init_lua(struct ewm_dsk_t *dsk, struct ewm_lua_t *lua);
#endif
#endif #endif

1035
src/ins.c

File diff suppressed because it is too large Load Diff

View File

@ -34,9 +34,12 @@
#include "alc.h" #include "alc.h"
#include "chr.h" #include "chr.h"
#include "scr.h" #include "scr.h"
#if defined(EWM_LUA)
#include "lua.h" #include "lua.h"
#endif
#include "two.h" #include "two.h"
#define EWM_A2P_SS_KBD 0xc000 #define EWM_A2P_SS_KBD 0xc000
#define EWM_A2P_SS_KBDSTRB 0xc010 #define EWM_A2P_SS_KBDSTRB 0xc010
#define EWM_A2P_SS_SPKR 0xc030 #define EWM_A2P_SS_SPKR 0xc030
@ -258,8 +261,10 @@ static int ewm_two_init(struct ewm_two_t *two, int type, SDL_Renderer *renderer,
two->type = type; two->type = type;
#if defined(EWM_LUA)
two->lua_key_down_fn = LUA_NOREF; two->lua_key_down_fn = LUA_NOREF;
two->lua_key_up_fn = LUA_NOREF; two->lua_key_up_fn = LUA_NOREF;
#endif
switch (type) { switch (type) {
case EWM_TWO_TYPE_APPLE2: { case EWM_TWO_TYPE_APPLE2: {
@ -325,7 +330,11 @@ void ewm_two_destroy(struct ewm_two_t *two) {
// TODO Or maybe not. // TODO Or maybe not.
} }
#if defined(EWM_LUA)
//
// Lua support // Lua support
//
static int two_lua_index(lua_State *state) { static int two_lua_index(lua_State *state) {
//void *two_data = luaL_checkudata(state, 1, "two_meta_table"); //void *two_data = luaL_checkudata(state, 1, "two_meta_table");
@ -429,6 +438,8 @@ int ewm_two_init_lua(struct ewm_two_t *two, struct ewm_lua_t *lua) {
return 0; return 0;
} }
#endif
// External API // External API
int ewm_two_load_disk(struct ewm_two_t *two, int drive, char *path) { int ewm_two_load_disk(struct ewm_two_t *two, int drive, char *path) {
@ -467,6 +478,7 @@ static bool ewm_two_poll_event(struct ewm_two_t *two, SDL_Window *window) { // T
break; break;
case SDL_KEYDOWN: case SDL_KEYDOWN:
#if defined(EWM_LUA)
if (two->lua_key_down_fn != LUA_NOREF) { if (two->lua_key_down_fn != LUA_NOREF) {
lua_rawgeti(two->lua->state, LUA_REGISTRYINDEX, two->lua_key_down_fn); lua_rawgeti(two->lua->state, LUA_REGISTRYINDEX, two->lua_key_down_fn);
ewm_lua_push_two(two->lua, two); ewm_lua_push_two(two->lua, two);
@ -486,6 +498,7 @@ static bool ewm_two_poll_event(struct ewm_two_t *two, SDL_Window *window) { // T
return true; return true;
} }
} }
#endif
if (event.key.keysym.mod & KMOD_CTRL) { if (event.key.keysym.mod & KMOD_CTRL) {
if (event.key.keysym.sym >= SDLK_a && event.key.keysym.sym <= SDLK_z) { if (event.key.keysym.sym >= SDLK_a && event.key.keysym.sym <= SDLK_z) {
@ -541,6 +554,7 @@ static bool ewm_two_poll_event(struct ewm_two_t *two, SDL_Window *window) { // T
break; break;
case SDL_KEYUP: case SDL_KEYUP:
#if defined(EWM_LUA)
if (two->lua_key_up_fn != LUA_NOREF) { if (two->lua_key_up_fn != LUA_NOREF) {
lua_rawgeti(two->lua->state, LUA_REGISTRYINDEX, two->lua_key_up_fn); lua_rawgeti(two->lua->state, LUA_REGISTRYINDEX, two->lua_key_up_fn);
ewm_lua_push_two(two->lua, two); ewm_lua_push_two(two->lua, two);
@ -560,6 +574,7 @@ static bool ewm_two_poll_event(struct ewm_two_t *two, SDL_Window *window) { // T
return true; return true;
} }
} }
#endif
if (event.key.keysym.mod & KMOD_ALT) { if (event.key.keysym.mod & KMOD_ALT) {
switch (event.key.keysym.sym) { switch (event.key.keysym.sym) {
@ -656,7 +671,9 @@ static void ewm_two_update_status_bar(struct ewm_two_t *two, double mhz) {
#define EWM_TWO_OPT_TRACE (6) #define EWM_TWO_OPT_TRACE (6)
#define EWM_TWO_OPT_STRICT (7) #define EWM_TWO_OPT_STRICT (7)
#define EWM_TWO_OPT_DEBUG (8) #define EWM_TWO_OPT_DEBUG (8)
#if defined(EWM_LUA)
#define EWM_TWO_OPT_SCRIPT (9) #define EWM_TWO_OPT_SCRIPT (9)
#endif
static struct option one_options[] = { static struct option one_options[] = {
{ "help", no_argument, NULL, EWM_TWO_OPT_HELP }, { "help", no_argument, NULL, EWM_TWO_OPT_HELP },
@ -668,7 +685,9 @@ static struct option one_options[] = {
{ "trace", optional_argument, NULL, EWM_TWO_OPT_TRACE }, { "trace", optional_argument, NULL, EWM_TWO_OPT_TRACE },
{ "strict", no_argument, NULL, EWM_TWO_OPT_STRICT }, { "strict", no_argument, NULL, EWM_TWO_OPT_STRICT },
{ "debug", no_argument, NULL, EWM_TWO_OPT_DEBUG }, { "debug", no_argument, NULL, EWM_TWO_OPT_DEBUG },
#if defined(EWM_LUA)
{ "script", required_argument, NULL, EWM_TWO_OPT_SCRIPT }, { "script", required_argument, NULL, EWM_TWO_OPT_SCRIPT },
#endif
{ NULL, 0, NULL, 0 } { NULL, 0, NULL, 0 }
}; };
@ -682,7 +701,9 @@ static void usage() {
fprintf(stderr, " --trace <file> trace cpu to file\n"); fprintf(stderr, " --trace <file> trace cpu to file\n");
fprintf(stderr, " --strict run emulator in strict mode\n"); fprintf(stderr, " --strict run emulator in strict mode\n");
fprintf(stderr, " --debug print debug info\n"); fprintf(stderr, " --debug print debug info\n");
#if defined(EWM_LUA)
fprintf(stderr, " --script <script> load Lua script into the emulator\n"); fprintf(stderr, " --script <script> load Lua script into the emulator\n");
#endif
} }
int ewm_two_main(int argc, char **argv) { int ewm_two_main(int argc, char **argv) {
@ -696,7 +717,9 @@ int ewm_two_main(int argc, char **argv) {
char *trace_path = NULL; char *trace_path = NULL;
bool strict = false; bool strict = false;
bool debug = false; bool debug = false;
#if defined(EWM_LUA)
char *script_path = NULL; char *script_path = NULL;
#endif
int ch; int ch;
while ((ch = getopt_long_only(argc, argv, "", one_options, NULL)) != -1) { while ((ch = getopt_long_only(argc, argv, "", one_options, NULL)) != -1) {
@ -735,9 +758,11 @@ int ewm_two_main(int argc, char **argv) {
case EWM_TWO_OPT_DEBUG: case EWM_TWO_OPT_DEBUG:
debug = true; debug = true;
break; break;
#if defined(EWM_LUA)
case EWM_TWO_OPT_SCRIPT: case EWM_TWO_OPT_SCRIPT:
script_path = optarg; script_path = optarg;
break; break;
#endif
default: { default: {
usage(); usage();
exit(1); exit(1);
@ -845,6 +870,7 @@ int ewm_two_main(int argc, char **argv) {
cpu_strict(two->cpu, strict); cpu_strict(two->cpu, strict);
cpu_trace(two->cpu, trace_path); cpu_trace(two->cpu, trace_path);
#if defined(EWM_LUA)
// Setup a Lua environment if scripts were specified // Setup a Lua environment if scripts were specified
if (script_path != NULL) { if (script_path != NULL) {
@ -862,6 +888,7 @@ int ewm_two_main(int argc, char **argv) {
exit(1); exit(1);
} }
} }
#endif
// Reset things to a known state // Reset things to a known state