From c34752ac652215f39e8235cb564875e25195a36a Mon Sep 17 00:00:00 2001 From: Rekai Musuka Date: Fri, 21 Oct 2022 05:13:00 -0300 Subject: [PATCH] feat: auto-detect RTC in commercial ROMS --- src/core/bus/GamePak.zig | 25 ++++++++++++++++++++++--- src/core/bus/backup.zig | 4 ++-- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/core/bus/GamePak.zig b/src/core/bus/GamePak.zig index 7bfde15..43667ef 100644 --- a/src/core/bus/GamePak.zig +++ b/src/core/bus/GamePak.zig @@ -22,7 +22,9 @@ pub fn init(allocator: Allocator, cpu: *Arm7tdmi, rom_path: []const u8, save_pat const file_buf = try file.readToEndAlloc(allocator, try file.getEndPos()); const title = file_buf[0xA0..0xAC].*; - const kind = Backup.guessKind(file_buf) orelse .None; + const kind = Backup.guessKind(file_buf); + const device = guessDevice(file_buf); + logHeader(file_buf, &title); return .{ @@ -30,10 +32,26 @@ pub fn init(allocator: Allocator, cpu: *Arm7tdmi, rom_path: []const u8, save_pat .allocator = allocator, .title = title, .backup = try Backup.init(allocator, kind, title, save_path), - .gpio = try Gpio.init(allocator, cpu, .Rtc), + .gpio = try Gpio.init(allocator, cpu, device), }; } +/// Searches the ROM to see if it can determine whether the ROM it's searching uses +/// any GPIO device, like a RTC for example. +fn guessDevice(buf: []const u8) Gpio.Device.Kind { + // Try to Guess if ROM uses RTC + const needle = "RTC_V"; // I was told SIIRTC_V, though Pokemen Firered (USA) is a false negative + + var i: usize = 0; + while ((i + needle.len) < buf.len) : (i += 1) { + if (std.mem.eql(u8, needle, buf[i..(i + needle.len)])) return .Rtc; + } + + // TODO: Detect other GPIO devices + + return .None; +} + fn logHeader(buf: []const u8, title: *const [12]u8) void { const code = buf[0xAC..0xB0]; const maker = buf[0xB0..0xB2]; @@ -258,8 +276,9 @@ const Gpio = struct { }; fn init(allocator: Allocator, cpu: *Arm7tdmi, kind: Device.Kind) !*This { - const self = try allocator.create(This); + log.info("Device: {}", .{kind}); + const self = try allocator.create(This); self.* = .{ .data = 0b0000, .direction = 0b1111, // TODO: What is GPIO DIrection set to by default? diff --git a/src/core/bus/backup.zig b/src/core/bus/backup.zig index 09d6b91..f89a0cc 100644 --- a/src/core/bus/backup.zig +++ b/src/core/bus/backup.zig @@ -61,7 +61,7 @@ pub const Backup = struct { return backup; } - pub fn guessKind(rom: []const u8) ?Kind { + pub fn guessKind(rom: []const u8) Kind { for (backup_kinds) |needle| { const needle_len = needle.str.len; @@ -71,7 +71,7 @@ pub const Backup = struct { } } - return null; + return .None; } pub fn deinit(self: *Self) void {