From f63ae76931c798036a92d7658f2d0cc18aff131a Mon Sep 17 00:00:00 2001 From: Rekai Musuka Date: Fri, 21 Oct 2022 05:12:15 -0300 Subject: [PATCH] fix(cpu): handle edge case in LDRSH --- src/cpu/thumb/data_transfer.zig | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/cpu/thumb/data_transfer.zig b/src/cpu/thumb/data_transfer.zig index 3b18d2b..fb5240c 100644 --- a/src/cpu/thumb/data_transfer.zig +++ b/src/cpu/thumb/data_transfer.zig @@ -44,8 +44,14 @@ pub fn format78(comptime op: u2, comptime T: bool) InstrFn { cpu.r[rd] = std.math.rotr(u32, @as(u32, value), 8 * (address & 1)); }, 0b11 => { - // LDSH - cpu.r[rd] = u32SignExtend(16, @as(u32, bus.read16(address & 0xFFFF_FFFE))); + // LDRSH + const value = if (address & 1 == 1) blk: { + break :blk u32SignExtend(8, bus.read8(address)); + } else blk: { + break :blk u32SignExtend(16, bus.read16(address)); + }; + + cpu.r[rd] = std.math.rotr(u32, value, 8 * (address & 1)); }, } } else {