2021-12-29 21:09:00 +00:00
|
|
|
const std = @import("std");
|
|
|
|
|
2022-10-21 08:11:49 +00:00
|
|
|
const BarrelShifter = @import("barrel_shifter.zig");
|
2022-10-21 08:11:50 +00:00
|
|
|
const Bus = @import("../Bus.zig");
|
2022-10-21 08:11:50 +00:00
|
|
|
const Arm7tdmi = @import("../cpu.zig").Arm7tdmi;
|
|
|
|
const InstrFn = @import("../cpu.zig").InstrFn;
|
2021-12-29 21:09:00 +00:00
|
|
|
|
2022-10-21 08:11:50 +00:00
|
|
|
pub fn dataProcessing(comptime I: bool, comptime S: bool, comptime instrKind: u4) InstrFn {
|
2021-12-29 21:09:00 +00:00
|
|
|
return struct {
|
2022-10-21 08:11:50 +00:00
|
|
|
fn inner(cpu: *Arm7tdmi, _: *Bus, opcode: u32) void {
|
2021-12-29 21:09:00 +00:00
|
|
|
const rd = opcode >> 12 & 0xF;
|
2022-10-21 08:11:55 +00:00
|
|
|
const rn = opcode >> 16 & 0xF;
|
|
|
|
|
|
|
|
if (S and rd == 0xF) std.debug.panic("[CPU] Data Processing Instruction w/ S set and Rd == 15", .{});
|
|
|
|
|
|
|
|
var op1: u32 = undefined;
|
|
|
|
if (rn == 0xF) {
|
|
|
|
op1 = cpu.fakePC();
|
|
|
|
} else {
|
|
|
|
op1 = cpu.r[rn];
|
|
|
|
}
|
2021-12-29 21:09:00 +00:00
|
|
|
|
|
|
|
var op2: u32 = undefined;
|
|
|
|
if (I) {
|
|
|
|
op2 = std.math.rotr(u32, opcode & 0xFF, (opcode >> 8 & 0xF) << 1);
|
|
|
|
} else {
|
2022-10-21 08:11:55 +00:00
|
|
|
op2 = BarrelShifter.exec(S, cpu, opcode);
|
2021-12-29 21:09:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (instrKind) {
|
|
|
|
0x4 => {
|
2022-10-21 08:11:44 +00:00
|
|
|
// ADD
|
2022-10-21 08:11:49 +00:00
|
|
|
var result: u32 = undefined;
|
2022-10-21 08:11:55 +00:00
|
|
|
const didOverflow = @addWithOverflow(u32, op1, op2, &result);
|
2022-10-21 08:11:49 +00:00
|
|
|
cpu.r[rd] = result;
|
2021-12-29 21:09:00 +00:00
|
|
|
|
2022-10-21 08:11:49 +00:00
|
|
|
if (S and rd != 0xF) {
|
|
|
|
cpu.cpsr.n.write(result >> 31 & 1 == 1);
|
|
|
|
cpu.cpsr.z.write(result == 0);
|
|
|
|
cpu.cpsr.c.write(didOverflow);
|
|
|
|
cpu.cpsr.v.write(((op1 ^ result) & (op2 ^ result)) >> 31 & 1 == 1);
|
|
|
|
}
|
2021-12-29 21:09:00 +00:00
|
|
|
},
|
2022-10-21 08:11:48 +00:00
|
|
|
0x8 => {
|
|
|
|
// TST
|
2022-10-21 08:11:55 +00:00
|
|
|
const result = op1 & op2;
|
2022-10-21 08:11:49 +00:00
|
|
|
|
|
|
|
cpu.cpsr.n.write(result >> 31 & 1 == 1);
|
|
|
|
cpu.cpsr.z.write(result == 0);
|
|
|
|
// Barrel Shifter should always calc CPSR C in TST
|
|
|
|
if (!S) _ = BarrelShifter.exec(true, cpu, opcode);
|
2022-10-21 08:11:48 +00:00
|
|
|
},
|
2022-10-21 08:11:53 +00:00
|
|
|
0x9 => {
|
|
|
|
// TEQ
|
2022-10-21 08:11:55 +00:00
|
|
|
const result = op1 ^ op2;
|
2022-10-21 08:11:53 +00:00
|
|
|
|
|
|
|
cpu.cpsr.n.write(result >> 31 & 1 == 1);
|
|
|
|
cpu.cpsr.z.write(result == 0);
|
|
|
|
// Barrel Shifter should always calc CPSR C in TEQ
|
|
|
|
if (!S) _ = BarrelShifter.exec(true, cpu, opcode);
|
|
|
|
},
|
2021-12-29 21:09:00 +00:00
|
|
|
0xD => {
|
2022-10-21 08:11:44 +00:00
|
|
|
// MOV
|
2021-12-29 21:09:00 +00:00
|
|
|
cpu.r[rd] = op2;
|
|
|
|
|
2022-10-21 08:11:49 +00:00
|
|
|
if (S and rd != 0xF) {
|
|
|
|
cpu.cpsr.n.write(op2 >> 31 & 1 == 1);
|
|
|
|
cpu.cpsr.z.write(op2 == 0);
|
|
|
|
// C set by Barr0x15el Shifter, V is unnafected
|
|
|
|
}
|
2021-12-29 21:09:00 +00:00
|
|
|
},
|
2022-10-21 08:11:44 +00:00
|
|
|
0xA => {
|
|
|
|
// CMP
|
2022-10-21 08:11:55 +00:00
|
|
|
const result = op1 -% op2;
|
2022-10-21 08:11:44 +00:00
|
|
|
|
2022-10-21 08:11:48 +00:00
|
|
|
cpu.cpsr.n.write(result >> 31 & 1 == 1);
|
|
|
|
cpu.cpsr.z.write(result == 0);
|
2022-10-21 08:11:55 +00:00
|
|
|
cpu.cpsr.c.write(op2 <= op1);
|
2022-10-21 08:11:49 +00:00
|
|
|
cpu.cpsr.v.write(((op1 ^ result) & (~op2 ^ result)) >> 31 & 1 == 1);
|
2022-10-21 08:11:44 +00:00
|
|
|
},
|
2022-10-21 08:11:53 +00:00
|
|
|
0xC => {
|
|
|
|
// ORR
|
2022-10-21 08:11:55 +00:00
|
|
|
const result = op1 | op2;
|
2022-10-21 08:11:54 +00:00
|
|
|
cpu.r[rd] = result;
|
2022-10-21 08:11:53 +00:00
|
|
|
|
|
|
|
if (S and rd != 0xF) {
|
|
|
|
cpu.cpsr.n.write(result >> 31 & 1 == 1);
|
|
|
|
cpu.cpsr.z.write(result == 0);
|
|
|
|
// C set by Barr0x15el Shifter, V is unnafected
|
|
|
|
}
|
|
|
|
},
|
2022-10-21 08:11:46 +00:00
|
|
|
else => std.debug.panic("[CPU] TODO: implement data processing type {}", .{instrKind}),
|
2021-12-29 21:09:00 +00:00
|
|
|
}
|
|
|
|
}
|
2022-10-21 08:11:50 +00:00
|
|
|
}.inner;
|
2021-12-29 21:09:00 +00:00
|
|
|
}
|