From 9f69b122d0df699a454780e4464967d74f259564 Mon Sep 17 00:00:00 2001 From: Rekai Musuka Date: Fri, 21 Oct 2022 05:12:46 -0300 Subject: [PATCH] chore: remove awful ptr casts in backup.zig and bios.zig --- src/Bus.zig | 9 ++++++--- src/bus/Bios.zig | 5 ++--- src/bus/GamePak.zig | 2 +- src/bus/backup.zig | 16 ++-------------- src/cpu.zig | 4 ++-- 5 files changed, 13 insertions(+), 23 deletions(-) diff --git a/src/Bus.zig b/src/Bus.zig index f4eaa9a..aa456da 100644 --- a/src/Bus.zig +++ b/src/Bus.zig @@ -75,7 +75,10 @@ pub fn debugRead(self: *const Self, comptime T: type, address: u32) T { const cached = self.sched.tick; defer self.sched.tick = cached; - return self.read(T, address); + // FIXME: This is bad but it's a debug read so I don't care that much? + const this = @intToPtr(*Self, @ptrToInt(self)); + + return this.read(T, address); } fn readOpenBus(self: *const Self, comptime T: type, address: u32) T { @@ -107,13 +110,13 @@ fn readOpenBus(self: *const Self, comptime T: type, address: u32) T { return @truncate(T, rotr(u32, word, 8 * (address & 3))); } -fn readBios(self: *const Self, comptime T: type, address: u32) T { +fn readBios(self: *Self, comptime T: type, address: u32) T { if (address < Bios.size) return self.bios.checkedRead(T, self.cpu.?.r[15], alignAddress(T, address)); return self.readOpenBus(T, address); } -pub fn read(self: *const Self, comptime T: type, address: u32) T { +pub fn read(self: *Self, comptime T: type, address: u32) T { const page = @truncate(u8, address >> 24); const align_addr = alignAddress(T, address); self.sched.tick += 1; diff --git a/src/bus/Bios.zig b/src/bus/Bios.zig index bdf916e..8e2ec01 100644 --- a/src/bus/Bios.zig +++ b/src/bus/Bios.zig @@ -31,11 +31,10 @@ pub fn deinit(self: Self) void { if (self.buf) |buf| self.alloc.free(buf); } -pub fn checkedRead(self: *const Self, comptime T: type, r15: u32, addr: u32) T { +pub fn checkedRead(self: *Self, comptime T: type, r15: u32, addr: u32) T { if (r15 < Self.size) { // FIXME: Just give up on *const Self on bus reads, Rekai - const this = @intToPtr(*Self, @ptrToInt(self)); - this.addr_latch = addr; + self.addr_latch = addr; return self.read(T, addr); } diff --git a/src/bus/GamePak.zig b/src/bus/GamePak.zig index 4ae26fa..d5d31fb 100644 --- a/src/bus/GamePak.zig +++ b/src/bus/GamePak.zig @@ -65,7 +65,7 @@ pub fn deinit(self: Self) void { self.backup.deinit(); } -pub fn read(self: *const Self, comptime T: type, address: u32) T { +pub fn read(self: *Self, comptime T: type, address: u32) T { const addr = address & 0x1FF_FFFF; if (self.backup.kind == .Eeprom) { diff --git a/src/bus/backup.zig b/src/bus/backup.zig index 1f856e6..efe8277 100644 --- a/src/bus/backup.zig +++ b/src/bus/backup.zig @@ -333,20 +333,8 @@ const Eeprom = struct { }; } - pub fn read(self: *const Self) u1 { - // Here I throw away the const qualifier which is bad and dumb but here's why. - // This is one of the few (as of when I write this, **only**) places that mutate - // some value upon access. Before this I've been able to have all read-related functions - // present a *const Self parameter with no issues. - // - // I don't think it's worth throwing away all the good that *const Self brings across the entire - // memory bus because reading from the EEPROM increments an internal counter which isn't even - // visible to neither the cartridge nor any other component of the emulator. - // - // By throwing away const, we can increment self.read_proc.i which has a range of 0 -> 67. This is - // a small enough scope (and a well defined one at that) so that this transgression isn't the worst, I think. - const self_mut = @intToPtr(*Self, @ptrToInt(self)); - return self_mut.reader.read(); + pub fn read(self: *Self) u1 { + return self.reader.read(); } pub fn write(self: *Self, word_count: u16, buf: *[]u8, bit: u1) void { diff --git a/src/cpu.zig b/src/cpu.zig index da70be4..d4d09a7 100644 --- a/src/cpu.zig +++ b/src/cpu.zig @@ -336,11 +336,11 @@ pub const Arm7tdmi = struct { prettyPrintPsr(&self.spsr); if (self.cpsr.t.read()) { - const opcode = self.bus.read(u16, self.r[15] - 4); + const opcode = self.bus.debugRead(u16, self.r[15] - 4); const id = thumbIdx(opcode); std.debug.print("opcode: ID: 0x{b:0>10} 0x{X:0>4}\n", .{ id, opcode }); } else { - const opcode = self.bus.read(u32, self.r[15] - 4); + const opcode = self.bus.debugRead(u32, self.r[15] - 4); const id = armIdx(opcode); std.debug.print("opcode: ID: 0x{X:0>3} 0x{X:0>8}\n", .{ id, opcode }); }