1
0
mirror of https://github.com/hsoft/collapseos.git synced 2024-11-17 07:08:05 +11:00

emul: deduplicate some code

This commit is contained in:
Virgil Dupras 2020-05-23 10:08:40 -04:00
parent 93c6d150e2
commit 1d4b75add2
5 changed files with 67 additions and 102 deletions

View File

@ -13,20 +13,20 @@ $(BLKPACK):
$(BLKUNPACK): $(BLKPACK) $(BLKUNPACK): $(BLKPACK)
stage: stage.c $(OBJS) stage: stage.c $(OBJS)
$(CC) stage.c -DFBIN_PATH=\"`pwd`/forth.bin\" -DBLKFS_PATH=\"`pwd`/blkfs\" $(OBJS) -o $@ $(CC) stage.c $(OBJS) -o $@
blkfs: $(BLKPACK) blkfs: $(BLKPACK)
$(BLKPACK) ../blk > $@ $(BLKPACK) ../blk > $@
forth: forth.c $(OBJS) forth: forth.c $(OBJS)
$(CC) forth.c -DFBIN_PATH=\"`pwd`/forth.bin\" -DBLKFS_PATH=\"`pwd`/blkfs\" $(OBJS) -lncurses -o $@ $(CC) forth.c $(OBJS) -lncurses -o $@
libz80/libz80.o: libz80/z80.c libz80/libz80.o: libz80/z80.c
$(MAKE) -C libz80/codegen opcodes $(MAKE) -C libz80/codegen opcodes
$(CC) -Wall -ansi -g -c -o libz80/libz80.o libz80/z80.c $(CC) -Wall -ansi -g -c -o libz80/libz80.o libz80/z80.c
emul.o: emul.c emul.o: emul.c
$(CC) -c -o emul.o emul.c $(CC) -DFBIN_PATH=\"`pwd`/forth.bin\" -DBLKFS_PATH=\"`pwd`/blkfs\" -c -o emul.o emul.c
.PHONY: updatebootstrap .PHONY: updatebootstrap

View File

