zba/src/core/cpu/arm/single_data_transfer.zig

56 lines
2.0 KiB
Zig
Raw Normal View History

const shifter = @import("../barrel_shifter.zig");
const Bus = @import("../../Bus.zig");
const Arm7tdmi = @import("../../cpu.zig").Arm7tdmi;
const InstrFn = @import("../../cpu.zig").arm.InstrFn;
2021-12-29 21:09:00 +00:00
const rotr = @import("zba-util").rotr;
2022-01-07 23:44:48 +00:00
pub fn singleDataTransfer(comptime I: bool, comptime P: bool, comptime U: bool, comptime B: bool, comptime W: bool, comptime L: bool) InstrFn {
2021-12-29 21:09:00 +00:00
return struct {
2022-01-07 23:44:48 +00:00
fn inner(cpu: *Arm7tdmi, bus: *Bus, opcode: u32) void {
2021-12-29 21:09:00 +00:00
const rn = opcode >> 16 & 0xF;
const rd = opcode >> 12 & 0xF;
const base = cpu.r[rn];
2022-10-01 16:17:57 +00:00
const offset = if (I) shifter.immediate(false, cpu, opcode) else opcode & 0xFFF;
2021-12-29 21:09:00 +00:00
const modified_base = if (U) base +% offset else base -% offset;
2021-12-29 21:09:00 +00:00
var address = if (P) modified_base else base;
var result: u32 = undefined;
2021-12-29 21:09:00 +00:00
if (L) {
if (B) {
// LDRB
2022-04-08 19:48:43 +00:00
result = bus.read(u8, address);
2021-12-29 21:09:00 +00:00
} else {
// LDR
2022-04-08 19:48:43 +00:00
const value = bus.read(u32, address);
result = rotr(u32, value, 8 * (address & 0x3));
2021-12-29 21:09:00 +00:00
}
} else {
if (B) {
// STRB
const value = cpu.r[rd] + if (rd == 0xF) 4 else @as(u32, 0); // PC is 12 ahead
2022-04-08 19:48:43 +00:00
bus.write(u8, address, @truncate(u8, value));
2021-12-29 21:09:00 +00:00
} else {
// STR
const value = cpu.r[rd] + if (rd == 0xF) 4 else @as(u32, 0);
2022-04-08 19:48:43 +00:00
bus.write(u32, address, value);
2021-12-29 21:09:00 +00:00
}
}
address = modified_base;
if (W and P or !P) {
cpu.r[rn] = address;
2022-09-28 19:11:25 +00:00
if (rn == 0xF) cpu.pipe.reload(cpu);
}
if (L) {
// This emulates the LDR rd == rn behaviour
cpu.r[rd] = result;
2022-09-28 19:11:25 +00:00
if (rd == 0xF) cpu.pipe.reload(cpu);
}
2021-12-29 21:09:00 +00:00
}
2022-01-07 23:44:48 +00:00
}.inner;
2021-12-29 21:09:00 +00:00
}