Compare commits

..

No commits in common. "5dd69500caf35f1e4bc6729f14ff1c08c0fd0afd" and "1901a471e454c4653949752cdfbebf11df178e31" have entirely different histories.

3 changed files with 36 additions and 31 deletions

View File

@ -9,19 +9,12 @@ const Timer = std.time.Timer;
const Thread = std.Thread;
const Atomic = std.atomic.Atomic;
// 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)
// One frame operates at 59.7275005696Hz
const cycles_per_frame: u64 = 228 * (308 * 4);
const clock_rate: u64 = 1 << 24;
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 {
@ -34,9 +27,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),
}
@ -54,7 +47,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);
}
@ -76,13 +69,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(fps_timer.lap());
fps.add(emuFps(fps_timer.lap()));
}
}
@ -100,7 +93,7 @@ pub fn runSyncFps(quit: *Atomic(bool), fps: *FpsAverage, sched: *Scheduler, cpu:
sleep(&timer, &wake_time);
// Determine FPS
fps.add(fps_timer.lap());
fps.add(emuFps(fps_timer.lap()));
// Update to the new wake time
wake_time += frame_period;
@ -156,3 +149,8 @@ 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,7 +17,6 @@ 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;
@ -38,9 +37,14 @@ 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);
}
const maybe_bios: ?[]const u8 = if (args.option("--bios")) |p| p else null;
var maybe_bios: ?[]const u8 = null;
if (args.option("--bios")) |path| {
maybe_bios = path;
}
const positionals = args.positionals();
const stderr = std.io.getStdErr();
@ -68,19 +72,22 @@ pub fn main() anyerror!void {
var cpu = Arm7tdmi.init(&scheduler, &bus);
cpu.fastBoot();
const log_file: ?File = if (enable_logging) blk: {
const file = try std.fs.cwd().createFile(if (is_binary) "zba.bin" else "zba.log", .{});
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, .{});
cpu.useLogger(&file, is_binary);
break :blk file;
} else null;
log_file = file;
}
defer if (log_file) |file| file.close();
// Init Atomics
var quit = Atomic(bool).init(false);
var emu_rate = FpsAverage.init();
var emu_fps = FpsAverage.init();
// Create Emulator Thread
const emu_thread = try Thread.spawn(.{}, emu.run, .{ .LimitedFPS, &quit, &emu_rate, &scheduler, &cpu, &bus });
const emu_thread = try Thread.spawn(.{}, emu.run, .{ .UnlimitedFPS, &quit, &emu_fps, &scheduler, &cpu, &bus });
defer emu_thread.join();
// Initialize SDL
@ -102,7 +109,7 @@ pub fn main() anyerror!void {
) orelse sdlPanic();
defer SDL.SDL_DestroyWindow(window);
const renderer = SDL.SDL_CreateRenderer(window, -1, SDL.SDL_RENDERER_ACCELERATED | SDL.SDL_RENDERER_PRESENTVSYNC) orelse sdlPanic();
var 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();
@ -113,7 +120,7 @@ pub fn main() anyerror!void {
emu_loop: while (true) {
var event: SDL.SDL_Event = undefined;
while (SDL.SDL_PollEvent(&event) != 0) {
if (SDL.SDL_PollEvent(&event) != 0) {
// Pause Emulation Thread during Input Writing
switch (event.type) {
@ -162,8 +169,8 @@ pub fn main() anyerror!void {
_ = SDL.SDL_RenderCopy(renderer, texture, null, null);
SDL.SDL_RenderPresent(renderer);
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;
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;
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) f64 {
return @intToFloat(f64, std.time.ns_per_s) / (@intToFloat(f64, self.total) / @intToFloat(f64, self.sample_count));
pub fn calc(self: *const Self) u64 {
return self.total / self.sample_count;
}
fn reset(self: *Self, sample: u64) void {