From c660ca8922da3db3d5589ca4d86bb89486a6fcd9 Mon Sep 17 00:00:00 2001 From: Rekai Musuka Date: Wed, 29 Dec 2021 17:16:32 -0600 Subject: [PATCH] feat: implement LDR STR --- src/bus.zig | 9 +++++++-- src/cpu.zig | 4 ++-- src/cpu/single_data_transfer.zig | 10 ++++++++-- src/pak.zig | 7 +++++++ 4 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/bus.zig b/src/bus.zig index 03c12ab..2a41c37 100644 --- a/src/bus.zig +++ b/src/bus.zig @@ -16,8 +16,13 @@ pub const Bus = struct { return self.pak.readWord(addr); } - pub fn writeWord(_: *@This(), _: u32, _: u32) void { - std.debug.panic("TODO: Implement Bus#writeWord", .{}); + pub fn writeWord(self: *@This(), addr: u32, word: u32) void { + // TODO: Actually implement the memory mmap + if (addr >= self.pak.buf.len) { + return; + } + + self.pak.writeWord(addr, word); } pub fn readHalfWord(self: *const @This(), addr: u32) u16 { diff --git a/src/cpu.zig b/src/cpu.zig index dbb758a..97cb45c 100644 --- a/src/cpu.zig +++ b/src/cpu.zig @@ -27,8 +27,8 @@ pub const ARM7TDMI = struct { pub inline fn step(self: *@This()) u64 { const opcode = self.fetch(); - // Debug - std.debug.print("R15: 0x{X:}\n", .{opcode}); + + std.debug.print("R15: 0x{X:}\n", .{opcode}); // Debug ARM_LUT[armIdx(opcode)](self, self.bus, opcode); diff --git a/src/cpu/single_data_transfer.zig b/src/cpu/single_data_transfer.zig index 468c15c..1d107dd 100644 --- a/src/cpu/single_data_transfer.zig +++ b/src/cpu/single_data_transfer.zig @@ -24,7 +24,9 @@ pub fn comptimeSingleDataTransfer(comptime I: bool, comptime P: bool, comptime U cpu.r[rd] = bus.readByte(address); } else { // LDR - std.debug.panic("Implement LDR", .{}); + + // FIXME: Unsure about how I calculate the boundary offset + cpu.r[rd] = std.math.rotl(u32, bus.readWord(address), address % 4); } } else { if (B) { @@ -37,7 +39,11 @@ pub fn comptimeSingleDataTransfer(comptime I: bool, comptime P: bool, comptime U bus.writeByte(address, src); } else { // STR - std.debug.panic("Implement STR", .{}); + + // FIXME: Is this right? + const src = cpu.r[rd]; + const aligned_addr = address - (address % 4); + bus.writeWord(aligned_addr, src); } } diff --git a/src/pak.zig b/src/pak.zig index 1e77b85..f16ca3c 100644 --- a/src/pak.zig +++ b/src/pak.zig @@ -20,6 +20,13 @@ pub const GamePak = struct { return (@as(u32, self.buf[addr + 3]) << 24) | (@as(u32, self.buf[addr + 2]) << 16) | (@as(u32, self.buf[addr + 1]) << 8) | (@as(u32, self.buf[addr])); } + pub fn writeWord(self: *const @This(), addr: u32, word: u32) void { + self.buf[addr + 3] = @truncate(u8, word >> 24); + self.buf[addr + 2] = @truncate(u8, word >> 16); + self.buf[addr + 1] = @truncate(u8, word >> 8); + self.buf[addr] = @truncate(u8, word); + } + pub fn readHalfWord(self: *const @This(), addr: u32) u16 { return (@as(u16, self.buf[addr + 1]) << 8) | @as(u16, self.buf[addr]); }