commit 6e21f3aba06d44dd656c3346b05f65ec0540658b
parent b53add0ba48afdcc08f2efdf8985e9a1ba190f03
Author: Andrew Alderwick <andrew@alderwick.co.uk>
Date: Sun, 1 Aug 2021 21:35:46 +0100
Removed stdio dependency from uxn.c
Reimplementation of patch sent in by Marc Schraffenberger
<marc@schraffenberger.com>: thank you for the rationale and inspiration!
Diffstat:
5 files changed, 45 insertions(+), 38 deletions(-)
diff --git a/src/uxn-fast.c b/src/uxn-fast.c
@@ -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
diff --git a/src/uxn.c b/src/uxn.c
@@ -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;
}
diff --git a/src/uxn.h b/src/uxn.h
@@ -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));
diff --git a/src/uxncli.c b/src/uxncli.c
@@ -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)
{
diff --git a/src/uxnemu.c b/src/uxnemu.c
@@ -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)
{