Skip to content

Commit

Permalink
improve debug info dumping
Browse files Browse the repository at this point in the history
  • Loading branch information
skyfloogle committed Mar 18, 2024
1 parent eda2369 commit 4698009
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 6 deletions.
2 changes: 1 addition & 1 deletion include/drc_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,6 @@ void drc_exit();
int drc_run();
void drc_loadSavedCache();
void drc_dumpCache(char* filename);
void drc_dumpDebugInfo();
void drc_dumpDebugInfo(int code);

#endif //DRC_CORE_H
5 changes: 5 additions & 0 deletions include/replay.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include "vb_types.h"

void replay_init(bool has_sram);
void replay_update(HWORD inputs);
void replay_save(char *fn);
33 changes: 29 additions & 4 deletions source/3ds/gui_hard.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ static C2D_TextBuf dynamic_textbuf;

static C2D_Text text_A, text_B, text_btn_A, text_btn_B, text_btn_X, text_btn_L, text_btn_R,
text_switch, text_saving, text_on, text_off, text_toggle, text_hold, text_3ds,
text_vbipd, text_left, text_right, text_sound_error, text_anykeyexit, text_about;
text_vbipd, text_left, text_right, text_sound_error, text_anykeyexit, text_about,
text_debug_filenames;

static C2D_SpriteSheet sprite_sheet;
static C2D_Sprite colour_wheel_sprite, logo_sprite;
Expand Down Expand Up @@ -167,6 +168,8 @@ static Button options_buttons[] = {
{.str="About", .x=176, .y=144, .w=128, .h=48},
#define OPTIONS_BACK 5
{.str="Back", .x=0, .y=208, .w=48, .h=32},
#define OPTIONS_DEBUG 6
{.str="Save debug info", .x=170, .y=208, .w=150, .h=32},
};

static void video_settings(int initial_button);
Expand Down Expand Up @@ -901,6 +904,7 @@ static void colour_filter() {
}
}

static void save_debug_info();
static void options(int initial_button) {
options_buttons[OPTIONS_FF].toggle = tVBOpt.FF_TOGGLE;
options_buttons[OPTIONS_SOUND].toggle = tVBOpt.SOUND;
Expand All @@ -927,6 +931,8 @@ static void options(int initial_button) {
return about();
case OPTIONS_BACK: // Back
return main_menu(MAIN_MENU_OPTIONS);
case OPTIONS_DEBUG: // Save debug info
return save_debug_info();
}
}

