chore: replace unnecessarily complex sign extension implementation

This commit is contained in:
2022-10-21 05:12:24 -03:00
parent 9455ffe837
commit 68b0601a42
6 changed files with 19 additions and 41 deletions

View File

@@ -4,13 +4,13 @@ const Bus = @import("../../Bus.zig");
const Arm7tdmi = @import("../../cpu.zig").Arm7tdmi;
const InstrFn = @import("../../cpu.zig").ArmInstrFn;
const u32SignExtend = @import("../../util.zig").u32SignExtend;
const sext = @import("../../util.zig").sext;
pub fn branch(comptime L: bool) InstrFn {
return struct {
fn inner(cpu: *Arm7tdmi, _: *Bus, opcode: u32) void {
if (L) cpu.r[14] = cpu.r[15];
cpu.r[15] = cpu.fakePC() +% u32SignExtend(24, opcode << 2);
cpu.r[15] = cpu.fakePC() +% sext(24, opcode << 2);
}
}.inner;
}

View File

@@ -4,7 +4,7 @@ const Bus = @import("../../Bus.zig");
const Arm7tdmi = @import("../../cpu.zig").Arm7tdmi;
const InstrFn = @import("../../cpu.zig").ArmInstrFn;
const u32SignExtend = @import("../../util.zig").u32SignExtend;
const sext = @import("../../util.zig").sext;
pub fn halfAndSignedDataTransfer(comptime P: bool, comptime U: bool, comptime I: bool, comptime W: bool, comptime L: bool) InstrFn {
return struct {
@@ -42,14 +42,14 @@ pub fn halfAndSignedDataTransfer(comptime P: bool, comptime U: bool, comptime I:
},
0b10 => {
// LDRSB
result = u32SignExtend(8, bus.read8(address));
result = sext(8, bus.read8(address));
},
0b11 => {
// LDRSH
const value = if (address & 1 == 1) blk: {
break :blk u32SignExtend(8, bus.read8(address));
break :blk sext(8, bus.read8(address));
} else blk: {
break :blk u32SignExtend(16, bus.read16(address));
break :blk sext(16, bus.read16(address));
};
result = std.math.rotr(u32, value, 8 * (address & 1));

View File

@@ -3,13 +3,13 @@ const Arm7tdmi = @import("../../cpu.zig").Arm7tdmi;
const InstrFn = @import("../../cpu.zig").ThumbInstrFn;
const checkCond = @import("../../cpu.zig").checkCond;
const u32SignExtend = @import("../../util.zig").u32SignExtend;
const sext = @import("../../util.zig").sext;
pub fn format16(comptime cond: u4) InstrFn {
return struct {
fn inner(cpu: *Arm7tdmi, _: *Bus, opcode: u16) void {
// B
const offset = u32SignExtend(8, opcode & 0xFF) << 1;
const offset = sext(8, opcode & 0xFF) << 1;
const should_execute = switch (cond) {
0xE, 0xF => cpu.panic("[CPU/THUMB.16] Undefined conditional branch with condition {}", .{cond}),
@@ -27,7 +27,7 @@ pub fn format18() InstrFn {
return struct {
// B but conditional
fn inner(cpu: *Arm7tdmi, _: *Bus, opcode: u16) void {
const offset = u32SignExtend(11, opcode & 0x7FF) << 1;
const offset = sext(11, opcode & 0x7FF) << 1;
cpu.r[15] = (cpu.r[15] + 2) +% offset;
}
}.inner;
@@ -47,7 +47,7 @@ pub fn format19(comptime is_low: bool) InstrFn {
cpu.r[14] = old_pc | 1;
} else {
// Instruction 1
cpu.r[14] = (cpu.r[15] + 2) +% (u32SignExtend(11, offset) << 12);
cpu.r[14] = (cpu.r[15] + 2) +% (sext(11, offset) << 12);
}
}
}.inner;

View File

@@ -14,7 +14,7 @@ pub fn format6(comptime rd: u3) InstrFn {
}.inner;
}
const u32SignExtend = @import("../../util.zig").u32SignExtend;
const sext = @import("../../util.zig").sext;
pub fn format78(comptime op: u2, comptime T: bool) InstrFn {
return struct {
@@ -34,7 +34,7 @@ pub fn format78(comptime op: u2, comptime T: bool) InstrFn {
},
0b01 => {
// LDSB
cpu.r[rd] = u32SignExtend(8, bus.read8(address));
cpu.r[rd] = sext(8, bus.read8(address));
},
0b10 => {
// LDRH
@@ -44,9 +44,9 @@ pub fn format78(comptime op: u2, comptime T: bool) InstrFn {
0b11 => {
// LDRSH
const value = if (address & 1 == 1) blk: {
break :blk u32SignExtend(8, bus.read8(address));
break :blk sext(8, bus.read8(address));
} else blk: {
break :blk u32SignExtend(16, bus.read16(address));
break :blk sext(16, bus.read16(address));
};
cpu.r[rd] = std.math.rotr(u32, value, 8 * (address & 1));