Before and after me, generating HDL from Python, synthesizing gates directly from Python, etc. ((((; ゚ Д ゚)))) is a jerky bull, but this is a very easy article.
SystemVerilog isn't just for writing hardware, isn't it? I mean, I've been able to use HDL since I became a member of society, but I've never synthesized hardware. I only program with SystemVerilog, but the disadvantage is that it is inefficient because there are few libraries (although there are a lot of libraries for verification). If it was a normal language, it would be one shot in the library. .. .. Even so, it's quite stressful to have to write it yourself.
In such a case, let's connect to Python, which everyone loves, and write as little as possible.
Use SystemVerilog DPI. In some book (SystemVerilog design startup?), DPI is planned to be connected not only to C but also to various languages, but at the moment, there is only DPI-C, so via DPI-C, with SystemVerilog You will be connecting Python.
SystemVerilog - (DPI) - C - Python
It is a flow.
This time, I would like to pass the coefficients of the quadratic equation from SystemVerilog to Python and return the solution to SystemVerilog.
Python Have SymPy solve the quadratic equation.
two_order_eq_py.py
import sympy
def calc_two_order_eq(a, b, c):
x = sympy.symbols('x')
f = a*x**2 + b*x + c
solve = sympy.solve(f, x)
return solve
C It's been C for the first time in a few years, so I don't think it feels very good. I think it can be made more versatile. .. ..
two_order_eq.c
#include <stdio.h>
#include <stdlib.h>
#include <Python.h>
#include <svdpi.h>
void two_order_eq(double* ary, double* answer) {
PyObject *pName, *pModule, *pFunc;
PyObject *pArgs, *pValue;
int i, order=2;
Py_Initialize();
pName = PyString_FromString("two_order_eq_py");
pModule = PyImport_Import(pName);
pFunc = PyObject_GetAttrString(pModule, "calc_two_order_eq");
pArgs = PyTuple_New(order+1); //Generate an argument tuple to pass to Python
for (i=0; i<order+1; i++) {
pValue = PyFloat_FromDouble(ary[i]);
PyTuple_SetItem(pArgs, i, pValue);
}
pValue = PyObject_CallObject(pFunc, pArgs); //Execute python function
for (i=0; i<order; i++) { //Collect Python results.
answer[i] = PyFloat_AsDouble(PyList_GetItem(pValue, i));
}
Py_Finalize();
}
two_order_eq.sv
module top();
real ary[3];
real answer[2];
import "DPI-C" function void two_order_eq(input real ary[3], output real answer[2]);
initial begin
ary = {1, -1, -6};
two_order_eq(ary, answer);
$display("Answer is %f, %f", answer[0], answer[1]);
$finish;
end
endmodule
Make If you have a high-priced simulator, you can pass the SV code and C code to the simulator and it will compile and execute at once. If it's not expensive, should I make * .o and pass it with -sv_lib?
A better sample can be found at https://github.com/rfukatani/pylink. It seems that delta-sigma modulation is written in the Python part. Looking at this, I feel that I can go to a certain place without Matlab.
Anyway, this allows you to throw tedious work into Python, which greatly increases your productivity. How much SV code can be reduced is the first step to happiness for me.
Recommended Posts