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

Subversion Repositories tv80

[/] [tv80/] [trunk/] [scripts/] [reglib.py] - Blame information for rev 111

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 59 ghutchis
#!/usr/bin/env 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 67 ghutchis
import string, math, re
23 59 ghutchis
 
24
def log2 (num):
25
    return math.ceil (math.log (num) / math.log (2))
26
 
27 67 ghutchis
# function that tries to interpret a number in Verilog notation
28
def number (str):
29
    try:
30
        robj = re.compile ("(\d+)'([dhb])([\da-fA-F]+)")
31
        mobj = robj.match (str)
32
        if (mobj):
33
            if mobj.group(2) == 'h': radix = 16
34
            elif mobj.group(2) == 'b': radix = 2
35
            else: radix = 10
36
 
37
            return int (mobj.group(3), radix)
38
        else:
39
            return int(str)
40
    except ValueError:
41
        print "ERROR: number conversion of %s failed" % str
42
        return 0
43
 
44 110 ghutchis
def int2bin (n):
45
    bStr = ''
46
    if (n < 0): raise ValueError, "must be positive integer"
47
    if (n == 0): return '0'
48
    while (n > 0):
49
        bStr = str (n%2) + bStr
50
        n = n >> 1
51
    return bStr
52
 
53 59 ghutchis
def comb_block (statements):
54
    result = 'always @*\n'
55
    result += '  begin\n'
56
    for s in statements:
57
        result += '    ' + s + '\n'
58
    result += '  end\n'
59
    return result
60
 
61
def seq_block (clock, statements):
62 110 ghutchis
    result = 'always @(posedge ' + clock + ' or negedge reset_n)\n'
63 59 ghutchis
    result += '  begin\n'
64
    for s in statements:
65
        result += '    ' + s + '\n'
66
    result += '  end\n'
67
    return result
68
 
69
class net:
70
    def __init__ (self, type, name, width=1):
71
        self.width = width
72
        self.name  = name
73
        self.type  = type
74
 
75
    def declaration (self):
76
        if (self.width == 1):
77
            return self.type + ' ' + self.name + ';'
78
        else:
79
            return "%s [%d:0] %s;" % (self.type, self.width-1, self.name)
80
 
81
class port:
82
    def __init__ (self, direction, name, width=1):
83
        self.direction = direction
84
        self.width = width
85
        self.name = name
86
 
87
    def declaration (self):
88
        if (self.width == 1):
89
            return self.direction + ' ' + self.name + ';'
90
        else:
91
            return "%s [%d:0] %s;" % (self.direction, self.width-1, self.name)
92 110 ghutchis
 
93
class decoder_range:
94
    def __init__ (self, name, base, bits):
95
        self.name = name
96
        self.base = base
97
        self.bits = bits
98
 
99
    def check_range(self):
100
        mask = (1 << self.bits) - 1
101
        if (self.base & mask):
102
            return 1
103
        else: return 0
104
 
105
    def get_base_addr(self):
106
        return self.base
107 59 ghutchis
 
108 110 ghutchis
class decoder_group:
109
    def __init__ (self, mem_mapped=0):
110
        self.addr_size = 16
111
        self.data_size = 8
112
        self.name = ''
113
        self.ranges = []
114
        self.ports = [port ('input', 'clk'), port('input','reset_n')]
115
        self.nets  = []
116
 
117
        self.blocks = []
118
 
119
    def build (self):
120
        self.ports.append (port ('input', 'cfgi_irdy'))
121
        self.ports.append (port ('output', 'cfgi_trdy'))
122
        self.ports.append (port ('input', 'cfgi_write'))
123
        self.ports.append (port ('input', 'cfgi_addr', self.addr_size))
124
        self.ports.append (port ('input', 'cfgi_wr_data', self.data_size))
125
        self.ports.append (port ('output', 'cfgo_wr_data', self.data_size))
126
        self.ports.append (port ('output', 'cfgi_rd_data', self.data_size))
127
        self.ports.append (port ('output', 'cfgo_addr', self.addr_size))
128
        self.ports.append (port ('output', 'cfgo_write'))
129
        self.nets.append (net('reg','cfgi_rd_data',self.data_size))
130
        self.nets.append (net('reg','nxt_rd_data',self.data_size))
131
        self.nets.append (net('reg', 'nxt_cfgi_trdy'))
132
        self.nets.append (net('reg', 'irdy_out'))
133
        self.nets.append (net('reg', 'trdy_out'))
134
        self.nets.append (net('reg', 'cfgo_wr_data', self.data_size))
135
        self.nets.append (net('reg', 'cfgo_addr', self.addr_size))
136
        self.nets.append (net ('reg', 'cfgo_write'))
137
        self.nets.append (net ('reg', 'cfgi_trdy'))
