KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_net_chain_bridging_delay.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20#include <boost/test/unit_test.hpp>
21
22#include <base_units.h>
23#include <board.h>
25#include <footprint.h>
26#include <net_chain_bridging.h>
27#include <netinfo.h>
28#include <pad.h>
29#include <padstack.h>
30
31
32namespace
33{
34
35constexpr int PAD_SIZE_NM = 500'000;
36
37
38PAD* addPad( FOOTPRINT* aFp, NETINFO_ITEM* aNet, const VECTOR2I& aPos )
39{
40 PAD* pad = new PAD( aFp );
42 pad->SetSize( PADSTACK::ALL_LAYERS, VECTOR2I( PAD_SIZE_NM, PAD_SIZE_NM ) );
43 pad->SetPosition( aPos );
44 pad->SetNet( aNet );
45 aFp->Add( pad );
46 return pad;
47}
48
49
50NETINFO_ITEM* addNet( BOARD* aBoard, const wxString& aName, int aCode, const wxString& aChain )
51{
52 NETINFO_ITEM* n = new NETINFO_ITEM( aBoard, aName, aCode );
53 n->SetNetChain( aChain );
54 aBoard->Add( n );
55 return n;
56}
57
58
59FOOTPRINT* addFootprint( BOARD* aBoard )
60{
61 FOOTPRINT* fp = new FOOTPRINT( aBoard );
62 aBoard->Add( fp );
63 return fp;
64}
65
66} // namespace
67
68
69BOOST_AUTO_TEST_SUITE( NetChainBridgingDelay )
70
71
72BOOST_AUTO_TEST_CASE( EmptyInputsReturnZero )
73{
74 BOARD board;
75
76 auto [len, delay] = BoardChainBridging( &board, wxS( "SIG" ) );
77
78 BOOST_CHECK_EQUAL( len, 0.0 );
79 BOOST_CHECK_EQUAL( delay, 0.0 );
80
81 auto [len2, delay2] = BoardChainBridging( nullptr, wxS( "SIG" ) );
82
83 BOOST_CHECK_EQUAL( len2, 0.0 );
84 BOOST_CHECK_EQUAL( delay2, 0.0 );
85
86 auto [len3, delay3] = BoardChainBridging( &board, wxString() );
87
88 BOOST_CHECK_EQUAL( len3, 0.0 );
89 BOOST_CHECK_EQUAL( delay3, 0.0 );
90}
91
92
93BOOST_AUTO_TEST_CASE( BridgingLengthAndDelayMatchFallback )
94{
95 BOARD board;
96
97 NETINFO_ITEM* netA = addNet( &board, wxS( "/A" ), 1, wxS( "SIG" ) );
98 NETINFO_ITEM* netB = addNet( &board, wxS( "/B" ), 2, wxS( "SIG" ) );
99
100 FOOTPRINT* fp = addFootprint( &board );
101 addPad( fp, netA, VECTOR2I( -1'000'000, 0 ) );
102 addPad( fp, netB, VECTOR2I( 1'000'000, 0 ) );
103
104 auto [len, delay] = BoardChainBridging( &board, wxS( "SIG" ) );
105
106 // 2 mm bridging span.
107 BOOST_CHECK_CLOSE( len, 2'000'000.0, 0.001 );
108
109 // 2 mm at fallback ps/mm, expressed in delay IU (attoseconds).
110 double expectedDelayIU = DEFAULT_PROPAGATION_DELAY_PS_PER_MM * pcbIUScale.IU_PER_PS * 2.0;
111 BOOST_CHECK_CLOSE( delay, expectedDelayIU, 0.001 );
112 BOOST_CHECK_GT( delay, 0.0 );
113}
114
115
116BOOST_AUTO_TEST_CASE( SingleNetFootprintContributesNothing )
117{
118 BOARD board;
119
120 NETINFO_ITEM* netA = addNet( &board, wxS( "/A" ), 1, wxS( "SIG" ) );
121
122 FOOTPRINT* fp = addFootprint( &board );
123 addPad( fp, netA, VECTOR2I( -1'000'000, 0 ) );
124 addPad( fp, netA, VECTOR2I( 1'000'000, 0 ) );
125
126 auto [len, delay] = BoardChainBridging( &board, wxS( "SIG" ) );
127
128 BOOST_CHECK_EQUAL( len, 0.0 );
129 BOOST_CHECK_EQUAL( delay, 0.0 );
130}
131
132
133BOOST_AUTO_TEST_CASE( MultipleFootprintsSum )
134{
135 BOARD board;
136
137 NETINFO_ITEM* netA = addNet( &board, wxS( "/A" ), 1, wxS( "SIG" ) );
138 NETINFO_ITEM* netB = addNet( &board, wxS( "/B" ), 2, wxS( "SIG" ) );
139 NETINFO_ITEM* netC = addNet( &board, wxS( "/C" ), 3, wxS( "SIG" ) );
140
141 FOOTPRINT* fp1 = addFootprint( &board );
142 addPad( fp1, netA, VECTOR2I( -1'500'000, 0 ) );
143 addPad( fp1, netB, VECTOR2I( 1'500'000, 0 ) );
144
145 FOOTPRINT* fp2 = addFootprint( &board );
146 addPad( fp2, netB, VECTOR2I( 10'000'000, 0 ) );
147 addPad( fp2, netC, VECTOR2I( 13'000'000, 0 ) );
148
149 auto [len, delay] = BoardChainBridging( &board, wxS( "SIG" ) );
150
151 BOOST_CHECK_CLOSE( len, 6'000'000.0, 0.001 );
152
153 double expectedDelayIU = DEFAULT_PROPAGATION_DELAY_PS_PER_MM * pcbIUScale.IU_PER_PS * 6.0;
154 BOOST_CHECK_CLOSE( delay, expectedDelayIU, 0.001 );
155}
156
157
158BOOST_AUTO_TEST_CASE( ChainsArePartitionedByName )
159{
160 BOARD board;
161
162 NETINFO_ITEM* netA = addNet( &board, wxS( "/A" ), 1, wxS( "SIG_X" ) );
163 NETINFO_ITEM* netB = addNet( &board, wxS( "/B" ), 2, wxS( "SIG_X" ) );
164 NETINFO_ITEM* netC = addNet( &board, wxS( "/C" ), 3, wxS( "SIG_Y" ) );
165 NETINFO_ITEM* netD = addNet( &board, wxS( "/D" ), 4, wxS( "SIG_Y" ) );
166
167 FOOTPRINT* fp1 = addFootprint( &board );
168 addPad( fp1, netA, VECTOR2I( -1'000'000, 0 ) );
169 addPad( fp1, netB, VECTOR2I( 1'000'000, 0 ) );
170
171 FOOTPRINT* fp2 = addFootprint( &board );
172 addPad( fp2, netC, VECTOR2I( 10'000'000, 0 ) );
173 addPad( fp2, netD, VECTOR2I( 14'000'000, 0 ) );
174
175 auto [xLen, xDelay] = BoardChainBridging( &board, wxS( "SIG_X" ) );
176 auto [yLen, yDelay] = BoardChainBridging( &board, wxS( "SIG_Y" ) );
177
178 BOOST_CHECK_CLOSE( xLen, 2'000'000.0, 0.001 );
179 BOOST_CHECK_CLOSE( yLen, 4'000'000.0, 0.001 );
180
181 double expectedXDelayIU = DEFAULT_PROPAGATION_DELAY_PS_PER_MM * pcbIUScale.IU_PER_PS * 2.0;
182 double expectedYDelayIU = DEFAULT_PROPAGATION_DELAY_PS_PER_MM * pcbIUScale.IU_PER_PS * 4.0;
183 BOOST_CHECK_CLOSE( xDelay, expectedXDelayIU, 0.001 );
184 BOOST_CHECK_CLOSE( yDelay, expectedYDelayIU, 0.001 );
185 BOOST_CHECK_GT( yDelay, xDelay );
186}
187
188
189BOOST_AUTO_TEST_CASE( LargeBridgingLengthDoesNotOverflowIntCast )
190{
191 BOARD board;
192
193 NETINFO_ITEM* netA = addNet( &board, wxS( "/A" ), 1, wxS( "SIG" ) );
194 NETINFO_ITEM* netB = addNet( &board, wxS( "/B" ), 2, wxS( "SIG" ) );
195
196 // 3 m span (3,000,000,000 nm) exceeds INT_MAX (~2.147e9). The previous
197 // (int) cast on lengthIU silently truncated to a negative value here,
198 // producing a negative delay; the direct-divide form must remain accurate.
199 FOOTPRINT* fp = addFootprint( &board );
200 addPad( fp, netA, VECTOR2I( -1'500'000'000, 0 ) );
201 addPad( fp, netB, VECTOR2I( 1'500'000'000, 0 ) );
202
203 auto [len, delay] = BoardChainBridging( &board, wxS( "SIG" ) );
204
205 BOOST_CHECK_CLOSE( len, 3'000'000'000.0, 0.001 );
206
207 double expectedDelayIU = DEFAULT_PROPAGATION_DELAY_PS_PER_MM * pcbIUScale.IU_PER_PS * 3000.0;
208 BOOST_CHECK_CLOSE( delay, expectedDelayIU, 0.001 );
209 BOOST_CHECK_GT( delay, 0.0 );
210}
211
212
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:121
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:372
void Add(BOARD_ITEM *aItem, ADD_MODE aMode=ADD_MODE::INSERT, bool aSkipConnectivity=false) override
Removes an item from the container.
Definition board.cpp:1295
void Add(BOARD_ITEM *aItem, ADD_MODE aMode=ADD_MODE::INSERT, bool aSkipConnectivity=false) override
Removes an item from the container.
Handle the data for a net.
Definition netinfo.h:46
void SetNetChain(const wxString &aNetChain)
Definition netinfo.h:113
static constexpr PCB_LAYER_ID ALL_LAYERS
! Temporary layer identifier to identify code that is not padstack-aware
Definition padstack.h:177
Definition pad.h:61
std::tuple< double, double > BoardChainBridging(const BOARD *aBoard, const wxString &aNetChain)
Compute both the chain bridging length and its associated propagation delay (in internal delay IU,...
constexpr double DEFAULT_PROPAGATION_DELAY_PS_PER_MM
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE(EmptyInputsReturnZero)
BOOST_CHECK_EQUAL(result, "25.4")
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683