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
###############################################################################
# Copyright (c) 2020-2024 by Altair Engineering, Inc.
# All rights reserved.
#
# Altair Engineering, Inc. makes this software available as part of the Vision
# tool platform.  As long as you are a licensee of the Vision tool platform
# you may make copies of the software and modify it to be used within the
# Vision tool platform, but you must include all of this notice on any copy.
# Redistribution without written permission to any third party, with or
# without modification, is not permitted.
# Altair Engineering, Inc. does not warrant that this software is error free
# or fit for any purpose.  Altair Engineering, Inc. disclaims any liability for
# all claims, expenses, losses, damages and costs any user may incur as a
# result of using, copying or modifying the software.
# =============================================================================
#   @script
#       Functions to compute a DB diff.
###############################################################################


namespace eval DiffCommon {
    variable _Db1
    variable _Db2
    variable _Report
    variable _Report_list
    variable _IgnoreCase
    variable _IgnoreEquivalentPrimitives
    variable _NormalizeArrays
}


# =============================================================================
# Compute - Compute the differences between db1 and db2; call the report
#           functions on each difference.
# =============================================================================
#
proc DiffCommon::Compute {
    db1
    db2
    report
    report_list
    ignore_case
    ignore_equivalent_primitives
    normalize_arrays
} {
    variable _Db1
    variable _Db2
    variable _Report
    variable _Report_list
    variable _IgnoreCase
    variable _IgnoreEquivalentPrimitives
    variable _NormalizeArrays

    set _Db1 $db1
    set _Db2 $db2
    set _Report $report
    set _Report_list $report_list
    set _IgnoreCase $ignore_case
    set _IgnoreEquivalentPrimitives $ignore_equivalent_primitives
    set _NormalizeArrays $normalize_arrays

    $_Db1 oper tsort
    $_Db2 oper tsort

    _foreach CELLS {} primitive
    _foreach CELLS {} module
}


# -----------------------------------------------------------------------------
# _foreach - Check difference of $type list in $obj1 and $obj2
# -----------------------------------------------------------------------------
#
proc DiffCommon::_foreach {
    class
    path
    type
    {obj1 {}}
    {obj2 {}}
} {
    variable _Db1
    variable _Db2
    variable _Report

    set list1 [dict create]
    set list2 [dict create]
    if {($obj1 != {}) && ($obj2 != {})} {
        $_Db1 foreach $type $obj1 oid {
            dict set list1 [_normalize [$_Db1 oid oname $oid]] $oid
        }
        $_Db2 foreach $type $obj2 oid {
            dict set list2 [_normalize [$_Db2 oid oname $oid]] $oid
        }
    } else {
        $_Db1 foreach $type oid {
            dict set list1 [_normalize [$_Db1 oid oname $oid]] $oid
        }
        $_Db2 foreach $type oid {
            dict set list2 [_normalize [$_Db2 oid oname $oid]] $oid
        }
    }

    dict for {name oid} $list1 {
        if {![dict exists $list2 $name]} {
            {*}$_Report \
                $class \
                $path "$type $name" \
                "" "missing" \
                $oid {} \
                $obj1 $obj2
        } else {
            set other [dict get $list2 $name]
            set path2 $path
            lappend path2 "$type $name" $oid $other
            _object $path2 $oid $other
        }
    }
    dict for {name oid} $list2 {
        if {![dict exists $list1 $name]} {
            {*}$_Report \
                $class \
                $path "$type $name" \
                "missing" "" \
                {} $oid \
                $obj1 $obj2
        }
    }
}


# -----------------------------------------------------------------------------
# _property - Check for difference in property $what of $obj1 and $obj2.
# -----------------------------------------------------------------------------
#
proc DiffCommon::_property {
    class
    path
    what
    obj1
    obj2
} {
    variable _Db1
    variable _Db2
    variable _Report

    if {$what == "pg"} {
        foreach flag {power ground negpower} {
            set val1 [$_Db1 flag $obj1 is $flag]
            set val2 [$_Db2 flag $obj2 is $flag]
            if {$val1 != $val2} {
                {*}$_Report \
                    $class \
                    $path "property $what/$flag" \
                    $val1 $val2 \
                    $obj1 $obj2
            }
        }
    } else {
        set val1 [$_Db1 $what $obj1]
        set val2 [$_Db2 $what $obj2]
        if {$val1 != $val2} {
            {*}$_Report \
                $class \
                $path "property $what" \
                $val1 $val2 \
                $obj1 $obj2
        }
    }
}


