chore: remove awful ptr casts in backup.zig and bios.zig

This commit is contained in:
Rekai Nyangadzayi Musuka 2022-05-27 20:24:09 -03:00
parent d4bc1f2cd0
commit 517ed7a835
5 changed files with 13 additions and 23 deletions

View File

@ -75,7 +75,10 @@ pub fn debugRead(self: *const Self, comptime T: type, address: u32) T {
const cached = self.sched.tick; const cached = self.sched.tick;
defer self.sched.tick = cached; 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 { 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))); 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)); if (address < Bios.size) return self.bios.checkedRead(T, self.cpu.?.r[15], alignAddress(T, address));
return self.readOpenBus(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 page = @truncate(u8, address >> 24);
const align_addr = alignAddress(T, address); const align_addr = alignAddress(T, address);
self.sched.tick += 1; self.sched.tick += 1;

View File

@ -31,11 +31,10 @@ pub fn deinit(self: Self) void {
if (self.buf) |buf| self.alloc.free(buf); 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) { if (r15 < Self.size) {
// FIXME: Just give up on *const Self on bus reads, Rekai // FIXME: Just give up on *const Self on bus reads, Rekai
const this = @intToPtr(*Self, @ptrToInt(self)); self.addr_latch = addr;
this.addr_latch = addr;
return self.read(T, addr); return self.read(T, addr);
} }

View File

@ -65,7 +65,7 @@ pub fn deinit(self: Self) void {
self.backup.deinit(); 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; const addr = address & 0x1FF_FFFF;
if (self.backup.kind == .Eeprom) { if (self.backup.kind == .Eeprom) {

View File

@ -333,20 +333,8 @@ const Eeprom = struct {
}; };
} }
pub fn read(self: *const Self) u1 { pub fn read(self: *Self) u1 {
// Here I throw away the const qualifier which is bad and dumb but here's why. return self.reader.read();
// 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 write(self: *Self, word_count: u16, buf: *[]u8, bit: u1) void { pub fn write(self: *Self, word_count: u16, buf: *[]u8, bit: u1) void {

View File

@ -336,11 +336,11 @@ pub const Arm7tdmi = struct {
prettyPrintPsr(&self.spsr); prettyPrintPsr(&self.spsr);
if (self.cpsr.t.read()) { 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); const id = thumbIdx(opcode);
std.debug.print("opcode: ID: 0x{b:0>10} 0x{X:0>4}\n", .{ id, opcode }); std.debug.print("opcode: ID: 0x{b:0>10} 0x{X:0>4}\n", .{ id, opcode });
} else { } 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); const id = armIdx(opcode);
std.debug.print("opcode: ID: 0x{X:0>3} 0x{X:0>8}\n", .{ id, opcode }); std.debug.print("opcode: ID: 0x{X:0>3} 0x{X:0>8}\n", .{ id, opcode });
} }