summaryrefslogtreecommitdiff
path: root/src/devices/system_device.rs
blob: 10ddad1408d689caaa2ac75891b81a5f6e869e84 (plain) (blame)
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
use crate::*;


pub struct SystemDevice {
    /// Name and version of this system.
    pub name: ReadBuffer,
    /// Authors of this system.
    pub authors: ReadBuffer,
    /// Mask of all devices waiting to wake from sleep.
    pub wakers: u16,
    /// Device that most recently woke the system.
    pub waker: u8,
    /// True if the system has been put to sleep.
    pub asleep: bool,
    /// Mask of all available devices.
    pub devices: u16,
    /// Name of the first custom devices.
    pub custom1: ReadBuffer,
    /// Name of the second custom devices.
    pub custom2: ReadBuffer,
    /// Name of the third custom devices.
    pub custom3: ReadBuffer,
    /// Name of the fourth custom devices.
    pub custom4: ReadBuffer,
}


impl Device for SystemDevice {
    fn read(&mut self, port: u8) -> u8 {
        match port {
            0x0 => 0x00,
            0x1 => 0x00,
            0x2 => self.waker,
            0x3 => 0x00,
            0x4 => 0x00,
            0x5 => 0x00,
            0x6 => 0x00,
            0x7 => 0x00,
            0x8 => self.name.read(),
            0x9 => self.authors.read(),
            0xa => 0x00,
            0xb => 0x00,
            0xc => 0x00,
            0xd => 0x00,
            0xe => read_h!(self.devices),
            0xf => read_l!(self.devices),
            _ => unreachable!(),
        }
    }

    fn write(&mut self, port: u8, value: u8) -> Option<Signal> {
        match port {
            0x0 =>   write_h!(self.wakers, value),
            0x1 => { write_l!(self.wakers, value);
                     self.asleep = true;
                     return Some(Signal::Sleep); },
            0x2 => (),
            0x3 => return Some(Signal::Fork),
            0x4 => (),
            0x5 => (),
            0x6 => (),
            0x7 => (),
            0x8 => self.name.pointer = 0,
            0x9 => self.authors.pointer = 0,
            0xa => (),
            0xb => (),
            0xc => (),
            0xd => (),
            0xe => (),
            0xf => (),
            _ => unreachable!(),
        };
        return None;
    }

    fn wake(&mut self) -> bool {
        false
    }
}


impl SystemDevice {
    pub fn new(devices: u16) -> Self {
        Self {
            name: get_name(),
            authors: get_authors(),
            wakers: 0,
            waker: 0,
            asleep: false,
            devices,
            custom1: ReadBuffer::new(),
            custom2: ReadBuffer::new(),
            custom3: ReadBuffer::new(),
            custom4: ReadBuffer::new(),
        }
    }
}


fn get_name() -> ReadBuffer {
    let pkg_version = env!("CARGO_PKG_VERSION");
    let pkg_name = env!("CARGO_PKG_NAME");
    ReadBuffer::from_str(&format!("{pkg_name}/{pkg_version}"))
}

fn get_authors() -> ReadBuffer {
    let pkg_authors = env!("CARGO_PKG_AUTHORS");
    let mut authors_string = String::new();
    for author in pkg_authors.split(':') {
        authors_string.push_str(author);
        authors_string.push('\n');
    }
    ReadBuffer::from_str(&authors_string)
}