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 | ###############################################################################
# Copyright (c) 2011-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
# Parse TSV Files
# @section
# Load Netlist Data
# @description
# Parse TSV (tab separated value) files and load the connectivity to
# zdb.
#
# Parser for a tab-separated-value (TSV) file which looks like this:
# ```
# b block_type
# d bus_name bus_direction
# i instance_name block_type
# n is_global net_name
# p instance_name port_name
# ```
#
# where:
# ```
# b is the module_name
# d is the port_name, and its direction (input/output/bidir)
# i is the instance_name and the module type of an instance in module_name
# n is the net_name which may be global
# p is the port_name on an instance which connects to the last
# net_name being defined.
# ```
#
# The C implementation `tsv2zdb.c` of this userware can be used to speed
# up the parsing process. The TCL implementation can be adjusted to the
# user's requirements without recompiling.
# @files
# cust13/readTSV.tcl
# cust13/tsv2zdb/demo.tsv
# @cmdline
# -userware2 @files[0] @files[1]
# @tag
# zdb parser
###############################################################################
# =============================================================================
# Parse - This is the main procedure that needs to be called twice with
# pass=1 and pass=2.
# =============================================================================
#
proc TSV:Parse {db pass} {
global TSV
##
# generate spos
#
set TSV(spos) 1
##
# Open the input file.
#
set in [open $TSV(fname) "r"]
##
# Add input file to spos database.
#
if {($pass == 1) && $TSV(spos)} {
$db spos addfile $TSV(fname)
}
##
# Initialize used variables.
#
set startPos 0
set endPos 0
set lineno 0
set size [file size $TSV(fname)]
##
# Loop over each line in the input file.
#
while {![eof $in]} {
set byte [gets $in line]
if {$byte < 0} {
break
}
##
# Calculate the end position of this line.
#
set endPos [expr {$startPos + $byte}]
##
# Add line information to spos database.
#
if {($pass == 1) && $TSV(spos)} {
$db spos addline $TSV(fname) [incr lineno] $endPos
}
##
# Update the progress bar.
#
if {[zprogress update "" $endPos $size]} {
break
}
##
# Split the line at the tab character and store the type and the
# value of column 1 and column 2.
#
set lineList [split [string trim $line] \t]
set type [lindex $lineList 0]
set col1 [lindex $lineList 1]
set col2 [lindex $lineList 2]
##
# Depending on the element type call the appropriate procedure.
#
switch -- $type {
"b" {TSV:_processBlock $db $col1 $startPos $endPos $pass}
"d" {TSV:_processPort $db $col1 $col2 $startPos $endPos $pass}
"i" {TSV:_processInst $db $col1 $col2 $startPos $endPos $pass}
"n" {TSV:_processNet $db $col1 $col2 $startPos $endPos $pass}
"p" {TSV:_processPin $db $col1 $col2 $startPos $endPos $pass}
default {}
}
##
# Set the new start position to the first character in the next line.
#
set startPos [expr {$endPos + 1}]
}
##
# Close the input file.
#
close $in
##
# Add end of file to spos database.
#
if {($pass == 1) && $TSV(spos)} {
$db spos addline $TSV(fname) end $size
}
}
# -----------------------------------------------------------------------------
# _processBlock - Process the element type 'b' that looks like this:
# b block_type
# Action: create a module for each 'b' element.
# -----------------------------------------------------------------------------
#
proc TSV:_processBlock {db cellName startPos endPos pass} {
global TSV
if {$cellName == "block_type"} {
return
}
##
# Store the name of this block in the global array.
#
set TSV(lastModule) $cellName
if {$pass == 1} {
##
# Remember the cell name and load a module in zdb.
#
set TSV(cells:$cellName) 1
set TSV(inst2cell:$cellName) $cellName
set cmd [list $db load module $cellName]
if {$TSV(spos)} {
lappend cmd -spos $TSV(fname) $startPos $endPos
}
eval $cmd
} else {
##
# In pass 2 reload the module created in pass 1.
#
$db reloadModule $cellName
}
}
# -----------------------------------------------------------------------------
# _processPort - Process the element type 'd' that looks like this:
# d bus_name bus_direction
# Create an interface port at the last defined module.
# -----------------------------------------------------------------------------
#
proc TSV:_processPort {db portName dir startPos endPos pass} {
global TSV
if {$pass != 1} {
return
}
if {$portName == "bus_name"} {
return
}
set cellName $TSV(lastModule)
set zDir ""
switch -- $dir {
"bi" {set zDir "inout"}
"in" {set zDir "input"}
"out" {set zDir "output"}
default {set zDir "unknown"}
}
set TSV(bus:$cellName:$portName) [list $zDir $startPos $endPos]
if {![info exists TSV(bus:member:$cellName:$portName)]} {
set TSV(bus:member:$cellName:$portName) {}
}
}
# -----------------------------------------------------------------------------
# _processInst - Process the element type 'i' that looks like this:
# i instance_name block_type
# In pass 1 create blackbox modules for all instantiated blocks.
# In pass 2 the actual instance is created.
# -----------------------------------------------------------------------------
#
proc TSV:_processInst {db instName cellName startPos endPos pass} {
global TSV
if {$instName == "instance_name"} {
return
}
if {$pass == 1} {
set TSV(cells:$cellName) 0
set TSV(inst2cell:$instName) $cellName
set TSV(inst2cell:$cellName) $cellName
} elseif {$pass == 2} {
set cmd [list $db load inst $instName $cellName]
if {$TSV(spos)} {
lappend cmd -spos $TSV(fname) $startPos $endPos
}
eval $cmd
}
}
# -----------------------------------------------------------------------------
# _processNet - Process the element type 'n' that looks like this:
# n is_global net_name
# In pass 2 the actual net is created.
# -----------------------------------------------------------------------------
#
proc TSV:_processNet {db isGlobal netName startPos endPos pass} {
global TSV
if {$pass != 2} {
return
}
if {$isGlobal == "is_global"} {
return
}
set cmd [list $db load net $netName]
if {$TSV(spos)} {
lappend cmd -spos $TSV(fname) $startPos $endPos
}
eval $cmd
set TSV(lastNet) $netName
}
# -----------------------------------------------------------------------------
# _processPin - Process the element type 'p' that looks like this:
# p instance_name port_name
# -----------------------------------------------------------------------------
#
proc TSV:_processPin {db instName pinName startPos endPos pass} {
global TSV
if {$instName == "instance_name"} {
return
}
if {$pass == 1} {
set cellName $TSV(inst2cell:$instName)
if {[_isBusMember $pinName]} {
set busName [_getBaseName $pinName]
} else {
set busName $pinName
}
if {![info exists TSV(bus:$cellName:$busName)]} {
set TSV(bus:$cellName:$busName) [list "unknown" 0 0]
}
if {![info exists TSV(bus:member:$cellName:$busName)]} {
set TSV(bus:member:$cellName:$busName) {}
}
if {[_isBusMember $pinName]} {
lappend TSV(bus:member:$cellName:$busName) $pinName
}
} elseif {$pass == 2} {
set mod [$db search module $TSV(lastModule)]
set inst [$db search inst $mod $instName]
if {[$db oid isnull $inst]} {
if {$instName == $TSV(lastModule)} {
set cmd [list $db load net $TSV(lastNet) -port $pinName]
if {$TSV(spos)} {
lappend cmd -spos $TSV(fname) $startPos $endPos
}
eval $cmd
}
} else {
set cmd [list $db load net $TSV(lastNet) -pin $instName $pinName]
if {$TSV(spos)} {
lappend cmd -pinspos $instName $pinName \
$TSV(fname) $startPos $endPos
lappend cmd -spos $TSV(fname) $startPos $endPos
}
eval $cmd
}
}
}
# -----------------------------------------------------------------------------
# _getBaseName - Return the bus name if the given name is a member of a bus.
# -----------------------------------------------------------------------------
#
proc _getBaseName {name} {
if {![_isBusMember $name]} {
return $name
}
set last [string last "\[" $name]
return [string range $name 0 $last-1]
}
# -----------------------------------------------------------------------------
# _isBusMember - Return true if the given name is a bus member.
# -----------------------------------------------------------------------------
#
proc _isBusMember {name} {
return [regexp {.\[[0-9]+\]} $name]
}
# -----------------------------------------------------------------------------
# _createModuleInterfaces - Create the interface ports of all modules.
# All blackbox modules including their interface are
# created in this procedure as well.
# -----------------------------------------------------------------------------
#
proc _createModuleInterfaces {db} {
global TSV
set cellList [array names TSV cells:*]
set max [llength $cellList]
set c 0
foreach cellName $cellList {
##
# Update progress bar.
#
if {[zprogress update "" [incr c] $max]} {
break
}
set cellName [string range $cellName 6 end]
set mod [$db search module $cellName]
if {[$db oid isnull $mod]} {
$db load module $cellName -flag undefined
} else {
$db reloadModule $cellName
}
foreach bus [lsort [array names TSV bus:$cellName:*]] {
set busName [string range $bus [string length bus:$cellName:] end]
set members $TSV(bus:member:$cellName:$busName)
set members [lsort -unique -dictionary $members]
set width [llength $members]
set dir [lindex $TSV($bus) 0]
set from [lindex $TSV($bus) 1]
set to [lindex $TSV($bus) 2]
if {$width == 0} {
set cmd [list $db load port $busName $dir]
if {$from && $to && $TSV(spos)} {
lappend cmd -spos $TSV(fname) $from $to
}
eval $cmd
} else {
set cmd [list $db load portBus $busName $dir $width]
foreach sub $members {
lappend cmd $sub
}
if {$from && $to && $TSV(spos)} {
lappend cmd -spos $TSV(fname) $from $to
}
eval $cmd
}
}
}
}
# -----------------------------------------------------------------------------
# _guessNetBuses - Guess net buses in all modules.
# -----------------------------------------------------------------------------
#
proc _guessNetBuses {db} {
global TSV
foreach cellName [array names TSV cells:*] {
set mod [$db search module [string range $cellName 6 end]]
$db oper guessNetBus $mod \[ \]
}
}
##
# Get command line args of this Userware.
#
if {$argc != 1} {
error "Wrong # of args."
}
set TSV(fname) [lindex $argv 0]
set db [zdb new]
zprogress begin
##
# Read input file pass 1: create all modules
#
zprogress push "Read file [file tail $TSV(fname)]: Pass 1" 0.25
TSV:Parse $db 1
if {[zprogress pop]} {
return
}
zprogress push "Create module interfaces." 0.5
_createModuleInterfaces $db
if {[zprogress pop]} {
return
}
##
# Read input file pass 2: create instances and connectivity.
#
zprogress push "Read file [file tail $TSV(fname)]: Pass 2" 0.95
TSV:Parse $db 2
if {[zprogress pop]} {
return
}
##
# Try to guess net buses.
#
zprogress push "Guess net buses" 1.0
_guessNetBuses $db
if {[zprogress pop]} {
return
}
##
# Validate the created database.
#
$db validate
##
# Inform the GUI about the new database.
#
gui database changed $db
##
# $db dump -spos -index -sortPrimitives -sortModules -sortFiles \
# -sortPins $TSV(fname).dmp
#
zprogress end
|