Brought changes for the console

This commit is contained in:
Devine Lu Linvega 2023-04-17 11:36:17 -07:00
parent b733d4dc5a
commit 29f057ba44
7 changed files with 82 additions and 96 deletions

View File

@ -2,7 +2,7 @@
RELEASE_FLAGS="-Os -DNDEBUG -g0 -s" RELEASE_FLAGS="-Os -DNDEBUG -g0 -s"
DEBUG_FLAGS="-std=c89 -D_POSIX_C_SOURCE=199309L -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" DEBUG_FLAGS="-std=c89 -D_POSIX_C_SOURCE=199309L -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"
CORE_DEVICES="src/uxn.c src/devices/system.c src/devices/console.c src/devices/file.c src/devices/datetime.c" CORE_DEVICES="src/uxn.c src/devices/system.c src/devices/file.c src/devices/datetime.c"
EMU_INC="${CORE_DEVICES} src/devices/screen.c src/devices/controller.c src/devices/mouse.c src/uxn11.c -o bin/uxn11 -lX11" EMU_INC="${CORE_DEVICES} src/devices/screen.c src/devices/controller.c src/devices/mouse.c src/uxn11.c -o bin/uxn11 -lX11"
CLI_INC="${CORE_DEVICES} src/uxncli.c -o bin/uxncli" CLI_INC="${CORE_DEVICES} src/uxncli.c -o bin/uxncli"

View File

@ -1,35 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include "../uxn.h"
#include "console.h"
/*
Copyright (c) 2022-2023 Devine Lu Linvega, Andrew Alderwick
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.
*/
int
console_input(Uxn *u, char c)
{
Uint8 *d = &u->dev[0x10];
d[0x02] = c;
return uxn_eval(u, PEEK2(d));
}
void
console_deo(Uint8 *d, Uint8 port)
{
FILE *fd = port == 0x8 ? stdout : port == 0x9 ? stderr :
0;
if(fd) {
fputc(d[port], fd);
fflush(fd);
}
}

View File

@ -1,13 +0,0 @@
/*
Copyright (c) 2021 Devine Lu Linvega, Andrew Alderwick
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.
*/
int console_input(Uxn *u, char c);
void console_deo(Uint8 *d, Uint8 port);

View File

@ -35,10 +35,10 @@ system_print(Stack *s, char *name)
static void static void
system_cmd(Uint8 *ram, Uint16 addr) system_cmd(Uint8 *ram, Uint16 addr)
{ {
if(ram[addr] == 0x01) { if(ram[addr] == 0x1) {
Uint16 i, length = PEEK16(ram + addr + 1); Uint16 i, length = PEEK2(ram + addr + 1);
Uint16 a_page = PEEK16(ram + addr + 1 + 2), a_addr = PEEK16(ram + addr + 1 + 4); Uint16 a_page = PEEK2(ram + addr + 1 + 2), a_addr = PEEK2(ram + addr + 1 + 4);
Uint16 b_page = PEEK16(ram + addr + 1 + 6), b_addr = PEEK16(ram + addr + 1 + 8); Uint16 b_page = PEEK2(ram + addr + 1 + 6), b_addr = PEEK2(ram + addr + 1 + 8);
int src = (a_page % RAM_PAGES) * 0x10000, dst = (b_page % RAM_PAGES) * 0x10000; int src = (a_page % RAM_PAGES) * 0x10000, dst = (b_page % RAM_PAGES) * 0x10000;
for(i = 0; i < length; i++) for(i = 0; i < length; i++)
ram[dst + (Uint16)(b_addr + i)] = ram[src + (Uint16)(a_addr + i)]; ram[dst + (Uint16)(b_addr + i)] = ram[src + (Uint16)(a_addr + i)];
@ -49,35 +49,10 @@ int
system_error(char *msg, const char *err) system_error(char *msg, const char *err)
{ {
fprintf(stderr, "%s: %s\n", msg, err); fprintf(stderr, "%s: %s\n", msg, err);
return 1; fflush(stderr);
}
int
uxn_halt(Uxn *u, Uint8 instr, Uint8 err, Uint16 addr)
{
Uint8 *d = &u->dev[0x00];
Uint16 handler = PEEK2(d);
if(handler) {
u->wst.ptr = 4;
u->wst.dat[0] = addr >> 0x8;
u->wst.dat[1] = addr & 0xff;
u->wst.dat[2] = instr;
u->wst.dat[3] = err;
return uxn_eval(u, handler);
} else {
system_inspect(u);
fprintf(stderr, "%s %s, by %02x at 0x%04x.\n", (instr & 0x40) ? "Return-stack" : "Working-stack", errors[err - 1], instr, addr);
}
return 0; return 0;
} }
void
system_inspect(Uxn *u)
{
system_print(&u->wst, "wst");
system_print(&u->rst, "rst");
}
int int
system_load(Uxn *u, char *filename) system_load(Uxn *u, char *filename)
{ {
@ -92,6 +67,13 @@ system_load(Uxn *u, char *filename)
return 1; return 1;
} }
void
system_inspect(Uxn *u)
{
system_print(&u->wst, "wst");
system_print(&u->rst, "rst");
}
/* IO */ /* IO */
void void
@ -106,3 +88,50 @@ system_deo(Uxn *u, Uint8 *d, Uint8 port)
break; break;
} }
} }
/* Errors */
int
uxn_halt(Uxn *u, Uint8 instr, Uint8 err, Uint16 addr)
{
Uint8 *d = &u->dev[0];
Uint16 handler = PEEK2(d);
if(handler) {
u->wst.ptr = 4;
u->wst.dat[0] = addr >> 0x8;
u->wst.dat[1] = addr & 0xff;
u->wst.dat[2] = instr;
u->wst.dat[3] = err;
return uxn_eval(u, handler);
} else {
system_inspect(u);
fprintf(stderr, "%s %s, by %02x at 0x%04x.\n", (instr & 0x40) ? "Return-stack" : "Working-stack", errors[err - 1], instr, addr);
}
return 0;
}
/* Console */
int
console_input(Uxn *u, char c, int type)
{
Uint8 *d = &u->dev[0x10];
d[0x2] = c;
d[0x7] = type;
return uxn_eval(u, PEEK2(d));
}
void
console_deo(Uint8 *d, Uint8 port)
{
switch(port) {
case 0x8:
fputc(d[port], stdout);
fflush(stdout);
return;
case 0x9:
fputc(d[port], stderr);
fflush(stderr);
return;
}
}

