From 93c6d150e21a17afdd25a69c2cf952b7bae21740 Mon Sep 17 00:00:00 2001 From: Virgil Dupras Date: Sat, 23 May 2020 09:54:26 -0400 Subject: [PATCH] emul: don't embed forth.bin in emul binaries Read the contents of forth.bin at runtime. This allows us to get rid of the bin2c tooling. --- emul/Makefile | 18 ++++++------------ emul/forth.c | 42 +++++++++++++++++++++++------------------- emul/stage.c | 19 ++++++++++++++----- tools/Makefile | 4 +--- tools/bin2c.c | 50 -------------------------------------------------- 5 files changed, 44 insertions(+), 89 deletions(-) delete mode 100644 tools/bin2c.c diff --git a/emul/Makefile b/emul/Makefile index c78c3c9..1620026 100644 --- a/emul/Makefile +++ b/emul/Makefile @@ -1,6 +1,5 @@ TARGETS = forth stage blkfs OBJS = emul.o libz80/libz80.o -BIN2C = ../tools/bin2c BLKPACK = ../tools/blkpack BLKUNPACK = ../tools/blkunpack @@ -10,22 +9,17 @@ all: $(TARGETS) $(BLKPACK): $(MAKE) -C ../tools -.PHONY: $(BIN2C) $(BLKUNPACK) -$(BIN2C): $(BLKPACK) +.PHONY: $(BLKUNPACK) $(BLKUNPACK): $(BLKPACK) -# not dependent on forth.bin to avoid circular deps. -forth-bin.h: $(BIN2C) - $(BIN2C) KERNEL < forth.bin > $@ - -stage: stage.c $(OBJS) forth-bin.h - $(CC) stage.c -DBLKFS_PATH=\"`pwd`/blkfs\" $(OBJS) -o $@ +stage: stage.c $(OBJS) + $(CC) stage.c -DFBIN_PATH=\"`pwd`/forth.bin\" -DBLKFS_PATH=\"`pwd`/blkfs\" $(OBJS) -o $@ blkfs: $(BLKPACK) $(BLKPACK) ../blk > $@ -forth: forth.c $(OBJS) forth-bin.h - $(CC) forth.c -DBLKFS_PATH=\"`pwd`/blkfs\" $(OBJS) -lncurses -o $@ +forth: forth.c $(OBJS) + $(CC) forth.c -DFBIN_PATH=\"`pwd`/forth.bin\" -DBLKFS_PATH=\"`pwd`/blkfs\" $(OBJS) -lncurses -o $@ libz80/libz80.o: libz80/z80.c $(MAKE) -C libz80/codegen opcodes @@ -37,7 +31,7 @@ emul.o: emul.c .PHONY: updatebootstrap updatebootstrap: stage xcomp.fs pack - ./stage < xcomp.fs > forth.bin + ./stage < xcomp.fs | tee forth.bin > /dev/null .PHONY: pack pack: diff --git a/emul/forth.c b/emul/forth.c index cf4a5e9..3965599 100644 --- a/emul/forth.c +++ b/emul/forth.c @@ -4,7 +4,12 @@ #include #include #include "emul.h" -#include "forth-bin.h" +#ifndef FBIN_PATH +#error FBIN_PATH needed +#endif +#ifndef BLKFS_PATH +#error BLKFS_PATH needed +#endif // in sync with glue.asm #define RAMSTART 0x900 @@ -56,41 +61,32 @@ static void iowr_blk(uint8_t val) { blkid <<= 8; blkid |= val; - if (blkfp != NULL) { - fseek(blkfp, blkid*1024, SEEK_SET); - } + fseek(blkfp, blkid*1024, SEEK_SET); } static uint8_t iord_blkdata() { uint8_t res = 0; - if (blkfp != NULL) { - int c = getc(blkfp); - if (c != EOF) { - res = c; - } + int c = getc(blkfp); + if (c != EOF) { + res = c; } return res; } static void iowr_blkdata(uint8_t val) { - if (blkfp != NULL) { - putc(val, blkfp); - } + putc(val, blkfp); } int run() { -#ifdef BLKFS_PATH fprintf(stderr, "Using blkfs %s\n", BLKFS_PATH); blkfp = fopen(BLKFS_PATH, "r+"); if (!blkfp) { fprintf(stderr, "Can't open\n"); + return 1; } -#else - blkfp = NULL; -#endif Machine *m = emul_init(); m->ramstart = RAMSTART; m->iord[STDIO_PORT] = iord_stdio; @@ -100,10 +96,18 @@ int run() m->iord[BLKDATA_PORT] = iord_blkdata; m->iowr[BLKDATA_PORT] = iowr_blkdata; // initialize memory - for (int i=0; imem[i] = KERNEL[i]; + FILE *bfp = fopen(FBIN_PATH, "r"); + if (!bfp) { + fprintf(stderr, "Can't open forth.bin\n"); + return 1; } - + int i = 0; + int c = getc(bfp); + while (c != EOF) { + m->mem[i++] = c; + c = getc(bfp); + } + fclose(bfp); // Run! while (emul_step()); diff --git a/emul/stage.c b/emul/stage.c index 48bf19b..cc38d7e 100644 --- a/emul/stage.c +++ b/emul/stage.c @@ -2,8 +2,9 @@ #include #include #include "emul.h" -#include "forth-bin.h" - +#ifndef FBIN_PATH +#error FBIN_PATH needed +#endif #ifndef BLKFS_PATH #error BLKFS_PATH needed #endif @@ -92,10 +93,18 @@ int main(int argc, char *argv[]) m->iowr[BLK_PORT] = iowr_blk; m->iord[BLKDATA_PORT] = iord_blkdata; // initialize memory - for (int i=0; imem[i] = KERNEL[i]; + FILE *bfp = fopen(FBIN_PATH, "r"); + if (!bfp) { + fprintf(stderr, "Can't open forth.bin\n"); + return 1; } - + int i = 0; + int c = getc(bfp); + while (c != EOF) { + m->mem[i++] = c; + c = getc(bfp); + } + fclose(bfp); // Run! running = 1; diff --git a/tools/Makefile b/tools/Makefile index 322f4ca..853a681 100644 --- a/tools/Makefile +++ b/tools/Makefile @@ -2,13 +2,12 @@ MEMDUMP_TGT = memdump UPLOAD_TGT = upload TTYSAFE_TGT = ttysafe PINGPONG_TGT = pingpong -BIN2C_TGT = bin2c EXEC_TGT = exec BLKPACK_TGT = blkpack BLKUNPACK_TGT = blkunpack BLKUP_TGT = blkup TARGETS = $(MEMDUMP_TGT) $(UPLOAD_TGT) \ - $(TTYSAFE_TGT) $(PINGPONG_TGT) $(BIN2C_TGT) $(EXEC_TGT) $(BLKPACK_TGT) \ + $(TTYSAFE_TGT) $(PINGPONG_TGT) $(EXEC_TGT) $(BLKPACK_TGT) \ $(BLKUNPACK_TGT) $(BLKUP_TGT) OBJS = common.o @@ -22,7 +21,6 @@ $(MEMDUMP_TGT): $(MEMDUMP_TGT).c $(UPLOAD_TGT): $(UPLOAD_TGT).c $(TTYSAFE_TGT): $(TTYSAFE_TGT).c $(PINGPONG_TGT): $(PINGPONG_TGT).c -$(BIN2C_TGT): $(BIN2C_TGT).c $(EXEC_TGT): $(EXEC_TGT).c $(BLKPACK_TGT): $(BLKPACK_TGT).c $(BLKUNPACK_TGT): $(BLKUNPACK_TGT).c diff --git a/tools/bin2c.c b/tools/bin2c.c deleted file mode 100644 index 93843b7..0000000 --- a/tools/bin2c.c +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright (c) 2020 Byron Grobe - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#include -#include -#include - -#define BUFSZ 32 - -static const char intro[] = "static const unsigned char %s[] = {\n "; - -int main(int argc, char **argv) { - int n; - int col = 0; - uint8_t buf[BUFSZ]; - - if (argc < 2) { - fprintf(stderr, "Specify a name for the data structure...\n"); - return 1; - } - - printf(intro, argv[1]); - - while(!feof(stdin)) { - n = fread(buf, 1, BUFSZ, stdin); - for(int i = 0; i < n; ++i) { - if (col+4 >= 76) { - printf("\n "); - col = 0; - } - printf("0x%.2x, ", buf[i]); - col += 6; - } - } - - printf("};\n"); -}