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

58 lines
2.0 KiB
Zig
Raw Normal View History

2021-12-29 21:09:00 +00:00
const std = @import("std");
2022-09-19 19:07:19 +00:00
const util = @import("../../../util.zig");
2021-12-29 21:09:00 +00:00
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
2022-09-19 19:07:19 +00:00
const rotr = @import("../../../util.zig").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;
var base: u32 = undefined;
if (rn == 0xF) {
base = cpu.fakePC();
if (!L) base += 4; // Offset of 12
} else {
base = cpu.r[rn];
}
const offset = if (I) shifter.immShift(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 = if (rd == 0xF) cpu.r[rd] + 8 else cpu.r[rd];
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 = if (rd == 0xF) cpu.r[rd] + 8 else cpu.r[rd];
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;
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:09:00 +00:00
}