chore: give more descriptive panic messages when changing mode fails

This commit is contained in:
Rekai Nyangadzayi Musuka 2022-10-21 05:12:13 -03:00
parent c0db2a987b
commit dd9b20030a
1 changed files with 11 additions and 8 deletions

View File

@ -120,7 +120,7 @@ pub const Arm7tdmi = struct {
} }
pub inline fn hasSPSR(self: *const Self) bool { pub inline fn hasSPSR(self: *const Self) bool {
const mode = getMode(self.cpsr.mode.read()) orelse unreachable; const mode = getModeChecked(self, self.cpsr.mode.read());
return switch (mode) { return switch (mode) {
.System, .User => false, .System, .User => false,
else => true, else => true,
@ -128,7 +128,7 @@ pub const Arm7tdmi = struct {
} }
pub inline fn isPrivileged(self: *const Self) bool { pub inline fn isPrivileged(self: *const Self) bool {
const mode = getMode(self.cpsr.mode.read()) orelse unreachable; const mode = getModeChecked(self, self.cpsr.mode.read());
return switch (mode) { return switch (mode) {
.User => false, .User => false,
else => true, else => true,
@ -141,12 +141,11 @@ pub const Arm7tdmi = struct {
} }
fn changeModeFromIdx(self: *Self, next: u5) void { fn changeModeFromIdx(self: *Self, next: u5) void {
const mode = getMode(next) orelse unreachable; self.changeMode(getModeChecked(self, next));
self.changeMode(mode);
} }
pub fn setUserModeRegister(self: *Self, idx: usize, value: u32) void { pub fn setUserModeRegister(self: *Self, idx: usize, value: u32) void {
const current = getMode(self.cpsr.mode.read()) orelse unreachable; const current = getModeChecked(self, self.cpsr.mode.read());
if (idx < 8) { if (idx < 8) {
self.r[idx] = value; self.r[idx] = value;
@ -166,7 +165,7 @@ pub const Arm7tdmi = struct {
} }
pub fn getUserModeRegister(self: *Self, idx: usize) u32 { pub fn getUserModeRegister(self: *Self, idx: usize) u32 {
const current = getMode(self.cpsr.mode.read()) orelse unreachable; const current = getModeChecked(self, self.cpsr.mode.read());
var result: u32 = undefined; var result: u32 = undefined;
if (idx < 8) { if (idx < 8) {
@ -189,7 +188,7 @@ pub const Arm7tdmi = struct {
} }
pub fn changeMode(self: *Self, next: Mode) void { pub fn changeMode(self: *Self, next: Mode) void {
const now = getMode(self.cpsr.mode.read()) orelse unreachable; const now = getModeChecked(self, self.cpsr.mode.read());
// Bank R8 -> r12 // Bank R8 -> r12
var r: usize = 8; var r: usize = 8;
@ -650,10 +649,14 @@ const Mode = enum(u5) {
System = 0b11111, System = 0b11111,
}; };
pub fn getMode(bits: u5) ?Mode { fn getMode(bits: u5) ?Mode {
return std.meta.intToEnum(Mode, bits) catch null; return std.meta.intToEnum(Mode, bits) catch null;
} }
fn getModeChecked(cpu: *const Arm7tdmi, bits: u5) Mode {
return getMode(bits) orelse cpu.panic("[CPU|CPSR] 0b{b:0>5} is an invalid CPU mode", .{bits});
}
fn armUndefined(cpu: *Arm7tdmi, _: *Bus, opcode: u32) void { fn armUndefined(cpu: *Arm7tdmi, _: *Bus, opcode: u32) void {
const id = armIdx(opcode); const id = armIdx(opcode);
cpu.panic("[CPU:ARM] ID: 0x{X:0>3} 0x{X:0>8} is an illegal opcode", .{ id, opcode }); cpu.panic("[CPU:ARM] ID: 0x{X:0>3} 0x{X:0>8} is an illegal opcode", .{ id, opcode });