#[repr(u8)]
#[derive(Clone, Copy, Debug)]
pub enum Interrupt {
    // CPU exceptions 0-31
    InvalidInterrupt = 0,
    PageFault = 1,
    ProtectionFault = 2,
    InvalidOpcode = 3,
    DivideByZero = 4,
    StackOverflow = 5,
    WriteToReadOnly = 6,
    ReadFromWriteOnly = 7,
    UnmappedIo = 8,
    // 8-31 reserved for future CPU exceptions

    // IRQ's (32-63)
    Hardware(u8),
    SerialIn = 32,

    // Syscalls (64-255)
    // OS defined, program uses INT 64-127
    Software(u8) = 128,
}

impl Interrupt {
    pub fn code(&self) -> u8 {
        match self {
            Interrupt::InvalidInterrupt => 0,
            Interrupt::PageFault => 1,
            Interrupt::ProtectionFault => 2,
            Interrupt::InvalidOpcode => 3,
            Interrupt::DivideByZero => 4,
            Interrupt::StackOverflow => 5,
            Interrupt::WriteToReadOnly => 6,
            Interrupt::ReadFromWriteOnly => 7,
            Interrupt::UnmappedIo => 8,
            Interrupt::SerialIn => 32,
            Interrupt::Hardware(x) => {
                if *x < 32 || *x > 63 {
                    0
                } else {
                    *x
                }
            }
            Interrupt::Software(x) => {
                if *x < 64 {
                    0
                } else {
                    *x
                }
            }
        }
    }
}
