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
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
###############################################################################
# Copyright (c) 2014-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
#       Get Control Path
###############################################################################


# -----------------------------------------------------------------------------
# _getCtrlTop - Get the top module of the design.
# -----------------------------------------------------------------------------
#
proc _getCtrlTop {db topName} {
    if {$topName == {}} {
        set top {}
        $db foreach top t {
            if {$top == {}} {
                set top $t
            } else {
                zmessage print WAR "Multiple tops, using [$db oid oname $top]"
                break
            }
        }
        if {$top == {}} {
            error "No top module found."
        }
    } else {
        set top [$db search top $topName]
        if {[$db oid isnull $top]} {
            error "Top module '$topName' not found."
        }
    }

    return $top
}


# -----------------------------------------------------------------------------
# _searchObj - Search for an object with the given name.
# -----------------------------------------------------------------------------
#
proc _searchObj {db type name} {
    global gcpConfig

    set top $gcpConfig(top)
    set port {}

    if {[catch {
        set port [$db oid createFromString ${type}Bus $top $name .]
    } msg]} {
        if {[catch {
            set port [$db oid createFromString $type $top $name .]
        } msg]} {
            zmessage print WAR "Object '$name' not found (ignored)."
        }
    }
    return $port
}


# -----------------------------------------------------------------------------
# _parseConfig - Parse the config file.
# -----------------------------------------------------------------------------
#
proc _parseConfig {db fname} {
    global gcpConfig

    ##
    # Read the config file line by line
    #
    set lineNo 0
    set in [open $fname "r"]
    while {![eof $in]} {
        ##
        # Increment line number
        #
        incr lineNo

        ##
        # Get the next line in the file.
        #
        set line [string trim [gets $in]]

        ##
        # Ignore empty lines
        #
        if {$line == {}} {
            continue
        }

        set keyword [lindex $line 0]
        set data    [lrange $line 1 end]
        switch -- $keyword {
            "add_pi"      -
            "add_po"      {
                set key [string range $keyword 4 end]
                foreach portName $data {
                    set port [_searchObj $db port $portName]
                    if {$port != {}} {
                        lappend gcpConfig($key) $port
                    }
                }
            }
            "add_control" {
                set key [string range $keyword 4 end]
                foreach netName $data {
                    set net [_searchObj $db net $netName]
                    if {$net != {}} {
                        lappend gcpConfig($key) $net
                    }
                }
            }
            "set_pi_constraint" {
                set portName [lindex $data 0]
                set value    [lindex $data 1]
                if {($value != 0) && ($value != 1)} {
                    set    msg "Unexpected set_pi_constraint value '$value' at "
                    append msg "$netName (ignored.)"
                    zmessage print WAR $msg
                    continue
                }
                set port [_searchObj $db port $portName]
                if {$port != {}} {
                    lappend gcpConfig(constraint) $port $value
                }
            }
            default {}
        }
    }
}


# -----------------------------------------------------------------------------
# _getOutputValue - Get the value at the output pin of the input pin.
# -----------------------------------------------------------------------------
#
proc _getOutputValue {db oid value} {
    switch -- [$db primFuncOf $oid] {
        "BUF" {
            return $value
        }
        "INV" {
            return [expr {!$value}]
        }
        "OR"  {
            if {$value == "1"} {
                return $value
            }
        }
        "AND" {
            if {$value == "0"} {
                return $value
            }
        }
        default {}
    }

    return {}
}


# -----------------------------------------------------------------------------
# _setConstraints - Set the constraints at all given nets/ports.
# -----------------------------------------------------------------------------
#
proc _setConstraints {db} {
    global gcpConfig

    ##
    # Flag all MUX with green and their select ports with blue.
    #
    $db foreach cell cell {
        switch -glob -- [$db primFuncOf $cell] {
            "WIDE_MUX" -
            "MUX" {
                set select [$db getFuncPort $cell -name "select"]
                $db flag $select set blue
                $db flag $cell   set green
            }
        }
    }

    foreach {port value} $gcpConfig(constraint) {
        if {[$db oid type $port] != "port"} {
            zms "w" "pi_constraint $port is not a port (ignored)."
            continue
        }

        ##
        # Find all paths to the target.
        #
        set res [$db cone -paths -out -targetFlaggedPort blue $port]

        ##
        # Set the given constraint value at the target pin.
        #
        foreach path $res {
            set curValue $value
            foreach oid [lrange $path 1 end] {
                if {[$db oid type $oid] == "port"} {
                    continue
                }
                if {[$db isModule $oid]} {
                    continue
                }
                if {[$db directionOf $oid] == "output"} {
                    continue
                }

                if {[$db flag [$db down $oid] is blue]} {
                    $db flatattr $oid set "constraint=$curValue"
                    $db flatflag [$db oid convertTo inst $oid] set white
                    break
                }

                set curValue [_getOutputValue $db $oid $curValue]
                if {$curValue == {}} {
                    zmessage print WAR "Cannot get output value of $oid"
                    break
                }
            }
        }
    }
}


