|
| 1 | +""" |
| 2 | +This module provides functions to generate a raw netlist semi-compatible with gdsfactory from a hdl21 module object. |
| 3 | +""" |
| 4 | +import yaml |
| 5 | +import hdl21 as h |
| 6 | + |
| 7 | +__all__ = [ |
| 8 | + 'ParsedProtoVLSIR', |
| 9 | + 'generate_raw_netlist_dict_from_module', |
| 10 | + 'generate_raw_yaml_from_module' |
| 11 | +] |
| 12 | + |
| 13 | +ParsedProtoVLSIR = dict |
| 14 | + |
| 15 | + |
| 16 | +def _parse_module_to_proto_dict(module: h.module) -> ParsedProtoVLSIR: |
| 17 | + """ |
| 18 | + Parse a hdl21 module object into a dictionary with the same structure as the proto VLSIR format. |
| 19 | + """ |
| 20 | + |
| 21 | + def parse_value(lines, index): |
| 22 | + value = {} |
| 23 | + while index < len(lines): |
| 24 | + line = lines[index].strip() |
| 25 | + if line == "}": |
| 26 | + return value, index |
| 27 | + elif line.endswith("{"): |
| 28 | + key = line[:-1].strip() |
| 29 | + sub_value, new_index = parse_value(lines, index + 1) |
| 30 | + if key not in value: |
| 31 | + value[key] = [] |
| 32 | + value[key].append(sub_value) |
| 33 | + index = new_index |
| 34 | + else: |
| 35 | + key, val = line.split(":", 1) |
| 36 | + value[key.strip()] = val.strip().strip('"') |
| 37 | + index += 1 |
| 38 | + return value, index |
| 39 | + |
| 40 | + raw_proto_str = str(h.to_proto(module)) |
| 41 | + lines = raw_proto_str.split("\n") |
| 42 | + result = {} |
| 43 | + index = 0 |
| 44 | + while index < len(lines): |
| 45 | + line = lines[index].strip() |
| 46 | + if line.endswith("{"): |
| 47 | + key = line[:-1].strip() |
| 48 | + sub_value, new_index = parse_value(lines, index + 1) |
| 49 | + if key not in result: |
| 50 | + result[key] = [] |
| 51 | + result[key].append(sub_value) |
| 52 | + index = new_index |
| 53 | + else: |
| 54 | + index += 1 |
| 55 | + |
| 56 | + return result |
| 57 | + |
| 58 | + |
| 59 | +def _parse_connections(proto_dict: ParsedProtoVLSIR) -> dict: |
| 60 | + """ |
| 61 | + Extract the connections from the proto_dict and return a dictionary with the connections. |
| 62 | + """ |
| 63 | + connections = {} |
| 64 | + |
| 65 | + # 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'] |
| 72 | + connection_key = f"{instance_name},{portname}" |
| 73 | + # Find the target instance and port |
| 74 | + target_instance_port = _find_target_instance_port(proto_dict, target_signal, instance_name) |
| 75 | + if target_instance_port: |
| 76 | + connections[connection_key] = target_instance_port |
| 77 | + |
| 78 | + return connections |
| 79 | + |
| 80 | + |
| 81 | +def _find_target_instance_port(proto_dict: ParsedProtoVLSIR, |
| 82 | + target_signal, |
| 83 | + current_instance_name): |
| 84 | + """ |
| 85 | + Find the target instance and port of the target signal in the proto_dict. |
| 86 | + """ |
| 87 | + # 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 | + continue |
| 92 | + for connection in instance.get('connections', []): |
| 93 | + if connection['target'][0]['sig'] == target_signal: |
| 94 | + return f"{instance['name']},{connection['portname']}" |
| 95 | + # 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: |
| 101 | + continue |
| 102 | + for connection in instance.get('connections', []): |
| 103 | + if connection['target'][0]['sig'] == target_signal: |
| 104 | + return f"{instance['name']},{connection['portname']}" |
| 105 | + |
| 106 | + return None |
| 107 | + |
| 108 | + |
| 109 | +def _generate_top_level_connections(proto_dict: ParsedProtoVLSIR): |
| 110 | + """ |
| 111 | + Generate the top-level connections from the proto_dict. |
| 112 | + """ |
| 113 | + top_level_connections = {} |
| 114 | + |
| 115 | + # 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 | + connection = _find_port_connection(proto_dict, port_signal) |
| 120 | + if connection: |
| 121 | + top_level_connections[port_signal] = connection |
| 122 | + |
| 123 | + return top_level_connections |
| 124 | + |
| 125 | + |
| 126 | +def _find_port_connection(proto_dict: ParsedProtoVLSIR, port_signal): |
| 127 | + """ |
| 128 | + Find the connection of the port signal in the proto_dict. |
| 129 | + """ |
| 130 | + # 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: |
| 136 | + return f"{instance_name},{connection['portname']}" |
| 137 | + return None |
| 138 | + |
| 139 | + |
| 140 | +def _extract_instance_parameters(proto_dict: ParsedProtoVLSIR): |
| 141 | + """ |
| 142 | + Extract the instance parameters from the proto_dict. |
| 143 | + """ |
| 144 | + instance_parameters = {} |
| 145 | + |
| 146 | + for module in proto_dict.get('modules', []): |
| 147 | + for instance in module.get('instances', []): |
| 148 | + instance_name = instance['name'] |
| 149 | + instance_info = { |
| 150 | + 'component': _extract_component_name(instance), |
| 151 | + 'info': {}, |
| 152 | + 'settings': {} |
| 153 | + } |
| 154 | + |
| 155 | + # 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 |
| 160 | + |
| 161 | + # 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 |
| 167 | + |
| 168 | + instance_parameters[instance_name] = instance_info |
| 169 | + |
| 170 | + return instance_parameters |
| 171 | + |
| 172 | + |
| 173 | +def _extract_component_name(instance): |
| 174 | + """ |
| 175 | + Extract the component name from the instance. |
| 176 | + """ |
| 177 | + external_modules = instance.get('module', []) |
| 178 | + if external_modules: |
| 179 | + domain = external_modules[0].get('external', [{}])[0].get('domain', '') |
| 180 | + name = external_modules[0].get('external', [{}])[0].get('name', '') |
| 181 | + return f"{name}" |
| 182 | + return 'unknown_component' |
| 183 | + |
| 184 | + |
| 185 | +def _extract_parameter_value(value): |
| 186 | + """ |
| 187 | + Extract the parameter value from the value dictionary. |
| 188 | + """ |
| 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', '') |
| 194 | + return f"{prefix}_{int64_value}" |
| 195 | + return None |
| 196 | + |
| 197 | + |
| 198 | +def _generate_raw_netlist_dict_from_proto_dict(proto_dict: ParsedProtoVLSIR): |
| 199 | + """ |
| 200 | + Generate a raw netlist dictionary from the proto_dict. |
| 201 | + """ |
| 202 | + raw_netlist_dict = { |
| 203 | + 'name': '', |
| 204 | + 'instances': {}, |
| 205 | + 'connections': {}, |
| 206 | + 'ports': {} |
| 207 | + } |
| 208 | + |
| 209 | + # Extract the top-level module name |
| 210 | + if proto_dict.get('modules'): |
| 211 | + raw_netlist_dict['name'] = proto_dict['modules'][0].get('name', '') |
| 212 | + |
| 213 | + # Generate instances information |
| 214 | + raw_netlist_dict['instances'] = _extract_instance_parameters(proto_dict) |
| 215 | + |
| 216 | + # Generate connections |
| 217 | + raw_netlist_dict['connections'] = _parse_connections(proto_dict) |
| 218 | + |
| 219 | + # Generate top-level connections |
| 220 | + raw_netlist_dict['ports'] = _generate_top_level_connections(proto_dict) |
| 221 | + |
| 222 | + return raw_netlist_dict |
| 223 | + |
| 224 | + |
| 225 | +def generate_raw_netlist_dict_from_module(module: h.module): |
| 226 | + """ |
| 227 | + Generate a raw netlist dictionary from a hdl21 module object. |
| 228 | + This just gives us a raw structure of the hdl21 modules, we cannot use this json equivalently to a gdsfactory netlist. |
| 229 | + """ |
| 230 | + proto_dict = _parse_module_to_proto_dict(module) |
| 231 | + return _generate_raw_netlist_dict_from_proto_dict(proto_dict) |
| 232 | + |
| 233 | + |
| 234 | +def generate_raw_yaml_from_module(module: h.module): |
| 235 | + """ |
| 236 | + Generate a raw netlist yaml from a hdl21 module object which could be manually edited for specific instances |
| 237 | + related to the corresponding SPICE. |
| 238 | + """ |
| 239 | + raw_netlist = generate_raw_netlist_dict_from_module(module) |
| 240 | + return yaml.dump(raw_netlist, default_flow_style=False) |
0 commit comments