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
###############################################################################
# Copyright (c) 2009-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.
# =============================================================================
#   @userware
#       Engineering Change Order
#   @section
#       Modify the Loaded Database
#   @description
#       Allow simple ECO changes.
#
#       This userware assist the user to do ECO changes. The Popup menu is
#       extended to perform the following operations on insts/pins:
#
#       ```
#       Disconnect         - Disconnect a pin from a net.
#       Connect            - Connect a pin to a net.
#       Change Connections - Change all connections of an instance.
#       Change Cellref     - Change the cell referenced by an instance.
#       Replace Instance   - Replace an instance by an instance of an other
#                            cell.
#       ```
#
#       For each operation a string with the performed action is printed to the
#       Console window. Instead of this text a DC-script could be created to
#       perform the ECO action on the netlist.
#   @files
#       eco.tcl
#   @example
#       demo/verilog/gl85.v
#   @cmdline
#       -verilog @example[0]
#       -userware @files[0]
#   @tag
#       zdb gui netlist verilog
###############################################################################


# -----------------------------------------------------------------------------
# _disconnect - Disconnect a net from a pin.
# -----------------------------------------------------------------------------
#
proc _disconnect {oidList} {
    set db [gui database get]

    if {$oidList == {}} {
        return
    }

    set oid [lindex $oidList 0]
    if {[$db oid type $oid] != "pin"} {
        zmessage print ERR "'$oid' is not a pin."
        return
    }
    _doDisconnect $db $oid
}


# -----------------------------------------------------------------------------
# _doDisconnect -
# -----------------------------------------------------------------------------
#
proc _doDisconnect {db oid} {
    gui console print "Disconnect '[$db connectedNet $oid]' from '$oid'"
    $db oper disconnect $oid
    gui database modified
    gui goto -class Schem [list $oid]
    gui tree setCurrentModule $oid
}


# -----------------------------------------------------------------------------
# _connectPin - Connect a pin to net.
# -----------------------------------------------------------------------------
#
proc _connectPin {oidList} {
    set db [gui database get]

    if {$oidList == {}} {
        return
    }

    set oid [lindex $oidList 0]
    if {[$db oid type $oid] != "pin"} {
        zmessage print ERR "Selected object ($oid) is not a pin."
        return
    }
    if {[$db isConnected $oid]} {
        zmessage print ERR "'$oid' is already connected."
        return
    }
    toplevel  .con
    label     .con.lab -text "Connect $oid to:"
    listbox   .con.net -yscrollcommand ".con.ysb set" -selectmode single
    ttk::scrollbar .con.ysb -orient vertical -command ".con.net yview"
    ttk::button .con.ok  -text "Connect" -command [list _doConnect $db $oid {}]
    $db foreach net [$db parentModule $oid] net {.con.net insert end $net}
    grid .con.lab -row 0 -column 0 -sticky w    -columnspan 2
    grid .con.net -row 1 -column 0 -sticky news
    grid .con.ysb -row 1 -column 1 -sticky ns
    grid .con.ok  -row 2 -column 0 -sticky we   -columnspan 2
    grid rowconfigure    .con 1 -weight 1
    grid columnconfigure .con 0 -weight 1
}


# -----------------------------------------------------------------------------
# _doConnect -
# -----------------------------------------------------------------------------
#
proc _doConnect {db oid net} {
    if {$net == {}} {
        set index [.con.net curselection]
        if {$index == {}} {
            destroy .con
            return
        }
        set net [.con.net get $index]
        destroy .con
    }
    $db reloadModule [$db oid oname [$db parentModule $oid]]
    set netName [$db oid oname $net]
    if {[$db oid type $oid] == "pin"} {
        $db load net $netName -pin [$db oid oname $oid] [$db oid pname $oid]
    } else {
        $db load net $netName -port [$db oid oname $oid]
    }
    gui console print "Connect '$oid' to '$net'"
    gui database modified
    gui goto -class Schem [list $oid]
    gui tree setCurrentModule $oid
}


