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#include "pns_node.h"
26#include "pns_router.h"
27#include "pns_solid.h"
28#include "pns_topology.h"
29
31#include <algorithm>
32
34
35namespace PNS {
36
38 MEANDER_PLACER_BASE( aRouter )
39{
40 m_currentNode = nullptr;
41
42 // Init temporary variables (do not leave uninitialized members)
43 m_initialSegment = nullptr;
44 m_lastLength = 0;
45 m_lastDelay = 0;
49 m_netClass = nullptr;
50}
51
52
56
57
58NODE* MEANDER_PLACER::CurrentNode( bool aLoopsRemoved ) const
59{
60 if( !m_currentNode )
61 return m_world;
62
63 return m_currentNode;
64}
65
66
67bool MEANDER_PLACER::Start( const VECTOR2I& aP, ITEM* aStartItem )
68{
69 if( !aStartItem || !aStartItem->OfKind( ITEM::SEGMENT_T | ITEM::ARC_T ) )
70 {
71 Router()->SetFailureReason( _( "Please select a track whose length you want to tune." ) );
72 return false;
73 }
74
75 m_initialSegment = static_cast<LINKED_ITEM*>( aStartItem );
76 m_currentNode = nullptr;
78
79 m_world = Router()->GetWorld()->Branch();
80 m_originLine = m_world->AssembleLine( m_initialSegment );
81
82 TOPOLOGY topo( m_world );
84
87
88 if( m_startPad_n )
89 {
90 m_padToDieLength += m_startPad_n->GetPadToDie();
91 m_padToDieDelay += m_startPad_n->GetPadToDieDelay();
92 }
93
94 if( m_endPad_n )
95 {
96 m_padToDieLength += m_endPad_n->GetPadToDie();
97 m_padToDieDelay += m_endPad_n->GetPadToDieDelay();
98 }
99
100 m_world->Remove( m_originLine );
101
103 m_currentEnd = VECTOR2I( 0, 0 );
104
105 const BOARD_CONNECTED_ITEM* conItem = static_cast<BOARD_CONNECTED_ITEM*>( aStartItem->GetSourceItem() );
106 m_netClass = conItem->GetEffectiveNetClass();
107
109 m_baselineDelay = m_settings.m_isTimeDomain ? origPathDelay() : 0;
110
112
114
115 return true;
116}
117
118
120{
121 return m_padToDieLength + m_settings.m_signalExtraLength
123}
124
125
127{
128 return m_padToDieDelay + m_settings.m_signalExtraDelay
130}
131
132
134{
135 // If this is a time domain tuning, calculate the target length for the desired total delay
136 if( m_settings.m_isTimeDomain )
137 {
138 // curDelayChain includes other nets (chain aggregate). curDelayNet excludes extras.
139 const int64_t curDelayChain = origPathDelay();
140 const int64_t curDelayNet = curDelayChain - m_settings.m_signalExtraDelay;
141
142 // Prefer chain-level target if explicitly set (i.e. not unconstrained and differs from net target)
143 bool useSignalTarget = ( m_settings.m_targetSignalLengthDelay.Opt() != MEANDER_SETTINGS::DELAY_UNCONSTRAINED );
144
145 const MINOPTMAX<long long int>& targetDelaySet = useSignalTarget ? m_settings.m_targetSignalLengthDelay
146 : m_settings.m_targetLengthDelay;
147
148 // Desired overall chain delay values
149 int64_t desiredDelayMin = targetDelaySet.Min();
150 int64_t desiredDelayOpt = targetDelaySet.Opt();
151 int64_t desiredDelayMax = targetDelaySet.Max();
152
153 // If using chain target, convert desired overall chain delay into desired per-net contribution
154 if( useSignalTarget )
155 {
156 desiredDelayMin = std::max<int64_t>( 0, desiredDelayMin - m_settings.m_signalExtraDelay );
157 desiredDelayOpt = std::max<int64_t>( 0, desiredDelayOpt - m_settings.m_signalExtraDelay );
158 desiredDelayMax = std::max<int64_t>( desiredDelayOpt, desiredDelayMax - m_settings.m_signalExtraDelay );
159 }
160
161 // Current delay basis for comparison (per-net when using chain target else aggregate)
162 const int64_t curDelay = useSignalTarget ? curDelayNet : curDelayChain;
163
164 const int64_t delayDifferenceOpt = desiredDelayOpt - curDelay;
165
166 const int64_t curLength = origPathLength();
167 const int64_t lengthDiffMin = m_router->GetInterface()->CalculateLengthForDelay(
168 desiredDelayOpt - desiredDelayMin, m_currentWidth, false, m_router->Sizes().DiffPairGap(),
169 m_router->GetCurrentLayer(), m_netClass );
170 int64_t lengthDiffOpt = m_router->GetInterface()->CalculateLengthForDelay(
171 std::abs( delayDifferenceOpt ), m_currentWidth, false, m_router->Sizes().DiffPairGap(),
172 m_router->GetCurrentLayer(), m_netClass );
173 const int64_t lengthDiffMax = m_router->GetInterface()->CalculateLengthForDelay(
174 desiredDelayMax - desiredDelayOpt, m_currentWidth, false, m_router->Sizes().DiffPairGap(),
175 m_router->GetCurrentLayer(), m_netClass );
176
177 lengthDiffOpt = delayDifferenceOpt > 0 ? lengthDiffOpt : -lengthDiffOpt;
178
179 m_settings.m_targetLength.SetMin( curLength + lengthDiffOpt - lengthDiffMin );
180 m_settings.m_targetLength.SetOpt( curLength + lengthDiffOpt );
181 m_settings.m_targetLength.SetMax( curLength + lengthDiffOpt + lengthDiffMax );
182 }
183}
184
185
186bool MEANDER_PLACER::Move( const VECTOR2I& aP, ITEM* aEndItem )
187{
188 // Reuse the chain-extras aggregate captured at Start(). Other nets in the chain are
189 // not edited during a tuning session, so we don't need to walk the BOARD again.
190 const long long extraDelay = m_chainExtrasValid ? m_chainExtrasDelay : 0;
191
192 // m_signalExtraDelay is needed for calculateTimeDomainTargets().
193 m_settings.m_signalExtraDelay = extraDelay;
194
195 // Derive per-net budget from chain target, accounting for stubs not in the PNS path.
196 // Take the tighter of chain budget and existing per-net constraint (from EditStart).
197 if( m_settings.m_targetSignalLength.Opt() != MEANDER_SETTINGS::LENGTH_UNCONSTRAINED )
198 {
199 const long long otherLen = chainNarrowingOffset();
200
201 long long budgetMin = std::max( 0LL, m_settings.m_targetSignalLength.Min() - otherLen );
202 long long budgetOpt = std::max( 0LL, m_settings.m_targetSignalLength.Opt() - otherLen );
203 long long budgetMax = std::max( budgetOpt, m_settings.m_targetSignalLength.Max() - otherLen );
204
205 if( m_settings.m_targetLength.Opt() == MEANDER_SETTINGS::LENGTH_UNCONSTRAINED )
206 {
207 m_settings.m_targetLength.SetMin( budgetMin );
208 m_settings.m_targetLength.SetOpt( budgetOpt );
209 m_settings.m_targetLength.SetMax( budgetMax );
210 }
211 else
212 {
213 m_settings.m_targetLength.SetMin( std::max( m_settings.m_targetLength.Min(), budgetMin ) );
214 m_settings.m_targetLength.SetOpt( std::min( m_settings.m_targetLength.Opt(), budgetOpt ) );
215 m_settings.m_targetLength.SetMax( std::min( m_settings.m_targetLength.Max(), budgetMax ) );
216 }
217 }
218
220
221 return doMove( aP, aEndItem, m_settings.m_targetLength.Opt(), m_settings.m_targetLength.Min(),
222 m_settings.m_targetLength.Max() );
223}
224
225
226bool MEANDER_PLACER::doMove( const VECTOR2I& aP, ITEM* aEndItem, long long int aTargetLength,
227 long long int aTargetMin, long long int aTargetMax )
228{
229 if( m_currentStart == aP )
230 return false;
231
232 if( m_currentNode )
233 delete m_currentNode;
234
235 m_currentNode = m_world->Branch();
236
237 SHAPE_LINE_CHAIN pre, tuned, post;
238
239 m_originLine.CLine().Split( m_currentStart, aP, pre, tuned, post );
240
241 m_result = MEANDERED_LINE( this, false );
242 m_result.SetWidth( m_originLine.Width() );
243 m_result.SetBaselineOffset( 0 );
244
245 for( int i = 0; i < tuned.SegmentCount(); i++ )
246 {
247 if( tuned.IsArcSegment( i ) )
248 {
249 ssize_t arcIndex = tuned.ArcIndex( i );
250 m_result.AddArc( tuned.Arc( arcIndex ) );
251 i = tuned.NextShape( i );
252
253 // NextShape will return -1 if last shape
254 if( i < 0 )
255 i = tuned.SegmentCount();
256
257 continue;
258 }
259
260 bool side = false;
261 const SEG s = tuned.CSegment( i );
262
263 if( m_settings.m_initialSide == 0 )
264 side = s.Side( aP ) < 0;
265 else
266 side = m_settings.m_initialSide < 0;
267
268 m_result.AddCorner( s.A );
269 m_result.MeanderSegment( s, side );
270 m_result.AddCorner( s.B );
271 }
272
273 long long int lineLen = origPathLength();
274 int64_t lineDelay = origPathDelay();
275
276 m_lastLength = lineLen;
279
280 if( lineLen > m_settings.m_targetLength.Max() )
281 {
283 }
284 else
285 {
286 m_lastLength = lineLen - tuned.Length();
287
288 if( m_settings.m_isTimeDomain )
289 {
291 - m_router->GetInterface()->CalculateDelayForShapeLineChain(
292 tuned, m_currentWidth, false, m_router->Sizes().DiffPairGap(),
293 m_router->GetCurrentLayer(), m_netClass );
294 }
295
296 tuneLineLength( m_result, aTargetLength - lineLen );
297 }
298
299 for( const ITEM* item : m_tunedPath.CItems() )
300 {
301 if( const LINE* l = dyn_cast<const LINE*>( item ) )
302 {
303 PNS_DBG( Dbg(), AddItem, l, BLUE, 30000, wxT( "tuned-line" ) );
304
305 m_router->GetInterface()->DisplayPathLine( l->CLine(), 1 );
306 }
307 }
308
309 if( m_lastStatus != TOO_LONG )
310 {
311 tuned.Clear();
312
313 for( MEANDER_SHAPE* m : m_result.Meanders() )
314 {
315 if( m->Type() != MT_EMPTY )
316 {
317 tuned.Append ( m->CLine( 0 ) );
318 }
319 }
320
321 m_lastLength += tuned.Length();
322
323 if( m_settings.m_isTimeDomain )
324 {
325 m_lastDelay += m_router->GetInterface()->CalculateDelayForShapeLineChain(
326 tuned, m_currentWidth, false, m_router->Sizes().DiffPairGap(), m_router->GetCurrentLayer(),
327 m_netClass );
328 }
329
330 if( m_lastLength > aTargetMax )
332 else if( m_lastLength < aTargetMin )
334 else
336 }
337
338 m_finalShape.Clear();
339
340 if( m_settings.m_keepEndpoints )
341 {
342 pre.Simplify();
343 tuned.Simplify();
344 post.Simplify();
345
346 m_finalShape.Append( pre );
347 m_finalShape.Append( tuned );
348 m_finalShape.Append( post );
349 }
350 else
351 {
352 m_finalShape.Append( pre );
353 m_finalShape.Append( tuned );
354 m_finalShape.Append( post );
355 m_finalShape.Simplify();
356 }
357
358 return true;
359}
360
361
362bool MEANDER_PLACER::FixRoute( const VECTOR2I& aP, ITEM* aEndItem, bool aForceFinish )
363{
364 if( !m_currentNode )
365 return false;
366
370
371 return true;
372}
373
374
376{
377 m_world->KillChildren();
378 return true;
379}
380
381
383{
384 return m_currentTrace.SegmentCount() > 0;
385}
386
387
389{
390 if( m_currentNode )
392
393 m_currentNode = nullptr;
394 return true;
395}
396
397
399{
400 LINE l( m_originLine, aShape->CLine( 0 ) );
401
402 if( m_currentNode->CheckColliding( &l ) )
403 return false;
404
405 int w = aShape->Width();
406 int clearance = w + m_settings.m_spacing;
407
408 return m_result.CheckSelfIntersections( aShape, clearance );
409}
410
411
417
419{
420 return m_tunedPath;
421}
422
424{
425 return m_currentStart;
426}
427
429{
430 return m_currentEnd;
431}
432
434{
435 return m_initialSegment->Layers().Start();
436}
437
438
440{
441 if( m_lastLength )
442 return m_lastLength;
443 else
444 return origPathLength();
445}
446
447
449{
450 if( m_lastDelay )
451 return m_lastDelay;
452 else
453 return origPathDelay();
454}
455
456
461
462}
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).
VECTOR2I getSnappedStartPoint(LINKED_ITEM *aStartItem, VECTOR2I aStartPoint)
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:240
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,...)
int clearance
Casted dyn_cast(From aObject)
A lightweight dynamic downcast.
Definition typeinfo.h:60
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:687