-
-
Notifications
You must be signed in to change notification settings - Fork 491
/
sch_connection.cpp
599 lines (480 loc) · 15.5 KB
/
sch_connection.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2018 CERN
* Copyright The KiCad Developers, see AUTHORS.txt for contributors.
*
* @author Jon Evans <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <regex>
#include <wx/tokenzr.h>
#include <connection_graph.h>
#include <sch_symbol.h>
#include <sch_pin.h>
#include <sch_screen.h>
#include <project/net_settings.h>
#include <advanced_config.h>
#include <string_utils.h>
#include <sch_connection.h>
#include <boost/algorithm/string/join.hpp>
/**
*
* Buses can be defined in multiple ways. A bus vector consists of a prefix and
* a numeric range of suffixes:
*
* BUS_NAME[M..N]
*
* For example, the bus A[3..0] will contain nets A3, A2, A1, and A0.
* The BUS_NAME is required. M and N must be integers but do not need to be in
* any particular order -- A[0..3] produces the same result.
*
* Like net names, bus names cannot contain whitespace.
*
* A bus group is just a grouping of signals, separated by spaces, some
* of which may be bus vectors. Bus groups can have names, but do not need to.
*
* MEMORY{A[15..0] D[7..0] RW CE OE}
*
* In named bus groups, the net names are expanded as <BUS_NAME>.<NET_NAME>
* In the above example, the nets would be named like MEMORY.A15, MEMORY.D0, etc.
*
* {USB_DP USB_DN}
*
* In the above example, the bus is unnamed and so the underlying net names are
* just USB_DP and USB_DN.
*
*/
SCH_CONNECTION::SCH_CONNECTION( SCH_ITEM* aParent, const SCH_SHEET_PATH& aPath ) :
m_sheet( aPath ),
m_local_sheet( aPath ),
m_parent( aParent ),
m_driver( nullptr ),
m_graph( nullptr )
{
Reset();
}
SCH_CONNECTION::SCH_CONNECTION( CONNECTION_GRAPH* aGraph ) :
m_sheet( SCH_SHEET_PATH() ),
m_local_sheet( SCH_SHEET_PATH() ),
m_parent( nullptr ),
m_driver( nullptr ),
m_graph( aGraph )
{
Reset();
}
SCH_CONNECTION::SCH_CONNECTION( SCH_CONNECTION& aOther ) :
m_parent( nullptr ),
m_driver( nullptr )
{
Reset();
Clone( aOther );
}
bool SCH_CONNECTION::operator==( const SCH_CONNECTION& aOther ) const
{
// NOTE: Not comparing m_dirty or net/bus/subgraph codes
if( ( aOther.m_driver == m_driver ) &&
( aOther.m_type == m_type ) &&
( aOther.m_name == m_name ) &&
( aOther.m_sheet == m_sheet ) )
{
return true;
}
return false;
}
void SCH_CONNECTION::SetDriver( SCH_ITEM* aItem )
{
m_driver = aItem;
recacheName();
for( const std::shared_ptr<SCH_CONNECTION>& member : m_members )
member->SetDriver( aItem );
}
void SCH_CONNECTION::SetSheet( SCH_SHEET_PATH aSheet )
{
m_sheet = aSheet;
m_local_sheet = aSheet;
recacheName();
for( const std::shared_ptr<SCH_CONNECTION>& member : m_members )
member->SetSheet( aSheet );
}
bool SCH_CONNECTION::operator!=( const SCH_CONNECTION& aOther ) const
{
return !( aOther == *this );
}
void SCH_CONNECTION::ConfigureFromLabel( const wxString& aLabel )
{
m_members.clear();
m_name = aLabel;
m_local_name = aLabel;
m_local_prefix = m_prefix;
wxString prefix;
std::vector<wxString> members;
wxString unescaped = UnescapeString( aLabel );
if( NET_SETTINGS::ParseBusVector( unescaped, &prefix, &members ) )
{
m_type = CONNECTION_TYPE::BUS;
m_vector_prefix = prefix;
long i = 0;
for( const wxString& vector_member : members )
{
auto member = std::make_shared<SCH_CONNECTION>( m_parent, m_sheet );
member->m_type = CONNECTION_TYPE::NET;
member->m_prefix = m_prefix;
member->m_local_name = vector_member;
member->m_local_prefix = m_prefix;
member->m_vector_index = i++;
member->SetName( vector_member );
member->SetGraph( m_graph );
m_members.push_back( member );
}
}
else if( NET_SETTINGS::ParseBusGroup( unescaped, &prefix, &members ) )
{
m_type = CONNECTION_TYPE::BUS_GROUP;
m_bus_prefix = prefix;
// Named bus groups generate a net prefix, unnamed ones don't
if( !prefix.IsEmpty() )
prefix += wxT( "." );
for( const wxString& group_member : members )
{
// Handle alias inside bus group member list
if( auto alias = m_graph->GetBusAlias( group_member ) )
{
for( const wxString& alias_member : alias->Members() )
{
auto member = std::make_shared< SCH_CONNECTION >( m_parent, m_sheet );
member->SetPrefix( prefix );
member->SetGraph( m_graph );
member->ConfigureFromLabel( EscapeString( alias_member, CTX_NETNAME ) );
m_members.push_back( member );
}
}
else
{
auto member = std::make_shared< SCH_CONNECTION >( m_parent, m_sheet );
member->SetPrefix( prefix );
member->SetGraph( m_graph );
member->ConfigureFromLabel( group_member );
m_members.push_back( member );
}
}
}
else
{
m_type = CONNECTION_TYPE::NET;
}
recacheName();
}
void SCH_CONNECTION::Reset()
{
m_type = CONNECTION_TYPE::NONE;
m_name.Empty();
m_local_name.Empty();
m_local_prefix.Empty();
m_cached_name.Empty();
m_cached_name_with_path.Empty();
m_prefix.Empty();
m_bus_prefix.Empty();
m_suffix .Empty();
m_lastDriver = m_driver;
m_driver = nullptr;
m_members.clear();
m_dirty = true;
m_net_code = 0;
m_bus_code = 0;
m_subgraph_code = 0;
m_vector_start = 0;
m_vector_end = 0;
m_vector_index = 0;
m_vector_prefix.Empty();
}
void SCH_CONNECTION::Clone( const SCH_CONNECTION& aOther )
{
m_graph = aOther.m_graph;
// Note: m_lastDriver is not cloned as it needs to be the last driver of *this* connection
m_driver = aOther.Driver();
m_sheet = aOther.Sheet();
// Note: m_local_sheet is not cloned
m_name = aOther.m_name;
m_type = aOther.m_type;
// Note: m_local_name is not cloned if not set yet
if( m_local_name.IsEmpty() )
{
m_local_name = aOther.LocalName();
m_local_prefix = aOther.Prefix();
}
m_prefix = aOther.Prefix();
// m_bus_prefix is not cloned; only used for local names
m_suffix = aOther.Suffix();
m_net_code = aOther.NetCode();
m_bus_code = aOther.BusCode();
m_vector_start = aOther.VectorStart();
m_vector_end = aOther.VectorEnd();
// Note: m_vector_index is not cloned
m_vector_prefix = aOther.VectorPrefix();
// Note: subgraph code isn't cloned, it should remain with the original object
// Handle vector bus members: make sure local names are preserved where possible
const std::vector<std::shared_ptr<SCH_CONNECTION>>& otherMembers = aOther.Members();
if( m_type == CONNECTION_TYPE::BUS && aOther.Type() == CONNECTION_TYPE::BUS )
{
if( m_members.empty() )
{
m_members = otherMembers;
}
else
{
size_t cloneLimit = std::min( m_members.size(), otherMembers.size() );
for( size_t i = 0; i < cloneLimit; ++i )
m_members[i]->Clone( *otherMembers[i] );
}
}
else if( m_type == CONNECTION_TYPE::BUS_GROUP && aOther.Type() == CONNECTION_TYPE::BUS_GROUP )
{
if( m_members.empty() )
{
m_members = otherMembers;
}
else
{
// TODO: refactor this once we support deep nesting
for( std::shared_ptr<SCH_CONNECTION>& member : m_members )
{
auto it = std::find_if( otherMembers.begin(), otherMembers.end(),
[&]( const std::shared_ptr<SCH_CONNECTION>& aTest )
{
return aTest->LocalName() == member->LocalName();
} );
if( it != otherMembers.end() )
member->Clone( **it );
}
}
}
m_type = aOther.Type();
recacheName();
}
bool SCH_CONNECTION::IsDriver() const
{
wxASSERT( Parent() );
switch( Parent()->Type() )
{
case SCH_LABEL_T:
case SCH_GLOBAL_LABEL_T:
case SCH_HIER_LABEL_T:
case SCH_SHEET_PIN_T:
case SCH_SHEET_T:
return true;
case SCH_PIN_T:
{
const SCH_PIN* pin = static_cast<const SCH_PIN*>( Parent() );
if( const SCH_SYMBOL* symbol = dynamic_cast<const SCH_SYMBOL*>( pin->GetParentSymbol() ) )
{
// Only annotated symbols should drive nets.
return pin->IsGlobalPower() || symbol->IsAnnotated( &m_sheet );
}
return true;
}
default:
return false;
}
}
bool SCH_CONNECTION::HasDriverChanged() const
{
return m_driver != m_lastDriver;
}
void SCH_CONNECTION::ClearDriverChanged()
{
m_lastDriver = m_driver;
}
wxString SCH_CONNECTION::Name( bool aIgnoreSheet ) const
{
wxASSERT( !m_cached_name.IsEmpty() );
return aIgnoreSheet ? m_cached_name : m_cached_name_with_path;
}
wxString SCH_CONNECTION::GetNetName() const
{
wxString retv;
if( m_graph )
{
CONNECTION_SUBGRAPH* subgraph = m_graph->GetSubgraphForItem( m_parent );
if( subgraph )
retv = subgraph->GetNetName();
}
return retv;
}
void SCH_CONNECTION::recacheName()
{
m_cached_name = m_name.IsEmpty() ? wxString( wxT( "<NO NET>" ) )
: wxString( m_prefix ) << m_name << m_suffix;
bool prepend_path = true;
if( !Parent() || m_type == CONNECTION_TYPE::NONE )
prepend_path = false;
if( m_driver )
{
switch( m_driver->Type() )
{
case SCH_GLOBAL_LABEL_T:
case SCH_PIN_T:
// Pins are either power connections or belong to a uniquely-annotated
// symbol, so they don't need a path if they are driving the subgraph.
prepend_path = false;
break;
default:
break;
}
}
m_cached_name_with_path = prepend_path ? m_sheet.PathHumanReadable() << m_cached_name
: m_cached_name;
}
void SCH_CONNECTION::SetPrefix( const wxString& aPrefix )
{
m_prefix = aPrefix;
recacheName();
for( const std::shared_ptr<SCH_CONNECTION>& m : Members() )
m->SetPrefix( aPrefix );
}
void SCH_CONNECTION::SetSuffix( const wxString& aSuffix )
{
m_suffix = aSuffix;
recacheName();
for( const std::shared_ptr<SCH_CONNECTION>& m : Members() )
m->SetSuffix( aSuffix );
}
void SCH_CONNECTION::AppendInfoToMsgPanel( std::vector<MSG_PANEL_ITEM>& aList ) const
{
wxString msg, group_name, members;
std::vector<wxString> group_members;
aList.emplace_back( _( "Connection Name" ), UnescapeString( Name() ) );
if( std::shared_ptr<BUS_ALIAS> alias = m_graph->GetBusAlias( m_name ) )
{
msg.Printf( _( "Bus Alias %s Members" ), m_name );
aList.emplace_back( msg, boost::algorithm::join( alias->Members(), " " ) );
}
else if( NET_SETTINGS::ParseBusGroup( m_name, &group_name, &group_members ) )
{
for( const wxString& group_member : group_members )
{
if( std::shared_ptr<BUS_ALIAS> group_alias = m_graph->GetBusAlias( group_member ) )
{
msg.Printf( _( "Bus Alias %s Members" ), group_alias->GetName() );
aList.emplace_back( msg, boost::algorithm::join( group_alias->Members(), " " ) );
}
}
}
#if defined(DEBUG)
// These messages are not flagged as translatable, because they are only debug messages
if( IsBus() )
aList.emplace_back( wxT( "Bus Code" ), wxString::Format( "%d", m_bus_code ) );
aList.emplace_back( wxT( "Subgraph Code" ), wxString::Format( "%d", m_subgraph_code ) );
if( SCH_ITEM* driver = Driver() )
{
UNITS_PROVIDER unitsProvider( schIUScale, EDA_UNITS::MILLIMETRES );
msg.Printf( wxS( "%s at %p" ),
driver->GetItemDescription( &unitsProvider, false ),
driver );
aList.emplace_back( wxT( "Connection Source" ), msg );
}
#endif
}
bool SCH_CONNECTION::IsBusLabel( const wxString& aLabel )
{
const wxString& unescaped = UnescapeString( aLabel );
return NET_SETTINGS::ParseBusVector( unescaped, nullptr, nullptr )
|| NET_SETTINGS::ParseBusGroup( unescaped, nullptr, nullptr );
}
bool SCH_CONNECTION::MightBeBusLabel( const wxString& aLabel )
{
// Weak heuristic for performance reasons. Stronger test will be used for connectivity
wxString label = UnescapeString( aLabel );
return label.Contains( wxT( "[" ) ) || label.Contains( wxT( "{" ) );
}
const std::vector< std::shared_ptr< SCH_CONNECTION > > SCH_CONNECTION::AllMembers() const
{
std::vector< std::shared_ptr< SCH_CONNECTION > > ret( m_members );
for( const std::shared_ptr<SCH_CONNECTION>& member : m_members )
{
if( member->IsBus() )
ret.insert( ret.end(), member->Members().begin(), member->Members().end() );
}
return ret;
}
static bool isSuperSubOverbar( wxChar c )
{
return c == '_' || c == '^' || c == '~';
};
wxString SCH_CONNECTION::PrintBusForUI( const wxString& aGroup )
{
size_t groupLen = aGroup.length();
size_t i = 0;
wxString ret;
// Parse prefix
//
for( ; i < groupLen; ++i )
{
if( isSuperSubOverbar( aGroup[i] ) && i + 1 < groupLen && aGroup[i+1] == '{' )
{
i++;
continue;
}
else if( aGroup[i] == '}' )
{
continue;
}
ret += aGroup[i];
if( aGroup[i] == '{' )
break;
}
// Parse members
//
i++; // '{' character
for( ; i < groupLen; ++i )
{
if( isSuperSubOverbar( aGroup[i] ) && i + 1 < groupLen && aGroup[i+1] == '{' )
{
i++;
continue;
}
else if( aGroup[i] == '}' )
{
continue;
}
ret += aGroup[i];
if( aGroup[i] == '}' )
break;
}
return ret;
}
bool SCH_CONNECTION::IsSubsetOf( SCH_CONNECTION* aOther ) const
{
if( !aOther->IsBus() )
return false;
for( const std::shared_ptr<SCH_CONNECTION>& member : aOther->Members() )
{
if( member->FullLocalName() == FullLocalName() )
return true;
}
return false;
}
bool SCH_CONNECTION::IsMemberOfBus( SCH_CONNECTION* aOther ) const
{
if( !aOther->IsBus() )
return false;
wxString me = Name( true );
for( const std::shared_ptr<SCH_CONNECTION>& m : aOther->Members() )
{
if( m->Name( true ) == me )
return true;
}
return false;
}