File tree Expand file tree Collapse file tree 3 files changed +58
-0
lines changed
Expand file tree Collapse file tree 3 files changed +58
-0
lines changed Original file line number Diff line number Diff line change 1+ # Makefile
2+
3+ SIM =icarus
4+ TOPLEVEL_LANG = verilog
5+ VERILOG_SOURCES = $(shell pwd) /dff.sv
6+ TOPLEVEL = dff
7+ MODULE = test_dff
8+
9+ include $(shell cocotb-config --makefiles) /Makefile.sim
Original file line number Diff line number Diff line change 1+ // dff.sv
2+
3+ `timescale 1us / 1ns
4+
5+ module dff (
6+ output logic q,
7+ input logic clk, d
8+ );
9+
10+ always @ (posedge clk) begin
11+ q <= d;
12+ end
13+
14+ endmodule
Original file line number Diff line number Diff line change 1+ # test_dff.py
2+
3+ import random
4+
5+ import cocotb
6+ from cocotb .clock import Clock
7+ from cocotb .triggers import RisingEdge
8+ from cocotb .types import LogicArray
9+
10+ @cocotb .test ()
11+ async def dff_simple_test (dut ):
12+ """Test that d propagates to q"""
13+
14+ # Assert initial output is unknown
15+ assert LogicArray (dut .q .value ) == LogicArray ("X" )
16+ # Set initial input value to prevent it from floating
17+ dut .d .value = 0
18+
19+ clock = Clock (dut .clk , 10 , units = "us" ) # Create a 10us period clock on port clk
20+ # Start the clock. Start it low to avoid issues on the first RisingEdge
21+ cocotb .start_soon (clock .start (start_high = False ))
22+
23+ # Synchronize with the clock. This will regisiter the initial `d` value
24+ await RisingEdge (dut .clk )
25+ expected_val = 0 # Matches initial input value
26+ for i in range (10 ):
27+ val = random .randint (0 , 1 )
28+ dut .d .value = val # Assign the random value val to the input port d
29+ await RisingEdge (dut .clk )
30+ assert dut .q .value == expected_val , f"output q was incorrect on the { i } th cycle"
31+ expected_val = val # Save random value for next RisingEdge
32+
33+ # Check the final input on the next clock
34+ await RisingEdge (dut .clk )
35+ assert dut .q .value == expected_val , "output q was incorrect on the last cycle"
You can’t perform that action at this time.
0 commit comments