1 from nmigen
.compat
.sim
import run_simulation
3 from CamEntry
import CamEntry
5 # This function allows for the easy setting of values to the Cam Entry
6 # unless the key is incorrect
8 # dut: The CamEntry being tested
9 # c (command): NA (0), Read (1), Write (2), Reserve (3)
10 # k (key): The key to be set
11 # d (data): The data to be set
12 def set_cam_entry(dut
, c
, k
, d
):
13 # Write desired values
14 yield dut
.command
.eq(c
)
15 yield dut
.key_in
.eq(k
)
16 yield dut
.data_in
.eq(d
)
19 yield dut
.command
.eq(0)
20 yield dut
.key_in
.eq(0)
21 yield dut
.data_in
.eq(0)
24 # Verifies the given values via the requested operation
26 # p (Prefix): Appended to the front of the assert statement
27 # e (Expected): The expected value
28 # o (Output): The output result
29 # op (Operation): (0 => ==), (1 => !=)
30 def check(p
, o
, e
, op
):
32 assert o
== e
, p
+ " Output " + str(o
) + " Expected " + str(e
)
34 assert o
!= e
, p
+ " Output " + str(o
) + " Not Expecting " + str(e
)
36 # Checks the key state of the CAM entry
38 # dut: The CamEntry being tested
39 # k (Key): The expected key
40 # op (Operation): (0 => ==), (1 => !=)
41 def check_key(dut
, k
, op
):
43 check("Key", out_k
, k
, op
)
45 # Checks the data state of the CAM entry
47 # dut: The CamEntry being tested
48 # d (Data): The expected data
49 # op (Operation): (0 => ==), (1 => !=)
50 def check_data(dut
, d
, op
):
51 out_d
= yield dut
.data
52 check("Data", out_d
, d
, op
)
54 # Checks the match state of the CAM entry
56 # dut: The CamEntry being tested
57 # m (Match): The expected match
58 # op (Operation): (0 => ==), (1 => !=)
59 def check_match(dut
, m
, op
):
60 out_m
= yield dut
.match
61 check("Match", out_m
, m
, op
)
63 # Checks the state of the CAM entry
65 # dut: The CamEntry being tested
66 # k (key): The expected key
67 # d (data): The expected data
68 # m (match): The expected match
69 # k_op (Operation): The operation for the key assertion (0 => ==), (1 => !=)
70 # d_op (Operation): The operation for the data assertion (0 => ==), (1 => !=)
71 # m_op (Operation): The operation for the match assertion (0 => ==), (1 => !=)
72 def check_all(dut
, k
, d
, m
, k_op
, d_op
, m_op
):
73 yield from check_key(dut
, k
, k_op
)
74 yield from check_data(dut
, d
, d_op
)
75 yield from check_match(dut
, m
, m_op
)
77 # This testbench goes through the paces of testing the CamEntry module
78 # It is done by writing and then reading various combinations of key/data pairs
79 # and reading the results with varying keys to verify the resulting stored
87 yield from set_cam_entry(dut
, command
, key
, data
)
88 yield from check_all(dut
, key
, data
, match
, 0, 0, 0)
95 yield from set_cam_entry(dut
, command
, key
, data
)
96 yield from check_all(dut
, key
, data
, match
, 1, 0, 0)
103 yield from set_cam_entry(dut
, command
, key
, data
)
104 yield from check_all(dut
, key
, data
, match
, 0, 0, 0)
111 yield from set_cam_entry(dut
, command
, key
, data
)
113 yield from check_all(dut
, key
, data
, match
, 0, 0, 0)
120 yield from set_cam_entry(dut
, command
, key
, data
)
121 yield from check_all(dut
, key
, data
, match
, 0, 0, 0)
123 # Extra clock cycle for waveform
126 if __name__
== "__main__":
128 run_simulation(dut
, testbench(dut
), vcd_name
="Waveforms/cam_entry_test.vcd")
129 print("CamEntry Unit Test Success")