138
        self.blocks.append ( """
139
   always @(posedge clk or negedge reset_n)
140
    begin
141
      if (~reset_n)
142
        begin
143
          irdy_out <= #1 0;
144
          cfgo_wr_data <= #1 0;
145
          cfgo_addr <= #1 0;
146
          cfgo_write <= #1 0;
147
          cfgi_trdy  <= #1 0;
148
        end
149
      else
150
        begin
151
          irdy_out <= #1 (irdy_out) ? !trdy_out : cfgi_irdy & ~cfgi_trdy;
152
          cfgi_trdy <= #1 irdy_out & trdy_out;
153
 
154
          if (cfgi_irdy & !irdy_out)
155
           begin
156
            cfgo_wr_data  <= #1 cfgi_wr_data;
157
            cfgo_addr     <= #1 cfgi_addr;
158
            cfgo_write    <= #1 cfgi_write;
159
           end
160
          if (trdy_out & !cfgo_write)
161
           cfgi_rd_data <= #1 nxt_rd_data;
162
        end // else: !if(reset)
163
    end // always @ (posedge clk)\n""")
164
 
165
        addr_mux = ["casez (cfgo_addr)\n"]
166
        for r in self.ranges:
167
            self.ports.append (port ('output', r.name + "_irdy"))
168
            self.ports.append (port ('input', r.name + "_trdy"))
169
            self.ports.append (port ('input', r.name + "_rd_data", self.data_size))
170
            self.nets.append (net('reg', r.name + "_irdy"))
171
            addr_mux.insert(0, "%s_irdy = 0;" % r.name)
172
            base_addr = int2bin (r.base)
173
            #for b in range(-r.bits,0):
174
            #    base_addr[b] = 'z'
175
            fill = self.addr_size - len(base_addr)
176
            if (fill > 0):
177
                base_addr = '0' * fill + base_addr
178
            base_addr = base_addr[:-r.bits] + 'z'*r.bits
179
            addr_mux.append ("%d'b%s :" % (self.addr_size, base_addr))
180
            addr_mux.append ("begin")
181
            addr_mux.append ("%s_irdy = irdy_out;" % r.name)
182
            addr_mux.append ("trdy_out = %s_trdy;" % r.name)
183
            addr_mux.append ("nxt_rd_data = %s_rd_data;" % r.name)
184
            addr_mux.append ("end")
185
 
186
        addr_mux.append("""
187
        default :
188
          begin
189
            trdy_out = 1'b1;
190
            nxt_rd_data = 0;
191
          end
192
        endcase\n""")
193
 
194
        self.blocks.append (comb_block(addr_mux))
195
 
196
 
197
    def verilog (self):
198
        self.build()
199
        result = 'module ' + self.name + ' (\n'
200
        result += string.join (map (lambda x: x.name, self.ports), ',')
201
        result += ');\n'
202
 
203
        # print port list
204
        for p in self.ports:
205
            result += p.declaration() + '\n'
206
 
207
        # print net list
208
        for n in self.nets:
209
            result += n.declaration() + '\n'
210
 
211
        # create all blocks in block list
212
        for b in self.blocks:
213
            result += b
214
 
215
        result += 'endmodule\n'
216
        return result
217
 
218
 
219
 
220
    def add_range (self, r):
221
        self.ranges.append (r)
222
 
223 59 ghutchis
class register_group:
224
    def __init__ (self, mem_mapped=0):
225
        self.base_addr = 0
226
        self.addr_size = 16
227
        self.data_size = 8
228
        self.name = ''
229 110 ghutchis
        self.local_width = 1  # number of address bits consumed
230 59 ghutchis
        self.registers = []
231 110 ghutchis
        self.ports = [port ('input', 'clk'), port('input','reset_n')]
232 59 ghutchis
        self.nets  = []
233 110 ghutchis
        self.interrupts = 0   # if interrupt registers present
234
        self.user = 0         # if user-defined registers present
235
        self.blocks = []
236
        self.registered_read = 0
237
        self.hold_regs = 0
238
        self.hold_inputs = []
239 59 ghutchis
 
240 110 ghutchis
    def top_intf (self):
241
        self.ports.append (port ('input', 'rf_irdy'))
242
        self.ports.append (port ('output', 'rf_trdy'))
243
        self.ports.append (port ('input', 'rf_write'))
244
        self.ports.append (port ('input', 'rf_addr', self.addr_size))
245
        self.ports.append (port ('input', 'rf_wr_data', self.data_size))
246
        self.ports.append (port ('output', 'rf_rd_data', self.data_size))
247
        self.nets.append (net('reg','rf_rd_data',self.data_size))
248
        self.nets.append (net('reg', 'nxt_rf_trdy'))
249
        if (self.registered_read):
250
            self.nets.append (net('reg','nxt_rf_rd_data',self.data_size))
251
        for i in range(0,self.hold_regs):
252
            self.nets.append (net('reg',"xxhold_%d" % i,self.data_size))
253
            self.nets.append (net('reg',"nxt_xxhold_%d" % i,self.data_size))
254
        self.nets.append (net('reg', 'rf_trdy'))
255 59 ghutchis
 
256 110 ghutchis
    def build_load_mux (self):
257
        for i in range(0,self.hold_regs):
258
            nn = "nxt_xxhold_%d" % i
259
            txt = []
260
            for hi,nregs in self.hold_inputs:
