fix: reimpl handleInterrupt code

This commit is contained in:
Rekai Nyangadzayi Musuka 2022-07-13 05:10:58 -03:00
parent 7d76308ed0
commit ee4bb9929f
1 changed files with 20 additions and 17 deletions

View File

@ -491,27 +491,26 @@ pub const Arm7tdmi = struct {
pub fn handleInterrupt(self: *Self) void { pub fn handleInterrupt(self: *Self) void {
const should_handle = self.bus.io.ie.raw & self.bus.io.irq.raw; const should_handle = self.bus.io.ie.raw & self.bus.io.irq.raw;
if (should_handle != 0) { // Return if IME is disabled, CPSR I is set or there is nothing to handle
if (!self.bus.io.ime or self.cpsr.i.read() or should_handle == 0) return;
// If pipeline isn't full, return but reschedule the handling of the event
if (!self.pipe.isFull()) return;
// log.debug("Handling Interrupt!", .{});
self.bus.io.haltcnt = .Execute; self.bus.io.haltcnt = .Execute;
// log.debug("An Interrupt was Fired!", .{});
// Either IME is not true or I in CPSR is true const ret_addr = self.r[15] - if (self.cpsr.t.read()) 2 else @as(u32, 4);
// Don't handle interrupts const new_spsr = self.cpsr.raw;
if (!self.bus.io.ime or self.cpsr.i.read()) return;
// log.debug("An interrupt was Handled!", .{});
// FIXME: Is the return address ahead?
const r15 = self.r[15];
const cpsr = self.cpsr.raw;
self.changeMode(.Irq); self.changeMode(.Irq);
self.cpsr.t.write(false); self.cpsr.t.write(false);
self.cpsr.i.write(true); self.cpsr.i.write(true);
self.r[14] = r15; self.r[14] = ret_addr;
self.spsr.raw = cpsr; self.spsr.raw = new_spsr;
self.r[15] = 0x000_0018; self.r[15] = 0x0000_0018;
} self.pipe.flush();
} }
inline fn fetch(self: *Self, comptime T: type) T { inline fn fetch(self: *Self, comptime T: type) T {
@ -671,6 +670,10 @@ const Pipline = struct {
self.flushed = true; self.flushed = true;
} }
pub fn isFull(self: *const Self) bool {
return self.stage[0] != null and self.stage[1] != null;
}
pub fn step(self: *Self, cpu: *Arm7tdmi, comptime T: type) ?u32 { pub fn step(self: *Self, cpu: *Arm7tdmi, comptime T: type) ?u32 {
comptime std.debug.assert(T == u32 or T == u16); comptime std.debug.assert(T == u32 or T == u16);