# DESCRIPTION: Verilator: Verilog Test driver/expect definition
#
# 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-FileCopyrightText: 2026 Wilson Snyder
# SPDX-License-Identifier: LGPL-3.0-only OR Artistic-2.0


def run(test, *, verilator_flags2=()):
    variant, mode, fmt = test.parse_name(r"t_trace_dumpvars_dyn_(0|1)_(cc|sc)_([a-z]+)")

    if mode == "sc" and not test.have_sc:
        test.skip("No SystemC installed")

    # All test use the same SV file
    test.top_filename = "t/t_trace_dumpvars_dyn.v"
    # Any variations after the format name must yield the exact same trace
    test.golden_filename = test.py_filename.rpartition(fmt)[0] + fmt + ".out"

    flags = [
        f"--{mode}",
        "--exe",
        f"--trace-{fmt}",
        "t/t_trace_dumpvars_dyn.cpp",
        "-CFLAGS",
        f"-DTRACE_FMT={fmt}",
        "-CFLAGS",
        f"-DTRACE_HEADER_C=verilated_{fmt}_c.h",
        "-CFLAGS",
        f"-DVERILATED_TRACE_C=Verilated{fmt.capitalize()}C",
    ]
    match variant:
        case "0":
            flags.extend(["-CFLAGS", "-DTEST_VARIANT_0"])
        case "1":
            flags.extend(["-CFLAGS", "-DTEST_VARIANT_1"])
    flags.extend(verilator_flags2)

    # Run test
    test.compile(make_main=False, verilator_flags2=flags)

    test.execute()

    test.trace_identical(test.trace_filename, test.golden_filename)

    test.passes()
