|
| 1 | +from __future__ import absolute_import |
| 2 | +from __future__ import print_function |
| 3 | + |
| 4 | +import veriloggen.core.vtypes as vtypes |
| 5 | +import veriloggen.core.module as module |
| 6 | + |
| 7 | + |
| 8 | +def mkMaddCore(index, awidth=32, bwidth=32, cwidth=32, |
| 9 | + asigned=True, bsigned=True, csigned=True, depth=6): |
| 10 | + |
| 11 | + retwidth = max(awidth + bwidth, cwidth) |
| 12 | + |
| 13 | + m = module.Module('madd_core_%d' % index) |
| 14 | + |
| 15 | + clk = m.Input('CLK') |
| 16 | + update = m.Input('update') |
| 17 | + |
| 18 | + a = m.Input('a', awidth) |
| 19 | + b = m.Input('b', bwidth) |
| 20 | + c = m.Input('c', cwidth) |
| 21 | + d = m.Output('d', retwidth) |
| 22 | + |
| 23 | + _a = m.Reg('_a', awidth, signed=asigned) |
| 24 | + _b = m.Reg('_b', bwidth, signed=bsigned) |
| 25 | + _c = m.Reg('_c', cwidth, signed=csigned) |
| 26 | + _mul = m.Wire('_mul', retwidth, signed=True) |
| 27 | + _madd = m.Wire('_madd', retwidth, signed=True) |
| 28 | + _pipe_madd = [m.Reg('_pipe_madd%d' % i, retwidth, signed=True) |
| 29 | + for i in range(depth - 1)] |
| 30 | + |
| 31 | + __a = _a |
| 32 | + __b = _b |
| 33 | + __c = _c |
| 34 | + if not asigned: |
| 35 | + __a = vtypes.SystemTask( |
| 36 | + 'signed', vtypes.Cat(vtypes.Int(0, width=1), _a)) |
| 37 | + if not bsigned: |
| 38 | + __b = vtypes.SystemTask( |
| 39 | + 'signed', vtypes.Cat(vtypes.Int(0, width=1), _b)) |
| 40 | + if not csigned: |
| 41 | + __c = vtypes.SystemTask( |
| 42 | + 'signed', vtypes.Cat(vtypes.Int(0, width=1), _c)) |
| 43 | + |
| 44 | + m.Assign(_mul(__a * __b)) |
| 45 | + m.Assign(_madd(_mul + __c)) |
| 46 | + m.Assign(d(_pipe_madd[depth - 2])) |
| 47 | + |
| 48 | + m.Always(vtypes.Posedge(clk))( |
| 49 | + vtypes.If(update)( |
| 50 | + _a(a), |
| 51 | + _b(b), |
| 52 | + _c(c), |
| 53 | + _pipe_madd[0](_madd), |
| 54 | + [_pipe_madd[i](_pipe_madd[i - 1]) for i in range(1, depth - 1)] |
| 55 | + )) |
| 56 | + |
| 57 | + return m |
| 58 | + |
| 59 | + |
| 60 | +def mkMadd(index, awidth=32, bwidth=32, cwidth=32, |
| 61 | + asigned=True, bsigned=True, csigned=True, depth=6): |
| 62 | + |
| 63 | + if awidth < 0: |
| 64 | + raise ValueError("data width must be greater than 0.") |
| 65 | + if bwidth < 0: |
| 66 | + raise ValueError("data width must be greater than 0.") |
| 67 | + if cwidth < 0: |
| 68 | + raise ValueError("data width must be greater than 0.") |
| 69 | + if depth < 2: |
| 70 | + raise ValueError("depth must be greater than 2.") |
| 71 | + |
| 72 | + retwidth = max(awidth + bwidth, cwidth) |
| 73 | + |
| 74 | + madd = mkMaddCore(index, awidth, bwidth, cwidth, |
| 75 | + asigned, bsigned, csigned, depth) |
| 76 | + |
| 77 | + m = module.Module('madd_%d' % index) |
| 78 | + |
| 79 | + clk = m.Input('CLK') |
| 80 | + update = m.Input('update') |
| 81 | + a = m.Input('a', awidth) |
| 82 | + b = m.Input('b', bwidth) |
| 83 | + c = m.Input('c', cwidth) |
| 84 | + d = m.Output('d', retwidth) |
| 85 | + |
| 86 | + ports = [('CLK', clk), ('update', update), |
| 87 | + ('a', a), ('b', b), ('c', c), ('d', d)] |
| 88 | + m.Instance(madd, 'madd', ports=ports) |
| 89 | + |
| 90 | + return m |
| 91 | + |
| 92 | + |
| 93 | +# global madd count |
| 94 | +index_count = 0 |
| 95 | + |
| 96 | + |
| 97 | +def get_madd(awidth=32, bwidth=32, cwidth=32, |
| 98 | + asigned=True, bsigned=True, csigned=True, depth=6): |
| 99 | + |
| 100 | + global index_count |
| 101 | + madd = mkMadd(index_count, awidth, bwidth, cwidth, |
| 102 | + asigned, bsigned, csigned, depth) |
| 103 | + index_count += 1 |
| 104 | + return madd |
| 105 | + |
| 106 | + |
| 107 | +def reset(): |
| 108 | + global index_count |
| 109 | + index_count = 0 |
0 commit comments