# -----------------------------------------------------------------------------
# _changeCellRef - Change the referenced Cell of an instance.
# -----------------------------------------------------------------------------
#
proc _changeCellRef {oidList} {
    set db [gui database get]

    if {$oidList == {}} {
        return
    }

    set oid [lindex $oidList 0]
    if {[$db oid type $oid] != "inst"} {
        zmessage print ERR "'$oid' is not an instance."
        return
    }
    set cname [$db oid cname $oid]
    toplevel  .cref
    label     .cref.lab  -text "Change Cellref for $oid ($cname) to:"
    listbox   .cref.cell -yscrollcommand ".cref.ysb set" -selectmode single
    ttk::scrollbar .cref.ysb  -orient vertical -command ".cref.net yview"
    ttk::button .cref.ok -text "Change" \
        -command [list _doChangeCellRef $db $oid {}]
    if {[$db isModule $oid]} {
        set cell [$db moduleOf $oid]
    } else {
        set cell [$db primitiveOf $oid]
    }
    set origInterface [_cellInterface $db $cell]
    set cellList {}
    $db foreach primitive cell {lappend cellList $cell}
    $db foreach module    cell {lappend cellList $cell}
    foreach cell $cellList {
        if {$origInterface != [_cellInterface $db $cell]} {
            continue
        }
        .cref.cell insert end [$db oid oname $cell]
    }
    grid .cref.lab  -row 0 -column 0 -sticky w    -columnspan 2
    grid .cref.cell -row 1 -column 0 -sticky news
    grid .cref.ysb  -row 1 -column 1 -sticky ns
    grid .cref.ok   -row 2 -column 0 -sticky we   -columnspan 2
    grid rowconfigure    .cref 1 -weight 1
    grid columnconfigure .cref 0 -weight 1
}


# -----------------------------------------------------------------------------
# _cellInterface -
# -----------------------------------------------------------------------------
#
proc _cellInterface {db cell} {
    set input   0
    set output  0
    set inout   0
    set unknown 0
    $db foreach port $cell port {incr [$db directionOf $port]}
    return "$input $output $inout $unknown"
}


# -----------------------------------------------------------------------------
# _doChangeCellRef -
# -----------------------------------------------------------------------------
#
proc _doChangeCellRef {db oid newCell} {
    if {$newCell == {}} {
        set index [.cref.cell curselection]
        if {$index == {}} {
            destroy .cref
            return
        }
        set newCell [.cref.cell get $index]
        destroy .cref
    }
    set oldCell [$db oid cname $oid]
    $db oper changecellref -updateOIDs $oid $newCell
    gui console print "Change cellref of '$oid ($oldCell)' to '$newCell'"
    gui database modified
    gui goto -class Schem [list $oid]
    gui tree setCurrentModule $oid
}


# -----------------------------------------------------------------------------
# _changeConn - Modify the nets connected to an instance.
# -----------------------------------------------------------------------------
#
proc _changeConn {oidList} {
    global _con

    if {$oidList == {}} {
        return
    }

    set db [gui database get]
    set oid [lindex $oidList 0]
    if {[$db oid type $oid] != "inst"} {
        zmessage print ERR "'$oid' is not an instance."
        return
    }
    toplevel  .change
    label     .change.lab  -text "Change nets connected to '$oid':"
    grid .change.lab  -row 0 -column 0 -sticky w -columnspan 2
    set nets {}
    $db foreach net [$db parentModule $oid] n {lappend nets [$db oid oname $n]}
    set i 0
    $db foreach pin $oid pin {
        label .change.p$i -text "Connect '$pin' to:"
        set pname [$db oid pname $pin]
        if {[$db isConnected $pin]} {
            set _con($pname) [$db oid oname [$db connectedNet $pin]]
        } else {
            set _con($pname) ""
        }
        ttk::combobox .change.n$i -textvariable _con($pname) -values $nets \
                                  -state readonly
        grid .change.p$i -row $i -column 0 -sticky nw
        grid .change.n$i -row $i -column 1 -sticky nwe
        incr i
    }
    ttk::button .change.ok -text "Change" -command [list _doChangeConn $db $oid]
    grid .change.ok   -row [expr {$i + 2}] -column 0 -sticky we   -columnspan 2
    grid rowconfigure    .change 1 -weight 1
    grid columnconfigure .change 1 -weight 1
}


# -----------------------------------------------------------------------------
# _doChangeConn -
# -----------------------------------------------------------------------------
#
proc _doChangeConn {db oid} {
    global _con

    $db foreach pin $oid pin {
        set pname [$db oid pname $pin]
        set old ""
        if {[$db isConnected $pin]} {
            set old [$db oid oname [$db connectedNet $pin]]
        }
        $db oper disconnect $pin
        $db reloadModule [$db oid oname [$db parentModule $oid]]
        set netName $_con($pname)
        $db load net $netName -pin [$db oid oname $pin] [$db oid pname $pin]
        gui console print \
            "Change connection of '$pin' from '$old' to '$_con($pname)'"
    }
    destroy .change
    gui database modified
    gui goto -class Schem [list $oid]
    gui tree setCurrentModule $oid
}


