Compare commits

..

2 Commits

Author SHA1 Message Date
Rekai Nyangadzayi Musuka 5dd69500ca fix: speed percentage in title is now accurate
We now properly account for full speed being 59.97Hz not, 59Hz or 60Hz
2022-03-22 10:39:42 -03:00
Rekai Nyangadzayi Musuka eff25a0ab2 chore: make some variables const 2022-03-19 02:00:53 -03:00
3 changed files with 31 additions and 36 deletions

View File

@ -9,12 +9,19 @@ const Timer = std.time.Timer;
const Thread = std.Thread;
const Atomic = std.atomic.Atomic;
// One frame operates at 59.7275005696Hz
const cycles_per_frame: u64 = 228 * (308 * 4);
const clock_rate: u64 = 1 << 24;
// 228 Lines which consist of 308 dots (which are 4 cycles long)
const cycles_per_frame: u64 = 228 * (308 * 4); //280896
const clock_rate: u64 = 1 << 24; // 16.78MHz
// TODO: Don't truncate this, be more accurate w/ timing
// 59.6046447754ns (truncated to just 59ns)
const clock_period: u64 = std.time.ns_per_s / clock_rate;
const frame_period = (clock_period * cycles_per_frame);
// 59.7275005696Hz
pub const frame_rate = @intToFloat(f64, std.time.ns_per_s) /
((@intToFloat(f64, std.time.ns_per_s) / @intToFloat(f64, clock_rate)) * @intToFloat(f64, cycles_per_frame));
const log = std.log.scoped(.Emulation);
const RunKind = enum {
@ -27,9 +34,9 @@ const RunKind = enum {
pub fn run(kind: RunKind, quit: *Atomic(bool), fps: *FpsAverage, sched: *Scheduler, cpu: *Arm7tdmi, bus: *Bus) void {
switch (kind) {
.Unlimited => runUnSync(quit, sched, cpu, bus),
.Unlimited => runUnsync(quit, sched, cpu, bus),
.Limited => runSync(quit, sched, cpu, bus),
.UnlimitedFPS => runUnSyncFps(quit, fps, sched, cpu, bus),
.UnlimitedFPS => runUnsyncFps(quit, fps, sched, cpu, bus),
.LimitedFPS => runSyncFps(quit, fps, sched, cpu, bus),
.LimitedBusy => runBusyLoop(quit, sched, cpu, bus),
}
@ -47,7 +54,7 @@ pub fn runFrame(sched: *Scheduler, cpu: *Arm7tdmi, bus: *Bus) void {
}
}
pub fn runUnSync(quit: *Atomic(bool), sched: *Scheduler, cpu: *Arm7tdmi, bus: *Bus) void {
pub fn runUnsync(quit: *Atomic(bool), sched: *Scheduler, cpu: *Arm7tdmi, bus: *Bus) void {
log.info("Unsynchronized EmuThread has begun", .{});
while (!quit.load(.Unordered)) runFrame(sched, cpu, bus);
}
@ -69,13 +76,13 @@ pub fn runSync(quit: *Atomic(bool), sched: *Scheduler, cpu: *Arm7tdmi, bus: *Bus
}
}
pub fn runUnSyncFps(quit: *Atomic(bool), fps: *FpsAverage, sched: *Scheduler, cpu: *Arm7tdmi, bus: *Bus) void {
pub fn runUnsyncFps(quit: *Atomic(bool), fps: *FpsAverage, sched: *Scheduler, cpu: *Arm7tdmi, bus: *Bus) void {
log.info("Unsynchronized EmuThread with FPS Tracking has begun", .{});
var fps_timer = Timer.start() catch unreachable;
while (!quit.load(.Unordered)) {
runFrame(sched, cpu, bus);
fps.add(emuFps(fps_timer.lap()));
fps.add(fps_timer.lap());
}
}
@ -93,7 +100,7 @@ pub fn runSyncFps(quit: *Atomic(bool), fps: *FpsAverage, sched: *Scheduler, cpu:
sleep(&timer, &wake_time);
// Determine FPS
fps.add(emuFps(fps_timer.lap()));
fps.add(fps_timer.lap());
// Update to the new wake time
wake_time += frame_period;
@ -149,8 +156,3 @@ fn sleep(timer: *Timer, wake_time: *u64) void {
fn spinLoop(timer: *Timer, wake_time: u64) void {
while (true) if (timer.read() > wake_time) break;
}
inline fn emuFps(left: u64) u64 {
@setRuntimeSafety(false);
return @floatToInt(u64, @intToFloat(f64, std.time.ns_per_s) / @intToFloat(f64, left));
}

View File

@ -17,6 +17,7 @@ const window_scale = 3;
const gba_width = @import("ppu.zig").width;
const gba_height = @import("ppu.zig").height;
const framebuf_pitch = @import("ppu.zig").framebuf_pitch;
const expected_rate = @import("emu.zig").frame_rate;
pub const enable_logging: bool = false;
const is_binary: bool = false;
@ -37,14 +38,9 @@ pub fn main() anyerror!void {
var args = try clap.parse(clap.Help, &params, .{});
defer args.deinit();
if (args.flag("--help")) {
return clap.help(std.io.getStdErr().writer(), &params);
}
if (args.flag("--help")) return clap.help(std.io.getStdErr().writer(), &params);
var maybe_bios: ?[]const u8 = null;
if (args.option("--bios")) |path| {
maybe_bios = path;
}
const maybe_bios: ?[]const u8 = if (args.option("--bios")) |p| p else null;
const positionals = args.positionals();
const stderr = std.io.getStdErr();
@ -72,22 +68,19 @@ pub fn main() anyerror!void {
var cpu = Arm7tdmi.init(&scheduler, &bus);
cpu.fastBoot();
var log_file: ?File = null;
if (enable_logging) {
const file_name: []const u8 = if (is_binary) "zba.bin" else "zba.log";
const file = try std.fs.cwd().createFile(file_name, .{});
const log_file: ?File = if (enable_logging) blk: {
const file = try std.fs.cwd().createFile(if (is_binary) "zba.bin" else "zba.log", .{});
cpu.useLogger(&file, is_binary);
log_file = file;
}
break :blk file;
} else null;
defer if (log_file) |file| file.close();
// Init Atomics
var quit = Atomic(bool).init(false);
var emu_fps = FpsAverage.init();
var emu_rate = FpsAverage.init();
// Create Emulator Thread
const emu_thread = try Thread.spawn(.{}, emu.run, .{ .UnlimitedFPS, &quit, &emu_fps, &scheduler, &cpu, &bus });
const emu_thread = try Thread.spawn(.{}, emu.run, .{ .LimitedFPS, &quit, &emu_rate, &scheduler, &cpu, &bus });
defer emu_thread.join();
// Initialize SDL
@ -109,7 +102,7 @@ pub fn main() anyerror!void {
) orelse sdlPanic();
defer SDL.SDL_DestroyWindow(window);
var renderer = SDL.SDL_CreateRenderer(window, -1, SDL.SDL_RENDERER_ACCELERATED | SDL.SDL_RENDERER_PRESENTVSYNC) orelse sdlPanic();
const renderer = SDL.SDL_CreateRenderer(window, -1, SDL.SDL_RENDERER_ACCELERATED | SDL.SDL_RENDERER_PRESENTVSYNC) orelse sdlPanic();
defer SDL.SDL_DestroyRenderer(renderer);
const texture = SDL.SDL_CreateTexture(renderer, SDL.SDL_PIXELFORMAT_RGBA8888, SDL.SDL_TEXTUREACCESS_STREAMING, 240, 160) orelse sdlPanic();
@ -120,7 +113,7 @@ pub fn main() anyerror!void {
emu_loop: while (true) {
var event: SDL.SDL_Event = undefined;
if (SDL.SDL_PollEvent(&event) != 0) {
while (SDL.SDL_PollEvent(&event) != 0) {
// Pause Emulation Thread during Input Writing
switch (event.type) {
@ -169,8 +162,8 @@ pub fn main() anyerror!void {
_ = SDL.SDL_RenderCopy(renderer, texture, null, null);
SDL.SDL_RenderPresent(renderer);
const avg = emu_fps.calc();
const dyn_title = std.fmt.bufPrint(&dyn_title_buf, "{s} [Emu: {d:0>3}fps, {d:0>3}%] ", .{ title, avg, (avg * 100 / 59) }) catch unreachable;
const actual = emu_rate.calc();
const dyn_title = std.fmt.bufPrint(&dyn_title_buf, "{s} [Emu: {d:0>3.2}fps, {d:0>3.2}%] ", .{ title, actual, actual * 100 / expected_rate }) catch unreachable;
SDL.SDL_SetWindowTitle(window, dyn_title.ptr);
}

View File

@ -33,8 +33,8 @@ pub const FpsAverage = struct {
self.sample_count += 1;
}
pub fn calc(self: *const Self) u64 {
return self.total / self.sample_count;
pub fn calc(self: *const Self) f64 {
return @intToFloat(f64, std.time.ns_per_s) / (@intToFloat(f64, self.total) / @intToFloat(f64, self.sample_count));
}
fn reset(self: *Self, sample: u64) void {