KiCad PCB EDA Suite
Loading...
Searching...
No Matches
pns_meander_placer.cpp
Go to the documentation of this file.
1/*
2 * KiRouter - a push-and-(sometimes-)shove PCB router
3 *
4 * Copyright (C) 2013-2015 CERN
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 * Author: Tomasz Wlostowski <[email protected]>
7 *
8 * This program is free software: you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation, either version 3 of the License, or (at your
11 * option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#include "pns_debug_decorator.h"
23#include "pns_itemset.h"
24#include "pns_meander_placer.h"
25
26#include "pns_helpers.h"
27#include "pns_node.h"
28#include "pns_router.h"
29#include "pns_solid.h"
30#include "pns_topology.h"
31
33#include <algorithm>
34
36
37namespace PNS {
38
40 MEANDER_PLACER_BASE( aRouter )
41{
42 m_currentNode = nullptr;
43
44 // Init temporary variables (do not leave uninitialized members)
45 m_initialSegment = nullptr;
46 m_lastLength = 0;
47 m_lastDelay = 0;
51 m_netClass = nullptr;
52}
53
54
58
59
60NODE* MEANDER_PLACER::CurrentNode( bool aLoopsRemoved ) const
61{
62 if( !m_currentNode )
63 return m_world;
64
65 return m_currentNode;
66}
67
68
69bool MEANDER_PLACER::Start( const VECTOR2I& aP, ITEM* aStartItem )
70{
71 if( !aStartItem || !aStartItem->OfKind( ITEM::SEGMENT_T | ITEM::ARC_T ) )
72 {
73 Router()->SetFailureReason( _( "Please select a track whose length you want to tune." ) );
74 return false;
75 }
76
77 m_initialSegment = static_cast<LINKED_ITEM*>( aStartItem );
78 m_currentNode = nullptr;
80
81 m_world = Router()->GetWorld()->Branch();
82 m_originLine = m_world->AssembleLine( m_initialSegment );
83
84 TOPOLOGY topo( m_world );
86
89
90 if( m_startPad_n )
91 {
92 m_padToDieLength += m_startPad_n->GetPadToDie();
93 m_padToDieDelay += m_startPad_n->GetPadToDieDelay();
94 }
95
96 if( m_endPad_n )
97 {
98 m_padToDieLength += m_endPad_n->GetPadToDie();
99 m_padToDieDelay += m_endPad_n->GetPadToDieDelay();
100 }
101
102 m_world->Remove( m_originLine );
103
105 m_currentEnd = VECTOR2I( 0, 0 );
106
107 const BOARD_CONNECTED_ITEM* conItem = static_cast<BOARD_CONNECTED_ITEM*>( aStartItem->GetSourceItem() );
108 m_netClass = conItem->GetEffectiveNetClass();
109
111 m_baselineDelay = m_settings.m_isTimeDomain ? origPathDelay() : 0;
112
114
116
117 return true;
118}
119
120
122{
123 return m_padToDieLength + m_settings.m_signalExtraLength
125}
126
127
129{
130 return m_padToDieDelay + m_settings.m_signalExtraDelay
132}
133
134
136{
137 // If this is a time domain tuning, calculate the target length for the desired total delay
138 if( m_settings.m_isTimeDomain )
139 {
140 // curDelayChain includes other nets (chain aggregate). curDelayNet excludes extras.
141 const int64_t curDelayChain = origPathDelay();
142 const int64_t curDelayNet = curDelayChain - m_settings.m_signalExtraDelay;
143
144 // Prefer chain-level target if explicitly set (i.e. not unconstrained and differs from net target)
145 bool useSignalTarget = ( m_settings.m_targetSignalLengthDelay.Opt() != MEANDER_SETTINGS::DELAY_UNCONSTRAINED );
146
147 const MINOPTMAX<long long int>& targetDelaySet = useSignalTarget ? m_settings.m_targetSignalLengthDelay
148 : m_settings.m_targetLengthDelay;
149
150 // Desired overall chain delay values
151 int64_t desiredDelayMin = targetDelaySet.Min();
152 int64_t desiredDelayOpt = targetDelaySet.Opt();
153 int64_t desiredDelayMax = targetDelaySet.Max();
154
155 // If using chain target, convert desired overall chain delay into desired per-net contribution
156 if( useSignalTarget )
157 {
158 desiredDelayMin = std::max<int64_t>( 0, desiredDelayMin - m_settings.m_signalExtraDelay );
159 desiredDelayOpt = std::max<int64_t>( 0, desiredDelayOpt - m_settings.m_signalExtraDelay );
160 desiredDelayMax = std::max<int64_t>( desiredDelayOpt, desiredDelayMax - m_settings.m_signalExtraDelay );
161 }
162
163 // Current delay basis for comparison (per-net when using chain target else aggregate)
164 const int64_t curDelay = useSignalTarget ? curDelayNet : curDelayChain;
165
166 const int64_t delayDifferenceOpt = desiredDelayOpt - curDelay;
167
168 const int64_t curLength = origPathLength();
169 const int64_t lengthDiffMin = m_router->GetInterface()->CalculateLengthForDelay(
170 desiredDelayOpt - desiredDelayMin, m_currentWidth, false, m_router->Sizes().DiffPairGap(),
171 m_router->GetCurrentLayer(), m_netClass );
172 int64_t lengthDiffOpt = m_router->GetInterface()->CalculateLengthForDelay(
173 std::abs( delayDifferenceOpt ), m_currentWidth, false, m_router->Sizes().DiffPairGap(),
174 m_router->GetCurrentLayer(), m_netClass );
175 const int64_t lengthDiffMax = m_router->GetInterface()->CalculateLengthForDelay(
176 desiredDelayMax - desiredDelayOpt, m_currentWidth, false, m_router->Sizes().DiffPairGap(),
177 m_router->GetCurrentLayer(), m_netClass );
178
179 lengthDiffOpt = delayDifferenceOpt > 0 ? lengthDiffOpt : -lengthDiffOpt;
180
181 m_settings.m_targetLength.SetMin( curLength + lengthDiffOpt - lengthDiffMin );
182 m_settings.m_targetLength.SetOpt( curLength + lengthDiffOpt );
183 m_settings.m_targetLength.SetMax( curLength + lengthDiffOpt + lengthDiffMax );
184 }
185}
186
187
188bool MEANDER_PLACER::Move( const VECTOR2I& aP, ITEM* aEndItem )
189{
190 // Reuse the chain-extras aggregate captured at Start(). Other nets in the chain are
191 // not edited during a tuning session, so we don't need to walk the BOARD again.
192 const long long extraDelay = m_chainExtrasValid ? m_chainExtrasDelay : 0;
193
194 // m_signalExtraDelay is needed for calculateTimeDomainTargets().
195 m_settings.m_signalExtraDelay = extraDelay;
196
197 // Derive per-net budget from chain target, accounting for stubs not in the PNS path.
198 // Take the tighter of chain budget and existing per-net constraint (from EditStart).
199 if( m_settings.m_targetSignalLength.Opt() != MEANDER_SETTINGS::LENGTH_UNCONSTRAINED )
200 {
201 const long long otherLen = chainNarrowingOffset();
202
203 long long budgetMin = std::max( 0LL, m_settings.m_targetSignalLength.Min() - otherLen );
204 long long budgetOpt = std::max( 0LL, m_settings.m_targetSignalLength.Opt() - otherLen );
205 long long budgetMax = std::max( budgetOpt, m_settings.m_targetSignalLength.Max() - otherLen );
206
207 if( m_settings.m_targetLength.Opt() == MEANDER_SETTINGS::LENGTH_UNCONSTRAINED )
208 {
209 m_settings.m_targetLength.SetMin( budgetMin );
210 m_settings.m_targetLength.SetOpt( budgetOpt );
211 m_settings.m_targetLength.SetMax( budgetMax );
212 }
213 else
214 {
215 m_settings.m_targetLength.SetMin( std::max( m_settings.m_targetLength.Min(), budgetMin ) );
216 m_settings.m_targetLength.SetOpt( std::min( m_settings.m_targetLength.Opt(), budgetOpt ) );
217 m_settings.m_targetLength.SetMax( std::min( m_settings.m_targetLength.Max(), budgetMax ) );
218 }
219 }
220
222
223 return doMove( aP, aEndItem, m_settings.m_targetLength.Opt(), m_settings.m_targetLength.Min(),
224 m_settings.m_targetLength.Max() );
225}
226
227
228bool MEANDER_PLACER::doMove( const VECTOR2I& aP, ITEM* aEndItem, long long int aTargetLength,
229 long long int aTargetMin, long long int aTargetMax )
230{
231 if( m_currentStart == aP )
232 return false;
233
234 if( m_currentNode )
235 delete m_currentNode;
236
237 m_currentNode = m_world->Branch();
238
239 SHAPE_LINE_CHAIN pre, tuned, post;
240
241 m_originLine.CLine().Split( m_currentStart, aP, pre, tuned, post );
242
243 m_result = MEANDERED_LINE( this, false );
244 m_result.SetWidth( m_originLine.Width() );
245 m_result.SetBaselineOffset( 0 );
246
247 for( int i = 0; i < tuned.SegmentCount(); i++ )
248 {
249 if( tuned.IsArcSegment( i ) )
250 {
251 ssize_t arcIndex = tuned.ArcIndex( i );
252 m_result.AddArc( tuned.Arc( arcIndex ) );
253 i = tuned.NextShape( i );
254
255 // NextShape will return -1 if last shape
256 if( i < 0 )
257 i = tuned.SegmentCount();
258
259 continue;
260 }
261
262 bool side = false;
263 const SEG s = tuned.CSegment( i );
264
265 if( m_settings.m_initialSide == 0 )
266 side = s.Side( aP ) < 0;
267 else
268 side = m_settings.m_initialSide < 0;
269
270 m_result.AddCorner( s.A );
271 m_result.MeanderSegment( s, side );
272 m_result.AddCorner( s.B );
273 }
274
275 long long int lineLen = origPathLength();
276 int64_t lineDelay = origPathDelay();
277
278 m_lastLength = lineLen;
281
282 if( lineLen > m_settings.m_targetLength.Max() )
283 {
285 }
286 else
287 {
288 m_lastLength = lineLen - tuned.Length();
289
290 if( m_settings.m_isTimeDomain )
291 {
293 - m_router->GetInterface()->CalculateDelayForShapeLineChain(
294 tuned, m_currentWidth, false, m_router->Sizes().DiffPairGap(),
295 m_router->GetCurrentLayer(), m_netClass );
296 }
297
298 tuneLineLength( m_result, aTargetLength - lineLen );
299 }
300
301 for( const ITEM* item : m_tunedPath.CItems() )
302 {
303 if( const LINE* l = dyn_cast<const LINE*>( item ) )
304 {
305 PNS_DBG( Dbg(), AddItem, l, BLUE, 30000, wxT( "tuned-line" ) );
306
307 m_router->GetInterface()->DisplayPathLine( l->CLine(), 1 );
308 }
309 }
310
311 if( m_lastStatus != TOO_LONG )
312 {
313 tuned.Clear();
314
315 for( MEANDER_SHAPE* m : m_result.Meanders() )
316 {
317 if( m->Type() != MT_EMPTY )
318 {
319 tuned.Append ( m->CLine( 0 ) );
320 }
321 }
322
323 m_lastLength += tuned.Length();
324
325 if( m_settings.m_isTimeDomain )
326 {
327 m_lastDelay += m_router->GetInterface()->CalculateDelayForShapeLineChain(
328 tuned, m_currentWidth, false, m_router->Sizes().DiffPairGap(), m_router->GetCurrentLayer(),
329 m_netClass );
330 }
331
332 if( m_lastLength > aTargetMax )
334 else if( m_lastLength < aTargetMin )
336 else
338 }
339
340 m_finalShape.Clear();
341
342 if( m_settings.m_keepEndpoints )
343 {
344 pre.Simplify();
345 tuned.Simplify();
346 post.Simplify();
347
348 m_finalShape.Append( pre );
349 m_finalShape.Append( tuned );
350 m_finalShape.Append( post );
351 }
352 else
353 {
354 m_finalShape.Append( pre );
355 m_finalShape.Append( tuned );
356 m_finalShape.Append( post );
357 m_finalShape.Simplify();
358 }
359
360 return true;
361}
362
363
364bool MEANDER_PLACER::FixRoute( const VECTOR2I& aP, ITEM* aEndItem, bool aForceFinish )
365{
366 if( !m_currentNode )
367 return false;
368
372
373 return true;
374}
375
376
378{
379 m_world->KillChildren();
380 return true;
381}
382
383
385{
386 return m_currentTrace.SegmentCount() > 0;
387}
388
389
391{
392 if( m_currentNode )
394
395 m_currentNode = nullptr;
396 return true;
397}
398
399
401{
402 LINE l( m_originLine, aShape->CLine( 0 ) );
403
404 if( m_currentNode->CheckColliding( &l ) )
405 return false;
406
407 int w = aShape->Width();
408 int clearance = w + m_settings.m_spacing;
409
410 return m_result.CheckSelfIntersections( aShape, clearance );
411}
412
413
419
421{
422 return m_tunedPath;
423}
424
426{
427 return m_currentStart;
428}
429
431{
432 return m_currentEnd;
433}
434
436{
437 return m_initialSegment->Layers().Start();
438}
439
440
442{
443 if( m_lastLength )
444 return m_lastLength;
445 else
446 return origPathLength();
447}
448
449
451{
452 if( m_lastDelay )
453 return m_lastDelay;
454 else
455 return origPathDelay();
456}
457
458
463
464}
A base class derived from BOARD_ITEM for items that can be connected and have a net,...
virtual NETCLASS * GetEffectiveNetClass() const
Return the NETCLASS for this item.
T Min() const
Definition minoptmax.h:33
T Max() const
Definition minoptmax.h:34
T Opt() const
Definition minoptmax.h:35
ROUTER * Router() const
Return current router settings.
ROUTER * m_router
DEBUG_DECORATOR * Dbg() const
Base class for PNS router board items.
Definition pns_item.h:98
BOARD_ITEM * GetSourceItem() const
Definition pns_item.h:202
bool OfKind(int aKindMask) const
Definition pns_item.h:181
Represents a track on a PCB, connecting two non-trivial joints (that is, vias, pads,...
Definition pns_line.h:62
Represent a set of meanders fitted over a single or two lines.
long long int chainNarrowingOffset() const
Return the length offset to subtract when converting a user-facing total signal length target into a ...
void tuneLineLength(MEANDERED_LINE &aTuned, long long int aElongation)
Take a set of meanders in aTuned and tunes their length to extend the original line length by aElonga...
int64_t m_baselineDelay
Aggregate length/delay of other nets in the same chain, cached at Start().
TUNING_STATUS
< Result of the length tuning operation
int m_currentWidth
Meander settings.
bool m_chainExtrasValid
Pointer to world to search colliding items.
void initChainExtras()
Cache the per-session chain-extras length/delay (other nets in the same chain) so per-Move use does n...
MEANDER_SETTINGS m_settings
The current end point.
int64_t lineDelay(const ITEM_SET &aLine, const SOLID *aStartPad, const SOLID *aEndPad) const
Calculate the total delay of the line represented by an item set (tracks and vias)
NODE * m_world
Width of the meandered trace(s).
long long int lineLength(const ITEM_SET &aLine, const SOLID *aStartPad, const SOLID *aEndPad) const
Calculate the total length of the line represented by an item set (tracks and vias)
virtual bool FixRoute(const VECTOR2I &aP, ITEM *aEndItem, bool aForceFinish=false) override
Function FixRoute()
bool doMove(const VECTOR2I &aP, ITEM *aEndItem, long long int aTargetLength, long long int aTargetMin, long long int aTargetMax)
virtual bool Move(const VECTOR2I &aP, ITEM *aEndItem) override
Function Move()
virtual long long int origPathLength() const
int CurrentLayer() const override
Function CurrentLayer()
virtual void calculateTimeDomainTargets()
current routing start point (end of tail, beginning of head)
int m_padToDieDelay
The netclass for the placed segments.
const VECTOR2I & CurrentEnd() const override
Function CurrentEnd()
const VECTOR2I & CurrentStart() const override
Function CurrentStart()
long long int TuningLengthResult() const override
Return the resultant length or skew of the tuned traces.
bool AbortPlacement() override
int64_t TuningDelayResult() const override
Return the resultant delay or skew of the tuned traces.
bool HasPlacedAnything() const override
NODE * CurrentNode(bool aLoopsRemoved=false) const override
Function CurrentNode()
LINKED_ITEM * m_initialSegment
Total length added by pad to die size.
SHAPE_LINE_CHAIN m_finalShape
const ITEM_SET TunedPath() override
const ITEM_SET Traces() override
Function Traces()
MEANDER_PLACER(ROUTER *aRouter)
virtual int64_t origPathDelay() const
bool CheckFit(MEANDER_SHAPE *aShape) override
Checks if it's OK to place the shape aShape (i.e.
VECTOR2I m_currentStart
Current world state.
TUNING_STATUS TuningStatus() const override
Return the tuning status (too short, too long, etc.) of the trace(s) being tuned.
virtual bool Start(const VECTOR2I &aP, ITEM *aStartItem) override
Function Start()
int m_padToDieLength
Total length added by pad to die size.
bool CommitPlacement() override
static const long long int LENGTH_UNCONSTRAINED
Definition pns_meander.h:73
static const long long int DELAY_UNCONSTRAINED
Definition pns_meander.h:76
The geometry of a single meander.
int Width() const
const SHAPE_LINE_CHAIN & CLine(int aShape) const
Keep the router "world" - i.e.
Definition pns_node.h:242
NODE * Branch()
Create a lightweight copy (called branch) of self that tracks the changes (added/removed items) wrs t...
Definition pns_node.cpp:157
void SetFailureReason(const wxString &aReason)
Definition pns_router.h:235
void CommitRouting()
NODE * GetWorld() const
Definition pns_router.h:186
const ITEM_SET AssembleTuningPath(ROUTER_IFACE *aRouterIface, ITEM *aStart, SOLID **aStartPad=nullptr, SOLID **aEndPad=nullptr)
Like AssembleTrivialPath, but follows the track length algorithm, which discards segments that are fu...
Definition seg.h:42
VECTOR2I A
Definition seg.h:49
VECTOR2I B
Definition seg.h:50
int Side(const VECTOR2I &aP) const
Determine on which side of directed line passing via segment ends point aP lies.
Definition seg.h:143
Represent a polyline containing arcs as well as line segments: A chain of connected line and/or arc s...
const SHAPE_ARC & Arc(size_t aArc) const
ssize_t ArcIndex(size_t aSegment) const
Return the arc index for the given segment index.
void Clear()
Remove all points from the line chain.
void Simplify(int aTolerance=0)
Simplify the line chain by removing colinear adjacent segments and duplicate vertices.
int NextShape(int aPointIndex) const
Return the vertex index of the next shape in the chain, or -1 if aPointIndex is the last shape.
void Append(int aX, int aY, bool aAllowDuplication=false)
Append a new point at the end of the line chain.
int SegmentCount() const
Return the number of segments in this line chain.
const SEG CSegment(int aIndex) const
Return a constant copy of the aIndex segment in the line chain.
bool IsArcSegment(size_t aSegment) const
long long int Length() const
Return length of the line chain in Euclidean metric.
@ BLUE
Definition color4d.h:56
#define _(s)
Push and Shove diff pair dimensions (gap) settings dialog.
@ MT_EMPTY
Definition pns_meander.h:49
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
Definition eda_angle.h:400
#define PNS_DBG(dbg, method,...)
static VECTOR2I GetSnappedStartPoint(LINKED_ITEM *aStartItem, VECTOR2I aStartPoint)
int clearance
Casted dyn_cast(From aObject)
A lightweight dynamic downcast.
Definition typeinfo.h:60
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:687