Skip to main content

gwr_components/
connect.rs

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