1 |
64 |
ghutchis |
#!/usr/bin/python
|
2 |
|
|
# Copyright (c) 2004 Guy Hutchison (ghutchis@opencores.org)
|
3 |
|
|
#
|
4 |
|
|
# Permission is hereby granted, free of charge, to any person obtaining a
|
5 |
|
|
# copy of this software and associated documentation files (the "Software"),
|
6 |
|
|
# to deal in the Software without restriction, including without limitation
|
7 |
|
|
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
8 |
|
|
# and/or sell copies of the Software, and to permit persons to whom the
|
9 |
|
|
# Software is furnished to do so, subject to the following conditions:
|
10 |
|
|
#
|
11 |
|
|
# The above copyright notice and this permission notice shall be included
|
12 |
|
|
# in all copies or substantial portions of the Software.
|
13 |
|
|
#
|
14 |
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15 |
|
|
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16 |
|
|
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
17 |
|
|
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
18 |
|
|
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
19 |
|
|
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
20 |
|
|
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
21 |
|
|
|
22 |
|
|
# This script generates I/O mapped control and status registers based
|
23 |
|
|
# on an XML configuration file.
|
24 |
|
|
|
25 |
|
|
import reglib
|
26 |
|
|
import xml.dom.minidom
|
27 |
|
|
import sys, os, re
|
28 |
|
|
|
29 |
|
|
def node_info (node):
|
30 |
|
|
print "Methods:",dir(node)
|
31 |
|
|
print "Child Nodes:",node.childNodes
|
32 |
|
|
|
33 |
|
|
def number (str):
|
34 |
|
|
try:
|
35 |
|
|
robj = re.compile ("(\d+)'([dhb])([\da-fA-F]+)")
|
36 |
|
|
mobj = robj.match (str)
|
37 |
|
|
if (mobj):
|
38 |
|
|
if mobj.group(2) == 'h': radix = 16
|
39 |
|
|
elif mobj.group(2) == 'b': radix = 2
|
40 |
|
|
else: radix = 10
|
41 |
|
|
|
42 |
|
|
return int (mobj.group(3), radix)
|
43 |
|
|
else:
|
44 |
|
|
return int(str)
|
45 |
|
|
except ValueError:
|
46 |
|
|
print "ERROR: number conversion of %s failed" % str
|
47 |
|
|
return 0
|
48 |
|
|
|
49 |
|
|
def create_reg_group (node):
|
50 |
|
|
rg = reglib.register_group()
|
51 |
|
|
|
52 |
|
|
rg.name = node.getAttribute ("name")
|
53 |
|
|
rg.addr_size = number(node.getAttribute ("addr_sz"))
|
54 |
|
|
rg.base_addr = number(node.getAttribute ("base_addr"))
|
55 |
|
|
|
56 |
|
|
return rg
|
57 |
|
|
|
58 |
|
|
def create_register (rg, node):
|
59 |
|
|
params = {}
|
60 |
|
|
params['name'] = node.getAttribute ("name")
|
61 |
|
|
type = node.getAttribute ("type")
|
62 |
|
|
params['width'] = int(node.getAttribute ("width"))
|
63 |
|
|
params['default'] = node.getAttribute ("default")
|
64 |
|
|
|
65 |
|
|
if type == '': type = 'config'
|
66 |
|
|
if params['default'] == '': params['default'] = 0
|
67 |
|
|
else: params['default'] = number (params['default'])
|
68 |
|
|
|
69 |
|
|
rg.add_register (type, params)
|
70 |
|
|
|
71 |
|
|
def create_verilog (top_node):
|
72 |
|
|
rg = create_reg_group (top_node)
|
73 |
|
|
|
74 |
|
|
# get list of register nodes
|
75 |
|
|
reg_nodes = top_node.getElementsByTagName ("register")
|
76 |
|
|
|
77 |
|
|
for r in reg_nodes:
|
78 |
|
|
create_register (rg, r)
|
79 |
|
|
|
80 |
|
|
fname = rg.name + ".v"
|
81 |
|
|
fh = open (fname, 'w')
|
82 |
|
|
fh.write (rg.verilog())
|
83 |
|
|
fh.close()
|
84 |
|
|
|
85 |
|
|
def parse_file (filename):
|
86 |
|
|
rdoc = xml.dom.minidom.parse (filename)
|
87 |
|
|
blk_list = rdoc.getElementsByTagName ("tv_registers")
|
88 |
|
|
|
89 |
|
|
for blk in blk_list:
|
90 |
|
|
create_verilog (blk)
|
91 |
|
|
|
92 |
|
|
rdoc.unlink()
|
93 |
|
|
|
94 |
|
|
if (len (sys.argv) > 1):
|
95 |
|
|
parse_file (sys.argv[1])
|
96 |
|
|
else:
|
97 |
|
|
print "Usage: %s <filename>" % os.path.basename (sys.argv[0])
|
98 |
|
|
|