|
5 | 5 | def random_int(): |
6 | 6 | return random.randint(-(2**127), 2**127 - 1) & ((1 << 128) - 1) |
7 | 7 |
|
| 8 | +def as_uint(x,nbits): |
| 9 | + return x & ((1 << nbits) - 1) |
8 | 10 |
|
9 | 11 | def main(): |
10 | | - dut = DUTAdder() # Assuming USE_VERILATOR |
11 | | - |
| 12 | + dut = DUTAdder() # Assuming USE_VERILATOR |
12 | 13 | print("Initialized UTAdder") |
13 | | - |
| 14 | + |
14 | 15 | for c in range(11451): |
15 | | - dut.a.value, dut.b.value, dut.cin.value = random_int(), random_int(), random_int() & 1 |
16 | | - #dut.Step(1) |
17 | | - # def dut_cal(): |
18 | | - # dut.a.value, dut.b.value, dut.cin.value = i.a, i.b, i.cin |
19 | | - # dut.Step(1) |
20 | | - # o_dut.sum = dut.sum.value |
21 | | - # o_dut.cout = dut.cout.value |
22 | | - |
23 | | - #dut_cal() |
24 | | - #ref_cal() |
25 | | - |
26 | | - print(f"[cycle {dut.xclock.clk}] a=0x{dut.a.value:x}, b=0x{dut.b.value:x}, cin=0x{dut.cin.value:x}") |
27 | | - dut.Step(1) |
28 | | - #print(f"DUT: sum=0x{o_dut.sum:x}, cout=0x{o_dut.cout:x}") |
29 | | - #print(f"REF: sum=0x{o_ref.sum:x}, cout=0x{o_ref.cout:x}") |
30 | | - |
31 | | - #assert o_dut.sum == o_ref.sum, "sum mismatch" |
| 16 | + a, b, cin = random_int(), random_int(), random_int() & 1 |
| 17 | + dut.a.value, dut.b.value, dut.cin.value = a, b, cin |
| 18 | + |
| 19 | + dut.Step(1) |
| 20 | + print(f"[cycle {dut.xclock.clk}] a=0x{dut.a.value:x}, b=0x{dut.b.value:x}, cin=0x{dut.cin.value:x}, sum {as_uint(a+b+cin,128)}, dut_sum {as_uint(dut.sum.value,128)}") |
| 21 | + assert as_uint(dut.sum.value,128) == as_uint(a + b + cin, 128), "sum mismatch" |
32 | 22 |
|
33 | 23 | print("Test Passed, destroy UTAdder") |
34 | 24 | dut.Finish() # When using VCS, DUT.Finish() will exit the program, so it should be the last line of the program |
35 | 25 |
|
36 | | - |
37 | 26 | if __name__ == "__main__": |
38 | 27 | main() |
| 28 | +~ |
| 29 | +~ |
| 30 | +~ |
0 commit comments