Polyphonyって何だろう(1)インストール

Python で書いた関数を Verilog HDL に変換する高位合成コンパイラである Polyphony を使ってみたいと思います。Veriloggen が Verilogpython で生成し、高位合成がおまけ的なのに対して、Polyphony は、python で動く関数を verlilog に変換するソフトです。

Anaconda インストール

前提として Windows で使うことを想定します。
準備は基本的に Veriloggen を使う時と同じなのでそちらを参照してください。
feynman.hatenablog.com
veriloggen 関連の代わりに

>pip install polyphony

で polyphony をインストールするのみで、非常に簡単です。

Icarus Verilog for Windowsのインストール

これも下記から、veriloggen の時と同じく iverilog をダウンロード してインストールしてください。
Icarus Verilog for Windows

簡単実行ユーティリティー

コマンドを打つのは面倒なので、事前に下記の python ファイル compile.py を作っておいて使いましょう。

import os
import polyphony
import subprocess

target = 'fib_class_n' #file name = module name

pytarget = target + '.py'
outtarget = target + '.out'
testtarget = target + '_test.v'
simtarget = target + '_test.vcd' #test name

# %%
def addtimescale(fname):
    '''
    指定されたfnameファイルの先頭にtimescale行を挿入
    '''
    with open(fname) as f:
        l = f.readlines()
    l.insert(0,"`timescale 1ns/1ns \n")
    with open(fname, mode='w') as f:
        f.writelines(l)

# %%
def delete_file(fname):
    if os.path.isfile(fname):
        os.remove(fname)

# %%
cmd = ['polyphony', '-vd', pytarget]
p = subprocess.run(cmd, timeout=5, 
                    stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if(p.stdout != b''):print(p.stdout)
if(p.stderr != b''):print(p.stderr)
print('polyphony finished')

addtimescale(testtarget)
print('addtimescale finished')

cmd = ['iverilog', '-o', outtarget, 'polyphony_out.v', testtarget]
p = subprocess.run(cmd, timeout=5, stdout=subprocess.PIPE)
if(p.stdout != b''):print(p.stdout)
print('iverilog finished')

cmd = ['vvp', outtarget]
p = subprocess.run(cmd, timeout=5, stdout=subprocess.PIPE)
if(p.stdout != b''):print(p.stdout)
print('vvp finished')

cmd = ['gtkwave', '--giga', simtarget]
p = subprocess.run(cmd, stdout=subprocess.PIPE)
print('gtkwave finished')

次回は、実際に polyphony を使った VerilogHDLシミュレーション例を載せます。
pythonデバッグして、Verilog 変換し、シミュレーション結果を gtkwave で見るという一連の流れが非常に簡単にできて驚いてしまいます。