Exemplo n.º 1
0
def test_empty_global(temp):
    @sv()
    def foo():
        pass

    foo.func_def.imports = {}
    compile_lib([foo], cwd=temp)
Exemplo n.º 2
0
def test_no_call_sv_compile(temp):
    from pysv.codegen import generate_cxx_binding, generate_sv_binding

    @sv
    def foo():
        pass

    compile_lib([foo], cwd=temp)
    generate_cxx_binding([foo], filename=os.path.join(temp, "test.hh"))
    generate_sv_binding([foo], filename=os.path.join(temp, "test.svh"))
Exemplo n.º 3
0
def test_sv_object_funcs(get_vector_filename, simulator, temp):
    avail, tester_cls = simulator_map[simulator]
    if not avail():
        pytest.skip(simulator + " is not available")

    class ClassA:
        @sv()
        def __init__(self):
            self.num = 21

    class ClassB:
        # object is allowed if you want duct typing
        @sv(a=DataType.Object)
        def __init__(self, a):
            self.a = a

        @sv(return_type=ClassA)
        def create_a(self, num):
            a = ClassA()
            a.num = num
            return a

        @sv(a=ClassA)
        def add(self, a):
            return self.a.num + a.num

    lib_path = compile_lib([ClassA, ClassB], cwd=temp)
    sv_pkg = os.path.join(os.path.abspath(temp), "pysv_pkg.sv")
    generate_sv_binding([ClassA, ClassB], filename=sv_pkg)
    tb_file = get_vector_filename("test_sv_object_funcs.sv")

    tester = tester_cls(lib_path, sv_pkg, tb_file, cwd=temp)
    tester.run()
Exemplo n.º 4
0
def test_model_constructor(temp):
    c_code = """
    void *ptr = {0}(42, 1);
    std::cout << ptr;
    """.format(TestModel.__init__.func_name)
    lib_path = compile_lib([TestModel], cwd=temp)
    compile_and_run(lib_path, c_code, temp, [TestModel])
Exemplo n.º 5
0
def test_compile(temp):
    @sv()
    def add(a, b):
        return a + b

    lib_file = compile_lib([add], cwd=temp)
    assert os.path.exists(lib_file)
Exemplo n.º 6
0
def test_cxx_object_funcs_1(temp):
    class ClassA:
        @sv()
        def __init__(self):
            self.num = 21

    class ClassB:
        @sv(a=DataType.Object)
        def __init__(self, a):
            self.a = a

        @sv(return_type=ClassA)
        def create_a(self, num):
            a = ClassA()
            a.num = num
            return a

        @sv(a=ClassA)
        def add(self, a):
            return self.a.num + a.num

    lib_file = compile_lib([ClassA, ClassB], cwd=temp)
    cxx_code = """
using namespace pysv;
ClassA a1;
ClassB b(&a1);
ClassA a2 = b.create_a(-42);
std::cout << b.add(&a1) << std::endl;
std::cout << b.add(&a2);
pysv_finalize();
        """
    values = compile_and_run(lib_file, cxx_code, temp, [ClassA, ClassB], use_implementation=True).split("\n")
    assert int(values[0]) == 42
    assert int(values[1]) == (21 - 42)
Exemplo n.º 7
0
def test_model_function_call(temp):
    c_code = """
    void *ptr = {0}(42, 1);
    auto r = {1}(ptr, 1);
    std::cout << r;
    """.format(TestModel.__init__.func_name, TestModel.foo.func_name)
    calls = [TestModel]
    lib_path = compile_lib(calls, cwd=temp)
    output = compile_and_run(lib_path, c_code, temp, calls)
    assert int(output) == 42
Exemplo n.º 8
0
def test_function_import(temp):
    from random import randint

    @sv()
    def rand_():
        print(randint(0, 42))

    lib_file = compile_lib([rand_], cwd=temp)
    call_str = rand_.make_call().str() + ";\n"
    value = compile_and_run(lib_file, call_str, temp, [rand_])
    value = int(value)
    # randint is [a, b]
    assert value in range(0, 42 + 1)
Exemplo n.º 9
0
def test_sv_simulator(get_vector_filename, simulator, temp):
    avail, tester_cls = simulator_map[simulator]
    if not avail():
        pytest.skip("{0} not available".format(simulator))

    lib_path = compile_lib([BoxFilter], cwd=temp)
    sv_pkg = os.path.join(os.path.abspath(temp), "pysv_pkg.sv")
    generate_sv_binding([BoxFilter], filename=sv_pkg)
    sv_file = get_vector_filename("box_filter.sv")
    tb_file = get_vector_filename("test_sv_boxfilter.sv")

    tester = tester_cls(lib_path, sv_pkg, sv_file, tb_file, cwd=temp)
    tester.run()
Exemplo n.º 10
0
def test_function_output_ref1(temp):
    @sv(return_type=Reference(a=DataType.Int))
    def func_output():
        return 42

    lib_file = compile_lib([func_output], cwd=temp)
    cxx_code = """
using namespace pysv;
int a;
func_output(&a);
printf("%d", a);
pysv_finalize();
"""
    value = compile_and_run(lib_file, cxx_code, temp, [func_output], use_implementation=True)
    value = int(value)
    assert value == 42
Exemplo n.º 11
0
def test_verilator(get_vector_filename, temp):
    lib_path = compile_lib([BoxFilter], cwd=temp)
    header_file = os.path.join(os.path.abspath(temp), "box_filter.hh")
    generate_cxx_binding([BoxFilter], filename=header_file)

    # we have three files
    # the sv file, the driver file, and the header
    sv_file = get_vector_filename("box_filter.sv")
    driver = get_vector_filename("test_verilator_boxfilter.cc")
    # just run teh verilator
    tester = pysv.util.VerilatorTester(lib_path,
                                       sv_file,
                                       header_file,
                                       driver,
                                       cwd=temp)
    tester.run()
Exemplo n.º 12
0
def test_function_output_ref2(temp):
    @sv(return_type=Reference(a=DataType.Int, b=DataType.Int))
    def func_output():
        return 42, 43

    lib_file = compile_lib([func_output], cwd=temp)
    cxx_code = """
using namespace pysv;
int a, b;
func_output(&a, &b);
printf("%d\\n%d", a, b);
pysv_finalize();
"""
    value = compile_and_run(lib_file, cxx_code, temp, [func_output], use_implementation=True)
    values = [int(v) for v in value.split()]
    assert values[0] == 42 and values[1] == 43;
Exemplo n.º 13
0
def test_numpy(temp):
    import numpy as np

    @sv()
    def min_(a, b):
        return np.min([a, b])

    lib_file = compile_lib([min_], cwd=temp)
    call_str = min_.make_call(-1, -2).str()
    code = """
    auto r = {0};
    std::cout << r << std::endl;
    """.format(call_str)

    outputs = compile_and_run(lib_file, code, temp, [min_])
    assert int(outputs) == -2
Exemplo n.º 14
0
def test_sv_return_reference(get_vector_filename, temp):
    # test out return reference
    simulator = "xcelium"
    avail, tester_cls = simulator_map[simulator]
    if not avail():
        pytest.skip(simulator + " is not available")

    @sv(return_type=Reference(a=DataType.Int, b=DataType.Int))
    def set_value():
        return 42, 43

    lib_path = compile_lib([set_value], cwd=temp)
    sv_pkg = os.path.join(os.path.abspath(temp), "pysv_pkg.sv")
    generate_sv_binding([set_value], filename=sv_pkg)
    tb_file = get_vector_filename("test_sv_return_reference.sv")

    tester = tester_cls(lib_path, sv_pkg, tb_file, cwd=temp)
    tester.run()
Exemplo n.º 15
0
def test_type_import(temp):
    from random import Random

    @sv()
    def foo():
        rand = Random()
        rand.seed(0)
        val = rand.randint(0, 42)
        print(val)

    lib_file = compile_lib([foo], cwd=temp)
    call_str = foo.make_call().str() + ";\n"
    value = compile_and_run(lib_file, call_str, temp, [foo])
    value = int(value)
    rand = Random()
    rand.seed(0)
    expected = rand.randint(0, 42)
    assert value == expected
