-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathsynthesis.py
More file actions
156 lines (113 loc) · 4.54 KB
/
synthesis.py
File metadata and controls
156 lines (113 loc) · 4.54 KB
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
import os
import re
def synthesis (rtl, tech, topmodule):
'''
Synthetizes a circuit file and map it to a specific techonolgy
Parameters
----------
rtl : str
path of the rtl circuit file
tech : str
path of the technology file
topmodule : str
name of the circuit we want to sintetize
tool : str
name of tool we are going to use to sintetize
Returns
-------
str
path of the sintetized netlist file
'''
# - - - - - - - - - - - - - Copy the synth.ys file - - - - - - - - - - - -
current_dir=os.path.dirname(__file__)
file = open(f"{current_dir}/templates/synth.ys","r")
file_text = file.read()
file.close()
netlist_path = os.path.dirname(rtl) + "/netlist.v"
file_text = file_text.replace("[[RTLFILENAME]]", f'"{rtl}"')
file_text = file_text.replace("[[TOPMODULE]]", topmodule)
file_text = file_text.replace("[[NETLIST]]", f'"{netlist_path}"')
file_text = file_text.replace("[[LIBRARY]]", f'"{current_dir}/templates/{tech}.lib"')
file_text = file_text.replace("[[LIBRARYABC]]", f'"{current_dir}/templates/{tech}.lib"')
file = open('synth.ys',"w")
file.write(file_text)
file.close()
# - - - - - - - - - - - - - - - Execute yosys - - - - - - - - - - - - - -
os.system ('yosys synth.ys;')
# - - - - - - - - - - - - - Delete temporal Files - - - - - - - - - - - -
os.remove ("synth.ys")
return netlist_path
def resynthesis(netlist, tech, topmodule):
'''
Pass a synthetized circuit to Yosys, reading the tech source file to repeat the synthesis.
:param netlist: string
Synthetized circuit netlist
:param tech: string
Name of the technology library
:param topmodule: string
Topmodule of the circuit
:return:
path-like string
Path to re-synthetized netlist
'''
current_dir=os.path.dirname(__file__)
file = open(f"{current_dir}/templates/resynth.ys","r")
file_text = file.read()
file.close()
netlist_path = os.path.dirname(netlist) + "/netlist.v"
file_text = file_text.replace("[[RTLFILENAME]]", netlist)
file_text = file_text.replace("[[TOPMODULE]]", topmodule)
file_text = file_text.replace("[[TECHNOLOGY]]", f'{current_dir}/templates/{tech}.v')
file_text = file_text.replace("[[NETLIST]]", netlist_path)
file_text = file_text.replace("[[LIBRARY]]", f"{current_dir}/templates/{tech}.lib")
file_text = file_text.replace("[[LIBRARYABC]]", f"{current_dir}/templates/{tech}.lib")
file = open('resynth.ys',"w")
file.write(file_text)
file.close()
# - - - - - - - - - - - - - - - Execute yosys - - - - - - - - - - - - - -
os.system ('yosys resynth.ys;')
# - - - - - - - - - - - - - Delete temporal Files - - - - - - - - - - - -
os.remove ("resynth.ys")
return netlist_path
def ys_get_area(netlist, tech, topmodule):
'''
Opens the circuit in Yosys and runs stat command to estimate area. Result is parsed
from a temporary log file generated.
:param netlist: string
Synthetized circuit netlist
:param tech: string
Name of the technology library
:param topmodule: string
Topmodule of the circuit
:return:
string
Area estimation obtained from yosys stat command
'''
current_dir=os.path.dirname(__file__)
file = open(f"{current_dir}/templates/stat.ys","r")
file_text = file.read()
file.close()
yosys_log_path = os.path.dirname(netlist) + "/yosys_log.txt"
file_text = file_text.replace("[[RTLFILENAME]]", f'"{netlist}"')
file_text = file_text.replace("[[TECHNOLOGY]]", f'"{current_dir}/templates/{tech}.v"')
file_text = file_text.replace("[[TOPMODULE]]", topmodule)
file_text = file_text.replace("[[LIBRARY]]", f'"{current_dir}/templates/{tech}.lib"')
file = open('stat.ys',"w")
file.write(file_text)
file.close()
# - - - - - - - - - - - - - - - Execute yosys - - - - - - - - - - - - - -
os.system (f'yosys stat.ys -l \"{yosys_log_path}\"')
# - - - - - - - - - - - - - - - Parse Area - - - - - - - - - - - - - - -
with open(yosys_log_path, "r") as read_file:
text = read_file.read()
pattern = r'Chip area for module\s\'[\s\S]*\':\s([\d\.]*)'
area_pattern = re.findall(pattern, text)
if (area_pattern):
area = area_pattern[0]
else:
area = 0
read_file.close()
# - - - - - - - - - - - - - Delete temporal Files - - - - - - - - - - - -
os.remove ("stat.ys")
os.remove (yosys_log_path)
return area