-
Notifications
You must be signed in to change notification settings - Fork 488
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Extend verilator main loop to support clock generation in C++ #2275
Open
marlonjames
wants to merge
1
commit into
cocotb:master
Choose a base branch
from
marlonjames:verilator_usrclk
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# Copyright cocotb contributors | ||
# Licensed under the Revised BSD License, see LICENSE for details. | ||
# SPDX-License-Identifier: BSD-3-Clause | ||
|
||
TOPLEVEL_LANG ?= verilog | ||
|
||
ifneq ($(TOPLEVEL_LANG),verilog) | ||
|
||
all: | ||
@echo "Skipping test due to TOPLEVEL_LANG=$(TOPLEVEL_LANG) not being verilog" | ||
|
||
clean:: | ||
# nothing to clean, just define target in this branch | ||
|
||
else ifneq ($(shell echo $(SIM) | tr A-Z a-z),verilator) | ||
all: | ||
@echo "Skipping test_verilator_user_clock due to SIM=$(SIM) not being verilator" | ||
|
||
clean:: | ||
# nothing to clean, just define target in this branch | ||
|
||
else | ||
|
||
|
||
PWD=$(shell pwd) | ||
|
||
VERILOG_SOURCES = $(PWD)/dual_clock.sv | ||
|
||
TOPLEVEL = dual_clock | ||
MODULE = test_verilator_user_clock | ||
VERILATOR_USRCLK_FILE = verilator_usrclk.cpp | ||
|
||
include $(shell cocotb-config --makefiles)/Makefile.sim | ||
|
||
endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Copyright cocotb contributors | ||
// Licensed under the Revised BSD License, see LICENSE for details. | ||
// SPDX-License-Identifier: BSD-3-Clause | ||
|
||
`timescale 1 ns / 1 ps | ||
|
||
module dual_clock ( | ||
input clk1, | ||
input clk2 | ||
); | ||
|
||
integer count1, count2; | ||
|
||
initial begin | ||
count1 = 0; | ||
count2 = 0; | ||
end | ||
|
||
always @(posedge clk1) begin | ||
count1 <= count1 + 1; | ||
end | ||
|
||
always @(posedge clk2) begin | ||
count2 <= count2 + 1; | ||
end | ||
|
||
endmodule |
15 changes: 15 additions & 0 deletions
15
tests/test_cases/test_verilator_user_clock/test_verilator_user_clock.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# Copyright cocotb contributors | ||
# Licensed under the Revised BSD License, see LICENSE for details. | ||
# SPDX-License-Identifier: BSD-3-Clause USE OF THIS | ||
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
|
||
import cocotb | ||
from cocotb.triggers import Timer | ||
|
||
|
||
@cocotb.test(skip=not cocotb.SIM_NAME.lower().startswith("verilator")) | ||
async def test_user_clock(dut): | ||
await Timer(100, 'us') | ||
|
||
assert dut.count1.value.integer == 114, "Expected count1 of 114, not {}".format(dut.count1.value.integer) | ||
assert dut.count2.value.integer == 47, "Expected count2 of 47, not {}".format(dut.count1.value.integer) |
39 changes: 39 additions & 0 deletions
39
tests/test_cases/test_verilator_user_clock/verilator_usrclk.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Copyright cocotb contributors | ||
// Licensed under the Revised BSD License, see LICENSE for details. | ||
// SPDX-License-Identifier: BSD-3-Clause | ||
|
||
#include <algorithm> // min | ||
#include <cassert> // assert | ||
#include <memory> // unique_ptr | ||
|
||
#include "Vtop.h" | ||
#include "verilated.h" // vluint64_t | ||
|
||
static const int CLK1_HALFPERIOD_PS = 438000; // 438ns | ||
static const int CLK2_HALFPERIOD_PS = 1056000; // 1.056us | ||
|
||
static vluint64_t next_clk1_toggle = CLK1_HALFPERIOD_PS; | ||
static vluint64_t next_clk2_toggle = CLK2_HALFPERIOD_PS; | ||
|
||
vluint64_t user_clock_cb(std::unique_ptr<Vtop> & topp, vluint64_t current_time) { | ||
|
||
// Initial clock value | ||
if (current_time == 0) { | ||
topp->clk1 = 0; | ||
topp->clk2 = 0; | ||
} else { | ||
if (current_time == next_clk1_toggle) { | ||
topp->clk1 = !topp->clk1; | ||
next_clk1_toggle += CLK1_HALFPERIOD_PS; | ||
} | ||
if (current_time == next_clk2_toggle) { | ||
topp->clk2 = !topp->clk2; | ||
next_clk2_toggle += CLK2_HALFPERIOD_PS; | ||
} | ||
} | ||
|
||
auto next_time = std::min(next_clk1_toggle, next_clk2_toggle); | ||
assert(next_time > current_time); | ||
|
||
return next_time; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you need this protection here since the main loop only calls the user_clock_cb on the next clock edge by design?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not in this single-clock example.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps the example should be expanded for multiple clocks and skew? Something that users can copy-paste-modify for their own tests.
I'm thinking an array of next clock edge times mapped to clock handles and halfperiods. Upon entry of the callback, walk the array to see if any clocks need updating by comparing it to the current time. If they do, toggle the clock and update the time with the next time using the halfperiod and skew. Finally, return the min time in the updated clock edge time array.
Users could copy and paste and modify the array definition as they see fit.