From 57108c2c7ee9d49da91e268983c9560467ce2880 Mon Sep 17 00:00:00 2001 From: Dreaded_X Date: Sun, 17 Jan 2021 16:10:47 +0100 Subject: [PATCH] Tried adjusting speed to be more accurate and added random device (does not exist im real hardware yet) --- Makefile | 9 +++++---- src/main.cpp | 34 ++++++++++++++++++++++++++++++---- 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/Makefile b/Makefile index aba1d9b..1307c82 100644 --- a/Makefile +++ b/Makefile @@ -1,11 +1,12 @@ -CXX = g++ +PREFIX= +CXX = $(PREFIX)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 +CFLAGS = -Wall -Wextra -std=c++20 -flto -O$(OPT) $(DEFINE) -Iinclude `$(PREFIX)pkg-config --cflags sdl2` -Ilibs/z80/API/emulation/CPU -Ilibs/z/API +LDFLAGS = -Wall -Wextra -flto -O$(OPT) `$(PREFIX)pkg-config --libs sdl2` `$(PREFIX)pkg-config --libs SDL2_image` -mconsole SRC = \ src/main.cpp \ @@ -30,7 +31,7 @@ $(BUILD)/%.o: %.c Makefile | $(BUILD) $(CXX) -c $(CFLAGS) $< -o $@ $(BUILD)/$(TARGET): $(OBJ) Makefile - $(CXX) $(LDFLAGS) $(OBJ) -o $@ + $(CXX) $(OBJ) -o $@ $(LDFLAGS) $(BUILD): mkdir $@ diff --git a/src/main.cpp b/src/main.cpp index 3e78b74..78be83d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,5 +1,5 @@ -#include #include +#include #include #include #include @@ -8,6 +8,7 @@ #include #include #include +#include #include #include @@ -22,6 +23,7 @@ #include "SDL_keyboard.h" #include "SDL_keycode.h" #include "SDL_pixels.h" +#include "SDL_scancode.h" #include "SDL_surface.h" #include "Z/types/base.h" #include "Z80.h" @@ -31,6 +33,9 @@ #define SD_PAGE_SIZE 512 #define CPM_PAGES (SD_PAGE_SIZE/CPM_PAGE_SIZE*SD_PAGES) +// @todo Make this commandline parameter +#define CLOCK_SPEED 4000e4 + struct Glyph { uint8_t c = 0x00; int fg = 0b111; @@ -388,7 +393,7 @@ class Disk { template class Emulator { public: - Emulator(std::filesystem::path rom_name, std::filesystem::path disk_name, std::string startup = "") : memory(rom_name), disk(disk_name) { + Emulator(std::filesystem::path rom_name, std::filesystem::path disk_name, std::string startup = "") : memory(rom_name), disk(disk_name), rd(), gen(rd()), dist(0x00, 0xFF), begin(std::chrono::steady_clock::now()), wait(0) { // SDL int render_flags = SDL_RENDERER_ACCELERATED; int window_flags = 0; @@ -477,6 +482,11 @@ class Emulator { case 0x03: return 0x01; + // @note This is not implemented in the hardware yet + case 0x09: { + return e->dist(e->gen); + } + // Input case 0x1E: { uint8_t c = e->input.front(); @@ -541,7 +551,12 @@ class Emulator { } void update() { - z80_run(&z, 1); + auto diff = std::chrono::steady_clock::now() - begin; + if (diff > wait) { + begin = std::chrono::steady_clock::now(); + size_t cycles = z80_run(&z, 1); + wait = std::chrono::nanoseconds((long)std::floor(10e9 * cycles / CLOCK_SPEED)) - (diff - wait); + } } void draw_screen() { @@ -605,6 +620,9 @@ class Emulator { // @todo For some reason we cannot capture ctrl??? if (SDL_GetModState() & KMOD_ALT && c == 'c') { input.push(0x03); + // @todo Even more strange behaviour in windows + } else if (SDL_GetModState() & KMOD_CTRL && c == ' ') { + input.push(0x03); } else { input.push(c); } @@ -640,8 +658,14 @@ class Emulator { Memory memory; Disk disk; + std::random_device rd; + std::mt19937 gen; + std::uniform_int_distribution<> dist; + std::queue input; - bool ctrl = false; + + std::chrono::steady_clock::time_point begin; + std::chrono::nanoseconds wait; }; int main(int argc, char* argv[]) { @@ -665,4 +689,6 @@ int main(int argc, char* argv[]) { e.render(); } } + + return 0; }