2021-01-29 19:17:59 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2021-01-29 19:35:59 +00:00
|
|
|
/*
|
2021-02-08 22:32:22 +00:00
|
|
|
Copyright (u) 2021 Devine Lu Linvega
|
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-02-08 22:18:01 +00:00
|
|
|
#include "uxn.h"
|
|
|
|
|
|
|
|
#pragma mark - Operations
|
|
|
|
|
|
|
|
/* clang-format off */
|
2021-02-11 18:34:31 +00:00
|
|
|
void setflag(Uint8 *a, char flag, int b) { if(b) *a |= flag; else *a &= (~flag); }
|
|
|
|
int getflag(Uint8 *a, char flag) { return *a & flag; }
|
2021-02-27 00:38:20 +00:00
|
|
|
Uint8 devpoke8(Uxn *u, Uint8 id, Uint8 b0, Uint8 b1){ return id < u->devices ? u->dev[id].poke(u->ram.dat, 0xff00 + id * 0x10, b0, b1) : b1; }
|
|
|
|
void mempoke8(Uxn *u, Uint16 a, Uint8 b) { u->ram.dat[a] = a >= 0xff00 ? devpoke8(u, (a & 0xff) >> 4, a & 0xf, b) : b; }
|
2021-03-05 18:06:09 +00:00
|
|
|
Uint8 mempeek8(Uxn *u, Uint16 a) { return u->ram.dat[a]; }
|
2021-02-26 18:16:35 +00: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-02-15 18:12:44 +00:00
|
|
|
void push8(Stack *s, Uint8 a) { s->dat[s->ptr++] = a; }
|
|
|
|
Uint8 pop8(Stack *s) { return s->dat[--s->ptr]; }
|
|
|
|
Uint8 peek8(Stack *s, Uint8 a) { return s->dat[s->ptr - a - 1]; }
|
|
|
|
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-02-08 22:18:01 +00:00
|
|
|
/* I/O */
|
2021-02-14 19:51:36 +00:00
|
|
|
void op_brk(Uxn *u) { setflag(&u->status, FLAG_HALT, 1); }
|
2021-02-14 00:37:03 +00:00
|
|
|
void op_lit(Uxn *u) { u->literal += 1; }
|
2021-03-11 20:19:59 +00:00
|
|
|
void op_nop(Uxn *u) { (void)u; }
|
2021-03-16 04:29:44 +00:00
|
|
|
void op_jmp(Uxn *u) { Uint8 a = pop8(u->src); 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; }
|
|
|
|
void op_ldr(Uxn *u) { Uint16 a = pop16(u->src); push8(u->src, mempeek8(u, a)); }
|
|
|
|
void op_str(Uxn *u) { Uint16 a = pop16(u->src); Uint8 b = pop8(u->src); mempoke8(u, a, b); }
|
2021-03-02 18:14:55 +00:00
|
|
|
/* Logic */
|
2021-03-16 04:29:44 +00:00
|
|
|
void op_and(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); push8(u->src, b & a); }
|
2021-03-19 03:53:47 +00:00
|
|
|
void op_ora(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); push8(u->src, b | a); }
|
2021-03-20 04:13:09 +00:00
|
|
|
void op_eor(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); push8(u->src, b ^ a); }
|
2021-03-20 04:22:51 +00:00
|
|
|
void op_sft(Uxn *u) { Uint8 a = pop8(u->src), b = pop8(u->src); Uint8 left = (a & 0xf0) >> 4; Uint8 right = (a & 0x0f); push8(u->src, b >> (right % 8) << (left % 8)); }
|
2021-02-08 22:18:01 +00:00
|
|
|
/* Stack */
|
2021-03-16 04:29:44 +00: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); }
|
|
|
|
void op_cln(Uxn *u) { push8(u->src, peek8(u->dst, 0)); }
|
2021-03-16 16:27:51 +00:00
|
|
|
void op_sth(Uxn *u) { Uint8 a = pop8(u->src); push8(u->dst, a); }
|
2021-02-08 22:18:01 +00:00
|
|
|
/* Arithmetic */
|
2021-03-16 04:29:44 +00: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); }
|
|
|
|
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); }
|
2021-02-10 19:06:36 +00:00
|
|
|
/* --- */
|
2021-02-14 00:37:03 +00:00
|
|
|
void op_lit16(Uxn *u) { u->literal += 2; }
|
2021-03-16 04:29:44 +00:00
|
|
|
void op_nop16(Uxn *u) { printf("%04x\n", pop16(u->src)); }
|
|
|
|
void op_jmp16(Uxn *u) { u->ram.ptr = pop16(u->src); }
|
|
|
|
void op_jsr16(Uxn *u) { push16(u->dst, u->ram.ptr); u->ram.ptr = pop16(u->src); }
|
|
|
|
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-20 04:13:09 +00:00
|
|
|
/* Logic */
|
2021-03-16 04:29:44 +00:00
|
|
|
void op_and16(Uxn *u) { Uint16 a = pop16(u->src), b = pop16(u->src); push16(u->src, b & a); }
|
2021-03-19 03:53:47 +00:00
|
|
|
void op_ora16(Uxn *u) { Uint16 a = pop16(u->src), b = pop16(u->src); push16(u->src, b | a); }
|
2021-03-20 04:13:09 +00:00
|
|
|
void op_eor16(Uxn *u) { Uint16 a = pop16(u->src), b = pop16(u->src); push16(u->src, b ^ a); }
|
2021-03-20 04:22:51 +00:00
|
|
|
void op_sft16(Uxn *u) { Uint16 a = pop16(u->src), b = pop16(u->src); Uint8 left = (a & 0x00f0) >> 4; Uint8 right = (a & 0x000f); push16(u->src, b >> (right % 16) << (left % 16)); }
|
2021-02-08 22:18:01 +00:00
|
|
|
/* Stack(16-bits) */
|
2021-03-16 04:29:44 +00: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); }
|
|
|
|
void op_cln16(Uxn *u) { push16(u->src, peek16(u->dst, 0)); }
|
2021-03-16 16:27:51 +00:00
|
|
|
void op_sth16(Uxn *u) { Uint16 a = pop16(u->src); push16(u->dst, a); }
|
2021-02-08 22:18:01 +00:00
|
|
|
/* Arithmetic(16-bits) */
|
2021-03-16 04:29:44 +00: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); }
|
|
|
|
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); }
|
2021-02-08 22:32:22 +00:00
|
|
|
|
|
|
|
void (*ops[])(Uxn *u) = {
|
2021-03-16 16:27:51 +00:00
|
|
|
op_brk, op_nop, op_lit, op_ldr, op_str, op_nop, op_jmp, op_jsr,
|
2021-03-20 04:13:09 +00:00
|
|
|
op_equ, op_neq, op_gth, op_lth, op_and, op_ora, op_eor, op_sft,
|
2021-03-16 16:27:51 +00:00
|
|
|
op_pop, op_dup, op_swp, op_ovr, op_rot, op_nop, op_cln, op_sth,
|
2021-03-20 04:13:09 +00:00
|
|
|
op_add, op_sub, op_mul, op_div, op_nop, op_nop, op_nop, op_eor,
|
2021-02-10 19:06:36 +00:00
|
|
|
/* 16-bit */
|
2021-03-16 16:27:51 +00:00
|
|
|
op_brk, op_nop16, op_lit16, op_ldr16, op_str16, op_nop, op_jmp16, op_jsr16,
|
2021-03-20 04:13:09 +00:00
|
|
|
op_equ16, op_neq16, op_gth16, op_lth16, op_and16, op_ora16, op_eor16, op_sft16,
|
2021-03-16 16:27:51 +00:00
|
|
|
op_pop16, op_dup16, op_swp16, op_ovr16, op_rot16, op_nop, op_cln16, op_sth16,
|
2021-03-20 04:13:09 +00:00
|
|
|
op_add16, op_sub16, op_mul16, op_div16, op_nop, op_nop, op_nop, op_nop
|
2021-02-08 22:18:01 +00:00
|
|
|
};
|
|
|
|
|
2021-03-14 17:40:13 +00:00
|
|
|
Uint8 opr[][4] = { /* wstack-/+ rstack-/+ */
|
2021-03-19 20:41:59 +00:00
|
|
|
{0,0,0,0}, {0,0,0,0}, {0,0,0,0}, {2,1,0,0}, {3,0,0,0}, {1,0,0,0}, {1,0,0,0}, {1,0,0,2},
|
2021-03-14 17:40:13 +00:00
|
|
|
{2,1,0,0}, {2,1,0,0}, {2,1,0,0}, {2,1,0,0}, {2,1,0,0}, {2,1,0,0}, {2,1,0,0}, {2,1,0,0},
|
2021-03-17 17:18:43 +00:00
|
|
|
{1,0,0,0}, {0,2,0,0}, {2,2,0,0}, {2,3,0,0}, {3,3,0,0}, {0,0,0,0}, {0,0,0,1}, {0,1,1,0},
|
2021-03-14 21:26:17 +00:00
|
|
|
{2,1,0,0}, {2,1,0,0}, {2,1,0,0}, {2,1,0,0}, {0,0,0,0}, {0,0,0,0}, {0,0,0,0}, {0,2,0,0},
|
2021-02-10 19:06:36 +00:00
|
|
|
/* 16-bit */
|
2021-03-19 20:41:59 +00:00
|
|
|
{0,0,0,0}, {0,0,0,0}, {0,0,0,0}, {2,2,0,0}, {4,0,0,0}, {2,0,0,0}, {2,0,0,0}, {2,0,0,2}, /* TODO */
|
2021-03-16 17:54:07 +00:00
|
|
|
{4,2,0,0}, {4,2,0,0}, {4,2,0,0}, {4,2,0,0}, {4,2,0,0}, {4,2,0,0}, {4,2,0,0}, {4,2,0,0}, /* TODO */
|
2021-03-17 17:18:43 +00:00
|
|
|
{0,2,0,0}, {0,2,0,0}, {1,1,0,0}, {4,6,0,0}, {6,6,0,0}, {0,0,0,0}, {0,0,0,2}, {0,2,2,0}, /* TODO */
|
2021-03-14 21:26:17 +00:00
|
|
|
{4,2,0,0}, {4,2,0,0}, {4,2,0,0}, {4,2,0,0}, {0,0,0,0}, {0,0,0,0}, {0,0,0,0}, {0,2,0,0}
|
2021-02-08 22:18:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* clang-format on */
|
|
|
|
|
2021-02-27 16:18:57 +00:00
|
|
|
#pragma mark - Core
|
|
|
|
|
2021-02-08 22:18:01 +00:00
|
|
|
int
|
2021-02-08 23:46:52 +00:00
|
|
|
haltuxn(Uxn *u, char *name, int id)
|
2021-02-08 22:18:01 +00:00
|
|
|
{
|
2021-02-15 18:12:44 +00:00
|
|
|
printf("Halted: %s#%04x, at 0x%04x\n", name, id, u->counter);
|
2021-02-08 22:18:01 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2021-02-08 23:46:52 +00:00
|
|
|
lituxn(Uxn *u, Uint8 instr)
|
2021-02-08 22:18:01 +00:00
|
|
|
{
|
2021-02-08 22:32:22 +00:00
|
|
|
if(u->wst.ptr >= 255)
|
2021-02-08 23:46:52 +00:00
|
|
|
return haltuxn(u, "Stack overflow", instr);
|
2021-02-11 18:15:26 +00:00
|
|
|
push8(&u->wst, instr);
|
2021-02-08 22:32:22 +00:00
|
|
|
u->literal--;
|
2021-02-08 22:18:01 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2021-02-08 23:46:52 +00:00
|
|
|
opcuxn(Uxn *u, Uint8 instr)
|
2021-02-08 22:18:01 +00:00
|
|
|
{
|
2021-03-16 04:29:44 +00:00
|
|
|
Uint8 op = instr & 0x1f, fcond, freturn;
|
2021-02-08 22:32:22 +00:00
|
|
|
setflag(&u->status, FLAG_SHORT, (instr >> 5) & 1);
|
2021-03-16 04:29:44 +00:00
|
|
|
setflag(&u->status, FLAG_RETURN, (instr >> 6) & 1);
|
2021-02-08 22:32:22 +00:00
|
|
|
setflag(&u->status, FLAG_COND, (instr >> 7) & 1);
|
2021-03-15 22:27:43 +00:00
|
|
|
fcond = getflag(&u->status, FLAG_COND);
|
2021-03-16 04:29:44 +00:00
|
|
|
freturn = getflag(&u->status, FLAG_RETURN);
|
|
|
|
u->src = freturn ? &u->rst : &u->wst;
|
|
|
|
u->dst = freturn ? &u->wst : &u->rst;
|
2021-02-08 22:32:22 +00:00
|
|
|
if(getflag(&u->status, FLAG_SHORT))
|
2021-02-10 19:06:36 +00:00
|
|
|
op += 32;
|
2021-03-16 04:29:44 +00:00
|
|
|
if(u->src->ptr < opr[op][0] || (fcond && u->src->ptr < 1))
|
2021-03-14 17:40:13 +00:00
|
|
|
return haltuxn(u, "Working-stack underflow", op);
|
2021-03-16 04:29:44 +00:00
|
|
|
if(u->src->ptr + opr[op][1] - opr[op][0] >= 255)
|
2021-03-14 17:40:13 +00:00
|
|
|
return haltuxn(u, "Working-stack overflow", instr);
|
2021-03-16 04:29:44 +00:00
|
|
|
if(u->dst->ptr < opr[op][2])
|
2021-03-14 17:40:13 +00:00
|
|
|
return haltuxn(u, "Return-stack underflow", op);
|
2021-03-16 04:29:44 +00:00
|
|
|
if(u->dst->ptr + opr[op][3] - opr[op][2] >= 255)
|
2021-03-14 17:40:13 +00:00
|
|
|
return haltuxn(u, "Return-stack overflow", instr);
|
2021-03-15 22:27:43 +00:00
|
|
|
if(!fcond || (fcond && pop8(&u->wst)))
|
2021-02-08 22:32:22 +00:00
|
|
|
(*ops[op])(u);
|
2021-03-16 17:54:07 +00:00
|
|
|
else
|
|
|
|
u->src->ptr -= opr[op][freturn ? 2 : 0] - opr[op][freturn ? 3 : 1];
|
2021-02-08 22:18:01 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-02-09 05:59:46 +00:00
|
|
|
int
|
2021-02-09 17:00:27 +00:00
|
|
|
stepuxn(Uxn *u, Uint8 instr)
|
2021-02-09 05:59:46 +00:00
|
|
|
{
|
|
|
|
if(u->literal > 0)
|
|
|
|
return lituxn(u, instr);
|
|
|
|
else
|
|
|
|
return opcuxn(u, instr);
|
|
|
|
}
|
|
|
|
|
2021-02-08 22:18:01 +00:00
|
|
|
int
|
2021-02-08 23:46:52 +00:00
|
|
|
evaluxn(Uxn *u, Uint16 vec)
|
2021-02-08 22:18:01 +00:00
|
|
|
{
|
2021-02-10 19:59:26 +00:00
|
|
|
u->literal = 0;
|
2021-02-08 23:46:52 +00:00
|
|
|
u->ram.ptr = vec;
|
|
|
|
setflag(&u->status, FLAG_HALT, 0);
|
2021-02-09 17:00:27 +00:00
|
|
|
while(!(u->status & FLAG_HALT)) {
|
|
|
|
Uint8 instr = u->ram.dat[u->ram.ptr++];
|
|
|
|
if(!stepuxn(u, instr))
|
|
|
|
return 0;
|
|
|
|
u->counter++;
|
|
|
|
}
|
2021-02-08 22:18:01 +00:00
|
|
|
return 1;
|
2021-02-08 22:08:58 +00:00
|
|
|
}
|
|
|
|
|
2021-01-29 19:17:59 +00:00
|
|
|
int
|
2021-02-08 23:46:52 +00:00
|
|
|
bootuxn(Uxn *u)
|
2021-01-29 19:17:59 +00:00
|
|
|
{
|
2021-02-09 05:59:46 +00:00
|
|
|
size_t i;
|
2021-03-16 16:01:47 +00:00
|
|
|
char *cptr = (char *)u;
|
|
|
|
for(i = 0; i < sizeof(*u); i++)
|
2021-02-09 05:59:46 +00:00
|
|
|
cptr[i] = 0;
|
2021-03-17 17:18:43 +00:00
|
|
|
|
2021-02-09 05:59:46 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
loaduxn(Uxn *u, char *filepath)
|
|
|
|
{
|
|
|
|
FILE *f;
|
|
|
|
if(!(f = fopen(filepath, "rb")))
|
2021-02-15 18:12:44 +00:00
|
|
|
return haltuxn(u, "Missing input rom.", 0);
|
2021-02-09 05:59:46 +00:00
|
|
|
fread(u->ram.dat, sizeof(u->ram.dat), 1, f);
|
2021-02-28 19:17:32 +00:00
|
|
|
u->vreset = mempeek16(u, 0xfff0);
|
|
|
|
u->vframe = mempeek16(u, 0xfff2);
|
|
|
|
u->verror = mempeek16(u, 0xfff4);
|
2021-02-09 05:59:46 +00:00
|
|
|
printf("Uxn loaded[%s] vrst:%04x vfrm:%04x verr:%04x.\n",
|
|
|
|
filepath,
|
|
|
|
u->vreset,
|
|
|
|
u->vframe,
|
|
|
|
u->verror);
|
2021-02-08 22:18:01 +00:00
|
|
|
return 1;
|
2021-01-29 19:17:59 +00:00
|
|
|
}
|
2021-02-08 23:46:52 +00:00
|
|
|
|
2021-02-10 01:22:52 +00:00
|
|
|
Device *
|
2021-03-05 18:06:09 +00:00
|
|
|
portuxn(Uxn *u, char *name, Uint8 (*pofn)(Uint8 *m, Uint16 ptr, Uint8 b0, Uint8 b1))
|
2021-02-09 05:59:46 +00:00
|
|
|
{
|
|
|
|
Device *d = &u->dev[u->devices++];
|
2021-02-27 03:46:56 +00:00
|
|
|
d->addr = 0xff00 + (u->devices - 1) * 0x10;
|
2021-02-26 22:36:48 +00:00
|
|
|
d->poke = pofn;
|
2021-02-27 03:46:56 +00:00
|
|
|
printf("Device #%d: %s, at 0x%04x \n", u->devices - 1, name, d->addr);
|
2021-02-10 01:22:52 +00:00
|
|
|
return d;
|
2021-02-09 05:59:46 +00:00
|
|
|
}
|