chore: drop *Bus argument from the InstrFn LUT

This commit is contained in:
2023-07-25 21:45:02 -05:00
parent f31c4bdb65
commit ba22b856ec
19 changed files with 100 additions and 119 deletions

View File

@@ -1,5 +1,3 @@
const Bus = @import("../../../lib.zig").Bus;
const rotr = @import("zba-util").rotr;
const sext = @import("zba-util").sext;
@@ -7,12 +5,12 @@ pub fn fmt6(comptime InstrFn: type, comptime rd: u3) InstrFn {
const Arm32 = @typeInfo(@typeInfo(InstrFn).Pointer.child).Fn.params[0].type.?;
return struct {
fn inner(cpu: Arm32, bus: Bus, opcode: u16) void {
fn inner(cpu: Arm32, opcode: u16) void {
// LDR
const offset = (opcode & 0xFF) << 2;
// Bit 1 of the PC intentionally ignored
cpu.r[rd] = bus.read(u32, (cpu.r[15] & ~@as(u32, 2)) + offset);
cpu.r[rd] = cpu.read(u32, (cpu.r[15] & ~@as(u32, 2)) + offset);
}
}.inner;
}
@@ -21,7 +19,7 @@ pub fn fmt78(comptime InstrFn: type, comptime op: u2, comptime T: bool) InstrFn
const Arm32 = @typeInfo(@typeInfo(InstrFn).Pointer.child).Fn.params[0].type.?;
return struct {
fn inner(cpu: Arm32, bus: Bus, opcode: u16) void {
fn inner(cpu: Arm32, opcode: u16) void {
const ro = opcode >> 6 & 0x7;
const rb = opcode >> 3 & 0x7;
const rd = opcode & 0x7;
@@ -35,20 +33,20 @@ pub fn fmt78(comptime InstrFn: type, comptime op: u2, comptime T: bool) InstrFn
// STRH
// FIXME: I shouldn't have to use @as(u8, ...) here
bus.write(u16, address, @as(u16, @truncate(cpu.r[rd])));
cpu.write(u16, address, @as(u16, @truncate(cpu.r[rd])));
},
0b01 => {
// LDSB
cpu.r[rd] = sext(u32, u8, bus.read(u8, address));
cpu.r[rd] = sext(u32, u8, cpu.read(u8, address));
},
0b10 => {
// LDRH
const value = bus.read(u16, address);
const value = cpu.read(u16, address);
cpu.r[rd] = rotr(u32, value, 8 * (address & 1));
},
0b11 => {
// LDRSH
const value = bus.read(u16, address);
const value = cpu.read(u16, address);
// FIXME: I shouldn't have to use @as(u8, ...) here
cpu.r[rd] = if (address & 1 == 1) sext(u32, u8, @as(u8, @truncate(value >> 8))) else sext(u32, u16, value);
@@ -59,22 +57,22 @@ pub fn fmt78(comptime InstrFn: type, comptime op: u2, comptime T: bool) InstrFn
switch (op) {
0b00 => {
// STR
bus.write(u32, address, cpu.r[rd]);
cpu.write(u32, address, cpu.r[rd]);
},
0b01 => {
// STRB
// FIXME: I shouldn't have to use @as(u8, ...) here
bus.write(u8, address, @as(u8, @truncate(cpu.r[rd])));
cpu.write(u8, address, @as(u8, @truncate(cpu.r[rd])));
},
0b10 => {
// LDR
const value = bus.read(u32, address);
const value = cpu.read(u32, address);
cpu.r[rd] = rotr(u32, value, 8 * (address & 0x3));
},
0b11 => {
// LDRB
cpu.r[rd] = bus.read(u8, address);
cpu.r[rd] = cpu.read(u8, address);
},
}
}
@@ -86,7 +84,7 @@ pub fn fmt9(comptime InstrFn: type, comptime B: bool, comptime L: bool, comptime
const Arm32 = @typeInfo(@typeInfo(InstrFn).Pointer.child).Fn.params[0].type.?;
return struct {
fn inner(cpu: Arm32, bus: Bus, opcode: u16) void {
fn inner(cpu: Arm32, opcode: u16) void {
const rb = opcode >> 3 & 0x7;
const rd = opcode & 0x7;
@@ -94,11 +92,11 @@ pub fn fmt9(comptime InstrFn: type, comptime B: bool, comptime L: bool, comptime
if (B) {
// LDRB
const address = cpu.r[rb] + offset;
cpu.r[rd] = bus.read(u8, address);
cpu.r[rd] = cpu.read(u8, address);
} else {
// LDR
const address = cpu.r[rb] + (@as(u32, offset) << 2);
const value = bus.read(u32, address);
const value = cpu.read(u32, address);
cpu.r[rd] = rotr(u32, value, 8 * (address & 0x3));
}
} else {
@@ -107,11 +105,11 @@ pub fn fmt9(comptime InstrFn: type, comptime B: bool, comptime L: bool, comptime
const address = cpu.r[rb] + offset;
// FIXME: I shouldn't have to use @as(u8, ...) here
bus.write(u8, address, @as(u8, @truncate(cpu.r[rd])));
cpu.write(u8, address, @as(u8, @truncate(cpu.r[rd])));
} else {
// STR
const address = cpu.r[rb] + (@as(u32, offset) << 2);
bus.write(u32, address, cpu.r[rd]);
cpu.write(u32, address, cpu.r[rd]);
}
}
}
@@ -122,7 +120,7 @@ pub fn fmt10(comptime InstrFn: type, comptime L: bool, comptime offset: u5) Inst
const Arm32 = @typeInfo(@typeInfo(InstrFn).Pointer.child).Fn.params[0].type.?;
return struct {
fn inner(cpu: Arm32, bus: Bus, opcode: u16) void {
fn inner(cpu: Arm32, opcode: u16) void {
const rb = opcode >> 3 & 0x7;
const rd = opcode & 0x7;
@@ -130,13 +128,13 @@ pub fn fmt10(comptime InstrFn: type, comptime L: bool, comptime offset: u5) Inst
if (L) {
// LDRH
const value = bus.read(u16, address);
const value = cpu.read(u16, address);
cpu.r[rd] = rotr(u32, value, 8 * (address & 1));
} else {
// STRH
// FIXME: I shouldn't have to use @as(u8, ...) here
bus.write(u16, address, @as(u16, @truncate(cpu.r[rd])));
cpu.write(u16, address, @as(u16, @truncate(cpu.r[rd])));
}
}
}.inner;
@@ -146,17 +144,17 @@ pub fn fmt11(comptime InstrFn: type, comptime L: bool, comptime rd: u3) InstrFn
const Arm32 = @typeInfo(@typeInfo(InstrFn).Pointer.child).Fn.params[0].type.?;
return struct {
fn inner(cpu: Arm32, bus: Bus, opcode: u16) void {
fn inner(cpu: Arm32, opcode: u16) void {
const offset = (opcode & 0xFF) << 2;
const address = cpu.r[13] + offset;
if (L) {
// LDR
const value = bus.read(u32, address);
const value = cpu.read(u32, address);
cpu.r[rd] = rotr(u32, value, 8 * (address & 0x3));
} else {
// STR
bus.write(u32, address, cpu.r[rd]);
cpu.write(u32, address, cpu.r[rd]);
}
}
}.inner;