Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #417 #611

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 53 additions & 6 deletions runtimes/native/src/runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <stdio.h>
#include <string.h>
#include <assert.h>

#include "apu.h"
#include "framebuffer.h"
Expand All @@ -13,6 +14,7 @@
#define HEIGHT 160

#define SYSTEM_PRESERVE_FRAMEBUFFER 1
#define SYSTEM_STANDARD_TRACING 4

typedef struct {
uint8_t _padding[4];
Expand Down Expand Up @@ -55,7 +57,7 @@ void w4_runtimeInit (uint8_t* memoryBytes, w4_Disk* diskBytes) {
w4_write16LE(&memory->mouseY, 0x7fff);

w4_apuInit();
w4_framebufferInit(&memory->drawColors, memory->framebuffer);
w4_framebufferInit(memory->drawColors, memory->framebuffer);
}

void w4_runtimeSetGamepad (int idx, uint8_t gamepad) {
Expand Down Expand Up @@ -166,12 +168,57 @@ void w4_runtimeTraceUtf16 (const uint16_t* str, int byteLength) {
printf("TODO: traceUtf16: %p, %d\n", str, byteLength);
}

void w4_runtimeTracef (const uint8_t* str, const void* stack) {
puts(str);
static unsigned align(unsigned v, unsigned a) {
return (v + a - 1) & ~(a - 1);
}

// This seems to crash on Linux release builds
// vprintf(str, (void*)&stack);
// putchar('\n');
void w4_runtimeTracef (const uint8_t* fmt, const uint8_t * stk) {
char out[256];
char buf[256];
size_t sofar = 0;
unsigned si = 0;
char * ctx = buf;
char * s;
snprintf(buf, sizeof(buf), "%s", fmt);
s = strsep(&ctx, "%");
while (s) {
char fb[32];
char fc;
char * fm = strsep(&ctx, "cdxfs");
if (!fm) {
sofar += snprintf(out+sofar, sizeof(out)-sofar, "%s", s);
break;
}
fc = fmt[(fm+strlen(fm)) - buf];
snprintf(fb, sizeof(fb), "%s%%%s%c", s, fm, fc);
switch (fc) {
case 0:
sofar += snprintf(out+sofar, sizeof(out)-sofar, fb);
break;
case 'c':
case 'd':
case 'x':
sofar += snprintf(out+sofar, sizeof(out)-sofar, fb, *(int32_t*)(stk+si));
si += 4;
break;
case 'f':
si = align(si, 8);
sofar += snprintf(out+sofar, sizeof(out)-sofar, fb, *(double*)(stk+si));
si += 8;
break;
case 's':
sofar += snprintf(out+sofar, sizeof(out)-sofar, fb, ((uint8_t*)memory)+*(int32_t*)(stk+si));
si += 4;
break;
default:
assert(0);
}
s = strsep(&ctx, "%");
}
if (memory->systemFlags & SYSTEM_STANDARD_TRACING)
fputs(out, stdout);
else
puts(out);
}

void w4_runtimeUpdate () {
Expand Down
2 changes: 1 addition & 1 deletion runtimes/native/src/runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ int w4_runtimeDiskw (const uint8_t* src, int size);
void w4_runtimeTrace (const uint8_t* str);
void w4_runtimeTraceUtf8 (const uint8_t* str, int byteLength);
void w4_runtimeTraceUtf16 (const uint16_t* str, int byteLength);
void w4_runtimeTracef (const uint8_t* str, const void* stack);
void w4_runtimeTracef (const uint8_t* str, const uint8_t* stack);

void w4_runtimeUpdate ();

Expand Down