Merge fixes from branch 'xorn' into stable-1.10
[geda-gaf.git] / xorn / src / gaf / netlist / instance.py
blobb088b12cdaca53cde381fa777601debd21e340f8
1 # gaf.netlist - gEDA Netlist Extraction and Generation
2 # Copyright (C) 1998-2010 Ales Hvezda
3 # Copyright (C) 1998-2010 gEDA Contributors (see ChangeLog for details)
4 # Copyright (C) 2013-2020 Roland Lutz
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software Foundation,
18 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 ## \namespace gaf.netlist.instance
21 ## Building one hierarchical netlist from instantiated sheet blueprints.
23 import sys
25 ## Instantiation of a schematic page.
27 # For every schematic instantiation (either via the command line, or
28 # via the \c source= attribute), a Sheet object is created, and its
29 # \a blueprint attribute is set to the instantiated
30 # gaf.netlist.blueprint.Schematic object.
32 class Sheet:
33 def __init__(self, netlist, blueprint, instantiating_component):
34 self.netlist = netlist
35 # schematic which is traversed
36 self.blueprint = blueprint
38 self.instantiating_component = instantiating_component
40 self.components = []
41 self.components_by_blueprint = {}
42 self.local_nets = []
43 self.local_nets_by_blueprint = {}
45 self.error = blueprint.error
46 self.warn = blueprint.warn
48 netlist.sheets.append(self)
49 if instantiating_component is not None:
50 instantiating_component.subsheets.append(self)
52 # prefix which is attached to all component and net names
53 # found in this schematic
54 next_parent = self.instantiating_component
55 namespace_parts = []
56 while next_parent is not None:
57 namespace_parts.insert(0, next_parent.blueprint.refdes)
58 next_parent = next_parent.sheet.instantiating_component
59 self.namespace = tuple(namespace_parts)
61 # Starting internal netlist creation
63 for net_blueprint in self.blueprint.nets:
64 LocalNet(self, net_blueprint)
66 for component_blueprint in self.blueprint.components:
67 Component(self, component_blueprint)
69 ## %Component.
71 # Represents a component in the netlist, either read from a toplevel
72 # schematic or instantiated via a subschematic symbol.
74 class Component:
75 def __init__(self, sheet, blueprint):
76 self.sheet = sheet
77 self.blueprint = blueprint
79 # set by netlist ctor
80 self.refdes = None
82 self.cpins = [] # list of ComponentPin
83 self.cpins_by_blueprint = {}
84 self.cpins_by_number = {}
86 self.subsheets = []
88 self.error = blueprint.error
89 self.warn = blueprint.warn
91 sheet.components.append(self)
92 sheet.components_by_blueprint[blueprint] = self
94 # create component pins
95 for pin_blueprint in blueprint.pins:
96 if pin_blueprint.ob is not None and pin_blueprint.ob.data().is_bus:
97 # ignore bus pins
98 continue
100 ComponentPin(
101 self, pin_blueprint,
102 sheet.local_nets_by_blueprint[pin_blueprint.net])
104 ## Pin on a component.
106 # One such object exists for every pin on a component in the netlist.
108 class ComponentPin:
109 def __init__(self, component, blueprint, local_net):
110 ## The component object to which this pin belongs.
111 self.component = component
113 ## The blueprint Pin object of which this is an instance.
115 # \c None for an artificial pin created via the \c "net=" attribute.
116 self.blueprint = blueprint
118 #self.is_bus = blueprint is not None and blueprint.ob.data().is_bus
120 ## The LocalNet object to which this pin is connected.
122 # For real pins, this is just the net instance for the
123 # blueprint pin's net
124 self.local_net = local_net
126 self.error = blueprint.error
127 self.warn = blueprint.warn
129 component.cpins.append(self)
130 local_net.cpins.append(self)
132 assert blueprint not in component.cpins_by_blueprint
133 component.cpins_by_blueprint[blueprint] = self
135 if blueprint.number is not None:
136 component.cpins_by_number[blueprint.number] = self
138 class LocalNet:
139 def __init__(self, sheet, blueprint):
140 self.sheet = sheet
141 self.blueprint = blueprint
142 self.cpins = []
143 self.net = None
145 sheet.local_nets.append(self)
146 sheet.local_nets_by_blueprint[blueprint] = self