View File

@ -10,9 +10,15 @@ WITH REGARD TO THIS SOFTWARE.
*/ */
#define RAM_PAGES 0x10 #define RAM_PAGES 0x10
#define PEEK16(d) ((d)[0] << 8 | (d)[1])
int system_error(char *msg, const char *err); #define CONSOLE_STD 0x1
#define CONSOLE_ARG 0x2
#define CONSOLE_EOA 0x3
#define CONSOLE_END 0x4
int system_load(Uxn *u, char *filename); int system_load(Uxn *u, char *filename);
void system_deo(Uxn *u, Uint8 *d, Uint8 port);
void system_inspect(Uxn *u); void system_inspect(Uxn *u);
int system_error(char *msg, const char *err);
void system_deo(Uxn *u, Uint8 *d, Uint8 port);
int console_input(Uxn *u, char c, int type);
void console_deo(Uint8 *d, Uint8 port);

View File

@ -9,7 +9,6 @@
#include "uxn.h" #include "uxn.h"
#include "devices/system.h" #include "devices/system.h"
#include "devices/console.h"
#include "devices/screen.h" #include "devices/screen.h"
#include "devices/controller.h" #include "devices/controller.h"
#include "devices/mouse.h" #include "devices/mouse.h"
@ -213,8 +212,8 @@ main(int argc, char **argv)
/* console vector */ /* console vector */
for(i = 2; i < argc; i++) { for(i = 2; i < argc; i++) {
char *p = argv[i]; char *p = argv[i];
while(*p) console_input(&u, *p++); while(*p) console_input(&u, *p++, CONSOLE_ARG);
console_input(&u, '\n'); console_input(&u, '\n', i == argc ? CONSOLE_END : CONSOLE_EOA);
} }
/* timer */ /* timer */
fds[0].fd = XConnectionNumber(display); fds[0].fd = XConnectionNumber(display);
@ -236,7 +235,7 @@ main(int argc, char **argv)
n = read(fds[2].fd, coninp, CONINBUFSIZE - 1); n = read(fds[2].fd, coninp, CONINBUFSIZE - 1);
coninp[n] = 0; coninp[n] = 0;
for(i = 0; i < n; i++) for(i = 0; i < n; i++)
console_input(&u, coninp[i]); console_input(&u, coninp[i], CONSOLE_STD);
} }
if(uxn_screen.fg.changed || uxn_screen.bg.changed) if(uxn_screen.fg.changed || uxn_screen.bg.changed)
emu_draw(); emu_draw();

View File

@ -3,7 +3,6 @@
#include "uxn.h" #include "uxn.h"
#include "devices/system.h" #include "devices/system.h"
#include "devices/console.h"
#include "devices/file.h" #include "devices/file.h"
#include "devices/datetime.h" #include "devices/datetime.h"
@ -46,23 +45,24 @@ int
main(int argc, char **argv) main(int argc, char **argv)
{ {
Uxn u; Uxn u;
int i; int i = 1;
if(argc < 2) if(i == argc)
return system_error("usage", "uxncli game.rom args"); return system_error("usage", "uxncli game.rom args");
if(!uxn_boot(&u, (Uint8 *)calloc(0x10000 * RAM_PAGES, sizeof(Uint8)))) if(!uxn_boot(&u, (Uint8 *)calloc(0x10000 * RAM_PAGES, sizeof(Uint8))))
return system_error("boot", "Failed"); return system_error("Boot", "Failed");
if(!system_load(&u, argv[1])) if(!system_load(&u, argv[i++]))
return system_error("load", "Failed"); return system_error("Load", "Failed");
u.dev[0x17] = i != argc;
if(!uxn_eval(&u, PAGE_PROGRAM)) if(!uxn_eval(&u, PAGE_PROGRAM))
return u.dev[0x0f] & 0x7f; return u.dev[0x0f] & 0x7f;
for(i = 2; i < argc; i++) { for(; i < argc; i++) {
char *p = argv[i]; char *p = argv[i];
while(*p) console_input(&u, *p++); while(*p) console_input(&u, *p++, CONSOLE_ARG);
console_input(&u, '\n'); console_input(&u, '\n', i == argc - 1 ? CONSOLE_END : CONSOLE_EOA);
} }
while(!u.dev[0x0f]) { while(!u.dev[0x0f]) {
int c = fgetc(stdin); int c = fgetc(stdin);
if(c != EOF) console_input(&u, (Uint8)c); if(c != EOF) console_input(&u, (Uint8)c, CONSOLE_STD);
} }
return u.dev[0x0f] & 0x7f; return u.dev[0x0f] & 0x7f;
} }