19
19
from caravel_cocotb .caravel_interfaces import report_test
20
20
import cocotb
21
21
22
+ # Read the 16-bit counter value from GPIO[37:30,7:0]
23
+ def counter_value (caravelEnv ):
24
+ return int ((caravelEnv .monitor_gpio (37 ,30 ).binstr + caravelEnv .monitor_gpio (7 ,0 ).binstr ),2 )
25
+
22
26
@cocotb .test ()
23
27
@report_test
24
28
async def counter_wb (dut ):
25
- caravelEnv = await test_configure (dut ,timeout_cycles = 22620 )
29
+ caravelEnv = await test_configure (dut ,timeout_cycles = 27000 )
26
30
27
- cocotb .log .info (f"[TEST] Start counter_wb test" )
31
+ cocotb .log .info (f"[TEST] Start counter_wb test" )
28
32
# wait for start of sending
29
33
await caravelEnv .release_csb ()
30
34
await caravelEnv .wait_mgmt_gpio (1 )
31
- cocotb .log .info (f"[TEST] finish configuration" )
32
- overwrite_val = 7 # value will be written to the counter by wishbone
35
+ cocotb .log .info (f"[TEST] Finished configuration" )
36
+ overwrite_val = 7 # value will be written to the counter by wishbone (hard-coded in firmware)
33
37
# expect value bigger than 7
34
- received_val = int (( caravelEnv . monitor_gpio ( 37 , 30 ). binstr + caravelEnv . monitor_gpio ( 7 , 0 ). binstr ), 2 )
38
+ received_val = counter_value ( caravelEnv )
35
39
counter = received_val
36
40
await cocotb .triggers .ClockCycles (caravelEnv .clk ,1 )
37
41
38
- while True : # wait until the value 1 start counting after the initial
39
- received_val = int ((caravelEnv .monitor_gpio (37 ,30 ).binstr + caravelEnv .monitor_gpio (7 ,0 ).binstr ),2 )
40
- if counter == 0xFFFF : # rollover
42
+ cocotb .log .info (f"[TEST] Testing Wishbone write" )
43
+ # Track the counter expected vs. actual, until they diverge...
44
+ while True :
45
+ received_val = counter_value (caravelEnv )
46
+ if counter == 0xFFFF : # rollover
41
47
counter = 0
42
48
else :
43
49
counter += 1
44
- if received_val != counter :
45
- if received_val == overwrite_val :
46
- counter = received_val + 1
47
- cocotb .log .info (f"Counter value has been overwritten by wishbone to be { received_val } " )
48
- while True : #wait until the wishbone writing finished and the counter start running again
49
- received_val = int ((caravelEnv .monitor_gpio (37 ,30 ).binstr + caravelEnv .monitor_gpio (7 ,0 ).binstr ),2 )
50
+ if received_val != counter :
51
+ # Counter actual has diverged from expected. Verify it was a Wishbone write...
52
+ if received_val == overwrite_val :
53
+ # Yes, new counter value matches the expected Wishbone overwrite value.
54
+ counter = received_val + 1 # Counter's next value will increment from this new value.
55
+ cocotb .log .info (f"Counter value has been overwritten by Wishbone to be { received_val } " )
56
+ while True : #wait until the Wishbone writing finished and the counter started running again
57
+ received_val = counter_value (caravelEnv )
50
58
if counter == received_val :
51
59
break
52
60
await cocotb .triggers .ClockCycles (caravelEnv .clk ,1 )
53
- cocotb .log .info (f"Counter value has been overwritten by wishbone to be { received_val } " )
61
+ cocotb .log .info (f"Counter value is now { received_val } " )
54
62
break
55
63
else :
56
- cocotb .log .error (f"Counter has wrong value before overwrite happened expected: { counter } received: { received_val } " )
64
+ cocotb .log .error (f"Counter has wrong value before overwrite. Expected= { counter } Received= { received_val } " )
57
65
await cocotb .triggers .ClockCycles (caravelEnv .clk ,1 )
58
66
67
+ cocotb .log .info (f"[TEST] Testing 100 more counts" )
59
68
for i in range (100 ):
60
- if counter != int (( caravelEnv . monitor_gpio ( 37 , 30 ). binstr + caravelEnv . monitor_gpio ( 7 , 0 ). binstr ), 2 ) :
61
- cocotb .log .error (f"Counter have wrong value expected = { counter } recieved = { int (( caravelEnv . monitor_gpio ( 37 , 30 ). binstr + caravelEnv . monitor_gpio ( 7 , 0 ). binstr ), 2 ) } " )
69
+ if counter != counter_value ( caravelEnv ) :
70
+ cocotb .log .error (f"Counter has wrong value. Expected= { counter } Received= { counter_value ( caravelEnv ) } " )
62
71
await cocotb .triggers .ClockCycles (caravelEnv .clk ,1 )
63
72
counter += 1
0 commit comments