From ae53f92d40152fd77dbafd1c4b6a438d7d2f216a Mon Sep 17 00:00:00 2001 From: Rekai Musuka Date: Fri, 21 Oct 2022 05:11:55 -0300 Subject: [PATCH] fix(cpu): make LDRH and STRH aware of r15 --- src/cpu/half_signed_data_transfer.zig | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/cpu/half_signed_data_transfer.zig b/src/cpu/half_signed_data_transfer.zig index 7ad507b..44ab3e0 100644 --- a/src/cpu/half_signed_data_transfer.zig +++ b/src/cpu/half_signed_data_transfer.zig @@ -13,7 +13,13 @@ pub fn halfAndSignedDataTransfer(comptime P: bool, comptime U: bool, comptime I: const rm = opcode & 0xF; const imm_offset_high = opcode >> 8 & 0xF; - const base = cpu.r[rn]; + var base: u32 = undefined; + if (rn == 0xF) { + base = cpu.fakePC(); + if (!L) base += 4; + } else { + base = cpu.r[rn]; + } var offset: u32 = undefined; if (I) { @@ -33,7 +39,8 @@ pub fn halfAndSignedDataTransfer(comptime P: bool, comptime U: bool, comptime I: }, 0b01 => { // LDRH - cpu.r[rd] = bus.read16(address); + const value = bus.read16(address & 0xFFFF_FFFE); + cpu.r[rd] = std.math.rotr(u32, @as(u32, value), 8 * (address & 1)); }, 0b10 => { // LDRSB