#!/usr/bin/python3 # Larry Doolittle, June 2015 # updates Sept 2015 # Lucas Russo, Feb 2022 (rest generation support and fixes to regex) # convert to python3 Feb 2022 # Pull port information out of a Verilog program # to build automatic documentation # Demand that the input have one port per line, and the # comment (if any) will be used for the documentation. # e.g., # input signed [17:0] x, // Multiplicand, signed, time-interleaved real and imaginary # HTML output needs hooks to put actual narrative in the file # No signed/unsigned information for ports? import re import json import os # from: https://stackoverflow.com/questions/8234274/how-to-indent-the-contents-of-a-multi-line-string try: import textwrap textwrap.indent except AttributeError: # undefined function (wasn't added until Python 3.3) def indent(text, amount, ch=" "): padding = amount * ch return "".join(padding+line for line in text.splitlines(True)) else: def indent(text, amount, ch=" "): return textwrap.indent(text, amount * ch) class verilog_port(): def __init__(self, io, signed, msb, lsb, ident, desc): self.io = io self.signed = signed self.msb = msb self.lsb = lsb self.ident = ident self.desc = desc if desc else "" def sign(self): if self.signed: return self.signed return "unsigned" def direction(self): r = self.io r = r[0].upper()+r[1:] return r def bits(self): if self.msb == 0 and self.lsb == 0: return "" return "[{}:{}]".format(self.msb, self.lsb) def xprint(self): print("Port {}".format(self.ident)) print(" inout {}".format(self.io)) print(" signed {}".format(self.signed)) print(" range [{}:{}]".format(self.msb, self.lsb)) print(" desc {}".format(self.desc)) def table_row(self): return "
")
print("
\n"
.format(fbase))
if param_list:
print("
| Name | Min | Max | Default | Description |
|---|
| Signal | Direction | Description |
|---|
") print("The portable") print("Verilog") print("implementation in {}".format(fname, fname)) print(hdata["implement_html"] + "\n") if True: print("
") print("A GTKWave-generated") print("timing diagram showing {} is shown here:" .format(hdata["timing_html"])) print("
\n"
.format(fbase))
try:
cfile = open(fbase+"_check", 'r')
print("
") print("Expected results of make {}_check:
".format(fbase))
print(cfile.read()+"\n")
except FileNotFoundError:
pass
print("")
def make_src_rst(fname):
fbase, fext = os.path.splitext(os.path.basename(fname))
print(".. _{}_source:".format(fbase))
print("")
print('{} Source File'.format(fbase))
print('{}'.format("".join(['='*(len(fbase)+12)])))
print("")
print(".. code-block:: verilog")
print(" :linenos:")
print("")
with open(fname, 'r') as ifile:
print(indent(ifile.read(), 3))
def make_rst(fname, param_list, port_list, mod_comment_list, with_timing=None):
fbase, fext = os.path.splitext(os.path.basename(fname))
print(".. _{}:".format(fbase))
print("")
print('{}'.format(fbase))
print('{}'.format("".join(['='*len(fbase)])))
print("")
if mod_comment_list:
description_txt = "Description"
print('{}'.format(description_txt))
print('{}'.format("".join(["\'"*len(description_txt)])))
print("")
for c in mod_comment_list:
# RST is sensitive with leading spaces, use line blocks
print("| " + c.desc_row_rst())
print("")
if True:
pinout_txt = "Pinout"
print('{}'.format(pinout_txt))
print('{}'.format("".join(["\'"*len(pinout_txt)])))
print("")
print(".. _fig:{}_block:".format(fbase))
print(".. figure:: {}_block.png".format(fbase))
print(" :alt: Schematic symbol")
print("")
if param_list:
param_txt = "Parameters"
print('{}'.format(param_txt))
print('{}'.format("".join(["\'"*len(param_txt)])))
print("")
print(".. list-table:: {}_param_table".format(fbase))
print(" :header-rows: 1")
print("")
print(indent("""* - Name
- Min
- Max
- Default
- Description""", 3))
for p in param_list:
print(indent(p.table_row_rst(), 3))
print("")
if port_list:
port_txt = "Ports"
print('{}'.format(port_txt))
print('{}'.format("".join(["\'"*len(port_txt)])))
print("")
print(".. list-table:: {}_port_table".format(fbase))
print(" :header-rows: 1")
print("")
print(indent("""* - Signal
- Direction
- Description""", 3))
for p in port_list:
print(indent(p.table_row_rst(), 3))
print("")
if True:
imp_txt = "Implementation and use"
print('{}'.format(imp_txt))
print('{}'.format("".join(["\'"*len(imp_txt)])))
print("")
print("The `portable`_ `Verilog`_")
print("implementation can be found in :ref:`{}_source`".format(fbase))
print("")
print(".. _`portable`: https://en.wikipedia.org/wiki/Software_portability")
print(".. _`Verilog`: https://en.wikipedia.org/wiki/Verilog")
print("")
if with_timing:
timing_txt = "Timing Diagram"
print('{}'.format(timing_txt))
print('{}'.format("".join(["\'"*len(timing_txt)])))
print("")
print("A `GTKWave`_-generated timing diagram is shown below:")
print("")
print(".. _`GTKWave`: https://gtkwave.sourceforge.net/")
print("")
print(".. _fig:{}_timing:".format(fbase))
print(".. figure:: {}_timing.png".format(fbase))
print(" :alt: Timing diagram")
print("")
def count_inout(port_list):
n_in, n_out = 0, 0
for p in port_list:
if p.io == "input":
n_in = n_in+1
elif p.io == "output":
n_out = n_out+1
else:
print("warning", p.io)
return n_in, n_out
def make_eps(port_list):
n_in, n_out = count_inout(port_list)
height = 24*max(n_in, n_out)
width = 192 # conceptually depends on length of port names
print("""%!PS-Adobe-3.0 EPSF-3.0
%%Title: symbol.eps
%%Creator: portfind.py
%%Pages: 1""")
print("%%BoundingBox:", "68 68 {} {}".format(112+width, 100+height)) # 244 220
print("""%%DocumentNeededResources: font Helvetica
%%EndComments
%%BeginProlog""")
print("""
/rshow { dup stringwidth pop neg 0 rmoveto show } def
/lpin { setlinewidth moveto gsave 7 -5 rmoveto show grestore -20 0 rlineto stroke } def
/rpin { setlinewidth moveto gsave -7 -5 rmoveto rshow grestore 20 0 rlineto stroke } def
/RT { % w h x y RT -
% draw a rectangle size w h at x y
moveto
dup 0 exch rlineto
exch 0 rlineto
neg 0 exch rlineto
closepath } def
/Helvetica findfont 15 scalefont setfont
""")
pitch = 24
y_offset = 72
y_center = y_offset + height/2
y_l = y_center+pitch*n_in/2
y_r = y_center+pitch*n_out/2
x_l = 90
for p in port_list:
if p.io == "input":
x = x_l
cmd = "lpin"
y = y_l
y_l = y_l - pitch
elif p.io == "output":
x = 90+width
cmd = "rpin"
y = y_r
y_r = y_r - pitch
if p.msb == 0 and p.lsb == 0:
w = 1 # single wire
else:
w = 3 # bus
print("({}) {} {} {} {}".format(p.ident, int(x), int(y), int(w), cmd))
print("1 setlinewidth {} {} {} {} RT stroke".format(width, height+24, x_l, y_offset))
print("""
showpage
%%Trailer
%%EOF""")
def svg_line(x0, y0, x1, y1, w):
print("