261
                high = (i+1) * self.data_size - 1
262
                low = i* self.data_size
263
                if (i < nregs):
264
                    txt.append ("if (%s_rd_stb) %s = %s_in[%d:%d];" % (hi, nn, hi, high, low))
265
                    txt.append ("else if (%s_%d_wr_sel) %s = rf_wr_data; else " % (hi, i, nn))
266
            txt.append("%s = xxhold_%d;" % (nn, i))
267
            self.blocks.append (comb_block(txt))
268
 
269 64 ghutchis
    # create a hook for post-processing to be done after all data has been
270
    # added to the object.
271
    def post (self):
272 110 ghutchis
        self.top_intf()
273
        for reg in self.registers:
274
            self.ports.extend (reg.io())
275
            self.nets.extend (reg.nets())
276
            #self.local_width = int(math.ceil (log2 (len (self.registers))))
277
            self.local_width = self.addr_size;
278
            rnum = 0
279
            for r in self.registers:
280
                r.offset = rnum
281
                rnum += 1
282
        self.build_load_mux()
283 67 ghutchis
        if (self.interrupts):
284
            self.int_ports()
285 64 ghutchis
 
286
    # create port for interrupt pin, as well as port for data output enable
287
    # when interrupt is asserted.
288
    # This block should be called after all register data has been read.
289
    def int_ports (self):
290
        self.ports.append (port ('output','int_n'))
291
        self.nets.append (net ('reg','int_n'))
292 110 ghutchis
        #self.nets.append (net ('reg','int_vec',self.data_size))
293 64 ghutchis
 
294
    def int_logic (self):
295
        int_nets = []
296
        for r in self.registers:
297
            if r.interrupt: int_nets.append (r.name + "_int")
298 110 ghutchis
        self.blocks.append (comb_block (["int_n = ~(" + string.join (int_nets, ' | ') + ");"]))
299 64 ghutchis
 
300 110 ghutchis
    def wait_logic (self):
301
        wait_nets = []
302
        for r in self.registers:
303
            if r.type() == 'user':
304
                wait_nets.append (r.name + "_wait_n")
305
            elif r.type() == 'ext_load':
306
                if r.eindex == 0:
307
                    wait_nets.append (r.name + "_wait_n")
308
        if (len(wait_nets) > 0):
309
            self.blocks.append (comb_block (["if (rf_trdy) nxt_rf_trdy = 0;",
310
                                            "else if (rf_irdy) nxt_rf_trdy = " + ' & '.join (wait_nets) + ";",
311
                                            "else nxt_rf_trdy = 0;"]))
312
        else:
313
            self.blocks.append (comb_block (["if (rf_trdy) nxt_rf_trdy = 0;",
314
                                            "else if (rf_irdy) nxt_rf_trdy = 1;",
315
                                            "else nxt_rf_trdy = 0;"]))
316
        self.blocks.append (seq_block ("clk", ["if (~reset_n) rf_trdy <= #1 0;",
317
                                        "else rf_trdy <= #1 nxt_rf_trdy;"]))
318
        #if (len(wait_nets) > 0):
319
        #    self.blocks.append (comb_block (["wait_n = " + string.join (wait_nets, ' & ') + ";"]))
320
 
321
        if (self.registered_read):
322
            self.blocks.append (seq_block ("clk", ["if (~reset_n) rf_rd_data <= #1 0;", "else if (nxt_rf_trdy) rf_rd_data <= #1 nxt_rf_rd_data;"]))
323
        for i in range (0,self.hold_regs):
324
            self.blocks.append (seq_block ("clk", ["if (~reset_n) xxhold_%d <= 0;"%i,"else xxhold_%d <= nxt_xxhold_%d;" % (i, i)]))
325
 
326
 
327 59 ghutchis
    def global_logic (self):
328
        # create select pin for this block
329 110 ghutchis
        statements = []
330 59 ghutchis
 
331
        # create read and write selects for each register
332
        for r in self.registers:
333 110 ghutchis
            slogic =  "(rf_addr[%d:%d] == %d) & rf_irdy & !rf_write" % (self.local_width-1,0,r.offset)
334 67 ghutchis
            #if r.interrupt:
335
            #    slogic = "%s_int | (%s)" % (r.name, slogic)
336 64 ghutchis
            s = "%s_rd_sel = %s;" % (r.name,slogic)
337 59 ghutchis
            statements.append (s)
338
            if r.write_cap():
339 110 ghutchis
                s = "%s_wr_sel = (rf_addr[%d:%d] == %d) & rf_irdy & rf_write;\n" % (r.name,self.local_width-1,0,r.offset)
340 59 ghutchis
                statements.append (s)
341
 
342
        return comb_block (statements)
343
 
344
    def read_mux (self):
345
        s = ''
346 64 ghutchis
        sments = []
347
        rd_sel_list = []
348
 
349 67 ghutchis
        # create data-output mux
350 64 ghutchis
        sments.append ("case (1'b1)")
351 110 ghutchis
        if (self.registered_read):
352
            rd_target = "nxt_rf_rd_data"
353
        else:
354
            rd_target = "rf_rd_data"
355 59 ghutchis
        for r in self.registers:
356 110 ghutchis
            sments.append ("  %s_rd_sel : %s = %s;" % (r.name, rd_target, r.name))
357 64 ghutchis
            rd_sel_list.append (r.name + "_rd_sel")
358 110 ghutchis
        #if (self.interrupts):
359
        #    sments.append ("  default : rd_data = int_vec;")
360
        sments.append ("  default : %s = %d'b0;" % (rd_target, self.data_size))
361 64 ghutchis
        sments.append ("endcase")
362 59 ghutchis
 
363 110 ghutchis
        #sments.append ("doe = %s;" % string.join (rd_sel_list, ' | '))
364 64 ghutchis
 
365
        return comb_block (sments)
366 59 ghutchis
 
367
 
368
    def verilog (self):
369 64 ghutchis
        self.post()
370
 
371 59 ghutchis
        result = 'module ' + self.name + ' (\n'
372
        result += string.join (map (lambda x: x.name, self.ports), ',')
373
        result += ');\n'
374
 
375
        # print port list
376
        for p in self.ports:
377
            result += p.declaration() + '\n'
378
 
379
        # print net list
380
        for n in self.nets:
381
            result += n.declaration() + '\n'
382
 
383
        # create global logic
384
        result += self.global_logic()
385
        result += self.read_mux()
386 110 ghutchis
        if (self.interrupts > 0): self.int_logic()
387
        self.wait_logic()
388
 
389
        # create all blocks in block list
390
        for b in self.blocks:
391
            result += b
392 59 ghutchis
 
393
        # print function blocks
394
        for r in self.registers:
395
            result += r.verilog_body()
396
 
397 64 ghutchis
        result += 'endmodule\n'
398 59 ghutchis
        return result
399
 
400 110 ghutchis
    # calculate number of holding registers required and update
401
    # hold_regs internal property
402
    def calc_hold (self, width):
403
        hregs = width / self.data_size
404
        if (width % self.data_size) != 0:
405
            hregs += 1
406
        if (hregs > self.hold_regs):
407
            self.hold_regs = hregs
408
        return hregs
409
 
410 64 ghutchis
    def add_register (self, type, params):
411
    #def add_register (self, name, type, width):
412 59 ghutchis
        if (type == 'status'):
413 64 ghutchis
            self.add (status_reg (params['name'],params['width']))
414 59 ghutchis
        elif (type == 'config'):
415 64 ghutchis
            self.add (config_reg (params['name'],params['width'],params['default']))
416 110 ghutchis
        elif (type == 'int_msk'):
417 64 ghutchis
            r2 = config_reg (params['name'] + "_msk",params['width'],params['default'])
418 110 ghutchis
            r1 = int_msk_reg (params['name'],r2,params['width'])
419 64 ghutchis
            self.add (r1)
420
            self.add (r2)
421
            self.interrupts += 1
422
        elif (type == 'soft_set'):
423
            self.add (soft_set_reg(params['name'],params['width'],params['default']))
424
        elif (type == 'read_stb'):
425
            self.add (read_stb_reg (params['name'],params['width']))
426
        elif (type == 'write_stb'):
427 67 ghutchis
            self.add (write_stb_reg (params['name'],params['width'],params['default']))
428 110 ghutchis
        elif (type == 'user'):
429
            self.user = 1
430
            self.add (user_reg (params['name'],params['width']))
431
        elif (type == 'ext_load'):
432
            width = params['width']
433
            regs = self.calc_hold (width)
434
            print "ext_load %s, splitting into %d regs" % (params['name'],regs)
435
            for i in range(0,regs):
436
                last = (i == (regs-1))
437
                self.add (ext_load_reg(params['name'],self.data_size,i,params['width'],last))
438
            self.hold_inputs.append ( (params['name'], regs) )
439
        elif (type == 'count'):
440
            self.add (count_reg (params['name'],params['width']))
441 59 ghutchis
        else:
442
            print "Unknown register type",type
443 64 ghutchis
 
444 59 ghutchis
    def add (self, reg):
445
        self.registers.append (reg)
446 110 ghutchis
        #self.ports.extend (reg.io())
447
        #self.nets.extend (reg.nets())
448
        #self.local_width = int(math.ceil (log2 (len (self.registers))))
449
        #rnum = 0
450
        #for r in self.registers:
451
        #    r.offset = rnum
452
        #    rnum += 1
453 59 ghutchis
 
454
class basic_register:
455
    def __init__ (self, name='', width=0):
456
        self.offset = 0
457
        self.width  = width
458
        self.name   = name
459 64 ghutchis
        self.interrupt = 0
460 59 ghutchis
 
461
    def verilog_body (self):
462
        pass
463
 
464 110 ghutchis
    def type (self):
465
        return 'basic'
466
 
467 59 ghutchis
    def io (self):
468
        return []
469
 
470
    def nets (self):
471
        return []
472
 
473
    def write_cap (self):
474
        return 0
475
 
476 64 ghutchis
    def id_comment (self):
477 110 ghutchis
        return "// %s: %s\n" % (self.type(), self.name)
478 64 ghutchis
 
479 59 ghutchis
class status_reg (basic_register):
480
    def __init__ (self, name='', width=0):
481
        basic_register.__init__(self, name, width)
482 110 ghutchis
 
483
    def type (self):
484
        return 'status'
485 59 ghutchis
 
486
    def verilog_body (self):
487 110 ghutchis
        return self.id_comment()
488 59 ghutchis
 
489
    def io (self):
490 64 ghutchis
        return [port('input', self.name, self.width)]
491 59 ghutchis
 
492
    def nets (self):
493 64 ghutchis
        return [ net('reg', self.name + '_rd_sel')]
494 59 ghutchis
 
495 110 ghutchis
class ext_load_reg (basic_register):
496
    def __init__ (self, name='', width=0, eindex=0, twidth=0, last=0):
497
        basic_register.__init__(self, name+"_%d" % eindex, width)
498
        self.fullname = name
499
        self.eindex = eindex
500
        self.twidth = twidth
501
        self.last = last
502
        print "Adding %s eindex %d" % (name, eindex)
503
 
504
    def type (self): return "ext_load"
505
 
506
    def write_cap (self): return 1
507
 
508
    def verilog_body (self):
509
        print "Building %s eindex %d" % (self.name, self.eindex)
510
 
511
        txt = ""
512
        low = self.eindex * self.width
513
        high = (self.eindex + 1) * self.width - 1
514
        txt += "assign %s = xxhold_%d;\n" % (self.name, self.eindex)
515
        txt += "assign %s_out[%d:%d] = xxhold_%d;\n" % (self.fullname, high, low, self.eindex)
516
        if (self.eindex == 0):
517
            #txt += "assign %s_rd_stb = %s_rd_sel & !rf_trdy;\n" % (self.fullname,self.name)
518
            sm = state_machine ("sm_" + self.name)
519
            sm.add_state ('idle')
520
            sm.add_state ('rd_req')
521
            sm.add_state ('done')
522
 
523
            sm.add_trans ('idle','rd_req',self.name+"_rd_sel", self.name+"_wait_n = 0")
524
            sm.add_trans ('rd_req','done',"1'b1")
525
            sm.add_moore ('rd_req',self.name+"_wait_n = 1'b0")
526
            sm.add_moore ('rd_req',self.fullname+"_rd_stb = 1'b1")
527
 
528
            sm.add_trans ('done','idle',"~%s_rd_sel" % self.name)
529
 
530
            sm.add_default (self.fullname+"_rd_stb", "1'b0")
531
            sm.add_default (self.name+"_wait_n", "1'b1")
532
            txt += sm.verilog()
533
 
534
        if (self.last):
535
            txt += seq_block ("clk", ["if (~reset_n) %s_wr_stb <= 1'b0;" % self.fullname,
536
                                      "else %s_wr_stb <= %s_%d_wr_sel;" % (self.fullname,self.fullname,self.eindex)])
537
        return txt
538
 
539
    def nets (self):
540
        nets = [net('reg', self.name + '_rd_sel'),
541
                net('reg', self.name + '_wr_sel'),
542
                net('wire', self.name, self.width)]
543
        if (self.eindex == 0):
544
            nets.append (net('reg', self.name + "_wait_n"))
545
            nets.append (net('reg',"sm_%s_state" % self.name, 2))
546
            nets.append (net('reg',"nxt_sm_%s_state" % self.name, 2))
547
        if (self.last):
548
            nets.append (net('reg', self.fullname + '_rd_stb'))
549
            nets.append (net('reg', self.fullname + '_wr_stb'))
550
        return nets
551
 
552
    def io (self):
553
        # ports are all tied to the 0 eindex register
554
        plist = []
555
        if (self.eindex == 0):
556
            plist.append (port('input', self.fullname+"_in", self.twidth))
557
            plist.append (port('output', self.fullname+"_out", self.twidth))
558
            plist.append (port('output', self.fullname+"_rd_stb", 1))
559
            plist.append (port('output', self.fullname+"_wr_stb", 1))
560
        return plist
561
 
562
 
563 59 ghutchis
class config_reg (basic_register):
564 64 ghutchis
    def __init__ (self, name='', width=0, default=0):
565 59 ghutchis
        basic_register.__init__(self, name, width)
566 64 ghutchis
        self.default = default
567 110 ghutchis
        self.fields = []
568 59 ghutchis
 
569
    def verilog_body (self):
570 110 ghutchis
        vstr = self.id_comment()
571
        statements = ["if (%s_wr_sel) nxt_%s = %s;" % (self.name, self.name, 'rf_wr_data'),
572
                      "else nxt_%s = %s;" % tuple([self.name] * 2)]
573
        vstr += comb_block (statements)
574
        statements = ["if (~reset_n) %s <= #1 %d'h%x;" % (self.name, self.width, self.default),
575
                      "else %s <= #1 nxt_%s;" % tuple([self.name] * 2)]
576
 
577
        vstr += seq_block ('clk', statements)
578
        if (len(self.fields) != 0):
