2021-03-07 04:58:59 +11:00
|
|
|
use crate::memory_mapped::MemoryMapped;
|
|
|
|
|
2021-03-07 12:06:34 +11:00
|
|
|
pub enum Interrupt {
|
2021-03-08 13:06:45 +11:00
|
|
|
VBlank = 0,
|
|
|
|
HBlank = 1,
|
|
|
|
VCounter = 2,
|
|
|
|
Timer0 = 3,
|
|
|
|
Timer1 = 4,
|
|
|
|
Timer2 = 5,
|
|
|
|
Timer3 = 6,
|
|
|
|
Serial = 7,
|
|
|
|
Dma0 = 8,
|
|
|
|
Dma1 = 9,
|
|
|
|
Dma2 = 10,
|
|
|
|
Dma3 = 11,
|
|
|
|
Keypad = 12,
|
|
|
|
Gamepak = 13,
|
2021-03-07 04:58:59 +11:00
|
|
|
}
|
|
|
|
|
2021-03-07 07:07:04 +11:00
|
|
|
const ENABLED_INTERRUPTS: MemoryMapped<u16> = unsafe { MemoryMapped::new(0x04000200) };
|
|
|
|
const INTERRUPTS_ENABLED: MemoryMapped<u16> = unsafe { MemoryMapped::new(0x04000208) };
|
2021-03-07 04:58:59 +11:00
|
|
|
|
2021-03-07 12:06:34 +11:00
|
|
|
pub fn enable(interrupt: Interrupt) {
|
2021-03-07 04:58:59 +11:00
|
|
|
let _interrupt_token = temporary_interrupt_disable();
|
|
|
|
let interrupt = interrupt as usize;
|
|
|
|
let enabled = ENABLED_INTERRUPTS.get() | (1 << (interrupt as u16));
|
|
|
|
ENABLED_INTERRUPTS.set(enabled);
|
|
|
|
}
|
|
|
|
|
2021-03-07 12:06:34 +11:00
|
|
|
pub fn disable(interrupt: Interrupt) {
|
2021-03-07 04:58:59 +11:00
|
|
|
let _interrupt_token = temporary_interrupt_disable();
|
|
|
|
let interrupt = interrupt as usize;
|
|
|
|
let enabled = ENABLED_INTERRUPTS.get() & !(1 << (interrupt as u16));
|
|
|
|
ENABLED_INTERRUPTS.set(enabled);
|
|
|
|
}
|
|
|
|
|
2021-03-07 12:06:34 +11:00
|
|
|
pub struct Disable {}
|
2021-03-07 04:58:59 +11:00
|
|
|
|
|
|
|
impl Drop for Disable {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
enable_interrupts();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-07 12:06:34 +11:00
|
|
|
pub fn temporary_interrupt_disable() -> Disable {
|
2021-03-07 04:58:59 +11:00
|
|
|
disable_interrupts();
|
|
|
|
Disable {}
|
|
|
|
}
|
|
|
|
|
2021-03-07 12:06:34 +11:00
|
|
|
pub fn enable_interrupts() {
|
2021-03-07 04:58:59 +11:00
|
|
|
INTERRUPTS_ENABLED.set(1);
|
|
|
|
}
|
|
|
|
|
2021-03-07 12:05:41 +11:00
|
|
|
pub(crate) fn disable_interrupts() {
|
2021-03-07 04:58:59 +11:00
|
|
|
INTERRUPTS_ENABLED.set(0);
|
|
|
|
}
|