zba/src/cpu/half_signed_data_transfer.zig

68 lines
2.4 KiB
Zig
Raw Normal View History

2021-12-29 21:09:00 +00:00
const std = @import("std");
2022-10-21 08:11:44 +00:00
const processor = @import("../cpu.zig");
2021-12-29 21:09:00 +00:00
const util = @import("../util.zig");
const Bus = @import("../bus.zig").Bus;
2022-10-21 08:11:44 +00:00
const Arm7tdmi = processor.Arm7tdmi;
const InstrFn = processor.InstrFn;
2021-12-29 21:09:00 +00:00
pub fn comptimeHalfSignedDataTransfer(comptime P: bool, comptime U: bool, comptime I: bool, comptime W: bool, comptime L: bool) InstrFn {
return struct {
2022-10-21 08:11:44 +00:00
fn halfSignedDataTransfer(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;
const base = cpu.r[rn];
var offset: u32 = undefined;
if (I) {
offset = imm_offset_high << 4 | rm;
} else {
offset = cpu.r[rm];
}
const modified_base = if (U) base + offset else base - offset;
var address = if (P) modified_base else base;
if (L) {
2022-10-21 08:11:43 +00:00
switch (@truncate(u2, opcode >> 5)) {
2021-12-29 21:09:00 +00:00
0b00 => {
2022-10-21 08:11:43 +00:00
// SWP
std.debug.panic("[CPU] TODO: Implement SWP", .{});
2021-12-29 21:09:00 +00:00
},
0b01 => {
// LDRH
const halfword = bus.read16(address);
2021-12-29 21:09:00 +00:00
cpu.r[rd] = @as(u32, halfword);
},
0b10 => {
// LDRSB
const byte = bus.read8(address);
cpu.r[rd] = util.u32SignExtend(8, @as(u32, byte));
2021-12-29 21:09:00 +00:00
},
0b11 => {
// LDRSH
const halfword = bus.read16(address);
cpu.r[rd] = util.u32SignExtend(16, @as(u32, halfword));
2022-10-21 08:11:43 +00:00
},
2021-12-29 21:09:00 +00:00
}
} else {
if (opcode >> 5 & 0x01 == 0x01) {
// STRH
const src = @truncate(u16, cpu.r[rd]);
bus.write16(address + 2, src);
bus.write16(address, src);
2021-12-29 21:09:00 +00:00
} else {
std.debug.panic("[CPU] TODO: Figure out if this is also SWP", .{});
2021-12-29 21:09:00 +00:00
}
}
address = modified_base;
if (W and P) cpu.r[rn] = address;
}
}.halfSignedDataTransfer;
2022-10-21 08:11:43 +00:00
}