Expand Down Expand Up @@ -1200,6 +1206,8 @@ void guiInit() {
C2D_TextOptimize(&text_right);
C2D_TextParse(&text_sound_error, static_textbuf, "Error: couldn't initialize audio.\nDid you dump your DSP firmware?");
C2D_TextOptimize(&text_sound_error);
C2D_TextParse(&text_debug_filenames, static_textbuf, "Please share debug_info.txt and\ndebug_replay.bin in your bug report.");
C2D_TextOptimize(&text_debug_filenames);
C2D_TextParse(&text_anykeyexit, static_textbuf, "Press any key to exit");
C2D_TextOptimize(&text_anykeyexit);
C2D_TextParse(&text_about, static_textbuf, VERSION "\nBy Floogle, danielps, & others\nHeavily based on Reality Boy by David Tucker\nMore info at:\ngithub.com/skyfloogle/red-viper");
Expand Down Expand Up @@ -1251,6 +1259,22 @@ void showSoundError() {
sound_error();
}

static void save_debug_info() {
C3D_FrameBegin(0);
C2D_TargetClear(screen, 0);
C2D_SceneBegin(screen);
C2D_DrawText(&text_saving, C2D_AlignCenter | C2D_WithColor, 320 / 2, 100, 0, 0.7, 0.7, C2D_Color32(TINT_R, TINT_G, TINT_B, 255));
C2D_Flush();
C3D_FrameEnd(0);

drc_dumpDebugInfo(0);

LOOP_BEGIN(about_buttons, 0);
C2D_DrawText(&text_debug_filenames, C2D_AlignCenter | C2D_WithColor, 320 / 2, 80, 0, 0.7, 0.7, C2D_Color32(TINT_R, TINT_G, TINT_B, 255));
LOOP_END(about_buttons);
return options(OPTIONS_DEBUG);
}

void showError(int code) {
if (!backlightEnabled) toggleBacklight(true);
gfxSetDoubleBuffering(GFX_BOTTOM, false);
Expand All @@ -1265,15 +1289,16 @@ void showError(int code) {
C3D_FrameBegin(0);
C2D_TargetClear(screen, 0);
C2D_SceneBegin(screen);
C2D_DrawText(&text, C2D_AlignCenter | C2D_WithColor, 320 / 2, 60, 0, 0.7, 0.7, C2D_Color32(TINT_R, TINT_G, TINT_B, 255));
C2D_DrawText(&text, C2D_AlignCenter | C2D_WithColor, 320 / 2, 40, 0, 0.7, 0.7, C2D_Color32(TINT_R, TINT_G, TINT_B, 255));
C2D_Flush();
C3D_FrameEnd(0);

drc_dumpDebugInfo();
drc_dumpDebugInfo(code);

C3D_FrameBegin(0);
C3D_FrameDrawOn(screen);
C2D_DrawText(&text_anykeyexit, C2D_AlignCenter | C2D_WithColor, 320 / 2, 140, 0, 0.7, 0.7, C2D_Color32(TINT_R, TINT_G, TINT_B, 255));
C2D_DrawText(&text_debug_filenames, C2D_AlignCenter | C2D_WithColor, 320 / 2, 120, 0, 0.7, 0.7, C2D_Color32(TINT_R, TINT_G, TINT_B, 255));
C2D_DrawText(&text_anykeyexit, C2D_AlignCenter | C2D_WithColor, 320 / 2, 180, 0, 0.7, 0.7, C2D_Color32(TINT_R, TINT_G, TINT_B, 255));
C2D_Flush();
C3D_FrameEnd(0);
}
Expand Down
3 changes: 3 additions & 0 deletions source/3ds/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "vb_sound.h"
#include "vb_gui.h"
#include "rom_db.h"
#include "replay.h"

char rom_path[256] = "sdmc:/vb/";
char rom_name[128];
Expand Down Expand Up @@ -135,6 +136,8 @@ int main() {
tHReg.SLB =(BYTE)(inputs&0xFF);
tHReg.SHB =(BYTE)((inputs>>8)&0xFF);

replay_update(inputs);

float last_drc_time = osTickCounterRead(&drcTickCounter);

#if DEBUGLEVEL == 0
Expand Down
61 changes: 61 additions & 0 deletions source/3ds/replay.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include <stdio.h>
#include "v810_mem.h"
#include "vb_set.h"
#include "vb_types.h"

typedef struct {
HWORD inputs;
HWORD count;
} ReplayEntry;

#define REPLAY_COUNT 200000

static const int version = 0;
static BYTE *initial_sram = NULL;
static int initial_sram_size;
static ReplayEntry *replay_buf = NULL, *replay_cursor;
static bool overflowed = false;

void replay_init(bool has_sram) {
if (initial_sram) free(initial_sram);
if (has_sram) {
initial_sram_size = V810_GAME_RAM.highaddr + 1 - V810_GAME_RAM.lowaddr;
initial_sram = malloc(initial_sram_size);
memcpy(initial_sram, V810_GAME_RAM.pmemory, initial_sram_size);
} else {
initial_sram_size = 0;
initial_sram = NULL;
}
if (!replay_buf) replay_buf = malloc(sizeof(ReplayEntry) * REPLAY_COUNT);
replay_cursor = replay_buf;
replay_cursor->inputs = 0;
replay_cursor->count = 0;
overflowed = false;
}

void replay_update(HWORD inputs) {
if (overflowed) return;
if (inputs != replay_cursor->inputs) {
if (replay_cursor->count != 0) replay_cursor++;
if (replay_cursor >= replay_buf + REPLAY_COUNT) {
overflowed = true;
return;
}
replay_cursor->inputs = inputs;
}
replay_cursor->count++;
}

void replay_save(char *fn) {
FILE *f = fopen(fn, "wb");
if (!f) return;
fwrite("RVRP", 4, 1, f);
fwrite(&version, 4, 1, f);
fwrite(&tVBOpt.CRC32, 4, 1, f);
fwrite(&initial_sram_size, 4, 1, f);
if (initial_sram_size) {
fwrite(initial_sram, 1, initial_sram_size, f);
}
fwrite(replay_buf, sizeof(ReplayEntry), replay_cursor + 1 - replay_buf, f);
fclose(f);
}
7 changes: 6 additions & 1 deletion source/common/drc_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
#include "arm_emit.h"
#include "arm_codegen.h"

#include "replay.h"

WORD* cache_start;
WORD* cache_pos;
int block_pos = 1;
Expand Down Expand Up @@ -1825,10 +1827,11 @@ void drc_dumpCache(char* filename) {
fclose(f);
}

void drc_dumpDebugInfo() {
void drc_dumpDebugInfo(int code) {
int i;
FILE* f = fopen("debug_info.txt", "w");

fprintf(f, "Error code: %d\n", code);
fprintf(f, "PC: 0x%08lx\n", v810_state->PC);
for (i = 0; i < 32; i++)
fprintf(f, "r%d: 0x%08lx\n", i, v810_state->P_REG[i]);
Expand All @@ -1845,5 +1848,7 @@ void drc_dumpDebugInfo() {
debug_dumpvbram();
}

replay_save("debug_replay.bin");

fclose(f);
}
4 changes: 4 additions & 0 deletions source/common/v810_cpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#include "rom_db.h"
#include "drc_core.h"

#include "replay.h"

#include "3ds/services/fs.h"

#define NEG(n) ((n) >> 31)
Expand Down Expand Up @@ -166,6 +168,8 @@ int v810_init() {
fread(V810_GAME_RAM.pmemory, 1, ((V810_GAME_RAM.highaddr + 1) - V810_GAME_RAM.lowaddr), f);
fclose(f);
}

replay_init((bool)f);

// If we need to save, we'll find out later
is_sram = false;
Expand Down

0 comments on commit 4698009

Please sign in to comment.