2020-03-10 13:26:02 +11:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <termios.h>
|
2020-04-25 07:53:58 +10:00
|
|
|
#include "emul.h"
|
2020-04-27 00:15:35 +10:00
|
|
|
#include "stage1-bin.h"
|
2020-04-25 08:01:55 +10:00
|
|
|
#include "blkfs-bin.h"
|
2020-03-10 13:26:02 +11:00
|
|
|
|
|
|
|
// in sync with glue.asm
|
2020-03-12 15:14:44 +11:00
|
|
|
#define RAMSTART 0x900
|
2020-03-10 13:26:02 +11:00
|
|
|
#define STDIO_PORT 0x00
|
2020-03-20 06:43:48 +11:00
|
|
|
// 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
|
2020-04-17 07:22:18 +10:00
|
|
|
// read 1024 bytes from the DATA port.
|
2020-04-14 23:05:43 +10:00
|
|
|
#define BLK_PORT 0x03
|
2020-04-17 07:22:18 +10:00
|
|
|
#define BLKDATA_PORT 0x04
|
2020-03-10 13:26:02 +11:00
|
|
|
|
|
|
|
static int running;
|
2020-03-12 10:03:47 +11:00
|
|
|
static FILE *fp;
|
2020-04-14 23:05:43 +10:00
|
|
|
static FILE *blkfp;
|
2020-03-20 06:43:48 +11:00
|
|
|
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()
|
|
|
|
{
|
2020-03-12 10:03:47 +11:00
|
|
|
int c = getc(fp);
|
2020-03-10 13:26:02 +11:00
|
|
|
if (c == EOF) {
|
|
|
|
running = 0;
|
|
|
|
}
|
|
|
|
return (uint8_t)c;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void iowr_stdio(uint8_t val)
|
|
|
|
{
|
|
|
|
if (val == 0x04) { // CTRL+D
|
|
|
|
running = 0;
|
|
|
|
} else {
|
|
|
|
putchar(val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-20 06:43:48 +11:00
|
|
|
static void iowr_ret(uint8_t val)
|
|
|
|
{
|
|
|
|
retcode = val;
|
|
|
|
}
|
|
|
|
|
2020-04-17 07:22:18 +10:00
|
|
|
static void iowr_blk(uint8_t val)
|
|
|
|
{
|
|
|
|
blkid <<= 8;
|
|
|
|
blkid |= val;
|
|
|
|
if (blkfp != NULL) {
|
|
|
|
fseek(blkfp, blkid*1024, SEEK_SET);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint8_t iord_blkdata()
|
2020-04-14 23:05:43 +10:00
|
|
|
{
|
|
|
|
uint8_t res = 0;
|
|
|
|
if (blkfp != NULL) {
|
|
|
|
int c = getc(blkfp);
|
|
|
|
if (c != EOF) {
|
|
|
|
res = c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2020-04-17 07:22:18 +10:00
|
|
|
static void iowr_blkdata(uint8_t val)
|
2020-04-14 23:05:43 +10:00
|
|
|
{
|
|
|
|
if (blkfp != NULL) {
|
2020-04-17 07:22:18 +10:00
|
|
|
putc(val, blkfp);
|
2020-04-14 23:05:43 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-20 06:43:48 +11:00
|
|
|
|
2020-03-10 13:26:02 +11:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2020-03-12 10:03:47 +11:00
|
|
|
bool tty = false;
|
2020-03-10 13:26:02 +11:00
|
|
|
struct termios termInfo;
|
2020-03-12 10:03:47 +11:00
|
|
|
if (argc == 2) {
|
|
|
|
fp = fopen(argv[1], "r");
|
|
|
|
if (fp == NULL) {
|
|
|
|
fprintf(stderr, "Can't open %s\n", argv[1]);
|
2020-03-10 13:26:02 +11:00
|
|
|
return 1;
|
|
|
|
}
|
2020-03-12 10:03:47 +11:00
|
|
|
} else if (argc == 1) {
|
|
|
|
fp = stdin;
|
|
|
|
tty = isatty(fileno(stdin));
|
|
|
|
if (tty) {
|
|
|
|
// Turn echo off: the shell takes care of its own echoing.
|
|
|
|
if (tcgetattr(0, &termInfo) == -1) {
|
|
|
|
printf("Can't setup terminal.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
termInfo.c_lflag &= ~ECHO;
|
|
|
|
termInfo.c_lflag &= ~ICANON;
|
|
|
|
tcsetattr(0, TCSAFLUSH, &termInfo);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "Usage: ./forth [filename]\n");
|
|
|
|
return 1;
|
2020-03-10 13:26:02 +11:00
|
|
|
}
|
2020-04-14 23:05:43 +10:00
|
|
|
blkfp = fopen("blkfs", "r+");
|
2020-04-25 08:01:55 +10:00
|
|
|
if (blkfp) {
|
|
|
|
fprintf(stderr, "Using blkfs file\n");
|
|
|
|
} else {
|
|
|
|
blkfp = fmemopen((char*)BLKFS, sizeof(BLKFS), "r");
|
|
|
|
fprintf(stderr, "Using in-memory read-only blkfs\n");
|
|
|
|
}
|
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;
|
2020-03-20 06:43:48 +11:00
|
|
|
m->iowr[RET_PORT] = iowr_ret;
|
2020-04-14 23:05:43 +10:00
|
|
|
m->iowr[BLK_PORT] = iowr_blk;
|
2020-04-17 07:22:18 +10:00
|
|
|
m->iord[BLKDATA_PORT] = iord_blkdata;
|
|
|
|
m->iowr[BLKDATA_PORT] = iowr_blkdata;
|
2020-03-10 13:26:02 +11:00
|
|
|
// initialize memory
|
|
|
|
for (int i=0; i<sizeof(KERNEL); i++) {
|
|
|
|
m->mem[i] = KERNEL[i];
|
|
|
|
}
|
2020-04-01 23:54:01 +11:00
|
|
|
|
2020-03-10 13:26:02 +11:00
|
|
|
// Run!
|
|
|
|
running = 1;
|
|
|
|
|
|
|
|
while (running && emul_step());
|
|
|
|
|
|
|
|
if (tty) {
|
2020-03-12 10:03:47 +11:00
|
|
|
printf("\nDone!\n");
|
2020-03-10 13:26:02 +11:00
|
|
|
termInfo.c_lflag |= ECHO;
|
|
|
|
termInfo.c_lflag |= ICANON;
|
|
|
|
tcsetattr(0, TCSAFLUSH, &termInfo);
|
|
|
|
emul_printdebug();
|
|
|
|
}
|
2020-04-16 00:56:49 +10:00
|
|
|
if (blkfp != NULL) {
|
|
|
|
fclose(blkfp);
|
|
|
|
}
|
2020-03-12 10:03:47 +11:00
|
|
|
fclose(fp);
|
2020-03-20 06:43:48 +11:00
|
|
|
return retcode;
|
2020-03-10 13:26:02 +11:00
|
|
|
}
|