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

Subversion Repositories tv80

[/] [tv80/] [trunk/] [scripts/] [rgen.py] - Blame information for rev 110

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 110 ghutchis
#!/usr/bin/env python
2 64 ghutchis
# 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 110 ghutchis
# 
25 64 ghutchis
 
26
import reglib
27
import xml.dom.minidom
28 110 ghutchis
import sys, os, re, string
29 64 ghutchis
 
30
def node_info (node):
31
    print "Methods:",dir(node)
32
    print "Child Nodes:",node.childNodes
33
 
34 110 ghutchis
def create_addr_vh (filename, dg):
35
    fh = open (filename, 'w')
36
    for d in dg.ranges:
37
        print repr(d)
38
        ba = d.get_base_addr()
39
        fh.write ("`define %s 'h%x\n" % (d.name.upper(), ba))
40
    fh.close()
41
 
42
def create_addr_decoder (node):
43
    rg = reglib.decoder_group()
44
 
45
    rg.name = node.getAttribute ("name")
46
    rg.addr_size = reglib.number(node.getAttribute ("addr_sz"))
47
 
48
    data_sz = node.getAttribute ("data_sz")
49
    if (data_sz != ''):
50
        rg.data_size = reglib.number(data_sz)
51
 
52
    return rg
53
 
54
def create_decoder_verilog (top_node):
55
    dg = create_addr_decoder (top_node)
56
 
57
    # get list of address ranges
58
    range_nodes = top_node.getElementsByTagName ("range")
59
    for rn in range_nodes:
60
        prefix = rn.getAttribute ("prefix")
61
        base = reglib.number(rn.getAttribute ("base"))
62
        bits = int(rn.getAttribute ("bits"))
63
        r = reglib.decoder_range (prefix, base, bits)
64
        dg.add_range (r)
65
 
66
    fname = dg.name + ".v"
67
    fh = open (fname, 'w')
68
    fh.write (dg.verilog())
69
    fh.close()
70
    create_addr_vh (dg.name + ".vh", dg)
71
 
72 64 ghutchis
def create_reg_group (node):
73
    rg = reglib.register_group()
74
 
75
    rg.name = node.getAttribute ("name")
76 67 ghutchis
    rg.addr_size = reglib.number(node.getAttribute ("addr_sz"))
77
    rg.base_addr = reglib.number(node.getAttribute ("base_addr"))
78 64 ghutchis
 
79 110 ghutchis
    data_sz = node.getAttribute ("data_sz")
80
    if (data_sz != ''):
81
        rg.data_size = reglib.number(data_sz)
82
 
83
    rread = node.getAttribute ("registered_read")
84
    if (data_sz != ''):
85
        rg.registered_read = reglib.number(rread)
86
 
87 64 ghutchis
    return rg
88
 
89
def create_register (rg, node):
90
    params = {}
91
    params['name'] = node.getAttribute ("name")
92
    type = node.getAttribute ("type")
93 110 ghutchis
    width = node.getAttribute ("width")
94
    if (width == ''): params['width'] = 1
95
    else : params['width'] = int(width)
96 64 ghutchis
    params['default'] = node.getAttribute ("default")
97 67 ghutchis
    params['int_value'] = node.getAttribute ("int_value")
98 64 ghutchis
 
99 67 ghutchis
    # May switch to this code later for a more general implementation
100
    #for anode in node.childNodes:
101
    #    if anode.nodeType = anode.ATTRIBUTE_NODE:
102
    #        params[anode.nodeName] = anode.nodeValue
103
 
104 110 ghutchis
    print "Reg:",params['name'], " width:",params['width']
105
    fld_nodes = node.getElementsByTagName ("field")
106
    fld_list = []
107
    cum_width = 0
108
    cum_default = 0L
109
    if (len(fld_nodes) != 0):
110
        for fld in fld_nodes:
111
            wstr = fld.getAttribute ("width")
112
            if wstr == '':
113
                width = 1
114
            else:
115
                width = int(wstr)
116
            fld_list.append (reglib.net('wire',fld.getAttribute("name"),width))
117
 
118
            default = fld.getAttribute ("default")
119
            if default == '':
120
                default = 0
121
            else:
122
                default = long(reglib.number (default))
123
            cum_default = cum_default | (default << cum_width)
124
            print "Fld: %20s CD: %x CW: %d D: %x" % (fld.getAttribute("name"),cum_default, cum_width, default)
125
            cum_width += width
126
 
127
        params['width'] = cum_width
128
        params['default'] = cum_default
129
        fld_list.reverse()
130
    else:
131
        if params['default'] == '': params['default'] = 0
132
        else: params['default'] = reglib.number (params['default'])
133
 
134 64 ghutchis
    if type == '': type = 'config'
135
 
136
    rg.add_register (type, params)
137 110 ghutchis
    rg.registers[-1].fields = fld_list
138 64 ghutchis
 
139
def create_verilog (top_node):
140
    rg = create_reg_group (top_node)
141
 
142
    # get list of register nodes
143
    reg_nodes = top_node.getElementsByTagName ("register")
144
 
145
    for r in reg_nodes:
146
        create_register (rg, r)
147
 
148
    fname = rg.name + ".v"
149
    fh = open (fname, 'w')
150
    fh.write (rg.verilog())
151
    fh.close()
152
 
153 110 ghutchis
    create_map (rg)
154
    create_vh (rg)
155
 
156
def create_vh (rg):
157
    fname = rg.name + ".vh"
158
    fh = open (fname, 'w')
159
    for r in rg.registers:
160
        fh.write ("`define %s 16'h%04x\n" % (string.upper(r.name), rg.base_addr+r.offset))
161
    fh.close()
162
 
163
def create_map (rg):
164
    fname = rg.name + ".h"
165
    fh = open (fname, 'w')
166
 
167
    for r in rg.registers:
168
         fh.write ("#define %s 0x%x\n" % (string.upper(r.name),r.offset))
169
 
170
    #for r in rg.registers:
171
    #    fh.write ("sfr at 0x%02x %s;\n" % (r.offset, r.name))
172
    fh.close()
173
 
174 64 ghutchis
def parse_file (filename):
175
    rdoc = xml.dom.minidom.parse (filename)
176
    blk_list = rdoc.getElementsByTagName ("tv_registers")
177
 
178
    for blk in blk_list:
179
        create_verilog (blk)
180
 
181 110 ghutchis
    dec_list = rdoc.getElementsByTagName ("it_decoder")
182
 
183
    for dec in dec_list:
184
        create_decoder_verilog (dec)
185
 
186 64 ghutchis
    rdoc.unlink()
187
 
188 110 ghutchis
def check_version():
189
    version = float (sys.version[0:3])
190
    if (version < 2.3):
191
        print "rgen requires at least Python 2.3 to function correctly"
192
        sys.exit (1)
193
 
194
check_version()
195 64 ghutchis
if (len (sys.argv) > 1):
196
    parse_file (sys.argv[1])
197
else:
198
    print "Usage: %s <filename>" % os.path.basename (sys.argv[0])
199
 

powered by: WebSVN 2.1.0

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