test_counter.py 1.52 KB
Newer Older
1
import tvm
2
from tvm.contrib import verilog
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

def test_counter():
    # Start a new session by run simulation on test_counter.v
    # Find file will search root/verilog and root/tests/verilog
    sess = verilog.session([
        verilog.find_file("test_counter.v"),
        verilog.find_file("example_counter.v")
    ])
    # Get the handles by their names
    rst = sess.main.rst
    counter = sess.main.counter
    cnt = sess.main["counter_unit1"]
    assert(counter.name == "main.counter")
    assert(counter.size == 4)
    rst.put_int(1)
    # This will advance the cycle to next pos-edge of clk.
    sess.yield_until_next_cycle()
    rst.put_int(0)
    sess.yield_until_next_cycle()

    for i in range(10):
        # get value of counter.
        assert(counter.get_int() == i)
        sess.yield_until_next_cycle()


def test_scratch():
    sess = verilog.session([
        verilog.find_file("test_counter.v"),
        verilog.find_file("example_counter.v")
    ])
    # Get the handles by their names
    rst = sess.main.rst
    counter = sess.main.counter
    rst.put_int(1)
    # This will advance the cycle to next pos-edge of clk.
    sess.yield_until_next_cycle()
    rst.put_int(0)
    temp = 0
    for i in range(10):
        if rst.get_int():
            rst.put_int(0)
            temp = counter.get_int()
        elif counter.get_int() == 3:
            rst.put_int(1)
        print("counter=%d, temp=%d" % (counter.get_int(), temp))
        sess.yield_until_next_cycle()

if __name__ == "__main__":
    test_scratch()
    test_counter()