├── .gitignore ├── scripts ├── bits-to-asm ├── gtkwave-helper ├── verilog-to-graphviz ├── vcd-prune └── addWavesRecursive.tcl ├── .travis.yml ├── .gitmodules ├── gtkwave └── Makefile ├── Verilog └── Makefile ├── verilator └── Makefile ├── Makefile ├── CONTRIBUTING.md ├── README.md └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | opt 2 | *~ 3 | .#* 4 | */build.log 5 | gtkwave/repo -------------------------------------------------------------------------------- /scripts/bits-to-asm: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | hexdump -v -e '".word 0x" 1 4 "%08x" "\n"' $@ 4 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | git: 2 | submodules: false 3 | language: generic 4 | dist: trusty 5 | 6 | script: 7 | - make build-all 8 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "verilator/repo"] 2 | path = verilator/repo 3 | url = http://git.veripool.org/git/verilator 4 | [submodule "Verilog/repo"] 5 | path = Verilog/repo 6 | url = http://git.veripool.org/git/Verilog-Perl 7 | [submodule "yosys/repo"] 8 | path = yosys/repo 9 | url = https://github.com/cliffordwolf/yosys.git 10 | -------------------------------------------------------------------------------- /gtkwave/Makefile: -------------------------------------------------------------------------------- 1 | PREFIX ?= $(abspath ../opt) 2 | 3 | .PHONY: all 4 | 5 | all: $(PREFIX)/bin/gtkwave 6 | 7 | install: 8 | $(PREFIX)/bin/gtkwave: | repo 9 | @echo "[INFO] Building GTKWave" 10 | (cd repo && \ 11 | ./configure --prefix=$(PREFIX) && \ 12 | make -j$(JOBS) install || true) 2>&1 | tee build.log 13 | 14 | repo: 15 | svn co "svn://svn.code.sf.net/p/gtkwave/code/gtkwave3" $@ 16 | 17 | mrproper: 18 | rm -rf repo 19 | -------------------------------------------------------------------------------- /Verilog/Makefile: -------------------------------------------------------------------------------- 1 | PREFIX ?= $(abspath ../opt) 2 | 3 | .PHONY: all 4 | 5 | all: $(PREFIX)/lib/perl5/x86_64-linux-gnu-thread-multi/Verilog/Netlist.pm 6 | 7 | install: 8 | $(PREFIX)/lib/perl5/x86_64-linux-gnu-thread-multi/Verilog/Netlist.pm: | repo/.git 9 | @echo "[INFO] Building Verilog::Perl" 10 | (cd repo && \ 11 | perl Makefile.PL INSTALL_BASE=$(PREFIX) && \ 12 | make JOBS=$(JOBS) && \ 13 | make install JOBS=$(JOBS)) 2>&1 | tee build.log 14 | 15 | repo/.git: 16 | git submodule update --init repo 17 | 18 | mrproper: 19 | rm -rf repo && git checkout repo 20 | -------------------------------------------------------------------------------- /verilator/Makefile: -------------------------------------------------------------------------------- 1 | PREFIX ?= $(abspath ../opt) 2 | 3 | .PHONY: all clean mrproper 4 | 5 | all: $(PREFIX)/bin/verilator 6 | 7 | intall: 8 | $(PREFIX)/bin/verilator: | repo/.git 9 | @echo "[INFO] Building Verilator" 10 | @echo "[INFO] Verilator uses a lot of memory to build, limiting jobs to 2" 11 | (cd repo && \ 12 | autoconf && \ 13 | ./configure --prefix=$(PREFIX) && \ 14 | make -j2 && \ 15 | make install) 2>&1 | tee build.log 16 | 17 | repo/.git: 18 | git submodule update --init repo 19 | 20 | clean: 21 | @(cd repo && make clean) >> build.log 22 | 23 | mrproper: 24 | rm -rf repo && git checkout repo 25 | -------------------------------------------------------------------------------- /scripts/gtkwave-helper: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # See LICENSE for license details. 4 | 5 | # Helper script that launches GTKWave for a specific [vcd-file] using 6 | # scripts/addWavesRecursive.tcl to populate the waveform window with 7 | # signals grouped by module. 8 | 9 | usage () { 10 | echo "Usage: $0 [vcd-file]" >&2 11 | } 12 | 13 | base_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"/.. 14 | ENV=env PATH=$base_dir/opt/bin:$PATH 15 | 16 | if [ $# -ne 1 ]; then 17 | usage 18 | exit 19 | fi 20 | 21 | if [ ! -f $base_dir/opt/bin/gtkwave ]; then 22 | make -C $base_dir gtkwave; 23 | fi; 24 | 25 | echo "[INFO] Preprocessing $1 to generate $1.gtkw" >&2 26 | time $ENV gtkwave -g -S $base_dir/scripts/addWavesRecursive.tcl $1 > $1.gtkw 27 | echo "[INFO] Launching graphical GTKWave..." >&2 28 | $ENV gtkwave -g $1 $1.gtkw 29 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | base_dir = $(abspath .) 2 | 3 | PREFIX ?= $(base_dir)/opt 4 | JOBS ?= 8 5 | 6 | tools = \ 7 | verilator \ 8 | gtkwave 9 | perl_libs = \ 10 | Verilog 11 | 12 | .PHONY: all build-all default gtkwave usage verilator 13 | 14 | default: usage 15 | 16 | usage: ## show this help 17 | @echo -e "Usage: make [target]\nBuild HDL tools in PREFIX (default: $(PREFIX))\n\nOptions:" 18 | @grep "##" $(MAKEFILE_LIST) | grep -v "#.ignore" | sed 's/^/ /' | sed 's/:.\+##/,/' | column -s, -t 19 | 20 | build-all: $(tools:%=$(PREFIX)/bin/%) $(PREFIX)/lib/perl5/x86_64-linux-gnu-thread-multi/Verilog/Netlist.pm ## build all tools 21 | verilator: $(PREFIX)/bin/verilator ## build verilator 22 | gtkwave: $(PREFIX)/bin/gtkwave ## build gtkwave 23 | verilog-perl: $(PREFIX)/lib/perl5/x86_64-linux-gnu-thread-multi/Verilog/Netlist.pm ## build Verilog::Perl 24 | 25 | $(PREFIX)/bin/%: 26 | $(MAKE) -C $(base_dir)/$* PREFIX=$(PREFIX) JOBS=$(JOBS) $@ 27 | $(PREFIX)/lib/perl5/x86_64-linux-gnu-thread-multi/Verilog/Netlist.pm: 28 | $(MAKE) -C $(base_dir)/Verilog PREFIX=$(PREFIX) JOBS=$(JOBS) $@ 29 | 30 | clean-installed: ## remove the installed tools in ./opt 31 | rm -rf $(base_dir)/opt 32 | 33 | clean-downloaded: ## blow away program source directories 34 | $(MAKE) -C $(base_dir)/verilator mrproper 35 | $(MAKE) -C $(base_dir)/gtkwave mrproper 36 | $(MAKE) -C $(base_dir)/Verilog mrproper 37 | 38 | mrproper: clean-installed clean-downloaded ## remote tools and blow away download sources 39 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | All contributors must agree to the Developer Certificate of Origin Version 1.1. (DCO 1.1) by signing their commits with: 2 | 3 | ``` 4 | DCO 1.1 Signed-off-by: [NAME] <[EMAIL]> 5 | ``` 6 | 7 | The full text of the DCO 1.1 is as follows: 8 | 9 | ``` 10 | Developer Certificate of Origin 11 | Version 1.1 12 | 13 | Copyright (C) 2004, 2006 The Linux Foundation and its contributors. 14 | 660 York Street, Suite 102, 15 | San Francisco, CA 94110 USA 16 | 17 | Everyone is permitted to copy and distribute verbatim copies of this 18 | license document, but changing it is not allowed. 19 | 20 | 21 | Developer's Certificate of Origin 1.1 22 | 23 | By making a contribution to this project, I certify that: 24 | 25 | (a) The contribution was created in whole or in part by me and I 26 | have the right to submit it under the open source license 27 | indicated in the file; or 28 | 29 | (b) The contribution is based upon previous work that, to the best 30 | of my knowledge, is covered under an appropriate open source 31 | license and I have the right under that license to submit that 32 | work with modifications, whether created in whole or in part 33 | by me, under the same open source license (unless I am 34 | permitted to submit under a different license), as indicated 35 | in the file; or 36 | 37 | (c) The contribution was provided directly to me by some other 38 | person who certified (a), (b) or (c) and I have not modified 39 | it. 40 | 41 | (d) I understand and agree that this project and the contribution 42 | are public and that a record of the contribution (including all 43 | personal information I submit with it, including my sign-off) is 44 | maintained indefinitely and may be redistributed consistent with 45 | this project or the open source license(s) involved. 46 | ``` 47 | -------------------------------------------------------------------------------- /scripts/verilog-to-graphviz: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env perl 2 | 3 | # See LICENSE for license details. 4 | 5 | use strict; 6 | use warnings; 7 | use vars qw($debug $fh); 8 | 9 | use Getopt::Long; 10 | use IO::File; 11 | use Pod::Usage; 12 | 13 | use lib 'opt/lib/perl5'; 14 | use Verilog::Netlist; 15 | 16 | # The default language is Verilog 2001 (1364-2001) as this is what 17 | # Chisel emits. 18 | Verilog::Language::language_standard("1364-2001"); 19 | 20 | my @opt_files; 21 | my $opt_output_filename; 22 | my $opt_module_regex = ""; 23 | my @opt_modules; 24 | $debug = 0; 25 | my $Opt = new Verilog::Getopt(filename_expansion=>1); 26 | if (! GetOptions 27 | ( 28 | "debug" => \$debug, 29 | "h|help" => \&usage, 30 | "l|language=s" => sub{shift;Verilog::Language::language_standard(shift);}, 31 | "m|module=s" => sub{shift;push @opt_modules, shift;}, 32 | "module-regex=s" => \$opt_module_regex, 33 | "o|output=s" => \$opt_output_filename, 34 | "<>" => \¶meter, 35 | )) { 36 | print STDERR "[ERROR] Bad usage\n" and usage(); 37 | } 38 | print "[ERROR] No input filenames specified.\n" and usage() if !@opt_files; 39 | 40 | # Parse all known Verilog-Perl options, see `man Verilog::Getopt` for 41 | # more information. 42 | @ARGV = $Opt->parameter(@ARGV); 43 | 44 | sub parameter { 45 | my $param = shift; 46 | if ($param =~ /^--?/) { 47 | die "[ERROR] Unknown parameter: $param\n"; 48 | } else { 49 | push @opt_files, "$param"; # Must quote for Getopt to string, bug298 50 | } 51 | } 52 | 53 | sub usage { 54 | pod2usage(-verbose=>2, -exitval=>1, -output=>\*STDOUT, -noperldoc=>1); 55 | } 56 | 57 | sub traverse { 58 | my $m = $_[0]; 59 | my $indent = $_[1]; 60 | my $i = $_[2]; 61 | my $prefix = $_[3]; 62 | 63 | my $name = $m->name; 64 | print "[DEBUG] $indent".$m->name."\n" if $debug; 65 | 66 | print $fh <<"END"; 67 | ${indent} ${prefix}_$i [label=$name] 68 | END 69 | my $cell_count = 0; 70 | foreach ($m->cells_sorted) { 71 | $cell_count ++; 72 | my $cell_name = $_->name; 73 | if (!$_->submod) { 74 | print "[DEBUG] $indent".$cell_name." (blackbox)\n" if $debug; 75 | next; 76 | } 77 | next if ($opt_module_regex && $_->submod->name =~ /$opt_module_regex/); 78 | traverse($_->submod, $indent." ", $cell_name, $prefix."_".$i); 79 | print $fh <<"END"; 80 | ${indent} ${prefix}_$i -> ${prefix}_${i}_$cell_name 81 | END 82 | } 83 | } 84 | 85 | my $nl = new Verilog::Netlist 86 | (options => $Opt, 87 | keep_comments => 1, 88 | use_vars => 1, 89 | link_read_nonfatal => 1, 90 | remove_defines_without_tick => 0, 91 | dump => 1, 92 | ); 93 | foreach my $file (@opt_files) { 94 | $nl->read_file(filename=>$file); 95 | } 96 | $nl->link(); 97 | $nl->exit_if_error(); 98 | 99 | if ($opt_output_filename) { 100 | open $fh, ">", $opt_output_filename or 101 | die "[ERROR] Unable to open $opt_output_filename\n"; 102 | my $header = <<"END"; 103 | digraph G { 104 | compound=true 105 | node[shape=box] 106 | END 107 | print $fh $header; 108 | } 109 | 110 | my @modules = $nl->top_modules_sorted; 111 | if (@opt_modules) { 112 | undef @modules; 113 | foreach (@opt_modules) { 114 | push @modules, $nl->find_module($_); 115 | } 116 | } 117 | 118 | foreach my $top (@modules) { 119 | traverse($top, " ", $top->name, ""); 120 | } 121 | 122 | if ($fh) { 123 | print $fh <<"END"; 124 | } 125 | END 126 | close $fh; 127 | } 128 | 129 | __END__ 130 | 131 | =pod 132 | 133 | =head1 NAME 134 | 135 | verilog-to-graphviz - Generate Graphviz output from Verilog input 136 | 137 | =head1 SYNOPSIS 138 | 139 | Usage: verilog-to-graphviz [OPTIONS] VERILOG.v 140 | 141 | =head1 OPTIONS 142 | 143 | TBD 144 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | HDL Tools [![Build Status](https://travis-ci.org/IBM/hdl-tools.svg?branch=master)](https://travis-ci.org/IBM/hdl-tools) 2 | ===================== 3 | 4 | Environment for working with HDLs. This builds a number of tools from source and relies on some scripts to facilitate HDL work. 5 | 6 | ## Programs 7 | All the programs are fetched (as a `git` submodule, via `svn`, or as a direct download) and built. You can do this with the included [Makefile](Makefile) targets for individual tools: 8 | * `verilator` -- Build Verilator 9 | * `gtkwave` -- Build GTKWave 10 | 11 | Additional targets include: 12 | * `build-all` -- Build all the tools 13 | * `usage` -- list all available build targets 14 | * `clean-installed` -- remove the installed tools in `./opt/` (__this will not respect `PREFIX` so that you don't nuke your /opt directory__) 15 | * `clean-downloaded` -- remove all downloaded program sources 16 | * `mrproper` -- remove downloaded program contents 17 | 18 | Tools will, by default, be installed in `./opt/`. However, you can pass a `PREFIX` option to the Makefile if for whatever reason you want to install these somewhere else (not advised). 19 | 20 | #### [Verilator](http://www.veripool.org/wiki/verilator) 21 | Verilog to C++ compiler. This is not required to be built for working with Rocket Chip (it will build its own version of Verilator). 22 | 23 | #### [GTKWave](http://gtkwave.sourceforge.net/) 24 | Open source (GPL2) waveform viewer with TCL integration. 25 | 26 | #### [Verilog::Perl](http://www.veripool.org/wiki/verilog-perl) 27 | Perl tools for working with Verilog netlists. 28 | 29 | ## Scripts 30 | 31 | #### [`addWavesRecursive.tcl`](scripts/addWavesRecursive.tcl) 32 | TCL script that, when passed to GTKWave, will generate a saved waveform view (a `.gtkw` file) that has all the signals in a given VCD file grouped by module. An example invocation would be: 33 | 34 | ``` 35 | gtkwave -S addWavesRecursive.tcl dump.vcd > dump.gtkw 36 | ``` 37 | 38 | You can then start GTKWave using this saved view with: 39 | 40 | ``` 41 | gtkwave dump.vcd dump.gtkw 42 | ``` 43 | 44 | After 904555632a5131686c20921593ba7010efece916, this is `O(n log n)` in the number of signals. Previously, this was `O(n^2)`. 45 | 46 | #### [`vcd-prune`](scripts/vcd-prune) 47 | Perl script to prune a VCD file to only include specific modules (and all their submodules). An example invocation would be: 48 | 49 | ``` 50 | ./vcd-prune dump.vcd -m MyModule -o dump-short.vcd 51 | ``` 52 | 53 | This will only dump signals contained in MyModule or its submodules. For testing a small part of a huge design (e.g., a RoCC unit attached to Rocket Chip), this can cut down dramatically on the size of the VCD file and the processing time of `addWavesRecursive.tcl` and startup time of a waveform viewer. 54 | 55 | * [`Verilator`](http://www.veripool.org/wiki/verilator) -- Verilog to C++ compiler 56 | * [`GTKWave`](http://gtkwave.sourceforge.net) -- Lightweight waveform viewer 57 | * `scripts/` 58 | * [`addWavesRecursive.tcl`](scripts/addWavesRecursive.tcl) -- TCL script for GTKWave that populates the waveform viewer with signals nested into the module hierarchy 59 | 60 | #### [`gtkwave-helper`](scripts/gtkwave-helper) 61 | Bash script that takes care of the boilerplate operations necessary to launch GTKWavewith `addWavesRecursive`. 62 | 63 | #### Complexities 64 | This comes up as these tools are intended to be used on large amounts of data 65 | 66 | | Tool | Complexity | What is N? | Critical Region | 67 | | ------------- | -------------: | -----: | --------------: | 68 | | [`addWavesRecursive.tcl`](scripts/addWavesRecursive.tcl) | n log n | number of signals | [tree merge](scripts/addWavesRecursive.tcl#L89) | 69 | | [`vcd-prune`](scripts/vcd-prune) | n | number of lines | [regex](scripts/vcd-prune#L112) | 70 | | [`gtkwave-helper`](scripts/gtkwave-helper) | | | | 71 | -------------------------------------------------------------------------------- /scripts/vcd-prune: -------------------------------------------------------------------------------- 1 | #!/usr/bin/perl -w 2 | 3 | # See LICENSE for license details. 4 | 5 | use Getopt::Long; 6 | use IO::File; 7 | use Pod::Usage; 8 | 9 | use strict; 10 | use warnings; 11 | use vars qw ($debug $PREFIX); 12 | 13 | $debug = 0; 14 | my $opt_modules = ""; 15 | my $opt_output_filename; 16 | my @opt_files; 17 | 18 | Getopt::Long::config ("no_auto_abbrev","no_pass_through"); 19 | if (! GetOptions 20 | ("debug" => \$debug, 21 | "h|help" => \&usage, 22 | "m|module=s" => sub{ 23 | shift; 24 | my $module = shift; 25 | if ($opt_modules eq "") { $opt_modules = "($module)"; } 26 | else {$opt_modules = "$opt_modules|($module)"} 27 | }, 28 | "o|output=s" => \$opt_output_filename, 29 | "<>" => \¶meter, 30 | )) { 31 | \&usage and die "[ERROR] Bad usage\n" 32 | } 33 | print STDERR "[ERROR] Specify at least one root module to dump.\n" and usage() 34 | if !$opt_modules; 35 | 36 | if ($debug) { print "[DEBUG] Using module regex match: /$opt_modules/\n"; } 37 | 38 | my $file_out; 39 | if ($opt_output_filename) { 40 | open $file_out, ">", "$opt_output_filename" or 41 | die "[ERROR] Unable to open $file_out\n"; 42 | } 43 | 44 | if (!@opt_files) { 45 | prune(\*STDIN); 46 | } else { 47 | foreach (@opt_files) { 48 | open my $fh, "<", $_ or die "[ERROR] Unable to open $_\n"; 49 | prune($fh); 50 | close $fh; 51 | } 52 | } 53 | 54 | if ($opt_output_filename) { 55 | close $file_out; 56 | } 57 | 58 | sub prune { 59 | my $fh = shift; 60 | 61 | # Build up a hash of all signals to include and signals to skip 62 | my %includes; 63 | my %skips; 64 | my @scope; 65 | my $header = ""; 66 | my $found_module = 0; 67 | while (<$fh>) { 68 | if ($_ =~ /^\$enddefinitions \$end$/) { 69 | if ($file_out) {print $file_out $_} else {print $_}; 70 | last; 71 | } 72 | if ($_ =~ /\$version|\$date|^\s*\$end$|\$timescale/) { 73 | if ($file_out) {print $file_out $_} else {print $_}; 74 | next; 75 | } 76 | 77 | if ($_ =~ /^\s*\$scope module (.+) \$end$/) { 78 | my $module = $1; 79 | if ($_ =~ /$opt_modules/ || $found_module) { 80 | $found_module++; 81 | if ($file_out) {print $file_out $_} else {print $_}; 82 | } 83 | 84 | push @scope, $module; 85 | if ($debug) { print "[DEBUG] $found_module $header$module\n"; } 86 | $header = "$header "; 87 | 88 | next; 89 | } 90 | if ($_ =~ /^\s*\$upscope \$end$/) { 91 | if ($found_module) { 92 | $found_module--; 93 | if ($file_out) {print $file_out $_} else {print $_}; 94 | } 95 | 96 | $header = substr($header, 0, -2); 97 | pop @scope; 98 | next; 99 | } 100 | if ($found_module) { 101 | if ($_ !~ /^\s*\$var\s+wire\s+\d+\s+(.+?)\s+(.+)\$end$/) { 102 | die; 103 | } 104 | if ($file_out) {print $file_out $_} else {print $_}; 105 | $includes{$1}++; 106 | } 107 | } 108 | 109 | # Prune the VCD file 110 | while (<$fh>) { 111 | if ($_ =~ /^#/ || /^$/) { 112 | if ($file_out) {print $file_out $_} else {print $_}; 113 | next; 114 | } 115 | my $signal; 116 | if ($_ =~ /^[01](.+)$/ || /^.+ (.+)$/) { 117 | $signal = $1; 118 | } 119 | if ($includes{$signal}) { 120 | if ($file_out) {print $file_out $_} else {print $_}; 121 | } 122 | } 123 | } 124 | 125 | sub usage { 126 | pod2usage(-verbose=>2, -exitval=>2, -output=>\*STDOUT, -noperldoc=>1); 127 | } 128 | 129 | sub parameter { 130 | my $param = shift; 131 | if ($param =~ /^--?/) { 132 | die "[ERROR] Unknown parameter: $param\n"; 133 | } else { 134 | push @opt_files, "$param"; 135 | } 136 | } 137 | 138 | __END__ 139 | 140 | =pod 141 | 142 | =head1 NAME 143 | 144 | vcd-prune - Remove/keep the signals of specific modules in a VCD file 145 | 146 | =head1 SYNOPSIS 147 | 148 | vcd-prune -m MODULE [-m MODULE...] [OPTION]... [VCD...] 149 | 150 | =head1 DESCRIPTION 151 | 152 | This reads an explicitly specified VCD file or STDIN and extracts 153 | only those signals encapsulated by the specified MODULE (or multiple 154 | modules). The output vcd file will be written to STDOUT by default 155 | or to a file if so specified. 156 | 157 | =head1 OPTIONS 158 | 159 | Mandatory arguments to long options are mandatory to short options, too. 160 | 161 | --debug Enable debugging information as the hierarchy is 162 | traversed. 163 | -h, --help Print this help text 164 | -m, --module=MODULE Modules to include in the VCD file. All submodules 165 | will be included. This option can be specified 166 | multiple times for multiple modules. By default 167 | all top modules are instrumented (why would you do 168 | this?). Note, this is technically a regex. 169 | -o, --output=FILE Output file. Default is STDOUT. 170 | -------------------------------------------------------------------------------- /scripts/addWavesRecursive.tcl: -------------------------------------------------------------------------------- 1 | # See LICENSE for license details. 2 | 3 | # TCL srcipt that uses the TCL-enabled version of GTKWave to generate 4 | # a .gtkw save file with all the signals and groups found in a .vcd 5 | # file. You will need to have the TCL enabled version of GTKWave 6 | # installed to use this, e.g.: 7 | # 8 | # yaourt -S gtkwave-tcl-svn 9 | # 10 | # Usage (run this to generate the .gtkw, then open it in GTKWave): 11 | # gtkwave -S addWavesRecursive.tcl [VCD FILE] > [GTKW FILE] 12 | # gtkwave [VCD FILE] [GTKW FILE] 13 | #------------------------------------------------------------------------------- 14 | 15 | #--------------------------------------- VCD file as a tree 16 | # Here, we're dealing with a data structure to describe a tree that 17 | # consists of nested lists. So, assume that we have the following HDL 18 | # structure as can be gleaned from looking at all the nodes in a VCD 19 | # file: 20 | # 21 | # A 22 | # |-- A.clk 23 | # |-- A.reset 24 | # |-- B 25 | # | |-- B.x 26 | # | \-- B.y 27 | # \-- C 28 | # 29 | # In a tree, this looks like: 30 | # 31 | # {A clk reset {B x y} C} 32 | # 33 | # Thereby the first entry in a list is the module name and everything 34 | # else is the module body (c.f., Lisp `car` and `cdr`). A signal is a 35 | # list with size == 1 while a module is a list with size > 1. 36 | proc car x { lindex $x 0 } 37 | proc cdr x { lrange $x 1 end } 38 | 39 | #--------------------------------------- New procedures for addWavesRecursive 40 | # Given a raw VCD signal, construct a tree out of this. 41 | proc constructTree {signal} { 42 | set tmp [lreverse [string map {. " "} $signal]] 43 | set tree [car $tmp] 44 | foreach node [cdr $tmp] { 45 | set tree [list $node $tree] 46 | } 47 | return $tree 48 | } 49 | 50 | # Merges two lexicographically sorted trees. 51 | proc merge_slow {a b} { 52 | set a_i 1 53 | set b_i 1 54 | while {($a_i < [llength $a]) || ($b_i < [llength $b])} { 55 | set x [lindex $a $a_i] 56 | set y [lindex $b $b_i] 57 | 58 | switch [string compare [car $x] [car $y]] { 59 | -1 {if {$a_i >= [llength $a] - 1} { break } 60 | incr a_i } 61 | 1 {if {$b_i >= [llength $b] - 1} { break } 62 | incr b_i } 63 | 0 {return [lreplace $a $a_i $a_i [merge_slow $x $y]] } 64 | } 65 | } 66 | return [lappend a [lindex $b 1]] 67 | } 68 | 69 | # Merges a bushy tree (branching factor >= 1) 'a' with all nodes at 70 | # the same depth lexicographically sorted with a slim tree (branching 71 | # factor == 1), 'b'. This compares, at the same depth, the last node 72 | # in the 'a' with the (only) node in 'b'. If these are the same, then 73 | # we recurse one layer deeper in 'a' and 'b'. Otherwise, 'b' is a new 74 | # branch that should be appended at this depth to 'a'. 75 | proc merge_fast {a b} { 76 | set a_i [expr {[llength $a] - 1}] 77 | set b_i 1 78 | 79 | set x [lindex $a $a_i] 80 | set y [lindex $b $b_i] 81 | 82 | if { [string compare [car $x] [car $y]] } { 83 | return [lappend a [lindex $b 1]] 84 | } 85 | 86 | return [lreplace $a $a_i $a_i [merge_fast $x $y]] 87 | } 88 | 89 | # GTKWave uses some special bit flags to tell it what type of signal 90 | # we're dealing with. This is all documented internally in their 91 | # "analyzer.h" file. However, all that we really care about are: 92 | # 93 | # 0x22: (right justified) | (hexadecimal format) 94 | # 0x28: (right justified) | (binary format) 95 | # 0xc00200: (group state) | (TR_CLOSED_B) | (TR_BLANK) 96 | # 0x1401200: (group end) | (TR_CLOSED_B) | (TR_BLANK) 97 | # 98 | # Whenever we see a group, we need to explicitly set these before the 99 | # group name. After the group, we need to revert to the default signal 100 | # format 0x22 (or 0x28). We set the groups as closed because this 101 | # seems to make the initial signal dump easier to look at. 102 | # alternatively, the following group bits can be used to have the 103 | # groups default to open: 104 | # 105 | # 0x800200: (group start) | (TR_BLANK) 106 | # 0x1000200: (group end) | (TR_BLANK) 107 | proc gtkwaveEnterModule {module header} { 108 | puts "\[*\]vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv" 109 | puts "@c00200" 110 | puts "-$header$module" 111 | puts "@22" 112 | } 113 | proc gtkwaveExitModule {module header} { 114 | puts "@1401200" 115 | puts "-$header$module" 116 | puts "@22" 117 | puts "\[*\]^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^" 118 | } 119 | 120 | # Walk a tree or emitting the enter/exit module GTK boilerplate 121 | # whenever we hit a module. 122 | proc gtkwaveEmitModule {tree prefix spacing header} { 123 | set car [car $tree] 124 | set cdr [cdr $tree] 125 | gtkwaveEnterModule $car $header 126 | # puts stderr "\[INFO\] $spacing$car" 127 | puts "\[*\] MODULE: $prefix$car" 128 | 129 | set signals [list] 130 | set newHeader [string map {"-" " "} $header] 131 | foreach signal $cdr { 132 | if {[llength $signal] > 1} { 133 | gtkwaveEmitModule $signal "$prefix$car." " $spacing" "$newHeader |-" 134 | } else { 135 | lappend signals $signal 136 | } 137 | } 138 | 139 | foreach signal $signals { 140 | puts "+{$signal} $prefix$car.$signal" 141 | } 142 | 143 | gtkwaveExitModule $car $header 144 | } 145 | 146 | #---------------------------------------- Main addWavesRecursive TCL script 147 | set nfacs [ gtkwave::getNumFacs ] 148 | set dumpname [ gtkwave::getDumpFileName ] 149 | set dmt [ gtkwave::getDumpType ] 150 | 151 | # Some information is included in the GTKWave header, however this 152 | # doesn't appear to have much effect on GTKWave. Generally, GTKWave 153 | # will just ignore things it doesn't understand. Nevertheless, we 154 | # default to using a coment syntax of "[*]": 155 | # puts "\[*\] number of signals in dumpfile '$dumpname' of type $dmt: $nfacs" 156 | # puts "\[dumpfile\] \"[file join [pwd] $dumpname]\"" 157 | # puts "\[dumpfile_size\] [file size [file join [pwd] $dumpname]]" 158 | # puts "\[optimize_vcd\]" 159 | # A .gtkw file has some additional meta information which we're not 160 | # using: 161 | # puts "\[savefile\]" 162 | # puts "\[timestart\] 0" 163 | 164 | # Keep the SST pane (left pane) collapsed as all signals are already 165 | # included 166 | puts "\[sst_expanded\] 0" 167 | 168 | # Get a list of all the signals in the design that are not "generated" 169 | # or "temporary". 170 | puts stderr "\[INFO\] Reading all signals in design" 171 | set signals [list] 172 | for {set i 0} {$i < $nfacs } {incr i} { 173 | set facname [ gtkwave::getFacName $i ] 174 | if {![regexp {^.*\._(GEN|T).*} $facname]} { 175 | lappend signals "$facname" 176 | } 177 | } 178 | puts stderr "\[INFO\] Found [llength $signals]" 179 | 180 | # Initialize a single node tree with the top module. Append each of 181 | # the signals to the tree. [TODO] Possibly a source of slowdown. 182 | puts stderr "\[INFO\] Construcing Trees" 183 | set trees [list] 184 | foreach signal $signals { 185 | lappend trees [constructTree $signal] 186 | } 187 | 188 | puts stderr "\[INFO\] Merging Trees" 189 | set tree [lindex [split [car $signals] .] 0] 190 | foreach signal $trees { 191 | set tree [merge_fast $tree $signal] 192 | } 193 | 194 | # Walk the tree emitting a .gtkw file describing the hierarchy 195 | puts stderr "\[INFO\] Emitting .gtkw" 196 | gtkwaveEmitModule $tree "" "" "" 197 | 198 | # We're done, so exit. 199 | gtkwave::/File/Quit 200 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright 2017 IBM 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | --------------------------------------------------------------------------------