579
            vstr += "assign {"
580
            vstr += ','.join (map(lambda(x):x.name,self.fields))
581
            vstr += "} = %s;\n" % self.name
582
        return vstr
583 59 ghutchis
 
584 110 ghutchis
    def type (self):
585
        return 'config'
586
 
587 59 ghutchis
    def io (self):
588 110 ghutchis
        if (len(self.fields) == 0):
589
            return [ port('output',self.name, self.width) ]
590
        else:
591
            plist = []
592
            for fld in self.fields:
593
                plist.append (port ('output', fld.name, fld.width))
594
            return plist
595 59 ghutchis
 
596
    def nets (self):
597 64 ghutchis
        return [ net('reg', self.name, self.width),
598 110 ghutchis
                 net('reg', "nxt_"+self.name, self.width),
599 64 ghutchis
                 net('reg', self.name + '_rd_sel'),
600
                 net('reg', self.name + '_wr_sel')]
601 59 ghutchis
 
602
    def write_cap (self):
603
        return 1
604 64 ghutchis
 
605 110 ghutchis
class count_reg (config_reg):
606 90 ghutchis
    def __init__ (self, name='', width=0, default=0):
607 110 ghutchis
        config_reg.__init__(self, name, width)
608 90 ghutchis
        self.default = default
609
 
610
    def verilog_body (self):
611 110 ghutchis
        txt = self.id_comment()
612
        alist = (self.name, self.name, self.width, self.name, self.name)
613
        statements = [
614
                      "if (%s_wr_sel) nxt_%s = %s;" % (self.name, self.name, 'rf_wr_data'),
615
                      "else if (%s_inc && (%s != {%d{1'b1}})) nxt_%s = %s + 1;" % alist,
616
                      "else nxt_%s = %s;" % (self.name,self.name)
617 90 ghutchis
                      ]
618 110 ghutchis
        txt += comb_block (statements)
619
        statements = ["if (~reset_n) %s <= #1 %d;" % (self.name, self.default),
620
                      "else %s <= #1 nxt_%s;" % (self.name, self.name)
621
                      ]
622
        txt += seq_block ('clk', statements)
623
        return txt
624
 
625 90 ghutchis
    def io (self):
626 110 ghutchis
        return [ port('input',self.name + "_inc", 1) ]
627 90 ghutchis
 
628 110 ghutchis
class int_msk_reg (basic_register):
629
    def __init__ (self, name, mask_reg, width=0):
630 64 ghutchis
        basic_register.__init__(self, name, width)
631
        self.mask_reg = mask_reg
632
        self.interrupt = 1
633
 
634
    def verilog_body (self):
635 110 ghutchis
        text = self.id_comment()
636
        statements = ["nxt_%s = (%s_set | %s) & ~( {%d{%s}} & %s);" % (self.name, self.name, self.name, self.width, self.name + '_wr_sel', 'rf_wr_data')]
637
        statements += ["nxt_%s_int = |(%s & ~%s);" % (self.name, self.name, self.mask_reg.name)]
638
        text += comb_block (statements)
639
        statements = ["if (~reset_n) %s <= #1 %d;" % (self.name, 0),
640
                      "else %s <= #1 nxt_%s;" % (self.name, self.name)]
641
        text += seq_block ('clk', statements)
642
        statements = ["if (~reset_n) %s_int <= #1 0;" % self.name,
643
                      "else %s_int <= nxt_%s_int;" % (self.name, self.name)
644 64 ghutchis
                      ]
645 110 ghutchis
        text +=  seq_block ('clk', statements)
646
        return text
647 64 ghutchis
 
648 110 ghutchis
    def type (self):
649
        return 'int_msk'
650
 
651 64 ghutchis
    def io (self):
652
        return [ port('input',self.name+"_set", self.width) ]
653
 
654
    def nets (self):
655
        return [ net('reg', self.name + '_rd_sel'),
656
                 net('reg', self.name, self.width),
657 110 ghutchis
                 net('reg', "nxt_"+self.name, self.width),
658 64 ghutchis
                 net('reg', self.name + '_wr_sel'),
659 110 ghutchis
                 net('reg', 'nxt_'+self.name + '_int'),
660 64 ghutchis
                 net('reg', self.name + '_int')]
661
 
662
    def write_cap (self):
663
        return 1
664
 
665 110 ghutchis
class soft_set_reg (config_reg):
666 64 ghutchis
    def __init__ (self, name='', width=0, default=0):
667
        basic_register.__init__(self, name, width)
668
        self.default = default
669
 
670
    def verilog_body (self):
671 110 ghutchis
        txt = self.id_comment()
672
        statements = [
673
                      "nxt_%s = ( ({%d{%s}} & %s) | %s) & ~(%s);" %
674
                            (self.name, self.width, self.name+'_wr_sel', 'rf_wr_data',
675 64 ghutchis
                             self.name, self.name + '_clr')
676
                      ]
677 110 ghutchis
        txt += comb_block (statements)
678
        statements = ["if (~reset_n) %s <= #1 %d;" % (self.name, self.default),
679
                      "else %s <= #1 nxt_%s;" % (self.name, self.name)
680
                      ]
