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
22#include <filesystem>
23#include <fstream>
24
25#include <base_units.h>
26#include <board.h>
28#include <footprint.h>
29#include <net_chain_bridging.h>
30#include <netinfo.h>
31#include <pad.h>
32#include <pcb_track.h>
34
35
36// Two-net daisy chain through a single bridge. Trunk = 20 mm (net A track) +
37// 5 mm (bridge pad-to-pad span) + 25 mm (net B track) = 50 mm. The board has
38// no stackup so the per-track length-delay calculator returns a known-type
39// item with zero delay (trackDelay() does not fall back when Type != UNKNOWN);
40// only the bridge edge carries delay, derived from the chain-wide fallback
41// 5.9 ps/mm. This pins the trunk-delay equal-to-bridge contract.
42static const char* DAISY_PCB = R"(
43(kicad_pcb
44 (version 20250904)
45 (generator "pcbnew")
46 (generator_version "9.99")
47 (layers
48 (0 "F.Cu" signal)
49 (2 "B.Cu" signal)
50 (44 "Edge.Cuts" user)
51 )
52 (net 0 "")
53 (net 1 "/NET_A")
54 (net 2 "/NET_B")
55 (gr_line (start -5 -5) (end 60 -5) (layer "Edge.Cuts") (width 0.05))
56 (gr_line (start 60 -5) (end 60 5) (layer "Edge.Cuts") (width 0.05))
57 (gr_line (start 60 5) (end -5 5) (layer "Edge.Cuts") (width 0.05))
58 (gr_line (start -5 5) (end -5 -5) (layer "Edge.Cuts") (width 0.05))
59 (footprint "Term1" (layer "F.Cu") (uuid "00000000-0000-0000-0000-000000000a01")
60 (at 0 0)
61 (pad "1" smd rect (at 0 0) (size 0.8 0.8) (layers "F.Cu") (net 1 "/NET_A") (uuid "00000000-0000-0000-0000-000000000a02"))
62 )
63 (footprint "Bridge" (layer "F.Cu") (uuid "00000000-0000-0000-0000-000000000b01")
64 (at 22.5 0)
65 (pad "1" smd rect (at -2.5 0) (size 0.8 0.8) (layers "F.Cu") (net 1 "/NET_A") (uuid "00000000-0000-0000-0000-000000000b02"))
66 (pad "2" smd rect (at 2.5 0) (size 0.8 0.8) (layers "F.Cu") (net 2 "/NET_B") (uuid "00000000-0000-0000-0000-000000000b03"))
67 )
68 (footprint "Term2" (layer "F.Cu") (uuid "00000000-0000-0000-0000-000000000c01")
69 (at 50 0)
70 (pad "1" smd rect (at 0 0) (size 0.8 0.8) (layers "F.Cu") (net 2 "/NET_B") (uuid "00000000-0000-0000-0000-000000000c02"))
71 )
72 (segment (start 0 0) (end 20 0) (width 0.2) (layer "F.Cu") (net 1))
73 (segment (start 25 0) (end 50 0) (width 0.2) (layer "F.Cu") (net 2))
74)
75)";
76
77
78namespace
79{
80std::unique_ptr<BOARD> loadBoard( const char* aText, const std::string& aSubdir )
81{
82 namespace fs = std::filesystem;
83 fs::path tmpDir = fs::temp_directory_path() / aSubdir;
84 fs::create_directories( tmpDir );
85 fs::path pcbPath = tmpDir / "trunk_delay.kicad_pcb";
86
87 {
88 std::ofstream out( pcbPath );
89 out << aText;
90 }
91
92 PCB_IO_KICAD_SEXPR plugin;
93 std::unique_ptr<BOARD> board = std::make_unique<BOARD>();
94 plugin.LoadBoard( pcbPath.string(), board.get() );
95 board->BuildConnectivity();
96 fs::remove( pcbPath );
97 return board;
98}
99
100
101void tagChainNets( BOARD* aBoard, const wxString& aChain )
102{
103 for( NETINFO_ITEM* n : aBoard->GetNetInfo() )
104 {
105 if( n && n->GetNetname().StartsWith( wxS( "/NET_" ) ) )
106 n->SetNetChain( aChain );
107 }
108}
109
110
111void setTerminals( BOARD* aBoard, const wxString& aChain, double aTermAxMm, double aTermBxMm )
112{
113 PAD* termA = nullptr;
114 PAD* termB = nullptr;
115 constexpr int EPS = 100;
116 const VECTOR2I targetA( static_cast<int>( aTermAxMm * 1000000 ), 0 );
117 const VECTOR2I targetB( static_cast<int>( aTermBxMm * 1000000 ), 0 );
118
119 for( FOOTPRINT* fp : aBoard->Footprints() )
120 {
121 if( fp->Pads().empty() )
122 continue;
123
124 VECTOR2I pos = fp->GetPosition();
125
126 if( std::abs( pos.x - targetA.x ) <= EPS && std::abs( pos.y - targetA.y ) <= EPS )
127 termA = fp->Pads().front();
128
129 if( std::abs( pos.x - targetB.x ) <= EPS && std::abs( pos.y - targetB.y ) <= EPS )
130 termB = fp->Pads().front();
131 }
132
133 for( NETINFO_ITEM* n : aBoard->GetNetInfo() )
134 {
135 if( n && n->GetNetChain() == aChain )
136 {
137 if( termA )
138 n->SetTerminalPad( 0, termA );
139 if( termB )
140 n->SetTerminalPad( 1, termB );
141 }
142 }
143}
144
145
146std::set<BOARD_CONNECTED_ITEM*> collectChainItems( BOARD* aBoard, const wxString& aChain )
147{
148 std::set<BOARD_CONNECTED_ITEM*> items;
149
150 for( PCB_TRACK* t : aBoard->Tracks() )
151 {
152 if( t->GetNet() && t->GetNet()->GetNetChain() == aChain )
153 items.insert( t );
154 }
155
156 for( FOOTPRINT* fp : aBoard->Footprints() )
157 {
158 for( PAD* p : fp->Pads() )
159 {
160 if( p->GetNet() && p->GetNet()->GetNetChain() == aChain )
161 items.insert( p );
162 }
163 }
164
165 return items;
166}
167
168} // namespace
169
170
171BOOST_AUTO_TEST_SUITE( NetChainTrunkDelay )
172
173
174// 50 mm trunk, no stackup => tracks contribute 0 delay, bridge contributes
175// 5 mm at the fallback 5.9 ps/mm. Verifies that the bridge edge's delay is
176// derived consistently with BoardChainBridging() and folded into TrunkDelay().
177BOOST_AUTO_TEST_CASE( DaisyTrunkDelayEqualsBridgeWithoutStackup )
178{
179 auto board = loadBoard( DAISY_PCB, "kicad_chain_trunk_delay" );
180 tagChainNets( board.get(), wxS( "DSY" ) );
181 setTerminals( board.get(), wxS( "DSY" ), 0.0, 50.0 );
182
183 CHAIN_TOPOLOGY topo( board.get(), wxS( "DSY" ),
184 collectChainItems( board.get(), wxS( "DSY" ) ) );
185
186 BOOST_REQUIRE( topo.IsValid() );
187 BOOST_CHECK_CLOSE( topo.TrunkLength(), 50.0e6, 5.0 );
188
189 double expectedDelayIU = DEFAULT_PROPAGATION_DELAY_PS_PER_MM * pcbIUScale.IU_PER_PS * 5.0;
190 BOOST_CHECK_CLOSE( topo.TrunkDelay(), expectedDelayIU, 5.0 );
191 BOOST_CHECK_GT( topo.TrunkDelay(), 0.0 );
192
193 // The bridging-only helper should agree with what the trunk picked up.
194 auto [bridgingLen, bridgingDelay] = BoardChainBridging( board.get(), wxS( "DSY" ) );
195 BOOST_CHECK_CLOSE( bridgingDelay, topo.TrunkDelay(), 0.001 );
196 BOOST_CHECK_CLOSE( bridgingLen, 5.0e6, 0.001 );
197}
198
199
200// Without terminal pads the topology cannot reduce to a trunk; callers must
201// fall back to BoardChainBridging. Verify both: the topology is not valid,
202// and the bridging-only delay covers the 5 mm cross-net pad span at the
203// fallback per-mm rate.
204BOOST_AUTO_TEST_CASE( NoTerminalsFallbackUsesBridgingDelay )
205{
206 auto board = loadBoard( DAISY_PCB, "kicad_chain_trunk_delay_noterms" );
207 tagChainNets( board.get(), wxS( "DSY" ) );
208 // Intentionally do NOT call setTerminals.
209
210 CHAIN_TOPOLOGY topo( board.get(), wxS( "DSY" ),
211 collectChainItems( board.get(), wxS( "DSY" ) ) );
212
213 BOOST_CHECK( !topo.IsValid() );
214 BOOST_CHECK_EQUAL( static_cast<int>( topo.GetStatus() ),
215 static_cast<int>( CHAIN_TOPOLOGY::STATUS::NO_TERMINAL_PADS ) );
216
217 auto [bridgingLen, bridgingDelay] = BoardChainBridging( board.get(), wxS( "DSY" ) );
218
219 BOOST_CHECK_CLOSE( bridgingLen, 5.0e6, 0.001 );
220
221 double expectedDelayIU = DEFAULT_PROPAGATION_DELAY_PS_PER_MM * pcbIUScale.IU_PER_PS * 5.0;
222 BOOST_CHECK_CLOSE( bridgingDelay, expectedDelayIU, 0.001 );
223}
224
225
constexpr EDA_IU_SCALE pcbIUScale
Definition base_units.h:121
Information pertinent to a Pcbnew printed circuit board.
Definition board.h:372
const NETINFO_LIST & GetNetInfo() const
Definition board.h:1086
const FOOTPRINTS & Footprints() const
Definition board.h:420
const TRACKS & Tracks() const
Definition board.h:418
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
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 ...
constexpr double EPS
Floating point comparison epsilon.
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
Definition eda_angle.h:400
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)
static const char * DAISY_PCB
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE(DaisyTrunkDelayEqualsBridgeWithoutStackup)
BOOST_CHECK_EQUAL(result, "25.4")
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683