Skip to content

Commit a06fa9a

Browse files
committed
Added an example
1 parent 5f8ea62 commit a06fa9a

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

src/example/Makefile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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

src/example/dff.sv

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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

src/example/test_dff.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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"

0 commit comments

Comments
 (0)