From 43c1005d616ef05c02f124d7563941f553beee29 Mon Sep 17 00:00:00 2001 From: Virgil Dupras Date: Tue, 16 Apr 2019 13:59:19 -0400 Subject: [PATCH] zasm: implement stdin/stdout in emulator --- apps/zasm/emul/wrapper.asm | 6 ++++-- apps/zasm/emul/zasm.c | 24 +++++++++++++++++++++--- apps/zasm/zasm.asm | 6 ++++-- 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/apps/zasm/emul/wrapper.asm b/apps/zasm/emul/wrapper.asm index c83630e..6bcb1f9 100644 --- a/apps/zasm/emul/wrapper.asm +++ b/apps/zasm/emul/wrapper.asm @@ -4,10 +4,12 @@ ld sp, hl ; zasm input ld hl, 0x9000 ; zasm output -ld hl, 0xc000 +ld de, 0xc000 call zasm ; signal the emulator we're done -out (0), a +; BC contains the number of written bytes +ld a, b +out (c), a halt zasm: ; beginning of the code diff --git a/apps/zasm/emul/zasm.c b/apps/zasm/emul/zasm.c index 751917d..7e688cb 100644 --- a/apps/zasm/emul/zasm.c +++ b/apps/zasm/emul/zasm.c @@ -1,4 +1,5 @@ #include +#include #include "libz80/z80.h" #include "wrapper.h" #include "zasm.h" @@ -10,9 +11,17 @@ * zasm in a special wrapper, wait until we receive the stop signal, then * spit the contents of the dest memory to stdout. */ + +// in sync with wrapper.asm +#define READFROM 0x9000 +#define WRITETO 0xc000 + static Z80Context cpu; static uint8_t mem[0xffff]; static int running; +// Number of bytes written to WRITETO +// We receive that result from an OUT (C), A call. C contains LSB, A is MSB. +static uint16_t written; static uint8_t io_read(int unused, uint16_t addr) @@ -22,8 +31,7 @@ static uint8_t io_read(int unused, uint16_t addr) static void io_write(int unused, uint16_t addr, uint8_t val) { - // zasm doesn't do any IO. If we receive any IO, it means that we're done - // because the wrapper told us through an "out" + written = (val << 8) + (addr & 0xff); running = 0; } @@ -48,6 +56,13 @@ int main() for (int i=0; i