Skip to content

Commit

Permalink
Added experimental scrolling of debug output
Browse files Browse the repository at this point in the history
  • Loading branch information
d0k3 committed Dec 18, 2015
1 parent 525af2e commit da6ce46
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 36 deletions.
45 changes: 20 additions & 25 deletions source/draw.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,8 @@
#include "fs.h"
#ifdef USE_THEME
#include "theme.h"
#else
#define STD_COLOR_BG COLOR_BLACK
#define STD_COLOR_FONT COLOR_WHITE

#define DBG_COLOR_BG COLOR_BLACK
#define DBG_COLOR_FONT COLOR_WHITE

#define DBG_START_Y 10
#define DBG_END_Y (SCREEN_HEIGHT - 10)
#define DBG_START_X 10
#define DBG_END_X (SCREEN_WIDTH_TOP - 10)
#define DBG_STEP_Y 10
#endif

#define DBG_N_CHARS_Y ((DBG_END_Y - DBG_START_Y) / DBG_STEP_Y)
#define DBG_N_CHARS_X (((DBG_END_X - DBG_START_X) / 8) + 1)

static char debugstr[DBG_N_CHARS_X * DBG_N_CHARS_Y] = { 0 };

void ClearScreen(u8* screen, int width, int color)
Expand Down Expand Up @@ -84,11 +69,11 @@ void DrawString(u8* screen, const char *str, int x, int y, int color, int bgcolo

void DrawStringF(int x, int y, bool use_top, const char *format, ...)
{
char str[256] = {};
char str[512] = {}; // 512 should be more than enough
va_list va;

va_start(va, format);
vsnprintf(str, 256, format, va);
vsnprintf(str, 512, format, va);
va_end(va);

if (use_top) {
Expand Down Expand Up @@ -151,6 +136,23 @@ void DebugClear()
LogWrite("");
}

void DebugSet(const char **strs)
{
if (strs != NULL) for (int y = 0; y < DBG_N_CHARS_Y; y++) {
int pos_dbgstr = DBG_N_CHARS_X * (DBG_N_CHARS_Y - 1 - y);
snprintf(debugstr + pos_dbgstr, DBG_N_CHARS_X, "%-*.*s", DBG_N_CHARS_X - 1, DBG_N_CHARS_X - 1, strs[y]);
}

int pos_y = DBG_START_Y;
for (char* str = debugstr + (DBG_N_CHARS_X * (DBG_N_CHARS_Y - 1)); str >= debugstr; str -= DBG_N_CHARS_X) {
if (str[0] != '\0') {
DrawString(TOP_SCREEN0, str, DBG_START_X, pos_y, DBG_COLOR_FONT, DBG_COLOR_BG);
DrawString(TOP_SCREEN1, str, DBG_START_X, pos_y, DBG_COLOR_FONT, DBG_COLOR_BG);
pos_y += DBG_STEP_Y;
}
}
}

void Debug(const char *format, ...)
{
char tempstr[128] = { 0 }; // 128 instead of DBG_N_CHARS_X for log file
Expand All @@ -164,14 +166,7 @@ void Debug(const char *format, ...)
memmove(debugstr + DBG_N_CHARS_X, debugstr, DBG_N_CHARS_X * (DBG_N_CHARS_Y - 1));
snprintf(debugstr, DBG_N_CHARS_X, "%-*.*s", DBG_N_CHARS_X - 1, DBG_N_CHARS_X - 1, tempstr);

int pos_y = DBG_START_Y;
for (char* str = debugstr + (DBG_N_CHARS_X * (DBG_N_CHARS_Y - 1)); str >= debugstr; str -= DBG_N_CHARS_X) {
if (str[0] != '\0') {
DrawString(TOP_SCREEN0, str, DBG_START_X, pos_y, DBG_COLOR_FONT, DBG_COLOR_BG);
DrawString(TOP_SCREEN1, str, DBG_START_X, pos_y, DBG_COLOR_FONT, DBG_COLOR_BG);
pos_y += DBG_STEP_Y;
}
}
DebugSet(NULL);
}

void ShowProgress(u64 current, u64 total)
Expand Down
18 changes: 18 additions & 0 deletions source/draw.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,23 @@
#define COLOR_PURPLE RGB(0x66, 0x00, 0xFF)
#define COLOR_TRANSPARENT RGB(0xFF, 0x00, 0xEF) // otherwise known as 'super fuchsia'

#ifndef USE_THEME
#define STD_COLOR_BG COLOR_BLACK
#define STD_COLOR_FONT COLOR_WHITE

#define DBG_COLOR_BG COLOR_BLACK
#define DBG_COLOR_FONT COLOR_WHITE

#define DBG_START_Y 10
#define DBG_END_Y (SCREEN_HEIGHT - 10)
#define DBG_START_X 10
#define DBG_END_X (SCREEN_WIDTH_TOP - 10)
#define DBG_STEP_Y 10
#endif

#define DBG_N_CHARS_Y ((DBG_END_Y - DBG_START_Y) / DBG_STEP_Y)
#define DBG_N_CHARS_X (((DBG_END_X - DBG_START_X) / 8) + 1)

#ifdef EXEC_GATEWAY
#define TOP_SCREEN0 (u8*)(*(u32*)((uint32_t)0x080FFFC0 + 4 * (*(u32*)0x080FFFD8 & 1)))
#define BOT_SCREEN0 (u8*)(*(u32*)0x080FFFD4)
Expand All @@ -45,6 +62,7 @@ void DrawStringF(int x, int y, bool use_top, const char *format, ...);

void Screenshot(const char* path);
void DebugClear();
void DebugSet(const char **strs);
void Debug(const char *format, ...);

void ShowProgress(u64 current, u64 total);
20 changes: 12 additions & 8 deletions source/fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -223,17 +223,20 @@ bool GetFileList(const char* path, char* list, int lsize, bool recursive)
return GetFileListWorker(&list, &lsize, fpath, 256, recursive);
}

bool LogWrite(const char* text)
size_t LogWrite(const char* text)
{
#ifdef LOG_FILE
static FIL lfile;
static bool lready = false;
static size_t lstart = 0;

if (text == NULL) {
if ((text == NULL) && lready) {
f_sync(&lfile);
f_close(&lfile);
lready = false;
return true;
return lstart; // return the log start
} else if (text == NULL) {
return 0;
}

if (!lready) {
Expand All @@ -246,21 +249,22 @@ bool LogWrite(const char* text)
#else
lready = (f_open(&lfile, LOG_FILE, flags) == FR_OK);
#endif
if (!lready) return false;
f_lseek(&lfile, f_size(&lfile));
if (!lready) return 0;
lstart = f_size(&lfile);
f_lseek(&lfile, lstart);
f_sync(&lfile);
}

const char newline = '\n';
UINT bytes_written;
UINT tlen = strnlen(text, 128);
f_write(&lfile, text, tlen, &bytes_written);
if (bytes_written != tlen) return false;
if (bytes_written != tlen) return 0;
f_write(&lfile, &newline, 1, &bytes_written);
if (bytes_written != 1) return false;
if (bytes_written != 1) return 0;
#endif

return true;
return f_size(&lfile); // return the current position
}

static uint64_t ClustersToBytes(FATFS* fs, DWORD clusters)
Expand Down
2 changes: 1 addition & 1 deletion source/fs.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ bool DirRead(char* fname, int fsize);
bool GetFileList(const char* path, char* list, int lsize, bool recursive);

/** Writes text to a constantly open log file **/
bool LogWrite(const char* text);
size_t LogWrite(const char* text);

/** Gets remaining space on SD card in bytes */
uint64_t RemainingStorageSpace();
Expand Down
69 changes: 67 additions & 2 deletions source/menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,63 @@
#include "decryptor/nand.h"


#ifdef LOG_FILE
u32 ScrollOutput()
{
u32 log_start = LogWrite(NULL);

// careful, these areas are used by other functions in Decrypt9
char** logptr = (char**) 0x20316000;
char* logtext = (char*) 0x20400000;
u32 log_size = 0; // log size
u32 l_total = 0; // total lines
u32 l_curr = 0; // current line

if (!FileOpen(LOG_FILE))
return 0;
log_size = FileGetSize();
if ((log_size <= log_start) || (log_size - log_start >= 1024 * 1024)) {
FileClose();
return 0; // allow 1MB of text max
}
log_size -= log_start;
if (!FileRead(logtext, log_size, log_start)) {
FileClose();
return 0;
}
FileClose();

// read lines
logtext[log_size - 1] = '\0';
for (char* line = logtext; line != NULL && l_total < 4000; line = strchr(line, '\n')) {
*line = '\0';
logptr[l_total++] = ++line;
}
if (l_total >= 4000) // allow 4000 lines of text max
return 0;
for (; l_total < DBG_N_CHARS_Y; logptr[l_total++] = logtext + log_size - 1);

// here, the actual output starts
l_curr = l_total - DBG_N_CHARS_Y ;
if (l_curr > 0) l_curr--; // start at the line before the last
while (true) {
DebugSet((const char**) logptr + l_curr);
u32 pad_state = InputWait();
if (pad_state & BUTTON_X) {
Screenshot(NULL);
} else if ((pad_state & BUTTON_UP) && (l_curr > 0)) {
l_curr--;
} else if ((pad_state & BUTTON_DOWN) && (l_curr < l_total - DBG_N_CHARS_Y)) {
l_curr++;
} else if (pad_state & (BUTTON_B | BUTTON_START)) {
return pad_state;
}
}

return 0;
}
#endif

u32 UnmountSd()
{
u32 pad_state;
Expand Down Expand Up @@ -48,7 +105,7 @@ void DrawMenu(MenuInfo* currMenu, u32 index, bool fullDraw, bool subMenu)
DrawStringF(menublock_x0, menublock_y1 + 10, top_screen, (subMenu) ? "A: Choose B: Return" : "A: Choose");
DrawStringF(menublock_x0, menublock_y1 + 20, top_screen, "SELECT: Unmount SD");
DrawStringF(menublock_x0, menublock_y1 + 30, top_screen, "START: Reboot");
DrawStringF(menublock_x1, SCREEN_HEIGHT - 20, top_screen, "SD card: %lluMB/%lluMB - %s", RemainingStorageSpace() / 1024 / 1024, TotalStorageSpace() / 1024 / 1024, (emunand_state == EMUNAND_READY) ? "EmuNAND ready" : (emunand_state == EMUNAND_GATEWAY) ? "GW EmuNAND" : (emunand_state == EMUNAND_REDNAND) ? "RedNAND" : "no EmuNAND");
DrawStringF(menublock_x1, SCREEN_HEIGHT - 20, top_screen, "SD card: %lluMB/%lluMB & %s", RemainingStorageSpace() / 1024 / 1024, TotalStorageSpace() / 1024 / 1024, (emunand_state == EMUNAND_READY) ? "EmuNAND ready" : (emunand_state == EMUNAND_GATEWAY) ? "GW EmuNAND" : (emunand_state == EMUNAND_REDNAND) ? "RedNAND" : "no EmuNAND");
DrawStringF(menublock_x1, SCREEN_HEIGHT - 30, top_screen, "Game directory: %s", GAME_DIR);
#ifdef WORK_DIR
if (DirOpen(WORK_DIR)) {
Expand Down Expand Up @@ -126,7 +183,15 @@ u32 ProcessEntry(MenuEntry* entry)
#ifdef USE_THEME
LoadThemeGfx((res == 0) ? GFX_DONE : GFX_FAILED, false);
#endif
while(!(pad_state = InputWait() & (BUTTON_B | BUTTON_START)));
while(!((pad_state = InputWait()) & (BUTTON_B | BUTTON_START))) {
if (pad_state & BUTTON_X) Screenshot(NULL);
#ifdef LOG_FILE
else if (pad_state & BUTTON_UP) {
pad_state = ScrollOutput();
break;
}
#endif
}

// returns the last known pad_state
return pad_state;
Expand Down

0 comments on commit da6ce46

Please sign in to comment.