aboutsummaryrefslogtreecommitdiffstats
path: root/parse.py
blob: c404d9705973403efd7d5f4764119d2533dea11f (plain)
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#!/usr/bin/env python
"""

TODO:
- values should only parse as section if XYZ???
- whitespace control
  http://jinja.pocoo.org/docs/templates/#whitespace-control
- disable HTML safety for non-html documents?
"""

from __future__ import print_function

import sys
import csv
import time
import os

import jinja2

AXI_ADDR_BITS = 16
WORD_BITS = 32

def parse_slug(s):
    pre = s.split('[')[0]
    post = None
    assert(pre == filter(lambda x: x.isalnum() or x is '_', pre))
    assert(len(pre) >= 1 and pre[0] != '_')
    if '[' in s:
        assert(s.count('[') == 1)
        assert(s.count(']') == 1 and s[-1] == ']')
        post = int(s.split('[')[1][:-1])
        assert(post >= 0)
    return (pre, post)

def str2val(s, bits):
    """
    Strip '_' characters (eg, 0x1111_2222).
    Allow 0x and 0b prefixes.
    """
    v = None
    s = s.lower().replace('_', '')
    if s.count("'") is 1:
        raise NotImplementedError("Can't handle verilog-style constants yet")
    if s.startswith('0b'):
        v = int(s[2:], 2)
    if s.startswith('0x'):
        v = int(s[2:], 16)
    else: # fallback
        v = int(s)
    assert(v >= 0 and v < 2**bits)
    return v

class Value():
    index = None
    bits = None
    section = None
    section_index = None
    slug = None
    slug_index = None
    default = None
    description = None
    mode = None
    addr = None

    def offset(self, offset):
        self.addr = offset + (4 * self.index)

    def addr_pp(self):
        return "0x%08X" % self.addr

    def __init__(self, word_index=None, bits=None, section=None, slug=None,
                 default=None, description=None, mode=None):
        # TODO: input validation/transforms
        self.index = int(word_index)
        assert(self.index >= 0)
        assert(self.index <= (2**AXI_ADDR_BITS - 1))

        if bits in [None, '']:
            raise ValueError("Bits not defined")
        self.bits = str2val(bits, 9)
        assert(self.bits >= 1)
        assert(self.bits <= 128)

        if section is None:
            (self.section, self.section_index) = ('top_level', None)
        else:
            (self.section, self.section_index) = parse_slug(section)

        if slug is None:
            (self.slug, self.slug_index) = (None, None)
        else:
            (self.slug, self.slug_index) = parse_slug(slug)

        if default not in [None, '']:
            self.default = str2val(default, self.bits)
        else:
            self.default = 0
        self.description = description
        self.mode = mode

    def __str__(self):
        return "<Value: %s>" % str(self.__dict__)

class Register(Value):
    read = False
    write = False

class Parameter(Value):
    pass

def check_overlaps(l):
    rangelist = []
    for val in l:
        # TODO: also handle larger ranges
        this = (val.index, val.index + ((val.bits-1)/WORD_BITS))
        inserted = False
        for i in range(len(rangelist)):
            that = rangelist[i]
            if ((that[0] <= this[0] <= that[1])
                    or (that[0] <= this[1] <= that[1])):
                raise ValueError("Overlapping memory ranges: %s and %s" %
                    (this, that))
            if this[0] < that[0]:
                rangelist.insert(i, this)
                inserted = True
                break
        if not inserted:
            rangelist.append(this)

def check_names(l):
    names = []
    n = None
    for val in l:
        if val.section:
            n = "%s.%s" % (val.section, val.slug)
        else:
            n = val.slug
        if n in names:
            raise ValueError("Dupliate name: %s" % n)
        names.append(n)

def error(s="unspecified"):
    sys.stderr.write(str(s) + '\n')
    sys.exit(-1)

class Repeated():
    section = None

    def __init__(self, word_index, slug):
        self.index = int(word_index)
        assert(self.index >= 0)
        assert(self.index <= (2**AXI_ADDR_BITS - 1))

        if self.section is '':
            self.section = ''
            self.section_index = None
        else:
            (self.section, self.section_index) = parse_slug(section)
   

req = ('word_index', 'bits', 'mode', 'section', 'slug', 'default',
       'description')

print("------- START READ")
f = open('example.csv', 'r')
reader = csv.DictReader(f)

registers = []
parameters = []
mode = None

for line in reader:
    if reader.line_num is 0:
        # validate fields just once
        for field in req:
            if not field in reader.fields:
                error("Missing column: %s" % field)

    # skip lines w/o 
    if line['word_index'] in [None, '']:
        print("Skipping line %d (no index)" % reader.line_num)
        continue

    mode = line['mode']
    try:
        if mode.lower() == 'p':
            p = Parameter(**line)
            parameters.append(p)
        elif mode.lower() == 'r':
            r = Register(**line)
            r.read = True
            registers.append(r)
        else:
            #error("Unknown mode: %s" % mode)
            print("Skipping line %d (unknown mode %s)" % (reader.line_num,
                                                          mode))
            pass
    except (AttributeError, TypeError, ValueError), e:
        error("Syntax error parsing line %d: %s" % (reader.line_num, e))
    sys.stdout.write(".")
print('')
f.close()

print("Registers:\t%d" % len(registers))
print("Parameters:\t%d" % len(parameters))

offset = 0x0
for r in registers:
    r.offset(offset)
for p in parameters:
    p.offset(offset)

check_overlaps(registers + parameters)
check_names(registers + parameters)
sections = {}
for val in (registers + parameters):
    if not val.section in sections.keys():
        sections[val.section] = []
    sections[val.section].append(val)

for key, sec in sections.iteritems():
    sections[key] = sorted(sec, key=lambda x: x.index)

print("------- END READ")

# TODO: process into sections; sort; apply offsets

context = dict(registers=registers,
               parameters=parameters,
               name="example",
               now=time.strftime("%Y-%m-%d %H:%M:%S UTC", time.gmtime()),
               attribution="Generated by AXI-Lite Generator",
               whoami=os.getenv('USER'),
               sections=sections)

# TODO:
# jinja2.ChoiceLoader
# jinja2.PackageLoader
env = jinja2.Environment(loader=jinja2.FileSystemLoader('templates'))
#print("------- START PYTHON")
"""
params: single helper to dump them all
registers:
    helper get/set by string (eg, get("meta.magic"))
    module cmd to dump them all
    module+slug cmd to get/set
    <section>.<slug> getter/setter functions
"""
#print("------- END PYTHON")

#print("------- START HDL")
"""
wrapper stub also.
params: passed all around
registers: just one place
"""
#print("------- END HDL")

#print("------- START C_HEADER")
"""
just structs for parameters/registers
"""
#print("------- END C_HEADER")

print("------- START HTML")
t = env.get_template('minimal.html.tmpl')
out_f = open('output/example.html', 'w')
out_f.write(t.render(**context))
out_f.close()
print("------- END HTML")

print("------- START RST")
t = env.get_template('minimal.rst.tmpl')
out_f = open('output/example.rst', 'w')
out_f.write(t.render(**context))
out_f.close()
print("------- END RST")

print("------- DONE!")