2021-03-22 22:04:31 -04:00
|
|
|
#include <stdio.h>
|
2021-06-27 16:45:50 -04:00
|
|
|
#include <unistd.h>
|
2021-10-22 19:41:23 -04:00
|
|
|
#include <time.h>
|
2021-05-12 21:28:45 -04:00
|
|
|
#include "uxn.h"
|
2021-03-22 22:04:31 -04: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-28 23:20:57 -04:00
|
|
|
static Device *devsystem, *devconsole;
|
2021-06-27 16:45:50 -04:00
|
|
|
|
2021-06-28 17:42:36 -04:00
|
|
|
static int
|
2021-03-22 22:04:31 -04:00
|
|
|
error(char *msg, const char *err)
|
|
|
|
{
|
2021-07-14 15:11:10 -04:00
|
|
|
fprintf(stderr, "Error %s: %s\n", msg, err);
|
2021-03-22 22:04:31 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-08-15 20:48:15 -04:00
|
|
|
static void
|
2021-08-29 16:18:42 -04:00
|
|
|
inspect(Stack *s, char *name)
|
2021-08-15 20:48:15 -04:00
|
|
|
{
|
|
|
|
Uint8 x, y;
|
2021-08-29 16:18:42 -04:00
|
|
|
fprintf(stderr, "\n%s\n", name);
|
2021-08-29 14:53:27 -04:00
|
|
|
for(y = 0; y < 0x04; ++y) {
|
2021-08-15 20:48:15 -04:00
|
|
|
for(x = 0; x < 0x08; ++x) {
|
|
|
|
Uint8 p = y * 0x08 + x;
|
|
|
|
fprintf(stderr,
|
2021-08-29 16:18:42 -04:00
|
|
|
p == s->ptr ? "[%02x]" : " %02x ",
|
|
|
|
s->dat[p]);
|
2021-08-15 20:48:15 -04:00
|
|
|
}
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-22 22:04:31 -04:00
|
|
|
#pragma mark - Devices
|
|
|
|
|
2021-11-04 12:38:32 -04:00
|
|
|
static Uint8
|
2021-11-04 13:13:01 -04:00
|
|
|
system_dei(Device *d, Uint8 port)
|
2021-07-28 18:41:07 -04:00
|
|
|
{
|
2021-11-04 13:13:01 -04:00
|
|
|
switch(port) {
|
2021-11-04 12:38:32 -04:00
|
|
|
case 0x2: return d->u->wst.ptr;
|
|
|
|
case 0x3: return d->u->rst.ptr;
|
2021-11-04 13:13:01 -04:00
|
|
|
default: return d->dat[port];
|
2021-08-15 20:48:15 -04:00
|
|
|
}
|
2021-07-28 18:41:07 -04:00
|
|
|
}
|
|
|
|
|
2021-11-04 12:38:32 -04:00
|
|
|
static void
|
2021-11-04 13:13:01 -04:00
|
|
|
system_deo(Device *d, Uint8 port)
|
2021-11-04 12:38:32 -04:00
|
|
|
{
|
2021-11-04 13:13:01 -04:00
|
|
|
switch(port) {
|
|
|
|
case 0x2: d->u->wst.ptr = d->dat[port]; break;
|
|
|
|
case 0x3: d->u->rst.ptr = d->dat[port]; break;
|
2021-11-04 12:38:32 -04:00
|
|
|
case 0xe:
|
|
|
|
inspect(&d->u->wst, "Working-stack");
|
|
|
|
inspect(&d->u->rst, "Return-stack");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2021-11-04 13:13:01 -04:00
|
|
|
console_deo(Device *d, Uint8 port)
|
2021-03-22 22:04:31 -04:00
|
|
|
{
|
2021-11-04 13:13:01 -04:00
|
|
|
if(port == 0x1)
|
2021-10-13 17:56:38 -04:00
|
|
|
d->vector = peek16(d->dat, 0x0);
|
2021-11-04 13:13:01 -04:00
|
|
|
if(port > 0x7)
|
|
|
|
write(port - 0x7, (char *)&d->dat[port], 1);
|
2021-03-22 22:04:31 -04:00
|
|
|
}
|
|
|
|
|
2021-11-04 12:38:32 -04:00
|
|
|
static void
|
2021-11-04 13:13:01 -04:00
|
|
|
file_deo(Device *d, Uint8 port)
|
2021-03-22 22:04:31 -04:00
|
|
|
{
|
2021-11-04 13:13:01 -04:00
|
|
|
Uint8 read = port == 0xd;
|
|
|
|
if(read || port == 0xf) {
|
2021-08-29 17:41:05 -04:00
|
|
|
char *name = (char *)&d->mem[peek16(d->dat, 0x8)];
|
|
|
|
Uint16 result = 0, length = peek16(d->dat, 0xa);
|
|
|
|
long offset = (peek16(d->dat, 0x4) << 16) + peek16(d->dat, 0x6);
|
2021-11-04 13:13:01 -04:00
|
|
|
Uint16 addr = peek16(d->dat, port - 1);
|
2021-08-31 14:30:46 -04:00
|
|
|
FILE *f = fopen(name, read ? "rb" : (offset ? "ab" : "wb"));
|
2021-04-20 17:30:26 -04:00
|
|
|
if(f) {
|
2021-05-26 13:02:13 -04:00
|
|
|
if(fseek(f, offset, SEEK_SET) != -1)
|
|
|
|
result = read ? fread(&d->mem[addr], 1, length, f) : fwrite(&d->mem[addr], 1, length, f);
|
2021-03-22 22:04:31 -04:00
|
|
|
fclose(f);
|
|
|
|
}
|
2021-08-29 17:41:05 -04:00
|
|
|
poke16(d->dat, 0x2, result);
|
2021-03-22 22:04:31 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-04 12:38:32 -04:00
|
|
|
static Uint8
|
2021-11-04 13:13:01 -04:00
|
|
|
datetime_dei(Device *d, Uint8 port)
|
2021-05-26 13:02:13 -04:00
|
|
|
{
|
|
|
|
time_t seconds = time(NULL);
|
|
|
|
struct tm *t = localtime(&seconds);
|
2021-11-04 13:13:01 -04:00
|
|
|
switch(port) {
|
2021-11-04 12:38:32 -04:00
|
|
|
case 0x0: return (t->tm_year + 1900) >> 8;
|
|
|
|
case 0x1: return (t->tm_year + 1900);
|
|
|
|
case 0x2: return t->tm_mon;
|
|
|
|
case 0x3: return t->tm_mday;
|
|
|
|
case 0x4: return t->tm_hour;
|
|
|
|
case 0x5: return t->tm_min;
|
|
|
|
case 0x6: return t->tm_sec;
|
|
|
|
case 0x7: return t->tm_wday;
|
|
|
|
case 0x8: return t->tm_yday >> 8;
|
|
|
|
case 0x9: return t->tm_yday;
|
|
|
|
case 0xa: return t->tm_isdst;
|
2021-11-04 13:13:01 -04:00
|
|
|
default: return d->dat[port];
|
2021-11-04 12:38:32 -04:00
|
|
|
}
|
2021-05-26 13:02:13 -04:00
|
|
|
}
|
|
|
|
|
2021-11-04 12:38:32 -04:00
|
|
|
static Uint8
|
2021-11-04 13:13:01 -04:00
|
|
|
nil_dei(Device *d, Uint8 port)
|
2021-03-22 22:04:31 -04:00
|
|
|
{
|
2021-11-04 13:13:01 -04:00
|
|
|
return d->dat[port];
|
2021-11-04 12:38:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2021-11-04 13:13:01 -04:00
|
|
|
nil_deo(Device *d, Uint8 port)
|
2021-11-04 12:38:32 -04:00
|
|
|
{
|
2021-11-04 13:13:01 -04:00
|
|
|
if(port == 0x1) d->vector = peek16(d->dat, 0x0);
|
2021-03-22 22:04:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - Generics
|
|
|
|
|
2021-08-01 16:35:46 -04:00
|
|
|
static const char *errors[] = {"underflow", "overflow", "division by zero"};
|
|
|
|
|
|
|
|
int
|
2021-08-01 17:46:43 -04:00
|
|
|
uxn_halt(Uxn *u, Uint8 error, char *name, int id)
|
2021-08-01 16:35:46 -04:00
|
|
|
{
|
|
|
|
fprintf(stderr, "Halted: %s %s#%04x, at 0x%04x\n", name, errors[error - 1], id, u->ram.ptr);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-10-13 17:56:38 -04:00
|
|
|
static int
|
|
|
|
console_input(Uxn *u, char c)
|
|
|
|
{
|
|
|
|
devconsole->dat[0x2] = c;
|
|
|
|
return uxn_eval(u, devconsole->vector);
|
|
|
|
}
|
|
|
|
|
2021-06-29 09:43:28 -04:00
|
|
|
static void
|
|
|
|
run(Uxn *u)
|
2021-03-22 22:04:31 -04:00
|
|
|
{
|
2021-10-13 17:56:38 -04:00
|
|
|
Uint16 vec;
|
2021-08-26 23:12:56 -04:00
|
|
|
while((!u->dev[0].dat[0xf]) && (read(0, &devconsole->dat[0x2], 1) > 0)) {
|
2021-08-29 17:41:05 -04:00
|
|
|
vec = peek16(devconsole->dat, 0);
|
2021-08-28 13:49:51 -04:00
|
|
|
if(!vec) vec = u->ram.ptr; /* continue after last BRK */
|
2021-08-26 23:12:56 -04:00
|
|
|
uxn_eval(u, vec);
|
2021-08-28 13:49:51 -04:00
|
|
|
}
|
2021-03-22 22:04:31 -04:00
|
|
|
}
|
|
|
|
|
2021-08-01 16:35:46 -04:00
|
|
|
static int
|
2021-08-01 17:56:12 -04:00
|
|
|
load(Uxn *u, char *filepath)
|
2021-08-01 16:35:46 -04:00
|
|
|
{
|
|
|
|
FILE *f;
|
|
|
|
if(!(f = fopen(filepath, "rb")))
|
|
|
|
return 0;
|
|
|
|
fread(u->ram.dat + PAGE_PROGRAM, sizeof(u->ram.dat) - PAGE_PROGRAM, 1, f);
|
2021-08-01 18:04:51 -04:00
|
|
|
fprintf(stderr, "Loaded %s\n", filepath);
|
2021-08-01 16:35:46 -04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-03-22 22:04:31 -04:00
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
Uxn u;
|
2021-10-13 17:56:38 -04:00
|
|
|
int i, loaded = 0;
|
2021-03-22 22:04:31 -04:00
|
|
|
|
2021-08-01 17:46:43 -04:00
|
|
|
if(!uxn_boot(&u))
|
2021-03-22 22:04:31 -04:00
|
|
|
return error("Boot", "Failed");
|
|
|
|
|
2021-11-04 12:38:32 -04: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 13:16:42 -04:00
|
|
|
|
2021-10-13 17:56:38 -04:00
|
|
|
for(i = 1; i < argc; ++i) {
|
|
|
|
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 09:43:28 -04:00
|
|
|
run(&u);
|
2021-03-22 22:04:31 -04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|