lc3_ensemble/sim/device/
keyboard.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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
use std::collections::VecDeque;
use std::sync::{Arc, RwLock, RwLockWriteGuard, TryLockError};

use super::{DevWrapper, ExternalDevice, Interrupt, KBDR, KBSR, KB_INTP, KB_INTV};

/// Scaffolding needed to implement [`ExternalDevice`] for keyboard devices.
trait KeyboardDevice: Send + Sync + 'static {
    /// State of interrupt enabled.
    fn interrupts_enabled(&self) -> bool;
    /// Sets interrupt enabled.
    fn set_interrupts_enabled(&mut self, value: bool);

    /// Whether the keyboard has input to take.
    fn ready(&self) -> bool;
    /// Reads a character from the input (but does not take it).
    fn get_input(&self) -> Option<u8>;
    /// Reads and removes a character from the input.
    fn pop_input(&mut self) -> Option<u8>;
    /// Clears the input completely.
    fn clear_input(&mut self);
}

impl<K: KeyboardDevice> ExternalDevice for DevWrapper<K, dyn KeyboardDevice> {
    fn io_read(&mut self, addr: u16, effectful: bool) -> Option<u16> {
        match addr {
            KBSR => {
                Some(u16::from(self.ready()) << 15 | u16::from(self.interrupts_enabled()) << 14)
            },
            KBDR if effectful => self.pop_input().map(u16::from),
            KBDR => self.get_input().map(u16::from),
            _ => None
        }
    }

    fn io_write(&mut self, addr: u16, data: u16) -> bool {
        match addr {
            KBSR => {
                let ie = (data >> 14) & 1 != 0;
                self.set_interrupts_enabled(ie);
                true
            },
            _ => false
        }
    }
    
    fn io_reset(&mut self) {
        self.clear_input();
        self.set_interrupts_enabled(false);
    }

    fn poll_interrupt(&mut self) -> Option<Interrupt> {
        match self.ready() && self.interrupts_enabled() {
            true  => Some(Interrupt::vectored(KB_INTV, KB_INTP)),
            false => None,
        }
    }
}

/// Keyboard that accesses input from a memory buffer.
#[derive(Default, Clone)]
pub struct BufferedKeyboard {
    buffer: Arc<RwLock<VecDeque<u8>>>,
    interrupts_enabled: bool
}
impl BufferedKeyboard {
    /// Creates a new keyboard, wrapping it around a given buffer.
    pub fn new(buffer: Arc<RwLock<VecDeque<u8>>>) -> Self {
        Self { buffer, interrupts_enabled: false }
    }

    /// Gets a reference to the internal buffer of this keyboard.
    pub fn get_buffer(&self) -> &Arc<RwLock<VecDeque<u8>>> {
        &self.buffer
    }
    
    fn try_input(&self) -> Option<RwLockWriteGuard<'_, VecDeque<u8>>> {
        match self.buffer.try_write() {
            Ok(g) => Some(g),
            Err(TryLockError::Poisoned(e)) => Some(e.into_inner()),
            Err(TryLockError::WouldBlock) => None,
        }
    }
}
impl KeyboardDevice for BufferedKeyboard {
    fn interrupts_enabled(&self) -> bool {
        self.interrupts_enabled
    }

    fn set_interrupts_enabled(&mut self, value: bool) {
        self.interrupts_enabled = value;
    }

    fn ready(&self) -> bool {
        self.try_input().is_some_and(|buf| !buf.is_empty())
    }

    fn get_input(&self) -> Option<u8> {
        self.try_input()?.front().copied()
    }

    fn pop_input(&mut self) -> Option<u8> {
        self.try_input()?.pop_front()
    }
    
    fn clear_input(&mut self) {
        if let Some(mut inp) = self.try_input() {
            inp.clear();
        }
    }
}
impl ExternalDevice for BufferedKeyboard {
    fn io_read(&mut self, addr: u16, effectful: bool) -> Option<u16> {
        DevWrapper::wrap(self).io_read(addr, effectful)
    }

    fn io_write(&mut self, addr: u16, data: u16) -> bool {
        DevWrapper::wrap(self).io_write(addr, data)
    }

    fn io_reset(&mut self) {
        DevWrapper::wrap(self).io_reset()
    }
    
    fn poll_interrupt(&mut self) -> Option<Interrupt> {
        DevWrapper::wrap(self).poll_interrupt()
    }
    
    fn _to_sim_device(self, _: super::internals::ToSimDeviceToken) -> super::internals::SimDevice
        where Self: Sized
    {
        super::internals::SimDevice::Keyboard(self)
    }
}