# -----------------------------------------------------------------------------
# _members - Check for difference in the members of type $what of $obj1
#            and $obj2.
# -----------------------------------------------------------------------------
#
proc DiffCommon::_members {
    class
    path
    what
    obj1
    obj2
} {
    variable _Db1
    variable _Db2
    variable _Report_list

    set keys1 {}
    set list1 {}
    $_Db1 foreach $what $obj1 oid {
        set name [_normalize [$_Db1 oid oname $oid]]
        lappend keys1 $name
        lappend list1 $oid
    }

    set keys2 {}
    set list2 {}
    $_Db2 foreach $what $obj2 oid {
        set name [_normalize [$_Db2 oid oname $oid]]
        lappend keys2 $name
        lappend list2 $oid
    }

    if {$keys1 != $keys2} {
        {*}$_Report_list \
            $class \
            $path "${what}s" \
            $list1 $list2 \
            $obj1 $obj2 \
            1
    }
}


# -----------------------------------------------------------------------------
# _connections - Check for difference in the connections of the nets $obj1
#                and $obj2.
# -----------------------------------------------------------------------------
#
proc DiffCommon::_connections {
    class
    path
    net1
    net2
} {
    variable _Db1
    variable _Db2
    variable _Report_list

    set list1 [dict create]
    $_Db1 foreach pin $net1 oid {
        if {[$_Db1 oid type $oid] eq "pin"} {
            set key [list \
                [_normalize [$_Db1 oid pname $oid]] \
                [_normalize [$_Db1 oid oname $oid]] \
            ]
        } else {
            set key [_normalize [$_Db1 oid pname $oid]]
        }
        dict set list1 $key $oid
    }

    set list2 [dict create]
    $_Db2 foreach pin $net2 oid {
        if {[$_Db2 oid type $oid] eq "pin"} {
            set key [list \
                [_normalize [$_Db2 oid pname $oid]] \
                [_normalize [$_Db2 oid oname $oid]] \
            ]
        } else {
            set key [_normalize [$_Db2 oid pname $oid]]
        }
        dict set list2 $key $oid
    }

    set only1 {}
    dict for {key oid} $list1 {
        if {![dict exists $list2 $key]} {
            lappend only1 $oid
        }
    }

    set only2 {}
    dict for {key oid} $list2 {
        if {![dict exists $list1 $key]} {
            lappend only2 $oid
        }
    }

    if {([llength $only1] != 0) || ([llength $only2] != 0)} {
        {*}$_Report_list \
            $class \
            $path "Connected pins/ports" \
            $only1 $only2 \
            $net1 $net2 \
            0
    }
}


# -----------------------------------------------------------------------------
# _object - Check for difference in objects $obj1 and $obj2.
# -----------------------------------------------------------------------------
#
proc DiffCommon::_object {
    path
    obj1
    obj2
} {
    variable _Db1

    set type [$_Db1 oid type $obj1]

    switch -exact -- $type {
        primitive {
            _primitive $path $obj1 $obj2
        }
        module  {
            _module $path $obj1 $obj2
        }
        port  {
            _port $path $obj1 $obj2
        }
        portBus  {
            _portBus $path $obj1 $obj2
        }
        inst  {
            _inst $path $obj1 $obj2
        }
        net  {
            _net $path $obj1 $obj2
        }
        netBus {
            _netBus $path $obj1 $obj2
        }
        default {
            error "DiffCommon::_object: bad type '$type'"
        }
    }
}


# -----------------------------------------------------------------------------
# _primitive - Check for difference in primitives $obj1 and $obj2.
# -----------------------------------------------------------------------------
#
proc DiffCommon::_primitive {
    path
    obj1
    obj2
} {
    _property CELLS      $path primFuncOf $obj1 $obj2
    _foreach  INTERFACES $path port       $obj1 $obj2
    _foreach  INTERFACES $path portBus    $obj1 $obj2
}


# -----------------------------------------------------------------------------
# _module - Check for difference in modules $obj1 and $obj2.
# -----------------------------------------------------------------------------
#
proc DiffCommon::_module {
    path
    obj1
    obj2
} {
    variable _Db1
    variable _Db2
    variable _Report

    set blackbox1 [$_Db1 flag $obj1 is undefined]
    set blackbox2 [$_Db2 flag $obj2 is undefined]
    if {$blackbox1 && (!$blackbox2)} {
        {*}$_Report \
            CELLS \
            $path "definition" \
            "is undefined" "has definition" \
            $obj1 $obj2
    } elseif {(!$blackbox1) && $blackbox2} {
        {*}$_Report \
            CELLS \
            $path "definition" \
            "has definition" "is undefined" \
            $obj1 $obj2
    }
    _property CELLS        $path primFuncOf $obj1 $obj2
    _foreach  INTERFACES   $path port       $obj1 $obj2
    _foreach  INTERFACES   $path portBus    $obj1 $obj2
    _foreach  INSTANCES    $path inst       $obj1 $obj2
    _foreach  CONNECTIVITY $path net        $obj1 $obj2
    _foreach  CONNECTIVITY $path netBus     $obj1 $obj2
}


