Skip to content

Commit 18547cf

Browse files
committed
setup testbench structure and add getting started example
1 parent efd3f49 commit 18547cf

File tree

10 files changed

+204
-0
lines changed

10 files changed

+204
-0
lines changed

.github/workflows/main.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
name: python-black
2+
on: [push, pull_request]
3+
jobs:
4+
linter_name:
5+
name: runner / black formatter
6+
runs-on: ubuntu-latest
7+
steps:
8+
- uses: actions/checkout@v4
9+
- uses: rickstaa/action-black@v1
10+
with:
11+
black_args: ". --check"

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
*.venv/
2+
*.pytest_cache/
3+
*__pycache__/
4+
*sim_build/
5+
*.log

Containerfile

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Setup dependancy versions
2+
ARG DEBIAN_VERSION=12
3+
4+
FROM docker.io/library/debian:${DEBIAN_VERSION}
5+
6+
RUN apt-get update && apt-get upgrade -y && \
7+
apt-get install -y \
8+
python3.11 \
9+
python3-pip \
10+
python3-venv \
11+
autoconf gperf make gcc g++ bison flex \
12+
git help2man perl perl-doc \
13+
libfl2 libfl-dev \
14+
ccache mold libgoogle-perftools-dev numactl \
15+
zlib1g zlib1g-dev \
16+
gtkwave
17+
18+
# Setup verilator
19+
WORKDIR /usr/src/
20+
ENV VERILATOR_VERSION=5.020
21+
RUN git clone https://github.com/verilator/verilator.git
22+
WORKDIR /usr/src/verilator
23+
RUN git checkout v${VERILATOR_VERSION}
24+
RUN autoconf && ./configure && make -j `nproc` && make install
25+
WORKDIR /usr/src/
26+
27+
# compile and install iverilog
28+
WORKDIR /usr/src/
29+
RUN git clone https://github.com/steveicarus/iverilog.git
30+
WORKDIR /usr/src/iverilog/
31+
RUN sh autoconf.sh && ./configure --prefix /usr && make && make install
32+
33+
WORKDIR /usr/src/
34+
# setup python virtual environment
35+
ENV VIRTUAL_ENV=.venv
36+
RUN python3 -m venv ${VIRTUAL_ENV}
37+
ENV PATH="${VIRTUAL_ENV}/bin:$PATH"
38+
39+
RUN pip3 install wheel
40+
# install needed python libraries
41+
COPY requirements.txt .
42+
RUN pip3 install -r requirements.txt && \
43+
rm requirements.txt
44+
45+
# jank way to activate the virtual environment
46+
ENTRYPOINT . .venv/bin/activate && /bin/bash

Makefile

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
CONTAINER_IMG := cocotb-dev
2+
CONTAINER_FILE := Containerfile
3+
4+
build::
5+
@podman build -f ${CONTAINER_FILE} -t ${CONTAINER_IMG} "${PWD}"
6+
7+
# publish::
8+
# @podman login
9+
# @podman push ${CONTAINER_IMG}:latest
10+
11+
run::
12+
@podman run -it --rm \
13+
-e DISPLAY \
14+
-v "/tmp/.X11-unix":"/tmp/.X11-unix" \
15+
-v "${XAUTHORITY}":"/root/.Xauthority" \
16+
-v "${PWD}":"/usr/src/verilogbits/" \
17+
--ipc host \
18+
${CONTAINER_IMG}
19+
20+
clean::
21+
@find . -type d -name "*__pycache__" -exec echo {} + -exec rm -rf {} +
22+
@find . -type d -name "*.pytest_cache" -exec echo {} + -exec rm -rf {} +
23+
@find . -type d -name "*sim_build" -exec echo {} + -exec rm -rf {} +
24+
@find . -type d -name "*.venv" -exec echo {} + -exec rm -rf {} +
25+
@find . -name "verilogbits.log" -exec echo {} + -exec rm -rf {} +

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,9 @@
11
# verilogbits
22
My solutions and my own tests for problems from the HDLBits website.
3+
4+
## Setting up a virtual environment
5+
```bash
6+
python3 -m venv .venv --prompt verilogbits
7+
source .venv/bin/activate
8+
pip3 install -r requirements.txt
9+
```
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
`resetall
2+
`timescale 1ns / 1ps
3+
`default_nettype none
4+
5+
module constant_one (
6+
output wire one
7+
);
8+
9+
assign one = 1;
10+
11+
endmodule
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import logging
2+
3+
4+
class DataError(Exception):
5+
pass
6+
7+
8+
class TB:
9+
def __init__(self, dut):
10+
self.dut = dut
11+
12+
# Start logging
13+
self.log = logging.getLogger("cocotb.tb")
14+
self.log.setLevel(logging.DEBUG)
15+
16+
# Simulation Signals
17+
self.get_signals()
18+
19+
def get_signals(self):
20+
self.one = self.dut.one
21+
22+
async def one_check(self):
23+
if int(self.one.value) != 1:
24+
raise DataError(f"Output should always be 1, got {self.one.value} instead.")
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import os
2+
import pytest
3+
import cocotb
4+
from cocotb.clock import Timer
5+
from cocotb.regression import TestFactory
6+
from cocotb_test.simulator import run
7+
8+
from top_module import TB
9+
10+
11+
async def output_stays_one(dut):
12+
tb = TB(dut)
13+
14+
tb.log.info("Checking output value...")
15+
# Await one nanosecond so the model can start.
16+
await Timer(1, units="ns")
17+
tb.one_check()
18+
19+
20+
if cocotb.SIM_NAME:
21+
test1 = TestFactory(output_stays_one)
22+
test1.generate_tests()
23+
24+
# cocotb-test
25+
26+
tests_dir = os.path.abspath(os.path.dirname(__file__))
27+
rtl_dir = os.path.abspath(os.path.join(tests_dir, "..", "rtl"))
28+
29+
parameter_list = [{}]
30+
31+
32+
@pytest.mark.parametrize("parameters", parameter_list)
33+
def test_constant_one(request, parameters):
34+
dut = "constant_one"
35+
module = os.path.splitext(os.path.basename(__file__))[0]
36+
toplevel = dut
37+
38+
verilog_sources = [
39+
os.path.join(rtl_dir, f"{dut}.v"),
40+
]
41+
42+
extra_env = {f"PARAM_{k}": str(v) for k, v in parameters.items()}
43+
44+
sim_build = os.path.join(
45+
tests_dir, "sim_build", request.node.name.replace("[", "-").replace("]", "")
46+
)
47+
48+
run(
49+
python_search=[tests_dir],
50+
verilog_sources=verilog_sources,
51+
toplevel=toplevel,
52+
module=module,
53+
parameters=parameters,
54+
sim_build=sim_build,
55+
extra_env=extra_env,
56+
compile_args=["--timing", "-O3"],
57+
waves=True,
58+
)

pytest.ini

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[pytest]
2+
env =
3+
SIM=verilator
4+
CXXFLAGS=-std=c++14
5+
WAVES=0
6+
7+
log_file = verilogbits.log
8+
log_file_level = DEBUG
9+
testpaths =
10+
problems/getting-started/getting-started/tb/test_constant_one.py

requirements.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
cocotb == 1.8.1
2+
cocotb-test == 0.2.5
3+
pytest == 7.4.3
4+
pytest-xdist == 3.5.0
5+
pytest-sugar == 0.9.7
6+
pytest-env == 1.1.3
7+
pytest-retry == 1.6.1

0 commit comments

Comments
 (0)