From 7ce40467961158b4f58f2461c82958a81ba20bfa Mon Sep 17 00:00:00 2001 From: Daniele Date: Sun, 6 Feb 2022 20:30:49 +0100 Subject: [PATCH] Setting up PAT80 Emulator --- .gitmodules | 3 +++ pat80-emulator/.gitignore | 1 + pat80-emulator/Makefile | 5 +++++ pat80-emulator/README.md | 9 +++++++++ pat80-emulator/main.c | 34 ++++++++++++++++++++++++++++++++++ pat80-emulator/z80 | 1 + 6 files changed, 53 insertions(+) create mode 100644 .gitmodules create mode 100644 pat80-emulator/.gitignore create mode 100644 pat80-emulator/Makefile create mode 100644 pat80-emulator/README.md create mode 100644 pat80-emulator/main.c create mode 160000 pat80-emulator/z80 diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..b28b723 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "pat80-emulator/z80"] + path = pat80-emulator/z80 + url = git@github.com:algodesigner/z80.git diff --git a/pat80-emulator/.gitignore b/pat80-emulator/.gitignore new file mode 100644 index 0000000..5eb94e9 --- /dev/null +++ b/pat80-emulator/.gitignore @@ -0,0 +1 @@ +pat80emu diff --git a/pat80-emulator/Makefile b/pat80-emulator/Makefile new file mode 100644 index 0000000..c75bc5a --- /dev/null +++ b/pat80-emulator/Makefile @@ -0,0 +1,5 @@ +build: + gcc -g -o pat80emu z80/console.c z80/z80.c main.c +clean: + rm -rf main +all: clean build diff --git a/pat80-emulator/README.md b/pat80-emulator/README.md new file mode 100644 index 0000000..c5f5489 --- /dev/null +++ b/pat80-emulator/README.md @@ -0,0 +1,9 @@ +# PAT80 Emulator +Uses algodesigner's Z80 emulator (see submodule in z80 folder). +Instances the Z80 object (representing the CPU) and: +- intercepts all memory access and returns the corresponding rom value (read from rom file) if PC is in first 32k of memory (TODO) +- intercepts all memory access and stops execution returning error if trying to write to first 32k of memory (TODO) + +## Usage +Build with `make all` +Run with `./pat80emu` diff --git a/pat80-emulator/main.c b/pat80-emulator/main.c new file mode 100644 index 0000000..6affdab --- /dev/null +++ b/pat80-emulator/main.c @@ -0,0 +1,34 @@ +#include +#include "z80/z80.h" + +/* + * The intercept function is invoked on each and every CPU instruction. It + * quits the simulation when the PC register reaches address #0005 by changing + * the virtual CPU status. + */ +static void intercept(void *ctx) +{ + z80 *cpu = ctx; + if (PC == 5) { + puts("\nReached address: 5, terminating the simulation..."); + cpu->status = 1; + } +} + +int main(int argc, char *argv[]) +{ + /* + * Instantiate the CPU, activate the debug mode, set the intercept + * function and run the virtual CPU until the termination criteria + * set by the intercept function, if any, are met. + */ + z80 *cpu = z80_new(); + cpu->debug = 1; + + // TODO: Copy rom to first 32k CPU memory + _RamWrite(0x03, 0x76); // As an example, here is how to set HALT at posizion 0x03. Use a loop to copy memory from a PAT80 OS rom file. + + z80_set_intercept(cpu, cpu, intercept); + z80_run(cpu); + z80_destroy(cpu); +} diff --git a/pat80-emulator/z80 b/pat80-emulator/z80 new file mode 160000 index 0000000..80df33f --- /dev/null +++ b/pat80-emulator/z80 @@ -0,0 +1 @@ +Subproject commit 80df33fc873e8eb374c1fcb33458a1b7220d20db