# -----------------------------------------------------------------------------
# _replaceInst - Replace an instance with a new instance of a different
# -----------------------------------------------------------------------------
#
proc _replaceInst {oidList} {
    set db [gui database get]

    if {$oidList == {}} {
        return
    }

    set oid [lindex $oidList 0]
    if {[$db oid type $oid] != "inst"} {
        zmessage print ERR "'$oid' is not an instance."
        return
    }
    set cname [$db oid cname $oid]
    toplevel  .new
    label     .new.lab  -text "Replace '$oid ($cname)' with an new inst of:"
    listbox   .new.cell -yscrollcommand ".new.ysb set" -selectmode single
    bind .new.cell <ButtonRelease-1> [list _showNewCon $db $oid]
    ttk::scrollbar .new.ysb  -orient vertical -command ".new.cell yview"
    $db foreach primitive cell {.new.cell insert end [$db oid oname $cell]}
    $db foreach module    cell {.new.cell insert end [$db oid oname $cell]}
    grid .new.lab  -row 0 -column 0 -sticky w    -columnspan 2
    grid .new.cell -row 1 -column 0 -sticky news
    grid .new.ysb  -row 1 -column 1 -sticky ns

    grid rowconfigure    .new 1 -weight 1
    grid columnconfigure .new 0 -weight 1
}


# -----------------------------------------------------------------------------
# _showNewCon -
# -----------------------------------------------------------------------------
#
proc _showNewCon {db oid} {
    global _net

    destroy .new.con
    set index [.new.cell curselection]
    if {$index == {}} {
        return
    }
    set cell [.new.cell get $index]
    if {[catch {set cell [$db oid create [list module $cell $cell]]}]} {
        set cell [$db oid create [list primitive $cell $cell]]
    }
    ttk::frame .new.con
    set nets {}
    $db foreach net [$db parentModule $oid] n {lappend nets [$db oid oname $n]}
    set i 0
    $db foreach port $cell port {
        label .new.con.p$i -text "Connect $port to:"
        eval tk_optionMenu .new.con.n$i _net($i) $nets
        set _net($i) [_lookupNetCon $db $oid [$db oid oname $port]]
        grid .new.con.p$i -row $i -column 0 -sticky we
        grid .new.con.n$i -row $i -column 1 -sticky we
        grid columnconfigure .new.con 1 -weight 1
        incr i
    }
    grid .new.con -row [expr {$i + 2}] -column 0 -columnspan 2 -sticky news
    ttk::button .new.con.ok -text "Change" \
        -command [list _doReplaceInst $db $oid {}]
    grid .new.con.ok -row [expr {$i + 3}] -column 0 -sticky we -columnspan 2
}


# -----------------------------------------------------------------------------
# _lookupNetCon -
# -----------------------------------------------------------------------------
#
proc _lookupNetCon {db oid pinName} {
    $db foreach pin $oid pin {
        if {![string equal $pinName [$db oid pname $pin]]} {
            continue
        }
        if {![$db isConnected $pin]} {
            continue
        }
        return [$db oid oname [$db connectedNet $pin]]
    }
    return ""
}


# -----------------------------------------------------------------------------
# _doReplaceInst -
# -----------------------------------------------------------------------------
#
proc _doReplaceInst {db oid newCell} {
    global _net

    if {$newCell == {}} {
        set index [.new.cell curselection]
        if {$index == {}} {
            destroy .new
            return
        }
        set newCell [.new.cell get $index]
    }
    set oldCell [$db oid cname $oid]
    $db reloadModule [$db oid oname [$db parentModule $oid]]
    $db flag $oid set zombie
    $db load inst [$db oid oname $oid] $newCell
    $db deleteZombies
    gui console print "Replace '$oid ($oldCell)' with a new inst of '$newCell'"
    set i 0
    $db foreach pin $oid pin {
        if {$_net($i) == ""} {incr i; continue}
        gui console print "Connect '$pin' with 'net $_net($i)'"
        $db load net $_net($i) -pin [$db oid oname $pin] [$db oid pname $pin]
        incr i
    }
    destroy .new
    gui database modified
    gui goto -class Schem [list $oid]
    gui tree setCurrentModule $oid
}


# =============================================================================
# Customize the Popup Menu.
# =============================================================================
#
gui popup append "Disconnect"         "_disconnect"
gui popup append "Connect"            "_connectPin"
gui popup append "Change Connections" "_changeConn"
gui popup append "Change Cellref"     "_changeCellRef"
gui popup append "Replace Instance"   "_replaceInst"