|
| 1 | +from __future__ import absolute_import |
| 2 | +from __future__ import print_function |
| 3 | +import sys |
| 4 | +import os |
| 5 | + |
| 6 | +# the next line can be removed after installation |
| 7 | +sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) |
| 8 | + |
| 9 | +from veriloggen import * |
| 10 | + |
| 11 | +def mkLed(): |
| 12 | + m = Module('blinkled') |
| 13 | + inc = m.Localparam('INC', Int(1) + Int(2) * Int(3)) |
| 14 | + width = m.Parameter('WIDTH', Int(1) * (Int(2) + Int(3))) |
| 15 | + clk = m.Input('CLK') |
| 16 | + rst = m.Input('RST') |
| 17 | + led = m.OutputReg('LED', width) |
| 18 | + count = m.Reg('count', 32) |
| 19 | + |
| 20 | + m.Always(Posedge(clk))( |
| 21 | + If(rst)( |
| 22 | + count(0) |
| 23 | + ).Else( |
| 24 | + If(count == 1023)( |
| 25 | + count(0) |
| 26 | + ).Else( |
| 27 | + count(count + Int(1)) |
| 28 | + ) |
| 29 | + )) |
| 30 | + |
| 31 | + m.Always(Posedge(clk))( |
| 32 | + If(rst)( |
| 33 | + led(0) |
| 34 | + ).Else( |
| 35 | + If(count == 1024 - 1)( |
| 36 | + led(led + inc) |
| 37 | + ) |
| 38 | + )) |
| 39 | + |
| 40 | + return m |
| 41 | + |
| 42 | +def mkTest(): |
| 43 | + m = Module('test') |
| 44 | + |
| 45 | + # target instance |
| 46 | + led = mkLed() |
| 47 | + |
| 48 | + # copy paras and ports |
| 49 | + params = m.copy_params(led) |
| 50 | + ports = m.copy_sim_ports(led) |
| 51 | + |
| 52 | + clk = ports['CLK'] |
| 53 | + rst = ports['RST'] |
| 54 | + |
| 55 | + uut = m.Instance(led, 'uut', |
| 56 | + params=m.connect_params(led), |
| 57 | + ports=m.connect_ports(led)) |
| 58 | + |
| 59 | + lib.simulation.setup_waveform(m, uut, m.get_vars()) |
| 60 | + lib.simulation.setup_clock(m, clk, hperiod=5) |
| 61 | + init = lib.simulation.setup_reset(m, rst, m.make_reset(), period=100) |
| 62 | + |
| 63 | + init.add( |
| 64 | + Delay(1000 * 100), |
| 65 | + Systask('finish'), |
| 66 | + ) |
| 67 | + |
| 68 | + return m |
| 69 | + |
| 70 | +if __name__ == '__main__': |
| 71 | + test = mkTest() |
| 72 | + verilog = test.to_verilog('tmp.v') |
| 73 | + print(verilog) |
0 commit comments