Call Python Functions from SystemVerilog

Invoke a Python function from your SystemVerilog.

This SystemVerilog code snippet demonstrates how to call Python’s functions from a SystemVerilog simulation environment. It uses Python integration to leverage Python’s built-in mathematical functions.

import pystim_pkg::*;

module math_factorial();
    typedef pystim_pkg::pystim py;

    initial begin
        py::initialize_interpreter();
        begin
            automatic py_module math_module = py_module::import_("math");
            automatic py_object fact_res = math_module.attr("factorial").call(py::int_(4));
            automatic int fact_value = pi.cast_int().get_value();
            $display("The value of 4! is: %0d", pi_value);
        end
        py::finalize_interpreter();
    end
endmodule

The math::factorial function is called with an input of 4, and its result is saved as py_object fact_res before being converted to a SystemVerilog int.