@ -1,13 +1,26 @@
/* Common code between shell, zasm and runbin. /* Common code between forth and stage binaries.
They all run on the same kind of virtual machine: A z80 CPU, 64K of RAM/ROM. They all run on the same kind of virtual machine: A z80 CPU, 64K of RAM/ROM.
*/ */
#include <string.h> #include <string.h>
#include "emul.h" #include "emul.h"
// Port for block reads. Write 2 bytes, MSB first, on that port and then
// read 1024 bytes from the DATA port.
#define BLK_PORT 0x03
#define BLKDATA_PORT 0x04
#ifndef BLKFS_PATH
#error BLKFS_PATH needed
#endif
#ifndef FBIN_PATH
#error FBIN_PATH needed
#endif
static Machine m; static Machine m;
static ushort traceval = 0; static ushort traceval = 0;
static uint16_t blkid = 0;
static FILE *blkfp;
static uint8_t io_read(int unused, uint16_t addr) static uint8_t io_read(int unused, uint16_t addr)
{ {
@ -32,6 +45,23 @@ static void io_write(int unused, uint16_t addr, uint8_t val)
} }
} }
static void iowr_blk(uint8_t val)
{
blkid <<= 8;
blkid |= val;
fseek(blkfp, blkid*1024, SEEK_SET);
}
static uint8_t iord_blkdata()
{
return getc(blkfp);
}
static void iowr_blkdata(uint8_t val)
{
putc(val, blkfp);
}
static uint8_t mem_read(int unused, uint16_t addr) static uint8_t mem_read(int unused, uint16_t addr)
{ {
return m.mem[addr]; return m.mem[addr];
@ -50,7 +80,26 @@ static void mem_write(int unused, uint16_t addr, uint8_t val)
Machine* emul_init() Machine* emul_init()
{ {
fprintf(stderr, "Using blkfs %s\n", BLKFS_PATH);
blkfp = fopen(BLKFS_PATH, "r+");
if (!blkfp) {
fprintf(stderr, "Can't open\n");
return NULL;
}
// initialize memory
memset(m.mem, 0, 0x10000); memset(m.mem, 0, 0x10000);
FILE *bfp = fopen(FBIN_PATH, "r");
if (!bfp) {
fprintf(stderr, "Can't open forth.bin\n");
return NULL;
}
int i = 0;
int c = getc(bfp);
while (c != EOF) {
m.mem[i++] = c;
c = getc(bfp);
}
fclose(bfp);
m.ramstart = 0; m.ramstart = 0;
m.minsp = 0xffff; m.minsp = 0xffff;
m.maxix = 0; m.maxix = 0;
@ -63,9 +112,16 @@ Machine* emul_init()
m.cpu.memWrite = mem_write; m.cpu.memWrite = mem_write;
m.cpu.ioRead = io_read; m.cpu.ioRead = io_read;
m.cpu.ioWrite = io_write; m.cpu.ioWrite = io_write;
m.iowr[BLK_PORT] = iowr_blk;
m.iord[BLKDATA_PORT] = iord_blkdata;
m.iowr[BLKDATA_PORT] = iowr_blkdata;
return &m; return &m;
} }
void emul_deinit()
{
fclose(blkfp);
}
bool emul_step() bool emul_step()
{ {

View File

@ -29,6 +29,7 @@ typedef enum {
} Tristate; } Tristate;
Machine* emul_init(); Machine* emul_init();
void emul_deinit();
bool emul_step(); bool emul_step();
bool emul_steps(unsigned int steps); bool emul_steps(unsigned int steps);
void emul_loop(); void emul_loop();

View File

@ -4,12 +4,6 @@
#include <curses.h> #include <curses.h>
#include <termios.h> #include <termios.h>
#include "emul.h" #include "emul.h"
#ifndef FBIN_PATH
#error FBIN_PATH needed
#endif
#ifndef BLKFS_PATH
#error BLKFS_PATH needed
#endif
// in sync with glue.asm // in sync with glue.asm
#define RAMSTART 0x900 #define RAMSTART 0x900
@ -23,9 +17,7 @@
#define BLKDATA_PORT 0x04 #define BLKDATA_PORT 0x04
static FILE *fp; static FILE *fp;
static FILE *blkfp;
static int retcode = 0; static int retcode = 0;
static uint16_t blkid = 0;
static uint8_t iord_stdio() static uint8_t iord_stdio()
{ {
@ -57,63 +49,20 @@ static void iowr_ret(uint8_t val)
retcode = val; retcode = val;
} }
static void iowr_blk(uint8_t val)
{
blkid <<= 8;
blkid |= val;
fseek(blkfp, blkid*1024, SEEK_SET);
}
static uint8_t iord_blkdata()
{
uint8_t res = 0;
int c = getc(blkfp);
if (c != EOF) {
res = c;
}
return res;
}
static void iowr_blkdata(uint8_t val)
{
putc(val, blkfp);
}
int run() int run()
{ {
fprintf(stderr, "Using blkfs %s\n", BLKFS_PATH); Machine *m = emul_init();
blkfp = fopen(BLKFS_PATH, "r+"); if (m == NULL) {
if (!blkfp) {
fprintf(stderr, "Can't open\n");
return 1; return 1;
} }
Machine *m = emul_init();
m->ramstart = RAMSTART; m->ramstart = RAMSTART;
m->iord[STDIO_PORT] = iord_stdio; m->iord[STDIO_PORT] = iord_stdio;
m->iowr[STDIO_PORT] = iowr_stdio; m->iowr[STDIO_PORT] = iowr_stdio;
m->iowr[RET_PORT] = iowr_ret; m->iowr[RET_PORT] = iowr_ret;
m->iowr[BLK_PORT] = iowr_blk;
m->iord[BLKDATA_PORT] = iord_blkdata;
m->iowr[BLKDATA_PORT] = iowr_blkdata;
// initialize memory
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! // Run!
while (emul_step()); while (emul_step());
if (blkfp != NULL) { emul_deinit();
fclose(blkfp);
}
return retcode; return retcode;
} }

View File

@ -2,12 +2,6 @@
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
#include "emul.h" #include "emul.h"
#ifndef FBIN_PATH
#error FBIN_PATH needed
#endif
#ifndef BLKFS_PATH
#error BLKFS_PATH needed
#endif
/* Staging binaries /* Staging binaries
@ -29,10 +23,6 @@ trouble of compiling defs to binary.
// To know which part of RAM to dump, we listen to port 2, which at the end of // To know which part of RAM to dump, we listen to port 2, which at the end of
// its compilation process, spits its HERE addr to port 2 (MSB first) // its compilation process, spits its HERE addr to port 2 (MSB first)
#define HERE_PORT 0x02 #define HERE_PORT 0x02
// Port for block reads. Write 2 bytes, MSB first, on that port and then
// read 1024 bytes from the DATA port.
#define BLK_PORT 0x03
#define BLKDATA_PORT 0x04
static int running; static int running;
// We support double-pokes, that is, a first poke to tell where to start the // We support double-pokes, that is, a first poke to tell where to start the
@ -40,8 +30,6 @@ static int running;
// then ending HERE and we start at sizeof(KERNEL). // then ending HERE and we start at sizeof(KERNEL).
static uint16_t start_here = 0; static uint16_t start_here = 0;
static uint16_t end_here = 0; static uint16_t end_here = 0;
static uint16_t blkid = 0;
static FILE *blkfp;
static uint8_t iord_stdio() static uint8_t iord_stdio()
{ {
@ -65,46 +53,16 @@ static void iowr_here(uint8_t val)
end_here |= val; end_here |= val;
} }
static void iowr_blk(uint8_t val)
{
blkid <<= 8;
blkid |= val;
fseek(blkfp, blkid*1024, SEEK_SET);
}
static uint8_t iord_blkdata()
{
return getc(blkfp);
}
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
fprintf(stderr, "Using blkfs %s\n", BLKFS_PATH); Machine *m = emul_init();
blkfp = fopen(BLKFS_PATH, "r+"); if (m == NULL) {
if (!blkfp) {
fprintf(stderr, "Can't open\n");
return 1; return 1;
} }
Machine *m = emul_init();
m->ramstart = RAMSTART; m->ramstart = RAMSTART;
m->iord[STDIO_PORT] = iord_stdio; m->iord[STDIO_PORT] = iord_stdio;
m->iowr[STDIO_PORT] = iowr_stdio; m->iowr[STDIO_PORT] = iowr_stdio;
m->iowr[HERE_PORT] = iowr_here; m->iowr[HERE_PORT] = iowr_here;
m->iowr[BLK_PORT] = iowr_blk;
m->iord[BLKDATA_PORT] = iord_blkdata;
// initialize memory
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! // Run!
running = 1; running = 1;
@ -114,6 +72,7 @@ int main(int argc, char *argv[])
for (int i=start_here; i<end_here; i++) { for (int i=start_here; i<end_here; i++) {
putchar(m->mem[i]); putchar(m->mem[i]);
} }
emul_deinit();
emul_printdebug(); emul_printdebug();
return 0; return 0;
} }