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
###############################################################################
# Copyright (c) 2018-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.
# =============================================================================
#   @plugin
#       LUT Symbols
#   @namespace
#       LutSymbols
#   @section
#       Modify the Loaded Database
#   @description
#       Add a main menu entry to apply LUT/PLA symbols to LUT-style instances.
#
#       In order to be recognized as a LUT-style instance, there must be
#       `n` inputs `(2<=n<=10)`, exactly 1 output, and the instance must have an
#       attribute `INIT` with an exact bit-width of `2^n`.
#   @configuration
#   @files
#       lutSymbols.tcl
#   @example
#       demo/edif/pulpino.edf
#   @cmdline
#       -edif @example[0]
#       -userware @files[0]
#   @tag
#       netlist verilog edif fpga
###############################################################################


# =============================================================================
# Init - Initialize the plugin.
# =============================================================================
#
proc LutSymbols:Init {} {
    gui plugin addConfig LutSymbols cellPattern "lut*" text \
        "Only consider cells matching this pattern."

    ##
    # Add a menu entry.
    #
    gui menu command {"Userware" "Apply LUT/PLA Symbols"} \
        [list LutSymbols:Start]
}


# =============================================================================
# Finit - Finalize the plugin.
# =============================================================================
#
proc LutSymbols:Finit {} {
    ##
    # Undo modifications of the GUI.
    #
    gui menu removeEntry {"Userware" "Apply LUT/PLA Symbols"}
}


# =============================================================================
# Start - Determine instances of LUT cells with INIT attribute. Singlize them
#         and add PLA symbol.
# =============================================================================
#
proc LutSymbols:Start {} {
    set db [gui database get]
    if {$db == {}} {
        return
    }

    set pattern [gui plugin getConfigValue LutSymbols cellPattern]
    if {$pattern eq ""} {
        set pattern "*"
    }

    set moduleCount 0
    $db foreach module container {
        incr moduleCount
    }

    zprogress begin
    set currentModule 0
    set candidates {}
    $db foreach module container {
        zprogress push \
                [format "Process Module: %.25s" [$db oid oname $container]] \
                [expr {double([incr currentModule]) / double($moduleCount)}]
        set instanceCount [lindex [$db report sizeOf $container] 1]
        set currentInst 0
        $db foreach inst $container lut_instance {
            if {[zprogress update "" [incr currentInst] $instanceCount]} {
                break
            }

            set lut_cell [$db oid down $lut_instance]
            set name [$db oid oname $lut_cell]

            ##
            # pattern doesn't match -> skip
            #
            if {![string match -nocase $pattern $name]} {
                continue
            }

            ##
            # there's already a symbol -> skip
            #
            if {[$db attr $lut_cell getValue "@symbol"] != ""} {
                continue
            }

            ##
            # INIT attribute is missing -> skip
            #
            set init ""
            $db attr $lut_instance foreach attr {
                if {[string match -nocase "init=*" $attr]} {
                    set initValue [string range $attr 5 end]
                    break
                }
            }
            if {$initValue eq ""} {
                continue
            }

            ##
            # If the INIT value includes a width then use it.
            #
            set init_split [split $initValue "'"]
            if {[llength $init_split] == 2} {
                set init_width [lindex $init_split 0]
                if {(![string is digit  $init_width]) ||
                    (![string is entier $init_width])} {
                    continue
                }

                ##
                # check port numbers; if bad -> skip
                #
                set inputs  0
                set outputs 0
                set other   0
                $db foreach port $lut_cell port {
                    switch -exact -- [$db directionOf $port] {
                        "input" {
                            incr inputs
                        }
                        "output" {
                            incr outputs
                        }
                        default {
                            incr other
                        }
                    }
                }
                set buses 0
                $db foreach portBus $lut_cell bus {
                    incr buses
                }
                if {($buses > 0) || ($other != 0) ||
                    ($outputs != 1) || ($inputs < 2) || ($inputs > 10) ||
                    ([expr {2 ** $inputs}] != $init_width)} \
                {
                    continue
                }

                set init_hex [LutSymbols:_convert_to_hex [lindex $init_split 1]]
            } else {
                if {[string is entier 0x$initValue]} {
                    set init_hex $initValue
                } else {
                    set init_hex ""
                }
            }

            ##
            # bad INIT bit pattern -> skip
            #
            if {$init_hex eq ""} {
                continue
            }

            lappend candidates $container $lut_instance $init_hex
        }
        if {[zprogress pop] || [zprogress isinterrupted]} {
            break
        }
    }
    zprogress end

    set created_primitives 0
    foreach {container instance init} $candidates {
        set cell     [$db oid down $instance]
        set name     [$db oid oname $cell]
        set new_name $name-$init
        set new_cell [$db search cell $new_name]
        if {[$db oid isnull $new_cell]} {
            incr created_primitives
            $db load primitive $new_name UNKNOWN
            set symbol "PLA($init)"
            $db foreach port $cell port {
                set port_name [$db oid oname $port]
                set port_direction [$db directionOf $port]
                $db load port $port_name $port_direction
                append symbol " pin $port_name $port_direction"
            }
            set new_cell [$db search cell $new_name]
            $db attr $new_cell set @symbol=$symbol
        }
        $db oper changecellref $instance $new_name
    }
    if {$candidates != {}} {
        $db oper changecellref -updateOIDs
        gui database modified
        gui console print "Applied $created_primitives unique LUT/PLA symbols\
            to [llength $candidates] instances."
    } else {
        gui console print "No LUT instances found."
    }
}


# -----------------------------------------------------------------------------
# _convert_to_hex - create hex string from "b[0-1]*", "o[0-7]*", "d[0-9]*",
#                   "h[0-0a-f]*"; return "" if $s has a bad format.
# -----------------------------------------------------------------------------
#
proc LutSymbols:_convert_to_hex {s} {
    set base [string range $s 0 0]
    set s    [string tolower [string range $s 1 end]]
    set b    0
    set cs   {}
    switch -nocase -- $base {
        b {
            set b 2
            set cs 01
        }
        o {
            set b 8
            set cs 01234567
        }
        d {
            set b 10
            set cs 0123456789
        }
        h {
            set b 16
            set cs 0123456789abcdef
        }
        default {
            return {}
        }
    }

    set v 0
    foreach c [split $s {}] {
        if {($c eq "-") || ($c eq "_")} {
            continue
        }
        set i [string first $c $cs]
        if {$i < 0} {
            return {}
        }
        set v [expr {($v * $b) + $i}]
    }
    return [format %X $v]
}


# =============================================================================
# Call the initialization procedure.
# =============================================================================
#
LutSymbols:Init