Tried adjusting speed to be more accurate and added random device (does not exist im real hardware yet)
This commit is contained in:
parent
1ae8c35ac6
commit
57108c2c7e
9
Makefile
9
Makefile
|
@ -1,11 +1,12 @@
|
||||||
CXX = g++
|
PREFIX=
|
||||||
|
CXX = $(PREFIX)g++
|
||||||
BUILD = .build
|
BUILD = .build
|
||||||
TARGET = emulator
|
TARGET = emulator
|
||||||
OPT = g -g
|
OPT = g -g
|
||||||
DEFINE = -DCPU_Z80_STATIC -DCPU_Z80_USE_LOCAL_HEADER
|
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
|
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 -O$(OPT) -flto `sdl2-config --libs` -lSDL2_image
|
LDFLAGS = -Wall -Wextra -flto -O$(OPT) `$(PREFIX)pkg-config --libs sdl2` `$(PREFIX)pkg-config --libs SDL2_image` -mconsole
|
||||||
|
|
||||||
SRC = \
|
SRC = \
|
||||||
src/main.cpp \
|
src/main.cpp \
|
||||||
|
@ -30,7 +31,7 @@ $(BUILD)/%.o: %.c Makefile | $(BUILD)
|
||||||
$(CXX) -c $(CFLAGS) $< -o $@
|
$(CXX) -c $(CFLAGS) $< -o $@
|
||||||
|
|
||||||
$(BUILD)/$(TARGET): $(OBJ) Makefile
|
$(BUILD)/$(TARGET): $(OBJ) Makefile
|
||||||
$(CXX) $(LDFLAGS) $(OBJ) -o $@
|
$(CXX) $(OBJ) -o $@ $(LDFLAGS)
|
||||||
|
|
||||||
$(BUILD):
|
$(BUILD):
|
||||||
mkdir $@
|
mkdir $@
|
||||||
|
|
34
src/main.cpp
34
src/main.cpp
|
@ -1,5 +1,5 @@
|
||||||
#include <bits/stdint-uintn.h>
|
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
|
#include <cstdlib>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
@ -8,6 +8,7 @@
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <queue>
|
#include <queue>
|
||||||
|
#include <random>
|
||||||
|
|
||||||
#include <SDL2/SDL.h>
|
#include <SDL2/SDL.h>
|
||||||
#include <SDL2/SDL_error.h>
|
#include <SDL2/SDL_error.h>
|
||||||
|
@ -22,6 +23,7 @@
|
||||||
#include "SDL_keyboard.h"
|
#include "SDL_keyboard.h"
|
||||||
#include "SDL_keycode.h"
|
#include "SDL_keycode.h"
|
||||||
#include "SDL_pixels.h"
|
#include "SDL_pixels.h"
|
||||||
|
#include "SDL_scancode.h"
|
||||||
#include "SDL_surface.h"
|
#include "SDL_surface.h"
|
||||||
#include "Z/types/base.h"
|
#include "Z/types/base.h"
|
||||||
#include "Z80.h"
|
#include "Z80.h"
|
||||||
|
@ -31,6 +33,9 @@
|
||||||
#define SD_PAGE_SIZE 512
|
#define SD_PAGE_SIZE 512
|
||||||
#define CPM_PAGES (SD_PAGE_SIZE/CPM_PAGE_SIZE*SD_PAGES)
|
#define CPM_PAGES (SD_PAGE_SIZE/CPM_PAGE_SIZE*SD_PAGES)
|
||||||
|
|
||||||
|
// @todo Make this commandline parameter
|
||||||
|
#define CLOCK_SPEED 4000e4
|
||||||
|
|
||||||
struct Glyph {
|
struct Glyph {
|
||||||
uint8_t c = 0x00;
|
uint8_t c = 0x00;
|
||||||
int fg = 0b111;
|
int fg = 0b111;
|
||||||
|
@ -388,7 +393,7 @@ class Disk {
|
||||||
template <int W, int H>
|
template <int W, int H>
|
||||||
class Emulator {
|
class Emulator {
|
||||||
public:
|
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
|
// SDL
|
||||||
int render_flags = SDL_RENDERER_ACCELERATED;
|
int render_flags = SDL_RENDERER_ACCELERATED;
|
||||||
int window_flags = 0;
|
int window_flags = 0;
|
||||||
|
@ -477,6 +482,11 @@ class Emulator {
|
||||||
case 0x03:
|
case 0x03:
|
||||||
return 0x01;
|
return 0x01;
|
||||||
|
|
||||||
|
// @note This is not implemented in the hardware yet
|
||||||
|
case 0x09: {
|
||||||
|
return e->dist(e->gen);
|
||||||
|
}
|
||||||
|
|
||||||
// Input
|
// Input
|
||||||
case 0x1E: {
|
case 0x1E: {
|
||||||
uint8_t c = e->input.front();
|
uint8_t c = e->input.front();
|
||||||
|
@ -541,7 +551,12 @@ class Emulator {
|
||||||
}
|
}
|
||||||
|
|
||||||
void update() {
|
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() {
|
void draw_screen() {
|
||||||
|
@ -605,6 +620,9 @@ class Emulator {
|
||||||
// @todo For some reason we cannot capture ctrl???
|
// @todo For some reason we cannot capture ctrl???
|
||||||
if (SDL_GetModState() & KMOD_ALT && c == 'c') {
|
if (SDL_GetModState() & KMOD_ALT && c == 'c') {
|
||||||
input.push(0x03);
|
input.push(0x03);
|
||||||
|
// @todo Even more strange behaviour in windows
|
||||||
|
} else if (SDL_GetModState() & KMOD_CTRL && c == ' ') {
|
||||||
|
input.push(0x03);
|
||||||
} else {
|
} else {
|
||||||
input.push(c);
|
input.push(c);
|
||||||
}
|
}
|
||||||
|
@ -640,8 +658,14 @@ class Emulator {
|
||||||
Memory memory;
|
Memory memory;
|
||||||
Disk disk;
|
Disk disk;
|
||||||
|
|
||||||
|
std::random_device rd;
|
||||||
|
std::mt19937 gen;
|
||||||
|
std::uniform_int_distribution<> dist;
|
||||||
|
|
||||||
std::queue<uint8_t> input;
|
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[]) {
|
int main(int argc, char* argv[]) {
|
||||||
|
@ -665,4 +689,6 @@ int main(int argc, char* argv[]) {
|
||||||
e.render();
|
e.render();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user