Skip to content

Commit

Permalink
Improve logging
Browse files Browse the repository at this point in the history
  • Loading branch information
socram8888 committed May 28, 2021
1 parent 564403f commit 5ed7956
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 27 deletions.
5 changes: 5 additions & 0 deletions loader/bios-asm.S
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ strncmp:
li t1, 0x18
j 0xA0

.global strcpy
strcpy:
li t1, 0x19
j 0xA0

.global strlen
strlen:
li t1, 0x1B
Expand Down
2 changes: 1 addition & 1 deletion loader/bios.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ handler_info_t * bios_get_syscall_handler(void) {
}

void logging_disc_error_handler(char type, int errorcode) {
debug_write("Disc error type %c code %x", type, errorcode);
debug_write("Disc error type %c code %d", type, errorcode);
}

void bios_inject_disc_error(void) {
Expand Down
56 changes: 38 additions & 18 deletions loader/debugscreen.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

#include <string.h>
#include "debugscreen.h"
#include "gpu.h"
#include "bios.h"
Expand Down Expand Up @@ -76,6 +77,9 @@ void decompressfont() {
}
}

char last_printed_line[64];
uint32_t last_printed_count;

void debug_init() {
bool pal = bios_is_european();
debug_switch_standard(pal);
Expand Down Expand Up @@ -197,29 +201,45 @@ void debug_write(const char * str, ...) {
va_list args;
va_start(args, str);

// For a render width of 640 this should be just 40 but let's be generous
char formatted[128];

// For a render width of 640 at width 10 max line should be 64 chars long
char formatted[64];
char formatted_repeated[64];
char * to_print;
mini_vsprintf(formatted, str, args);

// Flush old textures
gpu_flush_cache();

// Scroll text up
gpu_size_t line_size = {
.width = SCREEN_WIDTH - LOG_MARGIN,
.height = LOG_LINE_HEIGHT,
};
for (int line = 1; line < LOG_LINES; line++) {
gpu_point_t source_line = {
.x = LOG_MARGIN,
.y = LOG_START_Y + LOG_LINE_HEIGHT * line
};
gpu_point_t dest_line = {
.x = LOG_MARGIN,
.y = LOG_START_Y + LOG_LINE_HEIGHT * (line - 1)
if (strcmp(last_printed_line, formatted) != 0) {
// Line's text is different, so scroll up
gpu_size_t line_size = {
.width = SCREEN_WIDTH - LOG_MARGIN,
.height = LOG_LINE_HEIGHT,
};
gpu_copy_rectangle(&source_line, &dest_line, &line_size);
for (int line = 1; line < LOG_LINES; line++) {
gpu_point_t source_line = {
.x = LOG_MARGIN,
.y = LOG_START_Y + LOG_LINE_HEIGHT * line
};
gpu_point_t dest_line = {
.x = LOG_MARGIN,
.y = LOG_START_Y + LOG_LINE_HEIGHT * (line - 1)
};
gpu_copy_rectangle(&source_line, &dest_line, &line_size);
}

// Copy to last printed buffer and reset counter
strcpy(last_printed_line, formatted);
last_printed_count = 1;

to_print = formatted;
} else {
last_printed_count++;

// Same line, so print with a repeat counter
mini_sprintf(formatted_repeated, "%s (x%d)", last_printed_line, last_printed_count);

to_print = formatted_repeated;
}

uint32_t lastLinePos = LOG_START_Y + (LOG_LINES - 1) * LOG_LINE_HEIGHT;
Expand All @@ -240,7 +260,7 @@ void debug_write(const char * str, ...) {
gpu_draw_solid_rect(&black_box);

// Draw text on last line
debug_text_at(LOG_MARGIN, lastLinePos, formatted);
debug_text_at(LOG_MARGIN, lastLinePos, to_print);
}

void debug_switch_standard(bool pal) {
Expand Down
12 changes: 6 additions & 6 deletions loader/secondary.c
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ void try_boot_cd() {
FileClose(cnf_fd);

if (read == -1) {
debug_write("Read error %x", GetLastError());
debug_write("Read error %d", GetLastError());
return;
}

Expand All @@ -220,7 +220,7 @@ void try_boot_cd() {
} else {
uint32_t errorCode = GetLastError();
if (errorCode != FILEERR_NOT_FOUND) {
debug_write("Open error %x", errorCode);
debug_write("Open error %d", errorCode);
return;
}

Expand All @@ -243,14 +243,14 @@ void try_boot_cd() {
debug_write("Checking executable");
int32_t exe_fd = FileOpen(bootfile, FILE_READ);
if (exe_fd <= 0) {
debug_write("Open error %x", GetLastError());
debug_write("Open error %d", GetLastError());
return;
}

read = FileRead(exe_fd, data_buffer, 2048);

if (read != 2048) {
debug_write("Read error %x", GetLastError());
debug_write("Read error %d", GetLastError());
return;
}

Expand All @@ -270,10 +270,10 @@ void try_boot_cd() {
return;
}

debug_write("Loading executable (%x @ %x)", exe_header->load_size, exe_header->load_addr);
debug_write("Loading executable (%d bytes @ %x)", exe_header->load_size, exe_header->load_addr);

if (FileRead(exe_fd, exe_header->load_addr, exe_header->load_size) != (int32_t) exe_header->load_size) {
debug_write("Read error %x", GetLastError());
debug_write("Read error %d", GetLastError());
return;
}

Expand Down
40 changes: 38 additions & 2 deletions loader/str.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ int isspace(int c) {
}
}

int mini_sprintf(char * str, const char * format, ...) {
va_list args;
va_start(args, format);
return mini_vsprintf(str, format, args);
}

int mini_vsprintf(char * str, const char * format, va_list args) {
char * pos = str;

Expand Down Expand Up @@ -71,8 +77,38 @@ int mini_vsprintf(char * str, const char * format, va_list args) {
break;
}

case 'x':
case 'X': {
case 'd': {
int val = va_arg(args, int);

if (val == 0) {
*pos = '0';
pos++;
} else {
if (val < 0) {
*pos = '-';
pos++;
}

// Calculate digits backwards
char digits[10];
int digpos = 0;
do {
digits[digpos] = '0' + val % 10;
val = val / 10;
digpos++;
} while (val != 0);

// And copy them now also backwards
while (digpos != 0) {
digpos--;
*pos = digits[digpos];
pos++;
}
}
break;
}

case 'x': {
char c;
uint32_t val = va_arg(args, uint32_t);
for (int i = 28; i >= 0; i -= 4) {
Expand Down
2 changes: 2 additions & 0 deletions loader/str.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

int isspace(int c);

int mini_sprintf(char * str, const char * format, ...);

int mini_vsprintf(char * str, const char * format, va_list args);

int memcmp(const void * ptr1, const void * ptr2, uint32_t num);
Expand Down

0 comments on commit 5ed7956

Please sign in to comment.