chore(cpu): refactor barrel shifter

This commit is contained in:
Rekai Nyangadzayi Musuka 2022-01-17 11:17:04 -04:00
parent d05a924420
commit 1025500407
3 changed files with 31 additions and 55 deletions

View File

@ -19,83 +19,73 @@ pub fn exec(comptime S: bool, cpu: *Arm7tdmi, opcode: u32) u32 {
value = cpu.r[opcode & 0xF]; value = cpu.r[opcode & 0xF];
} }
if (S) {
return switch (@truncate(u2, opcode >> 5)) { return switch (@truncate(u2, opcode >> 5)) {
0b00 => logicalLeft(&cpu.cpsr, value, shift_amt), 0b00 => logicalLeft(S, &cpu.cpsr, value, shift_amt),
0b01 => logicalRight(&cpu.cpsr, value, shift_amt), 0b01 => logicalRight(S, &cpu.cpsr, value, shift_amt),
0b10 => arithmetic_right(&cpu.cpsr, value, shift_amt), 0b10 => arithmeticRight(S, &cpu.cpsr, value, shift_amt),
0b11 => rotateRight(&cpu.cpsr, value, shift_amt), 0b11 => rotateRight(S, &cpu.cpsr, value, shift_amt),
}; };
} else {
var dummy = CPSR{ .raw = 0x0000_0000 };
return switch (@truncate(u2, opcode >> 5)) {
0b00 => logicalLeft(&dummy, value, shift_amt),
0b01 => logicalRight(&dummy, value, shift_amt),
0b10 => arithmetic_right(&dummy, value, shift_amt),
0b11 => rotateRight(&dummy, value, shift_amt),
};
}
} }
pub fn logicalLeft(cpsr: *CPSR, rm: u32, shift_byte: u8) u32 { pub fn logicalLeft(comptime S: bool, cpsr: *CPSR, rm: u32, amount: u8) u32 {
const shift_amt = @truncate(u5, shift_byte); const shift_amt = @truncate(u5, amount);
const bit_count: u8 = @typeInfo(u32).Int.bits; const bit_count: u8 = @typeInfo(u32).Int.bits;
var result: u32 = 0x0000_0000; var result: u32 = 0x0000_0000;
if (shift_byte < bit_count) { if (amount < bit_count) {
// We can perform a well-defined shift here // We can perform a well-defined shift here
// FIXME: We assume cpu.r[rs] == 0 and imm_shift == 0 are equivalent // FIXME: We assume cpu.r[rs] == 0 and imm_shift == 0 are equivalent
if (shift_amt != 0) { if (S and shift_amt != 0) {
const carry_bit = @truncate(u5, bit_count - shift_amt); const carry_bit = @truncate(u5, bit_count - shift_amt);
cpsr.c.write(rm >> carry_bit & 1 == 1); cpsr.c.write(rm >> carry_bit & 1 == 1);
} }
result = rm << shift_amt; result = rm << shift_amt;
} else if (shift_byte == bit_count) { } else if (amount == bit_count) {
// Shifted all bits out, carry bit is bit 0 of rm // Shifted all bits out, carry bit is bit 0 of rm
cpsr.c.write(rm & 1 == 1); if (S) cpsr.c.write(rm & 1 == 1);
} else { } else {
// Shifted all bits out, carry bit has also been shifted out // Shifted all bits out, carry bit has also been shifted out
cpsr.c.write(false); if (S) cpsr.c.write(false);
} }
return result; return result;
} }
pub fn logicalRight(cpsr: *CPSR, rm: u32, shift_byte: u8) u32 { pub fn logicalRight(comptime S: bool, cpsr: *CPSR, rm: u32, amount: u32) u32 {
const shift_amt = @truncate(u5, shift_byte); const shift_amt = @truncate(u5, amount);
const bit_count: u8 = @typeInfo(u32).Int.bits; const bit_count: u8 = @typeInfo(u32).Int.bits;
var result: u32 = 0x0000_0000; var result: u32 = 0x0000_0000;
if (shift_byte == 0 or shift_byte == bit_count) { if (amount == 0 or amount == bit_count) {
// Actualy LSR #32 // Actualy LSR #32
cpsr.c.write(rm >> 31 & 1 == 1); if (S) cpsr.c.write(rm >> 31 & 1 == 1);
} else if (shift_byte < bit_count) { } else if (amount < bit_count) {
// We can perform a well-defined shift // We can perform a well-defined shift
const carry_bit = shift_amt - 1; const carry_bit = shift_amt - 1;
cpsr.c.write(rm >> carry_bit & 1 == 1); if (S) cpsr.c.write(rm >> carry_bit & 1 == 1);
result = rm >> shift_amt; result = rm >> shift_amt;
} else { } else {
// All bits have been shifted out, including carry bit // All bits have been shifted out, including carry bit
cpsr.c.write(false); if (S) cpsr.c.write(false);
} }
return result; return result;
} }
pub fn arithmetic_right(_: *CPSR, _: u32, _: u8) u32 { pub fn arithmeticRight(comptime _: bool, _: *CPSR, _: u32, _: u8) u32 {
// @bitCast(u32, @bitCast(i32, r_val) >> @truncate(u5, amount)) // @bitCast(u32, @bitCast(i32, r_val) >> @truncate(u5, amount))
std.debug.panic("[BarrelShifter] implement arithmetic shift right", .{}); std.debug.panic("[BarrelShifter] implement arithmetic shift right", .{});
} }
pub fn rotateRight(cpsr: *CPSR, rm: u32, shift_byte: u8) u32 { pub fn rotateRight(comptime S: bool, cpsr: *CPSR, rm: u32, amount: u8) u32 {
const result = std.math.rotr(u32, rm, shift_byte); const result = std.math.rotr(u32, rm, amount);
if (result != 0) { if (S and result != 0) {
cpsr.c.write(result >> 31 & 1 == 1); cpsr.c.write(result >> 31 & 1 == 1);
} }

View File

@ -22,18 +22,8 @@ pub fn dataProcessing(comptime I: bool, comptime S: bool, comptime instrKind: u4
var op2: u32 = undefined; var op2: u32 = undefined;
if (I) { if (I) {
const amt = @truncate(u8, (opcode >> 8 & 0xF) << 1); const amount = @truncate(u8, (opcode >> 8 & 0xF) << 1);
op2 = BarrelShifter.rotateRight(S, &cpu.cpsr, opcode & 0xFF, amount);
if (S) {
op2 = BarrelShifter.rotateRight(&cpu.cpsr, opcode & 0xFF, amt);
} else {
const PSR = @import("../../cpu.zig").PSR;
var dummy = PSR{ .raw = 0x0000_0000 };
op2 = BarrelShifter.rotateRight(&dummy, opcode & 0xFF, amt);
}
// op2 = std.math.rotr(u32, opcode & 0xFF, (opcode >> 8 & 0xF) << 1);
} else { } else {
op2 = BarrelShifter.exec(S, cpu, opcode); op2 = BarrelShifter.exec(S, cpu, opcode);
} }

View File

@ -4,7 +4,6 @@ const util = @import("../../util.zig");
const BarrelShifter = @import("barrel_shifter.zig"); const BarrelShifter = @import("barrel_shifter.zig");
const Bus = @import("../../Bus.zig"); const Bus = @import("../../Bus.zig");
const Arm7tdmi = @import("../../cpu.zig").Arm7tdmi; const Arm7tdmi = @import("../../cpu.zig").Arm7tdmi;
const CPSR = @import("../../cpu.zig").PSR;
const InstrFn = @import("../../cpu.zig").ArmInstrFn; const InstrFn = @import("../../cpu.zig").ArmInstrFn;
pub fn singleDataTransfer(comptime I: bool, comptime P: bool, comptime U: bool, comptime B: bool, comptime W: bool, comptime L: bool) InstrFn { pub fn singleDataTransfer(comptime I: bool, comptime P: bool, comptime U: bool, comptime B: bool, comptime W: bool, comptime L: bool) InstrFn {
@ -55,16 +54,13 @@ pub fn singleDataTransfer(comptime I: bool, comptime P: bool, comptime U: bool,
} }
fn registerOffset(cpu: *Arm7tdmi, opcode: u32) u32 { fn registerOffset(cpu: *Arm7tdmi, opcode: u32) u32 {
const shift_byte = @truncate(u8, opcode >> 7 & 0x1F); const amount = @truncate(u8, opcode >> 7 & 0x1F);
const rm = cpu.r[opcode & 0xF]; const rm = cpu.r[opcode & 0xF];
var dummy = CPSR{ .raw = 0x0000_0000 };
return switch (@truncate(u2, opcode >> 5)) { return switch (@truncate(u2, opcode >> 5)) {
0b00 => BarrelShifter.logicalLeft(&dummy, rm, shift_byte), 0b00 => BarrelShifter.logicalLeft(false, &cpu.cpsr, rm, amount),
0b01 => BarrelShifter.logicalRight(&dummy, rm, shift_byte), 0b01 => BarrelShifter.logicalRight(false, &cpu.cpsr, rm, amount),
0b10 => BarrelShifter.arithmetic_right(&dummy, rm, shift_byte), 0b10 => BarrelShifter.arithmeticRight(false, &cpu.cpsr, rm, amount),
0b11 => BarrelShifter.rotateRight(&dummy, rm, shift_byte), 0b11 => BarrelShifter.rotateRight(false, &cpu.cpsr, rm, amount),
}; };
} }