File: ybuffer.tcl.in

package info (click to toggle)
qflow 1.1.37-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 3,028 kB
  • ctags: 1,285
  • sloc: ansic: 6,451; sh: 2,625; csh: 1,780; makefile: 286
file content (154 lines) | stat: -rwxr-xr-x 4,223 bytes parent folder | download | duplicates (4)
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
#!TCLSH_PATH
#-------------------------------------------------------------------------
# ybuffer --- post-process a mapped .blif file generated by yosys
#
# Note that this file handles mapped blif files ONLY.  The only statements
# allowed in the file are ".model", ".inputs", ".outputs", ".latch",
# ".gate" (or ".subckt"), and ".end".
#
# All output nets are recorded
# Buffers are inserted before each output
# The existing output name is appended with "_RAW" and becomes an
#	internal net.
#
#-------------------------------------------------------------------------
# Written by Tim Edwards October 25, 2013
# Open Circuit Design
#-------------------------------------------------------------------------

if {$argc < 3} {
   puts stderr \
	"Usage:  ybuffer.tcl input_blif_file output_blif_file variables_file"
   exit 1
}

puts stdout "Buffering all outputs of module"

set mbliffile [lindex $argv 0]
set cellname [file rootname $mbliffile]
if {"$cellname" == "$mbliffile"} {
   set mbliffile ${cellname}.blif
}

set rootname ${cellname}
set outfile [lindex $argv 1]
set varsfile [lindex $argv 2]

#-------------------------------------------------------------
# Open files for read and write

if [catch {open $mbliffile r} bnet] {
   puts stderr "Error: can't open file $mbliffile for reading!"
   exit 1
}

if [catch {open $varsfile r} vfd] {
   puts stderr "Error: can't open file $varsfile for reading!"
   exit 1
}

if [catch {open $outfile w} onet] {
   puts stderr "Error: can't open file $outfile for writing!"
   exit 1
}

#-------------------------------------------------------------
# The variables file is a UNIX tcsh script, but it can be
# processed like a Tcl script if we substitute space for '='
# in the "set" commands.  Then all the variables are in Tcl
# variable space.
#-------------------------------------------------------------

while {[gets $vfd line] >= 0} {
   set tcmd [string map {= \ } $line]
   eval $tcmd
}

#-------------------------------------------------------------
# Yosys first pass of the blif file
# Parse all outputs and remember names.
#-------------------------------------------------------------

set outputs {}
set mode none

while {[gets $bnet line] >= 0} {
   if [regexp {^.outputs[ \t]*(.*)$} $line lmatch rest] {
      set mode outputs
      set line $rest
   }

   if {$mode == "outputs"} {
      while {[regexp {^[ \t]*([^ \t]+)(.*)$} $line lmatch signame rest] > 0} {
         lappend outputs $signame
	 set line $rest
      }
      if {![regexp {^\\[ \n\t]*$} $line lmatch]} {
         set mode none
      }
   }
}
seek $bnet 0

#-------------------------------------------------------------
# Now post-process the blif file
# The main thing to remember is that internal signals will be
# outputs of flops, but external pin names have to be translated
# to their internal names by looking at the OUTPUT section.
#-------------------------------------------------------------

while {[gets $bnet line] >= 0} {
   if [regexp {^[ \t]*\.gate} $line lmatch] {
      break
   }
   puts $onet $line
}

# Add buffers

foreach signal $outputs {
   puts $onet " .gate ${bufcell} ${bufpin_in}=${signal}_RAW ${bufpin_out}=${signal}"
}

# Reorder the outputs in decreasing order, such that outputs that are substrings
# of other outputs come after those outputs.  That way it will always match to
# the longest matching output name.

set outputs [lsort -decreasing $outputs]

while {1} {

   # All references to any signal in the output list have "_RAW" appended to the name

   if [regexp {^[ \t]*\.gate} $line lmatch] {
      set fidx 0
      foreach signal $outputs {
	 while {1} {
	    set sidx [string first ${signal} $line $fidx]
	    if {$sidx < 0} {
		break
	    } else {
		set fidx [string first " " $line $sidx]
		if {$fidx < 0} {
		   if {[expr {[string length $line] - $sidx}] == [string length $signal]} {
		      set line "${line}_RAW"
		   }
		   break
		} else {
		   if {[expr {$fidx - $sidx}] == [string length $signal]} {
		      set line [string replace $line $fidx $fidx "_RAW "]
		   }
		}
	    }
	 }
      }
   }
	 
   puts $onet $line

   if [regexp {^[ \t]*.end} $line lmatch] break
   if {[gets $bnet line] < 0} break
}

close $bnet
close $onet