681
        txt += seq_block ('clk', statements)
682
        if (len(self.fields) != 0):
683
            txt += "assign {"
684
            txt += ','.join (map(lambda(x):x.name,self.fields))
685
            txt += "} = %s;\n" % self.name
686
        return txt
687 64 ghutchis
 
688 110 ghutchis
    def type (self):
689
        return 'soft_set'
690
 
691 64 ghutchis
    def io (self):
692 110 ghutchis
        return config_reg.io(self) + [port ('input',self.name+"_clr", self.width)]
693 64 ghutchis
 
694 110 ghutchis
    #def nets (self):
695
    #    return [ net('reg', self.name, self.width),
696
    #             net('reg', "nxt_"+self.name, self.width),
697
    #             net('reg', self.name + '_rd_sel'),
698
    #             net('reg', self.name + '_wr_sel')]
699 64 ghutchis
 
700 110 ghutchis
    #def write_cap (self):
701
    #    return 1
702 64 ghutchis
 
703
class write_stb_reg (config_reg):
704
    def __init__ (self, name='', width=0, default=0):
705
        config_reg.__init__(self, name, width, default)
706
 
707
    def verilog_body (self):
708 110 ghutchis
        txt = self.id_comment()
709
        statements = [
710
                      "if (%s_wr_sel) nxt_%s = %s;" % (self.name, self.name, 'rf_wr_data'),
711
                      "else nxt_%s = %s;" % (self.name, self.name)]
712
        txt += comb_block (statements)
713
        statements = ["if (~reset_n) %s <= #1 %d;" % (self.name, self.default),
714
                      "else %s <= #1 nxt_%s;" % (self.name, self.name)]
715
        txt += seq_block('clk',statements)
716
        statements = [
717
                      "if (~reset_n) %s_stb <= #1 0;" % (self.name),
718
                      "else %s_stb <= #1 %s_wr_sel & rf_trdy;" % (self.name, self.name),
719 64 ghutchis
                      ]
720 110 ghutchis
        txt += seq_block ('clk', statements)
721 64 ghutchis
 
722 110 ghutchis
        if (len(self.fields) != 0):
723
            txt += "assign {"
724
            txt += ','.join (map(lambda(x):x.name,self.fields))
725
            txt += "} = %s;\n" % self.name
726
 
727
        return txt+seq_block ('clk', statements)
728
 
729
    def type (self):
730
        return 'write_stb'
731
 
732 64 ghutchis
    def io (self):
733
        io_list = config_reg.io (self)
734
        io_list.append ( port('output',self.name+"_stb") )
735
        return io_list
736
 
737
    def nets (self):
738
        net_list = config_reg.nets (self)
739
        net_list.append ( net('reg', self.name + "_stb") )
740
        return net_list
741
 
742
class read_stb_reg (status_reg):
743
    def __init__ (self, name='', width=0):
744
        status_reg.__init__(self, name, width)
745
 
746
    def verilog_body (self):
747
        statements = [
748 110 ghutchis
                      "if (~reset_n) %s_stb <= #1 0;" % (self.name),
749
                      "else %s_stb <= #1 %s_rd_sel & rf_trdy;" % (self.name, self.name),
750 64 ghutchis
                      ]
751
        return self.id_comment() + seq_block ('clk', statements)
752
 
753 110 ghutchis
    def type (self):
754
        return 'read_stb'
755
 
756 64 ghutchis
    def io (self):
757
        io_list = status_reg.io (self)
758
        io_list.append (port('output',self.name+"_stb"))
759
        return io_list
760
 
761
    def nets (self):
762
        net_list = status_reg.nets(self)
763
        net_list.append (net('reg',self.name + '_stb'))
764
        return net_list
765 110 ghutchis
 
766
class state_machine:
767
    def __init__ (self, name='', clk="clk", reset="reset_n"):
768
        self.name = name
769
        self.states = {}
770
        self.trans = []
771
        self.clk = clk
772
        self.reset = reset
773
        self.idle = ""
774
        self.defaults = []
775
 
776
    def add_state (self, name):
777
        self.states[name] = []
778
        if (self.idle == ""):
779
            self.idle = name
780
 
781
    def add_trans (self, st_from, st_to, cond, asrt=''):
782
        self.states[st_from].append ( (st_to, cond, asrt) )
783
 
784
    def add_moore (self, st_name, asrt):
785
        self.states[st_name].append ( (st_name, '1', asrt) )
786
 
787
    def add_default (self, signal, value):
788
        self.defaults.append ( (signal, value) )
789
 
790
    def verilog (self):
791
        code = "// state machine %s\n" % self.name
792
 
793
        # create state names
794
        snum = 0
795
        for st in self.states.keys():
796
            code += "parameter st_%s_%s = %d;\n" % (self.name, st, snum)
797
            snum += 1
798
 
799
        # create combinatorial block
800
        cblk = []
801
        for d in self.defaults:
802
            cblk.append ( "%s = %s;" % d)
