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
32constexpr double DEFAULT_PROPAGATION_DELAY_PS_PER_MM = 5.9 * pcbIUScale.IU_PER_PS;
33
34
35namespace
36{
37
38constexpr int PAD_SIZE_NM = 500'000;
39
40
41PAD* addPad( FOOTPRINT* aFp, NETINFO_ITEM* aNet, const VECTOR2I& aPos )
42{
43 PAD* pad = new PAD( aFp );
45 pad->SetSize( PADSTACK::ALL_LAYERS, VECTOR2I( PAD_SIZE_NM, PAD_SIZE_NM ) );
46 pad->SetPosition( aPos );
47 pad->SetNet( aNet );
48 aFp->Add( pad );
49 return pad;
50}
51
52
53NETINFO_ITEM* addNet( BOARD* aBoard, const wxString& aName, int aCode, const wxString& aChain )
54{
55 NETINFO_ITEM* n = new NETINFO_ITEM( aBoard, aName, aCode );
56 n->SetNetChain( aChain );
57 aBoard->Add( n );
58 return n;
59}
60
61
62FOOTPRINT* addFootprint( BOARD* aBoard )
63{
64 FOOTPRINT* fp = new FOOTPRINT( aBoard );
65 aBoard->Add( fp );
66 return fp;
67}
68
69} // namespace
70
71
72BOOST_AUTO_TEST_SUITE( NetChainBridgingDelay )
73
74
75BOOST_AUTO_TEST_CASE( EmptyInputsReturnZero )
76{
77 BOARD board;
78
79 auto [len, delay] = BoardChainBridging( &board, wxS( "SIG" ), DEFAULT_PROPAGATION_DELAY_PS_PER_MM );
80
81 BOOST_CHECK_EQUAL( len, 0.0 );
82 BOOST_CHECK_EQUAL( delay, 0.0 );
83
84 auto [len2, delay2] = BoardChainBridging( nullptr, wxS( "SIG" ) );
85
86 BOOST_CHECK_EQUAL( len2, 0.0 );
87 BOOST_CHECK_EQUAL( delay2, 0.0 );
88
89 auto [len3, delay3] = BoardChainBridging( &board, wxString() );
90
91 BOOST_CHECK_EQUAL( len3, 0.0 );
92 BOOST_CHECK_EQUAL( delay3, 0.0 );
93}
94
95
96BOOST_AUTO_TEST_CASE( BridgingLengthAndDelayMatchFallback )
97{
98 BOARD board;
99
100 NETINFO_ITEM* netA = addNet( &board, wxS( "/A" ), 1, wxS( "SIG" ) );
101 NETINFO_ITEM* netB = addNet( &board, wxS( "/B" ), 2, wxS( "SIG" ) );
102
103 FOOTPRINT* fp = addFootprint( &board );
104 addPad( fp, netA, VECTOR2I( -1'000'000, 0 ) );
105 addPad( fp, netB, VECTOR2I( 1'000'000, 0 ) );
106
107 auto [len, delay] = BoardChainBridging( &board, wxS( "SIG" ), DEFAULT_PROPAGATION_DELAY_PS_PER_MM );
108
109 // 2 mm bridging span.
110 BOOST_CHECK_CLOSE( len, 2'000'000.0, 0.001 );
111
112 // 2 mm at fallback ps/mm, expressed in delay IU (attoseconds).
113 double expectedDelayIU = DEFAULT_PROPAGATION_DELAY_PS_PER_MM * 2.0;
114 BOOST_CHECK_CLOSE( delay, expectedDelayIU, 0.001 );
115 BOOST_CHECK_GT( delay, 0.0 );
116}
117
118
119BOOST_AUTO_TEST_CASE( SingleNetFootprintContributesNothing )
120{
121 BOARD board;
122
123 NETINFO_ITEM* netA = addNet( &board, wxS( "/A" ), 1, wxS( "SIG" ) );
124
125 FOOTPRINT* fp = addFootprint( &board );
126 addPad( fp, netA, VECTOR2I( -1'000'000, 0 ) );
127 addPad( fp, netA, VECTOR2I( 1'000'000, 0 ) );
128
129 auto [len, delay] = BoardChainBridging( &board, wxS( "SIG" ), DEFAULT_PROPAGATION_DELAY_PS_PER_MM );
130
131 BOOST_CHECK_EQUAL( len, 0.0 );
132 BOOST_CHECK_EQUAL( delay, 0.0 );
133}
134
135
136BOOST_AUTO_TEST_CASE( MultipleFootprintsSum )
137{
138 BOARD board;
139
140 NETINFO_ITEM* netA = addNet( &board, wxS( "/A" ), 1, wxS( "SIG" ) );
141 NETINFO_ITEM* netB = addNet( &board, wxS( "/B" ), 2, wxS( "SIG" ) );
142 NETINFO_ITEM* netC = addNet( &board, wxS( "/C" ), 3, wxS( "SIG" ) );
143
144 FOOTPRINT* fp1 = addFootprint( &board );
145 addPad( fp1, netA, VECTOR2I( -1'500'000, 0 ) );
146 addPad( fp1, netB, VECTOR2I( 1'500'000, 0 ) );
147
148 FOOTPRINT* fp2 = addFootprint( &board );
149 addPad( fp2, netB, VECTOR2I( 10'000'000, 0 ) );
150 addPad( fp2, netC, VECTOR2I( 13'000'000, 0 ) );
151
152 auto [len, delay] = BoardChainBridging( &board, wxS( "SIG" ), DEFAULT_PROPAGATION_DELAY_PS_PER_MM );
153
154 BOOST_CHECK_CLOSE( len, 6'000'000.0, 0.001 );
155
156 double expectedDelayIU = DEFAULT_PROPAGATION_DELAY_PS_PER_MM * 6.0;
157 BOOST_CHECK_CLOSE( delay, expectedDelayIU, 0.001 );
158}
159
160
161BOOST_AUTO_TEST_CASE( ChainsArePartitionedByName )
162{
163 BOARD board;
164
165 NETINFO_ITEM* netA = addNet( &board, wxS( "/A" ), 1, wxS( "SIG_X" ) );
166 NETINFO_ITEM* netB = addNet( &board, wxS( "/B" ), 2, wxS( "SIG_X" ) );
167 NETINFO_ITEM* netC = addNet( &board, wxS( "/C" ), 3, wxS( "SIG_Y" ) );
168 NETINFO_ITEM* netD = addNet( &board, wxS( "/D" ), 4, wxS( "SIG_Y" ) );
169
170 FOOTPRINT* fp1 = addFootprint( &board );
171 addPad( fp1, netA, VECTOR2I( -1'000'000, 0 ) );
172 addPad( fp1, netB, VECTOR2I( 1'000'000, 0 ) );
173
174 FOOTPRINT* fp2 = addFootprint( &board );
175 addPad( fp2, netC, VECTOR2I( 10'000'000, 0 ) );
176 addPad( fp2, netD, VECTOR2I( 14'000'000, 0 ) );
177
178 auto [xLen, xDelay] = BoardChainBridging( &board, wxS( "SIG_X" ), DEFAULT_PROPAGATION_DELAY_PS_PER_MM );
179 auto [yLen, yDelay] = BoardChainBridging( &board, wxS( "SIG_Y" ), DEFAULT_PROPAGATION_DELAY_PS_PER_MM );
180
181 BOOST_CHECK_CLOSE( xLen, 2'000'000.0, 0.001 );
182 BOOST_CHECK_CLOSE( yLen, 4'000'000.0, 0.001 );
183
184 double expectedXDelayIU = DEFAULT_PROPAGATION_DELAY_PS_PER_MM * 2.0;
185 double expectedYDelayIU = DEFAULT_PROPAGATION_DELAY_PS_PER_MM * 4.0;
186 BOOST_CHECK_CLOSE( xDelay, expectedXDelayIU, 0.001 );
187 BOOST_CHECK_CLOSE( yDelay, expectedYDelayIU, 0.001 );
188 BOOST_CHECK_GT( yDelay, xDelay );
189}
190
191
192BOOST_AUTO_TEST_CASE( LargeBridgingLengthDoesNotOverflowIntCast )
193{
194 BOARD board;
195
196 NETINFO_ITEM* netA = addNet( &board, wxS( "/A" ), 1, wxS( "SIG" ) );
197 NETINFO_ITEM* netB = addNet( &board, wxS( "/B" ), 2, wxS( "SIG" ) );
198
199 // 3 m span (3,000,000,000 nm) exceeds INT_MAX (~2.147e9). The previous
200 // (int) cast on lengthIU silently truncated to a negative value here,
201 // producing a negative delay; the direct-divide form must remain accurate.
202 FOOTPRINT* fp = addFootprint( &board );
203 addPad( fp, netA, VECTOR2I( -1'500'000'000, 0 ) );
204 addPad( fp, netB, VECTOR2I( 1'500'000'000, 0 ) );
205
206 auto [len, delay] = BoardChainBridging( &board, wxS( "SIG" ), DEFAULT_PROPAGATION_DELAY_PS_PER_MM );
207
208 BOOST_CHECK_CLOSE( len, 3'000'000'000.0, 0.001 );
209
210 double expectedDelayIU = DEFAULT_PROPAGATION_DELAY_PS_PER_MM * 3000.0;
211 BOOST_CHECK_CLOSE( delay, expectedDelayIU, 0.001 );
212 BOOST_CHECK_GT( delay, 0.0 );
213}
214
215
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:121
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:373
void Add(BOARD_ITEM *aItem, ADD_MODE aMode=ADD_MODE::INSERT, bool aSkipConnectivity=false) override
Removes an item from the container.
Definition board.cpp:1355
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, const double aDefaultBridgeUnitDelay)
Compute both the chain bridging length and its associated propagation delay (in internal delay IU,...
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE(EmptyInputsReturnZero)
constexpr double DEFAULT_PROPAGATION_DELAY_PS_PER_MM
BOOST_CHECK_EQUAL(result, "25.4")
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683