11"""
22This module provides functions to generate a raw netlist semi-compatible with gdsfactory from a hdl21 module object.
33"""
4- import yaml
4+
55import hdl21 as h
6+ import yaml
67
78__all__ = [
8- ' ParsedProtoVLSIR' ,
9- ' generate_raw_netlist_dict_from_module' ,
10- ' generate_raw_yaml_from_module'
9+ " ParsedProtoVLSIR" ,
10+ " generate_raw_netlist_dict_from_module" ,
11+ " generate_raw_yaml_from_module" ,
1112]
1213
1314ParsedProtoVLSIR = dict
@@ -63,44 +64,46 @@ def _parse_connections(proto_dict: ParsedProtoVLSIR) -> dict:
6364 connections = {}
6465
6566 # Extract the instances and their connections
66- for module in proto_dict .get (' modules' , []):
67- for instance in module .get (' instances' , []):
68- instance_name = instance [' name' ]
69- for connection in instance .get (' connections' , []):
70- portname = connection [' portname' ]
71- target_signal = connection [' target' ][0 ][' sig' ]
67+ for module in proto_dict .get (" modules" , []):
68+ for instance in module .get (" instances" , []):
69+ instance_name = instance [" name" ]
70+ for connection in instance .get (" connections" , []):
71+ portname = connection [" portname" ]
72+ target_signal = connection [" target" ][0 ][" sig" ]
7273 connection_key = f"{ instance_name } ,{ portname } "
7374 # Find the target instance and port
74- target_instance_port = _find_target_instance_port (proto_dict , target_signal , instance_name )
75+ target_instance_port = _find_target_instance_port (
76+ proto_dict , target_signal , instance_name
77+ )
7578 if target_instance_port :
7679 connections [connection_key ] = target_instance_port
7780
7881 return connections
7982
8083
81- def _find_target_instance_port (proto_dict : ParsedProtoVLSIR ,
82- target_signal ,
83- current_instance_name ):
84+ def _find_target_instance_port (
85+ proto_dict : ParsedProtoVLSIR , target_signal , current_instance_name
86+ ):
8487 """
8588 Find the target instance and port of the target signal in the proto_dict.
8689 """
8790 # Search in the same module
88- for module in proto_dict .get (' modules' , []):
89- for instance in module .get (' instances' , []):
90- if instance [' name' ] == current_instance_name :
91+ for module in proto_dict .get (" modules" , []):
92+ for instance in module .get (" instances" , []):
93+ if instance [" name" ] == current_instance_name :
9194 continue
92- for connection in instance .get (' connections' , []):
93- if connection [' target' ][0 ][' sig' ] == target_signal :
95+ for connection in instance .get (" connections" , []):
96+ if connection [" target" ][0 ][" sig" ] == target_signal :
9497 return f"{ instance ['name' ]} ,{ connection ['portname' ]} "
9598 # Search in external modules
96- for ext_module in proto_dict .get (' ext_modules' , []):
97- for port in ext_module .get (' ports' , []):
98- if port [' signal' ] == target_signal :
99- for instance in module .get (' instances' , []):
100- if instance [' name' ] == current_instance_name :
99+ for ext_module in proto_dict .get (" ext_modules" , []):
100+ for port in ext_module .get (" ports" , []):
101+ if port [" signal" ] == target_signal :
102+ for instance in module .get (" instances" , []):
103+ if instance [" name" ] == current_instance_name :
101104 continue
102- for connection in instance .get (' connections' , []):
103- if connection [' target' ][0 ][' sig' ] == target_signal :
105+ for connection in instance .get (" connections" , []):
106+ if connection [" target" ][0 ][" sig" ] == target_signal :
104107 return f"{ instance ['name' ]} ,{ connection ['portname' ]} "
105108
106109 return None
@@ -113,9 +116,9 @@ def _generate_top_level_connections(proto_dict: ParsedProtoVLSIR):
113116 top_level_connections = {}
114117
115118 # Iterate over the top-level module ports
116- for module in proto_dict .get (' modules' , []):
117- for port in module .get (' ports' , []):
118- port_signal = port [' signal' ]
119+ for module in proto_dict .get (" modules" , []):
120+ for port in module .get (" ports" , []):
121+ port_signal = port [" signal" ]
119122 connection = _find_port_connection (proto_dict , port_signal )
120123 if connection :
121124 top_level_connections [port_signal ] = connection
@@ -128,11 +131,11 @@ def _find_port_connection(proto_dict: ParsedProtoVLSIR, port_signal):
128131 Find the connection of the port signal in the proto_dict.
129132 """
130133 # Search within the module instances
131- for module in proto_dict .get (' modules' , []):
132- for instance in module .get (' instances' , []):
133- instance_name = instance [' name' ]
134- for connection in instance .get (' connections' , []):
135- if connection [' target' ][0 ][' sig' ] == port_signal :
134+ for module in proto_dict .get (" modules" , []):
135+ for instance in module .get (" instances" , []):
136+ instance_name = instance [" name" ]
137+ for connection in instance .get (" connections" , []):
138+ if connection [" target" ][0 ][" sig" ] == port_signal :
136139 return f"{ instance_name } ,{ connection ['portname' ]} "
137140 return None
138141
@@ -143,27 +146,27 @@ def _extract_instance_parameters(proto_dict: ParsedProtoVLSIR):
143146 """
144147 instance_parameters = {}
145148
146- for module in proto_dict .get (' modules' , []):
147- for instance in module .get (' instances' , []):
148- instance_name = instance [' name' ]
149+ for module in proto_dict .get (" modules" , []):
150+ for instance in module .get (" instances" , []):
151+ instance_name = instance [" name" ]
149152 instance_info = {
150- ' component' : _extract_component_name (instance ),
151- ' info' : {},
152- ' settings' : {}
153+ " component" : _extract_component_name (instance ),
154+ " info" : {},
155+ " settings" : {},
153156 }
154157
155158 # Extract parameters into the settings
156- for parameter in instance .get (' parameters' , []):
157- param_name = parameter [' name' ]
158- param_value = _extract_parameter_value (parameter [' value' ])
159- instance_info [' settings' ][param_name ] = param_value
159+ for parameter in instance .get (" parameters" , []):
160+ param_name = parameter [" name" ]
161+ param_value = _extract_parameter_value (parameter [" value" ])
162+ instance_info [" settings" ][param_name ] = param_value
160163
161164 # Extract connections and add to settings
162- instance_info [' settings' ][ ' ports' ] = {}
163- for connection in instance .get (' connections' , []):
164- portname = connection [' portname' ]
165- target_signal = connection [' target' ][0 ][' sig' ]
166- instance_info [' settings' ][ ' ports' ][portname ] = target_signal
165+ instance_info [" settings" ][ " ports" ] = {}
166+ for connection in instance .get (" connections" , []):
167+ portname = connection [" portname" ]
168+ target_signal = connection [" target" ][0 ][" sig" ]
169+ instance_info [" settings" ][ " ports" ][portname ] = target_signal
167170
168171 instance_parameters [instance_name ] = instance_info
169172
@@ -174,23 +177,23 @@ def _extract_component_name(instance):
174177 """
175178 Extract the component name from the instance.
176179 """
177- external_modules = instance .get (' module' , [])
180+ external_modules = instance .get (" module" , [])
178181 if external_modules :
179- domain = external_modules [0 ].get (' external' , [{}])[0 ].get (' domain' , '' )
180- name = external_modules [0 ].get (' external' , [{}])[0 ].get (' name' , '' )
182+ domain = external_modules [0 ].get (" external" , [{}])[0 ].get (" domain" , "" )
183+ name = external_modules [0 ].get (" external" , [{}])[0 ].get (" name" , "" )
181184 return f"{ name } "
182- return ' unknown_component'
185+ return " unknown_component"
183186
184187
185188def _extract_parameter_value (value ):
186189 """
187190 Extract the parameter value from the value dictionary.
188191 """
189- if value and ' literal' in value [0 ]:
190- return value [0 ][' literal' ]
191- elif value and ' prefixed' in value [0 ]:
192- prefix = value [0 ][' prefixed' ][0 ].get (' prefix' , '' )
193- int64_value = value [0 ][' prefixed' ][0 ].get (' int64_value' , '' )
192+ if value and " literal" in value [0 ]:
193+ return value [0 ][" literal" ]
194+ elif value and " prefixed" in value [0 ]:
195+ prefix = value [0 ][" prefixed" ][0 ].get (" prefix" , "" )
196+ int64_value = value [0 ][" prefixed" ][0 ].get (" int64_value" , "" )
194197 return f"{ prefix } _{ int64_value } "
195198 return None
196199
@@ -199,25 +202,20 @@ def _generate_raw_netlist_dict_from_proto_dict(proto_dict: ParsedProtoVLSIR):
199202 """
200203 Generate a raw netlist dictionary from the proto_dict.
201204 """
202- raw_netlist_dict = {
203- 'name' : '' ,
204- 'instances' : {},
205- 'connections' : {},
206- 'ports' : {}
207- }
205+ raw_netlist_dict = {"name" : "" , "instances" : {}, "connections" : {}, "ports" : {}}
208206
209207 # Extract the top-level module name
210- if proto_dict .get (' modules' ):
211- raw_netlist_dict [' name' ] = proto_dict [' modules' ][0 ].get (' name' , '' )
208+ if proto_dict .get (" modules" ):
209+ raw_netlist_dict [" name" ] = proto_dict [" modules" ][0 ].get (" name" , "" )
212210
213211 # Generate instances information
214- raw_netlist_dict [' instances' ] = _extract_instance_parameters (proto_dict )
212+ raw_netlist_dict [" instances" ] = _extract_instance_parameters (proto_dict )
215213
216214 # Generate connections
217- raw_netlist_dict [' connections' ] = _parse_connections (proto_dict )
215+ raw_netlist_dict [" connections" ] = _parse_connections (proto_dict )
218216
219217 # Generate top-level connections
220- raw_netlist_dict [' ports' ] = _generate_top_level_connections (proto_dict )
218+ raw_netlist_dict [" ports" ] = _generate_top_level_connections (proto_dict )
221219
222220 return raw_netlist_dict
223221
0 commit comments