uxn

Varvara Ordinator, written in ANSI C(SDL2)
git clone https://git.eamoncaddigan.net/uxn.git
Log | Files | Refs | README | LICENSE

commit de22c37e07b4f873d795c54ec3a990ba7f355a74
parent 75b0fd06d911f82d164fba471862e0cb54a50e5c
Author: neauoire <aliceffekt@gmail.com>
Date:   Mon,  8 Feb 2021 21:59:46 -0800

Starting debugger cli

Diffstat:
Mbuild.sh | 19++++++++++++-------
Acli.c | 94+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Memulator.c | 22++++++++++++++++++----
Mexamples/test.usm | 8++++----
Muxn.c | 84++++++++++++++++++++++++++++++++++++++++++++++++-------------------------------
Muxn.h | 13++++++++++---
6 files changed, 189 insertions(+), 51 deletions(-)

diff --git a/build.sh b/build.sh @@ -5,17 +5,22 @@ mkdir -p bin # Assembler clang-format -i assembler.c -rm -f ./assembler -rm -f ./boot.rom +rm -f ./bin/assembler +rm -f ./bin/boot.rom cc -std=c89 -DDEBUG -Wall -Wno-unknown-pragmas -Wpedantic -Wshadow -Wextra -Werror=implicit-int -Werror=incompatible-pointer-types -Werror=int-conversion -Wvla -g -Og -fsanitize=address -fsanitize=undefined assembler.c -o bin/assembler ./bin/assembler examples/hello.usm bin/boot.rom -# Emulator -clang-format -i emulator.c +# Cli +clang-format -i cli.c clang-format -i uxn.h clang-format -i uxn.c -rm -f ./uxn -cc -std=c89 -DDEBUG -Wall -Wno-unknown-pragmas -Wpedantic -Wshadow -Wextra -Werror=implicit-int -Werror=incompatible-pointer-types -Werror=int-conversion -Wvla -g -Og -fsanitize=address -fsanitize=undefined uxn.c emulator.c -L/usr/local/lib -lSDL2 -o bin/emulator +rm -f ./bin/cli +cc -std=c89 -DDEBUG -Wall -Wno-unknown-pragmas -Wpedantic -Wshadow -Wextra -Werror=implicit-int -Werror=incompatible-pointer-types -Werror=int-conversion -Wvla -g -Og -fsanitize=address -fsanitize=undefined uxn.c cli.c -o bin/cli + +# Emulator +# clang-format -i emulator.c +# rm -f ./bin/emulator +# cc -std=c89 -DDEBUG -Wall -Wno-unknown-pragmas -Wpedantic -Wshadow -Wextra -Werror=implicit-int -Werror=incompatible-pointer-types -Werror=int-conversion -Wvla -g -Og -fsanitize=address -fsanitize=undefined uxn.c emulator.c -L/usr/local/lib -lSDL2 -o bin/emulator # run -./bin/emulator bin/boot.rom +./bin/cli bin/boot.rom diff --git a/cli.c b/cli.c @@ -0,0 +1,94 @@ +#include <stdio.h> + +/* +Copyright (c) 2021 Devine Lu Linvega + +Permission to use, copy, modify, and distribute this software for any +purpose with or without fee is hereby granted, provided that the above +copyright notice and this permission notice appear in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +WITH REGARD TO THIS SOFTWARE. +*/ + +#include "uxn.h" + +int +error(char *msg, const char *err) +{ + printf("Error %s: %s\n", msg, err); + return 0; +} + +void +console_onread(void) +{ +} + +void +console_onwrite(void) +{ +} + +void +echos(Stack8 *s, Uint8 len, char *name) +{ + int i; + printf("\n%s\n", name); + for(i = 0; i < len; ++i) { + if(i % 16 == 0) + printf("\n"); + printf("%02x%c", s->dat[i], s->ptr == i ? '<' : ' '); + } + printf("\n\n"); +} + +void +echom(Memory *m, Uint16 len, char *name) +{ + int i; + printf("\n%s\n", name); + for(i = 0; i < len; ++i) { + if(i % 16 == 0) + printf("\n"); + printf("%02x ", m->dat[i]); + } + printf("\n\n"); +} + +void +echof(Uxn *c) +{ + printf("ended @ %d steps | hf: %x sf: %x sf: %x cf: %x\n", + c->counter, + getflag(&c->status, FLAG_HALT) != 0, + getflag(&c->status, FLAG_SHORT) != 0, + getflag(&c->status, FLAG_SIGN) != 0, + getflag(&c->status, FLAG_COND) != 0); +} + +Uxn u; + +int +main(int argc, char **argv) +{ + if(argc < 2) + return error("Input", "Missing"); + if(!bootuxn(&u)) + return error("Boot", "Failed"); + if(!loaduxn(&u, argv[1])) + return error("Load", "Failed"); + + portuxn(&u, 0xfff0, 0xfff1, console_onread, console_onwrite); + + printf("VRESET\n"); + evaluxn(&u, u.vreset); + printf("VFRAME\n"); + evaluxn(&u, u.vframe); + + echos(&u.wst, 0x40, "stack"); + echom(&u.ram, 0x40, "ram"); + echof(&u); + + return 0; +} diff --git a/emulator.c b/emulator.c @@ -137,14 +137,26 @@ void domouse(SDL_Event *event) { (void)event; - printf("mouse\n"); + /* printf("mouse\n"); */ } void dokey(SDL_Event *event) { (void)event; - printf("key\n"); + /* printf("key\n"); */ +} + +#pragma mark - Devices + +void +console_onread(void) +{ +} + +void +console_onwrite(void) +{ } int @@ -175,11 +187,11 @@ start(Uxn *u) } } +Uxn u; + int main(int argc, char **argv) { - Uxn u; - if(argc < 2) return error("Input", "Missing"); if(!bootuxn(&u)) @@ -189,6 +201,8 @@ main(int argc, char **argv) if(!init()) return error("Init", "Failed"); + portuxn(&u, 0xfff0, 0xfff1, console_onread, console_onwrite); + start(&u); echos(&u.wst, 0x40, "stack"); diff --git a/examples/test.usm b/examples/test.usm @@ -4,10 +4,10 @@ :dev1r FFF0 :dev1w FFF1 -|0100 @RESET +|0100 @RESET +,01 ,02 ,03 +BRK -,01 ,02 ADD - -|c000 @FRAME BRK +|c000 @FRAME BRK |d000 @ERROR BRK |FFFA .RESET .FRAME .ERROR diff --git a/uxn.c b/uxn.c @@ -103,26 +103,6 @@ haltuxn(Uxn *u, char *name, int id) return 0; } -void -inituxn(Uxn *u) -{ - size_t i; - char *cptr = (char *)u; - for(i = 0; i < sizeof u; i++) - cptr[i] = 0; -} - -int -loaduxn(Uxn *u, char *filepath) -{ - FILE *f; - if(!(f = fopen(filepath, "rb"))) - return haltuxn(u, "Missing input.", 0); - fread(u->ram.dat, sizeof(u->ram.dat), 1, f); - printf("Uxn Loaded: %s\n", filepath); - return 1; -} - int lituxn(Uxn *u, Uint8 instr) { @@ -136,9 +116,13 @@ lituxn(Uxn *u, Uint8 instr) int devuxn(Uxn *u) /* experimental */ { - if(u->ram.dat[0xfff1]) { - printf("%c", u->ram.dat[0xfff1]); - u->ram.dat[0xfff1] = 0x00; + int i; + for(i = 0; i < u->devices; ++i) { + Uint16 addr = u->dev[i].w; + if(u->ram.dat[addr]) { + printf("%c", u->ram.dat[addr]); + u->ram.dat[addr] = 0; + } } return 1; } @@ -163,30 +147,64 @@ opcuxn(Uxn *u, Uint8 instr) } int +parse(Uxn *u) /* TODO: Rename */ +{ + Uint8 instr = u->ram.dat[u->ram.ptr++]; + u->counter++; + if(u->literal > 0) + return lituxn(u, instr); + else + return opcuxn(u, instr); +} + +int evaluxn(Uxn *u, Uint16 vec) { u->ram.ptr = vec; setflag(&u->status, FLAG_HALT, 0); - while(!(u->status & FLAG_HALT)) { - Uint8 instr = u->ram.dat[u->ram.ptr++]; - u->counter++; - if(u->literal > 0) - return lituxn(u, instr); - else - return opcuxn(u, instr); - } + while(!(u->status & FLAG_HALT) && parse(u)) + ; return 1; } int bootuxn(Uxn *u) { - inituxn(u); + size_t 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 haltuxn(u, "Missing input.", 0); + fread(u->ram.dat, sizeof(u->ram.dat), 1, f); u->vreset = mempeek16(u, 0xfffa); u->vframe = mempeek16(u, 0xfffc); u->verror = mempeek16(u, 0xfffe); - printf("Uxn Ready.\n"); + printf("Uxn loaded[%s] vrst:%04x vfrm:%04x verr:%04x.\n", + filepath, + u->vreset, + u->vframe, + u->verror); return 1; } /* to start: evaluxn(u, u->vreset); */ + +int +portuxn(Uxn *u, Uint16 r, Uint16 w, void (*onread)(), void (*onwrite)()) +{ + Device *d = &u->dev[u->devices++]; + d->r = r; + d->w = w; + d->rfn = onread; + d->wfn = onwrite; + printf("Created device: #%d, at r:%04x w:%04x\n", u->devices, r, w); + return 1; +} diff --git a/uxn.h b/uxn.h @@ -35,15 +35,23 @@ typedef struct { } Memory; typedef struct { - Uint8 literal, status; + Uint16 r, w; + void (*rfn)(void); + void (*wfn)(void); +} Device; + +typedef struct { + Uint8 literal, status, devices; Uint16 counter, vreset, vframe, verror; Stack8 wst; Stack16 rst; Memory ram; + Device dev[64]; } Uxn; void setflag(Uint8 *status, char flag, int b); int getflag(Uint8 *status, char flag); int loaduxn(Uxn *c, char *filepath); int bootuxn(Uxn *c); -int evaluxn(Uxn *u, Uint16 vec); -\ No newline at end of file +int evaluxn(Uxn *u, Uint16 vec); +int portuxn(Uxn *u, Uint16 r, Uint16 w, void (*onread)(), void (*onwrite)());