uxn/uxn.c

288 lines
7.7 KiB
C
Raw Normal View History

2021-01-29 14:17:59 -05:00
#include <stdio.h>
2021-01-29 14:35:59 -05:00
/*
2021-01-29 14:17:59 -05: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.
*/
2021-01-30 00:56:19 -05:00
#define FLAG_HALT 0x01
2021-02-06 13:39:13 -05:00
#define FLAG_SHORT 0x02
2021-02-07 12:01:40 -05:00
#define FLAG_SIGN 0x04
2021-01-30 00:56:19 -05:00
#define FLAG_TRAPS 0x08
2021-01-29 15:14:37 -05:00
typedef unsigned char Uint8;
2021-02-01 23:21:27 -05:00
typedef unsigned short Uint16;
2021-01-29 15:14:37 -05:00
2021-02-02 12:40:41 -05:00
typedef struct {
Uint8 ptr;
Uint8 dat[256];
2021-02-07 13:46:51 -05:00
} Stack8;
2021-02-02 12:40:41 -05:00
2021-02-05 21:41:45 -05:00
typedef struct {
Uint8 ptr;
Uint16 dat[256];
} Stack16;
2021-02-02 12:40:41 -05:00
typedef struct {
Uint16 ptr;
Uint8 dat[65536];
} Memory;
2021-01-29 15:14:37 -05:00
typedef struct {
2021-02-01 23:21:27 -05:00
Uint8 literal, status;
2021-02-04 18:23:04 -05:00
Uint16 counter, vreset, vframe, verror;
2021-02-07 13:46:51 -05:00
Stack8 wst;
2021-02-05 21:41:45 -05:00
Stack16 rst;
2021-02-07 13:46:51 -05:00
Memory ram;
2021-01-29 15:14:37 -05:00
} Computer;
2021-01-30 17:25:48 -05:00
Computer cpu;
#pragma mark - Helpers
2021-01-30 00:56:19 -05:00
void
2021-01-30 17:25:48 -05:00
setflag(char flag, int b)
2021-01-30 00:56:19 -05:00
{
if(b)
2021-01-30 17:25:48 -05:00
cpu.status |= flag;
2021-01-30 00:56:19 -05:00
else
2021-01-30 17:25:48 -05:00
cpu.status &= (~flag);
2021-01-30 00:56:19 -05:00
}
int
2021-01-30 17:25:48 -05:00
getflag(char flag)
2021-01-30 00:56:19 -05:00
{
2021-01-30 17:25:48 -05:00
return cpu.status & flag;
2021-01-30 00:56:19 -05:00
}
2021-01-29 14:35:59 -05:00
void
2021-02-07 13:46:51 -05:00
echos(Stack8 *s, Uint8 len, char *name)
2021-01-29 14:35:59 -05:00
{
int i;
2021-02-07 15:13:38 -05:00
printf("\n%s\n", name);
2021-01-29 15:14:37 -05:00
for(i = 0; i < len; ++i) {
2021-01-29 14:35:59 -05:00
if(i % 16 == 0)
printf("\n");
2021-02-02 15:22:20 -05:00
printf("%02x%c", s->dat[i], s->ptr == i ? '<' : ' ');
2021-01-29 14:35:59 -05:00
}
2021-01-30 00:56:19 -05:00
printf("\n\n");
2021-01-29 14:35:59 -05:00
}
2021-02-04 16:49:03 -05:00
void
echom(Memory *m, Uint8 len, char *name)
{
int i;
2021-02-07 15:13:38 -05:00
printf("\n%s\n", name);
2021-02-04 16:49:03 -05:00
for(i = 0; i < len; ++i) {
if(i % 16 == 0)
printf("\n");
printf("%02x ", m->dat[i]);
}
printf("\n\n");
}
2021-01-31 00:31:49 -05:00
#pragma mark - Operations
/* clang-format off */
2021-02-05 17:01:34 -05:00
Uint16 bytes2short(Uint8 a, Uint8 b) { return (a << 8) + b; }
2021-02-05 14:57:37 -05:00
Uint8 rampeek8(Uint16 s) { return cpu.ram.dat[s] & 0xff; }
2021-02-07 13:46:51 -05:00
Uint8 mempeek8(Uint16 s) { return cpu.ram.dat[s]; }
Uint16 mempeek16(Uint16 s) { return (cpu.ram.dat[s] << 8) + (cpu.ram.dat[s+1] & 0xff); }
2021-02-05 00:39:03 -05:00
void wspush8(Uint8 b) { cpu.wst.dat[cpu.wst.ptr++] = b; }
2021-02-06 13:39:13 -05:00
void wspush16(Uint16 s) { wspush8(s >> 8); wspush8(s & 0xff); }
2021-02-05 00:39:03 -05:00
Uint8 wspop8(void) { return cpu.wst.dat[--cpu.wst.ptr]; }
Uint16 wspop16(void) { return wspop8() + (wspop8() << 8); }
2021-02-07 12:01:40 -05:00
Uint8 wspeek8(Uint8 o) { return cpu.wst.dat[cpu.wst.ptr - o]; }
Uint16 wspeek16(Uint8 o) { return bytes2short(cpu.wst.dat[cpu.wst.ptr - o], cpu.wst.dat[cpu.wst.ptr - o + 1]); }
2021-02-05 21:41:45 -05:00
Uint16 rspop16(void) { return cpu.rst.dat[--cpu.rst.ptr]; }
void rspush16(Uint16 a) { cpu.rst.dat[cpu.rst.ptr++] = a; }
2021-02-03 14:30:18 -05:00
2021-02-07 13:21:41 -05:00
/* I/O */
2021-01-31 00:31:49 -05:00
void op_brk() { setflag(FLAG_HALT, 1); }
2021-02-07 13:46:51 -05:00
void op_lit() { cpu.literal += cpu.ram.dat[cpu.ram.ptr++]; }
2021-02-07 13:21:41 -05:00
void op_nop() { }
void op_ldr() { wspush8(cpu.ram.dat[wspop16()]); }
void op_str() { cpu.ram.dat[wspop16()] = wspop8(); }
/* Logic */
2021-02-07 13:46:51 -05:00
void op_jmu() { cpu.ram.ptr = wspop16(); }
2021-02-07 13:21:41 -05:00
void op_jmc() { Uint8 a = wspop8(); if(a) op_jmu(); }
2021-02-07 13:46:51 -05:00
void op_jsu() { rspush16(cpu.ram.ptr); cpu.ram.ptr = wspop16(); }
2021-02-07 13:21:41 -05:00
void op_jsc() { Uint8 a = wspop8(); if(a) op_jsu(); }
2021-02-07 13:46:51 -05:00
void op_rtu() { cpu.ram.ptr = rspop16(); }
2021-02-07 13:21:41 -05:00
void op_rtc() { /* TODO */ }
/* Stack */
void op_pop() { wspop8(); }
2021-02-07 12:01:40 -05:00
void op_dup() { wspush8(wspeek8(1)); }
2021-02-05 00:39:03 -05:00
void op_swp() { Uint8 b = wspop8(), a = wspop8(); wspush8(b); wspush8(a); }
2021-02-07 12:01:40 -05:00
void op_ovr() { Uint8 a = wspeek8(2); wspush8(a); }
2021-02-05 00:39:03 -05:00
void op_rot() { Uint8 c = wspop8(),b = wspop8(),a = wspop8(); wspush8(b); wspush8(c); wspush8(a); }
2021-02-07 12:01:40 -05:00
void op_and() { Uint8 a = wspop8(), b = wspop8(); wspush8(a & b); }
void op_ora() { Uint8 a = wspop8(), b = wspop8(); wspush8(a | b); }
void op_rol() { Uint8 a = wspop8(), b = wspop8(); wspush8(a << b); }
2021-02-07 13:21:41 -05:00
/* Arithmetic */
2021-02-07 15:13:38 -05:00
void op_add() { Uint8 a = wspop8(), b = wspop8(); wspush8(b + a); }
void op_sub() { Uint8 a = wspop8(), b = wspop8(); wspush8(b - a); }
void op_mul() { Uint8 a = wspop8(), b = wspop8(); wspush8(b * a); }
void op_div() { Uint8 a = wspop8(), b = wspop8(); wspush8(b / a); }
void op_equ() { Uint8 a = wspop8(), b = wspop8(); wspush8(b == a); }
void op_neq() { Uint8 a = wspop8(), b = wspop8(); wspush8(b != a); }
void op_gth() { Uint8 a = wspop8(), b = wspop8(); wspush8(b > a); }
void op_lth() { Uint8 a = wspop8(), b = wspop8(); wspush8(b < a); }
2021-02-07 13:21:41 -05:00
/* Stack(16-bits) */
void op_pop16() { wspop16(); }
2021-02-07 12:01:40 -05:00
void op_dup16() { wspush16(wspeek16(2)); }
void op_swp16() { Uint16 b = wspop16(), a = wspop16(); wspush16(b); wspush16(a); }
void op_ovr16() { Uint16 a = wspeek16(4); wspush16(a); }
void op_rot16() { Uint16 c = wspop16(), b = wspop16(), a = wspop16(); wspush16(b); wspush16(c); wspush16(a); }
void op_and16() { Uint16 a = wspop16(), b = wspop16(); wspush16(a & b); }
void op_ora16() { Uint16 a = wspop16(), b = wspop16(); wspush16(a | b); }
void op_rol16() { Uint16 a = wspop16(), b = wspop16(); wspush16(a << b); }
2021-02-07 13:21:41 -05:00
/* Arithmetic(16-bits) */
2021-02-07 15:13:38 -05:00
void op_add16() { Uint16 a = wspop16(), b = wspop16(); wspush16(b + a); }
void op_sub16() { Uint16 a = wspop16(), b = wspop16(); wspush16(b - a); }
void op_mul16() { Uint16 a = wspop16(), b = wspop16(); wspush16(b * a); }
void op_div16() { Uint16 a = wspop16(), b = wspop16(); wspush16(b / a); }
void op_equ16() { Uint16 a = wspop16(), b = wspop16(); wspush8(b == a); }
void op_neq16() { Uint16 a = wspop16(), b = wspop16(); wspush8(b != a); }
void op_gth16() { Uint16 a = wspop16(), b = wspop16(); wspush8(b > a); }
void op_lth16() { Uint16 a = wspop16(), b = wspop16(); wspush8(b < a); }
2021-02-06 23:18:49 -05:00
2021-02-07 13:21:41 -05:00
void (*ops[])() = {
2021-02-07 13:46:51 -05:00
op_brk, op_lit, op_nop, op_nop, op_nop, op_nop, op_ldr, op_str,
2021-02-07 13:21:41 -05:00
op_jmu, op_jmc, op_jsu, op_jsc, op_rtu, op_rtc, op_nop, op_nop,
op_pop, op_dup, op_swp, op_ovr, op_rot, op_and, op_ora, op_rol,
op_add, op_sub, op_mul, op_div, op_equ, op_neq, op_gth, op_lth,
op_pop16, op_dup16, op_swp16, op_ovr16, op_rot16, op_and16, op_ora16, op_rol16,
op_add16, op_sub16, op_mul16, op_div16, op_equ16, op_neq16, op_gth16, op_lth16
2021-02-06 23:18:49 -05:00
};
2021-02-07 15:13:38 -05:00
Uint8 opr[][2] = {
2021-02-07 13:21:41 -05:00
{0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0},
{0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0},
{0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0},
{0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0},
{0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0},
{0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}
2021-02-01 23:21:27 -05:00
};
2021-01-31 00:31:49 -05:00
/* clang-format on */
2021-01-29 15:14:37 -05:00
int
2021-02-06 13:39:13 -05:00
error(char *name, int id)
2021-01-29 15:14:37 -05:00
{
2021-02-06 13:39:13 -05:00
printf("Error: %s[%04x], at 0x%04x\n", name, id, cpu.counter);
2021-01-30 00:56:19 -05:00
return 0;
}
2021-02-05 14:57:37 -05:00
int
device1(Uint8 *read, Uint8 *write)
{
printf("%c", *write);
*write = 0;
2021-02-05 17:01:34 -05:00
(void)read;
2021-02-05 14:57:37 -05:00
return 0;
}
2021-02-06 13:39:13 -05:00
void
2021-02-06 23:18:49 -05:00
opc(Uint8 src, Uint8 *op)
2021-02-06 13:39:13 -05:00
{
*op = src;
*op &= ~(1 << 5);
*op &= ~(1 << 6);
*op &= ~(1 << 7);
}
2021-02-01 23:21:27 -05:00
int
2021-02-04 18:38:04 -05:00
eval(void)
2021-01-30 00:56:19 -05:00
{
2021-02-07 13:46:51 -05:00
Uint8 instr = cpu.ram.dat[cpu.ram.ptr++];
2021-02-06 23:18:49 -05:00
Uint8 op;
2021-02-06 13:39:13 -05:00
/* when literal */
2021-01-30 17:25:48 -05:00
if(cpu.literal > 0) {
2021-02-05 00:39:03 -05:00
wspush8(instr);
2021-01-30 17:25:48 -05:00
cpu.literal--;
2021-02-01 23:21:27 -05:00
return 1;
2021-01-30 00:56:19 -05:00
}
2021-02-06 13:39:13 -05:00
/* when opcode */
2021-02-06 23:18:49 -05:00
opc(instr, &op);
2021-02-06 13:39:13 -05:00
setflag(FLAG_SHORT, (instr >> 5) & 1);
2021-02-07 12:01:40 -05:00
setflag(FLAG_SIGN, (instr >> 6) & 1);
2021-02-06 13:39:13 -05:00
if((instr >> 7) & 1)
printf("Unused flag: %02x\n", instr);
if(cpu.wst.ptr < opr[op][0])
return error("Stack underflow", op);
2021-02-06 23:18:49 -05:00
if(getflag(FLAG_SHORT))
2021-02-07 13:21:41 -05:00
op += 16;
(*ops[op])();
2021-02-07 13:46:51 -05:00
/* experimental */
if(cpu.ram.dat[0xfff1])
device1(&cpu.ram.dat[0xfff0], &cpu.ram.dat[0xfff1]);
2021-02-01 23:21:27 -05:00
cpu.counter++;
return 1;
2021-01-29 15:14:37 -05:00
}
2021-02-06 13:39:13 -05:00
int
2021-02-07 13:46:51 -05:00
load(FILE *f)
{
fread(cpu.ram.dat, sizeof(cpu.ram.dat), 1, f);
return 1;
}
void
debug(void)
{
printf("ended @ %d steps | hf: %x sf: %x cf: %x tf: %x\n",
cpu.counter,
getflag(FLAG_HALT) != 0,
getflag(FLAG_SHORT) != 0,
getflag(FLAG_SIGN) != 0,
getflag(FLAG_TRAPS) != 0);
}
int
boot(void)
2021-01-29 15:14:37 -05:00
{
2021-02-05 00:39:03 -05:00
cpu.vreset = mempeek16(0xfffa);
cpu.vframe = mempeek16(0xfffc);
cpu.verror = mempeek16(0xfffe);
2021-02-04 18:38:04 -05:00
/* eval reset */
2021-02-07 13:46:51 -05:00
cpu.ram.ptr = cpu.vreset;
setflag(FLAG_HALT, 0);
while(!(cpu.status & FLAG_HALT) && eval())
;
2021-02-04 18:38:04 -05:00
/*eval frame */
2021-02-07 13:46:51 -05:00
cpu.ram.ptr = cpu.vframe;
2021-02-05 00:39:03 -05:00
setflag(FLAG_HALT, 0);
2021-02-04 18:38:04 -05:00
while(!(cpu.status & FLAG_HALT) && eval())
2021-02-01 23:21:27 -05:00
;
2021-02-06 13:39:13 -05:00
return 1;
2021-01-29 15:14:37 -05:00
}
2021-01-29 14:17:59 -05:00
int
main(int argc, char *argv[])
{
2021-01-29 15:14:37 -05:00
FILE *f;
if(argc < 2)
2021-02-06 13:39:13 -05:00
return error("No input.", 0);
2021-01-29 15:14:37 -05:00
if(!(f = fopen(argv[1], "rb")))
2021-02-06 13:39:13 -05:00
return error("Missing input.", 0);
2021-02-07 13:46:51 -05:00
if(!load(f))
return error("Load error", 0);
if(!boot())
return error("Boot error", 0);
2021-01-29 15:14:37 -05:00
/* print result */
2021-02-04 16:49:03 -05:00
echos(&cpu.wst, 0x40, "stack");
echom(&cpu.ram, 0x40, "ram");
2021-02-07 13:46:51 -05:00
debug();
2021-01-29 14:17:59 -05:00
return 0;
}