1 |
4 |
danv |
#!/usr/bin/env python3
|
2 |
|
|
###############################################################################
|
3 |
|
|
#
|
4 |
|
|
# Copyright (C) 2015
|
5 |
|
|
# ASTRON (Netherlands Institute for Radio Astronomy)
|
6 |
|
|
# P.O.Box 2, 7990 AA Dwingeloo, The Netherlands
|
7 |
|
|
#
|
8 |
|
|
# This program is free software: you can redistribute it and/or modify
|
9 |
|
|
# it under the terms of the GNU General Public License as published by
|
10 |
|
|
# the Free Software Foundation, either version 3 of the License, or
|
11 |
|
|
# (at your option) any later version.
|
12 |
|
|
#
|
13 |
|
|
# This program is distributed in the hope that it will be useful,
|
14 |
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
15 |
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
16 |
|
|
# GNU General Public License for more details.
|
17 |
|
|
#
|
18 |
|
|
# You should have received a copy of the GNU General Public License
|
19 |
|
|
# along with this program. If not, see .
|
20 |
|
|
#
|
21 |
|
|
###############################################################################
|
22 |
|
|
|
23 |
|
|
"""
|
24 |
|
|
Purpose:
|
25 |
|
|
. Generate an Altera QSYS file from a base QSYS file and user supplied list of regs.
|
26 |
|
|
Usage:
|
27 |
|
|
. python generate_qsys.py
|
28 |
|
|
"""
|
29 |
|
|
|
30 |
|
|
from common import *
|
31 |
|
|
|
32 |
|
|
def _to_element_str(reg_name, reg_base_addr):
|
33 |
|
|
"""
|
34 |
|
|
Returns a template XML 'element' filled in with reg_name and reg_base_addr.
|
35 |
|
|
"""
|
36 |
|
|
ELEMENT_TEMPLATE = """
|
37 |
|
|
element HDL_REG_NAME.mem
|
38 |
|
|
{
|
39 |
|
|
datum baseAddress
|
40 |
|
|
{
|
41 |
|
|
value = HDL_REG_BASE_ADDR;
|
42 |
|
|
type = "long";
|
43 |
|
|
}
|
44 |
|
|
datum _sortIndex
|
45 |
|
|
{
|
46 |
|
|
value = "8";
|
47 |
|
|
type = "int";
|
48 |
|
|
}
|
49 |
|
|
datum sopceditor_expanded
|
50 |
|
|
{
|
51 |
|
|
value = "0";
|
52 |
|
|
type = "boolean";
|
53 |
|
|
}
|
54 |
|
|
}
|
55 |
|
|
"""
|
56 |
|
|
return ELEMENT_TEMPLATE.replace('HDL_REG_NAME', reg_name).replace('HDL_REG_BASE_ADDR', str(reg_base_addr))
|
57 |
|
|
|
58 |
|
|
def _to_interface_str(reg_name):
|
59 |
|
|
"""
|
60 |
|
|
Returns a template XML 'interface' filled in with reg_name.
|
61 |
|
|
"""
|
62 |
|
|
INTERFACE_TEMPLATE = """
|
63 |
|
|
|
64 |
|
|
name="HDL_REG_NAME_reset"
|
65 |
|
|
internal="HDL_REG_NAME.reset"
|
66 |
|
|
type="conduit"
|
67 |
|
|
dir="end" />
|
68 |
|
|
|
69 |
|
|
name="HDL_REG_NAME_clk"
|
70 |
|
|
internal="HDL_REG_NAME.clk"
|
71 |
|
|
type="conduit"
|
72 |
|
|
dir="end" />
|
73 |
|
|
|
74 |
|
|
name="HDL_REG_NAME_address"
|
75 |
|
|
internal="HDL_REG_NAME.address"
|
76 |
|
|
type="conduit"
|
77 |
|
|
dir="end" />
|
78 |
|
|
|
79 |
|
|
name="HDL_REG_NAME_write"
|
80 |
|
|
internal="HDL_REG_NAME.write"
|
81 |
|
|
type="conduit"
|
82 |
|
|
dir="end" />
|
83 |
|
|
|
84 |
|
|
name="HDL_REG_NAME_writedata"
|
85 |
|
|
internal="HDL_REG_NAME.writedata"
|
86 |
|
|
type="conduit"
|
87 |
|
|
dir="end" />
|
88 |
|
|
|
89 |
|
|
name="HDL_REG_NAME_read"
|
90 |
|
|
internal="HDL_REG_NAME.read"
|
91 |
|
|
type="conduit"
|
92 |
|
|
dir="end" />
|
93 |
|
|
|
94 |
|
|
name="HDL_REG_NAME_readdata"
|
95 |
|
|
internal="HDL_REG_NAME.readdata"
|
96 |
|
|
type="conduit"
|
97 |
|
|
dir="end" />
|
98 |
|
|
"""
|
99 |
|
|
return INTERFACE_TEMPLATE.replace('HDL_REG_NAME', reg_name)
|
100 |
|
|
|
101 |
|
|
def _append_to_modules_mid_str(modules_mid_str, reg_name, reg_base_addr, reg_end_addr):
|
102 |
|
|
"""
|
103 |
|
|
The modules_mid section contains one line (dataSlaveMapParam) for all start- and end addresses.
|
104 |
|
|
Append the start and end address of our register to this line.
|
105 |
|
|
"""
|
106 |
|
|
DATASLAVEMAPPARAM_TEMPLATE = ""
|
107 |
|
|
return modules_mid_str + DATASLAVEMAPPARAM_TEMPLATE.replace('HDL_REG_NAME', reg_name).replace('HDL_REG_BASE_ADDR', str(reg_base_addr)).replace('HDL_REG_END_ADDR', str(reg_end_addr))
|
108 |
|
|
|
109 |
|
|
def _to_module_str(reg_name, reg_addr_w):
|
110 |
|
|
"""
|
111 |
|
|
Returns a template XML 'module' filled in with reg_name and reg_addr_w.
|
112 |
|
|
"""
|
113 |
|
|
MODULE_TEMPLATE = """
|
114 |
|
|
|
115 |
|
|
|
116 |
|
|
|
117 |
|
|
|
118 |
|
|
|
119 |
|
|
|
120 |
|
|
"""
|
121 |
|
|
return MODULE_TEMPLATE.replace('HDL_REG_NAME', reg_name).replace('HDL_REG_ADDR_W', str(reg_addr_w))
|
122 |
|
|
|
123 |
|
|
def _to_connection_str(reg_name, reg_base_addr):
|
124 |
|
|
"""
|
125 |
|
|
Returns a template XML 'connection' filled in with reg_name and reg_base_addr.
|
126 |
|
|
"""
|
127 |
|
|
CONNECTION_TEMPLATE = """
|
128 |
|
|
|
129 |
|
|
kind="reset"
|
130 |
|
|
version="11.1"
|
131 |
|
|
start="cpu_0.jtag_debug_module_reset"
|
132 |
|
|
end="HDL_REG_NAME.system_reset" />
|
133 |
|
|
|
134 |
|
|
kind="avalon"
|
135 |
|
|
version="11.1"
|
136 |
|
|
start="cpu_0.data_master"
|
137 |
|
|
end="HDL_REG_NAME.mem">
|
138 |
|
|
|
139 |
|
|
|
140 |
|
|
|
141 |
|
|
|
142 |
|
|
kind="reset"
|
143 |
|
|
version="11.1"
|
144 |
|
|
start="clk_input.clk_reset"
|
145 |
|
|
end="HDL_REG_NAME.system_reset" />
|
146 |
|
|
|
147 |
|
|
kind="clock"
|
148 |
|
|
version="11.1"
|
149 |
|
|
start="clk_input.clk"
|
150 |
|
|
end="HDL_REG_NAME.system" />
|
151 |
|
|
"""
|
152 |
|
|
return CONNECTION_TEMPLATE.replace('HDL_REG_NAME', reg_name).replace('HDL_REG_BASE_ADDR', str(reg_base_addr))
|
153 |
|
|
|
154 |
|
|
|
155 |
|
|
def generate_qsys(input_qsys, regs, output_filename):
|
156 |
|
|
"""
|
157 |
|
|
Creates an XML QSYS file (output_filename) from a base QSYS and a user-supplied list of registers to add (regs).
|
158 |
|
|
. regs = [ (reg_name, reg_base_addr, reg_span), .. ]
|
159 |
|
|
"""
|
160 |
|
|
# Read the base QSYS contents into a string
|
161 |
|
|
with open (input_qsys, "r") as base_qsys_file:
|
162 |
|
|
data=base_qsys_file.read()
|
163 |
|
|
|
164 |
|
|
# We'll split the base QSYS string up in 5 sections.
|
165 |
|
|
# . Note that string.split() throws away the delimiter so we'll restore those later.
|
166 |
|
|
elements = data.split(']]>', 1)[0]
|
167 |
|
|
parameters = data.split(']]>', 1)[1].split('', 1)[0]
|
168 |
|
|
interfaces = data.split(']]>', 1)[1].split('', 1)[1].split('
|
169 |
|
|
modules_head = data.split(']]>', 1)[1].split('', 1)[1].split('')[0]
|
170 |
|
|
modules_mid = data.split(']]>', 1)[1].split('', 1)[1].split('')[1].split(']]>',1)[0]
|
171 |
|
|
modules_tail = data.split(']]>', 1)[1].split('', 1)[1].split('')[1].split(']]>',1)[1]
|
172 |
|
|
connections = data.split(']]>', 1)[1].split('', 1)[1].split('
|
173 |
|
|
|
174 |
|
|
# Now we'll append our own XML strings to each section.
|
175 |
|
|
for reg_name, reg_base_addr, reg_addr_w in regs:
|
176 |
|
|
|
177 |
|
|
# Determine the end address of this register
|
178 |
|
|
reg_span = ceil_log2(reg_addr_w)
|
179 |
|
|
reg_end_addr = reg_base_addr + reg_span
|
180 |
|
|
|
181 |
|
|
# Add strings to the sections
|
182 |
|
|
elements += _to_element_str(reg_name, reg_base_addr)
|
183 |
|
|
parameters = parameters
|
184 |
|
|
interfaces += _to_interface_str(reg_name)
|
185 |
|
|
modules_head = modules_head
|
186 |
|
|
modules_mid = _append_to_modules_mid_str(modules_mid, reg_name, reg_base_addr, reg_end_addr)
|
187 |
|
|
modules_tail += _to_module_str(reg_name, reg_addr_w)
|
188 |
|
|
connections += _to_connection_str(reg_name, reg_base_addr)
|
189 |
|
|
|
190 |
|
|
# Re-assemble the sections into one string (add the delimiters that were thrown away by split())
|
191 |
|
|
qsys_str = elements + \
|
192 |
|
|
']]>\n' + \
|
193 |
|
|
parameters + \
|
194 |
|
|
'\n' + \
|
195 |
|
|
interfaces + \
|
196 |
|
|
'
|
197 |
|
|
modules_head + \
|
198 |
|
|
'"dataSlaveMapParam">' + \
|
199 |
|
|
modules_mid + \
|
200 |
|
|
']]>\n' + \
|
201 |
|
|
modules_tail + \
|
202 |
|
|
'
|
203 |
|
|
connections + \
|
204 |
|
|
'\n'
|
205 |
|
|
|
206 |
|
|
# Write the QSYS string to the output_file.
|
207 |
|
|
output_file = open(output_filename, "w")
|
208 |
|
|
output_file.write(qsys_str)
|
209 |
|
|
output_file.close()
|
210 |
|
|
|
211 |
|
|
|
212 |
|
|
################################################################################
|
213 |
|
|
# Example main on execution of this file
|
214 |
|
|
################################################################################
|
215 |
|
|
if __name__ == '__main__':
|
216 |
|
|
base_qsys_path = 'qsys_input.qsys'
|
217 |
|
|
regs = [('reg_my_peripheral', 16384, 3), ('reg_another_peripheral', 17152, 6)]
|
218 |
|
|
generate_qsys(base_qsys_path, regs, 'qsys_generated.qsys')
|