Leave the troublesome processing to Python

Introduction

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.

flow

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();
}

SV code

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?

Actually. .. ..

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

Leave the troublesome processing to Python
Draw graphs in Julia ... Leave the graphs to Python
python3 Measure the processing speed.
In the python command python points to python3.8
How to get the Python version
[Python] How to import the library
[Python] Change the alphabet to numbers
Python amateurs try to summarize the list ①
The story of blackjack A processing (python)
The road to compiling to Python 3 with Thrift
Updated to Python 2.7.9
python image processing
Python file processing
Python: I want to measure the processing time of a function neatly
"Backport" to python 2
How to use the C library in Python
The fastest way for beginners to master Python
Introduction to Python Let's prepare the development environment
Python constants like None (according to the reference)
Python inexperienced person tries to knock 100 language processing 14-16
Use the retry processing mode added to Boto3
How to do multi-core parallel processing with python
Operate the Speech Signal Processing Toolkit via python
Try to solve the Python class inheritance problem
[Python] How to change the date format (display format)
Just add the python array to the json data
Type Python scripts to run in QGIS Processing
The easiest way to synthesize speech with python
View the result of geometry processing in Python
Try to solve the man-machine chart with Python
Specify the Python executable to use with virtualenv
To dynamically replace the next method in python
Say hello to the world with Python with IntelliJ
A clever way to time processing in Python
Image processing? The story of starting Python for
Excel X Python The fastest way to work
The easiest way to use OpenCV with python
The trick to write flatten concisely in python
[Introduction to Python3 Day 20] Chapter 9 Unraveling the Web (9.1-9.4)
[Algorithm x Python] How to use the list
Use the Python framework "cocotb" to test Verilog.
How to erase the characters output by Python
Introduction to Python with Atom (on the way)
Experiment and leave evidence to determine the specifications.
How to get the files in the [Python] folder
Python inexperienced person tries to knock 100 language processing 07-09
Python inexperienced person tries to knock 100 language processing 10 ~ 13
[Introduction to Algorithm] Find the shortest path [Python3]
A python amateur tries to summarize the list ②
[Python] I will upload the FTP to the FTP server.
Knowledge notes needed to understand the Python framework
[Python] Throw a message to the slack channel
Python inexperienced person tries to knock 100 language processing 05-06
An article summarizing the pitfalls addicted to python
I want to display the progress in Python!
Python inexperienced person tries to knock 100 language processing 00-04
Python distributed processing Spartan
Find the maximum Python
Easy way to check the source of Python modules
python beginners tried to predict the number of criminals
[Python] How to remove duplicate values from the list