all repos — mgba @ 1a7a544ba7891471fd94f2626a1c1af2c3427228

mGBA Game Boy Advance Emulator

Core: Add basic scripting bridge
Vicki Pfau vi@endrift.com
Fri, 07 Jul 2017 14:09:32 -0700
commit

1a7a544ba7891471fd94f2626a1c1af2c3427228

parent

dce49ea99022187e3798e484580d9a4624ed02bd

M CMakeLists.txtCMakeLists.txt

@@ -19,6 +19,7 @@ set(USE_SQLITE3 ON CACHE BOOL "Whether or not to enable SQLite3 support")

set(M_CORE_GBA ON CACHE BOOL "Build Game Boy Advance core") set(M_CORE_GB ON CACHE BOOL "Build Game Boy core") set(USE_LZMA ON CACHE BOOL "Whether or not to enable 7-Zip support") +set(ENABLE_SCRIPTING ON CACHE BOOL "Whether or not to enable scripting support") set(BUILD_QT ON CACHE BOOL "Build Qt frontend") set(BUILD_SDL ON CACHE BOOL "Build SDL frontend") set(BUILD_LIBRETRO OFF CACHE BOOL "Build libretro core")

@@ -353,6 +354,7 @@

# Feature dependencies set(FEATURE_DEFINES) set(FEATURES) +set(ENABLES) if(CMAKE_SYSTEM_NAME MATCHES .*BSD) set(LIBEDIT_LIBRARIES -ledit) if (CMAKE_SYSTEM_NAME STREQUAL OpenBSD)

@@ -599,6 +601,10 @@ set(CPACK_DEBIAN_PACKAGE_DEPENDS "${CPACK_DEBIAN_PACKAGE_DEPENDS},libsqlite3-0")

list(APPEND FEATURE_SRC "${CMAKE_CURRENT_SOURCE_DIR}/src/feature/sqlite3/no-intro.c") endif() +if(ENABLE_SCRIPTING) + list(APPEND ENABLES SCRIPTING) +endif() + set(TEST_SRC ${CORE_TEST_SRC}) if(M_CORE_GB) add_definitions(-DM_CORE_GB)

@@ -644,6 +650,10 @@ endif()

foreach(FEATURE IN LISTS FEATURES) list(APPEND FEATURE_DEFINES "USE_${FEATURE}") +endforeach() + +foreach(ENABLE IN LISTS ENABLES) + list(APPEND FEATURE_DEFINES "ENABLE_${ENABLE}") endforeach() source_group("Virtual files" FILES ${CORE_VFS_SRC} ${VFS_SRC})
M include/mgba/debugger/debugger.hinclude/mgba/debugger/debugger.h

@@ -30,6 +30,7 @@ enum mDebuggerState {

DEBUGGER_PAUSED, DEBUGGER_RUNNING, DEBUGGER_CUSTOM, + DEBUGGER_SCRIPT, DEBUGGER_SHUTDOWN };

@@ -92,6 +93,7 @@ struct mCPUComponent d;

struct mDebuggerPlatform* platform; enum mDebuggerState state; struct mCore* core; + struct mScriptBridge* bridge; void (*init)(struct mDebugger*); void (*deinit)(struct mDebugger*);
M src/debugger/cli-debugger.csrc/debugger/cli-debugger.c

@@ -12,6 +12,10 @@ #include <mgba/core/version.h>

#include <mgba/internal/debugger/parser.h> #include <mgba-util/string.h> +#if ENABLE_SCRIPTING +#include <mgba/core/scripting.h> +#endif + #if !defined(NDEBUG) && !defined(_WIN32) #include <signal.h> #endif

@@ -51,6 +55,9 @@ static void _writeWord(struct CLIDebugger*, struct CLIDebugVector*);

static void _dumpByte(struct CLIDebugger*, struct CLIDebugVector*); static void _dumpHalfword(struct CLIDebugger*, struct CLIDebugVector*); static void _dumpWord(struct CLIDebugger*, struct CLIDebugVector*); +#ifdef ENABLE_SCRIPTING +static void _source(struct CLIDebugger*, struct CLIDebugVector*); +#endif static struct CLIDebuggerCommandSummary _debuggerCommands[] = { { "b", _setBreakpoint, CLIDVParse, "Set a breakpoint" },

@@ -92,6 +99,9 @@ { "watch/w", _setWriteWatchpoint, CLIDVParse, "Set a write watchpoint" },