# -----------------------------------------------------------------------------
# _initializeCtrl - Initialize all used flags, attributes and global arrays.
# -----------------------------------------------------------------------------
#
proc _initializeCtrl {db topName} {
    global gcpConfig gcpResult

    set gcpConfig(pi)         {}
    set gcpConfig(po)         {}
    set gcpConfig(control)    {}
    set gcpConfig(constraint) {}
    set gcpConfig(top)        [_getCtrlTop $db $topName]

    array unset gcpResult
    array   set gcpResult {}

    $db flag -db clear blue
    $db flag -db clear green
    $db flag -db clear yellow
    $db flag -db clear target

    $db flatattr $gcpConfig(top) deleteAll "constraint"
    $db flatflag $gcpConfig(top) clearall  white
}


# -----------------------------------------------------------------------------
# _getSelectPin - Return the select pin in the given path as oidList.
#                 If it is a pinBus then return a list of all members.
# -----------------------------------------------------------------------------
#
proc _getSelectPin {db oidList} {
    set selectPin {}
    array set _instHash {}

    foreach oid $oidList {
        if {[$db oid type $oid] != "pin"} {
            continue
        }

        set inst [$db oid convertTo inst $oid]
        if {[info exists _instHash($inst)]} {
            continue
        }
        set _instHash($inst) 1

        switch -- [$db primFuncOf $inst] {
            "WIDE_MUX" -
            "MUX"      -
            "SELECTOR" {
                set sel [$db getFuncPort $inst -name "select"]
                if {[$db oid isequal $sel $oid]} {
                    continue
                }

                if {$selectPin == {}} {
                    set selectPin $sel
                } else {
                    set pi [lindex $oidList   0]
                    set po [lindex $oidList end]
                    zmessage print ERR \
                        "Cannot find select pin in path from $pi -> $po"
                    return {}
                }
            }
            default {}
        }
    }

    set selectPinList {}
    if {$selectPin != {}} {
        if {[$db oid type $selectPin] == "pin"} {
            lappend selectPinList $selectPin
        } else {
            $db foreach pin $selectPin selBit {lappend selectPinList $selBit}
        }
    }

    return $selectPinList
}


# -----------------------------------------------------------------------------
# _getBitNumber - Extract the bit number from a given bus-member object.
# -----------------------------------------------------------------------------
#
proc _getBitNumber {db oid} {
    switch -- [$db oid type $oid] {
        "pin"   {set name [$db oid pname $oid]}
        "net"   {set name [$db oid oname $oid]}
        default {error "Given object cannot be a bus member."}
    }
    set pos [string last "\[" $name]
    if {$pos < 0} {
        zmessage print ERR "Internal error: cannot extract bit from '$oid'."
        return ""
    }
    return [string range $name $pos+1 end-1]
}