803
        cblk.append ("%s = %s;" % ("nxt_" + self.name + "_state", self.name + "_state"))
804
        cblk.append ("case (%s)" % (self.name + "_state"))
805
        for st in self.states.keys():
806
            cblk.append ( "st_%s_%s : " % (self.name, st))
807
            cblk.append ( "  begin")
808
            first = 1
809
            moore = []
810
            for c in self.states[st]:
811
                if (c[0] == st) and (c[1] == "1"):
812
                    moore.append (c[2])
813
                else:
814
                    if (not first): statement = "    else if"
815
                    else:
816
                        statement = "    if"
817
                        first = 0
818
                    cblk.append ( "%s (%s)" % (statement, c[1]))
819
                    cblk.append ("    begin")
820
                    if (c[0] != st):
821
                        cblk.append ( "    nxt_%s_state = st_%s_%s;" % (self.name, self.name, c[0]))
822
                    if (c[2] != ""):
823
                        cblk.append ( "    " + c[2] + ";")
824
                    cblk.append ( "    end")
825
            for m in moore:
826
                cblk.append ("    %s;" % m)
827
            cblk.append ("  end")
828
        cblk.append ( "endcase")
829
        code += comb_block (cblk)
830
 
831
        # create sequential block
832
        cblk = []
833
        cblk.append ("if(~%s)" % self.reset)
834
        cblk.append ("%s_state <= #1 st_%s_%s;" % (self.name, self.name, self.idle))
835
        cblk.append ("else")
836
        cblk.append ("%s_state <= #1 nxt_%s_state;" % (self.name, self.name))
837
        code += seq_block (self.clk, cblk)
838
        return code
839
 
840
class user_reg (basic_register):
841
    def __init__ (self, name='', width=0):
842
        basic_register.__init__(self, name, width)
843
 
844
    def io (self):
845
        io_list = []
846
        io_list.append (port('output',self.name+"_wr_stb"))
847
        io_list.append (port('output',self.name+"_rd_stb"))
848
        io_list.append (port('output',self.name+"_wr_data", self.width))
849
        io_list.append (port('input',self.name+"_rd_data",self.width))
850
        io_list.append (port('input',self.name+"_rd_ack"))
851
        io_list.append (port('input',self.name+"_wr_ack"))
852
        return io_list
853
 
854
    def type (self):
855
        return 'user'
856
 
857
    def nets (self):
858
        net_list = []
859
        net_list.append (net('reg',self.name+"_rd_sel"))
860
        net_list.append (net('reg',self.name+"_wr_sel"))
861
        net_list.append (net('reg',self.name+"_rd_stb"))
862
        net_list.append (net('reg',self.name+"_wr_stb"))
863
        net_list.append (net('reg',self.name+"_wait_n"))
864
        net_list.append (net('reg',self.name,self.width))
865
        net_list.append (net('reg',self.name+"_wr_data",self.width))
866
        net_list.append (net('reg',"sm_%s_state" % self.name, 2))
867
        net_list.append (net('reg',"nxt_sm_%s_state" % self.name, 2))
868
        return net_list
869
 
870
    def write_cap (self):
871
        return 1
872
 
873
    def verilog_body (self):
874
        sm = state_machine ("sm_" + self.name)
875
        sm.add_state ('idle')
876
        sm.add_state ('rd_req')
877
        sm.add_state ('wr_req')
878
        sm.add_state ('w_clear')
879
        #sm.add_state ('rd_ack')
880
        #sm.add_state ('wr_ack')
881
        sm.add_trans ('idle','rd_req',self.name+"_rd_sel", self.name+"_wait_n = 0")
882
        sm.add_trans ('idle','wr_req',self.name+"_wr_sel", self.name+"_wait_n = 0")
883
        sm.add_trans ('rd_req', 'w_clear', self.name+"_rd_ack", "")
884
        #sm.add_trans ('rd_req', 'rd_ack', '1','')
885
        sm.add_trans ('rd_req', 'rd_req', "!"+self.name+"_rd_ack", self.name+"_wait_n = 0")
886
        sm.add_moore ('rd_req', self.name+"_rd_stb = 1")
887
        sm.add_moore ('wr_req', self.name+"_wr_stb = 1")
888
        sm.add_trans ('wr_req', 'w_clear', self.name+"_wr_ack", "")
889
        sm.add_trans ('wr_req', 'wr_req', "!"+self.name+"_wr_ack", self.name+"_wait_n = 0")
890
 
891
        # added w_clear to avoid duplicate requests on interface
892
        sm.add_trans ('w_clear', 'idle', "~(%s_rd_sel | %s_wr_sel)" % (self.name,self.name), "")
893
 
894
        sm.add_default (self.name+"_rd_stb", "0")
895
        sm.add_default (self.name+"_wr_stb", "0")
896
        sm.add_default (self.name+"_wait_n", "1")
897
 
898
        comb = comb_block (["%s_wr_data = rf_wr_data;" % self.name,
899
                            "%s = %s_rd_data;" % (self.name, self.name)])
900
 
901
        return sm.verilog() + comb

powered by: WebSVN 2.1.0

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