diff --git a/xls/build_rules/cocotb_xls_test.bzl b/xls/build_rules/cocotb_xls_test.bzl new file mode 100644 index 0000000000..552a55e519 --- /dev/null +++ b/xls/build_rules/cocotb_xls_test.bzl @@ -0,0 +1,37 @@ +# Copyright 2023 The XLS Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +load("@rules_hdl//cocotb:cocotb.bzl", "cocotb_test") + +def cocotb_xls_test(**kwargs): + name = kwargs["name"] + top = kwargs["hdl_toplevel"] + + if "timescale" in kwargs: + timescale = kwargs["timescale"] + timestamp_target = name + "-timescale" + timestamp_verilog = name + "_timescale.v" + native.genrule( + name = timestamp_target, + srcs = [], + cmd = "echo \\`timescale {}/{} > $@".format( + timescale["unit"], + timescale["precission"], + ), + outs = [timestamp_verilog], + ) + kwargs["verilog_sources"].insert(0, timestamp_verilog) + kwargs.pop("timescale") + + cocotb_test(**kwargs) diff --git a/xls/examples/cocotb/BUILD b/xls/examples/cocotb/BUILD index 051c25e125..bbe56df4d3 100644 --- a/xls/examples/cocotb/BUILD +++ b/xls/examples/cocotb/BUILD @@ -12,9 +12,9 @@ # See the License for the specific language governing permissions and # limitations under the License. -load("@rules_hdl//cocotb:cocotb.bzl", "cocotb_test") load("@xls_pip_deps//:requirements.bzl", "requirement") load("//xls/build_rules:xls_build_defs.bzl", "xls_dslx_verilog") +load("//xls/build_rules:cocotb_xls_test.bzl", "cocotb_xls_test") xls_dslx_verilog( name = "running_counter_verilog", @@ -32,7 +32,7 @@ xls_dslx_verilog( verilog_file = "running_counter.v", ) -cocotb_test( +cocotb_xls_test( name = "running_counter_cocotb", hdl_toplevel = "RunningCounter", hdl_toplevel_lang = "verilog", @@ -40,10 +40,13 @@ cocotb_test( "cocotb_running_counter.py", ], verilog_sources = [ - "timescale.v", "dumpvcd.v", ":running_counter.v", ], + timescale = { + "unit": "1ns", + "precission": "1ps", + }, deps = [ requirement("cocotb"), requirement("cocotb_bus"),