diff --git a/src/crab/thumb/multiple_load_store.cr b/src/crab/thumb/multiple_load_store.cr index 8762751..6919268 100644 --- a/src/crab/thumb/multiple_load_store.cr +++ b/src/crab/thumb/multiple_load_store.cr @@ -4,21 +4,30 @@ module THUMB rb = bits(instr, 8..10) list = bits(instr, 0..7) address = @r[rb] - if load # ldmia - 8.times do |idx| - if bit?(list, idx) - set_reg(idx, @gba.bus.read_word(address)) - address &+= 4 + unless list == 0 + if load # ldmia + 8.times do |idx| + if bit?(list, idx) + set_reg(idx, @gba.bus.read_word(address)) + address &+= 4 + end + end + else # stmia + 8.times do |idx| + if bit?(list, idx) + @gba.bus[address] = @r[idx] + address &+= 4 + end end end - else # stmia - 7.downto(0).each do |idx| - if bit?(list, idx) - @gba.bus[address] = @r[idx] - address &+= 4 - end + set_reg(rb, address) + else # https://github.com/jsmolka/gba-suite/blob/0e32e15c6241e6dc20851563ba88f4656ac50936/thumb/memory.asm#L459 + if load + set_reg(15, @gba.bus.read_word(address)) + else + @gba.bus[address] = @r[15] &+ 2 end + set_reg(rb, address &+ 0x40) end - set_reg(rb, address) end end