Duckstation/src/core/interrupt_controller.cpp

120 lines
3.3 KiB
C++
Raw Normal View History

// SPDX-FileCopyrightText: 2019-2024 Connor McLaughlin <stenzek@gmail.com>
// SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0)
2019-09-17 06:26:00 +00:00
#include "interrupt_controller.h"
#include "cpu_core.h"
#include "util/state_wrapper.h"
#include "common/log.h"
2019-09-17 06:26:00 +00:00
Log_SetChannel(InterruptController);
namespace InterruptController {
JIT optimizations and refactoring (#675) * CPU/Recompiler: Use rel32 call where possible for no-args * JitCodeBuffer: Support using preallocated buffer * CPU/Recompiler/AArch64: Use bl instead of blr for short branches * CPU/CodeCache: Allocate recompiler buffer in program space This means we don't need 64-bit moves for every call out of the recompiler. * GTE: Don't store as u16 and load as u32 * CPU/Recompiler: Add methods to emit global load/stores * GTE: Convert class to namespace * CPU/Recompiler: Call GTE functions directly * Settings: Turn into a global variable * GPU: Replace local pointers with global * InterruptController: Turn into a global pointer * System: Replace local pointers with global * Timers: Turn into a global instance * DMA: Turn into a global instance * SPU: Turn into a global instance * CDROM: Turn into a global instance * MDEC: Turn into a global instance * Pad: Turn into a global instance * SIO: Turn into a global instance * CDROM: Move audio FIFO to the heap * CPU/Recompiler: Drop ASMFunctions No longer needed since we have code in the same 4GB window. * CPUCodeCache: Turn class into namespace * Bus: Local pointer -> global pointers * CPU: Turn class into namespace * Bus: Turn into namespace * GTE: Store registers in CPU state struct Allows relative addressing on ARM. * CPU/Recompiler: Align code storage to page size * CPU/Recompiler: Fix relative branches on A64 * HostInterface: Local references to global * System: Turn into a namespace, move events out * Add guard pages * Android: Fix build
2020-07-31 07:09:18 +00:00
static constexpr u32 REGISTER_WRITE_MASK = (u32(1) << NUM_IRQS) - 1;
static constexpr u32 DEFAULT_INTERRUPT_MASK = 0;
2019-09-17 06:26:00 +00:00
static void UpdateCPUInterruptRequest();
static u32 s_interrupt_status_register = 0;
static u32 s_interrupt_mask_register = DEFAULT_INTERRUPT_MASK;
static u32 s_interrupt_line_state = 0;
[[maybe_unused]] static constexpr std::array<const char*, static_cast<size_t>(IRQ::MaxCount)> s_irq_names = {
{"VBLANK", "GPU", "CDROM", "DMA", "TMR0", "TMR1", "TMR2", "PAD", "SIO", "SPU", "IRQ10"}};
2019-09-17 06:26:00 +00:00
} // namespace InterruptController
JIT optimizations and refactoring (#675) * CPU/Recompiler: Use rel32 call where possible for no-args * JitCodeBuffer: Support using preallocated buffer * CPU/Recompiler/AArch64: Use bl instead of blr for short branches * CPU/CodeCache: Allocate recompiler buffer in program space This means we don't need 64-bit moves for every call out of the recompiler. * GTE: Don't store as u16 and load as u32 * CPU/Recompiler: Add methods to emit global load/stores * GTE: Convert class to namespace * CPU/Recompiler: Call GTE functions directly * Settings: Turn into a global variable * GPU: Replace local pointers with global * InterruptController: Turn into a global pointer * System: Replace local pointers with global * Timers: Turn into a global instance * DMA: Turn into a global instance * SPU: Turn into a global instance * CDROM: Turn into a global instance * MDEC: Turn into a global instance * Pad: Turn into a global instance * SIO: Turn into a global instance * CDROM: Move audio FIFO to the heap * CPU/Recompiler: Drop ASMFunctions No longer needed since we have code in the same 4GB window. * CPUCodeCache: Turn class into namespace * Bus: Local pointer -> global pointers * CPU: Turn class into namespace * Bus: Turn into namespace * GTE: Store registers in CPU state struct Allows relative addressing on ARM. * CPU/Recompiler: Align code storage to page size * CPU/Recompiler: Fix relative branches on A64 * HostInterface: Local references to global * System: Turn into a namespace, move events out * Add guard pages * Android: Fix build
2020-07-31 07:09:18 +00:00
2019-09-17 06:26:00 +00:00
void InterruptController::Reset()
{
s_interrupt_status_register = 0;
s_interrupt_mask_register = DEFAULT_INTERRUPT_MASK;
s_interrupt_line_state = 0;
2019-09-17 06:26:00 +00:00
}
bool InterruptController::DoState(StateWrapper& sw)
{
sw.Do(&s_interrupt_status_register);
sw.Do(&s_interrupt_mask_register);
sw.DoEx(&s_interrupt_line_state, 63, s_interrupt_status_register);
2019-09-17 06:26:00 +00:00
return !sw.HasError();
}
void InterruptController::SetLineState(IRQ irq, bool state)
2019-09-17 06:26:00 +00:00
{
// Interupts are edge-triggered, so only set the flag in the status register on a 0-1 transition.
const u32 bit = (1u << static_cast<u32>(irq));
const u32 prev_state = s_interrupt_line_state;
s_interrupt_line_state = (s_interrupt_line_state & ~bit) | (state ? bit : 0u);
if (s_interrupt_line_state == prev_state)
return;
#ifdef _DEBUG
if (!(prev_state & bit) && state)
2024-05-23 10:55:28 +00:00
DEBUG_LOG("{} IRQ triggered", s_irq_names[static_cast<size_t>(irq)]);
else if ((prev_state & bit) && !state)
2024-05-23 10:55:28 +00:00
DEBUG_LOG("{} IRQ line inactive", s_irq_names[static_cast<size_t>(irq)]);
#endif
s_interrupt_status_register |= (state ? (prev_state ^ s_interrupt_line_state) : 0u) & s_interrupt_line_state;
2019-09-17 06:26:00 +00:00
UpdateCPUInterruptRequest();
}
u32 InterruptController::ReadRegister(u32 offset)
{
switch (offset)
{
case 0x00: // I_STATUS
return s_interrupt_status_register;
2019-09-17 06:26:00 +00:00
case 0x04: // I_MASK
return s_interrupt_mask_register;
2019-09-17 06:26:00 +00:00
2024-05-23 10:55:28 +00:00
[[unlikely]] default:
ERROR_LOG("Invalid read at offset 0x{:08X}", offset);
2019-09-17 06:26:00 +00:00
return UINT32_C(0xFFFFFFFF);
}
}
void InterruptController::WriteRegister(u32 offset, u32 value)
{
switch (offset)
{
case 0x00: // I_STATUS
{
#ifdef _DEBUG
const u32 cleared_bits = (s_interrupt_status_register & ~value);
for (u32 i = 0; i < static_cast<u32>(IRQ::MaxCount); i++)
{
if (cleared_bits & (1u << i))
2024-05-23 10:55:28 +00:00
DEBUG_LOG("{} IRQ cleared", s_irq_names[i]);
}
#endif
s_interrupt_status_register = s_interrupt_status_register & (value & REGISTER_WRITE_MASK);
2019-09-17 06:26:00 +00:00
UpdateCPUInterruptRequest();
}
break;
case 0x04: // I_MASK
{
2024-05-23 10:55:28 +00:00
DEBUG_LOG("Interrupt mask <- 0x{:08X}", value);
s_interrupt_mask_register = value & REGISTER_WRITE_MASK;
UpdateCPUInterruptRequest();
2019-09-17 06:26:00 +00:00
}
break;
2024-05-23 10:55:28 +00:00
default:
[[unlikely]] ERROR_LOG("Invalid write at offset 0x{:08X}", offset);
2019-09-17 06:26:00 +00:00
break;
}
}
ALWAYS_INLINE_RELEASE void InterruptController::UpdateCPUInterruptRequest()
2019-09-17 06:26:00 +00:00
{
const bool state = (s_interrupt_status_register & s_interrupt_mask_register) != 0;
CPU::SetIRQRequest(state);
2019-09-17 06:26:00 +00:00
}