chore: cleanup main

This commit is contained in:
Rekai Nyangadzayi Musuka 2022-10-21 05:12:44 -03:00
parent f373d8e17c
commit f194bee4eb
3 changed files with 73 additions and 66 deletions

View File

@ -4,7 +4,7 @@ const SDL = @import("sdl2");
const Bus = @import("Bus.zig"); const Bus = @import("Bus.zig");
const Scheduler = @import("scheduler.zig").Scheduler; const Scheduler = @import("scheduler.zig").Scheduler;
const Arm7tdmi = @import("cpu.zig").Arm7tdmi; const Arm7tdmi = @import("cpu.zig").Arm7tdmi;
const EmulatorFps = @import("util.zig").EmulatorFps; const FpsTracker = @import("util.zig").FpsTracker;
const Timer = std.time.Timer; const Timer = std.time.Timer;
const Thread = std.Thread; const Thread = std.Thread;
@ -35,7 +35,7 @@ const RunKind = enum {
LimitedBusy, LimitedBusy,
}; };
pub fn run(kind: RunKind, quit: *Atomic(bool), fps: *EmulatorFps, sched: *Scheduler, cpu: *Arm7tdmi) void { pub fn run(kind: RunKind, quit: *Atomic(bool), fps: *FpsTracker, sched: *Scheduler, cpu: *Arm7tdmi) void {
if (audio_sync) log.info("Audio sync enabled", .{}); if (audio_sync) log.info("Audio sync enabled", .{});
switch (kind) { switch (kind) {
@ -69,7 +69,7 @@ fn syncToAudio(cpu: *const Arm7tdmi) void {
while (SDL.SDL_AudioStreamAvailable(stream) > (@sizeOf(u16) * 2) * min_sample_count) {} while (SDL.SDL_AudioStreamAvailable(stream) > (@sizeOf(u16) * 2) * min_sample_count) {}
} }
pub fn runUnsynchronized(quit: *Atomic(bool), sched: *Scheduler, cpu: *Arm7tdmi, fps: ?*EmulatorFps) void { pub fn runUnsynchronized(quit: *Atomic(bool), sched: *Scheduler, cpu: *Arm7tdmi, fps: ?*FpsTracker) void {
log.info("Emulation thread w/out video sync", .{}); log.info("Emulation thread w/out video sync", .{});
if (fps) |tracker| { if (fps) |tracker| {
@ -89,7 +89,7 @@ pub fn runUnsynchronized(quit: *Atomic(bool), sched: *Scheduler, cpu: *Arm7tdmi,
} }
} }
pub fn runSynchronized(quit: *Atomic(bool), sched: *Scheduler, cpu: *Arm7tdmi, fps: ?*EmulatorFps) void { pub fn runSynchronized(quit: *Atomic(bool), sched: *Scheduler, cpu: *Arm7tdmi, fps: ?*FpsTracker) void {
log.info("Emulation thread w/ video sync", .{}); log.info("Emulation thread w/ video sync", .{});
var timer = Timer.start() catch unreachable; var timer = Timer.start() catch unreachable;
var wake_time: u64 = frame_period; var wake_time: u64 = frame_period;

View File

@ -9,7 +9,7 @@ const Bus = @import("Bus.zig");
const Apu = @import("apu.zig").Apu; const Apu = @import("apu.zig").Apu;
const Arm7tdmi = @import("cpu.zig").Arm7tdmi; const Arm7tdmi = @import("cpu.zig").Arm7tdmi;
const Scheduler = @import("scheduler.zig").Scheduler; const Scheduler = @import("scheduler.zig").Scheduler;
const EmulatorFps = @import("util.zig").EmulatorFps; const FpsTracker = @import("util.zig").FpsTracker;
const Timer = std.time.Timer; const Timer = std.time.Timer;
const Thread = std.Thread; const Thread = std.Thread;
@ -31,65 +31,61 @@ pub const log_level = if (builtin.mode != .Debug) .info else std.log.default_lev
const asString = @import("util.zig").asString; const asString = @import("util.zig").asString;
pub fn main() anyerror!void { // CLI Arguments + Help Text
// Allocator for Emulator + CLI const params = clap.parseParamsComptime(
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const alloc = gpa.allocator();
defer std.debug.assert(!gpa.deinit());
// CLI Arguments
const params = comptime clap.parseParamsComptime(
\\-h, --help Display this help and exit. \\-h, --help Display this help and exit.
\\-b, --bios <str> Optional path to a GBA BIOS ROM. \\-b, --bios <str> Optional path to a GBA BIOS ROM.
\\<str> Path to the GBA GamePak ROM \\<str> Path to the GBA GamePak ROM
\\ \\
); );
pub fn main() anyerror!void {
// Allocator for Emulator + CLI
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer std.debug.assert(!gpa.deinit());
const alloc = gpa.allocator();
// Setup CLI using zig-clap
var res = try clap.parse(clap.Help, &params, clap.parsers.default, .{}); var res = try clap.parse(clap.Help, &params, clap.parsers.default, .{});
defer res.deinit(); defer res.deinit();
const stderr = std.io.getStdErr(); const stderr = std.io.getStdErr();
defer stderr.close(); defer stderr.close();
// Display Help, if requested
// Grab ROM and BIOS paths if provided
if (res.args.help) return clap.help(stderr.writer(), clap.Help, &params, .{}); if (res.args.help) return clap.help(stderr.writer(), clap.Help, &params, .{});
const rom_path = try getRomPath(res, stderr);
const bios_path: ?[]const u8 = if (res.args.bios) |p| p else null; const bios_path: ?[]const u8 = if (res.args.bios) |p| p else null;
const rom_path = switch (res.positionals.len) {
1 => res.positionals[0],
0 => {
try stderr.writeAll("ZBA requires a positional path to a GamePak ROM.\n");
return CliError.InsufficientOptions;
},
else => {
try stderr.writeAll("ZBA received too many arguments.\n");
return CliError.UnneededOptions;
},
};
// Determine Save Directory // Determine Save Directory
const save_dir = try setupSavePath(alloc); const save_dir = try getSavePath(alloc);
defer if (save_dir) |path| alloc.free(path); defer if (save_dir) |path| alloc.free(path);
log.info("Found save directory: {s}", .{save_dir}); log.info("Found save directory: {s}", .{save_dir});
// Initialize SDL // Initialize Scheduler and ARM7TDMI Emulator
_ = initSdl2(); // Provide GBA Bus (initialized with ARM7TDMI) with a valid ptr to ARM7TDMI
defer SDL.SDL_Quit();
// Initialize Emulator
var scheduler = Scheduler.init(alloc); var scheduler = Scheduler.init(alloc);
defer scheduler.deinit(); defer scheduler.deinit();
const paths = .{ .bios = bios_path, .rom = rom_path, .save = save_dir }; const paths = .{ .bios = bios_path, .rom = rom_path, .save = save_dir };
var cpu = try Arm7tdmi.init(alloc, &scheduler, paths); var cpu = try Arm7tdmi.init(alloc, &scheduler, paths);
defer cpu.deinit(); defer cpu.deinit();
cpu.bus.attach(&cpu); cpu.bus.attach(&cpu);
// cpu.fastBoot(); // cpu.fastBoot(); // Uncomment to skip BIOS
// Initialize SDL Audio // Copy ROM title while Emulator still belongs to this thread
const audio_dev = initAudio(&cpu.bus.apu); const title = cpu.bus.pak.title;
defer SDL.SDL_CloseAudioDevice(audio_dev);
// Initialize SDL2
initSdl2();
defer SDL.SDL_Quit();
const dev = initAudio(&cpu.bus.apu);
defer SDL.SDL_CloseAudioDevice(dev);
// TODO: Refactor or delete this Logging code
// I probably still need logging in some form though (e.g. Golden Sun IIRC)
const log_file: ?File = if (enable_logging) blk: { const log_file: ?File = if (enable_logging) blk: {
const file = try std.fs.cwd().createFile(if (is_binary) "zba.bin" else "zba.log", .{}); const file = try std.fs.cwd().createFile(if (is_binary) "zba.bin" else "zba.log", .{});
cpu.useLogger(&file, is_binary); cpu.useLogger(&file, is_binary);
@ -97,16 +93,17 @@ pub fn main() anyerror!void {
} else null; } else null;
defer if (log_file) |file| file.close(); defer if (log_file) |file| file.close();
// Init Atomics
var quit = Atomic(bool).init(false); var quit = Atomic(bool).init(false);
var emu_rate = EmulatorFps.init(); var emu_rate = FpsTracker.init();
// Create Emulator Thread // Run Emulator in it's separate thread
// From this point on, interacting with Arm7tdmi or Scheduler
// be justified, as it will require to be thread-afe
const emu_thread = try Thread.spawn(.{}, emu.run, .{ .LimitedFPS, &quit, &emu_rate, &scheduler, &cpu }); const emu_thread = try Thread.spawn(.{}, emu.run, .{ .LimitedFPS, &quit, &emu_rate, &scheduler, &cpu });
defer emu_thread.join(); defer emu_thread.join();
var title_buf: [0x20]u8 = std.mem.zeroes([0x20]u8); var title_buf: [0x20]u8 = std.mem.zeroes([0x20]u8);
const window_title = try std.fmt.bufPrint(&title_buf, "ZBA | {s}", .{asString(cpu.bus.pak.title)}); const window_title = try std.fmt.bufPrint(&title_buf, "ZBA | {s}", .{asString(title)});
const window = createWindow(window_title, gba_width, gba_height); const window = createWindow(window_title, gba_width, gba_height);
defer SDL.SDL_DestroyWindow(window); defer SDL.SDL_DestroyWindow(window);
@ -158,7 +155,7 @@ pub fn main() anyerror!void {
SDL.SDLK_s => io.keyinput.shoulder_r.set(), SDL.SDLK_s => io.keyinput.shoulder_r.set(),
SDL.SDLK_RETURN => io.keyinput.start.set(), SDL.SDLK_RETURN => io.keyinput.start.set(),
SDL.SDLK_RSHIFT => io.keyinput.select.set(), SDL.SDLK_RSHIFT => io.keyinput.select.set(),
SDL.SDLK_i => std.debug.print("{} samples\n", .{@intCast(u32, SDL.SDL_AudioStreamAvailable(cpu.bus.apu.stream)) / (2 * @sizeOf(f32))}), SDL.SDLK_i => std.debug.print("{} samples\n", .{@intCast(u32, SDL.SDL_AudioStreamAvailable(cpu.bus.apu.stream)) / (2 * @sizeOf(u16))}),
else => {}, else => {},
} }
}, },
@ -189,31 +186,9 @@ fn sdlPanic() noreturn {
@panic(std.mem.sliceTo(str, 0)); @panic(std.mem.sliceTo(str, 0));
} }
// FIXME: Superfluous allocations? fn initSdl2() void {
fn setupSavePath(alloc: std.mem.Allocator) !?[]const u8 {
const save_subpath = try std.fs.path.join(alloc, &[_][]const u8{ "zba", "save" });
defer alloc.free(save_subpath);
const maybe_data_path = try known_folders.getPath(alloc, .data);
defer if (maybe_data_path) |path| alloc.free(path);
const save_path = if (maybe_data_path) |base| try std.fs.path.join(alloc, &[_][]const u8{ base, save_subpath }) else null;
if (save_path) |_| {
// If we've determined what our save path should be, ensure the prereq directories
// are present so that we can successfully write to the path when necessary
const maybe_data_dir = try known_folders.open(alloc, .data, .{});
if (maybe_data_dir) |data_dir| try data_dir.makePath(save_subpath);
}
return save_path;
}
fn initSdl2() c_int {
const status = SDL.SDL_Init(SDL.SDL_INIT_VIDEO | SDL.SDL_INIT_EVENTS | SDL.SDL_INIT_AUDIO | SDL.SDL_INIT_GAMECONTROLLER); const status = SDL.SDL_Init(SDL.SDL_INIT_VIDEO | SDL.SDL_INIT_EVENTS | SDL.SDL_INIT_AUDIO | SDL.SDL_INIT_GAMECONTROLLER);
if (status < 0) sdlPanic(); if (status < 0) sdlPanic();
return status;
} }
fn createWindow(title: []u8, width: c_int, height: c_int) *SDL.SDL_Window { fn createWindow(title: []u8, width: c_int, height: c_int) *SDL.SDL_Window {
@ -268,3 +243,35 @@ export fn audioCallback(userdata: ?*anyopaque, stream: [*c]u8, len: c_int) void
const apu = @ptrCast(*Apu, @alignCast(8, userdata)); const apu = @ptrCast(*Apu, @alignCast(8, userdata));
_ = SDL.SDL_AudioStreamGet(apu.stream, stream, len); _ = SDL.SDL_AudioStreamGet(apu.stream, stream, len);
} }
fn getSavePath(alloc: std.mem.Allocator) !?[]const u8 {
const save_subpath = "zba" ++ [_]u8{std.fs.path.sep} ++ "save";
const maybe_data_path = try known_folders.getPath(alloc, .data);
defer if (maybe_data_path) |path| alloc.free(path);
const save_path = if (maybe_data_path) |base| try std.fs.path.join(alloc, &[_][]const u8{ base, "zba", "save" }) else null;
if (save_path) |_| {
// If we've determined what our save path should be, ensure the prereq directories
// are present so that we can successfully write to the path when necessary
const maybe_data_dir = try known_folders.open(alloc, .data, .{});
if (maybe_data_dir) |data_dir| try data_dir.makePath(save_subpath);
}
return save_path;
}
fn getRomPath(res: clap.Result(clap.Help, &params, clap.parsers.default), stderr: std.fs.File) ![]const u8 {
return switch (res.positionals.len) {
1 => res.positionals[0],
0 => {
try stderr.writeAll("ZBA requires a positional path to a GamePak ROM.\n");
return CliError.InsufficientOptions;
},
else => {
try stderr.writeAll("ZBA received too many arguments.\n");
return CliError.UnneededOptions;
},
};
}

View File

@ -17,7 +17,7 @@ pub inline fn rotr(comptime T: type, x: T, r: anytype) T {
return x >> ar | x << (1 +% ~ar); return x >> ar | x << (1 +% ~ar);
} }
pub const EmulatorFps = struct { pub const FpsTracker = struct {
const Self = @This(); const Self = @This();
fps: u32, fps: u32,