2022-03-27 13:01:06 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2022-03-26 21:32:46 -04:00
|
|
|
#include "../uxn.h"
|
|
|
|
#include "system.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
Copyright (c) 2022 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static const char *errors[] = {
|
2022-04-10 12:42:56 -04:00
|
|
|
"underflow",
|
|
|
|
"overflow",
|
2022-06-13 14:08:33 -04:00
|
|
|
"division by zero"};
|
2022-03-26 21:32:46 -04:00
|
|
|
|
|
|
|
static void
|
|
|
|
system_print(Stack *s, char *name)
|
|
|
|
{
|
|
|
|
Uint8 i;
|
|
|
|
fprintf(stderr, "<%s>", name);
|
|
|
|
for(i = 0; i < s->ptr; i++)
|
|
|
|
fprintf(stderr, " %02x", s->dat[i]);
|
|
|
|
if(!i)
|
|
|
|
fprintf(stderr, " empty");
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
system_inspect(Uxn *u)
|
|
|
|
{
|
2022-06-13 15:20:20 -04:00
|
|
|
system_print(u->wst, "wst");
|
|
|
|
system_print(u->rst, "rst");
|
2022-03-26 21:32:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2022-04-10 12:42:56 -04:00
|
|
|
uxn_halt(Uxn *u, Uint8 instr, Uint8 err, Uint16 addr)
|
2022-03-26 21:32:46 -04:00
|
|
|
{
|
2022-04-10 12:42:56 -04:00
|
|
|
Uint8 *d = &u->dev[0x00];
|
2022-04-10 13:31:18 -04:00
|
|
|
if(instr & 0x40)
|
|
|
|
u->rst->err = err;
|
|
|
|
else
|
|
|
|
u->wst->err = err;
|
2022-04-10 12:42:56 -04:00
|
|
|
if(GETVEC(d))
|
|
|
|
uxn_eval(u, GETVEC(d));
|
|
|
|
else {
|
|
|
|
system_inspect(u);
|
2022-04-10 13:31:18 -04:00
|
|
|
fprintf(stderr, "%s %s, by %02x at 0x%04x.\n", (instr & 0x40) ? "Return-stack" : "Working-stack", errors[err - 1], instr, addr);
|
2022-04-10 12:42:56 -04:00
|
|
|
}
|
2022-03-26 21:32:46 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* IO */
|
|
|
|
|
|
|
|
void
|
2022-04-05 12:35:49 -04:00
|
|
|
system_deo(Uxn *u, Uint8 *d, Uint8 port)
|
2022-03-26 21:32:46 -04:00
|
|
|
{
|
|
|
|
switch(port) {
|
2022-04-05 12:35:49 -04:00
|
|
|
case 0x2: u->wst = (Stack *)(u->ram + (d[port] ? (d[port] * 0x100) : 0x10000)); break;
|
|
|
|
case 0x3: u->rst = (Stack *)(u->ram + (d[port] ? (d[port] * 0x100) : 0x10100)); break;
|
2022-06-13 15:20:20 -04:00
|
|
|
case 0xe: if(u->wst->ptr || u->rst->ptr) system_inspect(u); break;
|
2022-03-26 21:32:46 -04:00
|
|
|
}
|
|
|
|
}
|