KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_net_chain_trunk_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
23
24#include <base_units.h>
25#include <board.h>
27#include <footprint.h>
28#include <net_chain_bridging.h>
29#include <netinfo.h>
30#include <pad.h>
31#include <pcb_track.h>
33
34
35constexpr double DEFAULT_PROPAGATION_DELAY_PS_PER_MM = 5.9 * pcbIUScale.IU_PER_PS;
36
37// Two-net daisy chain through a single bridge. Trunk = 20 mm (net A track) +
38// 5 mm (bridge pad-to-pad span) + 25 mm (net B track) = 50 mm. The board has
39// no stackup so the per-track length-delay calculator returns a known-type
40// item with zero delay (trackDelay() does not fall back when Type != UNKNOWN);
41// only the bridge edge carries delay, derived from the chain-wide fallback
42// 5.9 ps/mm. This pins the trunk-delay equal-to-bridge contract.
43static const char* DAISY_PCB_FILE = "net_chains/net_chain_trunk_delay.kicad_pcb";
44
45
46namespace
47{
48std::unique_ptr<BOARD> loadBoard( const char* aBoardFile )
49{
50 PCB_IO_KICAD_SEXPR plugin;
51 std::unique_ptr<BOARD> board = plugin.LoadBoard( KI_TEST::GetPcbnewTestDataDir() + aBoardFile );
52 board->BuildConnectivity();
53
54 return board;
55}
56
57
58void tagChainNets( BOARD* aBoard, const wxString& aChain )
59{
60 for( NETINFO_ITEM* n : aBoard->GetNetInfo() )
61 {
62 if( n && n->GetNetname().StartsWith( wxS( "/NET_" ) ) )
63 n->SetNetChain( aChain );
64 }
65}
66
67
68// Anchor terminals at the extreme-X footprints; the fixture routes in a single
69// row and carries no Reference strings to match on
70void setTerminals( BOARD* aBoard, const wxString& aChain )
71{
72 FOOTPRINT* first = nullptr;
73 FOOTPRINT* last = nullptr;
74
75 for( FOOTPRINT* fp : aBoard->Footprints() )
76 {
77 if( fp->Pads().empty() )
78 continue;
79
80 if( !first || fp->GetPosition().x < first->GetPosition().x )
81 first = fp;
82
83 if( !last || fp->GetPosition().x > last->GetPosition().x )
84 last = fp;
85 }
86
87 BOOST_REQUIRE( first );
88 BOOST_REQUIRE( last );
89 BOOST_REQUIRE( first != last );
90
91 for( NETINFO_ITEM* n : aBoard->GetNetInfo() )
92 {
93 if( n && n->GetNetChain() == aChain )
94 {
95 n->SetTerminalPad( 0, first->Pads().front() );
96 n->SetTerminalPad( 1, last->Pads().front() );
97 }
98 }
99}
100
101
102std::set<BOARD_CONNECTED_ITEM*> collectChainItems( BOARD* aBoard, const wxString& aChain )
103{
104 std::set<BOARD_CONNECTED_ITEM*> items;
105
106 for( PCB_TRACK* t : aBoard->Tracks() )
107 {
108 if( t->GetNet() && t->GetNet()->GetNetChain() == aChain )
109 items.insert( t );
110 }
111
112 for( FOOTPRINT* fp : aBoard->Footprints() )
113 {
114 for( PAD* p : fp->Pads() )
115 {
116 if( p->GetNet() && p->GetNet()->GetNetChain() == aChain )
117 items.insert( p );
118 }
119 }
120
121 return items;
122}
123
124} // namespace
125
126
127BOOST_AUTO_TEST_SUITE( NetChainTrunkDelay )
128
129
130// 50 mm trunk, no stackup => tracks contribute 0 delay, bridge contributes
131// 5 mm at the fallback 5.9 ps/mm. Verifies that the bridge edge's delay is
132// derived consistently with BoardChainBridging() and folded into TrunkDelay().
133BOOST_AUTO_TEST_CASE( DaisyTrunkDelayEqualsBridgeWithoutStackup )
134{
135 auto board = loadBoard( DAISY_PCB_FILE );
136 tagChainNets( board.get(), wxS( "DSY" ) );
137 setTerminals( board.get(), wxS( "DSY" ) );
138
139 CHAIN_TOPOLOGY topo( board.get(), wxS( "DSY" ), collectChainItems( board.get(), wxS( "DSY" ) ),
141
142 BOOST_REQUIRE( topo.IsValid() );
143 BOOST_CHECK_CLOSE( topo.TrunkLength(), 50.0e6, 5.0 );
144
145 double expectedDelayIU = DEFAULT_PROPAGATION_DELAY_PS_PER_MM * 5.0;
146 BOOST_CHECK_CLOSE( topo.TrunkDelay(), expectedDelayIU, 5.0 );
147 BOOST_CHECK_GT( topo.TrunkDelay(), 0.0 );
148
149 // The bridging-only helper should agree with what the trunk picked up.
150 auto [bridgingLen, bridgingDelay] =
151 BoardChainBridging( board.get(), wxS( "DSY" ), DEFAULT_PROPAGATION_DELAY_PS_PER_MM );
152 BOOST_CHECK_CLOSE( bridgingDelay, topo.TrunkDelay(), 0.001 );
153 BOOST_CHECK_CLOSE( bridgingLen, 5.0e6, 0.001 );
154}
155
156
157// Without terminal pads the topology cannot reduce to a trunk; callers must
158// fall back to BoardChainBridging. Verify both: the topology is not valid,
159// and the bridging-only delay covers the 5 mm cross-net pad span at the
160// fallback per-mm rate.
161BOOST_AUTO_TEST_CASE( NoTerminalsFallbackUsesBridgingDelay )
162{
163 auto board = loadBoard( DAISY_PCB_FILE );
164 tagChainNets( board.get(), wxS( "DSY" ) );
165 // Intentionally do NOT call setTerminals.
166
167 CHAIN_TOPOLOGY topo( board.get(), wxS( "DSY" ), collectChainItems( board.get(), wxS( "DSY" ) ),
169
170 BOOST_CHECK( !topo.IsValid() );
171 BOOST_CHECK_EQUAL( static_cast<int>( topo.GetStatus() ),
172 static_cast<int>( CHAIN_TOPOLOGY::STATUS::NO_TERMINAL_PADS ) );
173
174 auto [bridgingLen, bridgingDelay] =
175 BoardChainBridging( board.get(), wxS( "DSY" ), DEFAULT_PROPAGATION_DELAY_PS_PER_MM );
176
177 BOOST_CHECK_CLOSE( bridgingLen, 5.0e6, 0.001 );
178
179 double expectedDelayIU = DEFAULT_PROPAGATION_DELAY_PS_PER_MM * 5.0;
180 BOOST_CHECK_CLOSE( bridgingDelay, expectedDelayIU, 0.001 );
181}
182
183
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:121
General utilities for PCB file IO for QA programs.
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:409
const NETINFO_LIST & GetNetInfo() const
Definition board.h:1207
const FOOTPRINTS & Footprints() const
Definition board.h:463
const TRACKS & Tracks() const
Definition board.h:461
Build a topological view of a single named net chain's routed copper.
STATUS GetStatus() const
double TrunkLength() const
bool IsValid() const
double TrunkDelay() const
std::deque< PAD * > & Pads()
Definition footprint.h:404
VECTOR2I GetPosition() const override
Definition footprint.h:435
Handle the data for a net.
Definition netinfo.h:50
Definition pad.h:61
A #PLUGIN derivation for saving and loading Pcbnew s-expression formatted files.
std::unique_ptr< BOARD > LoadBoard(const wxString &aFileName, const std::map< std::string, UTF8 > *aProperties=nullptr, PROJECT *aProject=nullptr)
Load information from some input file format that this PCB_IO implementation knows about into new BOA...
Definition pcb_io.cpp:72
std::string GetPcbnewTestDataDir()
Utility which returns a path to the data directory where the test board files are stored.
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)
static const char * DAISY_PCB_FILE
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
BOOST_AUTO_TEST_SUITE_END()
constexpr double DEFAULT_PROPAGATION_DELAY_PS_PER_MM
BOOST_AUTO_TEST_CASE(DaisyTrunkDelayEqualsBridgeWithoutStackup)
BOOST_CHECK_EQUAL(result, "25.4")