From d8bcf49e80fb4c20ca321e5a1e68143c10c8ae74 Mon Sep 17 00:00:00 2001 From: Stefan Arentz Date: Fri, 1 Jun 2018 19:03:11 -0400 Subject: [PATCH] Work in progress --- src/CMakeLists.txt | 6 ++-- src/Makefile | 12 ++++---- src/cpu.h | 5 ++++ src/dbg.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++ src/dbg.h | 14 ++++++++++ src/one.c | 14 +++++++++- 6 files changed, 109 insertions(+), 10 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 59d6ffb..4bdd69a 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -22,11 +22,11 @@ add_executable(cpu_test ${CPU_SOURCES} cpu_test.c) add_executable(cpu_bench ${CPU_SOURCES} cpu_bench.c) add_executable(ewm ${CPU_SOURCES} ${BOO_SOURCES} ${ONE_SOURCES} ${TWO_SOURCES} ${SDL_SOURCES} ewm.c) -target_link_libraries(ewm SDL2) +target_link_libraries(ewm SDL2 readline) add_executable(tty_test ${CPU_SOURCES} ${ONE_SOURCES} ${SDL_SOURCES} tty_test.c) -target_link_libraries(tty_test SDL2) +target_link_libraries(tty_test SDL2 readline) add_executable(scr_test ${CPU_SOURCES} ${TWO_SOURCES} ${SDL_SOURCES} scr_test.c) -target_link_libraries(scr_test SDL2) +target_link_libraries(scr_test SDL2 readline) diff --git a/src/Makefile b/src/Makefile index de80846..41fce78 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,5 +1,5 @@ # CMAKE generated file: DO NOT EDIT! -# Generated by "Unix Makefiles" Generator, CMake Version 3.9 +# Generated by "Unix Makefiles" Generator, CMake Version 3.8 # Default target executed when no arguments are given to make. default_target: all @@ -39,10 +39,10 @@ cmake_force: SHELL = /bin/sh # The CMake executable. -CMAKE_COMMAND = /usr/local/Cellar/cmake/3.9.4/bin/cmake +CMAKE_COMMAND = /Applications/CLion.app/Contents/bin/cmake/bin/cmake # The command to remove a file. -RM = /usr/local/Cellar/cmake/3.9.4/bin/cmake -E remove -f +RM = /Applications/CLion.app/Contents/bin/cmake/bin/cmake -E remove -f # Escaping for special characters. EQUALS = = @@ -59,7 +59,7 @@ CMAKE_BINARY_DIR = /Users/stefan/Projects/ewm/src # Special rule for the target rebuild_cache rebuild_cache: @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Running CMake to regenerate build system..." - /usr/local/Cellar/cmake/3.9.4/bin/cmake -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) + /Applications/CLion.app/Contents/bin/cmake/bin/cmake -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) .PHONY : rebuild_cache # Special rule for the target rebuild_cache @@ -69,8 +69,8 @@ rebuild_cache/fast: rebuild_cache # Special rule for the target edit_cache edit_cache: - @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Running CMake cache editor..." - /usr/local/Cellar/cmake/3.9.3_1/bin/ccmake -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) + @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "No interactive CMake dialog available..." + /Applications/CLion.app/Contents/bin/cmake/bin/cmake -E echo No\ interactive\ CMake\ dialog\ available. .PHONY : edit_cache # Special rule for the target edit_cache diff --git a/src/cpu.h b/src/cpu.h index 46cb1a1..78d5c3f 100644 --- a/src/cpu.h +++ b/src/cpu.h @@ -38,6 +38,9 @@ #define EWM_VECTOR_RES 0xfffc #define EWM_VECTOR_IRQ 0xfffe +#define EWM_CPU_STATUS_RUNNING (0) +#define EWM_CPU_STATUS_PAUSED (1) + struct cpu_instruction_t; struct ewm_lua_t; @@ -62,6 +65,8 @@ struct cpu_t { #if defined(EWM_LUA) struct ewm_lua_t *lua; #endif + + int status; }; typedef void (*cpu_instruction_handler_t)(struct cpu_t *cpu); diff --git a/src/dbg.c b/src/dbg.c index 29899f5..b215217 100644 --- a/src/dbg.c +++ b/src/dbg.c @@ -20,5 +20,73 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. +#include +#include + +#include +#include + +#include + +#include "cpu.h" #include "dbg.h" +static int ewm_dbg_thread(void *object) { + struct ewm_dbg_t *dbg = (struct ewm_dbg_t*) object; + printf("Welcome to the EWM debugger\n"); + while (1) { + // Wait for the CPU to pause + while (dbg->cpu->status == EWM_CPU_STATUS_RUNNING) { + // TODO Replace this with a condition variable + } + + printf("%.4x: A=%.2x X=%.2x Y=%.2x SP=%.2x\n", dbg->cpu->state.pc, dbg->cpu->state.a, + dbg->cpu->state.x, dbg->cpu->state.y, dbg->cpu->state.sp); + + char *line = readline("(ewm) "); + + if (strcmp(line, "quit") == 0) { + exit(0); + } + + if (strcmp(line, "run") == 0) { + ewm_dbg_continue(dbg); + continue; + } + + if (strcmp(line, "br $ffef") == 0) { + dbg_breakpoint_set(dbg, 0xffef); + } + + printf("Sorry I don't understand that\n"); + } + return 0; +} + +int ewm_dbg_start(struct ewm_dbg_t *dbg) { + SDL_Thread *thread = SDL_CreateThread(ewm_dbg_thread, "ewm: debugger", dbg); + if (thread == NULL) { + return -1; + } + return 0; +} + +void ewm_dbg_pause(struct ewm_dbg_t *dbg) { + dbg->cpu->status = EWM_CPU_STATUS_PAUSED; +} + +void ewm_dbg_continue(struct ewm_dbg_t *dbg) { + dbg->cpu->status = EWM_CPU_STATUS_RUNNING; +} + +void ewm_dbg_breakpoint_set(struct ewm_dbg_t *dbg, uint16_t addr) { + printf("Setting breakpoint at %.4x\n", addr); +} + +struct ewm_dbg_t *ewm_dbg_create(struct cpu_t *cpu) { + struct ewm_dbg_t *dbg = (struct ewm_dbg_t*) malloc(sizeof(struct ewm_dbg_t)); + if (dbg != NULL) { + dbg->cpu = cpu; + } + return dbg; +} diff --git a/src/dbg.h b/src/dbg.h index 4483300..e0c861f 100644 --- a/src/dbg.h +++ b/src/dbg.h @@ -23,4 +23,18 @@ #ifndef EWM_DBG_H #define EWM_DBG_H +struct cpu_t; + +struct ewm_dbg_t { + struct cpu_t *cpu; +}; + +struct ewm_dbg_t *ewm_dbg_create(struct cpu_t *cpu); +int ewm_dbg_start(struct ewm_dbg_t *dbg); + +void ewm_dbg_pause(struct ewm_dbg_t *dbg); +void ewm_dbg_continue(struct ewm_dbg_t *dbg); + +void ewm_dbg_breakpoint_set(struct ewm_dbg_t *dbg, uint16_t addr); + #endif // EWM_DBG_H diff --git a/src/one.c b/src/one.c index d710bfa..b2dd09b 100644 --- a/src/one.c +++ b/src/one.c @@ -145,6 +145,10 @@ static bool ewm_one_poll_event(struct ewm_one_t *one, SDL_Window *window) { } static bool ewm_one_step_cpu(struct ewm_one_t *one, int cycles) { + if (one->cpu->status == EWM_CPU_STATUS_PAUSED) { + return true; + } + while (true) { int ret = cpu_step(one->cpu); if (ret < 0) { @@ -305,9 +309,17 @@ int ewm_one_main(int argc, char **argv) { cpu_strict(one->cpu, strict); cpu_trace(one->cpu, trace_path); - cpu_reset(one->cpu); + if (debug_port != 0) { + struct ewm_dbg_t *dbg = ewm_dbg_create(one->cpu); + ewm_dbg_pause(dbg); + if (ewm_dbg_start(dbg) != 0) { + printf("ewm: one: failed to start debugger\n"); + exit(1); + } + } + // Main loop SDL_StartTextInput();