Debian Codegraph @

debian/tests/test-dff

raw symbols SCIP
1 #!/bin/sh
2 # autopkgtest check: test_dff from README.md
3
4 set -e
5
6 WORKDIR=$(mktemp -d)
7 trap "rm -rf $WORKDIR" 0 INT QUIT ABRT PIPE TERM
8 cd $WORKDIR
9
10 cat > Makefile << 'EOF'
11 TOPLEVEL_LANG ?= verilog
12
13 ifeq ($(TOPLEVEL_LANG),verilog)
14 VERILOG_SOURCES = $(shell pwd)/dff.sv
15 else ifeq ($(TOPLEVEL_LANG),vhdl)
16 VHDL_SOURCES = $(shell pwd)/dff.vhdl
17 endif
18
19 COCOTB_TEST_MODULES = test_dff
20 COCOTB_TOPLEVEL = dff
21
22 include $(shell cocotb-config --makefiles)/Makefile.sim
23 EOF
24
25 cat > dff.sv << 'EOF'
26 `timescale 1us/1us
27
28 module dff (
29 input logic clk, d,
30 output logic q
31 );
32
33 always @(posedge clk) begin
34 q <= d;
35 end
36
37 endmodule
38 EOF
39
40 cat > test_dff.py << 'EOF'
41 import os
42 import random
43 from pathlib import Path
44
45 import cocotb
46 from cocotb.clock import Clock
47 from cocotb.triggers import RisingEdge
48 from cocotb_tools.runner import get_runner
49
50 LANGUAGE = os.getenv("HDL_TOPLEVEL_LANG", "verilog").lower().strip()
51
52
53 @cocotb.test()
54 async def dff_simple_test(dut):
55 """Test that d propagates to q"""
56
57 # Set initial input value to prevent it from floating
58 dut.d.value = 0
59
60 # Create a 10us period clock driver on port `clk`
61 clock = Clock(dut.clk, 10, unit="us")
62 # Start the clock. Start it low to avoid issues on the first RisingEdge
63 clock.start(start_high=False)
64
65 # Synchronize with the clock. This will register the initial `d` value
66 await RisingEdge(dut.clk)
67
68 expected_val = 0 # Matches initial input value
69 for i in range(10):
70 val = random.randint(0, 1)
71 dut.d.value = val # Assign the random value val to the input port d
72 await RisingEdge(dut.clk)
73 assert dut.q.value == expected_val, f"output q was incorrect on the {i}th cycle"
74 expected_val = val # Save random value for next RisingEdge
75
76 # Check the final input on the next clock
77 await RisingEdge(dut.clk)
78 assert dut.q.value == expected_val, "output q was incorrect on the last cycle"
79
80
81 def test_simple_dff_runner():
82 sim = os.getenv("SIM", "icarus")
83
84 proj_path = Path(__file__).resolve().parent
85
86 if LANGUAGE == "verilog":
87 sources = [proj_path / "dff.sv"]
88 else:
89 sources = [proj_path / "dff.vhdl"]
90
91 runner = get_runner(sim)
92 runner.build(
93 sources=sources,
94 hdl_toplevel="dff",
95 always=True,
96 )
97
98 runner.test(hdl_toplevel="dff", test_module="test_dff,")
99
100
101 if __name__ == "__main__":
102 test_simple_dff_runner()
103 EOF
104
105 # Disable warning about experimental runners feature:
106 export PYTHONWARNINGS="ignore:Python runners:UserWarning"
107 make SIM=icarus