OpenCores
URL https://opencores.org/ocsvn/radiohdl/radiohdl/trunk

Subversion Repositories radiohdl

[/] [radiohdl/] [trunk/] [base/] [generate_qsys] - Blame information for rev 2

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2019 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.