File size: 8,924 Bytes
34292e0 | 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 | """LVS decks in Python on the PyPI klayout module.
KLayout's LVS language is its DRC language plus a netlist comparer
(src/lvs/lvs/built-in-macros/_lvs_netter.rb). This module adds to
klayout_drc.Deck what ORFS's LVS decks use: device extraction, the
schematic, the comparer's configuration and the comparison, with the
calls the LVS language makes, in its order: what is configured before
the netlist is extracted runs right after the extraction, what is
configured for the comparer runs when it is made.
"""
import argparse
import os
import sys
import klayout_drc
from klayout_drc import db
class LvsDeck(klayout_drc.Deck):
def __init__(self, in_gds, report_file, cdl_file, top_cell, threads=1):
super().__init__(in_gds, report_file, "LVS", mode="deep", threads=threads)
self.top_cell = top_cell
self.cdl_file = cdl_file
self.scaling = 1.0
self.post_extract = []
self.comparer_config = []
self.read_schematic(db.NetlistSpiceReader())
def read_schematic(self, reader):
"""Reads the CDL with a reader, as the LVS language's
schematic(file, reader)."""
self.info("Reading netlist: %s .." % self.cdl_file)
self.schematic = db.Netlist()
self.schematic.read(self.cdl_file, reader)
def _l2n_register(self, data):
if self.l2n is None:
self.l2n = db.LayoutVsSchematic(self.dss)
self.l2n.name = "LVS"
self.l2n.device_scaling = self.scaling
self.l2n_layers = {}
super()._l2n_register(data)
def device_scaling(self, factor):
self.scaling = factor
if self.l2n is not None:
self.l2n.device_scaling = factor
def extract_devices(self, extractor, layers):
"""layers: terminal or layer name to Layer, as the LVS language's
hash; registered in the order of the sorted names, as it does."""
selection = {}
for name in sorted(layers):
self._l2n_register(layers[name].data)
selection[name] = layers[name].data
self.l2n.extract_devices(extractor, selection)
return extractor.device_class()
def connect_global(self, layer, name):
self._l2n_register(layer.data)
self.l2n.connect(layer.data)
self.l2n.connect_global(layer.data, name)
def _l2n_data(self):
if not self.l2n.is_extracted():
super()._l2n_data()
for f in self.post_extract:
f(self.l2n)
return self.l2n
def netlist(self):
return self._l2n_data().netlist()
def _extracted(self):
return self.l2n is not None and self.l2n.is_extracted()
def _now_or_after_extraction(self, f):
if self._extracted():
f(self.l2n)
else:
self.post_extract.append(f)
def split_gates(self, device_name, circuit_pattern="*"):
def f(l2n):
dc = l2n.netlist().device_class_by_name(device_name)
if dc is None:
raise ValueError("'%s' is not a valid device name" % device_name)
for c in l2n.netlist().circuits_by_name(circuit_pattern):
dc.join_split_gates(c)
self._now_or_after_extraction(f)
def tolerance(self, device_class_name, parameter_name, absolute=0.0, relative=0.0):
def f(l2n):
dc = l2n.netlist().device_class_by_name(device_class_name)
if dc is not None and dc.has_parameter(parameter_name):
ep = db.EqualDeviceParameters(
dc.parameter_id(parameter_name), float(absolute), float(relative)
)
if dc.equal_parameters is None:
dc.equal_parameters = ep
else:
dc.equal_parameters += ep
self._now_or_after_extraction(f)
def same_circuits(self, a, b):
def f(comparer):
circuit_a = a and self.netlist().circuit_by_name(a)
circuit_b = b and self.schematic.circuit_by_name(b)
if circuit_a and circuit_b:
comparer.same_circuits(circuit_a, circuit_b)
self.comparer_config.append(f)
def equivalent_pins(self, circuit, *pins):
def f(comparer):
circuit_b = self.schematic.circuit_by_name(circuit)
if circuit_b is None:
return
by_index = list(circuit_b.each_pin())
ids = []
for p in pins:
if isinstance(p, str):
pin = circuit_b.pin_by_name(p)
else:
pin = by_index[int(p)]
if pin is None:
raise ValueError(
"Not a valid pin in circuit '%s' in 'equivalent_pins': %s"
% (circuit, p)
)
ids.append(pin.id())
comparer.equivalent_pins(circuit_b, ids)
self.comparer_config.append(f)
def max_res(self, value):
v = float(value)
def f(comparer):
comparer.max_resistance = v
self.comparer_config.append(f)
def min_caps(self, value):
v = float(value)
def f(comparer):
comparer.min_capacitance = v
self.comparer_config.append(f)
def _comparer(self):
comparer = db.NetlistComparer()
for f in self.comparer_config:
f(comparer)
return comparer
def align(self):
nl_a, nl_b = self.netlist(), self.schematic
comparer = self._comparer()
unmatched_a = comparer.unmatched_circuits_a(nl_a, nl_b)
top = self.l2n.internal_top_cell().name
if any(c.name == top for c in unmatched_a):
raise ValueError(
"Can't find a schematic counterpart for the top cell %s" % top
)
for c in unmatched_a:
self.info("Flatten layout cell (no schematic): %s" % c.name)
nl_a.flatten_circuits(unmatched_a)
unmatched_b = comparer.unmatched_circuits_b(nl_a, nl_b)
for c in unmatched_b:
self.info("Flatten schematic circuit (no layout): %s" % c.name)
nl_b.flatten_circuits(unmatched_b)
def compare(self):
self.netlist()
self.l2n.reference = self.schematic
return self.l2n.compare(self._comparer())
def write(self):
if self.l2n is not None and self.l2n.xref() is not None:
self.info("Writing LVS database: %s .." % self.report_file)
self.l2n.write(self.report_file, True)
# Device classes and factories handed to the extractors; the klayout
# objects hold them by pointer, so Python keeps them alive.
_keep = []
class _DeviceClassFactory(db.DeviceClassFactory if db else object):
"""A device class factory for a class, as the DRC language's
CustomDeviceClassFactory."""
def __init__(self, cls):
super().__init__()
self.cls = cls
def create_class(self):
dc = self.cls()
_keep.append(dc)
return dc
def _factory(cls):
if cls is None:
return None
f = _DeviceClassFactory(cls)
_keep.append(f)
return f
def mos4(name, cls=None):
return db.DeviceExtractorMOS4Transistor(name, False, _factory(cls))
def bjt3(name, cls=None):
return db.DeviceExtractorBJT3Transistor(name, _factory(cls))
def bjt4(name, cls=None):
return db.DeviceExtractorBJT4Transistor(name, _factory(cls))
def diode(name, cls=None):
return db.DeviceExtractorDiode(name, _factory(cls))
def resistor(name, sheet_rho, cls=None):
return db.DeviceExtractorResistor(name, sheet_rho, _factory(cls))
def parse_args(argv):
parser = argparse.ArgumentParser(
description="Run a Python LVS deck on a GDS/OAS file against a CDL"
" netlist and write a KLayout LVS database (.lvsdb)."
)
parser.add_argument("--deck", required=True, help="the deck, a .py file")
parser.add_argument("--in-gds", required=True)
parser.add_argument("--cdl-file", required=True)
parser.add_argument("--top-cell", required=True)
parser.add_argument("--report-file", required=True)
return parser.parse_args(argv)
def main(argv):
args = parse_args(argv)
klayout_drc.require_db()
module = klayout_drc.load_deck(args.deck)
deck = LvsDeck(
args.in_gds,
args.report_file,
args.cdl_file,
args.top_cell,
threads=module.THREADS,
)
match = module.lvs(deck)
deck.write()
if match:
print("INFO : Congratulations! Netlists match.")
else:
print("ERROR : Netlists don't match")
return 0
if __name__ == "__main__":
# The deck imports these modules by name; run those, not __main__,
# so the objects it passes are the ones checked here.
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import klayout_lvs
sys.exit(klayout_lvs.main(sys.argv[1:]))
|