# -----------------------------------------------------------------------------
# _searchNonGatedPaths - Search all non-gated paths from output to input.
# -----------------------------------------------------------------------------
#
proc _searchNonGatedPaths {db} {
    global gcpConfig gcpResult

    ##
    # Flag all primary output ports as targets.
    #
    foreach po $gcpConfig(po) {
        if {[$db oid type $po] == "portBus"} {
            $db foreach port $po poPort {$db flag $poPort set target}
        } else {
            $db flag $po set target
        }
    }

    ##
    # Create a list of all primary inputs.
    #
    set piList {}
    foreach pi $gcpConfig(pi) {
        if {[$db oid type $pi] == "portBus"} {
            $db foreach port $pi piPort {lappend piList $piPort}
        } else {
            lappend piList $pi
        }
    }

    ##
    # Loop over all primary inputs and find all paths to all primary outputs
    # flagged above.
    #
    foreach pi $piList {
        set res [$db cone -paths -out -targetFlaggedPort target $pi]

        ##
        # Loop over the result and find the SELECTOR instance in the path.
        #
        foreach path $res {
            ##
            # Remove first item (depth) from path.
            #
            set path [lrange $path 1 end]

            ##
            # Get the only select pin in the path.
            #
            set selectPinList [_getSelectPin $db $path]
            if {$selectPinList == {}} {
                continue
            }

            ##
            # Add path to the result array.
            #
            set po [lindex $path end]
            set key PATH:$pi:$po
            if {![info exists gcpResult($key)]} {
                set gcpResult($key) {}
            }
            lappend gcpResult($key) $path

            ##
            # Loop over each bit of the select pin.
            #
            array set controlBits {}
            set controlInst {}
            foreach selBit $selectPinList {
                ##
                # Search back from the select pin to all white flagged
                # instances (select port has a constraint value).
                #
                set back [$db cone -paths -in -targetFlatFlagged white $selBit]

                ##
                # Extract the control instance and the controlled bits.
                #
                foreach backPath $back {
                    set backPath [lrange $backPath 1 end]

                    ##
                    # Get the target pin and convert it to an instance.
                    #
                    set target [lindex $backPath end]
                    set inst   [$db oid convertTo inst $target]

                    ##
                    # Each target pin needs to be at the same instance.
                    #
                    if {$controlInst == {}} {
                        set controlInst $inst
                    }
                    if {$controlInst != $inst} {
                        set msg "different control inst found from '$selBit'"
                        zmessage print ERR "Internal error: $msg"
                        break
                    }

                    ##
                    # Get the select pin at the target instance. This pin needs
                    # a constraint attribute.
                    #
                    set sel [$db getFuncPort $inst -name "select"]
                    set val [$db flatattr $sel getValue "constraint"]
                    if {$val == ""} {
                        zmessage print ERR \
                            "Internal error: no constraint at select."
                        break
                    }

                    ##
                    # Extract the bit from the target pin name.
                    #
                    set bit [_getBitNumber $db $target]
                    if {$bit == ""} {
                        break
                    }

                    ##
                    # Depending on the constraint value search for
                    # the d0 or d1 input bit.
                    #
                    set pinName "d$val\[$bit\]"
                    set newStart [$db search pin $inst $pinName]
                    if {[$db oid isnull $newStart]} {
                        zmessage print ERR \
                            "Internal error: cannot find pin $pinName"
                        break
                    }
                    set controlBits($bit) [list $newStart $backPath]
                }
            }

            ##
            # Double check that the width of the select pin is the same as the
            # number of controlled bits.
            #
            set width [llength $selectPinList]
            if {[llength [array names controlBits]] != $width} {
                zmessage print ERR \
                    "Internal error: control bit count mismatch."
                break
            }

            ##
            # Add the path from the selector select bits to the constraint
            # controlled MUX.
            #
            set key CTRL:$pi:$po
            if {![info exists gcpResult($key)]} {
                set gcpResult($key) {}
            }
            foreach {bit value} [array get controlBits] {
                lappend gcpResult($key) [lindex $value 1]
            }

            ##
            # Flag each member of the control net with the yellow flag.
            #
            foreach targetObj $gcpConfig(control) {
                $db foreach net $targetObj sub {$db flag $sub set yellow}
            }

            ##
            # Search from each bit back to the control netBus.
            #
            foreach {bit value} [array get controlBits] {
                set start [lindex $value 0]

                ##
                # Exactly one path is expected.
                #
                set ctrl [$db cone -in -paths -targetFlaggedNet yellow $start]
                if {[llength $ctrl] != 1} {
                    zmessage print ERR \
                        "Internal error: found multiple control nets"
                    continue
                }

                set ctrl [lrange [lindex $ctrl 0] 1 end]

                ##
                # Get the target bit of the control netBus.
                #
                set targetPin [lindex $ctrl end]
                set targetNet {}
                if {![$db isConnected $targetPin]} {
                    zmessage print ERR \
                        "Internal error: target pin not connected."
                    break
                }
                set targetNet [$db connectedNet $targetPin]

                lappend ctrl $targetNet

                ##
                # Add the path from the constraint controlled MUX to the data.
                #
                set key DATA:$pi:$po
                if {![info exists gcpResult($key)]} {
                    set gcpResult($key) {}
                }
                lappend gcpResult($key) $ctrl
            }

            array unset controlBits
        }
    }
}


