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
|
|
|
|
#define FLAG_ZERO 0x02
|
|
|
|
#define FLAG_CARRY 0x04
|
|
|
|
#define FLAG_TRAPS 0x08
|
|
|
|
|
2021-01-29 14:35:59 -05:00
|
|
|
#define STACK_DEPTH 256
|
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
|
|
|
|
|
|
|
typedef struct {
|
2021-02-01 23:21:27 -05:00
|
|
|
Uint8 literal, status;
|
2021-01-31 00:31:49 -05:00
|
|
|
Uint8 mptr, sptr, rsptr;
|
2021-02-01 23:21:27 -05:00
|
|
|
Uint8 memory[STACK_DEPTH];
|
2021-01-30 00:56:19 -05:00
|
|
|
Uint8 stack[STACK_DEPTH];
|
2021-01-31 00:31:49 -05:00
|
|
|
Uint8 rstack[STACK_DEPTH];
|
2021-02-01 23:21:27 -05:00
|
|
|
Uint16 counter;
|
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-01-29 16:59:16 -05:00
|
|
|
echo(Uint8 *s, Uint8 len, char *name)
|
2021-01-29 14:35:59 -05:00
|
|
|
{
|
|
|
|
int i;
|
2021-01-29 16:59:16 -05:00
|
|
|
printf("%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-01-30 00:56:19 -05:00
|
|
|
printf("%02x ", s[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-01-31 00:31:49 -05:00
|
|
|
void
|
|
|
|
spush(Uint8 v)
|
|
|
|
{
|
|
|
|
cpu.stack[cpu.sptr++] = v;
|
|
|
|
}
|
|
|
|
|
|
|
|
Uint8
|
|
|
|
spop(void)
|
|
|
|
{
|
|
|
|
return cpu.stack[--cpu.sptr];
|
|
|
|
}
|
2021-01-30 17:25:48 -05:00
|
|
|
|
2021-01-29 15:14:37 -05:00
|
|
|
void
|
2021-01-31 00:31:49 -05:00
|
|
|
rspush(Uint8 v)
|
2021-01-29 15:14:37 -05:00
|
|
|
{
|
2021-01-31 00:31:49 -05:00
|
|
|
cpu.rstack[cpu.rsptr++] = v;
|
2021-01-29 15:14:37 -05:00
|
|
|
}
|
|
|
|
|
2021-01-30 17:25:48 -05:00
|
|
|
Uint8
|
2021-01-31 00:31:49 -05:00
|
|
|
rspop(void)
|
2021-01-29 15:14:37 -05:00
|
|
|
{
|
2021-01-31 00:31:49 -05:00
|
|
|
return cpu.rstack[--cpu.rsptr];
|
2021-01-29 15:14:37 -05:00
|
|
|
}
|
|
|
|
|
2021-01-31 00:31:49 -05:00
|
|
|
#pragma mark - Operations
|
|
|
|
|
|
|
|
/* clang-format off */
|
|
|
|
|
|
|
|
void op_brk() { setflag(FLAG_HALT, 1); }
|
2021-02-01 14:58:47 -05:00
|
|
|
void op_rts() { cpu.mptr = rspop(); }
|
2021-02-01 23:21:27 -05:00
|
|
|
void op_lit() { cpu.literal += cpu.memory[cpu.mptr++]; }
|
2021-01-31 00:31:49 -05:00
|
|
|
void op_drp() { spop(); }
|
|
|
|
void op_dup() { spush(cpu.stack[cpu.sptr - 1]); }
|
|
|
|
void op_swp() { Uint8 b = spop(), a = spop(); spush(b); spush(a); }
|
|
|
|
void op_ovr() { spush(cpu.stack[cpu.sptr - 2]); }
|
|
|
|
void op_rot() { Uint8 c = spop(),b = spop(),a = spop(); spush(b); spush(c); spush(a); }
|
|
|
|
void op_jmp() { cpu.mptr = spop(); }
|
|
|
|
void op_jsr() { rspush(cpu.mptr); cpu.mptr = spop(); }
|
2021-02-01 23:21:27 -05:00
|
|
|
void op_jmq() { Uint8 a = spop(); if(getflag(FLAG_ZERO)){ cpu.mptr = a; } setflag(FLAG_ZERO,0); }
|
|
|
|
void op_jsq() { Uint8 a = spop(); if(getflag(FLAG_ZERO)){ rspush(cpu.mptr); cpu.mptr = a; } setflag(FLAG_ZERO,0); }
|
2021-01-31 00:31:49 -05:00
|
|
|
void op_equ() { setflag(FLAG_ZERO, spop() == spop()); }
|
|
|
|
void op_neq() { setflag(FLAG_ZERO, spop() != spop()); }
|
|
|
|
void op_lth() { setflag(FLAG_ZERO, spop() < spop()); }
|
|
|
|
void op_gth() { setflag(FLAG_ZERO, spop() > spop()); }
|
|
|
|
void op_and() { spush(spop() & spop()); }
|
|
|
|
void op_ora() { spush(spop() | spop()); }
|
|
|
|
void op_rol() { spush(spop() << 1); }
|
|
|
|
void op_ror() { spush(spop() >> 1); }
|
|
|
|
void op_add() { spush(spop() + spop()); }
|
|
|
|
void op_sub() { spush(spop() - spop()); }
|
|
|
|
void op_mul() { spush(spop() * spop()); }
|
|
|
|
void op_div() { spush(spop() / spop()); }
|
|
|
|
|
|
|
|
void (*ops[])(void) = {
|
2021-02-01 14:58:47 -05:00
|
|
|
op_brk, op_rts, op_lit, op_drp, op_dup, op_swp, op_ovr, op_rot,
|
|
|
|
op_jmp, op_jsr, op_jmq, op_jsq, op_equ, op_neq, op_gth, op_lth,
|
2021-01-31 00:31:49 -05:00
|
|
|
op_and, op_ora, op_rol, op_ror, op_add, op_sub, op_mul, op_div};
|
|
|
|
|
2021-02-01 23:21:27 -05:00
|
|
|
Uint8 opr[][2] = {
|
|
|
|
{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}, {2,1}, {0,0}, {0,0}, {0,0},
|
|
|
|
{0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0}, {0,0},
|
|
|
|
};
|
|
|
|
|
2021-01-31 00:31:49 -05:00
|
|
|
/* clang-format on */
|
|
|
|
|
2021-01-29 15:14:37 -05:00
|
|
|
void
|
2021-01-30 17:25:48 -05:00
|
|
|
reset(void)
|
2021-01-29 15:14:37 -05:00
|
|
|
{
|
2021-01-30 00:56:19 -05:00
|
|
|
int i;
|
2021-01-30 17:25:48 -05:00
|
|
|
cpu.status = 0x00;
|
|
|
|
cpu.counter = 0x00;
|
|
|
|
cpu.mptr = 0x00;
|
|
|
|
cpu.sptr = 0x00;
|
|
|
|
cpu.literal = 0x00;
|
2021-01-30 00:56:19 -05:00
|
|
|
for(i = 0; i < 256; i++)
|
2021-01-30 17:25:48 -05:00
|
|
|
cpu.stack[i] = 0x00;
|
2021-01-29 15:14:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2021-01-30 00:56:19 -05:00
|
|
|
error(char *name)
|
2021-01-29 15:14:37 -05:00
|
|
|
{
|
2021-01-30 00:56:19 -05:00
|
|
|
printf("Error: %s\n", name);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2021-01-30 17:25:48 -05:00
|
|
|
load(FILE *f)
|
2021-01-30 00:56:19 -05:00
|
|
|
{
|
2021-01-30 17:25:48 -05:00
|
|
|
fread(cpu.memory, sizeof(cpu.memory), 1, f);
|
2021-01-30 00:56:19 -05:00
|
|
|
}
|
2021-01-29 16:59:16 -05:00
|
|
|
|
2021-02-01 23:21:27 -05:00
|
|
|
int
|
2021-01-30 17:25:48 -05:00
|
|
|
eval()
|
2021-01-30 00:56:19 -05:00
|
|
|
{
|
2021-01-30 17:25:48 -05:00
|
|
|
Uint8 instr = cpu.memory[cpu.mptr++];
|
|
|
|
if(cpu.literal > 0) {
|
2021-01-31 00:31:49 -05:00
|
|
|
spush(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-01 23:21:27 -05:00
|
|
|
if(instr < 24) {
|
|
|
|
if(cpu.sptr < opr[instr][0])
|
|
|
|
return error("Stack underflow");
|
|
|
|
/* TODO stack overflow */
|
2021-01-31 00:31:49 -05:00
|
|
|
(*ops[instr])();
|
2021-02-01 23:21:27 -05:00
|
|
|
}
|
2021-02-01 17:40:27 -05:00
|
|
|
if(instr > 0x10)
|
|
|
|
setflag(FLAG_ZERO, 0);
|
2021-02-01 23:21:27 -05:00
|
|
|
cpu.counter++;
|
|
|
|
return 1;
|
2021-01-29 15:14:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2021-01-30 17:25:48 -05:00
|
|
|
run(void)
|
2021-01-29 15:14:37 -05:00
|
|
|
{
|
2021-02-01 23:21:27 -05:00
|
|
|
while(!(cpu.status & FLAG_HALT) && eval(cpu))
|
|
|
|
;
|
2021-01-30 00:56:19 -05:00
|
|
|
/* debug */
|
2021-02-01 23:21:27 -05:00
|
|
|
printf("ended @ %d steps | ", cpu.counter);
|
2021-02-01 17:40:27 -05:00
|
|
|
printf("hf: %x zf: %x cf: %x tf: %x\n",
|
2021-02-01 23:21:27 -05:00
|
|
|
getflag(FLAG_HALT) != 0,
|
|
|
|
getflag(FLAG_ZERO) != 0,
|
|
|
|
getflag(FLAG_CARRY) != 0,
|
|
|
|
getflag(FLAG_TRAPS) != 0);
|
|
|
|
printf("\n");
|
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)
|
|
|
|
return error("No input.");
|
|
|
|
if(!(f = fopen(argv[1], "rb")))
|
|
|
|
return error("Missing input.");
|
2021-01-30 17:25:48 -05:00
|
|
|
reset();
|
|
|
|
load(f);
|
|
|
|
run();
|
2021-01-29 15:14:37 -05:00
|
|
|
/* print result */
|
2021-01-30 00:56:19 -05:00
|
|
|
echo(cpu.stack, 0x40, "stack");
|
|
|
|
echo(cpu.memory, 0x40, "memory");
|
2021-01-29 14:17:59 -05:00
|
|
|
return 0;
|
|
|
|
}
|