Exemplo n.º 16
0
def test_cxx_object_funcs_2(temp):
    class ClassA2:
        @sv()
        def __init__(self):
            self.num = 42

    @sv(a=ClassA2)
    def foo(a):
        print(a.num)

    lib_file = compile_lib([ClassA2, foo], cwd=temp)
    cxx_code = """
using namespace pysv;
ClassA2 a;
foo(&a);
pysv_finalize();
    """
    value = compile_and_run(lib_file, cxx_code, temp, [ClassA2, foo], use_implementation=True)
    value = int(value)
    assert value == 42
Exemplo n.º 17
0
def test_subclass_abc(temp):
    from abc import ABC, abstractmethod

    class ClassBase(ABC):
        @abstractmethod
        def foo(self):
            pass

    class ClassChild(ClassBase):
        @sv()
        def __init__(self):
            pass

        @sv()
        def foo(self):
            pass

    c_code = ""
    lib_path = compile_lib([ClassChild], cwd=temp)
    compile_and_run(lib_path, c_code, temp, [ClassChild])
Exemplo n.º 18
0
def test_str(temp):
    @sv(s=DataType.String, return_type=DataType.String)
    def mul_str(num, s):
        return num * s

    lib_file = compile_lib([mul_str], cwd=temp)
    code = """
    const char *str = "test ";
    int32_t repeat = 4;
    auto r1 = mul_str(repeat, str);
    std::cout << r1 << std::endl;
    // another call
    auto r2 = mul_str(repeat + 1, str);
    std::cout << r2;
    """

    outputs = compile_and_run(lib_file, code, temp, [mul_str])
    outputs = outputs.splitlines()
    assert outputs[0] == "test " * 4
    assert outputs[1] == "test " * (4 + 1)
Exemplo n.º 19
0
def test_verilator_return_reference(get_vector_filename, temp):
    @sv(return_type=Reference(a=DataType.UInt, b=DataType.UInt))
    def set_value():
        return 42, 43

    lib_path = compile_lib([set_value], cwd=temp)
    header_file = os.path.join(os.path.abspath(temp),
                               "test_verilator_return_ref.hh")
    generate_cxx_binding([set_value], filename=header_file)

    # we have three files
    # the sv file, the driver file, and the header
    sv_file = get_vector_filename("test_verilator_return_ref.sv")
    driver = get_vector_filename("test_verilator_return_ref.cc")
    # just run teh verilator
    tester = pysv.util.VerilatorTester(lib_path,
                                       sv_file,
                                       header_file,
                                       driver,
                                       cwd=temp)
    tester.run()
Exemplo n.º 20
0
def test_sv_object_funcs_2(get_vector_filename, temp):
    # test out a normal function takes in Python object
    simulator = "xcelium"
    avail, tester_cls = simulator_map[simulator]
    if not avail():
        pytest.skip(simulator + " is not available")

    class ClassA2:
        @sv()
        def __init__(self):
            self.num = 1

    @sv(a=ClassA2)
    def add(a):
        return a.num + 41

    lib_path = compile_lib([ClassA2, add], cwd=temp)
    sv_pkg = os.path.join(os.path.abspath(temp), "pysv_pkg.sv")
    generate_sv_binding([ClassA2, add], filename=sv_pkg)
    tb_file = get_vector_filename("test_sv_object_funcs_2.sv")

    tester = tester_cls(lib_path, sv_pkg, tb_file, cwd=temp)
    tester.run()
Exemplo n.º 21
0
def test_tensorflow(get_vector_filename, temp):
    # should work with any simulator, but it seems like the Xcelium we have doesn't ship
    # with the latest libstdc++. Use vcs instead
    avail, tester_cls = simulator_map["vcs"]
    if not avail():
        pytest.skip("VCS not available")
    import tensorflow as tf

    @sv()
    def simple_mat_mal(a, b):
        c = tf.constant([[a, a], [a, a]])
        d = tf.constant([[b, b], [b, b]])
        e = tf.matmul(c, d)
        n = e.numpy()
        s = np.sum(n)
        return s

    lib_path = compile_lib([simple_mat_mal], cwd=temp)
    sv_pkg = os.path.join(os.path.abspath(temp), "pysv_pkg.sv")
    generate_sv_binding([simple_mat_mal], filename=sv_pkg)
    tb_file = get_vector_filename("test_tensorflow.sv")

    tester = tester_cls(lib_path, sv_pkg, tb_file, cwd=temp)
    tester.run()