# -----------------------------------------------------------------------------
# _loadResult -
# -----------------------------------------------------------------------------
#
proc _loadResult {db pi po} {
    global gcpResult

    gui cone clear

    foreach path $gcpResult(PATH:$pi:$po) {
        gui cone append $path
    }
    foreach path $gcpResult(CTRL:$pi:$po) {
        gui cone append $path
    }
    foreach path $gcpResult(DATA:$pi:$po) {
        gui cone append $path
        set target [lindex $path end]

        set pinList {}
        $db foreach pin $target pin {lappend pinList $pin}
        gui cone append $pinList
    }
    gui cone regenerate
}


# -----------------------------------------------------------------------------
# _debugResult -
# -----------------------------------------------------------------------------
#
proc _debugResult {db pathsFile} {
    global gcpResult

    set fp ""
    if {$pathsFile ne ""} {
        set fp [open $pathsFile "wb"]
    }

    foreach key [lsort -dictionary [array names gcpResult "PATH:*"]] {
        set p  [split $key ":"]
        set pi [lindex $p 1]
        set po [lindex $p 2]

        set itemTxt "Path from [$db oid oname $pi] to [$db oid oname $po]"
        append itemTxt "  Control Bits:"

        foreach path $gcpResult(DATA:$pi:$po) {
            set target [lindex $path end]
            set bit    [_getBitNumber $db $target]
            if {$bit == ""} {
                set bit "?"
            }
            append itemTxt " $bit"
        }

        if {$fp ne ""} {
            puts $fp "$itemTxt $pi => $po:"
            puts $fp "\tPATH: $gcpResult(PATH:$pi:$po)"
            puts $fp "\tCTRL: $gcpResult(CTRL:$pi:$po)"
            puts $fp "\tDATA: $gcpResult(DATA:$pi:$po)"
        }
        gui menu command [list "DEBUG" $itemTxt] \
                           [list _loadResult $db $pi $po]
    }
    if {$fp ne ""} {
        close $fp
    }
}


# -----------------------------------------------------------------------------
# _dumpReportFile - Dump a report file with the calculated result.
# -----------------------------------------------------------------------------
#
proc _dumpReportFile {db pathsFile} {
    _debugResult $db $pathsFile
}


# =============================================================================
# GetControlPaths - This is the main procedure that calls the sub-procedures.
# =============================================================================
#
proc GetControlPaths {db topName configFile pathsFile} {
    _initializeCtrl      $db $topName
    _parseConfig         $db $configFile
    _setConstraints      $db
    _searchNonGatedPaths $db
    _dumpReportFile      $db $pathsFile
}


# -----------------------------------------------------------------------------
# Check different ways to run this script.
# -----------------------------------------------------------------------------
#
if {$argc == 2} {
    ##
    # Run in the GUI as '-userware3'.
    #
    set configFile [lindex $argv 0]
    set pathsFile  [lindex $argv 1]
    set db         [gui database get]
    GetControlPaths $db "cc_ts_sclkmux_0" $configFile $pathsFile
} elseif {$argc == 3} {
    ##
    # Run in a 'zdbsh'.
    #
    proc messageCallback {time type message file line tclScript} {
        switch -- $type {
            "x" {}
            default {
                puts "[zmessage expandType $type] $message"
            }
        }
    }
    zmessage init
    zmessage setCallback messageCallback
    zmessage setCallbackFilter {FAT ERR WAR}

    set configFile [lindex $argv 0]
    set pathsFile  [lindex $argv 1]
    set binfile    [lindex $argv 2]
    set db [zdb open $binfile]
    GetControlPaths $db "cc_ts_sclkmux_0" $configFile $pathsFile

    zmessage finit
} else {
    error "Wrong number of args."
}