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 sext = @import("zba-util").sext;
|
|
|
|
const rotr = @import("zba-util").rotr;
|
2022-02-06 22:48:05 +00:00
|
|
|
|
2022-01-07 23:44:48 +00:00
|
|
|
pub fn halfAndSignedDataTransfer(comptime P: bool, comptime U: bool, comptime I: 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 rm = opcode & 0xF;
|
|
|
|
const imm_offset_high = opcode >> 8 & 0xF;
|
|
|
|
|
2022-06-29 09:33:17 +00:00
|
|
|
const base = cpu.r[rn] + if (!L and rn == 0xF) 4 else @as(u32, 0);
|
|
|
|
const offset = if (I) imm_offset_high << 4 | rm else cpu.r[rm];
|
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) {
|
2021-12-29 21:13:50 +00:00
|
|
|
switch (@truncate(u2, opcode >> 5)) {
|
2021-12-29 21:09:00 +00:00
|
|
|
0b01 => {
|
|
|
|
// LDRH
|
2022-04-08 19:48:43 +00:00
|
|
|
const value = bus.read(u16, address);
|
2022-03-17 01:28:24 +00:00
|
|
|
result = rotr(u32, value, 8 * (address & 1));
|
2021-12-29 21:09:00 +00:00
|
|
|
},
|
|
|
|
0b10 => {
|
|
|
|
// LDRSB
|
2022-05-23 15:58:31 +00:00
|
|
|
result = sext(u32, u8, bus.read(u8, address));
|
2021-12-29 21:09:00 +00:00
|
|
|
},
|
|
|
|
0b11 => {
|
|
|
|
// LDRSH
|
2022-10-23 01:12:41 +00:00
|
|
|
const value = bus.read(u16, address);
|
|
|
|
result = if (address & 1 == 1) sext(u32, u8, @truncate(u8, value >> 8)) else sext(u32, u16, value);
|
2021-12-29 21:13:50 +00:00
|
|
|
},
|
2022-02-02 12:44:33 +00:00
|
|
|
0b00 => unreachable, // SWP
|
2021-12-29 21:09:00 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (opcode >> 5 & 0x01 == 0x01) {
|
|
|
|
// STRH
|
2022-04-08 19:48:43 +00:00
|
|
|
bus.write(u16, address, @truncate(u16, cpu.r[rd]));
|
2022-02-02 12:44:33 +00:00
|
|
|
} else unreachable; // SWP
|
2021-12-29 21:09:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
address = modified_base;
|
2022-01-04 09:55:41 +00:00
|
|
|
if (W and P or !P) cpu.r[rn] = address;
|
2022-02-02 18:00:43 +00:00
|
|
|
if (L) cpu.r[rd] = result; // // This emulates the LDR rd == rn behaviour
|
2021-12-29 21:09:00 +00:00
|
|
|
}
|
2022-01-07 23:44:48 +00:00
|
|
|
}.inner;
|
2021-12-29 21:13:50 +00:00
|
|
|
}
|