2021-03-23 02:04:31 +00:00
|
|
|
#include <stdio.h>
|
2021-05-26 17:02:13 +00:00
|
|
|
#include <time.h>
|
2021-06-27 20:45:50 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
2021-05-13 01:28:45 +00:00
|
|
|
#include "uxn.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);
|
2021-08-29 18:53:27 +00:00
|
|
|
for(y = 0; y < 0x04; ++y) {
|
2021-08-16 00:48:15 +00:00
|
|
|
for(x = 0; x < 0x08; ++x) {
|
|
|
|
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
|
|
|
|
|
2021-07-28 22:41:07 +00:00
|
|
|
static void
|
|
|
|
system_talk(Device *d, Uint8 b0, Uint8 w)
|
|
|
|
{
|
2021-08-16 00:48:15 +00:00
|
|
|
if(!w) { /* read */
|
|
|
|
switch(b0) {
|
|
|
|
case 0x2: d->dat[0x2] = d->u->wst.ptr; break;
|
|
|
|
case 0x3: d->dat[0x3] = d->u->rst.ptr; break;
|
|
|
|
}
|
|
|
|
} else { /* write */
|
|
|
|
switch(b0) {
|
|
|
|
case 0x2: d->u->wst.ptr = d->dat[0x2]; break;
|
|
|
|
case 0x3: d->u->rst.ptr = d->dat[0x3]; break;
|
2021-08-29 20:18:42 +00:00
|
|
|
case 0xe:
|
|
|
|
inspect(&d->u->wst, "Working-stack");
|
|
|
|
inspect(&d->u->rst, "Return-stack");
|
|
|
|
break;
|
2021-08-16 00:48:15 +00:00
|
|
|
case 0xf: d->u->ram.ptr = 0x0000; break;
|
2021-08-01 22:04:51 +00:00
|
|
|
}
|
2021-08-16 00:48:15 +00:00
|
|
|
}
|
2021-07-28 22:41:07 +00:00
|
|
|
}
|
|
|
|
|
2021-06-28 21:42:36 +00:00
|
|
|
static void
|
2021-04-24 17:15:47 +00:00
|
|
|
console_talk(Device *d, Uint8 b0, Uint8 w)
|
2021-03-23 02:04:31 +00:00
|
|
|
{
|
2021-07-11 16:51:11 +00:00
|
|
|
if(w && b0 > 0x7)
|
|
|
|
write(b0 - 0x7, (char *)&d->dat[b0], 1);
|
2021-03-23 02:04:31 +00:00
|
|
|
}
|
|
|
|
|
2021-06-28 21:42:36 +00:00
|
|
|
static void
|
2021-04-24 17:15:47 +00:00
|
|
|
file_talk(Device *d, Uint8 b0, Uint8 w)
|
2021-03-23 02:04:31 +00:00
|
|
|
{
|
2021-04-20 21:30:26 +00:00
|
|
|
Uint8 read = b0 == 0xd;
|
2021-04-24 17:15:47 +00:00
|
|
|
if(w && (read || b0 == 0xf)) {
|
2021-08-29 21:41:05 +00: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);
|
|
|
|
Uint16 addr = peek16(d->dat, b0 - 1);
|
2021-04-20 21:30:26 +00:00
|
|
|
FILE *f = fopen(name, read ? "r" : (offset ? "a" : "w"));
|
|
|
|
if(f) {
|
2021-07-14 19:10:45 +00:00
|
|
|
fprintf(stderr, "%s %s %s #%04x, ", read ? "Loading" : "Saving", name, read ? "to" : "from", addr);
|
2021-05-26 17:02:13 +00: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-07-14 19:10:45 +00:00
|
|
|
fprintf(stderr, "%04x bytes\n", result);
|
2021-03-23 02:04:31 +00:00
|
|
|
fclose(f);
|
|
|
|
}
|
2021-08-29 21:41:05 +00:00
|
|
|
poke16(d->dat, 0x2, result);
|
2021-03-23 02:04:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-28 21:42:36 +00:00
|
|
|
static void
|
2021-05-26 17:02:13 +00:00
|
|
|
datetime_talk(Device *d, Uint8 b0, Uint8 w)
|
|
|
|
{
|
|
|
|
time_t seconds = time(NULL);
|
|
|
|
struct tm *t = localtime(&seconds);
|
|
|
|
t->tm_year += 1900;
|
2021-08-29 21:41:05 +00:00
|
|
|
poke16(d->dat, 0x0, t->tm_year);
|
2021-05-26 17:02:13 +00:00
|
|
|
d->dat[0x2] = t->tm_mon;
|
|
|
|
d->dat[0x3] = t->tm_mday;
|
|
|
|
d->dat[0x4] = t->tm_hour;
|
|
|
|
d->dat[0x5] = t->tm_min;
|
|
|
|
d->dat[0x6] = t->tm_sec;
|
|
|
|
d->dat[0x7] = t->tm_wday;
|
2021-08-29 21:41:05 +00:00
|
|
|
poke16(d->dat, 0x08, t->tm_yday);
|
2021-05-26 17:02:13 +00:00
|
|
|
d->dat[0xa] = t->tm_isdst;
|
|
|
|
(void)b0;
|
|
|
|
(void)w;
|
|
|
|
}
|
|
|
|
|
2021-06-28 21:42:36 +00:00
|
|
|
static void
|
2021-04-24 17:15:47 +00:00
|
|
|
nil_talk(Device *d, Uint8 b0, Uint8 w)
|
2021-03-23 02:04:31 +00:00
|
|
|
{
|
2021-04-21 04:21:31 +00:00
|
|
|
(void)d;
|
2021-03-23 02:04:31 +00:00
|
|
|
(void)b0;
|
2021-04-24 17:15:47 +00:00
|
|
|
(void)w;
|
2021-03-23 02:04:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - Generics
|
|
|
|
|
2021-08-01 20:35:46 +00:00
|
|
|
static const char *errors[] = {"underflow", "overflow", "division by zero"};
|
|
|
|
|
|
|
|
int
|
2021-08-01 21:46:43 +00:00
|
|
|
uxn_halt(Uxn *u, Uint8 error, char *name, int id)
|
2021-08-01 20:35:46 +00:00
|
|
|
{
|
|
|
|
fprintf(stderr, "Halted: %s %s#%04x, at 0x%04x\n", name, errors[error - 1], id, u->ram.ptr);
|
|
|
|
u->ram.ptr = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-06-29 13:43:28 +00:00
|
|
|
static void
|
|
|
|
run(Uxn *u)
|
2021-03-23 02:04:31 +00:00
|
|
|
{
|
2021-08-28 17:49:51 +00:00
|
|
|
Uint16 vec = PAGE_PROGRAM;
|
2021-08-27 03:12:56 +00:00
|
|
|
uxn_eval(u, vec);
|
|
|
|
while((!u->dev[0].dat[0xf]) && (read(0, &devconsole->dat[0x2], 1) > 0)) {
|
2021-08-29 21:41:05 +00:00
|
|
|
vec = peek16(devconsole->dat, 0);
|
2021-08-28 17:49:51 +00:00
|
|
|
if(!vec) vec = u->ram.ptr; /* continue after last BRK */
|
2021-08-27 03:12:56 +00:00
|
|
|
uxn_eval(u, vec);
|
2021-08-28 17:49:51 +00:00
|
|
|
}
|
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;
|
|
|
|
if(!(f = fopen(filepath, "rb")))
|
|
|
|
return 0;
|
|
|
|
fread(u->ram.dat + PAGE_PROGRAM, sizeof(u->ram.dat) - PAGE_PROGRAM, 1, f);
|
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;
|
|
|
|
|
|
|
|
if(argc < 2)
|
|
|
|
return error("Input", "Missing");
|
2021-08-01 21:46:43 +00:00
|
|
|
if(!uxn_boot(&u))
|
2021-03-23 02:04:31 +00:00
|
|
|
return error("Boot", "Failed");
|
2021-08-01 21:56:12 +00:00
|
|
|
if(!load(&u, argv[1]))
|
2021-03-23 02:04:31 +00:00
|
|
|
return error("Load", "Failed");
|
|
|
|
|
2021-08-30 02:52:12 +00:00
|
|
|
/* system */ devsystem = uxn_port(&u, 0x0, system_talk);
|
|
|
|
/* console */ devconsole = uxn_port(&u, 0x1, console_talk);
|
|
|
|
/* empty */ uxn_port(&u, 0x2, nil_talk);
|
|
|
|
/* empty */ uxn_port(&u, 0x3, nil_talk);
|
|
|
|
/* empty */ uxn_port(&u, 0x4, nil_talk);
|
|
|
|
/* empty */ uxn_port(&u, 0x5, nil_talk);
|
|
|
|
/* empty */ uxn_port(&u, 0x6, nil_talk);
|
|
|
|
/* empty */ uxn_port(&u, 0x7, nil_talk);
|
|
|
|
/* empty */ uxn_port(&u, 0x8, nil_talk);
|
|
|
|
/* empty */ uxn_port(&u, 0x9, nil_talk);
|
|
|
|
/* file */ uxn_port(&u, 0xa, file_talk);
|
|
|
|
/* datetime */ uxn_port(&u, 0xb, datetime_talk);
|
|
|
|
/* empty */ uxn_port(&u, 0xc, nil_talk);
|
|
|
|
/* empty */ uxn_port(&u, 0xd, nil_talk);
|
|
|
|
/* empty */ uxn_port(&u, 0xe, nil_talk);
|
|
|
|
/* empty */ uxn_port(&u, 0xf, nil_talk);
|
2021-05-26 17:16:42 +00:00
|
|
|
|
2021-06-29 13:43:28 +00:00
|
|
|
run(&u);
|
2021-03-23 02:04:31 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|