2021-05-13 01:28:45 +00:00
|
|
|
#include "uxn.h"
|
2021-01-29 19:17:59 +00:00
|
|
|
|
2021-01-29 19:35:59 +00:00
|
|
|
/*
|
2021-02-08 22:32:22 +00:00
|
|
|
Copyright (u) 2021 Devine Lu Linvega
|
2021-05-23 16:33:00 +00:00
|
|
|
Copyright (u) 2021 Andrew Alderwick
|
2021-01-29 19:17:59 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2021-08-30 19:16:57 +00:00
|
|
|
#define MODE_SHORT 0x20
|
2021-08-30 17:19:33 +00:00
|
|
|
#define MODE_RETURN 0x40
|
2021-08-30 19:16:57 +00:00
|
|
|
#define MODE_KEEP 0x80
|
2021-08-05 03:06:46 +00:00
|
|
|
|
2021-02-08 22:18:01 +00:00
|
|
|
#pragma mark - Operations
|
|
|
|
|
|
|
|
/* clang-format off */
|
2021-08-29 22:05:37 +00:00
|
|
|
|
|
|
|
/* Utilities */
|
|
|
|
static void (*push)(Stack *s, Uint16 a);
|
|
|
|
static Uint16 (*pop8)(Stack *s);
|
|
|
|
static Uint16 (*pop)(Stack *s);
|
|
|
|
static void (*poke)(Uint8 *m, Uint16 a, Uint16 b);
|
|
|
|
static Uint16 (*peek)(Uint8 *m, Uint16 a);
|
2021-11-04 16:38:32 +00:00
|
|
|
static void (*devw)(Device *d, Uint8 a, Uint16 b);
|
2021-08-30 00:36:17 +00:00
|
|
|
static Uint16 (*devr)(Device *d, Uint8 a);
|
2021-08-30 00:49:22 +00:00
|
|
|
static void (*warp)(Uxn *u, Uint16 a);
|
2021-08-30 01:02:46 +00:00
|
|
|
static void (*pull)(Uxn *u);
|
2021-08-30 01:18:52 +00:00
|
|
|
/* byte mode */
|
2021-08-29 21:53:31 +00:00
|
|
|
static void push8(Stack *s, Uint16 a) { if(s->ptr == 0xff) { s->error = 2; return; } s->dat[s->ptr++] = a; }
|
2021-08-30 01:18:52 +00:00
|
|
|
static Uint16 pop8k(Stack *s) { if(s->kptr == 0) { s->error = 1; return 0; } return s->dat[--s->kptr]; }
|
|
|
|
static Uint16 pop8d(Stack *s) { if(s->ptr == 0) { s->error = 1; return 0; } return s->dat[--s->ptr]; }
|
2021-08-29 22:05:37 +00:00
|
|
|
static void poke8(Uint8 *m, Uint16 a, Uint16 b) { m[a] = b; }
|
|
|
|
static Uint16 peek8(Uint8 *m, Uint16 a) { return m[a]; }
|
2021-11-04 16:38:32 +00:00
|
|
|
static void devw8(Device *d, Uint8 a, Uint16 b) { d->dat[a & 0xf] = b; d->deo(d, a & 0x0f); }
|
|
|
|
static Uint16 devr8(Device *d, Uint8 a) { return d->dei(d, a & 0x0f); }
|
2021-08-30 01:18:52 +00:00
|
|
|
static void warp8(Uxn *u, Uint16 a){ u->ram.ptr += (Sint8)a; }
|
|
|
|
static void pull8(Uxn *u){ push8(u->src, peek8(u->ram.dat, u->ram.ptr++)); }
|
|
|
|
/* short mode */
|
2021-08-29 22:05:37 +00:00
|
|
|
static void push16(Stack *s, Uint16 a) { push8(s, a >> 8); push8(s, a); }
|
|
|
|
static Uint16 pop16(Stack *s) { Uint8 a = pop8(s), b = pop8(s); return a + (b << 8); }
|
2021-08-30 02:52:12 +00:00
|
|
|
void poke16(Uint8 *m, Uint16 a, Uint16 b) { poke8(m, a, b >> 8); poke8(m, a + 1, b); }
|
|
|
|
Uint16 peek16(Uint8 *m, Uint16 a) { return (peek8(m, a) << 8) + peek8(m, a + 1); }
|
2021-11-04 16:38:32 +00:00
|
|
|
static void devw16(Device *d, Uint8 a, Uint16 b) { devw8(d, a, b >> 8); devw8(d, a + 1, b); }
|
2021-08-30 00:36:17 +00:00
|
|
|
static Uint16 devr16(Device *d, Uint8 a) { return (devr8(d, a) << 8) + devr8(d, a + 1); }
|
2021-08-30 01:18:52 +00:00
|
|
|
static void warp16(Uxn *u, Uint16 a){ u->ram.ptr = a; }
|
|
|
|
static void pull16(Uxn *u){ push16(u->src, peek16(u->ram.dat, u->ram.ptr++)); u->ram.ptr++; }
|
2021-02-08 22:18:01 +00:00
|
|
|
|
2021-02-27 16:18:57 +00:00
|
|
|
#pragma mark - Core
|
|
|
|
|
2021-02-08 22:18:01 +00:00
|
|
|
int
|
2021-08-01 21:46:43 +00:00
|
|
|
uxn_eval(Uxn *u, Uint16 vec)
|
2021-02-08 22:18:01 +00:00
|
|
|
{
|
2021-08-17 21:48:48 +00:00
|
|
|
Uint8 instr;
|
2021-08-30 01:18:52 +00:00
|
|
|
Uint16 a,b,c;
|
2021-08-18 02:30:34 +00:00
|
|
|
if(!vec || u->dev[0].dat[0xf])
|
2021-07-28 17:29:09 +00:00
|
|
|
return 0;
|
2021-02-08 23:46:52 +00:00
|
|
|
u->ram.ptr = vec;
|
2021-08-05 03:14:53 +00:00
|
|
|
if(u->wst.ptr > 0xf8) u->wst.ptr = 0xf8;
|
2021-08-17 21:48:48 +00:00
|
|
|
while((instr = u->ram.dat[u->ram.ptr++])) {
|
2021-08-05 03:06:46 +00:00
|
|
|
/* Return Mode */
|
|
|
|
if(instr & MODE_RETURN) {
|
2021-08-30 01:18:52 +00:00
|
|
|
u->src = &u->rst;
|
2021-08-05 03:06:46 +00:00
|
|
|
u->dst = &u->wst;
|
|
|
|
} else {
|
2021-08-30 01:18:52 +00:00
|
|
|
u->src = &u->wst;
|
2021-08-05 03:06:46 +00:00
|
|
|
u->dst = &u->rst;
|
|
|
|
}
|
|
|
|
/* Keep Mode */
|
|
|
|
if(instr & MODE_KEEP) {
|
2021-08-30 01:18:52 +00:00
|
|
|
pop8 = pop8k;
|
2021-08-05 03:06:46 +00:00
|
|
|
u->src->kptr = u->src->ptr;
|
|
|
|
} else {
|
2021-08-30 01:18:52 +00:00
|
|
|
pop8 = pop8d;
|
2021-08-05 03:06:46 +00:00
|
|
|
}
|
2021-08-29 21:53:31 +00:00
|
|
|
/* Short Mode */
|
|
|
|
if(instr & MODE_SHORT) {
|
2021-08-30 01:18:52 +00:00
|
|
|
push = push16; pop = pop16;
|
|
|
|
poke = poke16; peek = peek16;
|
|
|
|
devw = devw16; devr = devr16;
|
|
|
|
warp = warp16; pull = pull16;
|
2021-08-29 21:53:31 +00:00
|
|
|
} else {
|
2021-08-30 01:18:52 +00:00
|
|
|
push = push8; pop = pop8;
|
|
|
|
poke = poke8; peek = peek8;
|
|
|
|
devw = devw8; devr = devr8;
|
|
|
|
warp = warp8; pull = pull8;
|
|
|
|
}
|
|
|
|
switch(instr & 0x1f){
|
|
|
|
/* Stack */
|
|
|
|
case 0x00: /* LIT */ pull(u); break;
|
|
|
|
case 0x01: /* INC */ a = pop(u->src); push(u->src, a + 1); break;
|
|
|
|
case 0x02: /* POP */ pop(u->src); break;
|
|
|
|
case 0x03: /* DUP */ a = pop(u->src); push(u->src, a); push(u->src, a); break;
|
|
|
|
case 0x04: /* NIP */ a = pop(u->src); pop(u->src); push(u->src, a); break;
|
|
|
|
case 0x05: /* SWP */ a = pop(u->src), b = pop(u->src); push(u->src, a); push(u->src, b); break;
|
|
|
|
case 0x06: /* OVR */ a = pop(u->src), b = pop(u->src); push(u->src, b); push(u->src, a); push(u->src, b); break;
|
|
|
|
case 0x07: /* ROT */ a = pop(u->src), b = pop(u->src), c = pop(u->src); push(u->src, b); push(u->src, a); push(u->src, c); break;
|
|
|
|
/* Logic */
|
|
|
|
case 0x08: /* EQU */ a = pop(u->src), b = pop(u->src); push8(u->src, b == a); break;
|
|
|
|
case 0x09: /* NEQ */ a = pop(u->src), b = pop(u->src); push8(u->src, b != a); break;
|
|
|
|
case 0x0a: /* GTH */ a = pop(u->src), b = pop(u->src); push8(u->src, b > a); break;
|
|
|
|
case 0x0b: /* LTH */ a = pop(u->src), b = pop(u->src); push8(u->src, b < a); break;
|
|
|
|
case 0x0c: /* JMP */ a = pop(u->src); warp(u, a); break;
|
2021-10-17 20:16:58 +00:00
|
|
|
case 0x0d: /* JCN */ a = pop(u->src); if(pop8(u->src)) warp(u, a); break;
|
2021-08-30 01:18:52 +00:00
|
|
|
case 0x0e: /* JSR */ a = pop(u->src); push16(u->dst, u->ram.ptr); warp(u, a); break;
|
|
|
|
case 0x0f: /* STH */ a = pop(u->src); push(u->dst, a); break;
|
|
|
|
/* Memory */
|
|
|
|
case 0x10: /* LDZ */ a = pop8(u->src); push(u->src, peek(u->ram.dat, a)); break;
|
|
|
|
case 0x11: /* STZ */ a = pop8(u->src); b = pop(u->src); poke(u->ram.dat, a, b); break;
|
|
|
|
case 0x12: /* LDR */ a = pop8(u->src); push(u->src, peek(u->ram.dat, u->ram.ptr + (Sint8)a)); break;
|
|
|
|
case 0x13: /* STR */ a = pop8(u->src); b = pop(u->src); poke(u->ram.dat, u->ram.ptr + (Sint8)a, b); break;
|
|
|
|
case 0x14: /* LDA */ a = pop16(u->src); push(u->src, peek(u->ram.dat, a)); break;
|
|
|
|
case 0x15: /* STA */ a = pop16(u->src); b = pop(u->src); poke(u->ram.dat, a, b); break;
|
|
|
|
case 0x16: /* DEI */ a = pop8(u->src); push(u->src, devr(&u->dev[a >> 4], a)); break;
|
2021-11-04 16:38:32 +00:00
|
|
|
case 0x17: /* DEO */ a = pop8(u->src); b = pop(u->src); devw(&u->dev[a >> 4], a, b); break;
|
2021-08-30 01:18:52 +00:00
|
|
|
/* Arithmetic */
|
|
|
|
case 0x18: /* ADD */ a = pop(u->src), b = pop(u->src); push(u->src, b + a); break;
|
|
|
|
case 0x19: /* SUB */ a = pop(u->src), b = pop(u->src); push(u->src, b - a); break;
|
|
|
|
case 0x1a: /* MUL */ a = pop(u->src), b = pop(u->src); push(u->src, b * a); break;
|
|
|
|
case 0x1b: /* DIV */ a = pop(u->src), b = pop(u->src); if(a == 0) { u->src->error = 3; a = 1; } push(u->src, b / a); break;
|
|
|
|
case 0x1c: /* AND */ a = pop(u->src), b = pop(u->src); push(u->src, b & a); break;
|
|
|
|
case 0x1d: /* ORA */ a = pop(u->src), b = pop(u->src); push(u->src, b | a); break;
|
|
|
|
case 0x1e: /* EOR */ a = pop(u->src), b = pop(u->src); push(u->src, b ^ a); break;
|
2021-08-30 19:16:57 +00:00
|
|
|
case 0x1f: /* SFT */ a = pop8(u->src), b = pop(u->src); push(u->src, b >> (a & 0x0f) << ((a & 0xf0) >> 4)); break;
|
2021-08-29 21:53:31 +00:00
|
|
|
}
|
2021-09-10 15:52:07 +00:00
|
|
|
if(u->wst.error) return uxn_halt(u, u->wst.error, "Working-stack", instr);
|
|
|
|
if(u->rst.error) return uxn_halt(u, u->rst.error, "Return-stack", instr);
|
2021-08-05 03:06:46 +00:00
|
|
|
}
|
2021-02-08 22:18:01 +00:00
|
|
|
return 1;
|
2021-02-08 22:08:58 +00:00
|
|
|
}
|
|
|
|
|
2021-08-30 01:18:52 +00:00
|
|
|
/* clang-format on */
|
|
|
|
|
2021-01-29 19:17:59 +00:00
|
|
|
int
|
2021-08-01 21:46:43 +00:00
|
|
|
uxn_boot(Uxn *u)
|
2021-01-29 19:17:59 +00:00
|
|
|
{
|
2021-08-01 20:35:46 +00:00
|
|
|
unsigned int i;
|
2021-03-16 16:01:47 +00:00
|
|
|
char *cptr = (char *)u;
|
|
|
|
for(i = 0; i < sizeof(*u); i++)
|
2021-08-29 17:36:23 +00:00
|
|
|
cptr[i] = 0x00;
|
2021-02-09 05:59:46 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-02-10 01:22:52 +00:00
|
|
|
Device *
|
2021-11-04 17:13:01 +00:00
|
|
|
uxn_port(Uxn *u, Uint8 id, Uint8 (*deifn)(Device *d, Uint8 port), void (*deofn)(Device *d, Uint8 port))
|
2021-02-09 05:59:46 +00:00
|
|
|
{
|
2021-04-04 15:34:18 +00:00
|
|
|
Device *d = &u->dev[id];
|
2021-04-20 21:30:26 +00:00
|
|
|
d->addr = id * 0x10;
|
2021-04-27 20:10:58 +00:00
|
|
|
d->u = u;
|
2021-04-21 11:58:05 +00:00
|
|
|
d->mem = u->ram.dat;
|
2021-11-04 16:38:32 +00:00
|
|
|
d->dei = deifn;
|
|
|
|
d->deo = deofn;
|
2021-02-10 01:22:52 +00:00
|
|
|
return d;
|
2021-02-09 05:59:46 +00:00
|
|
|
}
|