# -----------------------------------------------------------------------------
# _port - Check for difference in ports $obj1 and $obj2.
# -----------------------------------------------------------------------------
#
proc DiffCommon::_port {
    path
    obj1
    obj2
} {
    _property INTERFACES $path directionOf $obj1 $obj2
}


# -----------------------------------------------------------------------------
# _portBus - Check for difference in portbuses $obj1 and $obj2.
# -----------------------------------------------------------------------------
#
proc DiffCommon::_portBus {
    path
    obj1
    obj2
} {
    _property INTERFACES $path directionOf $obj1 $obj2
    _property INTERFACES $path widthOf     $obj1 $obj2
    _members  INTERFACES $path port        $obj1 $obj2
}


# -----------------------------------------------------------------------------
# _inst - Check for instances $obj1 and $obj2.
# -----------------------------------------------------------------------------
#
proc DiffCommon::_inst {
    path
    obj1
    obj2
} {
    variable _Db1
    variable _Db2
    variable _Report
    variable _IgnoreEquivalentPrimitives

    set cell1     [$_Db1 oid down $obj1]
    set cell2     [$_Db2 oid down $obj2]
    set cellName1 [_normalize [$_Db1 oid oname $cell1]]
    set cellName2 [_normalize [$_Db2 oid oname $cell2]]

    if {$cellName1 != $cellName2} {
        if {$_IgnoreEquivalentPrimitives && \
            (![$_Db1 isModule $cell1]) && \
            (![$_Db2 isModule $cell2]) && \
            ([$_Db1 primFuncOf $cell1] == [$_Db2 primFuncOf $cell2]) && \
            [_same_ports $cell1 $cell2] \
        } {
            ##
            # Different primitives with the same function are instantiated
            # => Ignore.
            #
            return
        }

        {*}$_Report \
            INSTANCES \
            $path "inst [$_Db1 oid oname $obj1]" \
            "instantiates $cell1" "instantiates $cell2" \
            $obj1 $obj2
    }
}


# -----------------------------------------------------------------------------
# _net - Check for difference in nets $obj1 and $obj2.
# -----------------------------------------------------------------------------
#
proc DiffCommon::_net {
    path
    obj1
    obj2
} {
    _property    CONNECTIVITY $path value $obj1 $obj2
    _property    CONNECTIVITY $path pg    $obj1 $obj2
    _connections CONNECTIVITY $path $obj1 $obj2
}


# -----------------------------------------------------------------------------
# _netBus - Check for difference in netbuses $obj1 and $obj2.
# -----------------------------------------------------------------------------
#
proc DiffCommon::_netBus {
    path
    obj1
    obj2
} {
    _property CONNECTIVITY $path value   $obj1 $obj2
    _property CONNECTIVITY $path widthOf $obj1 $obj2
    _members  CONNECTIVITY $path net     $obj1 $obj2
}


# -----------------------------------------------------------------------------
# _same_ports - Check is both cells have the same ports.
# -----------------------------------------------------------------------------
#
proc DiffCommon::_same_ports {
    cell1
    cell2
} {
    variable _Db1
    variable _Db2

    set ports1 [list]
    $_Db1 foreach port $cell1 p {
        lappend ports1 [_normalize [$_Db1 oid oname $p]] [$_Db1 directionOf $p]
    }

    set ports2 [list]
    $_Db2 foreach port $cell2 p {
        lappend ports2 [_normalize [$_Db2 oid oname $p]] [$_Db2 directionOf $p]
    }

    return [expr {$ports1 == $ports2}]
}


# -----------------------------------------------------------------------------
# _normalize - Normalize the given name.
# -----------------------------------------------------------------------------
#
proc DiffCommon::_normalize {
    name
} {
    variable _IgnoreCase
    variable _NormalizeArrays

    set normalized $name

    if {$_IgnoreCase} {
        set normalized [string tolower $normalized]
    }

    if {$_NormalizeArrays} {
        set match 1
        while {$match} {
            set match 0
            foreach re {
                {^(.*)<(\d+(?::\d+)?)>(.*)$}
                {^(.*)_(\d+(?::\d+)?)_(.*)$}
                {^(.*)\*(\d+(?::\d+)?)\*(.*)$}
                {^(.*)\((\d+(?::\d+)?)\)(.*)$}
                {^(.*)\{(\d+(?::\d+)?)\}(.*)$}
            } {
                if {[regexp -- $re $normalized -> prefix index suffix]} {
                    set match 1
                    set normalized $prefix\[$index\]$suffix
                }
            }
        }
    }

    return $normalized
}