About this manual
This manual is meant to be read in conjunction with the reference documentation for Ruby-VPI. In addition, if you are new to the Ruby language, you are encouraged to explore its documentation as necessary.
In this manual, you will notice that the numbers of chapters, sections, figures, admonitions, etc. are hyperlinks that take you back to the corresponding place in the table of contents. These links make it easy to navigate this manual, especially for users of text-only web browsers.
In addition, this manual is distributed as one big HTML file so that you can easily search for a particular topic using nothing more than your web browser’s built-in text search mechanism. This facilitates offline reading, where an Internet search engine is not available.
You can give feedback about this manual and, in general, any aspect of the Ruby-VPI project on the project forums. Furthermore, you can edit this manual and contribute your improvements to the project patches. Finally, you can find the newest version of this manual at the Ruby-VPI project website.
Legal notice
This manual is distributed under the same license as Ruby-VPI.
The admonition graphics used in this manual are Copyright 2005, 2006 Tango Desktop Project and are distributed under these terms.
Ruby-VPI is a platform for unit testing, rapid prototyping, and systems integration of Verilog modules through the Ruby programming language. It lets you:
Ruby-VPI is open source software released under this license.
Records
Documentation
Facilities
Portable
Agile
Powerful
The following software is necessary in order to use Ruby-VPI.
Verilog simulator
Ruby-VPI is known to work with the following simulators. However, you should be able to use it with any Verilog simulator that supports VPI.Compilers
Libraries
Here is a tiny sampling of code to whet your appetite. See the tutorial for more samples.
your_register.intVal = 2 ** 2048
your_module.all_net? { |net| net.z? }
puts your_register
your_memory.memoryWord_a[0..4]
your_memory.each_memoryWord {|w| w.intVal = 0}
Copyright 2006 Suraj N. Kurapati <snk@gna.org>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
1. All modified and unmodified copies and substantial portions of the Software (the "Derivatives") and their corresponding machine-readable source code (the "Code") must include the above copyright notice and this permission notice.
2. Upon distribution, the Derivatives must be accompanied either by the Code or, if the Code is obtainable for no more than the cost of distribution plus a nominal fee, by information on how to obtain the Code.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
See Section 2.3 above.
Tip 1. Add support for your Verilog simulator
Write a support request for your simulator, while providing a sample transcript of the commands you use to run a test with your simulator, and I will add support for your simulator in the next release!The following software may make your interactions with Ruby-VPI more pleasant.
Once you have satisfied the necessary requirements, you can install Ruby-VPI by running the
gem install -y ruby-vpicommand. RubyGems will install Ruby-VPI into the system gem directory, whose path can be determined by running the
gem env gemdircommand. Within this directory, there is a gems/ subdirectory which contains the Ruby-VPI installation, as illustrated below.
$ gem env gemdir /usr/lib/ruby/gems/1.8 $ ls -d /usr/lib/ruby/gems/1.8/gems/ruby-vpi* /usr/lib/ruby/gems/1.8/gems/ruby-vpi-7.0.0/
Note 1. Tuning for maximum performance
You can tune your installation of Ruby-VPI for maximum performance by adding your C compiler’s optimization flag to theCFLAGS
environment variable before you run the gem install -y ruby-vpicommand. For example, if your C compiler is GCC, then you can set
CFLAGS
to -O9 for maximum optimization.
After Ruby-VPI is compiled, it is linked to symbols whose names begin with _vpi. In GNU/Linux and similar operating systems, these symbols are allowed to be undefined. However, one cannot compile a shared object file with references to undefined symbols in Windows.
One solution to this problem is to supply the Verilog simulator’s VPI object file, which contains definitions of all VPI symbols, to the linker. The following steps illustrate this process.
for x in *.{o,so,dll}; do nm $x | grep -q '[Tt] _vpi' && echo $x; donecommand in Cygwin.
LDFLAGS
environment variable. For example, if the object file’s path is /foo/bar/vpi.so, then you would run the export LDFLAGS=/foo/bar/vpi.socommand in Cygwin.
gem install ruby-vpicommand in Cygwin.
gem update ruby-vpicommand.
gem uninstall ruby-vpicommand.
Learn more about using and manipulating RubyGems in the RubyGems user manual.
Ruby-VPI is a bridge between IEEE 1364-2005 Verilog VPI and the Ruby language. It enables Ruby programs to use VPI either (1) in the same, verbose way that C programs do, or (2) in a simpler, higher level way. In addition, it serves as a vehicle for the application of agile software development practices, such as TDD and BDD to the realm of hardware development with Verilog.
Ruby-VPI can be used with any Verilog simulator that supports VPI. In particular, it is known to operate with (1) Synopsys VCS and Mentor Modelsim, the two most prominent Verilog simulators in the Electronic Design Automation (EDA) industry; as well as (2) GPL Cver and Icarus Verilog, the two most prevalent open source Verilog simulators today.
Figure 1. Where does Ruby-VPI fit in?
In a typical VPI application written in C, the Verilog simulator is in charge. Verilog code temporarily transfers control to C by invoking C functions, which return control to Verilog when they finish.
In contrast, Ruby-VPI puts the specification in charge. The specification temporarily transfers control to the Verilog simulator by invoking the advance_time
method, which returns control to the specification when it finishes. This process is illustrated in Figure 2.
Ruby-VPI’s approach is the same as any software testing framework, where the specification drives the design under test. Whereas, the typical VPI & C approach is literally backwards because the design under test drives the specification.
Figure 2. Interaction between Ruby and Verilog
Vpi::advance_time
method with parameter Y, which specifies the number of simulation time steps to be simulated.In Ruby-VPI, the process of functional verification is neatly packaged into self-contained, executable tests. As Figure 3 illustrates, a test is composed of a bench, a design, and a specification.
Figure 3. Organization of a test in Ruby-VPI
The design is an instantiated Verilog module. To extend the analogy of the electronics laboratory, it corresponds to the electronic component that is verified by an engineer.
The specification is a Ruby program. In the electronics laboratory analogy, it corresponds to the engineer who inspects, manipulates, and verifies the electronic component. In terms of specification-driven functional verification, it corresponds to the executable specification.
Ruby-VPI makes the entire IEEE Std 1364-2005 VPI interface available to Ruby, but with the following minor differences.
The names of all VPI types, structures, and constants become capitalized because Ruby requires that the names of constants begin with a capital letter. However, note that Ruby’s capitalization rule does not apply to VPI functions.
For example, the s_vpi_value
structure becomes the S_vpi_value
class in Ruby. Likewise, the vpiIntVal
constant becomes the VpiIntVal
constant in Ruby. However, the vpi_handle
function remains as vpi_handle
in Ruby.
vprintf
is printf
vpi_vprintf
and vpi_mcd_vprintf
VPI functions are aliased to vpi_printf
and vpi_mcd_printf
respectively because:
va_list
, for them.va_list
type. For these compilers, the third line of source code shown below causes a “type mismatch” error.
#include <stdarg.h>
void foo(va_list ap) {
va_list *p = ≈
}
A handle is a reference to an object (such as a module, register, wire, and so on) inside the Verilog simulation. Handles allows you to inspect and manipulate the design under test and its internal components. They are instances of the Vpi::Handle
class (see reference documentation for details) in Ruby-VPI.
Handles have various properties, listed in the second column of Table 1, which provide different kinds of information about the underlying Verilog objects they represent. These properties are accessed through the VPI functions listed in the last column of Table 1.
Handles are typically obtained through the vpi_handle_by_name
and vpi_handle
functions. These functions are hierarchical in nature, as they allow you to obtain new handles that are related to existing ones. For example, to obtain a handle to a register contained within a module, one would typically write: your_reg = vpi_handle( VpiReg, your_handle )
Shortcuts for productivity
Given a handle, Ruby-VPI allows you to access (1) its relatives and (2) its properties simply by invoking methods on the handle. If a handle’s relative happens to have the same name as one its properties, then the relative is given priority because a handle’s properties can always be accessed explicitly through thehandle.get_value
and handle.put_value
methods.
Imagine that the design under test, say foo, instantiated a Verilog module named bar, which in turn contained a register named baz. To access baz from Ruby, one could employ VPI idioms by writing:
foo = vpi_handle_by_name( "foo", nil ) bar = vpi_handle_by_name( "bar", foo ) baz = vpi_handle_by_name( "baz", bar )
or by writing:
baz = vpi_handle_by_name( "foo.bar.bar", nil )
These idioms seem excessively verbose in a higher level language such as Ruby, so Ruby-VPI allows you to access a handle’s relative by simply invoking the relative’s name as a method on the handle:
foo.bar.baz
Imagine that the design under test, say foo, contained a register named bar. To access the integer value of bar in Ruby-VPI, one could employ VPI idioms by writing:
wrapper = S_vpi_value.new wrapper.format = VpiIntVal vpi_get_value( foo.bar, wrapper ) result = wrapper.value.integer
or, if bar is capable of storing more than 32 bits, one would convert a string representation of bar’s integer value into a limitless Ruby integer by writing:
wrapper = S_vpi_value.new wrapper.format = VpiHexStrVal vpi_get_value( foo.bar, wrapper ) result = wrapper.value.str.to_i( 16 )
These idioms seem excessively verbose in a higher level language such as Ruby, so Ruby-VPI allows you to access a handle’s properties by simply invoking property names, using the special naming format shown in Figure 4, as methods on the handle:
result = foo.bar.intVal
Figure 4. Method naming format for accessing a handle’s properties
Operation | _ | Property | _ | Accessor | Addendum |
---|---|---|---|---|---|
optional | required | optional |
Enumerable
module are valid operations.Table 1 shows a list of valid accessors and how they influence the means by which a property is accessed.
When it is a question mark (?), it suggests that the specified VPI property should be accessed as a boolean value. This suggestion is the same as specifying “b” as the Accessor.
Table 1. Possible accessors and their implications
Accessor | Kind of value accessed | VPI functions used to access the value |
---|---|---|
d | delay | vpi_get_delays , vpi_put_delays |
l | logic | vpi_get_value , vpi_put_value |
i | integer | vpi_get |
b | boolean | vpi_get |
s | string | vpi_get_str |
h | handle | vpi_handle |
a | array | vpi_iterate |
Table 2. Examples of accessing a handle’s properties
Ruby expression | Method naming format | Description | |||||
---|---|---|---|---|---|---|---|
Operation | _ | Property | _ | Accessor | Addendum | ||
handle.vpiIntVal |
vpiIntVal | Obtain the logic value of the handle’s VpiIntVal property. |
|||||
handle.vpiIntVal_l |
vpiIntVal | _ | l | ||||
handle.intVal |
intVal | ||||||
handle.intVal_l |
intVal | _ | l | ||||
handle.vpiIntVal = 15 |
vpiIntVal | = | Assign the integer 15 to the logic value of the handle’s VpiIntVal property. |
||||
handle.vpiIntVal_l = 15 |
vpiIntVal | _ | l | = | |||
handle.intVal = 15 |
intVal | = | |||||
handle.intVal_l = 15 |
intVal | _ | l | = | |||
handle.vpiType |
vpiType | Obtain the integer value of the handle’s VpiType property. |
|||||
handle.vpiType_i |
vpiType | _ | i | ||||
handle.type |
type | ||||||
handle.type_i |
type | _ | i | ||||
handle.vpiProtected |
vpiProtected | Obtain the boolean value of the handle’s VpiProtected property. |
|||||
handle.vpiProtected_b |
vpiProtected | _ | b | ||||
handle.vpiProtected? |
vpiProtected | ? | |||||
handle.protected |
protected | ||||||
handle.protected_b |
protected | _ | b | ||||
handle.protected? |
protected | ? | |||||
handle.vpiFullName |
vpiFullName | Obtain the string value of the handle’s VpiFullName property. |
|||||
handle.vpiFullName_s |
vpiFullName | _ | s | ||||
handle.fullName |
fullName | ||||||
handle.fullName_s |
fullName | _ | s | ||||
handle.vpiParent |
vpiParent | Obtain the handle value of the handle’s VpiParent property. |
|||||
handle.vpiParent_h |
vpiParent | _ | h | ||||
handle.parent |
parent | ||||||
handle.parent_h |
parent | _ | h | ||||
handle.each_vpiNet {|net| puts net.fullName} |
each | _ | vpiNet | Use the each operation to print the full name of each VpiNet object associated with the handle. |
|||
handle.each_net {|net| puts net.fullName} |
each | _ | net | ||||
handle.all_vpiReg? {|reg| reg.size == 1} |
all? | _ | vpiReg | Use the all? operation to check whether all VpiReg objects associated with the handle are capable of storing only one bit of information. |
|||
handle.all_reg? {|reg| reg.size == 1} |
all? | _ | reg | ||||
handle.select_vpiNet {|net| net.x?} |
select | _ | VpiNet | Use the select operation to obtain a list of VpiNet objects whose logic value is unknown (x). |
|||
handle.select_net {|net| net.x?} |
select | _ | net |
A callback is a mechanism that makes the Verilog simuluator execute a block of code, which is known as a “callback handler”, when some prescribed event occurs in the simulation.
Callbacks are added using the vpi_register_cb
function and removed using the vpi_remove_cb
function. However, instead of storing the address of a C function in the cb_rtn
field of the s_cb_data
structure (as you would do in C) you pass a block of code to the vpi_register_cb
method in Ruby. This block will be executed whenever the callback occurs.
Example 1. Using a callback for value change notification
This example shows how to use a callback for notification of changes in a handle’s VpiIntVal
property. When you no longer need this callback, you can tear it down using vpi_remove_cb(cb_handle)
.
In this example, the handle being monitored is the Counter.count
signal from Example 3.
cbTime = S_vpi_time.new cbTime.type = VpiSimTime cbTime.low = 0 cbTime.high = 0 cbValue = S_vpi_value.new cbValue.format = VpiIntVal cbData = S_cb_data.new cbData.reason = CbValueChange cbData.obj = Counter.count cbData.time = cbTime cbData.value = cbValue cbData.index = 0 cbHandle = vpi_register_cb(cbData) do |data| time = (data.time.high << 32) | data.time.low count = data.value.value.integer puts "hello from callback! time=#{time} count=#{count}" end
Append this code to the RSpec/counter_spec.rb file (provided in Section 5.5 and discussed in Section 5.6.3) and run the counter_RSpec test
Ruby-VPI enables you to rapidly prototype your designs in Ruby without having to do full-scale implementations in Verilog. This lets you explore and evaluate different design choices quickly.
The prototyping process is completely transparent: there is absolutely no difference, in the eyes of your executable specification, between a real Verilog design or its Ruby prototype.
In addition, the prototyping process is completely standard-based: Ruby prototypes emulate the behavior of real Verilog designs using nothing more than the VPI itself.
For example, compare the Verilog design shown in Example 11 with its Ruby prototype shown in figure Example 8. The prototype uses only VPI to (1) detect changes in its inputs and (2) manipulate its outputs accordingly. In addition, notice how well the prototype’s syntax reflects the intended behavior of the Verilog design. This similarity facilitates rapid translation of a prototype from Ruby into Verilog later in the design process.
Getting started
To create a prototype,Once you are satisfied with your prototype, you can proceed to implement your design in Verilog. This process is often a simple translation your Ruby prototype into your Verilog. At the very least, your prototype serves as a reference while you are implementing your Verilog design.
Once your design has been implemented in Verilog, you can use the same specification, which was originally used to verify your prototype, to verify your Verilog design (see Section 5.3 for details).
The advance_time
method normally transfers control from the executable specification to the Verilog simulator. However, when prototyping is enabled, Ruby-VPI redefines it so that the feign!
method (which is defined in a test’s proto.rb file) is invoked on the design under test. The feign!
method artificially simulates the behavior of the real Verilog design.
In this manner, control is kept within the Ruby interpreter when prototyping is enabled. An advantage of this approach is that it reduces the total execution time of a Ruby-VPI test by allowing Ruby’s POSIX thread to commandeer the Verilog simulator’s process. A disadvantage of this approach is that callbacks, which require the transfer of control to the Verilog simulator, must be ignored.
The ruby-debug project serves as the interactive debugger for Ruby-VPI.
DEBUGGER
environment variable (see Section 5.3 for details).debugger
command in your code—anywhere you wish to activate an interactive debugging session. These commands are automatically ignored when the debugger is disabled; so you can safely leave them in your code, if you wish.
Debugger.start
method. If you wish to perform more advanced initialization, such as having the debugger accept remote network connections for interfacing with a remote debugging session or perhaps with an IDE (see the ruby-debug documentation for details), then:
DEBUG
environment variable.A test runner is a file, generated by the automated test generator whose name ends with .rake. It helps you run generated tests—you can think of it as a makefile if you are familiar with C programming in a UNIX environment.
When you invoke a test runner without any arguments, it will show you a list of available tasks:
$ rake -f your_test_runner.rake (in /home/sun/tmp/ruby-vpi/doc) rake clean # Remove any temporary products. rake clobber # Remove any generated file. rake cver # Simulate with GPL Cver. rake default # Show a list of available tasks. rake ivl # Simulate with Icarus Verilog. rake ncsim # Simulate with Cadence NC-Sim. rake vcs # Simulate with Synopsys VCS. rake vsim # Simulate with Mentor Modelsim.
Test runners support the following environment variables, which allow you to easily change the behavior of the test runner.
COVERAGE
enables code coverage analysis and generation of code coverage reports.DEBUGGER
enables the interactive debugger in its post-mortem debugging mode.PROTOTYPE
enables the Ruby prototype for the design under test so that the prototype, rather than the real Verilog design, is verified by the specification.To activate these variables, simply assign the number 1 to them. For example, DEBUG=1
activates the DEBUG
variable.
To deactivate these variables, simply assign a different value to them or unset them in your shell. For example, both DEBUG=0
and DEBUG=
dectivate the DEBUG
variable.
Variables as command-line arguments
You can specify variable assignments as arguments to the rake command. For example,rake DEBUG=1is equivalent to
DEBUG=1 export DEBUG rakein Bourne shell or
setenv DEBUG 1 rakein C shell.
Example 2. Running a test with environment variables
Below, we enable the prototype and code coverage analysis:
rake -f your_test_runner.rake PROTOTYPE=1 COVERAGE=1
Below, we disable the prototype and enable the code coverage analysis. These invocations are equivalent if the PROTOTYPE
environment variable is unset.
rake -f your_test_runner.rake PROTOTYPE=0 COVERAGE=1
rake -f your_test_runner.rake PROTOTYPE= COVERAGE=1
rake -f your_test_runner.rake COVERAGE=1
The ruby-vpi command serves as a front-end to the tools provided by Ruby-VPI. You can see its help information (reproduced below) by simply running the command without any arguments.
This is a front-end for tools provided by Ruby-VPI. Usage: ruby-vpi Show this help message ruby-vpi TOOL --help Show help message for TOOL ruby-vpi TOOL arguments... Run TOOL with some arguments Tools: convert Converts Verilog source code into Ruby. generate Generates Ruby-VPI tests from Verilog 2001 and Verilog 95 module declarations. Simulators: cver GPL Cver ivl Icarus Verilog vcs Synopsys VCS vsim Mentor Modelsim ncsim Cadence NC-Sim
The generate tool generates scaffolding for Ruby-VPI tests from Verilog module declarations (written in either Verilog 2001 or Verilog 95 style).
A Ruby-VPI test is composed of the following files:rake ...instead of having to write
rake -f runner.rake ...every time.
As Example 4 shows, the name of each generated file is prefixed with the name of the Verilog module for which the test was generated. This convention helps organize tests within the file system, so that they are readily distinguishable from one another.
Caution 1. Do not rename generated files
Ruby-VPI uses the convention described above to dynamically create a direct Ruby interface to the design under test, so do not rename the generated files arbitrarily.You can try this tool by running the
ruby-vpi generate --helpcommand.
Tip 2. Using kdiff3 with the automated test generator.
#!/bin/sh # args: old file, new file kdiff3 --auto --output "$2" "$@"
chmod +x merge2command.
PATH
environment variable.MERGER
environment variable using your shell’s export or setenv command.From now on, kdiff3 will be invoked to help you transfer your changes between generated files. When you are finished transferring changes, simply issue the “save the file” command and quit kdiff3. Or, if you do not want to transfer any changes, simply quit kdiff3 without saving the file.
The convert tool can be used to convert Verilog header files into Ruby. You can try it by running the
ruby-vpi convert --helpcommand.
By converting Verilog header files into Ruby, your test can utilize the same `define
constants that are used in the Verilog design.
rakecommand to get started.
First, we need a Verilog design to test. In this tutorial, Example 3 will serve as our design under test. Its interface is composed of the following parts:
Size
defines the number of bits used to represent the counter’s value.clock
causes the count
register to increment whenever it reaches a positive edge.reset
causes the count
register to become zero when asserted.count
is a register that contains the counter’s value.
Example 3. Declaration of a simple up-counter with synchronous reset
module counter #(parameter Size = 5) (
input clock,
input reset,
output reg [Size-1 : 0] count
);
endmodule
Now that we have a Verilog design to test, we shall use the generate tool to generate some scaffolding for our test. This tool allows us to implement our specification using RSpec, xUnit, or any other format.
Each format represents a different software development methodology:In this tutorial, you will see how both RSpec and xUnit formats are used. So let us make separate directories for both formats to avoid generated tests from overwriting each other:
$ mkdir RSpec xUnit $ cp counter.v RSpec $ cp counter.v xUnit
Once we have decided how we want to implement our specification, we can proceed to generate a test for our design. This process is illustrated by Example 4 and Example 5.
Example 4. Generating a test with specification in RSpec format
$ ruby-vpi generate counter.v --RSpec module counter create counter_runner.rake create counter_design.rb create counter_proto.rb create counter_spec.rb create Rakefile
Example 5. Generating a test with specification in xUnit format
$ ruby-vpi generate counter.v --xUnit module counter create counter_runner.rake create counter_design.rb create counter_proto.rb create counter_spec.rb create Rakefile
So far, the test generation tool has created a basic foundation for our test Now we must build upon this foundation by identifying our expectation of the design under test. That is, how do we expect the design to behave under certain conditions?
Here are some reasonable expectations for our simple counter:Now that we have identified a set of expectations for our design, we are ready to implement them in our specification. This process is illustrated by Example 6 and Example 7.
Example 6. Specification implemented in RSpec format
require 'spec' # lowest upper bound of counter's value LIMIT = 2 ** Counter::Size # maximum allowed value for a counter MAX = LIMIT - 1 describe "A resetted counter's value" do setup do Counter.reset! end it "should be zero" do Counter.count.intVal.should == 0 end it "should increment upon each rising clock edge" do LIMIT.times do |i| Counter.count.intVal.should == i Counter.cycle! # increment the counter end end end describe "A counter with the maximum value" do setup do Counter.reset! # increment the counter to maximum value MAX.times { Counter.cycle! } Counter.count.intVal.should == MAX end it "should overflow upon increment" do Counter.cycle! # increment the counter Counter.count.intVal.should == 0 end end
Example 7. Specification implemented in xUnit format
require 'test/unit' # lowest upper bound of counter's value LIMIT = 2 ** Counter::Size # maximum allowed value for a counter MAX = LIMIT - 1 class ResettedCounterValue < Test::Unit::TestCase def setup Counter.reset! end def test_zero assert_equal( 0, Counter.count.intVal ) end def test_increment LIMIT.times do |i| assert_equal( i, Counter.count.intVal ) Counter.cycle! # increment the counter end end end class MaximumCounterValue < Test::Unit::TestCase def setup Counter.reset! # increment the counter to maximum value MAX.times { Counter.cycle! } assert_equal( MAX, Counter.count.intVal ) end def test_overflow Counter.cycle! # increment the counter assert_equal( 0, Counter.count.intVal ) end end
# Simulates the design under test for one clock cycle. def cycle! clock.high! advance_time clock.low! advance_time end # Brings the design under test into a blank state. def reset! reset.high! cycle! reset.low! end
Now that we have a specification against which to verify our design let us build a prototype of our design. By doing so, we exercise our specification, experience potential problems that may arise when we later implement our design in Verilog, and gain confidence in our work. The result of this proceess is illustrated by Example 8.
Example 8. Ruby prototype of our Verilog design
# Ruby prototype of the design under test's Verilog implementation. def feign! if clock.posedge? if reset.high? count.intVal = 0 else count.intVal += 1 end end end
Now that we have implemented our prototype, we are ready to verify it against our specification by running the test This process is illustrated by Example 9 and Example 10.
In these examples, the PROTOTYPE
environment variable is assigned the value 1 while running the test so that, instead of our design, our prototype is verified against our specification (see Section 5.3.1 for details). Also, the GPL Cver simulator denoted by cver, is used to run the simulation.
Example 9. Running a test with specification in RSpec format
$ cd RSpec $ rake cver PROTOTYPE=1 Ruby-VPI: prototype is enabled ... Finished in 0.05106 seconds 3 examples, 0 failures cd -
Example 10. Running a test with specification in xUnit format
$ cd xUnit $ rake cver PROTOTYPE=1 Ruby-VPI: prototype is enabled Loaded suite counter Started ... Finished in 0.043859 seconds. 3 tests, 35 assertions, 0 failures, 0 errors
Tip 3. What can the test runner do?
If you invoke the test runner (1) without any arguments or (2) with the --tasks option, it will show you a list of tasks that it can perform for you.
Now that we have implemented and verified our prototype, we are ready to implement our design This is often quite simple because we translate existing code from Ruby (our prototype) into Verilog (our design). The result of this process is illustrated by Example 11.
Example 11. Implementation of a simple up-counter with synchronous reset
/**
A simple up-counter with synchronous reset.
@param Size Number of bits used to represent the counter's value.
@param clock Increments the counter's value upon each positive edge.
@param reset Zeroes the counter's value when asserted.
@param count The counter's value.
*/
module counter #(parameter Size = 5) (
input clock,
input reset,
output reg [Size-1 : 0] count
);
always @(posedge clock) begin
if (reset)
count <= 0;
else
count <= count + 1;
end
endmodule
Now that we have implemented our design we are ready to verify it against our specification by running the test Example 12 and Example 13 illustrate this process.
In these examples, the PROTOTYPE
environment variable is not specified while running the test, so that our design, instead of our prototype, is verified against our specification. You can also achieve this effect by assigning an empty value to PROTOTYPE
, or by using your shell’s unset command. Finally, the GPL Cver simulator denoted by cver, is used to run the simulation.
Example 12. Running a test with specification in RSpec format
$ cd RSpec $ rake cver ... Finished in 0.041198 seconds 3 examples, 0 failures
Example 13. Running a test with specification in xUnit format
$ cd xUnit $ rake cver Loaded suite counter Started ... Finished in 0.040262 seconds. 3 tests, 35 assertions, 0 failures, 0 errors
darcs get http://ruby-vpi.rubyforge.org/src/ruby-vpi
In addition to the normal requirements you need the following software to build release packages:
Once you have satisfied these requirements, you can run
rake releaseto build the release packages. Also, see the output of
rake -Tfor more build options.
rakecommand to automatically generate the HTML documentation you are currently viewing.
This chapter presents known problems and possible solutions.
In version 0.8 and snapshot 20061009 of Icarus Verilog, the vpi_handle_by_name
function requires an absolute path (including the name of the bench which instantiates the design) to a Verilog object. In addition, vpi_handle_by_name
always returns nil
when its second parameter is specified.
For example, consider Example 14. Here, one must write vpi_handle_by_name("TestFoo.my_foo.clk", nil)
instead of vpi_handle_by_name("my_foo.clk", TestFoo)
in order to access the clk
input of the my_foo
module instance.
Example 14. Part of a bench which instantiates a Verilog design
module TestFoo;
reg clk_reg;
Foo my_foo(.clk(clk_reg));
endmodule
In version 0.8 of Icarus Verilog, if you want to access a register in a design, then it must be connected to something (either assigned to a wire or passed as a parameter to a module instantiation). Otherwise, you will get a nil
value as the result of vpi_handle_by_name
method.
For example, suppose you wanted to access the clk_reg
register, from the bench shown in Example 15 If you execute the statement clk_reg = vpi_handle_by_name("TestFoo.clk_reg", nil)
in a specification, then you will discover that the vpi_handle_by_name
method returns nil
instead of a handle to the clk_reg
register.
The solution is to change the design such that it appears like the one shown in Example 16 where the register is connected to a wire, or Example 14 where the register is connected to a module instantiation.
Example 15. Bad design with unconnected registers
Here theclk_reg
register is not connected to anything.
module TestFoo;
reg clk_reg;
endmodule
Example 16. Fixed design with wired registers
Here theclk_reg
register is connected to the clk_wire
wire.
module TestFoo;
reg clk_reg;
wire clk_wire;
assign clk_wire = clk_reg;
endmodule
vpi_control(vpiReset)
VPI function causes an assertion to fail inside the simulator. As a result, the simulation terminates and a core dump is produced.
Rake is a build tool, written in Ruby, using Ruby as a build language. Rake is similar to make in scope and purpose.
An agile software development methodology which emphasizes (1) testing functionality before implementing it and (2) refactoring.
See this introductory article for more information.
An agile software development methodology which emphasizes thinking in terms of behavior when designing, implementing, and verifying software.
See the official wiki for more information.
vprintf
is printf