1 from nmigen
.compat
.sim
import run_simulation
3 from CamEntry
import CamEntry
9 # This function allows for the easy setting of values to the Cam Entry
10 # unless the key is incorrect
12 # dut: The CamEntry being tested
13 # w (write): Read (0) or Write (1)
14 # k (key): The key to be set
15 # d (data): The data to be set
16 def set_cam(dut
, w
, k
, d
):
18 yield dut
.key_in
.eq(k
)
19 yield dut
.data_in
.eq(d
)
22 # Verifies the given values via the requested operation
24 # pre (Prefix): Appended to the front of the assert statement
25 # e (Expected): The expected value
26 # out (Output): The output result
27 # op (Operation): (0 => ==), (1 => !=)
28 def check(pre
, e
, out
, op
):
31 assert out
== e
, pre
+ " Output " + str(out
) + " Expected " + str(e
)
34 assert out
!= e
, pre
+ " Output " + str(out
) + " Expected " + 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("K", 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("D", 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("M", 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 # kop (Operation): The operation for the key assertion (0 => ==), (1 => !=)
70 # dop (Operation): The operation for the data assertion (0 => ==), (1 => !=)
71 # mop (Operation): The operation for the match assertion (0 => ==), (1 => !=)
72 def check_all(dut
, k
, d
, m
, kop
, dop
, mop
):
73 yield from check_key(dut
, k
, kop
)
74 yield from check_data(dut
, d
, dop
)
75 yield from check_match(dut
, m
, mop
)
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(dut
, write
, key
, data
)
88 yield from check_all(dut
, key
, data
, match
, 0, 0, 0)
95 yield from set_cam(dut
, write
, key
, data
)
96 yield from check_all(dut
, key
, data
, match
, 1, 0, 0)
103 yield from set_cam(dut
, write
, key
, data
)
104 yield from check_all(dut
, key
, data
, match
, 0, 0, 0)
111 yield from set_cam(dut
, write
, key
, data
)
112 yield from check_all(dut
, key
, data
, match
, 0, 0, 0)
116 if __name__
== "__main__":
118 run_simulation(dut
, testbench(dut
), vcd_name
="Waveforms/cam_entry_test.vcd")