bitsy_lang/sim/ext/
monitor.rs

1use super::*;
2
3/// A monitor.
4/// On reset and on clock, whatever value is presesnted to the `in` port is printed.
5#[derive(Debug)]
6pub struct Monitor(Option<String>);
7
8impl Monitor {
9  pub fn new() -> Monitor {
10      Monitor(None)
11  }
12}
13
14impl ExtInstance for Monitor {
15    fn incoming_ports(&self) -> Vec<PortName> { vec!["in".to_string()] }
16    fn outgoing_ports(&self) -> Vec<PortName> { vec![] }
17
18    fn update(&mut self, _port: &PortName, value: Value) -> Vec<(PortName, Value)> {
19        self.0 = Some(format!("{value:?}"));
20        vec![]
21    }
22
23    fn clock(&mut self) -> Vec<(PortName, Value)> {
24        if let Some(s) = &self.0 {
25            println!("Monitor: {s}");
26            self.0 = None
27        }
28        vec![]
29    }
30
31    fn reset(&mut self) -> Vec<(PortName, Value)> {
32        if let Some(s) = &self.0 {
33            println!("Monitor: {s}");
34            self.0 = None
35        }
36        vec![]
37    }
38}