From f884918d73caf5b137850498c67e71a9bf60d840 Mon Sep 17 00:00:00 2001 From: Virgil Dupras Date: Sat, 23 May 2020 08:24:39 -0400 Subject: [PATCH] emul: use ncurses in /emul/forth It doesn't change anything featurewise, but this change is in preparation for the addition of an eventual AT-XY. --- emul/Makefile | 2 +- emul/README.md | 7 ++++- emul/forth.c | 78 ++++++++++++++++++++++++++------------------------ 3 files changed, 48 insertions(+), 39 deletions(-) diff --git a/emul/Makefile b/emul/Makefile index 6269ab0..7c5f45a 100644 --- a/emul/Makefile +++ b/emul/Makefile @@ -28,7 +28,7 @@ blkfs-bin.h: blkfs $(BIN2C) $(BIN2C) BLKFS < blkfs > $@ forth: forth.c $(OBJS) forth-bin.h blkfs-bin.h - $(CC) forth.c $(OBJS) -o $@ + $(CC) forth.c $(OBJS) -lncurses -o $@ libz80/libz80.o: libz80/z80.c $(MAKE) -C libz80/codegen opcodes diff --git a/emul/README.md b/emul/README.md index 44181da..78e71ee 100644 --- a/emul/README.md +++ b/emul/README.md @@ -3,6 +3,11 @@ This folder contains a couple of tools running under the [libz80][libz80] emulator. +## Requirements + +You need `ncurses` to build the `forth` executable. In debian-based distros, +it's `libncurses5-dev`. + ## Not real hardware In the few emulated apps described below, we don't try to emulate real hardware @@ -20,7 +25,7 @@ First, make sure that the `libz80` git submodule is checked out. If not, run After that, you can run `make` and it builds the `forth` interpreter. -Run `./forth` to get the COllapse OS prompt. Type `0 LIST` for help. +Run `./forth` to get the Collapse OS prompt. Type `0 LIST` for help. ## Problems? diff --git a/emul/forth.c b/emul/forth.c index 0dda057..0cfc8d2 100644 --- a/emul/forth.c +++ b/emul/forth.c @@ -1,6 +1,7 @@ #include #include #include +#include #include #include "emul.h" #include "forth-bin.h" @@ -24,7 +25,12 @@ static uint16_t blkid = 0; static uint8_t iord_stdio() { - int c = getc(fp); + int c; + if (fp != NULL) { + c = getc(fp); + } else { + c = getch(); + } if (c == EOF) { c = 4; // ASCII EOT } @@ -33,7 +39,13 @@ static uint8_t iord_stdio() static void iowr_stdio(uint8_t val) { - putchar(val); + if (fp != NULL) { + putchar(val); + } else { + if (val >= 0x20 || val == '\n') { + echochar(val); + } + } } static void iowr_ret(uint8_t val) @@ -69,34 +81,8 @@ static void iowr_blkdata(uint8_t val) } } - -int main(int argc, char *argv[]) +int run() { - bool tty = false; - struct termios termInfo; - if (argc == 2) { - fp = fopen(argv[1], "r"); - if (fp == NULL) { - fprintf(stderr, "Can't open %s\n", argv[1]); - return 1; - } - } 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; - } blkfp = fopen("blkfs", "r+"); if (blkfp) { fprintf(stderr, "Using blkfs file\n"); @@ -120,16 +106,34 @@ int main(int argc, char *argv[]) // Run! while (emul_step()); - if (tty) { - printf("\nDone!\n"); - termInfo.c_lflag |= ECHO; - termInfo.c_lflag |= ICANON; - tcsetattr(0, TCSAFLUSH, &termInfo); - emul_printdebug(); - } if (blkfp != NULL) { fclose(blkfp); } - fclose(fp); return retcode; } + +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; + } +}