Add XML examples.

This commit is contained in:
Wilson Snyder 2019-11-02 16:35:50 -04:00
parent 7febeab275
commit 25f08b29c6
8 changed files with 355 additions and 1 deletions

View File

@ -34,6 +34,8 @@ bin/verilator_bin.*
bin/verilator_coverage_bin.*
docs/Makefile$
docs/doxygen-doc/.*
examples/xml_py/copied/
examples/xml_py/graph.*
src/Makefile$
src/Makefile_obj$
include/verilated.mk$

View File

@ -157,7 +157,7 @@ DISTFILES_INC = $(INFOS) .gitignore \
src/.gdbinit \
src/*.pl src/*.pod \
examples/*/.*ignore examples/*/Makefile* \
examples/*/*.[chv]* examples/*/*.pl \
examples/*/*.[chv]* examples/*/vl_* \
examples/*/CMakeLists.txt \
test_*/.*ignore test_*/Makefile* test_*/*.cpp \
test_*/*.pl test_*/*.v test_*/*.vc test_*/*.vh \

6
examples/xml_py/.gitignore vendored Normal file
View File

@ -0,0 +1,6 @@
obj_*
copied
*.csv
*.dot
*.dmp
*.pdf

49
examples/xml_py/Makefile Normal file
View File

@ -0,0 +1,49 @@
######################################################################
#
# DESCRIPTION: Verilator Example: XML tests
#
# Copyright 2003-2019 by Wilson Snyder. This program is free software; you can
# redistribute it and/or modify it under the terms of either the GNU
# Lesser General Public License Version 3 or the Perl Artistic License
# Version 2.0.
#
######################################################################
# This makefile is not intended to be useful as an example itself,
# it is just to run the small example scripts in this directory.
# If $VERILATOR_ROOT isn't in the environment, we assume it is part of a
# package install, and verilator is in your path. Otherwise find the
# binary relative to $VERILATOR_ROOT (such as when inside the git sources).
ifeq ($(VERILATOR_ROOT),)
VERILATOR = verilator
else
export VERILATOR_ROOT
VERILATOR = $(VERILATOR_ROOT)/bin/verilator
endif
DOT = dot
PYTHON = python3
default: test
test: \
test-vl_file_copy \
test-vl_hier_graph \
test-vl_file_copy:
@echo "-- vl_file_copy example"
$(PYTHON) vl_file_copy -odir copied top.v
@cmp copied/top.v top.v
@cmp copied/sub.v sub.v
test-vl_hier_graph:
@echo "-- vl_hier_graph example"
$(PYTHON) vl_hier_graph -o graph.dot top.v
$(DOT) -Tpdf -o graph.pdf graph.dot
######################################################################
maintainer-copy::
clean mostlyclean distclean maintainer-clean::
-rm -rf obj_dir *.log *.csv *.dmp *.dot *.vpd *.pdf core copied

17
examples/xml_py/sub.v Normal file
View File

@ -0,0 +1,17 @@
// DESCRIPTION: Verilator: Verilog Test module
//
// This file ONLY is placed into the Public Domain, for any use,
// without warranty, 2019 by Wilson Snyder.
// ======================================================================
module sub
#(parameter type TYPE_t = logic)
(
input TYPE_t in,
output TYPE_t out
);
// Some simple logic
always_comb out = ~ in;
endmodule

32
examples/xml_py/top.v Normal file
View File

@ -0,0 +1,32 @@
// DESCRIPTION: Verilator: Verilog example module
//
// This file ONLY is placed into the Public Domain, for any use,
// without warranty, 2019 by Wilson Snyder.
// ======================================================================
module top
(
input clk,
input fastclk,
input reset_l,
output wire [1:0] out_small,
output wire [39:0] out_quad,
output wire [69:0] out_wide,
input [1:0] in_small,
input [39:0] in_quad,
input [69:0] in_wide
);
sub #(.TYPE_t(logic [1:0])) sub_small
(.in(in_small),
.out(out_small));
sub #(.TYPE_t(logic [39:0])) sub_quad
(.in(in_quad),
.out(out_quad));
sub #(.TYPE_t(logic [69:0])) sub_wide
(.in(in_wide),
.out(out_wide));
endmodule

112
examples/xml_py/vl_file_copy Executable file
View File

@ -0,0 +1,112 @@
#!/usr/bin/env python3
# -*- Python -*- See copyright, etc below
######################################################################
import argparse
import os
import re
import subprocess
import sys
import tempfile
import xml.etree.ElementTree as ET
from shutil import copy2
from pprint import pprint,pformat
#######################################################################
class VlFileCopy:
def __init__(self,
verilator_args, # presently all verilator options are passed-thru
# ideally this script would check against options mentioned in help
debug=0,
output_dir='copied'): # directory name we output file uses
self.debug = debug
xml_temp = tempfile.NamedTemporaryFile()
args = ['--xml-output', xml_temp.name,
'--bbox-sys', # Parse some stuff can't translate
'--bbox-unsup',
'--prefix vlxml'] # So we know name of .xml output
args += verilator_args
self.run_verilator(args)
self.tree = ET.parse(xml_temp.name)
os.makedirs(output_dir, 0o777, True)
# Find and copy the files
root = self.tree.getroot()
xfiles = root.find('files')
for xfile in xfiles.findall('file'):
filename = xfile.get('filename')
if not re.match('^<', filename): # e.g. <built-in>
if self.debug:
print("\tcp %s %s" % (filename, output_dir))
copy2(filename, output_dir)
def run_verilator(self, args):
"""Run Verilator command, check errors"""
if os.getenv("VERILATOR_ROOT"):
command = os.getenv("VERILATOR_ROOT") + "/bin/verilator"
else:
command = "verilator"
command += ' ' + ' '.join(args)
if self.debug:
print("\t%s " % command)
status = subprocess.call(command, shell=True)
if status != 0:
raise Exception("Command failed running Verilator with '"+command+"', stopped")
#######################################################################
if __name__=='__main__':
parser = argparse.ArgumentParser(
allow_abbrev=False,
formatter_class=argparse.RawTextHelpFormatter,
description=
"""Example of using Verilator XML output to copy a list of files to an
output directory (-odir, defaults to 'copied'), e.g. to easily create a
tarball of the design to pass to others.
Example usage:
vl_file_copy -f input.vc top.v -odir mycopy
# This will make at least mycopy/top.v
""",
epilog=
"""All other arguments are pass-thru to Verilator: e.g.:
+define+<var>=<value> Set preprocessor define
-F <file> Parse options from a file, relatively
-f <file> Parse options from a file
-G<name>=<value> Overwrite toplevel parameter
+incdir+<dir> Directory to search for includes
+libext+<ext>+[ext]... Extensions for finding modules
-v <filename> Verilog library
-y <dir> Directory to search for modules
This file ONLY is placed into the Public Domain, for any use, without
warranty, 2019 by Wilson Snyder."""
)
parser.add_argument('-debug', '--debug',
action='store_const', const=9,
help='enable debug')
parser.add_argument('-odir', '--odir',
action='store', metavar='directory', required=True,
help='target output directory')
(args, rem) = parser.parse_known_args()
print("NOTE: vl_file_copy is only an example starting point for writing your own tool.")
# That is:
# 1. We will accept basic patches
# 2. We are not expecting to make this globally useful. (e.g. we don't cleanup obj_dir)
# 3. "make install" will not install this.
# 4. This has not had production-worthy validation.
fc = VlFileCopy(output_dir = args.odir,
debug = args.debug,
verilator_args = rem)
######################################################################
### Local Variables:
### compile-command: "./vl_file_copy -h ; VERILATOR_ROOT=$V4 ./vl_file_copy +define+thru top.v"
### End:

136
examples/xml_py/vl_hier_graph Executable file
View File

@ -0,0 +1,136 @@
#!/usr/bin/env python3
# -*- Python -*- See copyright, etc below
######################################################################
import argparse
import os
import re
import subprocess
import sys
import tempfile
import xml.etree.ElementTree as ET
from shutil import copy2
from pprint import pprint,pformat
#######################################################################
class VlHierGraph:
def __init__(self,
verilator_args, # presently all verilator options are passed-thru
# ideally this script would check against options mentioned in help
debug=0,
output_filename='graph.dot'): # output filename
self.debug = debug
self.next_vertex_number = 0
self.name_to_number = {}
xml_temp = tempfile.NamedTemporaryFile()
args = ['--xml-output', xml_temp.name,
'--bbox-sys', # Parse some stuff can't translate
'--bbox-unsup',
'--prefix vlxml'] # So we know name of .xml output
args += verilator_args
self.run_verilator(args)
self.tree = ET.parse(xml_temp.name)
with open(output_filename, "w") as fh:
# For more serious purposes, use the python graphviz package instead
fh.write("digraph {\n")
fh.write(" dpi=300;\n")
fh.write(" order=LR;\n")
fh.write(" node [fontsize=8 shape=\"box\" margin=0.01 width=0 height=0]")
fh.write(" edge [fontsize=6]")
# Find cells
root = self.tree.getroot()
netlist = root.find('netlist')
for module in netlist.findall('module'):
# origNames are before parameterization, name if after
mod_name = module.get('name')
mod_number = self.name_to_vertex_number(mod_name)
fh.write(" n%d [label=\"%s\""
% (mod_number, mod_name))
if module.get('topModule'):
fh.write(" color=\"red\" rank=1")
fh.write("];\n")
for instance in module.findall('instance'):
inst_name = instance.get('name')
def_name = instance.get('defName')
def_number = self.name_to_vertex_number(def_name)
fh.write(" n%d->n%d [label=\"%s\"];\n"
% (mod_number, def_number, inst_name));
fh.write("}\n")
def name_to_vertex_number(self, name):
if not name in self.name_to_number:
self.next_vertex_number += 1
self.name_to_number[name] = self.next_vertex_number
return self.name_to_number[name]
def run_verilator(self, args):
"""Run Verilator command, check errors"""
if os.getenv("VERILATOR_ROOT"):
command = os.getenv("VERILATOR_ROOT") + "/bin/verilator"
else:
command = "verilator"
command += ' ' + ' '.join(args)
if self.debug:
print("\t%s " % command)
status = subprocess.call(command, shell=True)
if status != 0:
raise Exception("Command failed running Verilator with '"+command+"', stopped")
#######################################################################
if __name__=='__main__':
parser = argparse.ArgumentParser(
allow_abbrev=False,
formatter_class=argparse.RawTextHelpFormatter,
description=
"""Example of using Verilator XML output to create a .dot file showing the
design module hierarchy.
Example usage:
vl_hier_graph -f input.vc top.v -o graph.dot
dot -Tpdf -o graph.pdf graph.dot
""",
epilog=
"""All other arguments are pass-thru to Verilator: e.g.:
+define+<var>=<value> Set preprocessor define
-F <file> Parse options from a file, relatively
-f <file> Parse options from a file
-G<name>=<value> Overwrite toplevel parameter
+incdir+<dir> Directory to search for includes
+libext+<ext>+[ext]... Extensions for finding modules
-v <filename> Verilog library
-y <dir> Directory to search for modules
This file ONLY is placed into the Public Domain, for any use, without
warranty, 2019 by Wilson Snyder."""
)
parser.add_argument('-debug', '--debug',
action='store_const', const=9,
help='enable debug')
parser.add_argument('-o', '--o',
action='store', metavar='filename', required=True,
help='output filename')
(args, rem) = parser.parse_known_args()
print("NOTE: vl_hier_graph is only an example starting point for writing your own tool.")
# That is:
# 1. We will accept basic patches
# 2. We are not expecting to make this globally useful. (e.g. we don't cleanup obj_dir)
# 3. "make install" will not install this.
# 4. This has not had production-worthy validation.
fc = VlHierGraph(output_filename = args.o,
debug = args.debug,
verilator_args = rem)
######################################################################
### Local Variables:
### compile-command: "./vl_hier_graph -h ; VERILATOR_ROOT=$V4 ./vl_hier_graph +define+thru top.v"
### End: