2021-12-29 21:09:00 +00:00
|
|
|
const std = @import("std");
|
|
|
|
|
2022-01-17 19:55:55 +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-01-14 09:23:16 +00:00
|
|
|
const InstrFn = @import("../../cpu.zig").ArmInstrFn;
|
2021-12-29 21:09:00 +00:00
|
|
|
|
2022-01-07 23:44:48 +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-01-07 23:44:48 +00:00
|
|
|
fn inner(cpu: *Arm7tdmi, _: *Bus, opcode: u32) void {
|
2022-01-18 18:36:03 +00:00
|
|
|
const rd = @truncate(u4, opcode >> 12 & 0xF);
|
2022-01-12 10:54:22 +00:00
|
|
|
const rn = opcode >> 16 & 0xF;
|
2022-01-18 18:16:58 +00:00
|
|
|
const old_carry = @boolToInt(cpu.cpsr.c.read());
|
2022-01-12 10:54:22 +00:00
|
|
|
|
2022-01-17 19:55:55 +00:00
|
|
|
const op1 = if (rn == 0xF) cpu.fakePC() else cpu.r[rn];
|
2021-12-29 21:09:00 +00:00
|
|
|
|
|
|
|
var op2: u32 = undefined;
|
|
|
|
if (I) {
|
2022-01-17 15:17:04 +00:00
|
|
|
const amount = @truncate(u8, (opcode >> 8 & 0xF) << 1);
|
2022-01-17 19:55:55 +00:00
|
|
|
op2 = shifter.rotateRight(S, &cpu.cpsr, opcode & 0xFF, amount);
|
2021-12-29 21:09:00 +00:00
|
|
|
} else {
|
2022-01-17 19:55:55 +00:00
|
|
|
op2 = shifter.execute(S, cpu, opcode);
|
2021-12-29 21:09:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (instrKind) {
|
2022-01-18 18:25:29 +00:00
|
|
|
0x0 => {
|
2022-01-18 19:09:25 +00:00
|
|
|
// AND
|
2022-01-18 18:25:29 +00:00
|
|
|
const result = op1 & op2;
|
|
|
|
cpu.r[rd] = result;
|
2022-01-19 11:46:49 +00:00
|
|
|
logicFlags(S, cpu, rd, result);
|
2022-01-18 18:25:29 +00:00
|
|
|
},
|
2022-01-18 18:27:07 +00:00
|
|
|
0x1 => {
|
|
|
|
// EOR
|
|
|
|
const result = op1 ^ op2;
|
|
|
|
cpu.r[rd] = result;
|
2022-01-19 11:46:49 +00:00
|
|
|
logicFlags(S, cpu, rd, result);
|
2022-01-18 18:27:07 +00:00
|
|
|
},
|
2022-01-19 11:46:49 +00:00
|
|
|
0x2 => cpu.r[rd] = sub(S, cpu, rd, op1, op2), // SUB
|
|
|
|
0x3 => cpu.r[rd] = sub(S, cpu, rd, op2, op1), // RSB
|
2021-12-29 21:09:00 +00:00
|
|
|
0x4 => {
|
2022-01-01 09:41:50 +00:00
|
|
|
// ADD
|
2022-01-05 20:45:52 +00:00
|
|
|
var result: u32 = undefined;
|
2022-01-12 10:54:22 +00:00
|
|
|
const didOverflow = @addWithOverflow(u32, op1, op2, &result);
|
2022-01-05 20:45:52 +00:00
|
|
|
cpu.r[rd] = result;
|
2021-12-29 21:09:00 +00:00
|
|
|
|
2022-01-19 11:46:49 +00:00
|
|
|
if (S) {
|
|
|
|
if (rd == 0xF) {
|
|
|
|
cpu.setCpsr(cpu.spsr.raw);
|
|
|
|
} else {
|
|
|
|
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);
|
|
|
|
}
|
2022-01-05 20:45:52 +00:00
|
|
|
}
|
2021-12-29 21:09:00 +00:00
|
|
|
},
|
2022-01-17 18:29:34 +00:00
|
|
|
0x5 => {
|
|
|
|
// ADC
|
|
|
|
var result: u32 = undefined;
|
|
|
|
|
|
|
|
const did = @addWithOverflow(u32, op1, op2, &result);
|
2022-01-18 18:16:58 +00:00
|
|
|
const overflow = @addWithOverflow(u32, result, old_carry, &result);
|
2022-01-17 18:29:34 +00:00
|
|
|
cpu.r[rd] = result;
|
|
|
|
|
2022-01-19 11:46:49 +00:00
|
|
|
if (S) {
|
|
|
|
if (rd == 0xF) {
|
|
|
|
cpu.setCpsr(cpu.spsr.raw);
|
|
|
|
} else {
|
|
|
|
cpu.cpsr.n.write(result >> 31 & 1 == 1);
|
|
|
|
cpu.cpsr.z.write(result == 0);
|
|
|
|
cpu.cpsr.c.write(did or overflow);
|
|
|
|
cpu.cpsr.v.write(((op1 ^ result) & (op2 ^ result)) >> 31 & 1 == 1);
|
|
|
|
}
|
2022-01-17 18:29:34 +00:00
|
|
|
}
|
|
|
|
},
|
2022-01-19 11:46:49 +00:00
|
|
|
0x6 => cpu.r[rd] = sbc(S, cpu, rd, op1, op2, old_carry), // SBC
|
|
|
|
0x7 => cpu.r[rd] = sbc(S, cpu, rd, op2, op1, old_carry), // RSC
|
2022-01-04 10:06:04 +00:00
|
|
|
0x8 => {
|
|
|
|
// TST
|
2022-01-12 10:54:22 +00:00
|
|
|
const result = op1 & op2;
|
2022-01-19 11:46:49 +00:00
|
|
|
testFlags(S, cpu, opcode, result);
|
2022-01-04 10:06:04 +00:00
|
|
|
},
|
2022-01-10 12:09:02 +00:00
|
|
|
0x9 => {
|
|
|
|
// TEQ
|
2022-01-12 10:54:22 +00:00
|
|
|
const result = op1 ^ op2;
|
2022-01-19 11:46:49 +00:00
|
|
|
testFlags(S, cpu, opcode, result);
|
2022-01-10 12:09:02 +00:00
|
|
|
},
|
2022-01-01 09:41:50 +00:00
|
|
|
0xA => {
|
|
|
|
// CMP
|
2022-01-12 10:54:22 +00:00
|
|
|
const result = op1 -% op2;
|
2022-01-02 03:08:36 +00:00
|
|
|
|
2022-01-04 10:06:04 +00:00
|
|
|
cpu.cpsr.n.write(result >> 31 & 1 == 1);
|
|
|
|
cpu.cpsr.z.write(result == 0);
|
2022-01-12 10:54:22 +00:00
|
|
|
cpu.cpsr.c.write(op2 <= op1);
|
2022-01-05 20:45:52 +00:00
|
|
|
cpu.cpsr.v.write(((op1 ^ result) & (~op2 ^ result)) >> 31 & 1 == 1);
|
2022-01-01 09:41:50 +00:00
|
|
|
},
|
2022-01-18 19:09:25 +00:00
|
|
|
0xB => {
|
|
|
|
// CMN
|
|
|
|
var result: u32 = undefined;
|
|
|
|
const didOverflow = @addWithOverflow(u32, op1, op2, &result);
|
|
|
|
|
|
|
|
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);
|
|
|
|
},
|
2022-01-10 12:06:00 +00:00
|
|
|
0xC => {
|
|
|
|
// ORR
|
2022-01-12 10:54:22 +00:00
|
|
|
const result = op1 | op2;
|
2022-01-10 14:56:41 +00:00
|
|
|
cpu.r[rd] = result;
|
2022-01-19 11:46:49 +00:00
|
|
|
logicFlags(S, cpu, rd, result);
|
2022-01-17 15:30:59 +00:00
|
|
|
},
|
2022-01-18 18:36:03 +00:00
|
|
|
0xD => {
|
|
|
|
// MOV
|
|
|
|
cpu.r[rd] = op2;
|
2022-01-19 11:46:49 +00:00
|
|
|
logicFlags(S, cpu, rd, op2);
|
2022-01-18 18:36:03 +00:00
|
|
|
},
|
2022-01-18 18:28:47 +00:00
|
|
|
0xE => {
|
|
|
|
// BIC
|
|
|
|
const result = op1 & ~op2;
|
|
|
|
cpu.r[rd] = result;
|
2022-01-19 11:46:49 +00:00
|
|
|
logicFlags(S, cpu, rd, result);
|
2022-01-18 18:28:47 +00:00
|
|
|
},
|
2022-01-17 15:30:59 +00:00
|
|
|
0xF => {
|
|
|
|
// MVN
|
|
|
|
const result = ~op2;
|
|
|
|
cpu.r[rd] = result;
|
2022-01-19 11:46:49 +00:00
|
|
|
logicFlags(S, cpu, rd, result);
|
2022-01-10 12:06:00 +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
|
|
|
}
|
2022-01-18 18:36:03 +00:00
|
|
|
|
2022-01-19 11:46:49 +00:00
|
|
|
fn sbc(comptime S: bool, cpu: *Arm7tdmi, rd: u4, left: u32, right: u32, old_carry: u1) u32 {
|
2022-01-18 18:46:40 +00:00
|
|
|
// TODO: Make your own version (thanks peach.bot)
|
|
|
|
const subtrahend = @as(u64, right) - old_carry + 1;
|
|
|
|
const result = @truncate(u32, left -% subtrahend);
|
|
|
|
|
2022-01-19 11:46:49 +00:00
|
|
|
if (S) {
|
|
|
|
if (rd == 0xF) {
|
|
|
|
cpu.setCpsr(cpu.spsr.raw);
|
|
|
|
} else {
|
|
|
|
cpu.cpsr.n.write(result >> 31 & 1 == 1);
|
|
|
|
cpu.cpsr.z.write(result == 0);
|
|
|
|
cpu.cpsr.c.write(subtrahend <= left);
|
|
|
|
cpu.cpsr.v.write(((left ^ result) & (~right ^ result)) >> 31 & 1 == 1);
|
|
|
|
}
|
2022-01-18 18:46:40 +00:00
|
|
|
}
|
2022-01-19 11:46:49 +00:00
|
|
|
|
|
|
|
return result;
|
2022-01-18 18:46:40 +00:00
|
|
|
}
|
|
|
|
|
2022-01-19 11:46:49 +00:00
|
|
|
fn sub(comptime S: bool, cpu: *Arm7tdmi, rd: u4, left: u32, right: u32) u32 {
|
2022-01-18 18:36:03 +00:00
|
|
|
const result = left -% right;
|
|
|
|
|
2022-01-19 11:46:49 +00:00
|
|
|
if (S) {
|
|
|
|
if (rd == 0xF) {
|
|
|
|
cpu.setCpsr(cpu.spsr.raw);
|
|
|
|
} else {
|
|
|
|
cpu.cpsr.n.write(result >> 31 & 1 == 1);
|
|
|
|
cpu.cpsr.z.write(result == 0);
|
|
|
|
cpu.cpsr.c.write(right <= left);
|
|
|
|
cpu.cpsr.v.write(((left ^ result) & (~right ^ result)) >> 31 & 1 == 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn logicFlags(comptime S: bool, cpu: *Arm7tdmi, rd: u4, result: u32) void {
|
|
|
|
if (S) {
|
|
|
|
if (rd == 0xF) {
|
|
|
|
cpu.setCpsr(cpu.spsr.raw);
|
|
|
|
} else {
|
|
|
|
cpu.cpsr.n.write(result >> 31 & 1 == 1);
|
|
|
|
cpu.cpsr.z.write(result == 0);
|
|
|
|
// C set by Barrel Shifter, V is unaffected
|
|
|
|
}
|
2022-01-18 18:36:03 +00:00
|
|
|
}
|
|
|
|
}
|
2022-01-19 11:46:49 +00:00
|
|
|
|
|
|
|
fn testFlags(comptime S: bool, cpu: *Arm7tdmi, opcode: u32, result: u32) void {
|
|
|
|
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) _ = shifter.execute(true, cpu, opcode);
|
|
|
|
}
|