Removed stdio dependency from uxn.c

Reimplementation of patch sent in by Marc Schraffenberger
<marc@schraffenberger.com>: thank you for the rationale and inspiration!
This commit is contained in:
Andrew Alderwick 2021-08-01 21:35:46 +01:00
parent b53add0ba4
commit 6e21f3aba0
5 changed files with 45 additions and 38 deletions

View File

@ -1,4 +1,3 @@
#include <stdio.h>
#include "uxn.h"
/*
@ -35,18 +34,6 @@ Uint16 mempeek16(Uint8 *m, Uint16 a) { return (mempeek8(m, a) << 8) + mempeek8(m
void devpoke16(Device *d, Uint8 a, Uint16 b) { devpoke8(d, a, b >> 8); devpoke8(d, a + 1, b); }
Uint16 devpeek16(Device *d, Uint16 a) { return (devpeek8(d, a) << 8) + devpeek8(d, a + 1); }
#ifndef NO_STACK_CHECKS
static const char *errors[] = {"underflow", "overflow", "division by zero"};
int
haltuxn(Uxn *u, Uint8 error, char *name, int id)
{
fprintf(stderr, "Halted: %s %s#%04x, at 0x%04x\n", name, errors[error - 1], id, u->ram.ptr);
u->ram.ptr = 0;
return 0;
}
#endif
/* clang-format on */
#pragma mark - Core

View File

@ -1,4 +1,3 @@
#include <stdio.h>
#include "uxn.h"
/*
@ -117,16 +116,6 @@ void (*ops[])(Uxn *u) = {
#pragma mark - Core
static const char *errors[] = {"underflow", "overflow", "division by zero"};
int
haltuxn(Uxn *u, Uint8 error, char *name, int id)
{
fprintf(stderr, "Halted: %s %s#%04x, at 0x%04x\n", name, errors[error - 1], id, u->ram.ptr);
u->ram.ptr = 0;
return 0;
}
void
opcuxn(Uxn *u, Uint8 instr)
{
@ -171,24 +160,13 @@ evaluxn(Uxn *u, Uint16 vec)
int
bootuxn(Uxn *u)
{
size_t i;
unsigned int i;
char *cptr = (char *)u;
for(i = 0; i < sizeof(*u); i++)
cptr[i] = 0;
return 1;
}
int
loaduxn(Uxn *u, char *filepath)
{
FILE *f;
if(!(f = fopen(filepath, "rb")))
return 0;
fread(u->ram.dat + PAGE_PROGRAM, sizeof(u->ram.dat) - PAGE_PROGRAM, 1, f);
fprintf(stderr, "Uxn loaded[%s].\n", filepath);
return 1;
}
Device *
portuxn(Uxn *u, Uint8 id, char *name, void (*talkfn)(Device *d, Uint8 b0, Uint8 w))
{
@ -197,6 +175,6 @@ portuxn(Uxn *u, Uint8 id, char *name, void (*talkfn)(Device *d, Uint8 b0, Uint8
d->u = u;
d->mem = u->ram.dat;
d->talk = talkfn;
fprintf(stderr, "Device added #%02x: %s, at 0x%04x \n", id, name, d->addr);
(void)name;
return d;
}

View File

@ -43,7 +43,7 @@ struct Uxn;
void mempoke16(Uint8 *m, Uint16 a, Uint16 b);
Uint16 mempeek16(Uint8 *m, Uint16 a);
int loaduxn(Uxn *c, char *filepath);
int bootuxn(Uxn *c);
int evaluxn(Uxn *u, Uint16 vec);
int haltuxn(Uxn *u, Uint8 error, char *name, int id);
Device *portuxn(Uxn *u, Uint8 id, char *name, void (*talkfn)(Device *, Uint8, Uint8));

View File

@ -111,6 +111,16 @@ nil_talk(Device *d, Uint8 b0, Uint8 w)
#pragma mark - Generics
static const char *errors[] = {"underflow", "overflow", "division by zero"};
int
haltuxn(Uxn *u, Uint8 error, char *name, int id)
{
fprintf(stderr, "Halted: %s %s#%04x, at 0x%04x\n", name, errors[error - 1], id, u->ram.ptr);
u->ram.ptr = 0;
return 0;
}
static void
run(Uxn *u)
{
@ -121,6 +131,17 @@ run(Uxn *u)
evaluxn(u, mempeek16(devconsole->dat, 0));
}
static int
loaduxn(Uxn *u, char *filepath)
{
FILE *f;
if(!(f = fopen(filepath, "rb")))
return 0;
fread(u->ram.dat + PAGE_PROGRAM, sizeof(u->ram.dat) - PAGE_PROGRAM, 1, f);
fprintf(stderr, "Uxn loaded[%s].\n", filepath);
return 1;
}
int
main(int argc, char **argv)
{

View File

@ -411,6 +411,16 @@ stdin_handler(void *p)
(void)p;
}
static const char *errors[] = {"underflow", "overflow", "division by zero"};
int
haltuxn(Uxn *u, Uint8 error, char *name, int id)
{
fprintf(stderr, "Halted: %s %s#%04x, at 0x%04x\n", name, errors[error - 1], id, u->ram.ptr);
u->ram.ptr = 0;
return 0;
}
static void
run(Uxn *u)
{
@ -465,6 +475,17 @@ run(Uxn *u)
}
}
static int
loaduxn(Uxn *u, char *filepath)
{
FILE *f;
if(!(f = fopen(filepath, "rb")))
return 0;
fread(u->ram.dat + PAGE_PROGRAM, sizeof(u->ram.dat) - PAGE_PROGRAM, 1, f);
fprintf(stderr, "Uxn loaded[%s].\n", filepath);
return 1;
}
int
main(int argc, char **argv)
{