Add one more function comment for vector address test
[soc.git] / TLB / test / test_address_encoder.py
1 import sys
2 sys.path.append("../src")
3 sys.path.append("../../TestUtil")
4
5 from nmigen.compat.sim import run_simulation
6
7 from AddressEncoder import AddressEncoder
8
9 from test_helper import assert_eq, assert_ne, assert_op
10
11 # This function allows for the easy setting of values to the AddressEncoder
12 # Arguments:
13 # dut: The AddressEncoder being tested
14 # i (Input): The array of single bits to be written
15 def set_encoder(dut, i):
16 yield dut.i.eq(i)
17 yield
18
19 # Checks the single match of the AddressEncoder
20 # Arguments:
21 # dut: The AddressEncoder being tested
22 # sm (Single Match): The expected match result
23 # op (Operation): (0 => ==), (1 => !=)
24 def check_single_match(dut, sm, op):
25 out_sm = yield dut.single_match
26 assert_op("Single Match", out_sm, sm, op)
27
28 # Checks the multiple match of the AddressEncoder
29 # Arguments:
30 # dut: The AddressEncoder being tested
31 # mm (Multiple Match): The expected match result
32 # op (Operation): (0 => ==), (1 => !=)
33 def check_multiple_match(dut, mm, op):
34 out_mm = yield dut.multiple_match
35 assert_op("Multiple Match", out_mm, mm, op)
36
37 # Checks the output of the AddressEncoder
38 # Arguments:
39 # dut: The AddressEncoder being tested
40 # o (Output): The expected output
41 # op (Operation): (0 => ==), (1 => !=)
42 def check_output(dut, o, op):
43 out_o = yield dut.o
44 assert_op("Output", out_o, o, op)
45
46 # Checks the state of the AddressEncoder
47 # Arguments:
48 # dut: The AddressEncoder being tested
49 # sm (Single Match): The expected match result
50 # mm (Multiple Match): The expected match result
51 # o (Output): The expected output
52 # ss_op (Operation): Operation for the match assertion (0 => ==), (1 => !=)
53 # mm_op (Operation): Operation for the match assertion (0 => ==), (1 => !=)
54 # o_op (Operation): Operation for the match assertion (0 => ==), (1 => !=)
55 def check_all(dut, sm, mm, o, sm_op, mm_op, o_op):
56 yield from check_single_match(dut, sm, sm_op)
57 yield from check_multiple_match(dut, mm, mm_op)
58 yield from check_output(dut, o, o_op)
59
60 def testbench(dut):
61 # Check invalid input
62 input = 0
63 single_match = 0
64 multiple_match = 0
65 output = 0
66 yield from set_encoder(dut, input)
67 yield from check_all(dut, single_match, multiple_match, output, 0, 0, 0)
68
69 # Check single bit
70 input = 1
71 single_match = 1
72 multiple_match = 0
73 output = 0
74 yield from set_encoder(dut, input)
75 yield from check_all(dut, single_match, multiple_match, output, 0, 0, 0)
76
77 # Check another single bit
78 input = 4
79 single_match = 1
80 multiple_match = 0
81 output = 2
82 yield from set_encoder(dut, input)
83 yield from check_all(dut, single_match, multiple_match, output, 0, 0, 0)
84
85 # Check multiple match
86 # We expected the lowest bit to be returned which is address 0
87 input = 5
88 single_match = 0
89 multiple_match = 1
90 output = 0
91 yield from set_encoder(dut, input)
92 yield from check_all(dut, single_match, multiple_match, output, 0, 0, 0)
93
94 # Check another multiple match
95 # We expected the lowest bit to be returned which is address 1
96 input = 6
97 single_match = 0
98 multiple_match = 1
99 output = 1
100 yield from set_encoder(dut, input)
101 yield from check_all(dut, single_match, multiple_match, output, 0, 0, 0)
102
103
104
105 if __name__ == "__main__":
106 dut = AddressEncoder(4)
107 run_simulation(dut, testbench(dut), vcd_name="Waveforms/test_address_encoder.vcd")
108 print("AddressEncoder Unit Test Success")