From 6a804a9c64a30752535e8d3dc05b09de48b27fbf Mon Sep 17 00:00:00 2001 From: Virgil Dupras Date: Fri, 10 May 2019 19:55:01 -0400 Subject: [PATCH] zasm: buffer stdin in emulator Also, add a port for rewinding stdin. --- tools/emul/zasm.c | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/tools/emul/zasm.c b/tools/emul/zasm.c index b7772e2..adaca66 100644 --- a/tools/emul/zasm.c +++ b/tools/emul/zasm.c @@ -18,24 +18,32 @@ * I/O Ports: * * 0 - stdin / stdout + * 1 - When written to, rewind stdin buffer to the beginning. */ // in sync with zasm_glue.asm #define USER_CODE 0x4800 #define STDIO_PORT 0x00 +#define STDIN_REWIND 0x01 +// Other consts +#define STDIN_BUFSIZE 0x8000 static Z80Context cpu; -static uint8_t mem[0xffff]; +static uint8_t mem[0x10000]; +// STDIN buffer, allows us to seek and tell +static uint8_t inpt[STDIN_BUFSIZE]; +static int inpt_size; +static int inpt_ptr; static uint8_t io_read(int unused, uint16_t addr) { addr &= 0xff; if (addr == STDIO_PORT) { - int c = getchar(); - if (c == EOF) { + if (inpt_ptr < inpt_size) { + return inpt[inpt_ptr++]; + } else { return 0; } - return c; } else { fprintf(stderr, "Out of bounds I/O read: %d\n", addr); return 0; @@ -47,6 +55,8 @@ static void io_write(int unused, uint16_t addr, uint8_t val) addr &= 0xff; if (addr == STDIO_PORT) { putchar(val); + } else if (addr == STDIN_REWIND) { + inpt_ptr = 0; } else { fprintf(stderr, "Out of bounds I/O write: %d / %d\n", addr, val); } @@ -71,6 +81,20 @@ int main() for (int i=0; i