2022-01-29 22:52:16 +00:00
|
|
|
const shifter = @import("../barrel_shifter.zig");
|
2022-01-14 08:26:09 +00:00
|
|
|
const Bus = @import("../../Bus.zig");
|
|
|
|
const Arm7tdmi = @import("../../cpu.zig").Arm7tdmi;
|
2022-07-27 16:23:29 +00:00
|
|
|
const InstrFn = @import("../../cpu.zig").arm.InstrFn;
|
2021-12-29 21:09:00 +00:00
|
|
|
|
2023-03-10 06:05:31 +00:00
|
|
|
const rotr = @import("zba-util").rotr;
|
2022-03-17 01:28:24 +00:00
|
|
|
|
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;
|
|
|
|
|
2023-01-29 14:57:20 +00:00
|
|
|
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
|
|
|
|
2022-02-02 18:00:43 +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;
|
|
|
|
|
2022-02-02 18:00:43 +00:00
|
|
|
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);
|
2022-03-17 01:28:24 +00:00
|
|
|
result = rotr(u32, value, 8 * (address & 0x3));
|
2021-12-29 21:09:00 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (B) {
|
|
|
|
// STRB
|
2022-06-29 09:33:17 +00:00
|
|
|
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
|
2022-06-29 09:33:17 +00:00
|
|
|
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;
|
2022-06-29 09:33:17 +00:00
|
|
|
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);
|
2022-06-29 09:33:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2022-06-29 09:33:17 +00:00
|
|
|
}
|
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
|
|
|
}
|