commit a1818fa31d680a9e91d890c683e5de38ee7886ac Author: Dreaded_X Date: Thu Jan 14 20:31:32 2021 +0100 First commit diff --git a/.cache/clangd/index/C++.h.2401D1B67C3E4120.idx b/.cache/clangd/index/C++.h.2401D1B67C3E4120.idx new file mode 100644 index 0000000..21888c0 Binary files /dev/null and b/.cache/clangd/index/C++.h.2401D1B67C3E4120.idx differ diff --git a/.cache/clangd/index/C++.h.66AA42539FBA96BA.idx b/.cache/clangd/index/C++.h.66AA42539FBA96BA.idx new file mode 100644 index 0000000..ff3a3b6 Binary files /dev/null and b/.cache/clangd/index/C++.h.66AA42539FBA96BA.idx differ diff --git a/.cache/clangd/index/C++03.h.B195076A0AE7B2E6.idx b/.cache/clangd/index/C++03.h.B195076A0AE7B2E6.idx new file mode 100644 index 0000000..68f46be Binary files /dev/null and b/.cache/clangd/index/C++03.h.B195076A0AE7B2E6.idx differ diff --git a/.cache/clangd/index/C++11.h.5AD2FD7EDA1618AB.idx b/.cache/clangd/index/C++11.h.5AD2FD7EDA1618AB.idx new file mode 100644 index 0000000..efe0b65 Binary files /dev/null and b/.cache/clangd/index/C++11.h.5AD2FD7EDA1618AB.idx differ diff --git a/.cache/clangd/index/C++14.h.11AF50373395E7A3.idx b/.cache/clangd/index/C++14.h.11AF50373395E7A3.idx new file mode 100644 index 0000000..1140568 Binary files /dev/null and b/.cache/clangd/index/C++14.h.11AF50373395E7A3.idx differ diff --git a/.cache/clangd/index/C++17.h.2A317CBE2F03DCCF.idx b/.cache/clangd/index/C++17.h.2A317CBE2F03DCCF.idx new file mode 100644 index 0000000..29b8162 Binary files /dev/null and b/.cache/clangd/index/C++17.h.2A317CBE2F03DCCF.idx differ diff --git a/.cache/clangd/index/C++85.h.72873F572A9B234B.idx b/.cache/clangd/index/C++85.h.72873F572A9B234B.idx new file mode 100644 index 0000000..5b670a9 Binary files /dev/null and b/.cache/clangd/index/C++85.h.72873F572A9B234B.idx differ diff --git a/.cache/clangd/index/C++89.h.E0CC89A09CC8815E.idx b/.cache/clangd/index/C++89.h.E0CC89A09CC8815E.idx new file mode 100644 index 0000000..1fcf97f Binary files /dev/null and b/.cache/clangd/index/C++89.h.E0CC89A09CC8815E.idx differ diff --git a/.cache/clangd/index/C++98.h.A35E6B350058C29E.idx b/.cache/clangd/index/C++98.h.A35E6B350058C29E.idx new file mode 100644 index 0000000..91b80f1 Binary files /dev/null and b/.cache/clangd/index/C++98.h.A35E6B350058C29E.idx differ diff --git a/.cache/clangd/index/C.h.52542E65CD8DF377.idx b/.cache/clangd/index/C.h.52542E65CD8DF377.idx new file mode 100644 index 0000000..d6f5c6b Binary files /dev/null and b/.cache/clangd/index/C.h.52542E65CD8DF377.idx differ diff --git a/.cache/clangd/index/C.h.E9CED35EBD2379FA.idx b/.cache/clangd/index/C.h.E9CED35EBD2379FA.idx new file mode 100644 index 0000000..9b71783 Binary files /dev/null and b/.cache/clangd/index/C.h.E9CED35EBD2379FA.idx differ diff --git a/.cache/clangd/index/CPU.h.3B64B6F8352B60DE.idx b/.cache/clangd/index/CPU.h.3B64B6F8352B60DE.idx new file mode 100644 index 0000000..a9080cf Binary files /dev/null and b/.cache/clangd/index/CPU.h.3B64B6F8352B60DE.idx differ diff --git a/.cache/clangd/index/CPU.h.86C71609A0AD382A.idx b/.cache/clangd/index/CPU.h.86C71609A0AD382A.idx new file mode 100644 index 0000000..63b6b4e Binary files /dev/null and b/.cache/clangd/index/CPU.h.86C71609A0AD382A.idx differ diff --git a/.cache/clangd/index/Clang.h.810B6521E0367D37.idx b/.cache/clangd/index/Clang.h.810B6521E0367D37.idx new file mode 100644 index 0000000..5c0040f Binary files /dev/null and b/.cache/clangd/index/Clang.h.810B6521E0367D37.idx differ diff --git a/.cache/clangd/index/IEEE 754.h.BBD5F238C29CF144.idx b/.cache/clangd/index/IEEE 754.h.BBD5F238C29CF144.idx new file mode 100644 index 0000000..3918c7a Binary files /dev/null and b/.cache/clangd/index/IEEE 754.h.BBD5F238C29CF144.idx differ diff --git a/.cache/clangd/index/LP64.h.FE7A85088828CA5F.idx b/.cache/clangd/index/LP64.h.FE7A85088828CA5F.idx new file mode 100644 index 0000000..695069c Binary files /dev/null and b/.cache/clangd/index/LP64.h.FE7A85088828CA5F.idx differ diff --git a/.cache/clangd/index/Z80.c.2B1185EB33948A72.idx b/.cache/clangd/index/Z80.c.2B1185EB33948A72.idx new file mode 100644 index 0000000..5d3b102 Binary files /dev/null and b/.cache/clangd/index/Z80.c.2B1185EB33948A72.idx differ diff --git a/.cache/clangd/index/Z80.h.F70B8E12786EB857.idx b/.cache/clangd/index/Z80.h.F70B8E12786EB857.idx new file mode 100644 index 0000000..ed4119d Binary files /dev/null and b/.cache/clangd/index/Z80.h.F70B8E12786EB857.idx differ diff --git a/.cache/clangd/index/Z80.h.FC09AC704ACEAD83.idx b/.cache/clangd/index/Z80.h.FC09AC704ACEAD83.idx new file mode 100644 index 0000000..54aad5d Binary files /dev/null and b/.cache/clangd/index/Z80.h.FC09AC704ACEAD83.idx differ diff --git a/.cache/clangd/index/base.h.2C4973B192A9A146.idx b/.cache/clangd/index/base.h.2C4973B192A9A146.idx new file mode 100644 index 0000000..90cc14f Binary files /dev/null and b/.cache/clangd/index/base.h.2C4973B192A9A146.idx differ diff --git a/.cache/clangd/index/base.h.CCC8CD001F658584.idx b/.cache/clangd/index/base.h.CCC8CD001F658584.idx new file mode 100644 index 0000000..48ee82c Binary files /dev/null and b/.cache/clangd/index/base.h.CCC8CD001F658584.idx differ diff --git a/.cache/clangd/index/compiler.h.34CC29CE6E50A209.idx b/.cache/clangd/index/compiler.h.34CC29CE6E50A209.idx new file mode 100644 index 0000000..c6b7816 Binary files /dev/null and b/.cache/clangd/index/compiler.h.34CC29CE6E50A209.idx differ diff --git a/.cache/clangd/index/compiler.h.5D4E023D9859CAF8.idx b/.cache/clangd/index/compiler.h.5D4E023D9859CAF8.idx new file mode 100644 index 0000000..3c098c4 Binary files /dev/null and b/.cache/clangd/index/compiler.h.5D4E023D9859CAF8.idx differ diff --git a/.cache/clangd/index/completion.h.2BEA81DD4954E275.idx b/.cache/clangd/index/completion.h.2BEA81DD4954E275.idx new file mode 100644 index 0000000..a728877 Binary files /dev/null and b/.cache/clangd/index/completion.h.2BEA81DD4954E275.idx differ diff --git a/.cache/clangd/index/completion.h.43CBE4BCFBBF7F61.idx b/.cache/clangd/index/completion.h.43CBE4BCFBBF7F61.idx new file mode 100644 index 0000000..35532b4 Binary files /dev/null and b/.cache/clangd/index/completion.h.43CBE4BCFBBF7F61.idx differ diff --git a/.cache/clangd/index/completion.h.6C4A5B11A525FECF.idx b/.cache/clangd/index/completion.h.6C4A5B11A525FECF.idx new file mode 100644 index 0000000..e2a811c Binary files /dev/null and b/.cache/clangd/index/completion.h.6C4A5B11A525FECF.idx differ diff --git a/.cache/clangd/index/completion.h.AB31B65596A5437A.idx b/.cache/clangd/index/completion.h.AB31B65596A5437A.idx new file mode 100644 index 0000000..d52eb4d Binary files /dev/null and b/.cache/clangd/index/completion.h.AB31B65596A5437A.idx differ diff --git a/.cache/clangd/index/completion.h.E95591B156F9FADE.idx b/.cache/clangd/index/completion.h.E95591B156F9FADE.idx new file mode 100644 index 0000000..7380003 Binary files /dev/null and b/.cache/clangd/index/completion.h.E95591B156F9FADE.idx differ diff --git a/.cache/clangd/index/completion.h.FAFC8F88E848F62B.idx b/.cache/clangd/index/completion.h.FAFC8F88E848F62B.idx new file mode 100644 index 0000000..6275b81 Binary files /dev/null and b/.cache/clangd/index/completion.h.FAFC8F88E848F62B.idx differ diff --git a/.cache/clangd/index/data model.h.0D53C4824D43770C.idx b/.cache/clangd/index/data model.h.0D53C4824D43770C.idx new file mode 100644 index 0000000..9649b0a Binary files /dev/null and b/.cache/clangd/index/data model.h.0D53C4824D43770C.idx differ diff --git a/.cache/clangd/index/data model.h.C39D0E257B0FE15C.idx b/.cache/clangd/index/data model.h.C39D0E257B0FE15C.idx new file mode 100644 index 0000000..cb29139 Binary files /dev/null and b/.cache/clangd/index/data model.h.C39D0E257B0FE15C.idx differ diff --git a/.cache/clangd/index/endianness.h.53DF0FE2568E912C.idx b/.cache/clangd/index/endianness.h.53DF0FE2568E912C.idx new file mode 100644 index 0000000..ad3ca67 Binary files /dev/null and b/.cache/clangd/index/endianness.h.53DF0FE2568E912C.idx differ diff --git a/.cache/clangd/index/floating-point.h.179668C72300C758.idx b/.cache/clangd/index/floating-point.h.179668C72300C758.idx new file mode 100644 index 0000000..f128911 Binary files /dev/null and b/.cache/clangd/index/floating-point.h.179668C72300C758.idx differ diff --git a/.cache/clangd/index/language.h.2D34AFEE4FE63289.idx b/.cache/clangd/index/language.h.2D34AFEE4FE63289.idx new file mode 100644 index 0000000..a417d23 Binary files /dev/null and b/.cache/clangd/index/language.h.2D34AFEE4FE63289.idx differ diff --git a/.cache/clangd/index/language.h.CD2416B12E1823E3.idx b/.cache/clangd/index/language.h.CD2416B12E1823E3.idx new file mode 100644 index 0000000..7f2d46b Binary files /dev/null and b/.cache/clangd/index/language.h.CD2416B12E1823E3.idx differ diff --git a/.cache/clangd/index/language.h.FAF8421999A92FE5.idx b/.cache/clangd/index/language.h.FAF8421999A92FE5.idx new file mode 100644 index 0000000..12c0026 Binary files /dev/null and b/.cache/clangd/index/language.h.FAF8421999A92FE5.idx differ diff --git a/.cache/clangd/index/main.cpp.A20EA819DE56DA4B.idx b/.cache/clangd/index/main.cpp.A20EA819DE56DA4B.idx new file mode 100644 index 0000000..3446bd3 Binary files /dev/null and b/.cache/clangd/index/main.cpp.A20EA819DE56DA4B.idx differ diff --git a/.cache/clangd/index/members.h.F47A120DFC90731E.idx b/.cache/clangd/index/members.h.F47A120DFC90731E.idx new file mode 100644 index 0000000..a48ca01 Binary files /dev/null and b/.cache/clangd/index/members.h.F47A120DFC90731E.idx differ diff --git a/.cache/clangd/index/order.h.93B09CF94BB4D9BA.idx b/.cache/clangd/index/order.h.93B09CF94BB4D9BA.idx new file mode 100644 index 0000000..010e256 Binary files /dev/null and b/.cache/clangd/index/order.h.93B09CF94BB4D9BA.idx differ diff --git a/.cache/clangd/index/pasting.h.45E298C12ED21297.idx b/.cache/clangd/index/pasting.h.45E298C12ED21297.idx new file mode 100644 index 0000000..faccd81 Binary files /dev/null and b/.cache/clangd/index/pasting.h.45E298C12ED21297.idx differ diff --git a/.cache/clangd/index/pointer.h.97BF7A2A5C7979BB.idx b/.cache/clangd/index/pointer.h.97BF7A2A5C7979BB.idx new file mode 100644 index 0000000..5e36e20 Binary files /dev/null and b/.cache/clangd/index/pointer.h.97BF7A2A5C7979BB.idx differ diff --git a/.cache/clangd/index/structure.h.3880DD6BD13ABEFA.idx b/.cache/clangd/index/structure.h.3880DD6BD13ABEFA.idx new file mode 100644 index 0000000..f32c4c9 Binary files /dev/null and b/.cache/clangd/index/structure.h.3880DD6BD13ABEFA.idx differ diff --git a/.cache/clangd/index/templating.h.08C7E7A485DC7E55.idx b/.cache/clangd/index/templating.h.08C7E7A485DC7E55.idx new file mode 100644 index 0000000..9d7e969 Binary files /dev/null and b/.cache/clangd/index/templating.h.08C7E7A485DC7E55.idx differ diff --git a/.cache/clangd/index/value.h.2688048F4F1E12E4.idx b/.cache/clangd/index/value.h.2688048F4F1E12E4.idx new file mode 100644 index 0000000..0f46805 Binary files /dev/null and b/.cache/clangd/index/value.h.2688048F4F1E12E4.idx differ diff --git a/.cache/clangd/index/value.h.FE62A6DE21B0FD10.idx b/.cache/clangd/index/value.h.FE62A6DE21B0FD10.idx new file mode 100644 index 0000000..a0b4264 Binary files /dev/null and b/.cache/clangd/index/value.h.FE62A6DE21B0FD10.idx differ diff --git a/.cache/clangd/index/version.h.878DD9E595BAEF5C.idx b/.cache/clangd/index/version.h.878DD9E595BAEF5C.idx new file mode 100644 index 0000000..e1d4a1e Binary files /dev/null and b/.cache/clangd/index/version.h.878DD9E595BAEF5C.idx differ diff --git a/.cache/clangd/index/x86-64.h.7BE9C336324BE052.idx b/.cache/clangd/index/x86-64.h.7BE9C336324BE052.idx new file mode 100644 index 0000000..1a7ccd9 Binary files /dev/null and b/.cache/clangd/index/x86-64.h.7BE9C336324BE052.idx differ diff --git a/.cache/clangd/index/x87.h.B525DE6A4B5DBA24.idx b/.cache/clangd/index/x87.h.B525DE6A4B5DBA24.idx new file mode 100644 index 0000000..13f32c2 Binary files /dev/null and b/.cache/clangd/index/x87.h.B525DE6A4B5DBA24.idx differ diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e8de739 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.build/ +.clangd +compile_commands.json diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..5b81b7e --- /dev/null +++ b/.gitmodules @@ -0,0 +1,6 @@ +[submodule "libs/z80"] + path = libs/z80 + url = https://github.com/redcode/z80 +[submodule "libs/z"] + path = libs/z + url = https://github.com/EnricoTrudu/z diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..aba1d9b --- /dev/null +++ b/Makefile @@ -0,0 +1,39 @@ +CXX = g++ +BUILD = .build +TARGET = emulator +OPT = g -g +DEFINE = -DCPU_Z80_STATIC -DCPU_Z80_USE_LOCAL_HEADER + +CFLAGS = -Wall -Wextra -std=c++20 -O$(OPT) $(DEFINE) -flto -Iinclude `sdl2-config --cflags` -Ilibs/z80/API/emulation/CPU -Ilibs/z/API +LDFLAGS = -Wall -Wextra -O$(OPT) -flto `sdl2-config --libs` -lSDL2_image + +SRC = \ +src/main.cpp \ +libs/z80/sources/Z80.c \ + +OBJ_1 = $(notdir $(SRC:.cpp=.o)) +OBJ_2 = $(notdir $(OBJ_1:.c=.o)) +OBJ = $(addprefix $(BUILD)/, $(OBJ_2)) +# We can't use this as it will use the wrong main +# vpath %.c $(sort $(dir $(SRC))) +vpath %.cpp $(dir $(SRC)) +vpath %.c $(dir $(SRC)) + +.PHONY: all clean + +all: $(BUILD) $(BUILD)/$(TARGET) + +$(BUILD)/%.o: %.cpp Makefile | $(BUILD) + $(CXX) -c $(CFLAGS) $< -o $@ + +$(BUILD)/%.o: %.c Makefile | $(BUILD) + $(CXX) -c $(CFLAGS) $< -o $@ + +$(BUILD)/$(TARGET): $(OBJ) Makefile + $(CXX) $(LDFLAGS) $(OBJ) -o $@ + +$(BUILD): + mkdir $@ + +clean: + rm -fr $(BUILD) diff --git a/libs/z b/libs/z new file mode 160000 index 0000000..28ec6ff --- /dev/null +++ b/libs/z @@ -0,0 +1 @@ +Subproject commit 28ec6ff73bad0528eb22f38f6ca0e53827723cb3 diff --git a/libs/z80 b/libs/z80 new file mode 160000 index 0000000..b60d218 --- /dev/null +++ b/libs/z80 @@ -0,0 +1 @@ +Subproject commit b60d21889e246250ee0cac63bd6c64230959fe3c diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..703ae46 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,224 @@ +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "Z/types/base.h" +#include "Z80.h" + +template +class Emulator { + public: + Emulator(std::filesystem::path rom_name) { + // SDL + int render_flags = SDL_RENDERER_ACCELERATED; + int window_flags = 0; + + if (SDL_Init(SDL_INIT_VIDEO) < 0) { + throw std::runtime_error("Failed to initialize SDL2: " + std::string(SDL_GetError())); + } + + window = SDL_CreateWindow("Z80 Emulator", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, W*16, H*16, window_flags); + if (!window) { + throw std::runtime_error("Failed to open window: " + std::string(SDL_GetError())); + } + + SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "linear"); + renderer = SDL_CreateRenderer(window, -1, render_flags); + if (!window) { + throw std::runtime_error("Failed to create rendere: " + std::string(SDL_GetError())); + } + + if (!IMG_Init(IMG_INIT_PNG)) { + throw std::runtime_error("Failed to initialize SDL2_image: " + std::string(SDL_GetError())); + } + + SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "Loading '../convert-font/font.png'"); + font = IMG_LoadTexture(renderer, "../convert-font/font.png"); + + if (!font) { + throw std::runtime_error("Failed to open font: " + std::string(IMG_GetError())); + } + + // Z80 + std::ifstream rom_file(rom_name, std::ios::in | std::ios::binary); + + rom_file.seekg(0, std::ios::end); + size_t rom_size = rom_file.tellg(); + rom_file.seekg(0, std::ios::beg); + + rom_file.read(reinterpret_cast(memory.data()), rom_size); + + z.context = this; + z.read = z_read; + z.write = z_write; + z.in = z_in; + z.out = z_out; + z.int_data = z_int_data; + + z80_reset(&z); + } + + ~Emulator() { + SDL_DestroyRenderer(renderer); + + SDL_DestroyWindow(window); + + SDL_Quit(); + } + + static zuint8 z_read(void* context, zuint16 address) { + auto e = (Emulator*)context; + return e->memory[address]; + } + + static void z_write(void* context, zuint16 address, zuint8 value) { + auto e = (Emulator*)context; + e->memory[address] = value; + } + + static zuint8 z_in(void* /* context */, zuint16 port) { + switch (port & 0xFF) { + case 0x03: + return 0x01; + + // char + case 0x1E: + return 0x00; + + // has_input + case 0x1F: + return 0x00; + + default: + SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "IO Read @ 0x%X\n", (port & 0xFF)); + + return 0x00; + } + } + + static void z_out(void* context, zuint16 port, zuint8 value) { + auto e = (Emulator*)context; + + switch (port & 0xFF) { + case 0x02: + e->input(value); + break; + + default: + SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "IO Wrte 0x%X @ 0x%X\n", value, (port & 0xFF)); + break; + } + } + + static zuint32 z_int_data(void* /* context */) { + return 0x00; + } + + void update() { + z80_run(&z, 1); + } + + // @todo This should handle the input properly (same as fpga) + void input(char c) { + buffer[cursor.y*W + cursor.x] = c; + + cursor.x++; + if (cursor.x > W) { + cursor.x = 0; + cursor.y++; + } + } + + void draw_buffer() { + SDL_Rect src; + src.w = 16; + src.h = 16; + + SDL_Rect dest; + dest.w = 16; + dest.h = 16; + + SDL_SetTextureColorMod(font, 255, 255, 255); + + for (int x = 0; x < W; ++x) { + for (int y = 0; y < H; ++y) { + char c = buffer[y*W + x]; + src.x = (c % 16) * 16; + src.y = (c / 16) * 16; + + dest.x = x * 16; + dest.y = y * 16; + + SDL_RenderCopy(renderer, font, &src, &dest); + } + } + } + + bool handle_events() { + SDL_Event event; + + while (SDL_PollEvent(&event)) { + switch (event.type) { + case SDL_QUIT: + return false; + + default: + break; + } + } + + return true; + } + + void prepare() { + SDL_SetRenderDrawColor(renderer, 96, 128, 255, 255); + SDL_RenderClear(renderer); + } + + void render() { + SDL_RenderPresent(renderer); + } + + private: + SDL_Renderer* renderer = nullptr; + SDL_Window* window = nullptr; + SDL_Texture* font = nullptr; + + std::array buffer = {0}; + + struct { + int x = 0; + int y = 0; + } cursor; + + Z80 z; + std::array memory; +}; + +int main() { + Emulator<80, 45> e("../../software/monitor/.build/rom_monitor.bin"); + + while (e.handle_events()) { + e.prepare(); + + e.update(); + + e.draw_buffer(); + + e.render(); + } +}