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
use super::*;
use std::collections::BTreeMap;

/// A monitor.
/// On reset and on clock, whatever value is presesnted to the `in` port is printed.
#[derive(Debug)]
pub struct Monitor(String, BTreeMap<Path, Option<String>>);

impl Monitor {
  pub fn new(name: String) -> Monitor {
      Monitor(name, BTreeMap::new())
  }
}

impl Ext for Monitor {
    fn name(&self) -> String {
        self.0.clone()
    }

    fn incoming_ports(&self) -> Vec<PortName> { vec!["in".to_string()] }
    fn outgoing_ports(&self) -> Vec<PortName> { vec![] }

    fn instantiate(&mut self, path: Path) {
        self.1.insert(path, None);
    }

    fn update(&mut self, path: Path, _port: &PortName, value: Value) -> Vec<(PortName, Value)> {
        self.1.insert(path, Some(format!("{value:?}")));
        vec![]
    }

    fn clock(&mut self, path: Path) -> Vec<(PortName, Value)> {
        if let Some(Some(s)) = &self.1.get(&path) {
            println!("{path}: {s}");
            self.1.insert(path, None);
        }
        vec![]
    }

    fn reset(&mut self, path: Path) -> Vec<(PortName, Value)> {
        if let Some(Some(s)) = &self.1.get(&path) {
            println!("{path}: {s}");
            self.1.insert(path, None);
        }
        vec![]
    }
}