2021-01-29 14:17:59 -05:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2021-01-29 14:35:59 -05:00
|
|
|
/*
|
2021-02-08 17:32:22 -05:00
|
|
|
Copyright (u) 2021 Devine Lu Linvega
|
2021-01-29 14:17:59 -05: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-02-08 17:18:01 -05:00
|
|
|
#include "uxn.h"
|
|
|
|
|
|
|
|
#pragma mark - Operations
|
|
|
|
|
|
|
|
/* clang-format off */
|
2021-03-26 11:35:45 -04:00
|
|
|
void push8(Stack *s, Uint8 a) { if (s->ptr == 0xff) { s->error = 2; return; } s->dat[s->ptr++] = a; }
|
|
|
|
Uint8 pop8(Stack *s) { if (s->ptr == 0) { s->error = 1; return 0; } return s->dat[--s->ptr]; }
|
|
|
|
Uint8 peek8(Stack *s, Uint8 a) { if (s->ptr < a + 1) s->error = 1; return s->dat[s->ptr - a - 1]; }
|
2021-04-20 13:31:50 -04:00
|
|
|
void mempoke8(Uxn *u, Uint16 a, Uint8 b) { u->ram.dat[a] = b; }
|
2021-03-27 14:04:05 -04:00
|
|
|
Uint8 mempeek8(Uxn *u, Uint16 a) { return u->ram.dat[a]; }
|
2021-02-15 13:12:44 -05:00
|
|
|
void push16(Stack *s, Uint16 a) { push8(s, a >> 8); push8(s, a); }
|
|
|
|
Uint16 pop16(Stack *s) { return pop8(s) + (pop8(s) << 8); }
|
|
|
|
Uint16 peek16(Stack *s, Uint8 a) { return peek8(s, a * 2) + (peek8(s, a * 2 + 1) << 8); }
|
2021-03-27 14:04:05 -04:00
|
|
|
void mempoke16(Uxn *u, Uint16 a, Uint16 b) { mempoke8(u, a, b >> 8); mempoke8(u, a + 1, b); }
|
|
|
|
Uint16 mempeek16(Uxn *u, Uint16 a) { return (mempeek8(u, a) << 8) + mempeek8(u, a + 1); }
|
2021-03-21 14:04:20 -04:00
|
|
|
/* Stack */
|
2021-04-04 23:24:11 -04:00
|
|
|
void op_brk(Uxn *u) { u->ram.ptr = 0; }
|
2021-03-11 15:19:59 -05:00
|
|
|
void op_nop(Uxn *u) { (void)u; }
|
2021-04-04 23:24:11 -04:00
|
|
|
void op_lit(Uxn *u) { push8(u->src, mempeek8(u, u->ram.ptr++)); }
|
2021-03-21 14:04:20 -04:00
|
|
|
void op_pop(Uxn *u) { pop8(u->src); }
|
|
|
|
void op_dup(Uxn *u) { push8(u->src, peek8(u->src, 0)); }
|
|
|
|
void op_swp(Uxn *u) { Uint8 b = pop8(u->src), a = pop8(u->src); push8(u->src, b); push8(u->src, a); }
|
|
|
|
void op_ovr(Uxn *u) { push8(u->src, peek8(u->src, 1)); }
|
|
|
|
void op_rot(Uxn *u) { Uint8 c = pop8(u->src), b = pop8(u->src), a = pop8(u->src); push8(u->src, b); push8(u->src, c); push8(u->src, a); }
|
2021-03-02 13:14:55 -05:00
|
|
|
/* Logic */
|
2021-03-21 13:30:43 -04:00
|
|
|
void op_equ(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); push8(u->src, b == a); }
|
|
|
|
void op_neq(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); push8(u->src, b != a); }
|
|
|
|
void op_gth(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); push8(u->src, b > a); }
|
|
|
|
void op_lth(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); push8(u->src, b < a); }
|
|
|
|
void op_gts(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); push8(u->src, (Sint8)b > (Sint8)a); }
|
|
|
|
void op_lts(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); push8(u->src, (Sint8)b < (Sint8)a); }
|
2021-04-20 13:31:50 -04:00
|
|
|
void op_ior(Uxn *u) { Uint8 a = pop8(u->src); push8(u->src, mempeek8(u, PAGE_DEVICE + a)); }
|
|
|
|
void op_iow(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); mempoke8(u, PAGE_DEVICE + a, b); u->dev[(a & 0xf0) >> 4].poke(u, PAGE_DEVICE + (a & 0xf0), a & 0x0f, b); }
|
2021-03-21 14:04:20 -04:00
|
|
|
/* Memory */
|
2021-03-24 19:17:18 -04:00
|
|
|
void op_pek(Uxn *u) { Uint8 a = pop8(u->src); push8(u->src, mempeek8(u, a)); }
|
|
|
|
void op_pok(Uxn *u) { Uint8 a = pop8(u->src); Uint8 b = pop8(u->src); mempoke8(u, a, b); }
|
2021-03-24 13:50:55 -04:00
|
|
|
void op_ldr(Uxn *u) { Uint8 a = pop8(u->src); push16(u->src, mempeek16(u, a)); }
|
|
|
|
void op_str(Uxn *u) { Uint8 a = pop8(u->src); Uint16 b = pop16(u->src); mempoke16(u, a, b); }
|
2021-03-26 23:16:48 -04:00
|
|
|
void op_jmp(Uxn *u) { Uint8 a = pop8(u->src); u->ram.ptr += (Sint8)a; }
|
|
|
|
void op_jnz(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); if (b) u->ram.ptr += (Sint8)a; }
|
|
|
|
void op_jsr(Uxn *u) { Uint8 a = pop8(u->src); push16(u->dst, u->ram.ptr); u->ram.ptr += (Sint8)a; }
|
2021-03-16 12:27:51 -04:00
|
|
|
void op_sth(Uxn *u) { Uint8 a = pop8(u->src); push8(u->dst, a); }
|
2021-02-08 17:18:01 -05:00
|
|
|
/* Arithmetic */
|
2021-03-16 00:29:44 -04:00
|
|
|
void op_add(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); push8(u->src, b + a); }
|
|
|
|
void op_sub(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); push8(u->src, b - a); }
|
|
|
|
void op_mul(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); push8(u->src, b * a); }
|
|
|
|
void op_div(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); push8(u->src, b / a); }
|
2021-03-21 13:30:43 -04:00
|
|
|
void op_and(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); push8(u->src, b & a); }
|
|
|
|
void op_ora(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); push8(u->src, b | a); }
|
|
|
|
void op_eor(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); push8(u->src, b ^ a); }
|
2021-03-28 15:37:37 -04:00
|
|
|
void op_sft(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); push8(u->src, b >> (a & 0x07) << ((a & 0x70) >> 4)); }
|
2021-03-21 14:04:20 -04:00
|
|
|
/* Stack */
|
2021-04-04 23:24:11 -04:00
|
|
|
void op_lit16(Uxn *u) { push16(u->src, mempeek16(u, u->ram.ptr++)); u->ram.ptr++; }
|
2021-03-21 14:04:20 -04:00
|
|
|
void op_pop16(Uxn *u) { pop16(u->src); }
|
|
|
|
void op_dup16(Uxn *u) { push16(u->src, peek16(u->src, 0)); }
|
|
|
|
void op_swp16(Uxn *u) { Uint16 b = pop16(u->src), a = pop16(u->src); push16(u->src, b); push16(u->src, a); }
|
|
|
|
void op_ovr16(Uxn *u) { push16(u->src, peek16(u->src, 1)); }
|
|
|
|
void op_rot16(Uxn *u) { Uint16 c = pop16(u->src), b = pop16(u->src), a = pop16(u->src); push16(u->src, b); push16(u->src, c); push16(u->src, a); }
|
2021-03-21 13:30:43 -04:00
|
|
|
/* Logic(16-bits) */
|
|
|
|
void op_equ16(Uxn *u) { Uint16 a = pop16(u->src), b = pop16(u->src); push8(u->src, b == a); }
|
|
|
|
void op_neq16(Uxn *u) { Uint16 a = pop16(u->src), b = pop16(u->src); push8(u->src, b != a); }
|
|
|
|
void op_gth16(Uxn *u) { Uint16 a = pop16(u->src), b = pop16(u->src); push8(u->src, b > a); }
|
|
|
|
void op_lth16(Uxn *u) { Uint16 a = pop16(u->src), b = pop16(u->src); push8(u->src, b < a); }
|
|
|
|
void op_gts16(Uxn *u) { Uint16 a = pop16(u->src), b = pop16(u->src); push8(u->src, (Sint16)b > (Sint16)a); }
|
|
|
|
void op_lts16(Uxn *u) { Uint16 a = pop16(u->src), b = pop16(u->src); push8(u->src, (Sint16)b < (Sint16)a); }
|
2021-04-20 13:31:50 -04:00
|
|
|
void op_ior16(Uxn *u) { Uint8 a = pop8(u->src); push16(u->src, mempeek16(u, PAGE_DEVICE + a)); }
|
|
|
|
void op_iow16(Uxn *u) { Uint8 a = pop8(u->src); Uint16 b = pop16(u->src); mempoke16(u, PAGE_DEVICE + a, b); u->dev[(a & 0xf0) >> 4].poke(u, PAGE_DEVICE + (a & 0xf0), (a & 0x0f)+1, b); }
|
2021-03-21 14:04:20 -04:00
|
|
|
/* Memory(16-bits) */
|
2021-03-21 14:28:59 -04:00
|
|
|
void op_pek16(Uxn *u) { Uint16 a = pop16(u->src); push8(u->src, mempeek8(u, a)); }
|
|
|
|
void op_pok16(Uxn *u) { Uint16 a = pop16(u->src); Uint8 b = pop8(u->src); mempoke8(u, a, b); }
|
2021-03-21 14:04:20 -04:00
|
|
|
void op_ldr16(Uxn *u) { Uint16 a = pop16(u->src); push16(u->src, mempeek16(u, a)); }
|
|
|
|
void op_str16(Uxn *u) { Uint16 a = pop16(u->src); Uint16 b = pop16(u->src); mempoke16(u, a, b); }
|
2021-03-26 23:16:48 -04:00
|
|
|
void op_jmp16(Uxn *u) { u->ram.ptr = pop16(u->src); }
|
|
|
|
void op_jnz16(Uxn *u) { Uint16 a = pop16(u->src); Uint8 b = pop8(u->src); if (b) u->ram.ptr = a; }
|
|
|
|
void op_jsr16(Uxn *u) { push16(u->dst, u->ram.ptr); u->ram.ptr = pop16(u->src); }
|
2021-03-16 12:27:51 -04:00
|
|
|
void op_sth16(Uxn *u) { Uint16 a = pop16(u->src); push16(u->dst, a); }
|
2021-02-08 17:18:01 -05:00
|
|
|
/* Arithmetic(16-bits) */
|
2021-03-16 00:29:44 -04:00
|
|
|
void op_add16(Uxn *u) { Uint16 a = pop16(u->src), b = pop16(u->src); push16(u->src, b + a); }
|
|
|
|
void op_sub16(Uxn *u) { Uint16 a = pop16(u->src), b = pop16(u->src); push16(u->src, b - a); }
|
|
|
|
void op_mul16(Uxn *u) { Uint16 a = pop16(u->src), b = pop16(u->src); push16(u->src, b * a); }
|
|
|
|
void op_div16(Uxn *u) { Uint16 a = pop16(u->src), b = pop16(u->src); push16(u->src, b / a); }
|
2021-03-21 13:30:43 -04:00
|
|
|
void op_and16(Uxn *u) { Uint16 a = pop16(u->src), b = pop16(u->src); push16(u->src, b & a); }
|
|
|
|
void op_ora16(Uxn *u) { Uint16 a = pop16(u->src), b = pop16(u->src); push16(u->src, b | a); }
|
|
|
|
void op_eor16(Uxn *u) { Uint16 a = pop16(u->src), b = pop16(u->src); push16(u->src, b ^ a); }
|
2021-03-28 15:37:37 -04:00
|
|
|
void op_sft16(Uxn *u) { Uint16 a = pop16(u->src), b = pop16(u->src); push16(u->src, b >> (a & 0x000f) << ((a & 0x00f0) >> 4)); }
|
2021-02-08 17:32:22 -05:00
|
|
|
|
|
|
|
void (*ops[])(Uxn *u) = {
|
2021-04-19 12:51:52 -04:00
|
|
|
op_brk, op_lit, op_nop, op_pop, op_dup, op_swp, op_ovr, op_rot,
|
2021-04-20 13:31:50 -04:00
|
|
|
op_equ, op_neq, op_gth, op_lth, op_gts, op_lts, op_ior, op_iow,
|
2021-03-26 23:16:48 -04:00
|
|
|
op_pek, op_pok, op_ldr, op_str, op_jmp, op_jnz, op_jsr, op_sth,
|
2021-03-21 13:24:44 -04:00
|
|
|
op_add, op_sub, op_mul, op_div, op_and, op_ora, op_eor, op_sft,
|
2021-02-10 14:06:36 -05:00
|
|
|
/* 16-bit */
|
2021-04-20 13:31:50 -04:00
|
|
|
op_brk, op_lit16, op_nop, op_pop16, op_dup16, op_swp16, op_ovr16, op_rot16,
|
|
|
|
op_equ16, op_neq16, op_gth16, op_lth16, op_gts16, op_lts16, op_ior16, op_iow16,
|
2021-03-26 23:16:48 -04:00
|
|
|
op_pek16, op_pok16, op_ldr16, op_str16, op_jmp16, op_jnz16, op_jsr16, op_sth16,
|
2021-03-21 13:24:44 -04:00
|
|
|
op_add16, op_sub16, op_mul16, op_div16, op_and16, op_ora16, op_eor16, op_sft16
|
2021-02-08 17:18:01 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
/* clang-format on */
|
|
|
|
|
2021-02-27 11:18:57 -05:00
|
|
|
#pragma mark - Core
|
|
|
|
|
2021-02-08 17:18:01 -05:00
|
|
|
int
|
2021-02-08 18:46:52 -05:00
|
|
|
haltuxn(Uxn *u, char *name, int id)
|
2021-02-08 17:18:01 -05:00
|
|
|
{
|
2021-04-04 11:34:18 -04:00
|
|
|
printf("Halted: %s#%04x, at 0x%04x\n", name, id, u->ram.ptr);
|
2021-04-04 23:35:52 -04:00
|
|
|
u->ram.ptr = 0;
|
2021-02-08 17:18:01 -05:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-03-26 11:35:45 -04:00
|
|
|
void
|
2021-02-08 18:46:52 -05:00
|
|
|
opcuxn(Uxn *u, Uint8 instr)
|
2021-02-08 17:18:01 -05:00
|
|
|
{
|
2021-03-28 15:38:43 -04:00
|
|
|
Uint8 op = instr & 0x3f, freturn = instr & 0x40;
|
2021-03-16 00:29:44 -04:00
|
|
|
u->src = freturn ? &u->rst : &u->wst;
|
|
|
|
u->dst = freturn ? &u->wst : &u->rst;
|
2021-03-26 11:35:45 -04:00
|
|
|
(*ops[op])(u);
|
2021-02-08 17:18:01 -05:00
|
|
|
}
|
|
|
|
|
2021-02-09 00:59:46 -05:00
|
|
|
int
|
2021-02-09 12:00:27 -05:00
|
|
|
stepuxn(Uxn *u, Uint8 instr)
|
2021-02-09 00:59:46 -05:00
|
|
|
{
|
2021-04-04 23:24:11 -04:00
|
|
|
opcuxn(u, instr);
|
2021-03-26 11:35:45 -04:00
|
|
|
if(u->wst.error)
|
|
|
|
return haltuxn(u, u->wst.error == 1 ? "Working-stack underflow" : "Working-stack overflow", instr);
|
|
|
|
if(u->rst.error)
|
|
|
|
return haltuxn(u, u->rst.error == 1 ? "Return-stack underflow" : "Return-stack overflow", instr);
|
|
|
|
return 1;
|
2021-02-09 00:59:46 -05:00
|
|
|
}
|
|
|
|
|
2021-02-08 17:18:01 -05:00
|
|
|
int
|
2021-02-08 18:46:52 -05:00
|
|
|
evaluxn(Uxn *u, Uint16 vec)
|
2021-02-08 17:18:01 -05:00
|
|
|
{
|
2021-02-08 18:46:52 -05:00
|
|
|
u->ram.ptr = vec;
|
2021-03-26 11:35:45 -04:00
|
|
|
u->wst.error = 0;
|
|
|
|
u->rst.error = 0;
|
2021-04-04 23:58:47 -04:00
|
|
|
while(u->ram.ptr)
|
2021-04-04 23:35:52 -04:00
|
|
|
if(!stepuxn(u, u->ram.dat[u->ram.ptr++]))
|
2021-02-09 12:00:27 -05:00
|
|
|
return 0;
|
2021-02-08 17:18:01 -05:00
|
|
|
return 1;
|
2021-02-08 17:08:58 -05:00
|
|
|
}
|
|
|
|
|
2021-01-29 14:17:59 -05:00
|
|
|
int
|
2021-02-08 18:46:52 -05:00
|
|
|
bootuxn(Uxn *u)
|
2021-01-29 14:17:59 -05:00
|
|
|
{
|
2021-02-09 00:59:46 -05:00
|
|
|
size_t i;
|
2021-03-16 12:01:47 -04:00
|
|
|
char *cptr = (char *)u;
|
|
|
|
for(i = 0; i < sizeof(*u); i++)
|
2021-02-09 00:59:46 -05:00
|
|
|
cptr[i] = 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
loaduxn(Uxn *u, char *filepath)
|
|
|
|
{
|
|
|
|
FILE *f;
|
|
|
|
if(!(f = fopen(filepath, "rb")))
|
2021-02-15 13:12:44 -05:00
|
|
|
return haltuxn(u, "Missing input rom.", 0);
|
2021-04-20 00:00:14 -04:00
|
|
|
fread(u->ram.dat + PAGE_PROGRAM, sizeof(u->ram.dat) - PAGE_PROGRAM, 1, f);
|
2021-03-28 13:19:06 -04:00
|
|
|
printf("Uxn loaded[%s].\n", filepath);
|
2021-02-08 17:18:01 -05:00
|
|
|
return 1;
|
2021-01-29 14:17:59 -05:00
|
|
|
}
|
2021-02-08 18:46:52 -05:00
|
|
|
|
2021-02-09 20:22:52 -05:00
|
|
|
Device *
|
2021-04-04 11:34:18 -04:00
|
|
|
portuxn(Uxn *u, Uint8 id, char *name, Uint8 (*pofn)(Uxn *u, Uint16 ptr, Uint8 b0, Uint8 b1))
|
2021-02-09 00:59:46 -05:00
|
|
|
{
|
2021-04-04 11:34:18 -04:00
|
|
|
Device *d = &u->dev[id];
|
|
|
|
d->addr = PAGE_DEVICE + id * 0x10;
|
2021-02-26 17:36:48 -05:00
|
|
|
d->poke = pofn;
|
2021-04-05 16:27:43 -04:00
|
|
|
printf("Device added #%02x: %s, at 0x%04x \n", id, name, d->addr);
|
2021-02-09 20:22:52 -05:00
|
|
|
return d;
|
2021-02-09 00:59:46 -05:00
|
|
|
}
|