0
0
Fork 0
mirror of https://git.sr.ht/~rabbits/uxn synced 2024-11-05 13:55:06 +00:00
uxn/uxn.c

221 lines
4.7 KiB
C
Raw Normal View History

2021-01-29 19:17:59 +00:00
#include <stdio.h>
2021-01-29 19:35:59 +00:00
/*
2021-01-29 19:17:59 +00: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 05:56:19 +00:00
#define FLAG_HALT 0x01
#define FLAG_ZERO 0x02
#define FLAG_CARRY 0x04
#define FLAG_TRAPS 0x08
2021-01-29 20:14:37 +00:00
typedef unsigned char Uint8;
2021-02-02 04:21:27 +00:00
typedef unsigned short Uint16;
2021-01-29 20:14:37 +00:00
2021-02-02 17:40:41 +00:00
typedef struct {
Uint8 ptr;
Uint8 dat[256];
} Stack;
typedef struct {
Uint16 ptr;
Uint8 dat[65536];
} Memory;
2021-01-29 20:14:37 +00:00
typedef struct {
2021-02-02 04:21:27 +00:00
Uint8 literal, status;
Uint16 counter;
2021-02-02 17:40:41 +00:00
Stack wst, rst;
Memory rom, ram;
2021-01-29 20:14:37 +00:00
} Computer;
2021-01-30 22:25:48 +00:00
Computer cpu;
#pragma mark - Helpers
2021-01-30 05:56:19 +00:00
void
2021-01-30 22:25:48 +00:00
setflag(char flag, int b)
2021-01-30 05:56:19 +00:00
{
if(b)
2021-01-30 22:25:48 +00:00
cpu.status |= flag;
2021-01-30 05:56:19 +00:00
else
2021-01-30 22:25:48 +00:00
cpu.status &= (~flag);
2021-01-30 05:56:19 +00:00
}
int
2021-01-30 22:25:48 +00:00
getflag(char flag)
2021-01-30 05:56:19 +00:00
{
2021-01-30 22:25:48 +00:00
return cpu.status & flag;
2021-01-30 05:56:19 +00:00
}
2021-01-29 19:35:59 +00:00
void
2021-02-02 20:22:20 +00:00
echo(Stack *s, Uint8 len, char *name)
2021-01-29 19:35:59 +00:00
{
int i;
2021-01-29 21:59:16 +00:00
printf("%s\n", name);
2021-01-29 20:14:37 +00:00
for(i = 0; i < len; ++i) {
2021-01-29 19:35:59 +00:00
if(i % 16 == 0)
printf("\n");
2021-02-02 20:22:20 +00:00
printf("%02x%c", s->dat[i], s->ptr == i ? '<' : ' ');
2021-01-29 19:35:59 +00:00
}
2021-01-30 05:56:19 +00:00
printf("\n\n");
2021-01-29 19:35:59 +00:00
}
2021-01-31 05:31:49 +00:00
#pragma mark - Operations
/* clang-format off */
2021-02-03 19:30:18 +00:00
void wspush(Uint8 v) { cpu.wst.dat[cpu.wst.ptr++] = v; }
Uint8 wspop(void) { return cpu.wst.dat[--cpu.wst.ptr]; }
2021-02-04 20:22:08 +00:00
Uint16 wspop16(void) {
Uint8 a = cpu.wst.dat[--cpu.wst.ptr];
Uint8 b = cpu.wst.dat[--cpu.wst.ptr];
return a + (b << 8);
}
2021-02-03 19:30:18 +00:00
Uint8 wspeek(void) { return cpu.wst.dat[cpu.wst.ptr - 1]; }
void rspush(Uint8 v) { cpu.rst.dat[cpu.rst.ptr++] = v; }
Uint8 rspop(void) { return cpu.rst.dat[--cpu.rst.ptr]; }
2021-01-31 05:31:49 +00:00
void op_brk() { setflag(FLAG_HALT, 1); }
2021-02-02 21:02:51 +00:00
void op_rts() { cpu.rom.ptr = rspop(); }
2021-02-02 17:40:41 +00:00
void op_lit() { cpu.literal += cpu.rom.dat[cpu.rom.ptr++]; }
2021-02-02 20:22:20 +00:00
void op_drp() { wspop(); }
2021-02-03 19:30:18 +00:00
void op_dup() { wspush(wspeek()); }
2021-02-02 20:22:20 +00:00
void op_swp() { Uint8 b = wspop(), a = wspop(); wspush(b); wspush(a); }
void op_ovr() { wspush(cpu.wst.dat[cpu.wst.ptr - 2]); }
void op_rot() { Uint8 c = wspop(),b = wspop(),a = wspop(); wspush(b); wspush(c); wspush(a); }
2021-02-04 05:53:56 +00:00
void op_jmu() { cpu.rom.ptr = wspop(); }
void op_jsu() { rspush(cpu.rom.ptr); cpu.rom.ptr = wspop(); }
void op_jmc() { if(wspop()) op_jmu(); }
void op_jsc() { if(wspop()) op_jsu(); }
void op_equ() { wspush(wspop() == wspop()); }
void op_neq() { wspush(wspop() != wspop()); }
void op_gth() { wspush(wspop() < wspop()); }
void op_lth() { wspush(wspop() > wspop()); }
2021-02-02 20:22:20 +00:00
void op_and() { wspush(wspop() & wspop()); }
void op_ora() { wspush(wspop() | wspop()); }
void op_rol() { wspush(wspop() << 1); }
void op_ror() { wspush(wspop() >> 1); }
void op_add() { wspush(wspop() + wspop()); }
void op_sub() { wspush(wspop() - wspop()); }
void op_mul() { wspush(wspop() * wspop()); }
void op_div() { wspush(wspop() / wspop()); }
2021-02-04 20:22:08 +00:00
void op_ldr() { }
void op_str() {
Uint8 b = wspop();
Uint16 addr = wspop16();
printf("store: %02x @ %04x\n", b, addr);
}
2021-01-31 05:31:49 +00:00
void (*ops[])(void) = {
2021-02-01 19:58:47 +00:00
op_brk, op_rts, op_lit, op_drp, op_dup, op_swp, op_ovr, op_rot,
2021-02-04 05:53:56 +00:00
op_jmu, op_jsu, op_jmc, op_jsc, op_equ, op_neq, op_gth, op_lth,
2021-02-04 20:22:08 +00:00
op_and, op_ora, op_rol, op_ror, op_add, op_sub, op_mul, op_div,
op_ldr, op_str, op_brk, op_brk, op_brk, op_brk, op_brk, op_brk
};
2021-01-31 05:31:49 +00:00
2021-02-02 04:21:27 +00:00
Uint8 opr[][2] = {
2021-02-03 19:30:18 +00:00
{0,0}, {0,0}, {0,0}, {1,0}, {0,1}, {1,1}, {0,1}, {3,3},
2021-02-04 05:53:56 +00:00
{2,0}, {2,0}, {2,0}, {2,0}, {2,1}, {2,1}, {2,1}, {2,1},
2021-02-02 21:02:51 +00:00
{1,0}, {1,0}, {1,0}, {1,0}, {2,1}, {0,0}, {0,0}, {0,0},
2021-02-04 20:22:08 +00:00
{2,1}, {2,1}, {2,1}, {2,1}, {2,1}, {2,1}, {2,1}, {2,1},
{3,1}, {3,1}
2021-02-02 04:21:27 +00:00
};
2021-01-31 05:31:49 +00:00
/* clang-format on */
2021-01-29 20:14:37 +00:00
void
2021-01-30 22:25:48 +00:00
reset(void)
2021-01-29 20:14:37 +00:00
{
2021-01-30 05:56:19 +00:00
int i;
2021-01-30 22:25:48 +00:00
cpu.status = 0x00;
cpu.counter = 0x00;
2021-02-04 01:43:13 +00:00
cpu.literal = 0x00;
2021-02-02 17:40:41 +00:00
cpu.rom.ptr = 0x00;
cpu.wst.ptr = 0x00;
cpu.rst.ptr = 0x00;
2021-02-04 01:43:13 +00:00
for(i = 0; i < 256; i++) {
2021-02-02 17:40:41 +00:00
cpu.wst.dat[i] = 0x00;
2021-02-04 01:43:13 +00:00
cpu.rst.dat[i] = 0x00;
}
2021-01-29 20:14:37 +00:00
}
int
2021-01-30 05:56:19 +00:00
error(char *name)
2021-01-29 20:14:37 +00:00
{
2021-01-30 05:56:19 +00:00
printf("Error: %s\n", name);
return 0;
}
void
2021-01-30 22:25:48 +00:00
load(FILE *f)
2021-01-30 05:56:19 +00:00
{
2021-02-02 17:40:41 +00:00
fread(cpu.rom.dat, sizeof(cpu.rom.dat), 1, f);
2021-01-30 05:56:19 +00:00
}
2021-01-29 21:59:16 +00:00
2021-02-02 04:21:27 +00:00
int
2021-01-30 22:25:48 +00:00
eval()
2021-01-30 05:56:19 +00:00
{
2021-02-02 17:40:41 +00:00
Uint8 instr = cpu.rom.dat[cpu.rom.ptr++];
2021-01-30 22:25:48 +00:00
if(cpu.literal > 0) {
2021-02-02 20:22:20 +00:00
wspush(instr);
2021-01-30 22:25:48 +00:00
cpu.literal--;
2021-02-02 04:21:27 +00:00
return 1;
2021-01-30 05:56:19 +00:00
}
2021-02-04 20:22:08 +00:00
if(instr < 32) {
2021-02-02 17:40:41 +00:00
if(cpu.wst.ptr < opr[instr][0])
2021-02-02 04:21:27 +00:00
return error("Stack underflow");
/* TODO stack overflow */
2021-01-31 05:31:49 +00:00
(*ops[instr])();
2021-02-02 04:21:27 +00:00
}
2021-02-01 22:40:27 +00:00
if(instr > 0x10)
setflag(FLAG_ZERO, 0);
2021-02-02 21:02:51 +00:00
if(cpu.counter == 128) {
2021-02-02 20:22:20 +00:00
printf("REACHED COUNTER\n");
return 0;
}
2021-02-02 04:21:27 +00:00
cpu.counter++;
return 1;
2021-01-29 20:14:37 +00:00
}
void
2021-01-30 22:25:48 +00:00
run(void)
2021-01-29 20:14:37 +00:00
{
2021-02-02 04:21:27 +00:00
while(!(cpu.status & FLAG_HALT) && eval(cpu))
;
2021-01-30 05:56:19 +00:00
/* debug */
2021-02-02 04:21:27 +00:00
printf("ended @ %d steps | ", cpu.counter);
2021-02-01 22:40:27 +00:00
printf("hf: %x zf: %x cf: %x tf: %x\n",
2021-02-02 04:21:27 +00:00
getflag(FLAG_HALT) != 0,
getflag(FLAG_ZERO) != 0,
getflag(FLAG_CARRY) != 0,
getflag(FLAG_TRAPS) != 0);
printf("\n");
2021-01-29 20:14:37 +00:00
}
2021-01-29 19:17:59 +00:00
int
main(int argc, char *argv[])
{
2021-01-29 20:14:37 +00:00
FILE *f;
if(argc < 2)
return error("No input.");
if(!(f = fopen(argv[1], "rb")))
return error("Missing input.");
2021-01-30 22:25:48 +00:00
reset();
load(f);
run();
2021-01-29 20:14:37 +00:00
/* print result */
2021-02-02 20:22:20 +00:00
echo(&cpu.wst, 0x40, "stack");
2021-01-29 19:17:59 +00:00
return 0;
}