From 7f27d63c19d470c2ef96a169e5c144e433b3eaf8 Mon Sep 17 00:00:00 2001 From: Virgil Dupras Date: Thu, 9 May 2019 14:09:40 -0400 Subject: [PATCH] Move apps/zasm/emul to tools/emul --- apps/zasm/emul/.gitignore | 4 -- apps/zasm/emul/Makefile | 12 ---- apps/zasm/emul/bin2c.sh | 5 -- apps/zasm/emul/glue.asm | 28 -------- apps/zasm/emul/zasm.c | 81 --------------------- apps/zasm/main.asm | 9 ++- apps/zasm/tests/Makefile | 6 ++ apps/zasm/tests/runtests.sh | 2 +- tools/emul/.gitignore | 4 +- tools/emul/Makefile | 16 ++++- tools/emul/shell.c | 5 +- tools/emul/zasm.c | 84 ++++++++++++++++++++++ tools/emul/zasm_glue.asm | 51 +++++++++++++ .../zasm/emul/user.inc => tools/emul/zasm_user.asm | 7 +- 14 files changed, 169 insertions(+), 145 deletions(-) delete mode 100644 apps/zasm/emul/.gitignore delete mode 100644 apps/zasm/emul/Makefile delete mode 100755 apps/zasm/emul/bin2c.sh delete mode 100644 apps/zasm/emul/glue.asm delete mode 100644 apps/zasm/emul/zasm.c create mode 100644 apps/zasm/tests/Makefile create mode 100644 tools/emul/zasm.c create mode 100644 tools/emul/zasm_glue.asm rename apps/zasm/emul/user.inc => tools/emul/zasm_user.asm (71%) diff --git a/apps/zasm/emul/.gitignore b/apps/zasm/emul/.gitignore deleted file mode 100644 index 367abc6..0000000 --- a/apps/zasm/emul/.gitignore +++ /dev/null @@ -1,4 +0,0 @@ -libz80 -kernel.h -zasm.h -zasm diff --git a/apps/zasm/emul/Makefile b/apps/zasm/emul/Makefile deleted file mode 100644 index 6a1bd8c..0000000 --- a/apps/zasm/emul/Makefile +++ /dev/null @@ -1,12 +0,0 @@ -zasm: zasm.c libz80/libz80.o kernel.h zasm.h - cc $< libz80/libz80.o -o $@ - -libz80/libz80.o: libz80/z80.c - make -C libz80/codegen opcodes - gcc -Wall -ansi -g -c -o libz80/libz80.o libz80/z80.c - -kernel.h: glue.asm - scas -o - -I ../../../parts/z80 $< | ./bin2c.sh KERNEL | tee $@ > /dev/null - -zasm.h: $(addprefix ../, main.asm instr.asm directive.asm tok.asm parse.asm literal.asm util.asm) - scas -o - -I.. $< | ./bin2c.sh ZASM | tee $@ > /dev/null diff --git a/apps/zasm/emul/bin2c.sh b/apps/zasm/emul/bin2c.sh deleted file mode 100755 index 62f0c09..0000000 --- a/apps/zasm/emul/bin2c.sh +++ /dev/null @@ -1,5 +0,0 @@ -#!/bin/sh - -echo "unsigned char $1[] = { " -xxd -i - -echo " };" diff --git a/apps/zasm/emul/glue.asm b/apps/zasm/emul/glue.asm deleted file mode 100644 index 4ad8c23..0000000 --- a/apps/zasm/emul/glue.asm +++ /dev/null @@ -1,28 +0,0 @@ -#include "user.inc" -; Glue code for the emulated environment -ZASM_INPUT .equ 0xa000 -ZASM_OUTPUT .equ 0xd000 - -jr init ; 2 bytes -; *** JUMP TABLE *** -jp strncmp -jp addDE -jp upcase -jp unsetZ -jp intoDE - -init: - di - ld hl, RAMEND - ld sp, hl - ld hl, ZASM_INPUT - ld de, ZASM_OUTPUT - call USER_CODE - ; signal the emulator we're done - ; BC contains the number of written bytes - ld a, c - ld c, b - out (c), a - halt - -#include "core.asm" diff --git a/apps/zasm/emul/zasm.c b/apps/zasm/emul/zasm.c deleted file mode 100644 index 180f1ef..0000000 --- a/apps/zasm/emul/zasm.c +++ /dev/null @@ -1,81 +0,0 @@ -#include -#include -#include "libz80/z80.h" -#include "kernel.h" -#include "zasm.h" - -/* zasm is a "pure memory" application. It starts up being told memory location - * to read and memory location to write. - * - * This program works be writing stdin in a specific location in memory, run - * 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 glue.asm -#define READFROM 0xa000 -#define WRITETO 0xd000 -#define ZASM_CODE_OFFSET 0x8000 - -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) -{ - return 0; -} - -static void io_write(int unused, uint16_t addr, uint8_t val) -{ - written = ((addr & 0xff) << 8) + (val & 0xff); - running = 0; -} - -static uint8_t mem_read(int unused, uint16_t addr) -{ - return mem[addr]; -} - -static void mem_write(int unused, uint16_t addr, uint8_t val) -{ - mem[addr] = val; -} - -int main() -{ - // initialize memory - for (int i=0; i /dev/null +zasm-kernel.h: zasm_glue.asm +$(KERNEL_HEADERS): + scas -o - -I ../../parts/z80 $< | ./bin2c.sh KERNEL | tee $@ > /dev/null + +zasm-user.h: zasm_user.asm + scas -o - -I ../../apps/zasm $< | ./bin2c.sh USERSPACE | tee $@ > /dev/null shell: shell.c libz80/libz80.o shell-kernel.h +zasm: zasm.c libz80/libz80.o zasm-kernel.h zasm-user.h +$(TARGETS): cc $< libz80/libz80.o -o $@ libz80/libz80.o: libz80/z80.c @@ -13,4 +23,4 @@ libz80/libz80.o: libz80/z80.c .PHONY: clean clean: - rm shell shell-kernel.h + rm -f $(TARGETS) $(KERNEL_HEADERS) diff --git a/tools/emul/shell.c b/tools/emul/shell.c index eb51930..ae544c4 100644 --- a/tools/emul/shell.c +++ b/tools/emul/shell.c @@ -18,7 +18,6 @@ */ // in sync with shell.asm -#define RAMSTART 0x4000 #define STDIO_PORT 0x00 #define STDIN_ST_PORT 0x01 @@ -79,8 +78,8 @@ int main() // initialize memory - for (int i=0; i +#include +#include "libz80/z80.h" +#include "zasm-kernel.h" +#include "zasm-user.h" + +/* zasm is a "pure memory" application. It starts up being told memory location + * to read and memory location to write. + * + * + * Memory layout: + * + * 0x0000 - 0x3fff: ROM code from zasm_glue.asm + * 0x4000 - 0xffff: Userspace + * + * I/O Ports: + * + * 0 - stdin / stdout + */ + +// in sync with zasm_glue.asm +#define USER_CODE 0x4000 +#define STDIO_PORT 0x00 + +static Z80Context cpu; +static uint8_t mem[0xffff]; + +static uint8_t io_read(int unused, uint16_t addr) +{ + addr &= 0xff; + if (addr == STDIO_PORT) { + int c = getchar(); + if (c == EOF) { + return 0; + } + return c; + } else { + fprintf(stderr, "Out of bounds I/O read: %d\n", addr); + return 0; + } +} + +static void io_write(int unused, uint16_t addr, uint8_t val) +{ + addr &= 0xff; + if (addr == STDIO_PORT) { + putchar(val); + } else { + fprintf(stderr, "Out of bounds I/O write: %d / %d\n", addr, val); + } +} + +static uint8_t mem_read(int unused, uint16_t addr) +{ + return mem[addr]; +} + +static void mem_write(int unused, uint16_t addr, uint8_t val) +{ + mem[addr] = val; +} + +int main() +{ + // initialize memory + for (int i=0; i