1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
use crate::error::Error;


pub struct InterruptController {
    interrupts: Vec<(bool, u8)>,
    highest: u8,
}

impl Default for InterruptController {
    fn default() -> InterruptController {
        InterruptController {
            interrupts: vec![(false, 0); 7],
            highest: 0,
        }
    }
}

impl InterruptController {
    pub fn set(&mut self, state: bool, priority: u8, number: u8) -> Result<(), Error> {
        self.interrupts[priority as usize].0 = state;
        self.interrupts[priority as usize].1 = number;
        if state && priority > self.highest {
            self.highest = priority;
        }
        Ok(())
    }

    pub fn check(&mut self) -> (bool, u8, u8) {
        if self.highest > 0 {
            (true, self.highest, self.interrupts[self.highest as usize].1)
        } else {
            (false, 0, 0)
        }
    }

    pub fn acknowledge(&mut self, priority: u8) -> Result<u8, Error> {
        let acknowledge = self.interrupts[priority as usize].1;
        self.interrupts[priority as usize].0 = false;
        while self.highest > 0 && !self.interrupts[self.highest as usize].0 {
            self.highest -= 1;
        }
        Ok(acknowledge)
    }
}