File size: 5,765 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
#!/usr/bin/env -S klayout -b -r
#
# Generates a KLayout technology file (.lyt) from the Virtuoso and Tech LEF
# files
#
# The Ruby files LEFViaData.rb and KLayoutLayerMapGenerator need to be in
# ~/.klayout/ruby
#
# KLayout doesn't support script-specific command line options, so work around
# it with env vars
#
# Usage: KLayoutTechFileGenerator.rb
#

include RBA
require 'LEFViaData'
require 'KLayoutLayerMapGenerator'

#
# Class to generate the KLayout tech file
#
class KLayoutTechFileGenerator
    #
    # Initializer
    #
    # tech: KLayout technology object
    #
    def initialize(name, description, layer_properties_file)
        @tech = Technology.new()
        @tech.name = name
        @tech.description = description
        @tech.layer_properties_file = layer_properties_file
    end

    #
    # Iterates through the Virtuoso layer map and creates the layer map that
    # gets written to the tech file.
    #
    # layer map maps layer_name to a string "layer_name:gds_layer/gds_datatype"
    #
    # Caveats
    #  1) KLayout only writes out one mapping per layer, so we only add the
    #     first occurrence of the layer into the map. This is usually datatype
    #     == 0
    #
    def get_layer_map(sorted_layer_map)
        layer_map = LayerMap.new()
        sorted_layer_map.each do |layer_name, layer_list|
            is_first = true
            layer_list.each do |layer_info|
                gds_info = layer_info.gds
                if layer_name.match?(/\A\d/)
                    # single quote escaping required for layers that start
                    # with a digit
                    layer_name = "'" + layer_name + "'"
                end
                map_str = sprintf("%s:%d/%d", layer_name, gds_info.layer,
                                  gds_info.datatype)
                if is_first
                    layer_map.map(map_str)
                    is_first = false
                end
            end
        end
        return layer_map
    end

    #
    # Adds the via layer stack to the connectivity object
    #
    def add_via_connectivity(lef_file, sorted_layer_map)
        via_data = LEFViaData.new()
        via_data.read_file(lef_file)
        connectivity = @tech.component("connectivity")
        via_data.get_map().each do | via_name, via |
            layer_list = via.get_layer_list()
            lower_layer = layer_list[0]
            via_layer = layer_list[1]
            upper_layer = layer_list[2]
            connection = NetTracerConnectivity.new
            connection.name = via_name
            connection.connection(lower_layer.get_layer().get_name(),
                                  via_layer.get_layer().get_name(),
                                  upper_layer.get_layer().get_name())
            connectivity.add(connection)
        end
        # add symbolic mappings based on layers having multiple mappings
        sorted_layer_map.each do | layer_name, layer_list |
            if layer_list.length > 1
                formatted_layer_list = []
                layer_list.each do | layer_info |
                    gds_info = layer_info.gds
                    gds_data = sprintf("%d/%d", gds_info.layer,
                                       gds_info.datatype)
                    formatted_layer_list << gds_data
                end
                expr = formatted_layer_list.join("+")
                connection = NetTracerConnectivity.new
                connection.symbol(layer_name, expr)
                connectivity.add(connection)
            end
        end
    end

    #
    # Returns the Virtuoso layer map
    #
    def get_virtuoso_layer_map(file_name, layer_name_mapper)
        layer_map_gen = KLayoutLayerMapGenerator.new(layer_name_mapper)
        layer_map_gen.read_virtuoso_layer_map_file(file_name)
        sorted_layer_map = layer_map_gen.get_map()
        return sorted_layer_map
    end

    #
    # Adds the layer map to the technology object
    #
    def add_layer_map(layer_map)
        # Gets a copy of the reader options
        reader_opts = @tech.load_layout_options
        lefdef_config = reader_opts.lefdef_config
        lefdef_config.layer_map = layer_map
        # Add a placeholder LEF, so that the technology file has a lef-files
        # element. ORFS keys off this later and replaces the element with the
        # real list of LEF files during during 6_final
        lefdef_config.lef_files = [ "placeholder.lef" ]
        # Store the updated reader options back on the tech object
        @tech.load_layout_options = reader_opts
    end

    #
    # Writes the tech file
    #
    def save_tech(file_name)
        @tech.save(file_name)
    end
    
    #
    # Standalone main driver
    #
    # Uses env vars to get arguments since KLayout doesn't support
    # script-specific arguments (e.g. it thinks that all arguments are for it)
    #
    def KLayoutTechFileGenerator.main()
        name = ENV["TECH_NAME"]
        desc = ENV["TECH_DESC"]
        layer_map_file = ENV["VIRTUOSO_LAYER_MAP_FILE"]
        lef_file = ENV["TECH_LEF"]
        lyp_file = ENV["KLAYOUT_LAYER_PROPERTIES_FILE"]
        lyt_file = ENV["KLAYOUT_TECH_FILE"]
        layer_name_mapper = ENV["LAYER_NAME_MAPPER"]
        rep = KLayoutTechFileGenerator.new(name, desc, lyp_file)
        sorted_layer_map = rep.get_virtuoso_layer_map(layer_map_file,
                                                      layer_name_mapper)
        layer_map = rep.get_layer_map(sorted_layer_map)
        rep.add_layer_map(layer_map)
        rep.add_via_connectivity(lef_file, sorted_layer_map)
        rep.save_tech(lyt_file)
    end
end

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