Skip to content

Instantly share code, notes, and snippets.

@bigfarts
Created June 11, 2022 01:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bigfarts/edc75b8c207cbf76c98e336cfa2948ef to your computer and use it in GitHub Desktop.
Save bigfarts/edc75b8c207cbf76c98e336cfa2948ef to your computer and use it in GitHub Desktop.
$ git diff
diff --git a/src/arm/arm.c b/src/arm/arm.c
index 37dfe726c..89ad3d900 100644
--- a/src/arm/arm.c
+++ b/src/arm/arm.c
@@ -4,6 +4,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include <mgba/internal/arm/arm.h>
+#include <mgba-util/common.h>
#include <mgba/internal/arm/isa-arm.h>
#include <mgba/internal/arm/isa-inlines.h>
@@ -216,8 +217,31 @@ static inline void ARMStep(struct ARMCore* cpu) {
instruction(cpu, opcode);
}
+static struct ARMCore* g_bound_cpu;
+static bool g_is_tracing = false;
+
static inline void ThumbStep(struct ARMCore* cpu) {
+ struct ARMCore* bound_cpu = NULL;
+ if (ATOMIC_CMPXCHG(g_bound_cpu, bound_cpu, cpu)) {
+ fprintf(stderr, "bound cpu @ %p\n", cpu);
+ bound_cpu = cpu;
+ }
+
uint32_t opcode = cpu->prefetch[0];
+ if (bound_cpu == cpu) {
+ if (cpu->gprs[ARM_PC] == 0x08006440) {
+ ATOMIC_STORE(g_is_tracing, true);
+ } else if (cpu->gprs[ARM_PC] == 0x08006442) {
+ ATOMIC_STORE(g_is_tracing, false);
+ fprintf(stderr, "trace end\n");
+ }
+ bool is_tracing;
+ ATOMIC_LOAD(is_tracing, g_is_tracing);
+ if (is_tracing) {
+ fprintf(stderr, "trace: pc = %08x, lr = %08x, opcode = %04x\n", cpu->gprs[ARM_PC], cpu->gprs[14], opcode);
+ }
+ }
+
cpu->prefetch[0] = cpu->prefetch[1];
cpu->gprs[ARM_PC] += WORD_SIZE_THUMB;
LOAD_16(cpu->prefetch[1], cpu->gprs[ARM_PC] & cpu->memory.activeMask, cpu->memory.activeRegion);
diff --git a/src/debugger/debugger.c b/src/debugger/debugger.c
index cc6beab9c..da160bb9d 100644
--- a/src/debugger/debugger.c
+++ b/src/debugger/debugger.c
@@ -9,6 +9,7 @@
#include <mgba/internal/debugger/cli-debugger.h>
#include <mgba/internal/debugger/symbols.h>
+#include <mgba/internal/arm/debugger/debugger.h>
#ifdef USE_GDB_STUB
#include <mgba/internal/debugger/gdb-stub.h>
@@ -67,6 +68,8 @@ struct mDebugger* mDebuggerCreate(enum mDebuggerType type, struct mCore* core) {
return &debugger->d;
}
+void ARMRunFake(struct ARMCore* cpu, uint32_t opcode);
+
void mDebuggerAttach(struct mDebugger* debugger, struct mCore* core) {
debugger->d.id = DEBUGGER_ID;
debugger->d.init = mDebuggerInit;
diff --git a/src/gba/gba.c b/src/gba/gba.c
index de79671c5..3b272ecde 100644
--- a/src/gba/gba.c
+++ b/src/gba/gba.c
@@ -813,6 +813,7 @@ void GBAIllegal(struct ARMCore* cpu, uint32_t opcode) {
void GBABreakpoint(struct ARMCore* cpu, int immediate) {
struct GBA* gba = (struct GBA*) cpu->master;
+ if (immediate == 0xef) { ARMRunFake(cpu, 0x1c04); }
if (immediate >= CPU_COMPONENT_MAX) {
return;
}
@@ -968,6 +969,7 @@ void GBASetBreakpoint(struct GBA* gba, struct mCPUComponent* component, uint32_t
value |= immediate & 0xFF;
GBAPatch16(gba->cpu, address, value, &old);
*opcode = (uint16_t) old;
+ fprintf(stderr, "trace: set breakpoint at %08x, old = %04x, new = %04x\n", address, *opcode, value);
}
}

replace 0x0000643E in MEGA_EXE3_BLA3XE with EF BE (should be 04 1C originally)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment