Skip to content

Commit

Permalink
Update dev (#12)
Browse files Browse the repository at this point in the history
* Add docker command to README.md and modify simulation files so they avoid features we do not have available.

* Rename example folders and add verilator docker scripts

* Remove unecessary code.

* Update action

* Remove interactive docker when not necessary.

* Set shell in makefile

* Try different approach to escape #
  • Loading branch information
magnmaeh authored Feb 9, 2024
1 parent 5cb9a7a commit 3e14267
Show file tree
Hide file tree
Showing 24 changed files with 90 additions and 36 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/makefile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ jobs:
- uses: actions/checkout@v3

- name: Docker test
run: docker run -v ${PWD}:/work --user $(id -u):$(id -g) -e CCACHE_DIR=/work/.ccache --entrypoint make verilator/verilator:stable
run: make
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
examples/**/obj_dir
examples/**/*.txt
examples/**/*.vcd
.ccache
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ The main contribution of this project is the `verilator.mk` makefile. Include it

The examples show how Verilator can be used to simualate a Verilog circuit. Feel free to add more examples and tests for them.

All examples can be run using the `makefile` at the top level of this repository.
All examples can be run using the `makefile` at the top level of this repository. Simply type `make` to run the makefile.

## And

Expand Down
File renamed without changes.
File renamed without changes.
8 changes: 3 additions & 5 deletions examples/add/sim.cpp → examples/Add/sim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@
#include <iostream>

int main(int argc, char** argv) {
VerilatedContext* contextp = new VerilatedContext;
contextp->commandArgs(argc, argv);
VAdd* top = new VAdd{contextp};
VAdd* top = new VAdd;
Verilated::commandArgs(argc, argv);

int stimuli_a[] = { 2, -1, 1, -6 };
int stimuli_b[] = { 4, 5, 2, 7 };

for (int i = 0; i < sizeof(stimuli_a) / sizeof(stimuli_a[0]); i++) {
if (contextp->gotFinish()) break;
if (Verilated::gotFinish()) break;

top->a = stimuli_a[i];
top->b = stimuli_b[i];
Expand All @@ -25,6 +24,5 @@ int main(int argc, char** argv) {
}

delete top;
delete contextp;
return 0;
}
File renamed without changes.
File renamed without changes.
8 changes: 3 additions & 5 deletions examples/and/sim.cpp → examples/And/sim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@
#include <iostream>

int main(int argc, char** argv) {
VerilatedContext* contextp = new VerilatedContext;
contextp->commandArgs(argc, argv);
VAnd* top = new VAnd{contextp};
VAnd* top = new VAnd;
Verilated::commandArgs(argc, argv);

// For waveform generation
unsigned long tickcount = 0;
Expand All @@ -27,7 +26,7 @@ int main(int argc, char** argv) {
bool stimuli_b[] = { false, true, false, true };

for (int i = 0; i < sizeof(stimuli_a) / sizeof(stimuli_a[0]); i++) {
if (contextp->gotFinish()) break;
if (Verilated::gotFinish()) break;

top->a = stimuli_a[i];
top->b = stimuli_b[i];
Expand All @@ -47,6 +46,5 @@ int main(int argc, char** argv) {
delete trace;

delete top;
delete contextp;
return 0;
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 2 additions & 4 deletions examples/doublesub/sim.cpp → examples/Doublesub/sim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
int get_stimuli(std::string filename, int *data);

int main(int argc, char** argv) {
VerilatedContext* contextp = new VerilatedContext;
contextp->commandArgs(argc, argv);
VDoubleSub* top = new VDoubleSub{contextp};
VDoubleSub* top = new VDoubleSub;
Verilated::commandArgs(argc, argv);

int *operands = new int[200];
int noperands = get_stimuli(std::string{"operands.data"}, operands);
Expand All @@ -29,6 +28,5 @@ int main(int argc, char** argv) {


delete top;
delete contextp;
return 0;
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
#include <iostream>

int main(int argc, char** argv) {
VerilatedContext* contextp = new VerilatedContext;
contextp->commandArgs(argc, argv);
VDelay* top = new VDelay{contextp};
VDelay* top = new VDelay;
Verilated::commandArgs(argc, argv);

int data_in = 0x66234223;

Expand Down Expand Up @@ -40,6 +39,5 @@ int main(int argc, char** argv) {
assert(top->delayed_data == data_in);

delete top;
delete contextp;
return 0;
}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
`include "../doublesub/Sub.sv"
`include "delay/Delay.sv"
`include "../Doublesub/Sub.sv"
`include "Delay/Delay.sv"

module Hierarchy (
input clk,
Expand Down
6 changes: 2 additions & 4 deletions examples/hierarchy/main.cpp → examples/Hierarchy/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
#include <iostream>

int main(int argc, char** argv) {
VerilatedContext* contextp = new VerilatedContext;
contextp->commandArgs(argc, argv);
VHierarchy* top = new VHierarchy{contextp};
VHierarchy* top = new VHierarchy;
Verilated::commandArgs(argc, argv);

int data_in = 0x66234223;
int subtract = 22;
Expand Down Expand Up @@ -44,6 +43,5 @@ int main(int argc, char** argv) {
assert(top->data_out == (data_in - subtract));

delete top;
delete contextp;
return 0;
}
File renamed without changes.
17 changes: 17 additions & 0 deletions verilator-docker.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash
# DESCRIPTION: Wrap a verilator call to run a docker container
#
# Copyright 2020 by Stefan Wallentowitz. This program is free software; you
# can redistribute it and/or modify it under the terms of either the GNU
# Lesser General Public License Version 3 or the Perl Artistic License
# Version 2.0.
# SPDX-License-Identifier: LGPL-3.0-only OR Artistic-2.0

# Pull newest image
docker pull verilator/verilator:4.038 > /dev/null

# Get the project root
project_root=$(realpath $3)

# Run the docker image with the user we "made" above
docker run -e CCACHE_DIR=/work/.ccache -v ${project_root}:/work -w /work/$2 --user $(id -u):$(id -g) verilator/verilator:$1 "${@:4}"
31 changes: 31 additions & 0 deletions verilator-wrap.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/bin/bash
# DESCRIPTION: Wrap a Verilator call and copy vlt includes
# (inside docker container)
#
# Copyright 2020 by Stefan Wallentowitz. This program is free software; you
# can redistribute it and/or modify it under the terms of either the GNU
# Lesser General Public License Version 3 or the Perl Artistic License
# Version 2.0.
# SPDX-License-Identifier: LGPL-3.0-only OR Artistic-2.0

perl /usr/local/bin/verilator "$@"
status=$?
if [ $status -ne 0 ]; then
exit $status
fi

# Check if user set an obj_dir
obj_dir=$(echo " $@" | grep -oP '\s--Mdir\s*\K\S+')
if [ "$obj_dir" == "" ]; then
obj_dir="obj_dir"
fi

# If the run was successful: Copy required files to allow build without this container
if [ -e ${obj_dir} ]; then
# Copy files required for the build
mkdir -p ${obj_dir}/vlt
cp -r /usr/local/share/verilator/bin ${obj_dir}/vlt
cp -r /usr/local/share/verilator/include ${obj_dir}/vlt
# Point Makefile to that folder
sed -i 's/VERILATOR_ROOT = \/usr\/local\/share\/verilator/VERILATOR_ROOT = vlt/g' ${obj_dir}/*.mk
fi
35 changes: 25 additions & 10 deletions verilator.mk
Original file line number Diff line number Diff line change
@@ -1,23 +1,38 @@
MODULE_FOLDER ?= examples
PROJECT_ROOT ?= ${VERILATOR_ROOT}

VERILATOR_EXE := obj_dir/V$(PROJECT_NAME)
SHELL := /bin/bash

PARSER ?= $(VERILATOR_ROOT)/default_parser.sh
# Hashtag is a comment in make, but we want to use it in shell commands
HASHTAG := \#

# Find the path between the top level folder and the source code folder
# Just doing ${MODULE_FOLDER}/${PROJECT_NAME} is not sufficient, because there
# might be folders between the module folder and project folder. Like in the
# Hierachy example.
PATH_FROM_ROOT_TO_SRC = $(shell p=$(shell pwd); g=$${p${HASHTAG}${HASHTAG}*/${MODULE_FOLDER}}; echo $$g)

VERILATOR_ARGS += --cc --build --exe --trace
VERILATOR := $(VERILATOR_ROOT)/verilator-docker.sh
VERILATOR_ARGS := 4.038 ${MODULE_FOLDER}${PATH_FROM_ROOT_TO_SRC} ${PROJECT_ROOT}
VERILATOR_ARGS_LINT := --lint-only -Wall
VERILATOR_ARGS_EMULATOR := --cc --build --exe --trace

VERILATOR_EMULATOR := obj_dir/V$(PROJECT_NAME)
PARSER ?= $(VERILATOR_ROOT)/default_parser.sh

all: run

lint: $(SOURCES)
verilator --lint-only -Wall $^ --top-module $(PROJECT_NAME)
lint: $(SOURCES) $(GENERATED)
./$(VERILATOR) $(VERILATOR_ARGS) $(VERILATOR_ARGS_LINT) $(SOURCES) --top-module $(PROJECT_NAME)

$(VERILATOR_EXE): lint | $(SOURCES)
verilator $(VERILATOR_ARGS) $(SIMFILES) $(SOURCES) > /dev/null
$(VERILATOR_EMULATOR): lint | $(SOURCES) $(GENERATED)
./$(VERILATOR) $(VERILATOR_ARGS) $(VERILATOR_ARGS_EMULATOR) $(SIMFILES) $(SOURCES) > /dev/null

run: $(VERILATOR_EXE)
# https://stackoverflow.com/questions/17757039/equivalent-of-pipefail-in-dash-shell
# https://stackoverflow.com/questions/17757039/equivalent-of-pipefail-in-dash-shell
run: $(VERILATOR_EMULATOR)
@mkfifo named_pipe
@tee output.txt < named_pipe &
@./$(VERILATOR_EXE) > named_pipe; ./$(PARSER) $(PROJECT_NAME) $$? output.txt
@./$(VERILATOR_EMULATOR) > named_pipe; ./$(PARSER) $(PROJECT_NAME) $$? output.txt
@rm named_pipe

clean:
Expand Down

0 comments on commit 3e14267

Please sign in to comment.