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