{ "x/1", _dumpByte, CLIDVParse, "Examine bytes at a specified offset" }, { "x/2", _dumpHalfword, CLIDVParse, "Examine halfwords at a specified offset" }, { "x/4", _dumpWord, CLIDVParse, "Examine words at a specified offset" }, +#ifdef ENABLE_SCRIPTING + { "source", _source, CLIDVStringParse, "Load a script" }, +#endif #if !defined(NDEBUG) && !defined(_WIN32) { "!", _breakInto, 0, "Break into attached debugger (for developers)" }, #endif

@@ -410,6 +420,20 @@ }

debugger->backend->printf(debugger->backend, "\n"); } } + +#ifdef ENABLE_SCRIPTING +static void _source(struct CLIDebugger* debugger, struct CLIDebugVector* dv) { + if (!dv) { + debugger->backend->printf(debugger->backend, "Needs a filename\n"); + return; + } + if (debugger->d.bridge && mScriptBridgeLoadScript(debugger->d.bridge, dv->charValue)) { + debugger->d.state = DEBUGGER_SCRIPT; + } else { + debugger->backend->printf(debugger->backend, "Failed to load script\n"); + } +} +#endif static void _setBreakpoint(struct CLIDebugger* debugger, struct CLIDebugVector* dv) { if (!dv || dv->type != CLIDV_INT_TYPE) {
M src/debugger/debugger.csrc/debugger/debugger.c

@@ -13,6 +13,10 @@ #ifdef USE_GDB_STUB

#include <mgba/internal/debugger/gdb-stub.h> #endif +#if ENABLE_SCRIPTING +#include <mgba/core/scripting.h> +#endif + const uint32_t DEBUGGER_ID = 0xDEADBEEF; mLOG_DEFINE_CATEGORY(DEBUGGER, "Debugger", "core.debugger");

@@ -34,6 +38,7 @@ #endif

}; union DebugUnion* debugger = malloc(sizeof(union DebugUnion)); + memset(debugger, 0, sizeof(*debugger)); switch (type) { case DEBUGGER_CLI:

@@ -89,6 +94,14 @@ case DEBUGGER_PAUSED:

if (debugger->paused) { debugger->paused(debugger); } else { + debugger->state = DEBUGGER_RUNNING; + } + break; + case DEBUGGER_SCRIPT: +#ifdef ENABLE_SCRIPTING + mScriptBridgeRun(debugger->bridge); +#endif + if (debugger->state == DEBUGGER_SCRIPT) { debugger->state = DEBUGGER_RUNNING; } break;
M src/platform/python/_builder.hsrc/platform/python/_builder.h

@@ -55,3 +55,6 @@ #include <mgba/internal/gb/gb.h>

#include <mgba/internal/gba/input.h> #include <mgba/internal/gb/renderers/tile-cache.h> #endif +#ifdef USE_DEBUGGERS +#include <mgba/debugger/debugger.h> +#endif
M src/platform/python/_builder.pysrc/platform/python/_builder.py

@@ -26,6 +26,7 @@ #include <mgba/core/mem-search.h>

#include <mgba/core/thread.h> #include <mgba/core/tile-cache.h> #include <mgba/core/version.h> +#include <mgba/debugger/debugger.h> #include <mgba/internal/arm/arm.h> #include <mgba/internal/gba/gba.h> #include <mgba/internal/gba/input.h>
M src/platform/sdl/main.csrc/platform/sdl/main.c

@@ -13,6 +13,9 @@ #endif

#ifdef USE_EDITLINE #include "feature/editline/cli-el-backend.h" #endif +#ifdef ENABLE_SCRIPTING +#include <mgba/core/scripting.h> +#endif #include <mgba/core/core.h> #include <mgba/core/config.h>

@@ -159,6 +162,10 @@ if (!mCoreLoadFile(renderer->core, args->fname)) {

return 1; } mCoreAutoloadSave(renderer->core); +#ifdef ENABLE_SCRIPTING + struct mScriptBridge* bridge = mScriptBridgeCreate(); +#endif + #ifdef USE_DEBUGGERS struct mDebugger* debugger = mDebuggerCreate(args->debuggerType, renderer->core); if (debugger) {

@@ -171,6 +178,9 @@ #endif

mDebuggerAttach(debugger, renderer->core); mDebuggerEnter(debugger, DEBUGGER_ENTER_MANUAL, NULL); } +#ifdef ENABLE_SCRIPTING + mScriptBridgeSetDebugger(bridge, debugger); +#endif #endif if (args->patch) {

@@ -212,6 +222,11 @@ } else {

printf("Could not run game. Are you sure the file exists and is a compatible game?\n"); } renderer->core->unloadROM(renderer->core); + +#ifdef ENABLE_SCRIPTING + mScriptBridgeDestroy(bridge); +#endif + return didFail; }