commit cca1253376a5ad3eb728f6ed8b4e4b06ca8a9a62
parent 22c6e07fca0955214f2ce94a9817a02473a8c3db
Author: neauoire <aliceffekt@gmail.com>
Date: Sat, 1 Jan 2022 15:20:48 -0800
Use calloc for memory array
Diffstat:
6 files changed, 35 insertions(+), 12 deletions(-)
diff --git a/projects/examples/devices/console.tal b/projects/examples/devices/console.tal
@@ -1,7 +1,9 @@
( dev/console )
-|00 @System $e &debug
-|10 @Console $8 &write
+%HALT { #010f DEO }
+%EMIT { #18 DEO }
+%DEBUG { ;print-hex/byte JSR2 #0a EMIT }
+%DEBUG2 { ;print-hex JSR2 #0a EMIT }
( init )
@@ -9,11 +11,28 @@
;hello-word
&while
- ( send ) LDAk .Console/write DEO
+ ( send ) LDAk EMIT
+ #20 EMIT
+ DUP2 ;print-hex JSR2
+ #20 EMIT
+ LDAk ;print-hex/byte JSR2
+ #0a EMIT
INC2 LDAk ,&while JCN
POP2
- ( show debugger ) #01 .System/debug DEO
+ ( stop ) HALT
BRK
+@print-hex ( value* -- )
+
+ SWP ,&byte JSR
+ &byte ( byte -- )
+ STHk #04 SFT ,&parse JSR #18 DEO
+ STHr #0f AND ,&parse JSR #18 DEO
+ JMP2r
+ &parse ( byte -- char ) DUP #09 GTH ,&above JCN #30 ADD JMP2r
+ &above #57 ADD JMP2r
+
+JMP2r
+
@hello-word "Hello 20 "Uxn!
\ No newline at end of file
diff --git a/src/uxn-fast.c b/src/uxn-fast.c
@@ -4018,12 +4018,13 @@ error:
}
int
-uxn_boot(Uxn *u)
+uxn_boot(Uxn *u, Uint8 *memory)
{
unsigned int i;
char *cptr = (char *)u;
for(i = 0; i < sizeof(*u); ++i)
cptr[i] = 0x00;
+ u->ram.dat = memory;
return 1;
}
diff --git a/src/uxn.c b/src/uxn.c
@@ -136,12 +136,13 @@ uxn_eval(Uxn *u, Uint16 vec)
/* clang-format on */
int
-uxn_boot(Uxn *u)
+uxn_boot(Uxn *u, Uint8 *memory)
{
Uint32 i;
char *cptr = (char *)u;
for(i = 0; i < sizeof(*u); ++i)
cptr[i] = 0x00;
+ u->ram.dat = memory;
return 1;
}
diff --git a/src/uxn.h b/src/uxn.h
@@ -24,7 +24,7 @@ typedef struct {
typedef struct {
Uint16 ptr;
- Uint8 dat[65536];
+ Uint8 *dat;
} Memory;
typedef struct Device {
@@ -44,7 +44,7 @@ typedef struct Uxn {
void poke16(Uint8 *m, Uint16 a, Uint16 b);
Uint16 peek16(Uint8 *m, Uint16 a);
-int uxn_boot(Uxn *c);
+int uxn_boot(Uxn *c, Uint8 *memory);
int uxn_eval(Uxn *u, Uint16 vec);
int uxn_halt(Uxn *u, Uint8 error, char *name, int id);
Device *uxn_port(Uxn *u, Uint8 id, Uint8 (*deifn)(Device *, Uint8), void (*deofn)(Device *, Uint8));
diff --git a/src/uxncli.c b/src/uxncli.c
@@ -1,4 +1,5 @@
#include <stdio.h>
+#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include "uxn.h"
@@ -147,7 +148,7 @@ load(Uxn *u, char *filepath)
FILE *f;
int r;
if(!(f = fopen(filepath, "rb"))) return 0;
- r = fread(u->ram.dat + PAGE_PROGRAM, 1, sizeof(u->ram.dat) - PAGE_PROGRAM, f);
+ r = fread(u->ram.dat + PAGE_PROGRAM, 1, 0xffff - PAGE_PROGRAM, f);
fclose(f);
if(r < 1) return 0;
fprintf(stderr, "Loaded %s\n", filepath);
@@ -160,7 +161,7 @@ main(int argc, char **argv)
Uxn u;
int i, loaded = 0;
- if(!uxn_boot(&u))
+ if(!uxn_boot(&u, (Uint8 *)calloc(0xffff, sizeof(Uint8))))
return error("Boot", "Failed");
/* system */ devsystem = uxn_port(&u, 0x0, system_dei, system_deo);
diff --git a/src/uxnemu.c b/src/uxnemu.c
@@ -273,7 +273,7 @@ load(Uxn *u, char *rom)
SDL_RWops *f;
int r;
if(!(f = SDL_RWFromFile(rom, "rb"))) return 0;
- r = f->read(f, u->ram.dat + PAGE_PROGRAM, 1, sizeof(u->ram.dat) - PAGE_PROGRAM);
+ r = f->read(f, u->ram.dat + PAGE_PROGRAM, 1, 0xffff - PAGE_PROGRAM);
f->close(f);
if(r < 1) return 0;
fprintf(stderr, "Loaded %s\n", rom);
@@ -284,7 +284,8 @@ load(Uxn *u, char *rom)
static int
start(Uxn *u, char *rom)
{
- if(!uxn_boot(u))
+ Uint8 *memory = (Uint8 *)calloc(0xffff, sizeof(Uint8));
+ if(!uxn_boot(u, memory))
return error("Boot", "Failed to start uxn.");
if(!load(u, rom))
return error("Boot", "Failed to load rom.");