Skip to content

Commit 23afd2a

Browse files
committed
Add cocotb_xls_test macro
Creates cocotb_xls_test macro that generates a file with the timestamp specified in the rule definition. The macro is required to generate the timestamp for Icarus Verilog smulation in Cocotb. Internal-tag: [#46586] Signed-off-by: Robert Winkler <rwinkler@antmicro.com>
1 parent 0a9aff8 commit 23afd2a

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

xls/build_rules/cocotb_xls_test.bzl

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Copyright 2023 The XLS Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
load("@rules_hdl//cocotb:cocotb.bzl", "cocotb_test")
16+
17+
def cocotb_xls_test(**kwargs):
18+
name = kwargs["name"]
19+
top = kwargs["hdl_toplevel"]
20+
21+
if "timescale" in kwargs:
22+
timescale = kwargs["timescale"]
23+
timestamp_target = name + "-timescale"
24+
timestamp_verilog = name + "_timescale.v"
25+
native.genrule(
26+
name = timestamp_target,
27+
srcs = [],
28+
cmd = "echo \\`timescale {}/{} > $@".format(
29+
timescale["unit"],
30+
timescale["precission"],
31+
),
32+
outs = [timestamp_verilog],
33+
)
34+
kwargs["verilog_sources"].insert(0, timestamp_verilog)
35+
kwargs.pop("timescale")
36+
37+
cocotb_test(**kwargs)

xls/examples/cocotb/BUILD

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
load("@rules_hdl//cocotb:cocotb.bzl", "cocotb_test")
1615
load("@xls_pip_deps//:requirements.bzl", "requirement")
1716
load("//xls/build_rules:xls_build_defs.bzl", "xls_dslx_verilog")
17+
load("//xls/build_rules:cocotb_xls_test.bzl", "cocotb_xls_test")
1818

1919
xls_dslx_verilog(
2020
name = "running_counter_verilog",
@@ -32,18 +32,21 @@ xls_dslx_verilog(
3232
verilog_file = "running_counter.v",
3333
)
3434

35-
cocotb_test(
35+
cocotb_xls_test(
3636
name = "running_counter_cocotb",
3737
hdl_toplevel = "RunningCounter",
3838
hdl_toplevel_lang = "verilog",
3939
test_module = [
4040
"cocotb_running_counter.py",
4141
],
4242
verilog_sources = [
43-
"timescale.v",
4443
"dumpvcd.v",
4544
":running_counter.v",
4645
],
46+
timescale = {
47+
"unit": "1ns",
48+
"precission": "1ps",
49+
},
4750
deps = [
4851
requirement("cocotb"),
4952
requirement("cocotb_bus"),

0 commit comments

Comments
 (0)