11import zlib # for deterministic hash
2+ from enum import Enum , auto
23from typing import List
34
4- from .. import edgir
5- from ..core import TransformUtil
65from .NetlistGenerator import Netlist , NetBlock , Net
6+ from .. import edgir
7+
8+
9+ class RefdesMode (Enum ):
10+ PathnameAsValue = auto () # conventional refdes w/ pathname as value, except for TPs
11+ Conventional = auto () # conventional refdes only, value passed through
12+ Pathname = auto () # pathname as refdes, value passed through
13+
714
815
916###############################################################################################################################################################################################
@@ -17,17 +24,24 @@ def gen_header() -> str:
1724
1825"""2. Generating Blocks"""
1926
20- def block_name (block : NetBlock , refdes_pathname : bool ) -> str :
21- if refdes_pathname :
27+ def block_name (block : NetBlock , refdes_mode : RefdesMode ) -> str :
28+ if refdes_mode == RefdesMode . Pathname :
2229 return '.' .join (block .path )
2330 else :
24- return block .refdes
31+ return block .refdes # default is conventional refdes
2532
2633def gen_block_comp (block_name : str ) -> str :
2734 return f'(comp (ref "{ block_name } ")'
2835
29- def gen_block_value (block_value : str ) -> str :
30- return f'(value "{ block_value } ")'
36+ def gen_block_value (block : NetBlock , refdes_mode : RefdesMode ) -> str :
37+ if refdes_mode == RefdesMode .PathnameAsValue :
38+ if block .refdes .startswith ('TP' ): # test points keep their value
39+ return f'(value "{ block .value } ")'
40+ else :
41+ pathname = '.' .join (block .path )
42+ return f'(value "{ pathname } ")'
43+ else :
44+ return f'(value "{ block .value } ")'
3145
3246def gen_block_footprint (block_footprint : str ) -> str :
3347 return f'(footprint "{ block_footprint } ")'
@@ -59,19 +73,14 @@ def gen_block_prop_sheetfile(block_path: List[edgir.LibraryPath]) -> str:
5973 value = ""
6074 return f'(property (name "Sheetfile") (value "{ value } "))'
6175
62- def gen_block_prop_path (block_path : TransformUtil .Path ) -> str :
63- return f'(property (name "edg_path") (value "{ "." .join (block_path .to_tuple ())} "))'
64-
65- def gen_block_prop_shortpath (block_path : List [str ]) -> str :
66- return f'(property (name "edg_short_path") (value "{ "." .join (block_path )} "))'
67-
68- def gen_block_prop_refdes (refdes : str ) -> str :
69- return f'(property (name "edg_refdes") (value "{ refdes } "))'
70-
71- def gen_block_prop_part (part : str ) -> str :
72- return f'(property (name "edg_part") (value "{ part } "))'
76+ def gen_block_prop_edg (block : NetBlock , refdes_mode : RefdesMode ) -> str :
77+ return f'(property (name "edg_path") (value "{ "." .join (block .full_path .to_tuple ())} "))\n ' + \
78+ f' (property (name "edg_short_path") (value "{ "." .join (block .path )} "))\n ' + \
79+ f' (property (name "edg_refdes") (value "{ block .refdes } "))\n ' + \
80+ f' (property (name "edg_part") (value "{ block .part } "))\n ' + \
81+ f' (property (name "edg_value") (value "{ block .value } "))\n '
7382
74- def block_exp (blocks : List [NetBlock ], refdes_pathname : bool ) -> str :
83+ def block_exp (blocks : List [NetBlock ], refdes_mode : RefdesMode ) -> str :
7584 """Given a dictionary of block_names (strings) as keys and Blocks (namedtuples) as corresponding values
7685
7786 Example:
@@ -87,15 +96,12 @@ def block_exp(blocks: List[NetBlock], refdes_pathname: bool) -> str:
8796 """
8897 result = '(components'
8998 for block in blocks :
90- result += '\n ' + gen_block_comp (block_name (block , refdes_pathname )) + '\n ' + \
91- " " + gen_block_value (block . value ) + '\n ' + \
99+ result += '\n ' + gen_block_comp (block_name (block , refdes_mode )) + '\n ' + \
100+ " " + gen_block_value (block , refdes_mode ) + '\n ' + \
92101 " " + gen_block_footprint (block .footprint ) + '\n ' + \
93102 " " + gen_block_prop_sheetname (block .path ) + '\n ' + \
94103 " " + gen_block_prop_sheetfile (block .class_path ) + '\n ' + \
95- " " + gen_block_prop_path (block .full_path ) + '\n ' + \
96- " " + gen_block_prop_shortpath (block .path ) + '\n ' + \
97- " " + gen_block_prop_refdes (block .refdes ) + '\n ' + \
98- " " + gen_block_prop_part (block .part ) + '\n ' + \
104+ " " + gen_block_prop_edg (block , refdes_mode ) + '\n ' + \
99105 " " + gen_block_sheetpath (block .path [:- 1 ]) + '\n ' + \
100106 " " + gen_block_tstamp (block .path )
101107 return result + ')'
@@ -110,7 +116,7 @@ def gen_net_header(net_count: int, net_name: str) -> str:
110116def gen_net_pin (block_name : str , pin_name : str ) -> str :
111117 return "(node (ref {}) (pin {}))" .format (block_name , pin_name )
112118
113- def net_exp (nets : List [Net ], blocks : List [NetBlock ], refdes_pathname : bool ) -> str :
119+ def net_exp (nets : List [Net ], blocks : List [NetBlock ], refdes_mode : RefdesMode ) -> str :
114120 """Given a dictionary of net names (strings) as keys and a list of connected Pins (namedtuples) as corresponding values
115121
116122 Example:
@@ -130,7 +136,7 @@ def net_exp(nets: List[Net], blocks: List[NetBlock], refdes_pathname: bool) -> s
130136 for i , net in enumerate (nets ):
131137 result += '\n ' + gen_net_header (i + 1 , net .name )
132138 for pin in net .pins :
133- result += '\n ' + gen_net_pin (block_name (block_dict [pin .block_path ], refdes_pathname ), pin .pin_name )
139+ result += '\n ' + gen_net_pin (block_name (block_dict [pin .block_path ], refdes_mode ), pin .pin_name )
134140 result += ')'
135141 return result + ')'
136142
@@ -139,6 +145,6 @@ def net_exp(nets: List[Net], blocks: List[NetBlock], refdes_pathname: bool) -> s
139145"""4. Generate Full Netlist"""
140146
141147
142- def generate_netlist (netlist : Netlist , refdes_pathname : bool ) -> str :
143- return gen_header () + '\n ' + block_exp (netlist .blocks , refdes_pathname ) + '\n ' \
144- + net_exp (netlist .nets , netlist .blocks , refdes_pathname ) + '\n ' + ')'
148+ def generate_netlist (netlist : Netlist , refdes_mode : RefdesMode ) -> str :
149+ return gen_header () + '\n ' + block_exp (netlist .blocks , refdes_mode ) + '\n ' \
150+ + net_exp (netlist .nets , netlist .blocks , refdes_mode ) + '\n ' + ')'
0 commit comments