fix: reimpl handleInterrupt code
This commit is contained in:
		@@ -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
 | 
				
			||||||
            self.bus.io.haltcnt = .Execute;
 | 
					        if (!self.bus.io.ime or self.cpsr.i.read() or should_handle == 0) return;
 | 
				
			||||||
            // log.debug("An Interrupt was Fired!", .{});
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
            // Either IME is not true or I in CPSR is true
 | 
					        // If pipeline isn't full, return but reschedule the handling of the event
 | 
				
			||||||
            // Don't handle interrupts
 | 
					        if (!self.pipe.isFull()) return;
 | 
				
			||||||
            if (!self.bus.io.ime or self.cpsr.i.read()) return;
 | 
					 | 
				
			||||||
            // log.debug("An interrupt was Handled!", .{});
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
            // FIXME: Is the return address ahead?
 | 
					        // log.debug("Handling Interrupt!", .{});
 | 
				
			||||||
            const r15 = self.r[15];
 | 
					        self.bus.io.haltcnt = .Execute;
 | 
				
			||||||
            const cpsr = self.cpsr.raw;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
            self.changeMode(.Irq);
 | 
					        const ret_addr = self.r[15] - if (self.cpsr.t.read()) 2 else @as(u32, 4);
 | 
				
			||||||
            self.cpsr.t.write(false);
 | 
					        const new_spsr = self.cpsr.raw;
 | 
				
			||||||
            self.cpsr.i.write(true);
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
            self.r[14] = r15;
 | 
					        self.changeMode(.Irq);
 | 
				
			||||||
            self.spsr.raw = cpsr;
 | 
					        self.cpsr.t.write(false);
 | 
				
			||||||
            self.r[15] = 0x000_0018;
 | 
					        self.cpsr.i.write(true);
 | 
				
			||||||
        }
 | 
					
 | 
				
			||||||
 | 
					        self.r[14] = ret_addr;
 | 
				
			||||||
 | 
					        self.spsr.raw = new_spsr;
 | 
				
			||||||
 | 
					        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);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user