aboutsummaryrefslogtreecommitdiffstats
path: root/parse.py
blob: 7541dde910fe95117769ac20157937220073ec8c (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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#!/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_DATA_WIDTH = 32
AXI_ADDR_WIDTH = 16
AXI_ADDR_MSB = AXI_ADDR_WIDTH-1
AXI_ADDR_LSB = 2

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

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
    signed = False

    def set_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_WIDTH - 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__)

    def hdlwidth(self):
        if self.bits == 1:
            return "[0]"
        else:
            return "[%d:0] " % (self.bits - 1)

    def pphdlwidth(self):
        if self.bits == 1:
            return ""
        else:
            return "[%d:0] " % (self.bits - 1)

    def ppdefault(self):
        return "%d'h%X" % (self.bits, self.default)

    def word_list(self):
        l = []
        b = self.bits
        bottom = 0
        a = self.index
        span = None
        while b > 0:
            if b < 32:
                if (self.bits == 1):
                    span = ""
                else:
                    span = "[%d:%d]" % (bottom+b-1, bottom)
                l.append((a, "{%d'd0, %s%s}" % (32-b, self.slug, span), span))
            else:
                if (self.bits == 1):
                    span = ""
                else:
                    span = "[%d:%d]" % (bottom+31, bottom)
                l.append((a, "%s%s" % (self.slug, span), span))
            a += 1
            b -= 32
            bottom += 32
        return l

    def ctype(self):
        if self.bits <= 32:
            return self.signed and "int32_t" or "uint32_t"
        elif self.bits <= 64:
            return self.signed and "int64_t" or "uint64_t"
        else:
            raise ValueError("Can't represent %d bits in C... ?" % self.bits)


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


class Parameter(Value):
    def ppslug(self):
        return self.slug.upper()


def check_overlaps(l):
    rangelist = []
    for val in l:
        # TODO: also handle larger ranges
        this = (val.index, val.index + ((val.bits-1)/AXI_DATA_WIDTH))
        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):
    """
    Checks that all section+slug combinations are unique (no duplicates)
    'l' should be the set of all values, in any order.
    """
    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 check_gaps(l):
    """
    Checks for gaps between memory map locations within a section.
    Assumes 'l' is a list of values in a section, already sorted by index.
    """
    n = None
    for v in l:
        if n is not None:
            if v.index != n:
                raise Exception("Gap between values! Oh no! At: %s.%s (n=%d)"
                                % (v.section, v.slug, n))
        n = v.index + 1 + (v.bits-1)/32


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


def parse():
    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'].lower()
        try:
            if mode == 'p':
                p = Parameter(**line)
                parameters.append(p)
            elif mode in ['r', 'w', 'rw', 'wr']:
                r = Register(**line)
                r.read = 'r' in mode
                r.write = 'w' in mode
                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.set_offset(offset)
    for p in parameters:
        p.set_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)
        check_gaps(sections[key])

    print("------- END READ")
    return registers, parameters, sections


def output(registers, parameters, sections):
    settings = {
        'stub_axi_nets': True,
        'stub_nets': True,
    }
    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,
                AXI_DATA_WIDTH=AXI_DATA_WIDTH,
                AXI_ADDR_WIDTH=AXI_ADDR_WIDTH,
                AXI_ADDR_MSB=AXI_ADDR_MSB,
                AXI_ADDR_LSB=AXI_ADDR_LSB,
                settings=settings)

    def guess_autoescape(template_name):
        """Only auto-escape HTML documents"""
        if template_name is None:
            return False
        if 'html' in template_name.lower():
            return True
        else:
            return False

    # TODO:
    # jinja2.ChoiceLoader
    # jinja2.PackageLoader
    env = jinja2.Environment(loader=jinja2.FileSystemLoader('templates'),
                            lstrip_blocks=True,
                            trim_blocks=True,
                            autoescape=guess_autoescape,
                            extensions=['jinja2.ext.autoescape'])
    #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
    """
    t = env.get_template('partial_axi_lite_slave.v.tmpl')
    out_f = open('output/axi_lite_slave_%s.v' % context['name'], 'w')
    out_f.write(t.render(**context))
    out_f.close()

    t = env.get_template('stub.v.tmpl')
    out_f = open('output/%s_stub.v' % context['name'], 'w')
    out_f.write(t.render(**context))
    out_f.close()
    print("------- END HDL")

    #print("------- START C_HEADER")
    """
    just structs for parameters/registers
    """
    t = env.get_template('headers.h.tmpl')
    out_f = open('output/%s_headers.h' % context['name'], 'w')
    out_f.write(t.render(**context))
    out_f.close()
    #print("------- END C_HEADER")

    print("------- START HTML")
    t = env.get_template('minimal.html.tmpl')
    out_f = open('output/%s.html' % context['name'], '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/%s.rst' % context['name'], 'w')
    out_f.write(t.render(**context))
    out_f.close()
    print("------- END RST")

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

def main():
    r, p, s = parse()
    output(r,p,s)

if __name__=="__main__":
    main()