chore: modify type signature of util.sext
This commit is contained in:
parent
f130d1991c
commit
ff3f79801c
|
@ -10,7 +10,7 @@ pub fn branch(comptime L: bool) InstrFn {
|
||||||
return struct {
|
return struct {
|
||||||
fn inner(cpu: *Arm7tdmi, _: *Bus, opcode: u32) void {
|
fn inner(cpu: *Arm7tdmi, _: *Bus, opcode: u32) void {
|
||||||
if (L) cpu.r[14] = cpu.r[15];
|
if (L) cpu.r[14] = cpu.r[15];
|
||||||
cpu.r[15] = cpu.fakePC() +% (sext(24, opcode) << 2);
|
cpu.r[15] = cpu.fakePC() +% (sext(u32, u24, opcode) << 2);
|
||||||
}
|
}
|
||||||
}.inner;
|
}.inner;
|
||||||
}
|
}
|
||||||
|
|
|
@ -43,14 +43,14 @@ pub fn halfAndSignedDataTransfer(comptime P: bool, comptime U: bool, comptime I:
|
||||||
},
|
},
|
||||||
0b10 => {
|
0b10 => {
|
||||||
// LDRSB
|
// LDRSB
|
||||||
result = sext(8, bus.read(u8, address));
|
result = sext(u32, u8, bus.read(u8, address));
|
||||||
},
|
},
|
||||||
0b11 => {
|
0b11 => {
|
||||||
// LDRSH
|
// LDRSH
|
||||||
result = if (address & 1 == 1) blk: {
|
result = if (address & 1 == 1) blk: {
|
||||||
break :blk sext(8, bus.read(u8, address));
|
break :blk sext(u32, u8, bus.read(u8, address));
|
||||||
} else blk: {
|
} else blk: {
|
||||||
break :blk sext(16, bus.read(u16, address));
|
break :blk sext(u32, u16, bus.read(u16, address));
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
0b00 => unreachable, // SWP
|
0b00 => unreachable, // SWP
|
||||||
|
|
|
@ -9,7 +9,7 @@ pub fn format16(comptime cond: u4) InstrFn {
|
||||||
return struct {
|
return struct {
|
||||||
fn inner(cpu: *Arm7tdmi, _: *Bus, opcode: u16) void {
|
fn inner(cpu: *Arm7tdmi, _: *Bus, opcode: u16) void {
|
||||||
// B
|
// B
|
||||||
const offset = sext(8, opcode & 0xFF) << 1;
|
const offset = sext(u32, u8, opcode & 0xFF) << 1;
|
||||||
|
|
||||||
const should_execute = switch (cond) {
|
const should_execute = switch (cond) {
|
||||||
0xE, 0xF => cpu.panic("[CPU/THUMB.16] Undefined conditional branch with condition {}", .{cond}),
|
0xE, 0xF => cpu.panic("[CPU/THUMB.16] Undefined conditional branch with condition {}", .{cond}),
|
||||||
|
@ -27,7 +27,7 @@ pub fn format18() InstrFn {
|
||||||
return struct {
|
return struct {
|
||||||
// B but conditional
|
// B but conditional
|
||||||
fn inner(cpu: *Arm7tdmi, _: *Bus, opcode: u16) void {
|
fn inner(cpu: *Arm7tdmi, _: *Bus, opcode: u16) void {
|
||||||
const offset = sext(11, opcode & 0x7FF) << 1;
|
const offset = sext(u32, u11, opcode & 0x7FF) << 1;
|
||||||
cpu.r[15] = (cpu.r[15] + 2) +% offset;
|
cpu.r[15] = (cpu.r[15] + 2) +% offset;
|
||||||
}
|
}
|
||||||
}.inner;
|
}.inner;
|
||||||
|
@ -47,7 +47,7 @@ pub fn format19(comptime is_low: bool) InstrFn {
|
||||||
cpu.r[14] = old_pc | 1;
|
cpu.r[14] = old_pc | 1;
|
||||||
} else {
|
} else {
|
||||||
// Instruction 1
|
// Instruction 1
|
||||||
cpu.r[14] = (cpu.r[15] + 2) +% (sext(11, offset) << 12);
|
cpu.r[14] = (cpu.r[15] + 2) +% (sext(u32, u11, offset) << 12);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}.inner;
|
}.inner;
|
||||||
|
|
|
@ -36,7 +36,7 @@ pub fn format78(comptime op: u2, comptime T: bool) InstrFn {
|
||||||
},
|
},
|
||||||
0b01 => {
|
0b01 => {
|
||||||
// LDSB
|
// LDSB
|
||||||
cpu.r[rd] = sext(8, bus.read(u8, address));
|
cpu.r[rd] = sext(u32, u8, bus.read(u8, address));
|
||||||
},
|
},
|
||||||
0b10 => {
|
0b10 => {
|
||||||
// LDRH
|
// LDRH
|
||||||
|
@ -46,9 +46,9 @@ pub fn format78(comptime op: u2, comptime T: bool) InstrFn {
|
||||||
0b11 => {
|
0b11 => {
|
||||||
// LDRSH
|
// LDRSH
|
||||||
cpu.r[rd] = if (address & 1 == 1) blk: {
|
cpu.r[rd] = if (address & 1 == 1) blk: {
|
||||||
break :blk sext(8, bus.read(u8, address));
|
break :blk sext(u32, u8, bus.read(u8, address));
|
||||||
} else blk: {
|
} else blk: {
|
||||||
break :blk sext(16, bus.read(u16, address));
|
break :blk sext(u32, u16, bus.read(u16, address));
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
13
src/util.zig
13
src/util.zig
|
@ -1,11 +1,16 @@
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const Log2Int = std.math.Log2Int;
|
const Log2Int = std.math.Log2Int;
|
||||||
|
|
||||||
pub inline fn sext(comptime bits: comptime_int, value: u32) u32 {
|
// Sign-Extend value of type `T` to type `U`
|
||||||
comptime std.debug.assert(bits <= 32);
|
pub fn sext(comptime T: type, comptime U: type, value: T) T {
|
||||||
const amount = 32 - bits;
|
// U must have less bits than T
|
||||||
|
comptime std.debug.assert(@typeInfo(U).Int.bits <= @typeInfo(T).Int.bits);
|
||||||
|
|
||||||
return @bitCast(u32, @bitCast(i32, value << amount) >> amount);
|
const iT = std.meta.Int(.signed, @typeInfo(T).Int.bits);
|
||||||
|
const ExtU = if (@typeInfo(U).Int.signedness == .unsigned) T else iT;
|
||||||
|
const shift = @intCast(Log2Int(T), @typeInfo(T).Int.bits - @typeInfo(U).Int.bits);
|
||||||
|
|
||||||
|
return @bitCast(T, @bitCast(iT, @as(ExtU, @truncate(U, value)) << shift) >> shift);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// See https://godbolt.org/z/W3en9Eche
|
/// See https://godbolt.org/z/W3en9Eche
|
||||||
|
|
Loading…
Reference in New Issue