Compare commits
No commits in common. "fc5a3460dd32e34794f175a668467299d2d1098b" and "2f3213f69383dad807a3c20181a5f62929c461ea" have entirely different histories.
fc5a3460dd
...
2f3213f693
|
@ -201,10 +201,9 @@ fn armPopulate() [0x1000]ArmInstrFn {
|
||||||
if (i >> 10 & 0x3 == 0b00 and i >> 7 & 0x3 == 0b10 and i >> 4 & 1 == 0) {
|
if (i >> 10 & 0x3 == 0b00 and i >> 7 & 0x3 == 0b10 and i >> 4 & 1 == 0) {
|
||||||
// PSR Transfer
|
// PSR Transfer
|
||||||
const I = i >> 9 & 1 == 1;
|
const I = i >> 9 & 1 == 1;
|
||||||
const R = i >> 6 & 1 == 1;
|
const isSpsr = i >> 6 & 1 == 1;
|
||||||
const kind = i >> 4 & 0x3;
|
|
||||||
|
|
||||||
lut[i] = psrTransfer(I, R, kind);
|
lut[i] = psrTransfer(I, isSpsr);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (i == 0x121) {
|
if (i == 0x121) {
|
||||||
|
|
|
@ -33,7 +33,7 @@ fn immShift(comptime S: bool, cpu: *Arm7tdmi, opcode: u32) u32 {
|
||||||
const amount = @truncate(u8, opcode >> 7 & 0x1F);
|
const amount = @truncate(u8, opcode >> 7 & 0x1F);
|
||||||
|
|
||||||
const rm_idx = opcode & 0xF;
|
const rm_idx = opcode & 0xF;
|
||||||
const rm = if (rm_idx == 0xF) cpu.fakePC() else cpu.r[rm_idx];
|
const rm = if (rm_idx == 0xF) cpu.fakePC() + 4 else cpu.r[rm_idx];
|
||||||
|
|
||||||
var result: u32 = undefined;
|
var result: u32 = undefined;
|
||||||
if (amount == 0) {
|
if (amount == 0) {
|
||||||
|
|
|
@ -8,7 +8,7 @@ const InstrFn = @import("../../cpu.zig").ArmInstrFn;
|
||||||
pub fn dataProcessing(comptime I: bool, comptime S: bool, comptime instrKind: u4) InstrFn {
|
pub fn dataProcessing(comptime I: bool, comptime S: bool, comptime instrKind: u4) InstrFn {
|
||||||
return struct {
|
return struct {
|
||||||
fn inner(cpu: *Arm7tdmi, _: *Bus, opcode: u32) void {
|
fn inner(cpu: *Arm7tdmi, _: *Bus, opcode: u32) void {
|
||||||
const rd = @truncate(u4, opcode >> 12 & 0xF);
|
const rd = opcode >> 12 & 0xF;
|
||||||
const rn = opcode >> 16 & 0xF;
|
const rn = opcode >> 16 & 0xF;
|
||||||
const old_carry = @boolToInt(cpu.cpsr.c.read());
|
const old_carry = @boolToInt(cpu.cpsr.c.read());
|
||||||
|
|
||||||
|
@ -23,30 +23,18 @@ pub fn dataProcessing(comptime I: bool, comptime S: bool, comptime instrKind: u4
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (instrKind) {
|
switch (instrKind) {
|
||||||
0x0 => {
|
0x2 => {
|
||||||
// AND
|
// SUB
|
||||||
const result = op1 & op2;
|
const result = op1 -% op2;
|
||||||
cpu.r[rd] = result;
|
cpu.r[rd] = result;
|
||||||
|
|
||||||
if (S and rd != 0xF) {
|
if (S and rd != 0xF) {
|
||||||
cpu.cpsr.n.write(result >> 31 & 1 == 1);
|
cpu.cpsr.n.write(result >> 31 & 1 == 1);
|
||||||
cpu.cpsr.z.write(result == 0);
|
cpu.cpsr.z.write(result == 0);
|
||||||
// C set by Barrel Shifter, V is unaffected
|
cpu.cpsr.c.write(op2 <= op1);
|
||||||
|
cpu.cpsr.v.write(((op1 ^ result) & (~op2 ^ result)) >> 31 & 1 == 1);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
0x1 => {
|
|
||||||
// EOR
|
|
||||||
const result = op1 ^ op2;
|
|
||||||
cpu.r[rd] = result;
|
|
||||||
|
|
||||||
if (S and rd != 0xF) {
|
|
||||||
cpu.cpsr.n.write(result >> 31 & 1 == 1);
|
|
||||||
cpu.cpsr.z.write(result == 0);
|
|
||||||
// C set by Barrel Shifter, V is unaffected
|
|
||||||
}
|
|
||||||
},
|
|
||||||
0x2 => sub(S, cpu, rd, op1, op2), // SUB
|
|
||||||
0x3 => sub(S, cpu, rd, op2, op1), // RSB
|
|
||||||
0x4 => {
|
0x4 => {
|
||||||
// ADD
|
// ADD
|
||||||
var result: u32 = undefined;
|
var result: u32 = undefined;
|
||||||
|
@ -75,8 +63,20 @@ pub fn dataProcessing(comptime I: bool, comptime S: bool, comptime instrKind: u4
|
||||||
cpu.cpsr.v.write(((op1 ^ result) & (op2 ^ result)) >> 31 & 1 == 1);
|
cpu.cpsr.v.write(((op1 ^ result) & (op2 ^ result)) >> 31 & 1 == 1);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
0x6 => sbc(S, cpu, rd, op1, op2, old_carry), // SBC
|
0x6 => {
|
||||||
0x7 => sbc(S, cpu, rd, op2, op1, old_carry), // RSC
|
// SBC
|
||||||
|
// TODO: Make your own
|
||||||
|
const subtrahend = @as(u64, op2) - old_carry + 1;
|
||||||
|
const result = @truncate(u32, op1 -% subtrahend);
|
||||||
|
cpu.r[rd] = result;
|
||||||
|
|
||||||
|
if (S and rd != 0xF) {
|
||||||
|
cpu.cpsr.n.write(result >> 31 & 1 == 1);
|
||||||
|
cpu.cpsr.z.write(result == 0);
|
||||||
|
cpu.cpsr.c.write(subtrahend <= op1);
|
||||||
|
cpu.cpsr.v.write(((op1 ^ result) & (~op2 ^ result)) >> 31 & 1 == 1);
|
||||||
|
}
|
||||||
|
},
|
||||||
0x8 => {
|
0x8 => {
|
||||||
// TST
|
// TST
|
||||||
const result = op1 & op2;
|
const result = op1 & op2;
|
||||||
|
@ -95,36 +95,6 @@ pub fn dataProcessing(comptime I: bool, comptime S: bool, comptime instrKind: u4
|
||||||
// Barrel Shifter should always calc CPSR C in TEQ
|
// Barrel Shifter should always calc CPSR C in TEQ
|
||||||
if (!S) _ = shifter.execute(true, cpu, opcode);
|
if (!S) _ = shifter.execute(true, cpu, opcode);
|
||||||
},
|
},
|
||||||
0xA => {
|
|
||||||
// CMP
|
|
||||||
const result = op1 -% op2;
|
|
||||||
|
|
||||||
cpu.cpsr.n.write(result >> 31 & 1 == 1);
|
|
||||||
cpu.cpsr.z.write(result == 0);
|
|
||||||
cpu.cpsr.c.write(op2 <= op1);
|
|
||||||
cpu.cpsr.v.write(((op1 ^ result) & (~op2 ^ result)) >> 31 & 1 == 1);
|
|
||||||
},
|
|
||||||
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);
|
|
||||||
},
|
|
||||||
0xC => {
|
|
||||||
// ORR
|
|
||||||
const result = op1 | op2;
|
|
||||||
cpu.r[rd] = result;
|
|
||||||
|
|
||||||
if (S and rd != 0xF) {
|
|
||||||
cpu.cpsr.n.write(result >> 31 & 1 == 1);
|
|
||||||
cpu.cpsr.z.write(result == 0);
|
|
||||||
// C set by Barrel Shifter, V is unaffected
|
|
||||||
}
|
|
||||||
},
|
|
||||||
0xD => {
|
0xD => {
|
||||||
// MOV
|
// MOV
|
||||||
cpu.r[rd] = op2;
|
cpu.r[rd] = op2;
|
||||||
|
@ -135,9 +105,18 @@ pub fn dataProcessing(comptime I: bool, comptime S: bool, comptime instrKind: u4
|
||||||
// C set by Barrel Shifter, V is unaffected
|
// C set by Barrel Shifter, V is unaffected
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
0xE => {
|
0xA => {
|
||||||
// BIC
|
// CMP
|
||||||
const result = op1 & ~op2;
|
const result = op1 -% op2;
|
||||||
|
|
||||||
|
cpu.cpsr.n.write(result >> 31 & 1 == 1);
|
||||||
|
cpu.cpsr.z.write(result == 0);
|
||||||
|
cpu.cpsr.c.write(op2 <= op1);
|
||||||
|
cpu.cpsr.v.write(((op1 ^ result) & (~op2 ^ result)) >> 31 & 1 == 1);
|
||||||
|
},
|
||||||
|
0xC => {
|
||||||
|
// ORR
|
||||||
|
const result = op1 | op2;
|
||||||
cpu.r[rd] = result;
|
cpu.r[rd] = result;
|
||||||
|
|
||||||
if (S and rd != 0xF) {
|
if (S and rd != 0xF) {
|
||||||
|
@ -157,33 +136,8 @@ pub fn dataProcessing(comptime I: bool, comptime S: bool, comptime instrKind: u4
|
||||||
// C set by Barrel Shifter, V is unaffected
|
// C set by Barrel Shifter, V is unaffected
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
else => std.debug.panic("[CPU] TODO: implement data processing type {}", .{instrKind}),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}.inner;
|
}.inner;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn sbc(comptime S: bool, cpu: *Arm7tdmi, rd: u4, left: u32, right: u32, old_carry: u1) void {
|
|
||||||
// TODO: Make your own version (thanks peach.bot)
|
|
||||||
const subtrahend = @as(u64, right) - old_carry + 1;
|
|
||||||
const result = @truncate(u32, left -% subtrahend);
|
|
||||||
cpu.r[rd] = result;
|
|
||||||
|
|
||||||
if (S and rd != 0xF) {
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn sub(comptime S: bool, cpu: *Arm7tdmi, rd: u4, left: u32, right: u32) void {
|
|
||||||
const result = left -% right;
|
|
||||||
cpu.r[rd] = result;
|
|
||||||
|
|
||||||
if (S and rd != 0xF) {
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -3,59 +3,49 @@ const std = @import("std");
|
||||||
const Bus = @import("../../Bus.zig");
|
const Bus = @import("../../Bus.zig");
|
||||||
const Arm7tdmi = @import("../../cpu.zig").Arm7tdmi;
|
const Arm7tdmi = @import("../../cpu.zig").Arm7tdmi;
|
||||||
const InstrFn = @import("../../cpu.zig").ArmInstrFn;
|
const InstrFn = @import("../../cpu.zig").ArmInstrFn;
|
||||||
const PSR = @import("../../cpu.zig").PSR;
|
|
||||||
|
|
||||||
pub fn psrTransfer(comptime I: bool, comptime R: bool, comptime kind: u2) InstrFn {
|
pub fn psrTransfer(comptime I: bool, comptime isSpsr: bool) InstrFn {
|
||||||
return struct {
|
return struct {
|
||||||
fn inner(cpu: *Arm7tdmi, _: *Bus, opcode: u32) void {
|
fn inner(cpu: *Arm7tdmi, _: *Bus, opcode: u32) void {
|
||||||
switch (kind) {
|
switch (@truncate(u3, opcode >> 19)) {
|
||||||
0b00 => {
|
0b001 => {
|
||||||
// MRS
|
// MRS
|
||||||
const rd = opcode >> 12 & 0xF;
|
const rn = opcode >> 12 & 0xF;
|
||||||
|
|
||||||
if (R) {
|
if (isSpsr) {
|
||||||
std.debug.panic("[CPU/PSR Transfer] TODO: MRS on SPSR_<current_mode> is unimplemented", .{});
|
std.debug.panic("[CPU] TODO: MRS on SPSR_<current_mode> is unimplemented", .{});
|
||||||
} else {
|
} else {
|
||||||
cpu.r[rd] = cpu.cpsr.raw;
|
cpu.r[rn] = cpu.cpsr.raw;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
0b10 => {
|
0b101 => {
|
||||||
// MSR
|
// MSR
|
||||||
const field_mask = @truncate(u4, opcode >> 16 & 0xF);
|
const rm = opcode & 0xF;
|
||||||
|
|
||||||
if (I) {
|
switch (@truncate(u3, opcode >> 16)) {
|
||||||
const imm = std.math.rotr(u32, opcode & 0xFF, (opcode >> 8 & 0xF) << 1);
|
0b000 => {
|
||||||
|
const right = if (I) std.math.rotr(u32, opcode & 0xFF, opcode >> 7 & 0xF) else cpu.r[rm];
|
||||||
|
|
||||||
if (R) {
|
if (isSpsr) {
|
||||||
std.debug.panic("[CPU/PSR Transfer] TODO: MSR (flags only) on SPSR_<current_mode> is unimplemented", .{});
|
std.debug.panic("[CPU] TODO: MSR (flags only) on SPSR_<current_mode> is unimplemented", .{});
|
||||||
} else {
|
} else {
|
||||||
cpu.cpsr.raw = fieldMask(&cpu.cpsr, field_mask, imm);
|
const mask: u32 = 0xF000_0000;
|
||||||
}
|
cpu.cpsr.raw = (cpu.cpsr.raw & ~mask) | (right & mask);
|
||||||
} else {
|
|
||||||
const rm_idx = opcode & 0xF;
|
|
||||||
|
|
||||||
if (R) {
|
|
||||||
std.debug.panic("[CPU/PSR Transfer] TODO: MSR on SPSR_<current_mode> is unimplemented", .{});
|
|
||||||
} else {
|
|
||||||
cpu.cpsr.raw = fieldMask(&cpu.cpsr, field_mask, cpu.r[rm_idx]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
else => std.debug.panic("[CPU/PSR Transfer] Bits 21:220 of {X:0>8} are undefined", .{opcode}),
|
0b001 => {
|
||||||
|
if (isSpsr) {
|
||||||
|
std.debug.panic("[CPU] TODO: MSR on SPSR_<current_mode> is unimplemented", .{});
|
||||||
|
} else {
|
||||||
|
cpu.cpsr = .{ .raw = cpu.r[rm] };
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
else => unreachable,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
else => unreachable,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}.inner;
|
}.inner;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn fieldMask(psr: *const PSR, field_mask: u4, right: u32) u32 {
|
|
||||||
const bits = @truncate(u2, (field_mask >> 2 & 0x2) | (field_mask & 1));
|
|
||||||
|
|
||||||
const mask: u32 = switch (bits) {
|
|
||||||
0b00 => 0x0000_0000,
|
|
||||||
0b01 => 0x0000_00FF,
|
|
||||||
0b10 => 0xF000_0000,
|
|
||||||
0b11 => 0xF000_00FF,
|
|
||||||
};
|
|
||||||
|
|
||||||
return (psr.raw & ~mask) | (right & mask);
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue