2021-03-23 02:04:31 +00:00
|
|
|
#include <stdio.h>
|
2022-01-01 23:20:48 +00:00
|
|
|
#include <stdlib.h>
|
2021-06-27 20:45:50 +00:00
|
|
|
#include <unistd.h>
|
2022-01-07 18:02:28 +00:00
|
|
|
|
2021-05-13 01:28:45 +00:00
|
|
|
#include "uxn.h"
|
2022-01-08 18:03:21 +00:00
|
|
|
|
2022-01-11 22:16:27 +00:00
|
|
|
Uint8 *bank0, *bank1;
|
2022-01-08 18:03:21 +00:00
|
|
|
|
2022-01-05 13:28:22 +00:00
|
|
|
#include "devices/system.h"
|
2021-11-05 21:32:45 +00:00
|
|
|
#include "devices/file.h"
|
2022-01-07 18:02:28 +00:00
|
|
|
#include "devices/datetime.h"
|
2021-03-23 02:04:31 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma mark - Core
|
|
|
|
|
2021-07-29 03:20:57 +00:00
|
|
|
static Device *devsystem, *devconsole;
|
2021-06-27 20:45:50 +00:00
|
|
|
|
2021-06-28 21:42:36 +00:00
|
|
|
static int
|
2021-03-23 02:04:31 +00:00
|
|
|
error(char *msg, const char *err)
|
|
|
|
{
|
2021-07-14 19:11:10 +00:00
|
|
|
fprintf(stderr, "Error %s: %s\n", msg, err);
|
2021-03-23 02:04:31 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-08-16 00:48:15 +00:00
|
|
|
static void
|
2021-08-29 20:18:42 +00:00
|
|
|
inspect(Stack *s, char *name)
|
2021-08-16 00:48:15 +00:00
|
|
|
{
|
|
|
|
Uint8 x, y;
|
2021-08-29 20:18:42 +00:00
|
|
|
fprintf(stderr, "\n%s\n", name);
|
2022-01-04 02:04:09 +00:00
|
|
|
for(y = 0; y < 0x04; y++) {
|
|
|
|
for(x = 0; x < 0x08; x++) {
|
2021-08-16 00:48:15 +00:00
|
|
|
Uint8 p = y * 0x08 + x;
|
|
|
|
fprintf(stderr,
|
2021-08-29 20:18:42 +00:00
|
|
|
p == s->ptr ? "[%02x]" : " %02x ",
|
|
|
|
s->dat[p]);
|
2021-08-16 00:48:15 +00:00
|
|
|
}
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-23 02:04:31 +00:00
|
|
|
#pragma mark - Devices
|
|
|
|
|
2022-01-05 13:28:22 +00:00
|
|
|
void
|
|
|
|
system_deo_special(Device *d, Uint8 port)
|
2021-07-28 22:41:07 +00:00
|
|
|
{
|
2022-01-05 13:28:22 +00:00
|
|
|
if(port == 0xe) {
|
2022-01-06 03:48:51 +00:00
|
|
|
inspect(d->u->wst, "Working-stack");
|
|
|
|
inspect(d->u->rst, "Return-stack");
|
2021-11-04 16:38:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2021-11-04 17:13:01 +00:00
|
|
|
console_deo(Device *d, Uint8 port)
|
2021-03-23 02:04:31 +00:00
|
|
|
{
|
2022-01-11 19:07:25 +00:00
|
|
|
FILE *fd = port == 0x8 ? stdout : port == 0x9 ? stderr
|
|
|
|
: 0;
|
|
|
|
if(fd) {
|
|
|
|
fputc(d->dat[port], fd);
|
|
|
|
fflush(fd);
|
|
|
|
}
|
2021-03-23 02:04:31 +00:00
|
|
|
}
|
|
|
|
|
2021-11-04 16:38:32 +00:00
|
|
|
static Uint8
|
2021-11-04 17:13:01 +00:00
|
|
|
nil_dei(Device *d, Uint8 port)
|
2021-03-23 02:04:31 +00:00
|
|
|
{
|
2021-11-04 17:13:01 +00:00
|
|
|
return d->dat[port];
|
2021-11-04 16:38:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2021-11-04 17:13:01 +00:00
|
|
|
nil_deo(Device *d, Uint8 port)
|
2021-11-04 16:38:32 +00:00
|
|
|
{
|
2022-01-08 00:46:50 +00:00
|
|
|
(void)d;
|
|
|
|
(void)port;
|
2021-03-23 02:04:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - Generics
|
|
|
|
|
2021-10-13 21:56:38 +00:00
|
|
|
static int
|
|
|
|
console_input(Uxn *u, char c)
|
|
|
|
{
|
|
|
|
devconsole->dat[0x2] = c;
|
2022-01-08 00:46:50 +00:00
|
|
|
return uxn_eval(u, GETVECTOR(devconsole));
|
2021-10-13 21:56:38 +00:00
|
|
|
}
|
|
|
|
|
2021-06-29 13:43:28 +00:00
|
|
|
static void
|
|
|
|
run(Uxn *u)
|
2021-03-23 02:04:31 +00:00
|
|
|
{
|
2022-01-03 21:23:57 +00:00
|
|
|
Device *d = devconsole;
|
2022-01-11 19:07:25 +00:00
|
|
|
while((!u->dev[0].dat[0xf]) && (read(0, &d->dat[0x2], 1) > 0))
|
|
|
|
uxn_eval(u, GETVECTOR(d));
|
2021-03-23 02:04:31 +00:00
|
|
|
}
|
|
|
|
|
2021-08-01 20:35:46 +00:00
|
|
|
static int
|
2021-08-01 21:56:12 +00:00
|
|
|
load(Uxn *u, char *filepath)
|
2021-08-01 20:35:46 +00:00
|
|
|
{
|
|
|
|
FILE *f;
|
2021-11-17 13:29:36 +00:00
|
|
|
int r;
|
|
|
|
if(!(f = fopen(filepath, "rb"))) return 0;
|
2022-01-08 00:51:43 +00:00
|
|
|
r = fread(u->ram + PAGE_PROGRAM, 1, 0x10000 - PAGE_PROGRAM, f);
|
2021-11-17 13:29:36 +00:00
|
|
|
fclose(f);
|
|
|
|
if(r < 1) return 0;
|
2021-08-01 22:04:51 +00:00
|
|
|
fprintf(stderr, "Loaded %s\n", filepath);
|
2021-08-01 20:35:46 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-03-23 02:04:31 +00:00
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
Uxn u;
|
2021-10-13 21:56:38 +00:00
|
|
|
int i, loaded = 0;
|
2021-03-23 02:04:31 +00:00
|
|
|
|
2022-01-11 22:16:27 +00:00
|
|
|
bank0 = (Uint8 *)calloc(0x10000, sizeof(Uint8));
|
|
|
|
bank1 = (Uint8 *)calloc(0x10000, sizeof(Uint8));
|
|
|
|
if(!uxn_boot(&u, bank1, bank0 + PAGE_DEV, (Stack *)(bank0 + PAGE_WST), (Stack *)(bank0 + PAGE_RST)))
|
2021-03-23 02:04:31 +00:00
|
|
|
return error("Boot", "Failed");
|
|
|
|
|
2022-01-07 19:48:09 +00:00
|
|
|
/* system */ devsystem = uxn_port(&u, 0x0, system_dei, system_deo);
|
|
|
|
/* console */ devconsole = uxn_port(&u, 0x1, nil_dei, console_deo);
|
|
|
|
/* empty */ uxn_port(&u, 0x2, nil_dei, nil_deo);
|
|
|
|
/* empty */ uxn_port(&u, 0x3, nil_dei, nil_deo);
|
|
|
|
/* empty */ uxn_port(&u, 0x4, nil_dei, nil_deo);
|
|
|
|
/* empty */ uxn_port(&u, 0x5, nil_dei, nil_deo);
|
|
|
|
/* empty */ uxn_port(&u, 0x6, nil_dei, nil_deo);
|
|
|
|
/* empty */ uxn_port(&u, 0x7, nil_dei, nil_deo);
|
|
|
|
/* empty */ uxn_port(&u, 0x8, nil_dei, nil_deo);
|
|
|
|
/* empty */ uxn_port(&u, 0x9, nil_dei, nil_deo);
|
|
|
|
/* file */ uxn_port(&u, 0xa, nil_dei, file_deo);
|
|
|
|
/* datetime */ uxn_port(&u, 0xb, datetime_dei, nil_deo);
|
|
|
|
/* empty */ uxn_port(&u, 0xc, nil_dei, nil_deo);
|
|
|
|
/* empty */ uxn_port(&u, 0xd, nil_dei, nil_deo);
|
|
|
|
/* empty */ uxn_port(&u, 0xe, nil_dei, nil_deo);
|
|
|
|
/* empty */ uxn_port(&u, 0xf, nil_dei, nil_deo);
|
2021-05-26 17:16:42 +00:00
|
|
|
|
2022-01-04 02:04:09 +00:00
|
|
|
for(i = 1; i < argc; i++) {
|
2021-10-13 21:56:38 +00:00
|
|
|
if(!loaded++) {
|
|
|
|
if(!load(&u, argv[i]))
|
|
|
|
return error("Load", "Failed");
|
|
|
|
if(!uxn_eval(&u, PAGE_PROGRAM))
|
|
|
|
return error("Init", "Failed");
|
|
|
|
} else {
|
|
|
|
char *p = argv[i];
|
|
|
|
while(*p) console_input(&u, *p++);
|
|
|
|
console_input(&u, '\n');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!loaded)
|
|
|
|
return error("Input", "Missing");
|
|
|
|
|
2021-06-29 13:43:28 +00:00
|
|
|
run(&u);
|
2021-03-23 02:04:31 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|