Starting debugger cli

This commit is contained in:
neauoire 2021-02-08 21:59:46 -08:00
parent 75b0fd06d9
commit de22c37e07
6 changed files with 189 additions and 50 deletions

View File

@ -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

94
cli.c Normal file
View File

@ -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;
}

View File

@ -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");

View File

@ -5,8 +5,8 @@
:dev1w FFF1
|0100 @RESET
,01 ,02 ADD
,01 ,02 ,03
BRK
|c000 @FRAME BRK
|d000 @ERROR BRK

78
uxn.c
View File

@ -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
evaluxn(Uxn *u, Uint16 vec)
parse(Uxn *u) /* TODO: Rename */
{
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);
}
}
int
evaluxn(Uxn *u, Uint16 vec)
{
u->ram.ptr = vec;
setflag(&u->status, FLAG_HALT, 0);
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;
}

10
uxn.h
View File

@ -35,11 +35,18 @@ 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);
@ -47,3 +54,4 @@ int getflag(Uint8 *status, char flag);
int loaduxn(Uxn *c, char *filepath);
int bootuxn(Uxn *c);
int evaluxn(Uxn *u, Uint16 vec);
int portuxn(Uxn *u, Uint16 r, Uint16 w, void (*onread)(), void (*onwrite)());