File size: 2,147 Bytes
cb65407
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import os
import sys
import tempfile

import pytest
from cocotb.runner import get_runner
from cocotb.utils import _get_log_time_scale
from test_cocotb import (
    compile_args,
    gpi_interfaces,
    hdl_toplevel,
    hdl_toplevel_lang,
    sim,
    sim_args,
    sim_build,
    tests_dir,
    verilog_sources,
    vhdl_sources,
)

sys.path.insert(0, os.path.join(tests_dir, "pytest"))

cocotb_test_contents = """
import cocotb
from cocotb.utils import _get_simulator_precision

@cocotb.test()
async def check_timescale(dut):
    assert _get_simulator_precision() == {precision}
"""


@pytest.mark.simulator_required
@pytest.mark.skipif(
    os.getenv("SIM", "icarus") not in ["icarus", "ghdl"],
    reason="Currently only Icarus and GHDL support timescale setting when using Cocotb runner",
)
@pytest.mark.parametrize("precision", ["fs", "ps", "ns", "us", "ms", "s"])
def test_precision(precision):
    build_dir = os.path.join(sim_build, f"test_timescale_{precision}")

    timescale = (f"1{precision}", f"1{precision}")
    precision_log = _get_log_time_scale(precision if precision != "s" else "sec")

    with tempfile.TemporaryDirectory() as d:
        sys.path.insert(0, d)
        test_module_path = os.path.join(d, "test_precision.py")
        test_module = os.path.basename(os.path.splitext(test_module_path)[0])
        with open(test_module_path, "w") as f:
            f.write(cocotb_test_contents.format(precision=precision_log))
            f.flush()

        runner = get_runner(sim)
        runner.build(
            always=True,
            clean=True,
            verilog_sources=verilog_sources,
            vhdl_sources=vhdl_sources,
            hdl_toplevel=hdl_toplevel,
            build_dir=build_dir,
            build_args=compile_args,
            defines={"NOTIMESCALE": 1},
            timescale=timescale,
        )

        runner.test(
            hdl_toplevel_lang=hdl_toplevel_lang,
            hdl_toplevel=hdl_toplevel,
            gpi_interfaces=gpi_interfaces,
            test_module=test_module,
            test_args=sim_args,
            build_dir=build_dir,
            timescale=timescale,
        )