|
| 1 | +from __future__ import absolute_import |
| 2 | +from __future__ import print_function |
| 3 | +import sys |
| 4 | +import os |
| 5 | +import math |
| 6 | + |
| 7 | +# the next line can be removed after installation |
| 8 | +sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) |
| 9 | + |
| 10 | +from veriloggen import * |
| 11 | +import veriloggen.dataflow as dataflow |
| 12 | + |
| 13 | +def complex_add(x, y): |
| 14 | + a = x[0] |
| 15 | + b = x[1] |
| 16 | + c = y[0] |
| 17 | + d = y[1] |
| 18 | + return (a + c), (b + d) |
| 19 | + |
| 20 | +def complex_sub(x, y): |
| 21 | + a = x[0] |
| 22 | + b = x[1] |
| 23 | + c = y[0] |
| 24 | + d = y[1] |
| 25 | + return (a - c), (b - d) |
| 26 | + |
| 27 | +def complex_mult(x, y): |
| 28 | + a = x[0] |
| 29 | + b = x[1] |
| 30 | + c = y[0] |
| 31 | + d = y[1] |
| 32 | + # (a + jb) * (c + jd) = ac - bd + j(ad + bc) |
| 33 | + ac = a * c |
| 34 | + bd = b * d |
| 35 | + ad = a * d |
| 36 | + bc = b * c |
| 37 | + re = ac - bd |
| 38 | + im = ad + bc |
| 39 | + return re, im |
| 40 | + |
| 41 | +def radix2(x, y, c): |
| 42 | + d0 = complex_add(x, y) |
| 43 | + d1 = complex_sub(x, y) |
| 44 | + r0 = d0 # as-is |
| 45 | + r1 = complex_mult(d1, c) |
| 46 | + return r0, r1 |
| 47 | + |
| 48 | +def mkRadix2(datawidth=32): |
| 49 | + din0 = (dataflow.Variable('din0re', width=datawidth), |
| 50 | + dataflow.Variable('din0im', width=datawidth)) |
| 51 | + din1 = (dataflow.Variable('din1re', width=datawidth), |
| 52 | + dataflow.Variable('din1im', width=datawidth)) |
| 53 | + cnst = (dataflow.Variable('cnstre', width=datawidth), |
| 54 | + dataflow.Variable('cnstim', width=datawidth)) |
| 55 | + |
| 56 | + r0, r1 = radix2(din0, din1, cnst) |
| 57 | + |
| 58 | + r0[0].output('dout0re') |
| 59 | + r0[1].output('dout0im') |
| 60 | + r1[0].output('dout1re') |
| 61 | + r1[1].output('dout1im') |
| 62 | + |
| 63 | + rslt = list(r0) + list(r1) |
| 64 | + df = dataflow.Dataflow(*rslt) |
| 65 | + |
| 66 | + m = df.to_module('radix2') |
| 67 | + |
| 68 | + try: |
| 69 | + df.draw_graph() |
| 70 | + except: |
| 71 | + print('Dataflow graph could not be generated.', file=sys.stderr) |
| 72 | + |
| 73 | + return m |
| 74 | + |
| 75 | +def mkTest(datawidth=32): |
| 76 | + m = Module('test') |
| 77 | + |
| 78 | + main = mkRadix2(datawidth) |
| 79 | + |
| 80 | + params = m.copy_params(main) |
| 81 | + ports = m.copy_sim_ports(main) |
| 82 | + |
| 83 | + clk = ports['CLK'] |
| 84 | + rst = ports['RST'] |
| 85 | + |
| 86 | + din0re = ports['din0re'] |
| 87 | + din0im = ports['din0im'] |
| 88 | + din1re = ports['din1re'] |
| 89 | + din1im = ports['din1im'] |
| 90 | + cnstre = ports['cnstre'] |
| 91 | + cnstim = ports['cnstim'] |
| 92 | + dout0re = ports['dout0re'] |
| 93 | + dout0im = ports['dout0im'] |
| 94 | + dout1re = ports['dout1re'] |
| 95 | + dout1im = ports['dout1im'] |
| 96 | + |
| 97 | + uut = m.Instance(main, 'uut', |
| 98 | + params=m.connect_params(main), |
| 99 | + ports=m.connect_ports(main)) |
| 100 | + |
| 101 | + reset_done = m.Reg('reset_done', initval=0) |
| 102 | + reset_stmt = [] |
| 103 | + reset_stmt.append( reset_done(0) ) |
| 104 | + reset_stmt.append( din0re(2) ) |
| 105 | + reset_stmt.append( din0im(0) ) |
| 106 | + reset_stmt.append( din1re(1) ) |
| 107 | + reset_stmt.append( din1im(0) ) |
| 108 | + reset_stmt.append( cnstre(0) ) |
| 109 | + reset_stmt.append( cnstim(1) ) |
| 110 | + |
| 111 | + simulation.setup_waveform(m, uut) |
| 112 | + simulation.setup_clock(m, clk, hperiod=5) |
| 113 | + init = simulation.setup_reset(m, rst, reset_stmt, period=100) |
| 114 | + |
| 115 | + nclk = simulation.next_clock |
| 116 | + |
| 117 | + init.add( |
| 118 | + Delay(1000), |
| 119 | + reset_done(1), |
| 120 | + nclk(clk), |
| 121 | + Delay(10000), |
| 122 | + Systask('finish'), |
| 123 | + ) |
| 124 | + |
| 125 | + send_fsm = FSM(m, 'send_fsm', clk, rst) |
| 126 | + send_fsm.goto_next(cond=reset_done) |
| 127 | + |
| 128 | + for i in range(10): |
| 129 | + send_fsm.goto_next() |
| 130 | + |
| 131 | + send_fsm.add( Systask('finish') ) |
| 132 | + |
| 133 | + send_fsm.make_always() |
| 134 | + |
| 135 | + return m |
| 136 | + |
| 137 | +if __name__ == '__main__': |
| 138 | + test = mkTest() |
| 139 | + verilog = test.to_verilog('tmp.v') |
| 140 | + print(verilog) |
| 141 | + |
| 142 | + # run simulator (Icarus Verilog) |
| 143 | + sim = simulation.Simulator(test) |
| 144 | + rslt = sim.run() # display=False |
| 145 | + #rslt = sim.run(display=True) |
| 146 | + print(rslt) |
0 commit comments