191 lines
5 KiB
C
191 lines
5 KiB
C
/***************************************************************
|
|
crc's _ _
|
|
(_) | ___
|
|
| | |/ _ \ a tiny virtual computer
|
|
| | | (_) | 64kw RAM, 32-bit, Dual Stack, MISC
|
|
|_|_|\___/ ilo.c (c) charles childers
|
|
**************************************************************/
|
|
|
|
/* This implementation uses memory separated into multiple
|
|
banks. This makes it easier to run on systems with less
|
|
RAM. */
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
#define SLICES 8
|
|
#define SLICESIZE 8192
|
|
|
|
long ip, sp, rp, data[33], address[257];
|
|
long *memory[SLICES];
|
|
|
|
#define TOS data[sp]
|
|
#define NOS data[sp-1]
|
|
#define TORS address[rp]
|
|
|
|
long pop() {
|
|
sp--;
|
|
return data[sp + 1];
|
|
}
|
|
|
|
void push(long value) {
|
|
sp++;
|
|
data[sp] = value;
|
|
}
|
|
|
|
void store(long a, long v) {
|
|
long slice = a / SLICESIZE;
|
|
long actual = a - (slice * SLICESIZE);
|
|
memory[slice][actual] = v;
|
|
}
|
|
|
|
long fetch(long a) {
|
|
long slice = a / SLICESIZE;
|
|
long actual = a - (slice * SLICESIZE);
|
|
return memory[slice][actual];
|
|
}
|
|
|
|
void prepare_vm() {
|
|
for (ip = 0; ip < 32; ip++) data[ip] = 0;
|
|
for (ip = 0; ip < 128; ip++) address[ip] = 0;
|
|
ip = sp = rp = 0;
|
|
}
|
|
|
|
void load_image() {
|
|
FILE *fp;
|
|
long x, y;
|
|
fp = fopen("ilo.rom", "rb");
|
|
for (y = 0; y < 65536; y++) {
|
|
x = 0;
|
|
fread(&x, 4, 1, fp);
|
|
store(y, x);
|
|
}
|
|
fclose(fp);
|
|
prepare_vm();
|
|
}
|
|
|
|
void save_image() {
|
|
FILE *fp;
|
|
long i;
|
|
fp = fopen("ilo.rom", "wb");
|
|
for (i = 0; i < SLICES; i++)
|
|
fwrite(&memory[i], sizeof(long), SLICESIZE, fp);
|
|
fclose(fp);
|
|
}
|
|
|
|
void read_block() {
|
|
FILE *f;
|
|
long block, buffer, x, c;
|
|
buffer = pop();
|
|
block = pop();
|
|
f = fopen("ilo.blo", "r+b");
|
|
fseek(f, 4096 * block, SEEK_SET);
|
|
for (x = 0; x < 1024; x++) {
|
|
c = 0;
|
|
fread(&c, 4, 1, f);
|
|
store(buffer + x, c);
|
|
}
|
|
fclose(f);
|
|
}
|
|
|
|
void write_block() {
|
|
FILE *f;
|
|
long block, buffer, x, c;
|
|
buffer = pop();
|
|
block = pop();
|
|
f = fopen("ilo.blo", "r+b");
|
|
fseek(f, 4096 * block, SEEK_SET);
|
|
for (x = 0; x < 1024; x++) {
|
|
c = fetch(buffer + x);
|
|
fwrite(&c, 4, 1, f);
|
|
}
|
|
fclose(f);
|
|
}
|
|
|
|
void process(long o) {
|
|
long a, b, target, flag;
|
|
long src, dest, len;
|
|
switch (o) {
|
|
case 0: break;
|
|
case 1: ip++; push(fetch(ip)); break;
|
|
case 2: push(TOS); break;
|
|
case 3: data[sp] = 0; sp--; break;
|
|
case 4: a = TOS; TOS = NOS; NOS = a; break;
|
|
case 5: rp++; TORS = pop(); break;
|
|
case 6: push(TORS); rp--; break;
|
|
case 7: ip = pop() - 1; break;
|
|
case 8: rp++; TORS = ip; ip = pop() - 1; break;
|
|
case 9: target = pop(); flag = pop();
|
|
if (flag != 0) { rp++; TORS = ip; ip = target - 1; }
|
|
break;
|
|
case 10: target = pop(); flag = pop();
|
|
if (flag != 0) ip = target - 1; break;
|
|
case 11: ip = TORS; rp--; break;
|
|
case 12: NOS = (NOS == TOS) ? -1 : 0; sp--; break;
|
|
case 13: NOS = (NOS != TOS) ? -1 : 0; sp--; break;
|
|
case 14: NOS = (NOS < TOS) ? -1 : 0; sp--; break;
|
|
case 15: NOS = (NOS > TOS) ? -1 : 0; sp--; break;
|
|
case 16: TOS = fetch(TOS); break;
|
|
case 17: store(TOS, NOS); sp--; sp--; break;
|
|
case 18: NOS += TOS; sp--; break;
|
|
case 19: NOS -= TOS; sp--; break;
|
|
case 20: NOS *= TOS; sp--; break;
|
|
case 21: a = TOS; b = NOS; TOS = b / a; NOS = b % a; break;
|
|
case 22: NOS = TOS & NOS; sp--; break;
|
|
case 23: NOS = TOS | NOS; sp--; break;
|
|
case 24: NOS = TOS ^ NOS; sp--; break;
|
|
case 25: NOS = NOS << TOS; sp--; break;
|
|
case 26: NOS = NOS >> TOS; sp--; break;
|
|
case 27: len = pop(); dest = pop();
|
|
src = pop(); flag = -1;
|
|
while (len) {
|
|
if (fetch(dest) != fetch(src)) flag = 0;
|
|
len -= 1; src += 1; dest += 1;
|
|
}; push(flag); break;
|
|
case 28: len = pop(); dest = pop();
|
|
src = pop();
|
|
while (len) {
|
|
store(dest, fetch(src));
|
|
len -= 1; src += 1; dest += 1;
|
|
}; break;
|
|
case 29: switch (pop()) {
|
|
case 0: putc(pop(), stdout); break;
|
|
case 1: push(getc(stdin)); break;
|
|
case 2: read_block(); break;
|
|
case 3: write_block(); break;
|
|
case 4: save_image(); break;
|
|
case 5: load_image(); ip = -1; break;
|
|
case 6: ip = 65536; break;
|
|
case 7: push(sp); push(rp); break;
|
|
default: break;
|
|
} break;
|
|
default: break;
|
|
}
|
|
}
|
|
|
|
void process_opcode_bundle(long opcode) {
|
|
process(opcode & 0xFF);
|
|
process((opcode >> 8) & 0xFF);
|
|
process((opcode >> 16) & 0xFF);
|
|
process((opcode >> 24) & 0xFF);
|
|
}
|
|
|
|
void execute(long address) {
|
|
ip = address;
|
|
while (ip < 65536) {
|
|
process_opcode_bundle(fetch(ip));
|
|
ip++;
|
|
}
|
|
}
|
|
|
|
int main(long argc, char **argv) {
|
|
long i;
|
|
for (i = 0; i < SLICES; i++)
|
|
memory[i] = malloc(SLICESIZE * sizeof(long));
|
|
load_image();
|
|
execute(0);
|
|
for (i = 0; i < SLICES; i++)
|
|
free(memory[i]);
|
|
for (i = 1; sp >= i; i++) printf(" %d", data[i]);
|
|
printf("\n");
|
|
}
|