File size: 6,769 Bytes
784ee2c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env ruby
#
# Extracts via layer stack information from the technology LEF file
#
# Usage: LEFViaData.rb -i tech_lef_file [-s sha_file] [-l layer_lef_file ]
#                     [-v via_lef_file ]
#
# This file needs to be located in ~/.klayout/ruby
#

require 'optparse'
require_relative 'LEFVia'
require_relative 'LEFLayer'

#
# Class to extract via layer stack information from technology LEF
#
class LEFViaData
    #
    # Initialization method
    #
    # via_map: stores the mapping from a key to the list of layers in the stack
    # layer_ct: current layer count - used to set layer numbers establishing
    #           layer stack order
    # start_via_re: regexp to find the start of the LEF VIA statement
    # via_layer_re: regexp to extract the layer name in the via layer stack
    # start_layer_re: regexp to find the start of the LEF LAYER statement
    # rect_re: regexp to extract rect coords
    # layer_type_re: regexp to extract LAYER TYPE
    # end_re: regexp to find the end of a section
    #
    def initialize()
        @layer_map = {}
        @layer_ct = 0
        @via_map = {}
        @start_via_re = /^\s*VIA\s+(\S+)(?i)(\s+DEFAULT)?/
        @via_layer_re = /^\s*LAYER\s+(\S+)\s*\;/
        @start_layer_re = /^\s*LAYER\s+(\S+)\s*$/
        @rect_re = /^\s*RECT\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)/
        @layer_type_re = /^\s*TYPE\s+(\S+)/
        @end_re = /^\s*END\s+\S+/
    end

    #
    # Reads the tech LEF and creates the VIA map
    #
    def read_file(file_name)
        fh = File.open(file_name, chomp: true)
        if fh
            read(fh)
            fh.close
        else
            puts "Error: can't open " + file_name
        end
    end

    #
    # Reads the content from a stream and looks for the LEF LAYER and VIA
    # statements. The layers are added to the layer_map and the vias are added
    # to the via_map
    #
    def read(fh)
        while (line = fh.gets)
            result = line.match(@start_via_re)
            if result
                via_name = result[1]
                is_default = result[2]
                via = LEFVia.new(via_name, is_default)
                read_via(fh, via)
                @via_map[via.get_name()] = via
            else
                result = line.match(@start_layer_re)
                if result
                    layer_name = result[1]
                    layer = LEFLayer.new(layer_name, @layer_ct)
                    read_layer(fh, layer)
                    @layer_map[layer_name] = layer
                    @layer_ct += 1
                end
            end
        end
    end

    #
    # Reads the LEF LAYER definition and adds it to the layer object
    #
    def read_layer(fh, layer)
        while (line = fh.gets)
            if line.match(@end_re)
                return
            end
            result = line.match(@layer_type_re)
            if result
                layer.set_type(result[1])
            end
        end
    end

    #
    # Reads the LEF VIA definition and adds it to the via object
    #
    def read_via(fh, via)
        current_layer = nil
        while (line = fh.gets)
            if line.match(@end_re)
                via.add_layer(current_layer)
                return
            end
            result = line.match(@via_layer_re)
            if result
                if current_layer
                    via.add_layer(current_layer)
                end
                current_layer = LEFVia::LEFViaLayer.new(@layer_map[result[1]])
            else
                result = line.match(@rect_re)
                if result
                    rect = [result[1].to_f, result[2].to_f, result[3].to_f, result[4].to_f]
                    current_layer.add_shape(rect)
                end
            end
        end
    end

    #
    # Accessor to get the via map
    #
    def get_map()
        return @via_map
    end

    #
    # Utility method to write out LEF VIA snippet to output stream
    #
    def write_via_lef(outfh)
        @via_map.each do | via_name, via |
            via.write_lef(outfh)
        end
    end

    #
    # Utility method to write out LEF VIA snippet to file
    #
    def write_via_lef_file(file_name)
        outfh = File.open(file_name, "w")
        write_via_lef(outfh)
        outfh.close
    end

    #
    # Utility method to write out SHA list to output stream
    #
    def write_sha(outfh)
        @via_map.each do | via_name, via |
            sha256 = via.sha256()
            outfh.puts sprintf("%s %s", sha256, via_name)
        end
    end

    #
    # Utility method to write out SHA list to output file
    #
    def write_sha_file(file_name)
        outfh = File.open(file_name, "w")
        write_sha(outfh)
        outfh.close
    end
    
    #
    # Utility method to write out LEF LAYER snippet to output stream
    #
    def write_layer_lef(outfh)
        sorted_layer_map = @layer_map.sort_by { |layer_name, layer| layer.get_number }.to_h
        sorted_layer_map.each do | layer_name, layer |
            layer.write_lef(outfh)
        end
    end

    #
    # Utility method to write out LEF LAYER snippet to output file
    #
    def write_layer_lef_file(file_name)
        outfh = File.open(file_name, "w")
        write_layer_lef(outfh)
        outfh.close
    end

    #
    # Standalone main driver
    #
    def LEFViaData.main()
        options = {}
        OptionParser.new do |opts|
            opts.banner = "Usage: LEFViaData.rb -i tech_lef_file [-s <sha_file>] [-l <layer_lef_file>] [-v <via_lef_file]"
            opts.on("-iTECH_LEF_FILE", "--input_file=TECH_LEF_FILE") do |input_file|
                options[:input_file] = input_file
            end
            opts.on("-sSHA_FILE", "--sha_file=SHA_FILE") do |sha_file|
                options[:sha_file] = sha_file
            end
            opts.on("-lLAYER_LEF_FILE", "--layer_lef_file=LAYER_LEF_FILE") do |layer_lef_file|
                options[:layer_lef_file] = layer_lef_file
            end
            opts.on("-vVIA_LEF_FILE", "--via_lef_file=VIA_LEF_FILE") do |via_lef_file|
                options[:via_lef_file] = via_lef_file
            end
        end.parse!

        if options[:input_file]
            rep = LEFViaData.new
            rep.read_file(options[:input_file])
            if options[:sha_file]
                rep.write_sha_file(options[:sha_file])
            end
            if options[:layer_lef_file]
                rep.write_layer_lef_file(options[:layer_lef_file])
            end
            if options[:via_lef_file]
                rep.write_via_lef_file(options[:via_lef_file])
            end
        end
    end
end

#
# Only call the main driver if we're calling this as a script
#
if __FILE__ == $0
    LEFViaData.main()
end