2021-05-17 19:18:27 +02:00
|
|
|
/* RETRO is a clean, elegant, and pragmatic dialect of Forth. It provides
|
|
|
|
a simple alternative for those willing to make a break from legacy
|
|
|
|
systems.
|
2018-01-09 14:58:34 +01:00
|
|
|
|
2021-05-17 19:18:27 +02:00
|
|
|
The language draws influences from many sources including traditional
|
|
|
|
Forth systems, cmForth, colorForth, Factor, and Parable. It was
|
|
|
|
designed to be easy to grasp and adapt to specific uses.
|
2018-01-08 23:01:05 +01:00
|
|
|
|
2021-05-17 19:18:27 +02:00
|
|
|
The basic language is very portable and runs on a tiny virtual machine.
|
2018-01-08 23:01:05 +01:00
|
|
|
|
2021-05-17 19:18:27 +02:00
|
|
|
This file contains a minimal implementation of the virtual machine.
|
2018-01-08 23:01:05 +01:00
|
|
|
|
2021-05-17 19:18:27 +02:00
|
|
|
|
|
|
|
Copyright (c) 2008 - 2021, Charles Childers
|
|
|
|
Copyright (c) 2009 - 2010, Luke Parrish
|
|
|
|
Copyright (c) 2010, Marc Simpson
|
|
|
|
Copyright (c) 2010, Jay Skeer
|
|
|
|
Copyright (c) 2011, Kenneth Keating
|
|
|
|
*/
|
2017-10-16 18:09:39 +02:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdint.h>
|
2019-09-23 17:11:56 +02:00
|
|
|
#include <limits.h>
|
2018-01-08 23:01:05 +01:00
|
|
|
|
2019-09-23 17:11:56 +02:00
|
|
|
#define CELL int32_t
|
|
|
|
#define CELL_MIN INT_MIN + 1
|
|
|
|
#define CELL_MAX INT_MAX - 1
|
|
|
|
|
2021-05-17 19:18:27 +02:00
|
|
|
#define IMAGE_SIZE 524288 /* Amount of RAM. */
|
|
|
|
#define ADDRESSES 128 /* Max address stack depth */
|
|
|
|
#define STACK_DEPTH 32 /* Max data stack depth */
|
2018-11-22 01:05:28 +01:00
|
|
|
|
2021-05-17 19:18:27 +02:00
|
|
|
CELL sp, rp, ip; /* Stack and instruction pointers */
|
|
|
|
CELL data[STACK_DEPTH]; /* The data stack */
|
|
|
|
CELL address[ADDRESSES]; /* The address stack */
|
|
|
|
CELL memory[IMAGE_SIZE + 1]; /* Image Memory */
|
2017-10-19 22:48:04 +02:00
|
|
|
|
2021-05-17 19:18:27 +02:00
|
|
|
#define TOS data[sp] /* Top item on stack */
|
|
|
|
#define NOS data[sp-1] /* Second item on stack */
|
|
|
|
#define TORS address[rp] /* Top item on address stack */
|
2017-10-19 22:48:04 +02:00
|
|
|
|
2020-10-16 21:05:03 +02:00
|
|
|
typedef void (*Handler)(void);
|
|
|
|
|
2017-10-19 22:48:04 +02:00
|
|
|
CELL stack_pop();
|
|
|
|
void stack_push(CELL value);
|
2018-07-11 02:39:44 +02:00
|
|
|
void execute(CELL cell);
|
2021-05-17 19:18:27 +02:00
|
|
|
void load_image(char *imageFile);
|
2020-11-02 14:34:28 +01:00
|
|
|
void prepare_vm();
|
|
|
|
void process_opcode_bundle(CELL opcode);
|
2017-10-19 22:48:04 +02:00
|
|
|
|
|
|
|
CELL stack_pop() {
|
|
|
|
sp--;
|
|
|
|
return data[sp + 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
void stack_push(CELL value) {
|
|
|
|
sp++;
|
|
|
|
data[sp] = value;
|
|
|
|
}
|
|
|
|
|
2018-07-11 02:39:44 +02:00
|
|
|
void execute(CELL cell) {
|
2017-10-19 22:48:04 +02:00
|
|
|
CELL opcode;
|
|
|
|
rp = 1;
|
|
|
|
ip = cell;
|
|
|
|
while (ip < IMAGE_SIZE) {
|
|
|
|
opcode = memory[ip];
|
2021-05-17 19:18:27 +02:00
|
|
|
process_opcode_bundle(opcode);
|
2017-10-19 22:48:04 +02:00
|
|
|
ip++;
|
|
|
|
if (rp == 0)
|
|
|
|
ip = IMAGE_SIZE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-08 23:01:05 +01:00
|
|
|
int main(int argc, char **argv) {
|
2020-11-02 14:34:28 +01:00
|
|
|
prepare_vm();
|
|
|
|
load_image("ngaImage");
|
2021-05-17 19:18:27 +02:00
|
|
|
execute(0);
|
2018-01-08 23:01:05 +01:00
|
|
|
exit(0);
|
2017-10-19 22:48:04 +02:00
|
|
|
}
|
|
|
|
|
2021-05-17 19:18:27 +02:00
|
|
|
void load_image(char *imageFile) {
|
2017-10-19 22:48:04 +02:00
|
|
|
FILE *fp;
|
|
|
|
long fileLen;
|
|
|
|
if ((fp = fopen(imageFile, "rb")) != NULL) {
|
|
|
|
fseek(fp, 0, SEEK_END);
|
|
|
|
fileLen = ftell(fp) / sizeof(CELL);
|
|
|
|
rewind(fp);
|
2021-05-17 19:18:27 +02:00
|
|
|
fread(&memory, sizeof(CELL), fileLen, fp);
|
2017-10-19 22:48:04 +02:00
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-02 14:34:28 +01:00
|
|
|
void prepare_vm() {
|
2017-10-19 22:48:04 +02:00
|
|
|
ip = sp = rp = 0;
|
|
|
|
for (ip = 0; ip < IMAGE_SIZE; ip++)
|
2020-11-02 14:34:28 +01:00
|
|
|
memory[ip] = 0; /* NO - nop instruction */
|
2017-10-19 22:48:04 +02:00
|
|
|
for (ip = 0; ip < STACK_DEPTH; ip++)
|
|
|
|
data[ip] = 0;
|
|
|
|
for (ip = 0; ip < ADDRESSES; ip++)
|
|
|
|
address[ip] = 0;
|
|
|
|
}
|
|
|
|
|
2020-11-02 14:34:28 +01:00
|
|
|
void inst_no() {
|
2017-10-19 22:48:04 +02:00
|
|
|
}
|
|
|
|
|
2020-11-02 14:34:28 +01:00
|
|
|
void inst_li() {
|
2017-10-19 22:48:04 +02:00
|
|
|
ip++;
|
2021-05-17 19:18:27 +02:00
|
|
|
stack_push(memory[ip]);
|
2017-10-19 22:48:04 +02:00
|
|
|
}
|
|
|
|
|
2020-11-02 14:34:28 +01:00
|
|
|
void inst_du() {
|
2021-05-17 19:18:27 +02:00
|
|
|
stack_push(TOS);
|
2017-10-19 22:48:04 +02:00
|
|
|
}
|
|
|
|
|
2020-11-02 14:34:28 +01:00
|
|
|
void inst_dr() {
|
2017-10-19 22:48:04 +02:00
|
|
|
data[sp] = 0;
|
|
|
|
if (--sp < 0)
|
|
|
|
ip = IMAGE_SIZE;
|
|
|
|
}
|
|
|
|
|
2020-11-02 14:34:28 +01:00
|
|
|
void inst_sw() {
|
2018-07-11 02:39:44 +02:00
|
|
|
CELL a;
|
2017-10-19 22:48:04 +02:00
|
|
|
a = TOS;
|
|
|
|
TOS = NOS;
|
|
|
|
NOS = a;
|
|
|
|
}
|
|
|
|
|
2020-11-02 14:34:28 +01:00
|
|
|
void inst_pu() {
|
2017-10-19 22:48:04 +02:00
|
|
|
rp++;
|
|
|
|
TORS = TOS;
|
2020-11-02 14:34:28 +01:00
|
|
|
inst_dr();
|
2017-10-19 22:48:04 +02:00
|
|
|
}
|
|
|
|
|
2020-11-02 14:34:28 +01:00
|
|
|
void inst_po() {
|
2021-05-17 19:18:27 +02:00
|
|
|
stack_push(TORS);
|
2017-10-19 22:48:04 +02:00
|
|
|
rp--;
|
|
|
|
}
|
|
|
|
|
2020-11-02 14:34:28 +01:00
|
|
|
void inst_ju() {
|
2017-10-19 22:48:04 +02:00
|
|
|
ip = TOS - 1;
|
2020-11-02 14:34:28 +01:00
|
|
|
inst_dr();
|
2017-10-19 22:48:04 +02:00
|
|
|
}
|
|
|
|
|
2020-11-02 14:34:28 +01:00
|
|
|
void inst_ca() {
|
2017-10-19 22:48:04 +02:00
|
|
|
rp++;
|
|
|
|
TORS = ip;
|
|
|
|
ip = TOS - 1;
|
2020-11-02 14:34:28 +01:00
|
|
|
inst_dr();
|
2017-10-19 22:48:04 +02:00
|
|
|
}
|
|
|
|
|
2020-11-02 14:34:28 +01:00
|
|
|
void inst_cc() {
|
2018-07-11 02:39:44 +02:00
|
|
|
CELL a, b;
|
2020-11-02 14:34:28 +01:00
|
|
|
a = TOS; inst_dr(); /* Target */
|
|
|
|
b = TOS; inst_dr(); /* Flag */
|
2017-10-19 22:48:04 +02:00
|
|
|
if (b != 0) {
|
|
|
|
rp++;
|
|
|
|
TORS = ip;
|
|
|
|
ip = a - 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-02 14:34:28 +01:00
|
|
|
void inst_re() {
|
2017-10-19 22:48:04 +02:00
|
|
|
ip = TORS;
|
|
|
|
rp--;
|
|
|
|
}
|
|
|
|
|
|
|
|
void inst_eq() {
|
|
|
|
NOS = (NOS == TOS) ? -1 : 0;
|
2020-11-02 14:34:28 +01:00
|
|
|
inst_dr();
|
2017-10-19 22:48:04 +02:00
|
|
|
}
|
|
|
|
|
2020-11-02 14:34:28 +01:00
|
|
|
void inst_ne() {
|
2017-10-19 22:48:04 +02:00
|
|
|
NOS = (NOS != TOS) ? -1 : 0;
|
2020-11-02 14:34:28 +01:00
|
|
|
inst_dr();
|
2017-10-19 22:48:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void inst_lt() {
|
|
|
|
NOS = (NOS < TOS) ? -1 : 0;
|
2020-11-02 14:34:28 +01:00
|
|
|
inst_dr();
|
2017-10-19 22:48:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void inst_gt() {
|
|
|
|
NOS = (NOS > TOS) ? -1 : 0;
|
2020-11-02 14:34:28 +01:00
|
|
|
inst_dr();
|
2017-10-19 22:48:04 +02:00
|
|
|
}
|
|
|
|
|
2020-11-02 14:34:28 +01:00
|
|
|
void inst_fe() {
|
2017-10-19 22:48:04 +02:00
|
|
|
switch (TOS) {
|
|
|
|
case -1: TOS = sp - 1; break;
|
|
|
|
case -2: TOS = rp; break;
|
|
|
|
case -3: TOS = IMAGE_SIZE; break;
|
2019-11-13 22:34:17 +01:00
|
|
|
case -4: TOS = CELL_MIN; break;
|
|
|
|
case -5: TOS = CELL_MAX; break;
|
2017-10-19 22:48:04 +02:00
|
|
|
default: TOS = memory[TOS]; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-02 14:34:28 +01:00
|
|
|
void inst_st() {
|
2017-11-21 13:20:55 +01:00
|
|
|
if (TOS <= IMAGE_SIZE && TOS >= 0) {
|
2017-11-21 13:19:00 +01:00
|
|
|
memory[TOS] = NOS;
|
2020-11-02 14:34:28 +01:00
|
|
|
inst_dr();
|
|
|
|
inst_dr();
|
2017-11-21 13:19:00 +01:00
|
|
|
} else {
|
|
|
|
ip = IMAGE_SIZE;
|
|
|
|
}
|
2017-10-19 22:48:04 +02:00
|
|
|
}
|
|
|
|
|
2020-11-02 14:34:28 +01:00
|
|
|
void inst_ad() {
|
2017-10-19 22:48:04 +02:00
|
|
|
NOS += TOS;
|
2020-11-02 14:34:28 +01:00
|
|
|
inst_dr();
|
2017-10-19 22:48:04 +02:00
|
|
|
}
|
|
|
|
|
2020-11-02 14:34:28 +01:00
|
|
|
void inst_su() {
|
2017-10-19 22:48:04 +02:00
|
|
|
NOS -= TOS;
|
2020-11-02 14:34:28 +01:00
|
|
|
inst_dr();
|
2017-10-19 22:48:04 +02:00
|
|
|
}
|
|
|
|
|
2020-11-02 14:34:28 +01:00
|
|
|
void inst_mu() {
|
2017-10-19 22:48:04 +02:00
|
|
|
NOS *= TOS;
|
2020-11-02 14:34:28 +01:00
|
|
|
inst_dr();
|
2017-10-19 22:48:04 +02:00
|
|
|
}
|
|
|
|
|
2020-11-02 14:34:28 +01:00
|
|
|
void inst_di() {
|
2018-07-11 02:39:44 +02:00
|
|
|
CELL a, b;
|
2017-10-19 22:48:04 +02:00
|
|
|
a = TOS;
|
|
|
|
b = NOS;
|
|
|
|
TOS = b / a;
|
|
|
|
NOS = b % a;
|
|
|
|
}
|
|
|
|
|
2020-11-02 14:34:28 +01:00
|
|
|
void inst_an() {
|
2017-10-19 22:48:04 +02:00
|
|
|
NOS = TOS & NOS;
|
2020-11-02 14:34:28 +01:00
|
|
|
inst_dr();
|
2017-10-19 22:48:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void inst_or() {
|
|
|
|
NOS = TOS | NOS;
|
2020-11-02 14:34:28 +01:00
|
|
|
inst_dr();
|
2017-10-19 22:48:04 +02:00
|
|
|
}
|
|
|
|
|
2020-11-02 14:34:28 +01:00
|
|
|
void inst_xo() {
|
2017-10-19 22:48:04 +02:00
|
|
|
NOS = TOS ^ NOS;
|
2020-11-02 14:34:28 +01:00
|
|
|
inst_dr();
|
2017-10-19 22:48:04 +02:00
|
|
|
}
|
|
|
|
|
2020-11-02 14:34:28 +01:00
|
|
|
void inst_sh() {
|
2017-10-19 22:48:04 +02:00
|
|
|
CELL y = TOS;
|
|
|
|
CELL x = NOS;
|
|
|
|
if (TOS < 0)
|
2021-05-17 19:18:27 +02:00
|
|
|
NOS = NOS << (0 - TOS);
|
2017-10-19 22:48:04 +02:00
|
|
|
else {
|
|
|
|
if (x < 0 && y > 0)
|
|
|
|
NOS = x >> y | ~(~0U >> y);
|
|
|
|
else
|
|
|
|
NOS = x >> y;
|
|
|
|
}
|
2020-11-02 14:34:28 +01:00
|
|
|
inst_dr();
|
2017-10-19 22:48:04 +02:00
|
|
|
}
|
|
|
|
|
2020-11-02 14:34:28 +01:00
|
|
|
void inst_zr() {
|
2017-10-19 22:48:04 +02:00
|
|
|
if (TOS == 0) {
|
2020-11-02 14:34:28 +01:00
|
|
|
inst_dr();
|
2017-10-19 22:48:04 +02:00
|
|
|
ip = TORS;
|
|
|
|
rp--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-02 14:34:28 +01:00
|
|
|
void inst_ha() {
|
2017-10-19 22:48:04 +02:00
|
|
|
ip = IMAGE_SIZE;
|
|
|
|
}
|
|
|
|
|
2018-11-22 01:05:28 +01:00
|
|
|
void inst_ie() {
|
2021-05-17 19:18:27 +02:00
|
|
|
stack_push(2);
|
2018-11-22 01:05:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void inst_iq() {
|
2021-05-17 19:18:27 +02:00
|
|
|
if (TOS == 0) {
|
|
|
|
inst_dr();
|
|
|
|
stack_push(0);
|
|
|
|
stack_push(0);
|
|
|
|
} else if (TOS == 1) {
|
|
|
|
inst_dr();
|
|
|
|
stack_push(1);
|
|
|
|
stack_push(1);
|
|
|
|
}
|
2018-11-22 01:05:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void inst_ii() {
|
2021-05-17 19:18:27 +02:00
|
|
|
if (TOS == 0) {
|
|
|
|
inst_dr();
|
|
|
|
putc(stack_pop(), stdout);
|
|
|
|
} else if (TOS == 1) {
|
|
|
|
inst_dr();
|
|
|
|
stack_push(getc(stdin));
|
|
|
|
} else {
|
|
|
|
inst_dr();
|
|
|
|
}
|
2018-11-22 01:05:28 +01:00
|
|
|
}
|
|
|
|
|
2020-10-16 21:05:03 +02:00
|
|
|
Handler instructions[] = {
|
2020-11-02 14:34:28 +01:00
|
|
|
inst_no, inst_li, inst_du, inst_dr, inst_sw, inst_pu, inst_po,
|
|
|
|
inst_ju, inst_ca, inst_cc, inst_re, inst_eq, inst_ne, inst_lt,
|
|
|
|
inst_gt, inst_fe, inst_st, inst_ad, inst_su, inst_mu, inst_di,
|
|
|
|
inst_an, inst_or, inst_xo, inst_sh, inst_zr, inst_ha, inst_ie,
|
2018-11-22 01:05:28 +01:00
|
|
|
inst_iq, inst_ii
|
2017-10-19 22:48:04 +02:00
|
|
|
};
|
|
|
|
|
2020-11-02 14:34:28 +01:00
|
|
|
void process_opcode_bundle(CELL opcode) {
|
2021-05-17 19:18:27 +02:00
|
|
|
instructions[opcode & 0xFF]();
|
|
|
|
instructions[(opcode >> 8) & 0xFF]();
|
|
|
|
instructions[(opcode >> 16) & 0xFF]();
|
|
|
|
instructions[(opcode >> 24) & 0xFF]();
|
2017-10-19 22:48:04 +02:00
|
|
|
}
|