From 2629d15e2f63aee5ecd33fe180e3d072ddb7e482 Mon Sep 17 00:00:00 2001 From: Rekai Musuka Date: Fri, 10 Mar 2023 21:13:38 -0600 Subject: [PATCH] feat: don't require path to ROM in CLI --- README.md | 3 +-- src/core/bus/GamePak.zig | 35 +++++++++++++++++++++-------------- src/core/bus/backup.zig | 2 +- src/main.zig | 6 +++--- src/util.zig | 2 +- 5 files changed, 27 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index cf7ea2f..ba251da 100644 --- a/README.md +++ b/README.md @@ -15,14 +15,13 @@ This is a simple (read: incomplete) for-fun long-term project. I hope to get "mo - [x] Affine Sprites - [ ] Windowing (see [this branch](https://git.musuka.dev/paoda/zba/src/branch/window)) - [ ] Audio Resampler (Having issues with SDL2's) -- [x] Immediate Mode GUI (see [this branch](https://git.musuka.dev/paoda/zba/src/branch/imgui)) - [ ] Refactoring for easy-ish perf boosts ## Usage As it currently exists, ZBA is run from the terminal. In your console of choice, type `./zba --help` to see what you can do. -I typically find myself typing `./zba -b ./bin/bios.bin ./bin/test/suite.gba` to see how badly my "cool new feature" broke everything else. +I typically find myself typing `./zba -b ./bin/bios.bin` and then going to File -> Insert ROM to load the title of my choice. Need a BIOS? Why not try using the open-source [Cult-Of-GBA BIOS](https://github.com/Cult-of-GBA/BIOS) written by [fleroviux](https://github.com/fleroviux) and [DenSinH](https://github.com/DenSinH)? diff --git a/src/core/bus/GamePak.zig b/src/core/bus/GamePak.zig index 750202e..d2203a1 100644 --- a/src/core/bus/GamePak.zig +++ b/src/core/bus/GamePak.zig @@ -179,23 +179,30 @@ pub fn write(self: *Self, comptime T: type, word_count: u16, address: u32, value } } -pub fn init(allocator: Allocator, cpu: *Arm7tdmi, rom_path: []const u8, save_path: ?[]const u8) !Self { - const file = try std.fs.cwd().openFile(rom_path, .{}); - defer file.close(); +pub fn init(allocator: Allocator, cpu: *Arm7tdmi, maybe_rom: ?[]const u8, maybe_save: ?[]const u8) !Self { + const Device = Gpio.Device; - const file_buf = try file.readToEndAlloc(allocator, try file.getEndPos()); - const title = file_buf[0xA0..0xAC].*; - const kind = Backup.guess(file_buf); - const device = if (config.config().guest.force_rtc) .Rtc else guessDevice(file_buf); + const items: struct { []u8, [12]u8, Backup.Kind, Device.Kind } = if (maybe_rom) |file_path| blk: { + const file = try std.fs.cwd().openFile(file_path, .{}); + defer file.close(); - logHeader(file_buf, title); + const buffer = try file.readToEndAlloc(allocator, try file.getEndPos()); + const title = buffer[0xA0..0xAC]; + logHeader(buffer, title); + + const device_kind = if (config.config().guest.force_rtc) .Rtc else guessDevice(buffer); + + break :blk .{ buffer, title.*, Backup.guess(buffer), device_kind }; + } else .{ try allocator.alloc(u8, 0), [_]u8{0} ** 12, .None, .None }; + + const title = items[1]; return .{ - .buf = file_buf, + .buf = items[0], .allocator = allocator, .title = title, - .backup = try Backup.init(allocator, kind, title, save_path), - .gpio = try Gpio.init(allocator, cpu, device), + .backup = try Backup.init(allocator, items[2], title, maybe_save), + .gpio = try Gpio.init(allocator, cpu, items[3]), }; } @@ -223,11 +230,11 @@ fn guessDevice(buf: []const u8) Gpio.Device.Kind { return .None; } -fn logHeader(buf: []const u8, title: [12]u8) void { - const ver = buf[0xBC]; +fn logHeader(buf: []const u8, title: *const [12]u8) void { + const version = buf[0xBC]; log.info("Title: {s}", .{title}); - if (ver != 0) log.info("Version: {}", .{ver}); + if (version != 0) log.info("Version: {}", .{version}); log.info("Game Code: {s}", .{buf[0xAC..0xB0]}); log.info("Maker Code: {s}", .{buf[0xB0..0xB2]}); diff --git a/src/core/bus/backup.zig b/src/core/bus/backup.zig index d1d708d..8db2571 100644 --- a/src/core/bus/backup.zig +++ b/src/core/bus/backup.zig @@ -32,7 +32,7 @@ pub const Backup = struct { flash: Flash, eeprom: Eeprom, - const Kind = enum { + pub const Kind = enum { Eeprom, Sram, Flash, diff --git a/src/main.zig b/src/main.zig index 39faa08..eff677a 100644 --- a/src/main.zig +++ b/src/main.zig @@ -139,7 +139,7 @@ pub fn main() void { fn handleArguments(allocator: Allocator, data_path: []const u8, result: *const clap.Result(clap.Help, ¶ms, clap.parsers.default)) !FilePaths { const rom_path = romPath(result); - log.info("ROM path: {s}", .{rom_path}); + log.info("ROM path: {?s}", .{rom_path}); const bios_path = result.args.bios; if (bios_path) |path| log.info("BIOS path: {s}", .{path}) else log.warn("No BIOS provided", .{}); @@ -188,10 +188,10 @@ fn ensureConfigDirExists(config_path: []const u8) !void { try dir.makePath("zba"); } -fn romPath(result: *const clap.Result(clap.Help, ¶ms, clap.parsers.default)) []const u8 { +fn romPath(result: *const clap.Result(clap.Help, ¶ms, clap.parsers.default)) ?[]const u8 { return switch (result.positionals.len) { + 0 => null, 1 => result.positionals[0], - 0 => exitln("ZBA requires a path to a GamePak ROM", .{}), else => exitln("ZBA received too many positional arguments.", .{}), }; } diff --git a/src/util.zig b/src/util.zig index 276a658..9b6bbc9 100644 --- a/src/util.zig +++ b/src/util.zig @@ -50,7 +50,7 @@ pub fn escape(title: [12]u8) [12]u8 { } pub const FilePaths = struct { - rom: []const u8, + rom: ?[]const u8, bios: ?[]const u8, save: ?[]const u8, };