gwr_components/
connect.rs

1// Copyright (c) 2023 Graphcore Ltd. All rights reserved.
2
3//! Helper connection macros
4
5pub use paste::paste;
6
7#[macro_export]
8/// Connect an [OutPort](gwr_engine::port::OutPort) port to an
9/// [InPort](gwr_engine::port::InPort)
10macro_rules! connect_port {
11    ($from:expr, $from_port_name:ident => $to:expr, $to_port_name:ident) => {
12        {
13            gwr_track::debug!($from.entity() ; "Connect {}.{} => {}.{}", $from, stringify!($from_port_name), $to, stringify!($to_port_name));
14            $crate::connect::paste! {
15                $from.[< connect_port_ $from_port_name >]($to.[< port_ $to_port_name >]())
16            }
17        }
18    };
19    ($from:expr, $from_port_name:ident, $from_index:expr => $to:expr, $to_port_name:ident) => {
20        {
21            let from_index: usize = $from_index;
22            gwr_track::debug!($from.entity() ; "Connect {}.{}[{}] => {}.{}", $from, stringify!($from_port_name), from_index, $to, stringify!($to_port_name));
23            $crate::connect::paste! {
24                $from.[< connect_port_ $from_port_name _i >](from_index, $to.[< port_ $to_port_name >]())
25            }
26        }
27    };
28    ($from:expr, $from_port_name:ident => $to:expr, $to_port_name:ident, $to_index:expr) => {
29        {
30            let to_index: usize = $to_index;
31            gwr_track::debug!($from.entity() ; "Connect {}.{} => {}.{}[{}]", $from, stringify!($from_port_name), $to, stringify!($to_port_name), to_index);
32            $crate::connect::paste! {
33                $from.[< connect_port_ $from_port_name >]($to.[< port_ $to_port_name _i >](to_index))
34            }
35        }
36    };
37    ($from:expr, $from_port_name:ident, $from_index:expr => $to:expr, $to_port_name:ident, $to_index:expr) => {
38        {
39            let from_index: usize = $from_index;
40            let to_index: usize = $to_index;
41            gwr_track::debug!($from.entity() ; "Connect {}.{}[{}] => {}.{}[{}]", $from, stringify!($from_port_name), from_index, $to, stringify!($to_port_name), to_index);
42            $crate::connect::paste! {
43                $from.[< connect_port_ $from_port_name _i >](from_index, $to.[< port_ $to_port_name _i >](to_index))
44            }
45        }
46    };
47}
48
49#[macro_export]
50/// Create and connect a dummy RX port
51macro_rules! connect_dummy_rx {
52    ($from:expr, $from_port_name:ident => $engine:expr, $clock:expr, $entity:expr) => {
53        {
54            use gwr_track::entity::GetEntity;
55            let rx_port = gwr_engine::port::InPort::new($engine, $clock, $entity, "dummy");
56
57            gwr_track::debug!($from.entity() ; "Connect {}.{} => {}", $from, stringify!($from_port_name), rx_port);
58            $crate::connect::paste! {
59                $from.[< connect_port_ $from_port_name >](rx_port.state())
60            }
61        }
62    };
63    ($from:expr, $from_port_name:ident, $from_index:expr => $engine:expr, $clock:expr, $entity:expr) => {
64        {
65            use gwr_track::entity::GetEntity;
66            let rx_port = gwr_engine::port::InPort::new($engine, $clock, $entity, "dummy");
67
68            let from_index: usize = $from_index;
69            gwr_track::debug!($from.entity() ; "Connect {}.{}[{}] => {}", $from, stringify!($from_port_name), from_index, rx_port);
70            $crate::connect::paste! {
71                $from.[< connect_port_ $from_port_name _i >](from_index, rx_port.state())
72            }
73        }
74    };
75}
76
77#[macro_export]
78/// Create and connect a dummy TX port
79macro_rules! connect_dummy_tx {
80    ($entity:expr => $to:expr, $to_port_name:ident) => {
81        {
82            let mut tx_port = gwr_engine::port::OutPort::new($entity, "dummy");
83
84            gwr_track::debug!($entity ; "Connect {} => {}.{}", tx_port, $to, stringify!($to_port_name));
85            $crate::connect::paste! {
86                tx_port.connect($to.[< port_ $to_port_name >]())
87            }
88        }
89    };
90    ($entity:expr => $to:expr, $to_port_name:ident, $to_index:expr) => {
91        {
92            let mut tx_port = gwr_engine::port::OutPort::new($entity, "dummy");
93
94            let to_index: usize = $to_index;
95            gwr_track::debug!($entity ; "Connect {} => {}.{}[{}]", tx_port, $to, stringify!($to_port_name), to_index);
96            $crate::connect::paste! {
97                tx_port.connect($to.[< port_ $to_port_name _i >](to_index))
98            }
99        }
100    };
101}
102
103#[macro_export]
104/// Connect a tx port for a subcomponent.
105///
106/// The subcomponent is expected to be stored in a `RefCell<Option<>>`
107macro_rules! connect_tx {
108    ($component:expr, $fn:ident ; $port_state:ident) => {
109        $crate::connect::paste! {
110            $component
111                .borrow_mut()
112                .as_mut()
113                .unwrap()
114                .$fn($port_state)
115        }
116    };
117}
118
119#[macro_export]
120/// Connect a tx port for a subcomponent where the port is one of an array.
121///
122/// The subcomponent is expected to be stored in a `RefCell<Option<>>`
123macro_rules! connect_tx_i {
124    ($component:expr, $fn:ident, $index:expr ; $port_state:ident) => {
125        $component
126            .borrow_mut()
127            .as_mut()
128            .unwrap()
129            .$fn($index, $port_state)
130    };
131}
132
133#[macro_export]
134/// Access rx port for a subcomponent.
135///
136/// The subcomponent is expected to be stored in a `RefCell<Option<>>`
137macro_rules! port_rx {
138    ($component:expr, $fn:ident) => {
139        $component.borrow().as_ref().unwrap().$fn()
140    };
141}
142
143#[macro_export]
144/// Access an individual index of an rx port array for a subcomponent.
145///
146/// The subcomponent is expected to be stored in a `RefCell<Option<>>`
147macro_rules! port_rx_i {
148    ($component:expr, $fn:ident, $index:expr) => {
149        $component.borrow().as_ref().unwrap().$fn($index)
150    };
151}
152
153#[macro_export]
154/// Get a reference to a variable stored in a `RefCell<Option<>>`.
155macro_rules! borrow_option {
156    ($var:expr) => {
157        $var.borrow().as_ref().unwrap()
158    };
159}
160
161#[macro_export]
162/// Get a mutable reference to a variable stored in a `RefCell<Option<>>`.
163macro_rules! borrow_option_mut {
164    ($var:expr) => {
165        $var.borrow_mut().as_mut().unwrap()
166    };
167}
168
169#[macro_export]
170/// Take a variable out of a `RefCell<Option<>>`.
171macro_rules! take_option {
172    ($var:expr) => {
173        $var.borrow_mut().take().unwrap()
174    };
175}