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