Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Oxore/e80ab980adb449a608bd32eb8c81a9c1 to your computer and use it in GitHub Desktop.
Save Oxore/e80ab980adb449a608bd32eb8c81a9c1 to your computer and use it in GitHub Desktop.
A patch for Blastem emulator to obtain all values that program counter had through the game that you were playing. Very handy for disassembling Sega Mega Drive / Genesis games. https://github.com/libretro/blastem, commit 277e4a62668597d4f59cadda1cbafb844f981d45.
diff --git a/bindings.c b/bindings.c
index 2921aa8..bdacf0e 100644
--- a/bindings.c
+++ b/bindings.c
@@ -292,6 +292,40 @@ https://github.com/libretro/blastem, commit 277e4a62668597d4f59cadda1cbafb844f981d45
return path;
}
+#define ROM_SIZE ((size_t)(4*1024*1024))
+static unsigned char pc_map[ROM_SIZE] = {0};
+
+#include <assert.h>
+#include <errno.h>
+
+void trace_pc(uint32_t pc)
+{
+ assert(pc < ROM_SIZE);
+ pc_map[pc] = 1;
+}
+
+static void dump_pc(void)
+{
+ const char *file_name = "/tmp/trace.txt";
+ FILE *out = fopen(file_name, "w");
+ if (out == NULL) {
+ const int err = errno;
+ fprintf(
+ stderr,
+ "dump_pc: fopen(\"%s\", \"r\"): Error (%d): \"%s\"\n",
+ file_name,
+ err,
+ strerror(err));
+ return;
+ }
+ for (size_t i = 0; i < ROM_SIZE; i++) {
+ if (pc_map[i]) {
+ fprintf(out, "%zu\n", i);
+ }
+ }
+ fclose(out);
+}
+
void handle_binding_up(keybinding * binding)
{
uint8_t allow_content_binds = content_binds_enabled && current_system;
@@ -324,6 +358,7 @@ void handle_binding_up(keybinding * binding)
if (allow_content_binds) {
current_system->save_state = QUICK_SAVE_SLOT+1;
}
+ dump_pc();
break;
case UI_NEXT_SPEED:
if (allow_content_binds) {
diff --git a/m68k_core.c b/m68k_core.c
index cd975cb..66b2a99 100644
--- a/m68k_core.c
+++ b/m68k_core.c
@@ -1021,6 +1021,8 @@ void translate_m68k_stream(uint32_t address, m68k_context * context)
fflush(opts->address_log);
}
do {
+ extern void trace_pc(uint32_t);
+ trace_pc(address);
encoded = get_native_pointer(address, (void **)context->mem_pointers, &opts->gen);
if (!encoded) {
code_ptr start = code->cur;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment