-
Notifications
You must be signed in to change notification settings - Fork 2
/
sync_char.rs
78 lines (70 loc) · 2.19 KB
/
sync_char.rs
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
//! The synchronous character keyboard component offers a simple one-character reading system.
//! See [`SyncLineKeyboard`] for more details.
use lrvm::board::Bus;
use lrvm_tools::{
exceptions::AuxHwException,
metadata::{DeviceMetadata, KeyboardType},
};
/// The keyboard works with a 1-word buffer and a handler. When it receives a read request, the data is read from the buffer.
/// Writing into the buffer is forbidden but writing to the second word of the component results in it interpreting the provided action code:
///
/// * `0xAA`: trigger a synchronous input and put the result in the buffer
/// * `0xFF`: clear the buffer's content
///
/// The buffer is guaranteed to contain a valid UTF-8 character.
pub struct SyncCharKeyboard {
buffer: char,
handler: Box<dyn FnMut() -> char>,
hw_id: u64,
}
impl SyncCharKeyboard {
/// Create a synchronous character keyboard component.
pub fn new(handler: Box<dyn FnMut() -> char>, hw_id: u64) -> Self {
Self {
buffer: 0 as char,
handler,
hw_id,
}
}
}
impl Bus for SyncCharKeyboard {
fn name(&self) -> &'static str {
"Synchronous Character Keyboard"
}
fn metadata(&self) -> [u32; 8] {
DeviceMetadata::new(
self.hw_id,
8,
KeyboardType::ReadCharSynchronous.into(),
None,
None,
)
.encode()
}
fn read(&mut self, addr: u32, ex: &mut u16) -> u32 {
if addr == 0 {
self.buffer as u32
} else if addr == 4 {
*ex = AuxHwException::MemoryNotReadable.into();
0
} else {
unreachable!() // Safety guarantee
}
}
fn write(&mut self, addr: u32, word: u32, ex: &mut u16) {
if addr == 0 {
*ex = 0x31 << 8;
} else if addr == 4 {
match word {
0xAA => self.buffer = (self.handler)(),
0xFF => self.reset(),
code => *ex = AuxHwException::UnknownOperation(code as u8).into(),
}
} else {
unreachable!() // Safety guarantee
}
}
fn reset(&mut self) {
self.buffer = 0 as char;
}
}