Add comments for AddressEncoder and associated tests
[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 def set_encoder(dut, i):
12 yield dut.i.eq(i)
13 yield
14
15 # Checks the single match of the AddressEncoder
16 # Arguments:
17 # dut: The AddressEncoder being tested
18 # sm (Single Match): The expected match result
19 # op (Operation): (0 => ==), (1 => !=)
20 def check_single_match(dut, sm, op):
21 out_sm = yield dut.single_match
22 assert_op("Single Match", out_sm, sm, op)
23
24 # Checks the multiple match of the AddressEncoder
25 # Arguments:
26 # dut: The AddressEncoder being tested
27 # mm (Multiple Match): The expected match result
28 # op (Operation): (0 => ==), (1 => !=)
29 def check_multiple_match(dut, mm, op):
30 out_mm = yield dut.multiple_match
31 assert_op("Multiple Match", out_mm, mm, op)
32
33 # Checks the output of the AddressEncoder
34 # Arguments:
35 # dut: The AddressEncoder being tested
36 # o (Output): The expected output
37 # op (Operation): (0 => ==), (1 => !=)
38 def check_output(dut, o, op):
39 out_o = yield dut.o
40 assert_op("Output", out_o, o, op)
41
42 # Checks the state of the AddressEncoder
43 # Arguments:
44 # dut: The AddressEncoder being tested
45 # sm (Single Match): The expected match result
46 # mm (Multiple Match): The expected match result
47 # o (Output): The expected output
48 # ss_op (Operation): Operation for the match assertion (0 => ==), (1 => !=)
49 # mm_op (Operation): Operation for the match assertion (0 => ==), (1 => !=)
50 # o_op (Operation): Operation for the match assertion (0 => ==), (1 => !=)
51 def check_all(dut, sm, mm, o, sm_op, mm_op, o_op):
52 yield from check_single_match(dut, sm, sm_op)
53 yield from check_multiple_match(dut, mm, mm_op)
54 yield from check_output(dut, o, o_op)
55
56 def testbench(dut):
57 # Check invalid input
58 input = 0
59 single_match = 0
60 multiple_match = 0
61 output = 0
62 yield from set_encoder(dut, input)
63 yield from check_all(dut, single_match, multiple_match, output, 0, 0, 0)
64
65 # Check single bit
66 input = 1
67 single_match = 1
68 multiple_match = 0
69 output = 0
70 yield from set_encoder(dut, input)
71 yield from check_all(dut, single_match, multiple_match, output, 0, 0, 0)
72
73 # Check another single bit
74 input = 4
75 single_match = 1
76 multiple_match = 0
77 output = 2
78 yield from set_encoder(dut, input)
79 yield from check_all(dut, single_match, multiple_match, output, 0, 0, 0)
80
81 # Check multiple match
82 # We expected the lowest bit to be returned which is address 0
83 input = 5
84 single_match = 0
85 multiple_match = 1
86 output = 0
87 yield from set_encoder(dut, input)
88 yield from check_all(dut, single_match, multiple_match, output, 0, 0, 0)
89
90 # Check another multiple match
91 # We expected the lowest bit to be returned which is address 1
92 input = 6
93 single_match = 0
94 multiple_match = 1
95 output = 1
96 yield from set_encoder(dut, input)
97 yield from check_all(dut, single_match, multiple_match, output, 0, 0, 0)
98
99
100
101 if __name__ == "__main__":
102 dut = AddressEncoder(4)
103 run_simulation(dut, testbench(dut), vcd_name="Waveforms/test_address_encoder.vcd")
104 print("AddressEncoder Unit Test Success")