2020-03-10 13:26:02 +11:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
2020-05-23 22:24:39 +10:00
|
|
|
#include <curses.h>
|
2020-03-10 13:26:02 +11:00
|
|
|
#include <termios.h>
|
2020-04-25 07:53:58 +10:00
|
|
|
#include "emul.h"
|
2020-03-10 13:26:02 +11:00
|
|
|
|
2020-05-24 04:20:50 +10:00
|
|
|
#define WCOLS 80
|
|
|
|
#define WLINES 32
|
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-12-07 11:50:57 +11:00
|
|
|
// Port for block reads. Each read or write has to be done in 5 IO writes:
|
|
|
|
// 1 - r/w. 1 for read, 2 for write.
|
|
|
|
// 2 - blkid MSB
|
|
|
|
// 3 - blkid LSB
|
|
|
|
// 4 - dest addr MSB
|
|
|
|
// 5 - dest addr LSB
|
|
|
|
#define BLK_PORT 0x03
|
2020-05-24 10:15:28 +10:00
|
|
|
#define SETX_PORT 0x05
|
|
|
|
#define SETY_PORT 0x06
|
2020-03-10 13:26:02 +11:00
|
|
|
|
2020-12-07 11:50:57 +11:00
|
|
|
static Machine *m;
|
2020-03-12 10:03:47 +11:00
|
|
|
static FILE *fp;
|
2020-03-20 06:43:48 +11:00
|
|
|
static int retcode = 0;
|
2020-05-24 04:42:36 +10:00
|
|
|
WINDOW *bw, *dw, *w;
|
2020-12-07 11:50:57 +11:00
|
|
|
static uint64_t blkop = 0; // 5 bytes
|
|
|
|
static FILE *blkfp;
|
2020-05-24 04:42:36 +10:00
|
|
|
|
|
|
|
void debug_panel()
|
|
|
|
{
|
|
|
|
char buf[30];
|
|
|
|
emul_debugstr(buf);
|
|
|
|
mvwaddnstr(dw, 0, 0, buf, 30);
|
|
|
|
wrefresh(dw);
|
|
|
|
}
|
2020-03-10 13:26:02 +11:00
|
|
|
|
|
|
|
static uint8_t iord_stdio()
|
|
|
|
{
|
2020-05-23 22:24:39 +10:00
|
|
|
int c;
|
|
|
|
if (fp != NULL) {
|
|
|
|
c = getc(fp);
|
|
|
|
} else {
|
2020-05-24 04:42:36 +10:00
|
|
|
debug_panel();
|
2020-05-24 04:20:50 +10:00
|
|
|
c = wgetch(w);
|
2020-05-23 22:24:39 +10:00
|
|
|
}
|
2020-03-10 13:26:02 +11:00
|
|
|
if (c == EOF) {
|
2020-05-14 11:44:46 +10:00
|
|
|
c = 4; // ASCII EOT
|
2020-03-10 13:26:02 +11:00
|
|
|
}
|
|
|
|
return (uint8_t)c;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void iowr_stdio(uint8_t val)
|
|
|
|
{
|
2020-05-23 22:24:39 +10:00
|
|
|
if (fp != NULL) {
|
|
|
|
putchar(val);
|
|
|
|
} else {
|
|
|
|
if (val >= 0x20 || val == '\n') {
|
2020-05-24 04:20:50 +10:00
|
|
|
wechochar(w, val);
|
2020-05-24 04:47:19 +10:00
|
|
|
} else if (val == 0x08) {
|
|
|
|
int y, x; getyx(w, y, x);
|
|
|
|
wmove(w, y, x-1);
|
2020-05-23 22:24:39 +10:00
|
|
|
}
|
|
|
|
}
|
2020-03-10 13:26:02 +11:00
|
|
|
}
|
|
|
|
|
2020-12-07 11:50:57 +11:00
|
|
|
static void iowr_blk(uint8_t val)
|
|
|
|
{
|
|
|
|
blkop <<= 8;
|
|
|
|
blkop |= val;
|
|
|
|
uint8_t rw = blkop >> 32;
|
|
|
|
if (rw) {
|
|
|
|
uint16_t blkid = (blkop >> 16);
|
|
|
|
uint16_t dest = blkop & 0xffff;
|
|
|
|
blkop = 0;
|
|
|
|
fseek(blkfp, blkid*1024, SEEK_SET);
|
|
|
|
if (rw==2) { // write
|
|
|
|
fwrite(&m->mem[dest], 1024, 1, blkfp);
|
|
|
|
} else { // read
|
|
|
|
fread(&m->mem[dest], 1024, 1, blkfp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-20 06:43:48 +11:00
|
|
|
static void iowr_ret(uint8_t val)
|
|
|
|
{
|
|
|
|
retcode = val;
|
|
|
|
}
|
|
|
|
|
2020-05-24 10:15:28 +10:00
|
|
|
static void iowr_setx(uint8_t val)
|
|
|
|
{
|
|
|
|
int y, x; getyx(w, y, x);
|
|
|
|
wmove(w, y, val);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void iowr_sety(uint8_t val)
|
|
|
|
{
|
|
|
|
int y, x; getyx(w, y, x);
|
|
|
|
wmove(w, val, x);
|
|
|
|
}
|
|
|
|
|
2020-05-24 04:20:50 +10:00
|
|
|
int main(int argc, char *argv[])
|
2020-03-10 13:26:02 +11:00
|
|
|
{
|
2020-12-07 11:50:57 +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;
|
|
|
|
}
|
|
|
|
fseek(blkfp, 0, SEEK_END);
|
|
|
|
if (ftell(blkfp) < 100 * 1024) {
|
|
|
|
fclose(blkfp);
|
|
|
|
fprintf(stderr, "emul/blkfs too small, something's wrong, aborting.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
fseek(blkfp, 0, SEEK_SET);
|
|
|
|
m = emul_init(FBIN_PATH, 0);
|
2020-05-24 00:08:40 +10:00
|
|
|
if (m == NULL) {
|
2020-05-23 23:54:26 +10:00
|
|
|
return 1;
|
2020-04-25 08:01:55 +10:00
|
|
|
}
|
2020-03-10 13:26:02 +11:00
|
|
|
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-12-07 11:50:57 +11:00
|
|
|
m->iowr[BLK_PORT] = iowr_blk;
|
2020-05-24 10:15:28 +10:00
|
|
|
m->iowr[SETX_PORT] = iowr_setx;
|
|
|
|
m->iowr[SETY_PORT] = iowr_sety;
|
2020-05-24 04:20:50 +10:00
|
|
|
w = NULL;
|
2020-05-23 22:24:39 +10:00
|
|
|
if (argc == 2) {
|
|
|
|
fp = fopen(argv[1], "r");
|
|
|
|
if (fp == NULL) {
|
|
|
|
fprintf(stderr, "Can't open %s\n", argv[1]);
|
|
|
|
return 1;
|
|
|
|
}
|
2020-05-24 04:20:50 +10:00
|
|
|
while (emul_step());
|
2020-05-23 22:24:39 +10:00
|
|
|
fclose(fp);
|
|
|
|
} else if (argc == 1) {
|
|
|
|
fp = NULL;
|
|
|
|
initscr(); cbreak(); noecho(); nl(); clear();
|
2020-05-24 04:42:36 +10:00
|
|
|
// border window
|
2020-05-24 04:20:50 +10:00
|
|
|
bw = newwin(WLINES+2, WCOLS+2, 0, 0);
|
|
|
|
wborder(bw, 0, 0, 0, 0, 0, 0, 0, 0);
|
|
|
|
wrefresh(bw);
|
2020-05-24 04:42:36 +10:00
|
|
|
// debug panel
|
|
|
|
dw = newwin(1, 30, LINES-1, COLS-30);
|
2020-05-24 04:20:50 +10:00
|
|
|
w = newwin(WLINES, WCOLS, 1, 1);
|
|
|
|
scrollok(w, 1);
|
2020-05-24 04:42:36 +10:00
|
|
|
while (emul_steps(1000)) {
|
|
|
|
debug_panel();
|
|
|
|
}
|
|
|
|
nocbreak(); echo(); delwin(w); delwin(bw); delwin(dw); endwin();
|
2020-05-23 22:24:39 +10:00
|
|
|
printf("\nDone!\n");
|
|
|
|
emul_printdebug();
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "Usage: ./forth [filename]\n");
|
2020-05-24 04:20:50 +10:00
|
|
|
retcode = 1;
|
2020-05-23 22:24:39 +10:00
|
|
|
}
|
2020-12-07 11:50:57 +11:00
|
|
|
fclose(blkfp);
|
2020-05-24 04:20:50 +10:00
|
|
|
return retcode;
|
2020-05-23 22:24:39 +10:00
|
|
|
}
|