collapseos/emul/forth.c

145 lines
2.9 KiB
C
Raw Normal View History

2020-03-10 13:26:02 +11:00
#include <stdint.h>
#include <stdio.h>
#include <unistd.h>
#include <curses.h>
2020-03-10 13:26:02 +11:00
#include <termios.h>
#include "emul.h"
#ifndef FBIN_PATH
#error FBIN_PATH needed
#endif
#ifndef BLKFS_PATH
#error BLKFS_PATH needed
#endif
2020-03-10 13:26:02 +11:00
// in sync with glue.asm
#define RAMSTART 0x900
2020-03-10 13:26:02 +11:00
#define STDIO_PORT 0x00
// This binary is also used for automated tests and those tests, when
// failing, send a non-zero value to RET_PORT to indicate failure
#define RET_PORT 0x01
2020-04-14 23:05:43 +10:00
// Port for block reads. Write 2 bytes, MSB first, on that port and then
// read 1024 bytes from the DATA port.
2020-04-14 23:05:43 +10:00
#define BLK_PORT 0x03
#define BLKDATA_PORT 0x04
2020-03-10 13:26:02 +11:00
static FILE *fp;
2020-04-14 23:05:43 +10:00
static FILE *blkfp;
static int retcode = 0;
2020-04-14 23:05:43 +10:00
static uint16_t blkid = 0;
2020-03-10 13:26:02 +11:00
static uint8_t iord_stdio()
{
int c;
if (fp != NULL) {
c = getc(fp);
} else {
c = getch();
}
2020-03-10 13:26:02 +11:00
if (c == EOF) {
c = 4; // ASCII EOT
2020-03-10 13:26:02 +11:00
}
return (uint8_t)c;
}
static void iowr_stdio(uint8_t val)
{
if (fp != NULL) {
putchar(val);
} else {
if (val >= 0x20 || val == '\n') {
echochar(val);
}
}
2020-03-10 13:26:02 +11:00
}
static void iowr_ret(uint8_t val)
{
retcode = val;
}
static void iowr_blk(uint8_t val)
{
blkid <<= 8;
blkid |= val;
fseek(blkfp, blkid*1024, SEEK_SET);
}
static uint8_t iord_blkdata()
2020-04-14 23:05:43 +10:00
{
uint8_t res = 0;
int c = getc(blkfp);
if (c != EOF) {
res = c;
2020-04-14 23:05:43 +10:00
}
return res;
}
static void iowr_blkdata(uint8_t val)
2020-04-14 23:05:43 +10:00
{
putc(val, blkfp);
2020-04-14 23:05:43 +10:00
}
int run()
2020-03-10 13:26:02 +11:00
{
fprintf(stderr, "Using blkfs %s\n", BLKFS_PATH);
blkfp = fopen(BLKFS_PATH, "r+");
if (!blkfp) {
fprintf(stderr, "Can't open\n");
return 1;
}
2020-03-10 13:26:02 +11:00
Machine *m = emul_init();
m->ramstart = RAMSTART;
m->iord[STDIO_PORT] = iord_stdio;
m->iowr[STDIO_PORT] = iowr_stdio;
m->iowr[RET_PORT] = iowr_ret;
2020-04-14 23:05:43 +10:00
m->iowr[BLK_PORT] = iowr_blk;
m->iord[BLKDATA_PORT] = iord_blkdata;
m->iowr[BLKDATA_PORT] = iowr_blkdata;
2020-03-10 13:26:02 +11:00
// initialize memory
FILE *bfp = fopen(FBIN_PATH, "r");
if (!bfp) {
fprintf(stderr, "Can't open forth.bin\n");
return 1;
2020-03-10 13:26:02 +11:00
}
int i = 0;
int c = getc(bfp);
while (c != EOF) {
m->mem[i++] = c;
c = getc(bfp);
}
fclose(bfp);
2020-03-10 13:26:02 +11:00
// Run!
while (emul_step());
2020-03-10 13:26:02 +11:00
if (blkfp != NULL) {
fclose(blkfp);
}
return retcode;
2020-03-10 13:26:02 +11:00
}
int main(int argc, char *argv[])
{
if (argc == 2) {
fp = fopen(argv[1], "r");
if (fp == NULL) {
fprintf(stderr, "Can't open %s\n", argv[1]);
return 1;
}
int ret = run();
fclose(fp);
return ret;
} else if (argc == 1) {
fp = NULL;
initscr(); cbreak(); noecho(); nl(); clear();
scrollok(stdscr, 1);
int ret = run();
nocbreak(); echo(); endwin();
printf("\nDone!\n");
emul_printdebug();
return ret;
} else {
fprintf(stderr, "Usage: ./forth [filename]\n");
return 1;
}
}