rust_hdl_core/
check_error.rs

1use crate::block::Block;
2use crate::check_connected::check_connected;
3use crate::check_logic_loops::check_logic_loops;
4use crate::check_write_inputs::check_inputs_not_written;
5
6use std::collections::HashMap;
7
8/// A map of open connections, hashed on the signal ID
9pub type OpenMap = HashMap<usize, PathedName>;
10
11/// Struct to capture a signal in the design for human consumption
12#[derive(Clone, Debug, PartialEq)]
13pub struct PathedName {
14    /// The path to the signal (i.e., the hierarchical namespace such as `uut:flasher:blah`)
15    pub path: String,
16    /// The name of the signal that is being referenced, such as `pulse_in`.
17    pub name: String,
18}
19
20/// A list of [PathedName]
21pub type PathedNameList = Vec<PathedName>;
22
23/// The enum models the errors that can be returned from "checking"
24/// a circuit using [check_all].
25#[derive(Debug, Clone, PartialEq)]
26pub enum CheckError {
27    /// The check failed because of one or more open signals (described by the [OpenMap])
28    OpenSignal(OpenMap),
29    /// The circuit contains logical loops (i.e., `A <- B <- A`), and will not simulate
30    LogicLoops(PathedNameList),
31    /// The circuit attempts to write to the inputs, which is not allowed in RustHDL.
32    WritesToInputs(PathedNameList),
33}
34
35/// This is a helper function used to check a [Block] for connection, loops, and
36/// writes to the inputs.  
37/// ```rust
38/// use rust_hdl_core::prelude::*;
39///
40/// #[derive(LogicBlock, Default)]
41/// struct Circuit {
42///    pub in1: Signal<In, Bit>,
43///    pub out1: Signal<Out, Bit>,
44/// }
45///
46/// impl Logic for Circuit {
47///    #[hdl_gen]
48///    fn update(&mut self) {
49///         self.out1.next = !self.in1.val();
50///    }
51/// }
52///
53/// let mut uut = Circuit::default();  uut.connect_all();
54/// assert!(check_all(&uut).is_ok());
55/// ```
56pub fn check_all(uut: &dyn Block) -> Result<(), CheckError> {
57    check_connected(uut)?;
58    check_logic_loops(uut)?;
59    check_inputs_not_written(uut)?;
60    Ok(())
61}