fix(cpu): resolve off-by-one error when executing LDM

This commit is contained in:
Rekai Nyangadzayi Musuka 2022-10-21 05:11:56 -03:00
parent 97b933d9ea
commit 9b26454c72
1 changed files with 8 additions and 9 deletions

View File

@ -12,9 +12,10 @@ pub fn blockDataTransfer(comptime P: bool, comptime U: bool, comptime S: bool, c
if (S and opcode >> 15 & 1 == 0) std.debug.panic("[CPU] TODO: STM/LDM with S set but R15 not in transfer list", .{}); if (S and opcode >> 15 & 1 == 0) std.debug.panic("[CPU] TODO: STM/LDM with S set but R15 not in transfer list", .{});
var address: u32 = undefined;
if (U) { if (U) {
// Increment // Increment
var address = if (P) base + 4 else base; address = if (P) base + 4 else base;
var i: u5 = 0; var i: u5 = 0;
while (i < 0x10) : (i += 1) { while (i < 0x10) : (i += 1) {
@ -23,24 +24,22 @@ pub fn blockDataTransfer(comptime P: bool, comptime U: bool, comptime S: bool, c
address += 4; address += 4;
} }
} }
if (W and P or !P) cpu.r[rn] = address - 4;
} else { } else {
// Decrement // Decrement
var address = if (P) base - 4 else base; address = if (P) base - 4 else base;
var i: u5 = 0x10; var i: u5 = 0x10;
while (i > 0) : (i -= 1) { while (i > 0) : (i -= 1) {
const reg_idx = i - 1; const j = i - 1;
if (opcode >> reg_idx & 1 == 1) { if (opcode >> j & 1 == 1) {
transfer(cpu, bus, reg_idx, address); transfer(cpu, bus, j, address);
address -= 4; address -= 4;
} }
} }
if (W and P or !P) cpu.r[rn] = address + 4;
} }
if (W and P or !P) cpu.r[rn] = if (U) address else address + 4;
} }
fn transfer(cpu: *Arm7tdmi, bus: *Bus, i: u5, address: u32) void { fn transfer(cpu: *Arm7tdmi, bus: *Bus, i: u5, address: u32) void {