Tried adjusting speed to be more accurate and added random device (does not exist im real hardware yet)

This commit is contained in:
Dreaded_X 2021-01-17 16:10:47 +01:00
parent 1ae8c35ac6
commit 57108c2c7e
2 changed files with 35 additions and 8 deletions

View File

@ -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 $@

View File

@ -1,5 +1,5 @@
#include <bits/stdint-uintn.h>
#include <chrono>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <stdexcept>
@ -8,6 +8,7 @@
#include <functional>
#include <string>
#include <queue>
#include <random>
#include <SDL2/SDL.h>
#include <SDL2/SDL_error.h>
@ -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 <int W, int H>
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<uint8_t> 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;
}