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 = std::make_unique<BOARD>();
52 plugin.LoadBoard( KI_TEST::GetPcbnewTestDataDir() + aBoardFile, board.get() );
53 board->BuildConnectivity();
54
55 return board;
56}
57
58
59void tagChainNets( BOARD* aBoard, const wxString& aChain )
60{
61 for( NETINFO_ITEM* n : aBoard->GetNetInfo() )
62 {
63 if( n && n->GetNetname().StartsWith( wxS( "/NET_" ) ) )
64 n->SetNetChain( aChain );
65 }
66}
67
68
69// Anchor terminals at the extreme-X footprints; the fixture routes in a single
70// row and carries no Reference strings to match on
71void setTerminals( BOARD* aBoard, const wxString& aChain )
72{
73 FOOTPRINT* first = nullptr;
74 FOOTPRINT* last = nullptr;
75
76 for( FOOTPRINT* fp : aBoard->Footprints() )
77 {
78 if( fp->Pads().empty() )
79 continue;
80
81 if( !first || fp->GetPosition().x < first->GetPosition().x )
82 first = fp;
83
84 if( !last || fp->GetPosition().x > last->GetPosition().x )
85 last = fp;
86 }
87
88 BOOST_REQUIRE( first );
89 BOOST_REQUIRE( last );
90 BOOST_REQUIRE( first != last );
91
92 for( NETINFO_ITEM* n : aBoard->GetNetInfo() )
93 {
94 if( n && n->GetNetChain() == aChain )
95 {
96 n->SetTerminalPad( 0, first->Pads().front() );
97 n->SetTerminalPad( 1, last->Pads().front() );
98 }
99 }
100}
101
102
103std::set<BOARD_CONNECTED_ITEM*> collectChainItems( BOARD* aBoard, const wxString& aChain )
104{
105 std::set<BOARD_CONNECTED_ITEM*> items;
106
107 for( PCB_TRACK* t : aBoard->Tracks() )
108 {
109 if( t->GetNet() && t->GetNet()->GetNetChain() == aChain )
110 items.insert( t );
111 }
112
113 for( FOOTPRINT* fp : aBoard->Footprints() )
114 {
115 for( PAD* p : fp->Pads() )
116 {
117 if( p->GetNet() && p->GetNet()->GetNetChain() == aChain )
118 items.insert( p );
119 }
120 }
121
122 return items;
123}
124
125} // namespace
126
127
128BOOST_AUTO_TEST_SUITE( NetChainTrunkDelay )
129
130
131// 50 mm trunk, no stackup => tracks contribute 0 delay, bridge contributes
132// 5 mm at the fallback 5.9 ps/mm. Verifies that the bridge edge's delay is
133// derived consistently with BoardChainBridging() and folded into TrunkDelay().
134BOOST_AUTO_TEST_CASE( DaisyTrunkDelayEqualsBridgeWithoutStackup )
135{
136 auto board = loadBoard( DAISY_PCB_FILE );
137 tagChainNets( board.get(), wxS( "DSY" ) );
138 setTerminals( board.get(), wxS( "DSY" ) );
139
140 CHAIN_TOPOLOGY topo( board.get(), wxS( "DSY" ), collectChainItems( board.get(), wxS( "DSY" ) ),
142
143 BOOST_REQUIRE( topo.IsValid() );
144 BOOST_CHECK_CLOSE( topo.TrunkLength(), 50.0e6, 5.0 );
145
146 double expectedDelayIU = DEFAULT_PROPAGATION_DELAY_PS_PER_MM * 5.0;
147 BOOST_CHECK_CLOSE( topo.TrunkDelay(), expectedDelayIU, 5.0 );
148 BOOST_CHECK_GT( topo.TrunkDelay(), 0.0 );
149
150 // The bridging-only helper should agree with what the trunk picked up.
151 auto [bridgingLen, bridgingDelay] =
152 BoardChainBridging( board.get(), wxS( "DSY" ), DEFAULT_PROPAGATION_DELAY_PS_PER_MM );
153 BOOST_CHECK_CLOSE( bridgingDelay, topo.TrunkDelay(), 0.001 );
154 BOOST_CHECK_CLOSE( bridgingLen, 5.0e6, 0.001 );
155}
156
157
158// Without terminal pads the topology cannot reduce to a trunk; callers must
159// fall back to BoardChainBridging. Verify both: the topology is not valid,
160// and the bridging-only delay covers the 5 mm cross-net pad span at the
161// fallback per-mm rate.
162BOOST_AUTO_TEST_CASE( NoTerminalsFallbackUsesBridgingDelay )
163{
164 auto board = loadBoard( DAISY_PCB_FILE );
165 tagChainNets( board.get(), wxS( "DSY" ) );
166 // Intentionally do NOT call setTerminals.
167
168 CHAIN_TOPOLOGY topo( board.get(), wxS( "DSY" ), collectChainItems( board.get(), wxS( "DSY" ) ),
170
171 BOOST_CHECK( !topo.IsValid() );
172 BOOST_CHECK_EQUAL( static_cast<int>( topo.GetStatus() ),
173 static_cast<int>( CHAIN_TOPOLOGY::STATUS::NO_TERMINAL_PADS ) );
174
175 auto [bridgingLen, bridgingDelay] =
176 BoardChainBridging( board.get(), wxS( "DSY" ), DEFAULT_PROPAGATION_DELAY_PS_PER_MM );
177
178 BOOST_CHECK_CLOSE( bridgingLen, 5.0e6, 0.001 );
179
180 double expectedDelayIU = DEFAULT_PROPAGATION_DELAY_PS_PER_MM * 5.0;
181 BOOST_CHECK_CLOSE( bridgingDelay, expectedDelayIU, 0.001 );
182}
183
184
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:373
const NETINFO_LIST & GetNetInfo() const
Definition board.h:1098
const FOOTPRINTS & Footprints() const
Definition board.h:421
const TRACKS & Tracks() const
Definition board.h:419
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:375
VECTOR2I GetPosition() const override
Definition footprint.h:406
Handle the data for a net.
Definition netinfo.h:46
Definition pad.h:61
A #PLUGIN derivation for saving and loading Pcbnew s-expression formatted files.
BOARD * LoadBoard(const wxString &aFileName, BOARD *aAppendToMe, const std::map< std::string, UTF8 > *aProperties=nullptr, PROJECT *aProject=nullptr) override
Load information from some input file format that this PCB_IO implementation knows about into either ...
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")