About HDLRuby
HDLRuby is a library for describing and simulating digital electronic systems.
Note:
If you are new to HDLRuby, it is recommended that you consult first the following tutorial (even if you are a hardware person):
And if you want an html version the following command with create a
tuto
folder containing all the required files, then just
open tuto/tutorial_sw.html
:
hdrcc --get-tuto
What’s new
For HDLRuby version 3.3.0:
Remade the description of software components using the program construct. Now the Code objects are deprecated.
Added HW-SW co-simulation capability for Ruby and compiled C-compatible software programs.
Added a browser-based graphical interface simulating simulates a development board that interacts with the HDLRuby simulator.
Updated the documentation and tutorial accordingly and fixed several typos.
For HDLRuby version 3.2.0:
Added component for declaring BRAM and BRAM-based stacks. The goal is to allocate memories inside FPGAs efficiently.
Inner code overhaul for preparing version 4.0.0
Multiple bug fixes
For HDLRuby version 3.1.0:
Functions for sequencers supporting recursion;
The
function
keyword was replaced withhdef
for better consistency with the new functions for sequencers;The
steps
command was added for waiting several steps in a sequencer;Verilog HDL code generation was improved to preserve as much as possible the original names of the signals;
Several bug fixes for the sequencers.
For HDLRuby version 3.0.0:
This section;
The sequencers for software-like hardware design;
A tutorial for software people;
The stable standard libraries are loaded by default.
Install:
The recommended installation method is from rubygems as follows:
gem install HDLRuby
Developers willing to contribute to HDLRuby can install the sources from GitHub as follows:
git clone https://github.com/civol/HDLRuby.git
Warning:
- This is still preliminary work which may change before we release a stable version.
- It is highly recommended to have both basic knowledge of the Ruby language and hardware description languages before using HDLRuby.
Compiling HDLRuby descriptions
Using the HDLRuby compiler
‘hdrcc’ is the HDLRuby compiler. It takes as input an HDLRuby file, checks it, and can produce as output a Verilog HDL, VHDL, or a YAML low-level description of HW components but it can also simulate the input description.
Usage:
hdrcc [options] <input file> <output directory>
Where:
options
is a list of options<input file>
is the initial file to compile (mandatory)<output directory>
is the directory where the generated files will be put
Options | |
---|---|
-I, --interactive |
Run in interactive mode |
-y, --yaml |
Output in YAML format |
-v, --verilog |
Output in Verilog HDL format |
-V, --vhdl |
Output in VHDL format |
-s, --syntax |
Output the Ruby syntax tree |
-C, --clang |
Output the C code of the standalone simulator |
-S, --sim |
Perform the simulation with the default engine |
--csim |
Perform the simulation with the standalone engine |
--rsim |
Perform the simulation with the Ruby engine |
--rcsim |
Perform the simulation with the Hybrid engine |
--vcd |
Make the simulator generate a VCD file |
-d, --directory |
Specify the base directory for loading the HDLRuby files |
-D, --debug |
Set the HDLRuby debug mode |
-t, --top system |
Specify the top system describing the circuit to compile |
-p, --param x,y,z |
Specify the generic parameters |
--get-samples |
Copy the sample directory (hdr_samples) to current one, then exit |
--version |
Show the version number, then exit |
-h, --help |
Show the help message |
Notes:
If no top system is given, it is automatically looked for from the input file.
If no option is given, it simply checks the input file.
If you are new to HDLRuby, or if you want to see how new features work, we strongly encourage you to get a local copy of the test HDLRuby sample using:
<span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a><span class="ex">hdrcc</span> <span class="at">--get-samples</span></span>
Then in your current directory (folder) the
hdr_samples
subdirectory will appear that contains several HDLRuby example files. Details about the samples can be found here: samples.
Examples:
- Compile system named
adder
fromadder.rb
input file and generate a low-level YAML description into directoryadder
:
<span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a><span class="ex">hdrcc</span> <span class="at">--yaml</span> <span class="at">--top</span> adder adder.rb adder</span>
- Compile
adder.rb
input file and generate a low-level Verilog HDL description into the directoryadder
:
<span id="cb7-1"><a href="#cb7-1" aria-hidden="true" tabindex="-1"></a><span class="ex">hdrcc</span> <span class="at">-v</span> adder.rb adder</span>
- Compile system
adder
whose bit width is generic fromadder_gen.rb
input file to a 16-bit circuit low-level VHDL description into directoryadder
:
<span id="cb8-1"><a href="#cb8-1" aria-hidden="true" tabindex="-1"></a><span class="ex">hdrcc</span> <span class="at">-V</span> <span class="at">-t</span> adder <span class="at">--param</span> 16 adder_gen.rb adder</span>
- Compile system
multer
with inputs and output bit width is generic frommulter_gen.rb
input file to a 16x16->32-bit circuit whose low-level YAML description into directorymulter
:
<span id="cb9-1"><a href="#cb9-1" aria-hidden="true" tabindex="-1"></a><span class="ex">hdrcc</span> <span class="at">-y</span> <span class="at">-t</span> multer <span class="at">-p</span> 16,16,32 multer_gen.rb multer</span>
- Simulate the circuit described in file
counter_bench.rb
using the default simulation engine and putting the simulator’s files in the directorycounter
:
<span id="cb10-1"><a href="#cb10-1" aria-hidden="true" tabindex="-1"></a><span class="ex">hdrcc</span> <span class="at">-S</span> counter_bench.rb counter</span>
As a policy, the default simulation engine is set to the fastest one (currently it is the hybrid engine).
- Run in interactive mode.
<span id="cb11-1"><a href="#cb11-1" aria-hidden="true" tabindex="-1"></a><span class="ex">hdrcc</span> <span class="at">-I</span></span>
- Run in interactive mode using pry as UI.
<span id="cb12-1"><a href="#cb12-1" aria-hidden="true" tabindex="-1"></a><span class="ex">hdrcc</span> <span class="at">-I</span> pry</span>
Using HDLRuby in interactive mode
When running in interactive mode, the HDLRuby framework starts a REPL
prompt and creates a working directory called
HDLRubyWorkspace
. By default, the REPL is irb
,
but it can be set to pry
. Within this prompt, HDLRuby code
can be written like in an HDLRuby description file. However, to process
this code the following commands are added:
- Compile an HDLRuby module:
<span id="cb13-1"><a href="#cb13-1" aria-hidden="true" tabindex="-1"></a>hdr_make(<span class="kw"><</span><span class="cf">module</span><span class="kw">></span>)</span>
- Generate and display the IR of the compiled module in YAML form:
<span id="cb14-1"><a href="#cb14-1" aria-hidden="true" tabindex="-1"></a>hdr_yaml</span>
- Regenerate and display the HDLRuby description of the compiled module:
<span id="cb15-1"><a href="#cb15-1" aria-hidden="true" tabindex="-1"></a>hdr_hdr</span>
- Generate and output in the working directory the Verilog HDL RTL of the compiled module:
<span id="cb16-1"><a href="#cb16-1" aria-hidden="true" tabindex="-1"></a>hdr_verilog</span>
- Generate and output in the working directory the Verilog HDL RTL of the compiled module:
<span id="cb17-1"><a href="#cb17-1" aria-hidden="true" tabindex="-1"></a>hdr_vhdl</span>
- Simulate the compiled module:
<span id="cb18-1"><a href="#cb18-1" aria-hidden="true" tabindex="-1"></a>hdr_sim</span>
HDLRuby programming guide
HDLRuby has been designed to bring the high flexibility of the Ruby language to hardware descriptions while ensuring that they remain synthesizable. In this context, all the abstractions provided by HDLRuby are in the way of describing hardware, but not in its execution model, this latter being RTL by construction.
The second specificity of HDLRuby is that it supports natively all the features of the Ruby language.
Notes:
- It is still possible to extend HDLRuby to support hardware descriptions of a higher level than RTL, please refer to section Extending HDLRuby for more details.
- In this document, HDLRuby constructs will often be compared to their Verilog HDL or VHDL equivalents for simpler explanations.
Introduction
This introduction gives a glimpse of the possibilities of the language. However, we do recommend consulting the section about the high-level programming features to have a more complete view of the advanced possibilities of this language.
At first glance, HDLRuby appears like any other HDL (like Verilog HDL or VHDL), for instance, the following code describes a simple D-FF:
<span id="cb19-1"><a href="#cb19-1" aria-hidden="true" tabindex="-1"></a><span class="fu">system</span> <span class="wa">:dff</span> <span class="cf">do</span></span>
<span id="cb19-2"><a href="#cb19-2" aria-hidden="true" tabindex="-1"></a> bit<span class="at">.input</span> <span class="wa">:clk</span>, <span class="wa">:rst</span>, <span class="wa">:d</span></span>
<span id="cb19-3"><a href="#cb19-3" aria-hidden="true" tabindex="-1"></a> bit<span class="at">.output</span> <span class="wa">:q</span></span>
<span id="cb19-4"><a href="#cb19-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb19-5"><a href="#cb19-5" aria-hidden="true" tabindex="-1"></a> par(clk<span class="at">.posedge</span>) <span class="cf">do</span></span>
<span id="cb19-6"><a href="#cb19-6" aria-hidden="true" tabindex="-1"></a> q <span class="kw"><=</span> d <span class="kw">&</span> <span class="kw">~</span>rst</span>
<span id="cb19-7"><a href="#cb19-7" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span>
<span id="cb19-8"><a href="#cb19-8" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
As can be seen in the code above, system
is the keyword
used for describing a digital circuit. This keyword is the equivalent of
the Verilog HDL module
. In such a system, signals are
declared using a <type>.<direction>
construct
where type
is the data type of the signal (e.g.,
bit
as in the code above) and direction
indicates if the signal is an input, an output, an inout or an inner
one; and executable blocks (similar to always
block of
Verilog HDL) are described using the par
keyword when they
are parallel and seq
when they are sequential (i.e., with
respectively non-blocking and blocking assignments).
After such a system has been defined, it can be instantiated. For
example, a single instance of the dff
system named
dff0
can be declared as follows:
<span id="cb20-1"><a href="#cb20-1" aria-hidden="true" tabindex="-1"></a>dff <span class="wa">:dff0</span></span>
The ports of this instance can then be accessed to be used like any
other signals, e.g., dff0.d
for accessing the
d
input of the FF.
Several instances can also be declared in a single statement. For
example, a 2-bit counter based on the previous dff
circuits
can be described as follows:
<span id="cb21-1"><a href="#cb21-1" aria-hidden="true" tabindex="-1"></a><span class="fu">system</span> <span class="wa">:counter2</span> <span class="cf">do</span></span>
<span id="cb21-2"><a href="#cb21-2" aria-hidden="true" tabindex="-1"></a> input <span class="wa">:clk</span>,<span class="wa">:rst</span></span>
<span id="cb21-3"><a href="#cb21-3" aria-hidden="true" tabindex="-1"></a> output <span class="wa">:q</span></span>
<span id="cb21-4"><a href="#cb21-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb21-5"><a href="#cb21-5" aria-hidden="true" tabindex="-1"></a> dff <span class="kw">[</span> <span class="wa">:dff0</span>, <span class="wa">:dff1</span> <span class="kw">]</span></span>
<span id="cb21-6"><a href="#cb21-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb21-7"><a href="#cb21-7" aria-hidden="true" tabindex="-1"></a> dff0<span class="at">.clk</span> <span class="kw"><=</span> clk</span>
<span id="cb21-8"><a href="#cb21-8" aria-hidden="true" tabindex="-1"></a> dff0<span class="at">.rst</span> <span class="kw"><=</span> rst</span>
<span id="cb21-9"><a href="#cb21-9" aria-hidden="true" tabindex="-1"></a> dff0<span class="at">.d</span> <span class="kw"><=</span> <span class="kw">~</span>dff0<span class="at">.q</span></span>
<span id="cb21-10"><a href="#cb21-10" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb21-11"><a href="#cb21-11" aria-hidden="true" tabindex="-1"></a> dff1<span class="at">.clk</span> <span class="kw"><=</span> <span class="kw">~</span>dff0<span class="at">.q</span> </span>
<span id="cb21-12"><a href="#cb21-12" aria-hidden="true" tabindex="-1"></a> dff1<span class="at">.rst</span> <span class="kw"><=</span> rst</span>
<span id="cb21-13"><a href="#cb21-13" aria-hidden="true" tabindex="-1"></a> dff1<span class="at">.d</span> <span class="kw"><=</span> <span class="kw">~</span>dff1<span class="at">.q</span></span>
<span id="cb21-14"><a href="#cb21-14" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb21-15"><a href="#cb21-15" aria-hidden="true" tabindex="-1"></a> q <span class="kw"><=</span> dff1<span class="at">.q</span></span>
<span id="cb21-16"><a href="#cb21-16" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
The instances can also be connected while being declared. For example, the code above can be rewritten as follows:
<span id="cb22-1"><a href="#cb22-1" aria-hidden="true" tabindex="-1"></a><span class="fu">system</span> <span class="wa">:counter2</span> <span class="cf">do</span></span>
<span id="cb22-2"><a href="#cb22-2" aria-hidden="true" tabindex="-1"></a> input <span class="wa">:clk</span>, <span class="wa">:rst</span></span>
<span id="cb22-3"><a href="#cb22-3" aria-hidden="true" tabindex="-1"></a> output <span class="wa">:q</span></span>
<span id="cb22-4"><a href="#cb22-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb22-5"><a href="#cb22-5" aria-hidden="true" tabindex="-1"></a> dff(<span class="wa">:dff0</span>)<span class="kw">.</span>(<span class="wa">clk: </span>clk, <span class="wa">rst: </span>rst, <span class="wa">d: </span><span class="kw">~</span>dff0<span class="at">.q</span>)</span>
<span id="cb22-6"><a href="#cb22-6" aria-hidden="true" tabindex="-1"></a> dff(<span class="wa">:dff1</span>)<span class="kw">.</span>(<span class="kw">~</span>dff0<span class="at">.q</span>, rst, <span class="kw">~</span>dff1<span class="at">.q</span>, q)</span>
<span id="cb22-7"><a href="#cb22-7" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
In the code above, two possible connection methods are shown: for
dff0
ports are connected by name, and for dff1
ports are connected in declaration order. Please notice that it is also
possible to connect only a subset of the ports while declaring, as well
as to reconnect already connected ports in further statements.
While a circuit can be generated from the code given above, a
benchmark must be provided to test it. Such a benchmark is described by
constructs called timed behavior that give the evolution of signals
depending on the time. For example, the following code simulates the
previous D-FF for 4 cycles of 20ns each, with a reset on the first
cycle, set of signal d
to 1 for the third cycle and set
this signal to 0 for the last.
<span id="cb23-1"><a href="#cb23-1" aria-hidden="true" tabindex="-1"></a><span class="fu">system</span> <span class="wa">:dff_bench</span> <span class="cf">do</span></span>
<span id="cb23-2"><a href="#cb23-2" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb23-3"><a href="#cb23-3" aria-hidden="true" tabindex="-1"></a> dff <span class="wa">:dff0</span></span>
<span id="cb23-4"><a href="#cb23-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb23-5"><a href="#cb23-5" aria-hidden="true" tabindex="-1"></a> timed <span class="cf">do</span></span>
<span id="cb23-6"><a href="#cb23-6" aria-hidden="true" tabindex="-1"></a> dff0<span class="at">.clk</span> <span class="kw"><=</span> <span class="dv">0</span></span>
<span id="cb23-7"><a href="#cb23-7" aria-hidden="true" tabindex="-1"></a> dff0<span class="at">.rst</span> <span class="kw"><=</span> <span class="dv">1</span></span>
<span id="cb23-8"><a href="#cb23-8" aria-hidden="true" tabindex="-1"></a> !<span class="dv">10</span><span class="at">.ns</span></span>
<span id="cb23-9"><a href="#cb23-9" aria-hidden="true" tabindex="-1"></a> dff0<span class="at">.clk</span> <span class="kw"><=</span> <span class="dv">1</span></span>
<span id="cb23-10"><a href="#cb23-10" aria-hidden="true" tabindex="-1"></a> !<span class="dv">10</span><span class="at">.ns</span></span>
<span id="cb23-11"><a href="#cb23-11" aria-hidden="true" tabindex="-1"></a> dff0<span class="at">.clk</span> <span class="kw"><=</span> <span class="dv">0</span></span>
<span id="cb23-12"><a href="#cb23-12" aria-hidden="true" tabindex="-1"></a> dff0<span class="at">.rst</span> <span class="kw"><=</span> <span class="dv">0</span></span>
<span id="cb23-13"><a href="#cb23-13" aria-hidden="true" tabindex="-1"></a> dff0<span class="at">.d</span> <span class="kw"><=</span> <span class="dv">1</span></span>
<span id="cb23-14"><a href="#cb23-14" aria-hidden="true" tabindex="-1"></a> !<span class="dv">10</span><span class="at">.ns</span></span>
<span id="cb23-15"><a href="#cb23-15" aria-hidden="true" tabindex="-1"></a> dff0<span class="at">.clk</span> <span class="kw"><=</span> <span class="dv">1</span></span>
<span id="cb23-16"><a href="#cb23-16" aria-hidden="true" tabindex="-1"></a> !<span class="dv">10</span><span class="at">.ns</span></span>
<span id="cb23-17"><a href="#cb23-17" aria-hidden="true" tabindex="-1"></a> dff0<span class="at">.clk</span> <span class="kw"><=</span> <span class="dv">0</span></span>
<span id="cb23-18"><a href="#cb23-18" aria-hidden="true" tabindex="-1"></a> !<span class="dv">10</span><span class="at">.ns</span></span>
<span id="cb23-19"><a href="#cb23-19" aria-hidden="true" tabindex="-1"></a> dff0<span class="at">.clk</span> <span class="kw"><=</span> <span class="dv">1</span></span>
<span id="cb23-20"><a href="#cb23-20" aria-hidden="true" tabindex="-1"></a> !<span class="dv">10</span><span class="at">.ns</span></span>
<span id="cb23-21"><a href="#cb23-21" aria-hidden="true" tabindex="-1"></a> dff0<span class="at">.clk</span> <span class="kw"><=</span> <span class="dv">0</span></span>
<span id="cb23-22"><a href="#cb23-22" aria-hidden="true" tabindex="-1"></a> dff0<span class="at">.d</span> <span class="kw"><=</span> <span class="dv">1</span></span>
<span id="cb23-23"><a href="#cb23-23" aria-hidden="true" tabindex="-1"></a> !<span class="dv">10</span><span class="at">.ns</span></span>
<span id="cb23-24"><a href="#cb23-24" aria-hidden="true" tabindex="-1"></a> dff0<span class="at">.clk</span> <span class="kw"><=</span> <span class="dv">1</span></span>
<span id="cb23-25"><a href="#cb23-25" aria-hidden="true" tabindex="-1"></a> !<span class="dv">10</span><span class="at">.ns</span></span>
<span id="cb23-26"><a href="#cb23-26" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span>
<span id="cb23-27"><a href="#cb23-27" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
The code describing a dff
given above is not much
different from its equivalent in any other HDL. However, HDLRuby
provides several features for achieving higher productivity when
describing hardware. We will now describe a few of them.
First, several syntactic sugars exist that allow shorter code, for
instance, the following code is strictly equivalent to the previous
description of dff
:
<span id="cb24-1"><a href="#cb24-1" aria-hidden="true" tabindex="-1"></a><span class="fu">system</span> <span class="wa">:dff</span> <span class="cf">do</span></span>
<span id="cb24-2"><a href="#cb24-2" aria-hidden="true" tabindex="-1"></a> input <span class="wa">:clk</span>, <span class="wa">:rst</span>, <span class="wa">:d</span></span>
<span id="cb24-3"><a href="#cb24-3" aria-hidden="true" tabindex="-1"></a> output <span class="wa">:q</span></span>
<span id="cb24-4"><a href="#cb24-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb24-5"><a href="#cb24-5" aria-hidden="true" tabindex="-1"></a> (q <span class="kw"><=</span> d <span class="kw">&</span> <span class="kw">~</span>rst)<span class="at">.at</span>(clk<span class="at">.posedge</span>)</span>
<span id="cb24-6"><a href="#cb24-6" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
Then, it often happens that a system will end up with only one instance. In such a case, the system declaration can be omitted, and an instance can be directly declared as follows:
<span id="cb25-1"><a href="#cb25-1" aria-hidden="true" tabindex="-1"></a>instance <span class="wa">:dff_single</span> <span class="cf">do</span></span>
<span id="cb25-2"><a href="#cb25-2" aria-hidden="true" tabindex="-1"></a> input <span class="wa">:clk</span>, <span class="wa">:rst</span>, <span class="wa">:d</span></span>
<span id="cb25-3"><a href="#cb25-3" aria-hidden="true" tabindex="-1"></a> output <span class="wa">:q</span></span>
<span id="cb25-4"><a href="#cb25-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb25-5"><a href="#cb25-5" aria-hidden="true" tabindex="-1"></a> (q <span class="kw"><=</span> d <span class="kw">&</span> <span class="kw">~</span>rst)<span class="at">.at</span>(clk<span class="at">.posedge</span>)</span>
<span id="cb25-6"><a href="#cb25-6" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
In the example above, dff_single
is an instance
describing, again, a D-FF, but whose system is anonymous.
Furthermore, generic parameters can be used for anything in HDLRuby. For instance, the following code describes an 8-bit register without any parameterization:
<span id="cb26-1"><a href="#cb26-1" aria-hidden="true" tabindex="-1"></a><span class="fu">system</span> <span class="wa">:reg8</span> <span class="cf">do</span></span>
<span id="cb26-2"><a href="#cb26-2" aria-hidden="true" tabindex="-1"></a> input <span class="wa">:clk</span>, <span class="wa">:rst</span></span>
<span id="cb26-3"><a href="#cb26-3" aria-hidden="true" tabindex="-1"></a> <span class="kw">[</span><span class="dv">7</span><span class="kw">..</span><span class="dv">0</span><span class="kw">]</span><span class="at">.input</span> <span class="wa">:d</span></span>
<span id="cb26-4"><a href="#cb26-4" aria-hidden="true" tabindex="-1"></a> <span class="kw">[</span><span class="dv">7</span><span class="kw">..</span><span class="dv">0</span><span class="kw">]</span><span class="at">.output</span> <span class="wa">:q</span></span>
<span id="cb26-5"><a href="#cb26-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb26-6"><a href="#cb26-6" aria-hidden="true" tabindex="-1"></a> (q <span class="kw"><=</span> d <span class="kw">&</span> <span class="kw">[~</span>rst<span class="kw">]*</span><span class="dv">8</span>)<span class="at">.at</span>(clk<span class="at">.posedge</span>)</span>
<span id="cb26-7"><a href="#cb26-7" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
But it is also possible to describe a register of arbitrary size as
follows, where n
is the parameter giving the number of bits
of the register:
<span id="cb27-1"><a href="#cb27-1" aria-hidden="true" tabindex="-1"></a><span class="fu">system</span> <span class="wa">:regn</span> <span class="cf">do</span> <span class="kw">|</span>n<span class="kw">|</span></span>
<span id="cb27-2"><a href="#cb27-2" aria-hidden="true" tabindex="-1"></a> input <span class="wa">:clk</span>, <span class="wa">:rst</span></span>
<span id="cb27-3"><a href="#cb27-3" aria-hidden="true" tabindex="-1"></a> <span class="kw">[</span>n<span class="dv">-1</span><span class="kw">..</span><span class="dv">0</span><span class="kw">]</span><span class="at">.input</span> <span class="wa">:d</span></span>
<span id="cb27-4"><a href="#cb27-4" aria-hidden="true" tabindex="-1"></a> <span class="kw">[</span>n<span class="dv">-1</span><span class="kw">..</span><span class="dv">0</span><span class="kw">]</span><span class="at">.output</span> <span class="wa">:q</span></span>
<span id="cb27-5"><a href="#cb27-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb27-6"><a href="#cb27-6" aria-hidden="true" tabindex="-1"></a> (q <span class="kw"><=</span> d <span class="kw">&</span> <span class="kw">[~</span>rst<span class="kw">]*</span>n)<span class="at">.at</span>(clk<span class="at">.posedge</span>)</span>
<span id="cb27-7"><a href="#cb27-7" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
Or, even further, it is possible to describe a register of arbitrary type (not only bit vectors) as follows:
<span id="cb28-1"><a href="#cb28-1" aria-hidden="true" tabindex="-1"></a><span class="fu">system</span> <span class="wa">:reg</span> <span class="cf">do</span> <span class="kw">|</span>typ<span class="kw">|</span></span>
<span id="cb28-2"><a href="#cb28-2" aria-hidden="true" tabindex="-1"></a> input <span class="wa">:clk</span>, <span class="wa">:rst</span></span>
<span id="cb28-3"><a href="#cb28-3" aria-hidden="true" tabindex="-1"></a> typ<span class="at">.input</span> <span class="wa">:d</span></span>
<span id="cb28-4"><a href="#cb28-4" aria-hidden="true" tabindex="-1"></a> typ<span class="at">.output</span> <span class="wa">:q</span></span>
<span id="cb28-5"><a href="#cb28-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb28-6"><a href="#cb28-6" aria-hidden="true" tabindex="-1"></a> (q <span class="kw"><=</span> d <span class="kw">&</span> <span class="kw">[~</span>rst<span class="kw">]*</span>typ<span class="at">.width</span>)<span class="at">.at</span>(clk<span class="at">.posedge</span>)</span>
<span id="cb28-7"><a href="#cb28-7" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
Wait… I have just realized that D-FF without any inverted output does
not look very serious. So, let us extend the existing dff
to provide an inverted output. There are three ways to do this. First,
inheritance can be used: a new system is built inheriting from
dff
as it is done in the following code.
<span id="cb29-1"><a href="#cb29-1" aria-hidden="true" tabindex="-1"></a><span class="fu">system</span> <span class="wa">:dff_full</span>, dff <span class="cf">do</span></span>
<span id="cb29-2"><a href="#cb29-2" aria-hidden="true" tabindex="-1"></a> output <span class="wa">:qb</span></span>
<span id="cb29-3"><a href="#cb29-3" aria-hidden="true" tabindex="-1"></a> qb <span class="kw"><=</span> <span class="kw">~</span>q</span>
<span id="cb29-4"><a href="#cb29-4" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
The second possibility is to modify dff
afterward. In
HDLRuby, this is achieved using the open
method as it is
done in the following code:
<span id="cb30-1"><a href="#cb30-1" aria-hidden="true" tabindex="-1"></a>dff<span class="at">.open</span> <span class="cf">do</span></span>
<span id="cb30-2"><a href="#cb30-2" aria-hidden="true" tabindex="-1"></a> output <span class="wa">:qb</span></span>
<span id="cb30-3"><a href="#cb30-3" aria-hidden="true" tabindex="-1"></a> qb <span class="kw"><=</span> <span class="kw">~</span>q</span>
<span id="cb30-4"><a href="#cb30-4" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
The third possibility is to modify directly a single instance of
dff
which requires an inverted output, using again the
open
method, as in the following code:
<span id="cb31-1"><a href="#cb31-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Declare dff0 as an instance of dff</span></span>
<span id="cb31-2"><a href="#cb31-2" aria-hidden="true" tabindex="-1"></a>dff <span class="wa">:dff0</span></span>
<span id="cb31-3"><a href="#cb31-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb31-4"><a href="#cb31-4" aria-hidden="true" tabindex="-1"></a><span class="co"># Modify it</span></span>
<span id="cb31-5"><a href="#cb31-5" aria-hidden="true" tabindex="-1"></a>dff0<span class="at">.open</span> <span class="cf">do</span></span>
<span id="cb31-6"><a href="#cb31-6" aria-hidden="true" tabindex="-1"></a> output <span class="wa">:qb</span></span>
<span id="cb31-7"><a href="#cb31-7" aria-hidden="true" tabindex="-1"></a> qb <span class="kw"><=</span> <span class="kw">~</span>q</span>
<span id="cb31-8"><a href="#cb31-8" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
In this latter case, only dff0
will have an inverted
output, the other instances of dff
will not change.
Now assuming we opted for the first solution, we have now
dff_full
, a highly advanced D-FF with such unique features
as an inverted output. So, we would like to use it in other designs, for
example, a shift register of n
bits. Such a system will
include a generic number of dff_full
instances and can be
described as follows making use of the native Ruby method
each_cons
for connecting them:
<span id="cb32-1"><a href="#cb32-1" aria-hidden="true" tabindex="-1"></a><span class="fu">system</span> <span class="wa">:shifter</span> <span class="cf">do</span> <span class="kw">|</span>n<span class="kw">|</span></span>
<span id="cb32-2"><a href="#cb32-2" aria-hidden="true" tabindex="-1"></a> input <span class="wa">:clk</span>, <span class="wa">:rst</span></span>
<span id="cb32-3"><a href="#cb32-3" aria-hidden="true" tabindex="-1"></a> input <span class="wa">:i0</span></span>
<span id="cb32-4"><a href="#cb32-4" aria-hidden="true" tabindex="-1"></a> output <span class="wa">:o0</span>, <span class="wa">:o0b</span></span>
<span id="cb32-5"><a href="#cb32-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb32-6"><a href="#cb32-6" aria-hidden="true" tabindex="-1"></a> <span class="co"># Instantiating n D-FF</span></span>
<span id="cb32-7"><a href="#cb32-7" aria-hidden="true" tabindex="-1"></a> <span class="kw">[</span>n<span class="kw">]</span><span class="at">.dff_full</span> <span class="wa">:dffIs</span></span>
<span id="cb32-8"><a href="#cb32-8" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb32-9"><a href="#cb32-9" aria-hidden="true" tabindex="-1"></a> <span class="co"># Connect the clock and the reset.</span></span>
<span id="cb32-10"><a href="#cb32-10" aria-hidden="true" tabindex="-1"></a> dffIs<span class="at">.each</span> <span class="kw">{</span> <span class="kw">|</span>ff<span class="kw">|</span> ff<span class="at">.clk</span> <span class="kw"><=</span> clk ; ff<span class="at">.rst</span> <span class="kw"><=</span> rst <span class="kw">}</span></span>
<span id="cb32-11"><a href="#cb32-11" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb32-12"><a href="#cb32-12" aria-hidden="true" tabindex="-1"></a> <span class="co"># Interconnect them as a shift register</span></span>
<span id="cb32-13"><a href="#cb32-13" aria-hidden="true" tabindex="-1"></a> dffIs<span class="kw">[</span><span class="dv">0</span><span class="kw">..-</span><span class="dv">1</span><span class="kw">]</span><span class="at">.each_cons</span>(<span class="dv">2</span>) <span class="kw">{</span> <span class="kw">|</span>ff0,ff1<span class="kw">|</span> ff1<span class="at">.d</span> <span class="kw"><=</span> ff0<span class="at">.q</span> <span class="kw">}</span></span>
<span id="cb32-14"><a href="#cb32-14" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb32-15"><a href="#cb32-15" aria-hidden="true" tabindex="-1"></a> <span class="co"># Connects the input and output of the circuit</span></span>
<span id="cb32-16"><a href="#cb32-16" aria-hidden="true" tabindex="-1"></a> dffIs<span class="kw">[</span><span class="dv">0</span><span class="kw">]</span><span class="at">.d</span> <span class="kw"><=</span> i0</span>
<span id="cb32-17"><a href="#cb32-17" aria-hidden="true" tabindex="-1"></a> o0 <span class="kw"><=</span> dffIs<span class="kw">[-</span><span class="dv">1</span><span class="kw">]</span><span class="at">.q</span></span>
<span id="cb32-18"><a href="#cb32-18" aria-hidden="true" tabindex="-1"></a> o0b <span class="kw"><=</span> dffIs<span class="kw">[-</span><span class="dv">1</span><span class="kw">]</span><span class="at">.qb</span></span>
<span id="cb32-19"><a href="#cb32-19" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
As can be seen in the above examples, in HDLRuby, any construct is an
object and therefore includes methods. For instance, declaring a signal
of a given type
and direction (input, output, or inout) is
done as follows so that direction
is a method of the type,
and the signal names are the arguments of this method (symbols or string
are supported.)
<span id="cb33-1"><a href="#cb33-1" aria-hidden="true" tabindex="-1"></a><span class="kw"><</span>type<span class="kw">>.<</span>direction<span class="kw">></span> <span class="kw"><</span>list of symbols representing the signal<span class="kw">></span></span>
Of course, if you do not need to use the specific component
dff_full
you can describe a shift register more simply as
follows:
<span id="cb34-1"><a href="#cb34-1" aria-hidden="true" tabindex="-1"></a><span class="fu">system</span> <span class="wa">:shifter</span> <span class="cf">do</span> <span class="kw">|</span>n<span class="kw">|</span></span>
<span id="cb34-2"><a href="#cb34-2" aria-hidden="true" tabindex="-1"></a> input <span class="wa">:clk</span>, <span class="wa">:rst</span></span>
<span id="cb34-3"><a href="#cb34-3" aria-hidden="true" tabindex="-1"></a> input <span class="wa">:i0</span></span>
<span id="cb34-4"><a href="#cb34-4" aria-hidden="true" tabindex="-1"></a> output <span class="wa">:o0</span></span>
<span id="cb34-5"><a href="#cb34-5" aria-hidden="true" tabindex="-1"></a> <span class="kw">[</span>n<span class="kw">]</span><span class="at">.inner</span> <span class="wa">:sh</span></span>
<span id="cb34-6"><a href="#cb34-6" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb34-7"><a href="#cb34-7" aria-hidden="true" tabindex="-1"></a> par (clk<span class="at">.posedge</span>) <span class="cf">do</span></span>
<span id="cb34-8"><a href="#cb34-8" aria-hidden="true" tabindex="-1"></a> hif(rst) <span class="kw">{</span> sh <span class="kw"><=</span> <span class="dv">0</span> <span class="kw">}</span></span>
<span id="cb34-9"><a href="#cb34-9" aria-hidden="true" tabindex="-1"></a> helse <span class="kw">{</span> sh <span class="kw"><=</span> <span class="kw">[</span>sh<span class="kw">[</span>n<span class="dv">-2</span><span class="kw">..</span><span class="dv">0</span><span class="kw">]</span>, i0<span class="kw">]</span> <span class="kw">}</span></span>
<span id="cb34-10"><a href="#cb34-10" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span>
<span id="cb34-11"><a href="#cb34-11" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb34-12"><a href="#cb34-12" aria-hidden="true" tabindex="-1"></a> o0 <span class="kw"><=</span> sh<span class="kw">[</span>n<span class="dv">-1</span><span class="kw">]</span></span>
<span id="cb34-13"><a href="#cb34-13" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
Now, let us assume you want to design a circuit that performs a sum of products of several inputs with constant coefficients. For the case of 4 16-bit signed inputs and given coefficients as 3, 4, 5, and 6. The corresponding basic code could be as follows:
<span id="cb35-1"><a href="#cb35-1" aria-hidden="true" tabindex="-1"></a><span class="fu">system</span> <span class="wa">:sumprod_16_3456</span> <span class="cf">do</span></span>
<span id="cb35-2"><a href="#cb35-2" aria-hidden="true" tabindex="-1"></a> signed<span class="kw">[</span><span class="dv">16</span><span class="kw">]</span><span class="at">.input</span> <span class="wa">:i0</span>, <span class="wa">:i1</span>, <span class="wa">:i2</span>, <span class="wa">:i3</span></span>
<span id="cb35-3"><a href="#cb35-3" aria-hidden="true" tabindex="-1"></a> signed<span class="kw">[</span><span class="dv">16</span><span class="kw">]</span><span class="at">.output</span> <span class="wa">:o</span></span>
<span id="cb35-4"><a href="#cb35-4" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb35-5"><a href="#cb35-5" aria-hidden="true" tabindex="-1"></a> o <span class="kw"><=</span> i0<span class="kw">*</span><span class="dv">3</span> <span class="kw">+</span> i1<span class="kw">*</span><span class="dv">4</span> <span class="kw">+</span> i2<span class="kw">*</span><span class="dv">5</span> <span class="kw">+</span> i3<span class="kw">*</span><span class="dv">6</span></span>
<span id="cb35-6"><a href="#cb35-6" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
The description above is straightforward, but it would be necessary to rewrite it if another circuit with different bit widths or coefficients is to be designed. Moreover, if the number of coefficients is large an error in the expression will be easy to make and hard to find. A better approach would be to use a generic description of such a circuit as follows:
<span id="cb36-1"><a href="#cb36-1" aria-hidden="true" tabindex="-1"></a><span class="fu">system</span> <span class="wa">:sumprod</span> <span class="cf">do</span> <span class="kw">|</span>typ,coefs<span class="kw">|</span></span>
<span id="cb36-2"><a href="#cb36-2" aria-hidden="true" tabindex="-1"></a> typ<span class="kw">[</span>coefs<span class="at">.size</span><span class="kw">]</span><span class="at">.input</span> <span class="wa">:ins</span></span>
<span id="cb36-3"><a href="#cb36-3" aria-hidden="true" tabindex="-1"></a> typ<span class="at">.output</span> <span class="wa">:o</span></span>
<span id="cb36-4"><a href="#cb36-4" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb36-5"><a href="#cb36-5" aria-hidden="true" tabindex="-1"></a> o <span class="kw"><=</span> coefs<span class="at">.each_with_index.reduce</span>(_b0) <span class="cf">do</span> <span class="kw">|</span>sum,(coef,i)<span class="kw">|</span></span>
<span id="cb36-6"><a href="#cb36-6" aria-hidden="true" tabindex="-1"></a> sum <span class="kw">+</span> ins<span class="kw">[</span>i<span class="kw">]*</span>coef</span>
<span id="cb36-7"><a href="#cb36-7" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span>
<span id="cb36-8"><a href="#cb36-8" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
In the code above, there are two generic parameters,
typ
, which indicates the data type of the circuit, and
coefs
, which is assumed to be an array of coefficients.
Since the number of inputs depends on the number of provided
coefficients, it is declared as an array of width
bit
signed whose size is equal to the number of coefficients.
The description of the sum of products may be more difficult to
understand for people not familiar with the Ruby language. The
each_with_index
method iterates over the coefficients
adding their index as an iteration variable, the resulting operation
(i.e., the iteration loop) is then modified by the reduce
method that accumulates the code passed as arguments. This code,
starting by |sum,coef,i|
performs the addition of the
current accumulation result (sum
) with the product of the
current coefficient (coef
) and input (ins[i]
,
where i
is the index) in the iteration. The argument
_b0
initializes the sum to 0
.
While slightly longer than the previous description, this description allows declaring a circuit implementing a sum of products with any bit width and any number of coefficients. For instance, the following code describes a signed 32-bit sum of products with 16 coefficients (just random numbers here).
<span id="cb37-1"><a href="#cb37-1" aria-hidden="true" tabindex="-1"></a>sumprod(signed<span class="kw">[</span><span class="dv">32</span><span class="kw">]</span>, <span class="kw">[</span><span class="dv">3</span>,<span class="dv">78</span>,<span class="dv">43</span>,<span class="dv">246</span>, <span class="dv">3</span>,<span class="dv">67</span>,<span class="dv">1</span>,<span class="dv">8</span>, <span class="dv">47</span>,<span class="dv">82</span>,<span class="dv">99</span>,<span class="dv">13</span>, <span class="dv">5</span>,<span class="dv">77</span>,<span class="dv">2</span>,<span class="dv">4</span><span class="kw">]</span>)<span class="kw">.</span>(<span class="wa">:my_circuit</span>) </span>
As seen in the code above, when passing a generic argument for instantiating a generic system, the name of the instance is put between brackets to avoid confusion.
While the description sumprod
is already usable in a
wide range of cases, it still uses standard addition and multiplication.
However, there are cases where specific components are to be used for
these operations, either for the sake of performance, compliance with
constraints, or because functionally different operations are required
(e.g., saturated computations). This can be solved by using functions
implementing such computation in place of operators, for example, as
follows:
<span id="cb38-1"><a href="#cb38-1" aria-hidden="true" tabindex="-1"></a><span class="fu">system</span> <span class="wa">:sumprod_func</span> <span class="cf">do</span> <span class="kw">|</span>typ,coefs<span class="kw">|</span></span>
<span id="cb38-2"><a href="#cb38-2" aria-hidden="true" tabindex="-1"></a> typ<span class="kw">[</span>coefs<span class="at">.size</span><span class="kw">]</span><span class="at">.input</span> ins</span>
<span id="cb38-3"><a href="#cb38-3" aria-hidden="true" tabindex="-1"></a> typ<span class="at">.output</span> <span class="wa">:o</span></span>
<span id="cb38-4"><a href="#cb38-4" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb38-5"><a href="#cb38-5" aria-hidden="true" tabindex="-1"></a> o <span class="kw"><=</span> coefs<span class="at">.each_with_index.reduce</span>(_b0) <span class="cf">do</span> </span>
<span id="cb38-6"><a href="#cb38-6" aria-hidden="true" tabindex="-1"></a> <span class="kw">|</span>sum,(coef,i)<span class="kw">|</span></span>
<span id="cb38-7"><a href="#cb38-7" aria-hidden="true" tabindex="-1"></a> add(sum, mult(ins<span class="kw">[</span>i<span class="kw">]*</span>coef))</span>
<span id="cb38-8"><a href="#cb38-8" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span>
<span id="cb38-9"><a href="#cb38-9" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
Where add
and mult
are functions
implementing the required specific operations. HDLRuby functions are
equivalent to the Verilog HDL ones. In our example, an addition that
saturates at 1000 could be described as follows:
<span id="cb39-1"><a href="#cb39-1" aria-hidden="true" tabindex="-1"></a>hdef <span class="wa">:add</span> <span class="cf">do</span> <span class="kw">|</span>x,y<span class="kw">|</span></span>
<span id="cb39-2"><a href="#cb39-2" aria-hidden="true" tabindex="-1"></a> inner <span class="wa">:res</span></span>
<span id="cb39-3"><a href="#cb39-3" aria-hidden="true" tabindex="-1"></a> seq <span class="cf">do</span></span>
<span id="cb39-4"><a href="#cb39-4" aria-hidden="true" tabindex="-1"></a> res <span class="kw"><=</span> x <span class="kw">+</span> y</span>
<span id="cb39-5"><a href="#cb39-5" aria-hidden="true" tabindex="-1"></a> (res <span class="kw"><=</span> <span class="dv">1000</span>)<span class="at">.hif</span>(res <span class="kw">></span> <span class="dv">1000</span>)</span>
<span id="cb39-6"><a href="#cb39-6" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span>
<span id="cb39-7"><a href="#cb39-7" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
With HDLRuby functions, the result of the last statement in the return value, in this case, that will be the value of res. The code above is also an example of the usage of the postfixed if statement, it is an equivalent of the following code:
<span id="cb40-1"><a href="#cb40-1" aria-hidden="true" tabindex="-1"></a> hif(res<span class="kw">></span><span class="dv">1000</span>) <span class="kw">{</span> res <span class="kw"><=</span> <span class="dv">1000</span> <span class="kw">}</span></span>
With functions, it is enough to change their content to obtain a new kind of circuit without changing the main code. This approach suffers from two drawbacks though: first, the level of saturation is hard coded in the function, and second, it would be preferable to be able to select the function to execute instead of modifying its code. For the first problem, a simple approach is to add an argument to the function given the saturation level. Such an add function would therefore be as follows:
<span id="cb41-1"><a href="#cb41-1" aria-hidden="true" tabindex="-1"></a>hdef <span class="wa">:add</span> <span class="cf">do</span> <span class="kw">|</span>max, x, y<span class="kw">|</span></span>
<span id="cb41-2"><a href="#cb41-2" aria-hidden="true" tabindex="-1"></a> inner <span class="wa">:res</span></span>
<span id="cb41-3"><a href="#cb41-3" aria-hidden="true" tabindex="-1"></a> seq <span class="cf">do</span></span>
<span id="cb41-4"><a href="#cb41-4" aria-hidden="true" tabindex="-1"></a> res <span class="kw"><=</span> x <span class="kw">+</span> y</span>
<span id="cb41-5"><a href="#cb41-5" aria-hidden="true" tabindex="-1"></a> (res <span class="kw"><=</span> max)<span class="at">.hif</span>(res <span class="kw">></span> max)</span>
<span id="cb41-6"><a href="#cb41-6" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span>
<span id="cb41-7"><a href="#cb41-7" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
It would however be necessary to add this argument when invoking the
function, e.g., add(1000,sum,mult(...))
. While this
argument is relevant for addition with saturation, it is not for the
other kinds of addition operations, and hence, the code of
sumprod
has no general purpose.
HDLRuby provides two ways to address such issues. First, it is
possible to pass code as an argument. In the case of
sumprod
, it would then be enough to add two arguments that
perform the required addition and multiplication. The example is
below:
<span id="cb42-1"><a href="#cb42-1" aria-hidden="true" tabindex="-1"></a><span class="fu">system</span> <span class="wa">:sumprod_proc</span> <span class="cf">do</span> <span class="kw">|</span>add,mult,typ,coefs<span class="kw">|</span></span>
<span id="cb42-2"><a href="#cb42-2" aria-hidden="true" tabindex="-1"></a> typ<span class="kw">[</span>coefs<span class="at">.size</span><span class="kw">]</span><span class="at">.input</span> ins</span>
<span id="cb42-3"><a href="#cb42-3" aria-hidden="true" tabindex="-1"></a> typ<span class="at">.output</span> <span class="wa">:o</span></span>
<span id="cb42-4"><a href="#cb42-4" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb42-5"><a href="#cb42-5" aria-hidden="true" tabindex="-1"></a> o <span class="kw"><=</span> coefs<span class="at">.each_with_index.reduce</span>(_b0) <span class="cf">do</span> </span>
<span id="cb42-6"><a href="#cb42-6" aria-hidden="true" tabindex="-1"></a> <span class="kw">|</span>sum,(coef,i)<span class="kw">|</span></span>
<span id="cb42-7"><a href="#cb42-7" aria-hidden="true" tabindex="-1"></a> add<span class="kw">.</span>(sum, mult<span class="kw">.</span>(ins<span class="kw">[</span>i<span class="kw">]*</span>coef))</span>
<span id="cb42-8"><a href="#cb42-8" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span>
<span id="cb42-9"><a href="#cb42-9" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
Note:
- With HDLRuby, when some code is passed as an argument, it is invoked
using the
.()
operator, and not simple parenthesis-like functions.
Assuming the addition with saturation is now implemented by a
function named add_sat
and a multiplication with saturation
is implemented by a function named mult_sat
(with similar
arguments), a circuit implementing a signed 16-bit sum of product
saturating at 1000 with 16 coefficients could be described as
follows:
<span id="cb43-1"><a href="#cb43-1" aria-hidden="true" tabindex="-1"></a>sumprod_proc( </span>
<span id="cb43-2"><a href="#cb43-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">proc</span> <span class="kw">{</span> <span class="kw">|</span>x,y<span class="kw">|</span> add_sat(<span class="dv">1000</span>,x,y) <span class="kw">}</span>,</span>
<span id="cb43-3"><a href="#cb43-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">proc</span> <span class="kw">{</span> <span class="kw">|</span>x,y<span class="kw">|</span> mult_sat(<span class="dv">1000</span>,x,y) <span class="kw">}</span>,</span>
<span id="cb43-4"><a href="#cb43-4" aria-hidden="true" tabindex="-1"></a> signed<span class="kw">[</span><span class="dv">64</span><span class="kw">]</span>, </span>
<span id="cb43-5"><a href="#cb43-5" aria-hidden="true" tabindex="-1"></a> <span class="kw">[</span><span class="dv">3</span>,<span class="dv">78</span>,<span class="dv">43</span>,<span class="dv">246</span>, <span class="dv">3</span>,<span class="dv">67</span>,<span class="dv">1</span>,<span class="dv">8</span>,</span>
<span id="cb43-6"><a href="#cb43-6" aria-hidden="true" tabindex="-1"></a> <span class="dv">47</span>,<span class="dv">82</span>,<span class="dv">99</span>,<span class="dv">13</span>, <span class="dv">5</span>,<span class="dv">77</span>,<span class="dv">2</span>,<span class="dv">4</span><span class="kw">]</span>)<span class="kw">.</span>(<span class="wa">:my_circuit</span>)</span>
As seen in the example above, a piece of code is passed as an argument using the proc keyword.
A second possible approach provided by HDLRuby is to declare a new data type with redefined addition and multiplication operators. For the case of a 16-bit saturated addition and multiplication the following generic data type can be defined (for signed computations):
signed[16].typedef(:sat16_1000)
sat16_1000.define_operator(:+) do |x,y|
tmp = x + y
mux(tmp > 1000,tmp,1000)
end
end
In the code above, the first line defines the new type
sat16_1000
to be 16-bit signed, and the remaining overloads
(redefines) the +
operator for this type (the same should
be done for the *
operator). Then, the initial version of
sumprod
can be used with this type to achieve saturated
computations as follows:
<span id="cb45-1"><a href="#cb45-1" aria-hidden="true" tabindex="-1"></a>sumprod(sat16_1000, </span>
<span id="cb45-2"><a href="#cb45-2" aria-hidden="true" tabindex="-1"></a> <span class="kw">[</span><span class="dv">3</span>,<span class="dv">78</span>,<span class="dv">43</span>,<span class="dv">246</span>, <span class="dv">3</span>,<span class="dv">67</span>,<span class="dv">1</span>,<span class="dv">8</span>,</span>
<span id="cb45-3"><a href="#cb45-3" aria-hidden="true" tabindex="-1"></a> <span class="dv">47</span>,<span class="dv">82</span>,<span class="dv">99</span>,<span class="dv">13</span>, <span class="dv">5</span>,<span class="dv">77</span>,<span class="dv">2</span>,<span class="dv">4</span><span class="kw">]</span>)<span class="kw">.</span>(<span class="wa">:my_circuit</span>)</span>
It is also possible to declare a generic type. For instance, a generic signed type with saturation can be declared as follows:
<span id="cb46-1"><a href="#cb46-1" aria-hidden="true" tabindex="-1"></a>typedef <span class="wa">:sat</span> <span class="cf">do</span> <span class="kw">|</span>width, max<span class="kw">|</span></span>
<span id="cb46-2"><a href="#cb46-2" aria-hidden="true" tabindex="-1"></a> signed<span class="kw">[</span>width<span class="kw">]</span></span>
<span id="cb46-3"><a href="#cb46-3" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
<span id="cb46-4"><a href="#cb46-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb46-5"><a href="#cb46-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb46-6"><a href="#cb46-6" aria-hidden="true" tabindex="-1"></a>sat<span class="at">.define_operator</span>(:<span class="kw">+</span>) <span class="cf">do</span> <span class="kw">|</span>width,max, x,y<span class="kw">|</span></span>
<span id="cb46-7"><a href="#cb46-7" aria-hidden="true" tabindex="-1"></a> tmp <span class="kw">=</span> x <span class="kw">+</span> y</span>
<span id="cb46-8"><a href="#cb46-8" aria-hidden="true" tabindex="-1"></a> mux(tmp <span class="kw">></span> max, tmp, max)</span>
<span id="cb46-9"><a href="#cb46-9" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span>
<span id="cb46-10"><a href="#cb46-10" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
Note:
- The generic parameters have also to be declared for the operator redefinitions.
With this generic type, the circuit can be declared as follows:
<span id="cb47-1"><a href="#cb47-1" aria-hidden="true" tabindex="-1"></a>sumprod(sat(<span class="dv">16</span>,<span class="dv">1000</span>), </span>
<span id="cb47-2"><a href="#cb47-2" aria-hidden="true" tabindex="-1"></a> <span class="kw">[</span><span class="dv">3</span>,<span class="dv">78</span>,<span class="dv">43</span>,<span class="dv">246</span>, <span class="dv">3</span>,<span class="dv">67</span>,<span class="dv">1</span>,<span class="dv">8</span>,</span>
<span id="cb47-3"><a href="#cb47-3" aria-hidden="true" tabindex="-1"></a> <span class="dv">47</span>,<span class="dv">82</span>,<span class="dv">99</span>,<span class="dv">13</span>, <span class="dv">5</span>,<span class="dv">77</span>,<span class="dv">2</span>,<span class="dv">4</span><span class="kw">]</span>)<span class="kw">.</span>(<span class="wa">:my_circuit</span>)</span>
Lastly note, HDLRuby is also a language with supports reflection for
all its constructs. For example, the system of an instance can be
accessed using the systemT
method, and this latter can be
used to create other instances. For example, previously,
dff_single
was declared with an anonymous system (i.e., it
cannot be accessed by name). This system can however be used as follows
to generate another instance:
<span id="cb48-1"><a href="#cb48-1" aria-hidden="true" tabindex="-1"></a>dff_single<span class="at">.systemT.instantiate</span>(<span class="wa">:dff_not_single</span>)</span>
In the above example, dff_not_single
is declared to be
an instance of the same system as dff_single
.
This reflection capability can also be used for instance, for
accessing the data type of a signal (sig.type
), but also
the current basic block (cur_block
), the current process
(cur_behavior
), and so on. The standard library of HDLRuby
includes several hardware constructs like finite state machine
descriptors and is mainly based on using these reflection features.
How does HDLRuby work
Contrary to descriptions in high-level HDL like SystemVerilog, VHDL, or SystemC, HDLRuby descriptions are not software-like descriptions of hardware but are programs meant to produce hardware descriptions. In other words, while the execution of a common HDL code will result in some simulation of the described hardware, the execution of HDLRuby code will result in some low-level hardware description. This low-level description is synthesizable and can also be simulated like any standard hardware description. This decoupling of the representation of the hardware from the point of view of the user (HDLRuby), and the actual hardware description (HDLRuby::Low) makes it possible to provide the user with any advanced software features without jeopardizing the synthesizability of the actual hardware description.
For that purpose, each construct in HDLRuby is not a direct
description of some hardware construct, but a program that generates the
corresponding description. For example, let us consider the following
line of code of HDLRuby describing the connection between signal
a
and signal b
:
<span id="cb49-1"><a href="#cb49-1" aria-hidden="true" tabindex="-1"></a> a <span class="kw"><=</span> b</span>
Its execution will produce the actual hardware description of this
connection as an object of the HDLRuby::Low library — in this case, an
instance of the HDLRuby::Low::Connection
class. Concretely,
an HDLRuby system is described by a Ruby block, and the instantiation of
this system is performed by executing this block. The actual
synthesizable description of this hardware is the execution result of
this instantiation.
From there, we will describe in more detail each construct of HDLRuby.
Naming rules
Several constructs in HDLRuby are referred to by name, e.g., systems
and signals. When such constructs are declared, their names are to be
specified by Ruby symbols starting with a lowercase. For example,
:hello
is a valid name declaration, but :Hello
is not.
After being declared, the construct can be referred to by using the
name directly (i.e., without the :
of Ruby symbols). For
example, if a construct has been declared with :hello
as
the name, it will be afterward referred to by hello
.
Systems and Signals
A system represents a digital system and corresponds to a Verilog HDL module. A system has an interface comprising input, output, and inout signals, and includes structural and behavioral descriptions.
A signal represents a state in a system. It has a data type and a value, the latter varying with time. HDLRuby signals can be viewed as abstractions of both wires and registers in a digital circuit. As a general rule, a signal whose value is explicitly set all the time models a wire, otherwise it models a register.
Declaring an empty system
A system is declared using the keyword system
. It must
be given a Ruby symbol for its name and a block that describes its
content. For instance, the following code describes an empty system
named box
:
<span id="cb50-1"><a href="#cb50-1" aria-hidden="true" tabindex="-1"></a><span class="fu">system</span>(<span class="wa">:box</span>) <span class="kw">{}</span></span>
Notes:
- Since this is Ruby code, the body can also be delimited by the
do
andend
Ruby keywords (in which case the parentheses can be omitted) as follows:
<span id="cb51-1"><a href="#cb51-1" aria-hidden="true" tabindex="-1"></a><span class="fu">system</span> <span class="wa">:box</span> does</span>
<span id="cb51-2"><a href="#cb51-2" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
- Names in HDLRuby are natively stored as Ruby symbols, but strings
can also be used, e.g.,
system("box") {}
is also valid.
Declaring a system with an interface
The interface of a system can be described anywhere in its body, but it is recommended to do it at its beginning. This is done by declaring input, output, or inout signals of given data types as follows:
<span id="cb52-1"><a href="#cb52-1" aria-hidden="true" tabindex="-1"></a><span class="kw"><</span>data type<span class="kw">>.<</span>direction<span class="kw">></span> <span class="kw"><</span>list of colon<span class="kw">-</span>preceded names<span class="kw">></span></span>
For example, declaring a 1-bit input signal named clk
can be declared as follows:
<span id="cb53-1"><a href="#cb53-1" aria-hidden="true" tabindex="-1"></a>bit<span class="at">.input</span> <span class="wa">:clk</span></span>
Now, since bit
is the default data type in HDLRuby, it
can be omitted as follows:
<span id="cb54-1"><a href="#cb54-1" aria-hidden="true" tabindex="-1"></a>input <span class="wa">:clk</span></span>
The following is a more complete example: it is the code of a system
describing an 8-bit data, 16-bit address memory whose interface includes
a 1-bit input clock (clk
), a 1-bit signal for selecting
reading or writing access (rwb
), a 16-bit address input
(addr
), and an 8-bit data inout — the remaining of the code
describes the content and the behavior of the memory.
<span id="cb55-1"><a href="#cb55-1" aria-hidden="true" tabindex="-1"></a><span class="fu">system</span> <span class="wa">:mem8_16</span> <span class="cf">do</span></span>
<span id="cb55-2"><a href="#cb55-2" aria-hidden="true" tabindex="-1"></a> input <span class="wa">:clk</span>, <span class="wa">:rwb</span></span>
<span id="cb55-3"><a href="#cb55-3" aria-hidden="true" tabindex="-1"></a> <span class="kw">[</span><span class="dv">15</span><span class="kw">..</span><span class="dv">0</span><span class="kw">]</span><span class="at">.input</span> <span class="wa">:addr</span></span>
<span id="cb55-4"><a href="#cb55-4" aria-hidden="true" tabindex="-1"></a> <span class="kw">[</span><span class="dv">7</span><span class="kw">..</span><span class="dv">0</span><span class="kw">]</span><span class="at">.inout</span> <span class="wa">:data</span></span>
<span id="cb55-5"><a href="#cb55-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb55-6"><a href="#cb55-6" aria-hidden="true" tabindex="-1"></a> bit<span class="kw">[</span><span class="dv">7</span><span class="kw">..</span><span class="dv">0</span><span class="kw">][</span><span class="dv">2</span><span class="kw">**</span><span class="dv">16</span><span class="kw">]</span><span class="at">.inner</span> <span class="wa">:content</span></span>
<span id="cb55-7"><a href="#cb55-7" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb55-8"><a href="#cb55-8" aria-hidden="true" tabindex="-1"></a> par(clk<span class="at">.posedge</span>) <span class="cf">do</span></span>
<span id="cb55-9"><a href="#cb55-9" aria-hidden="true" tabindex="-1"></a> hif(rwb) <span class="kw">{</span> data <span class="kw"><=</span> content<span class="kw">[</span>addr<span class="kw">]</span> <span class="kw">}</span></span>
<span id="cb55-10"><a href="#cb55-10" aria-hidden="true" tabindex="-1"></a> helse <span class="kw">{</span> content<span class="kw">[</span>addr<span class="kw">]</span> <span class="kw"><=</span> data <span class="kw">}</span></span>
<span id="cb55-11"><a href="#cb55-11" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span>
<span id="cb55-12"><a href="#cb55-12" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
Structural description in a system
In a system, structural descriptions consist of subsystems and interconnections among them.
A subsystem is obtained by instantiating an existing system as
follows, where <system name>
is the name of the
system to instantiate (without any colon):
<span id="cb56-1"><a href="#cb56-1" aria-hidden="true" tabindex="-1"></a><span class="kw"><</span><span class="fu">system</span> name<span class="kw">></span> :<span class="kw"><</span>instance name<span class="kw">></span></span>
For example, system mem8_16
declared in the previous
section can be instantiated as follows:
<span id="cb57-1"><a href="#cb57-1" aria-hidden="true" tabindex="-1"></a>mem8_16 <span class="wa">:mem8_16I</span></span>
It is also possible to declare multiple instances of the same system at a time as follows:
<span id="cb58-1"><a href="#cb58-1" aria-hidden="true" tabindex="-1"></a><span class="kw"><</span><span class="fu">system</span> name<span class="kw">></span> <span class="kw">[</span>list of colon<span class="kw">-</span>separated instance names<span class="kw">]</span></span>
For example, the following code declares two instances of system
mem8_16
:
<span id="cb59-1"><a href="#cb59-1" aria-hidden="true" tabindex="-1"></a>mem8_16 <span class="kw">[</span> <span class="wa">:mem8_16I0</span>, <span class="wa">:mem8_16I1</span> <span class="kw">]</span></span>
Interconnecting instances may require internal signals in the system.
Such signals are declared using the inner
direction. For
example, the following code declares a 1-bit inner signal named
w1
and a 2-bit inner signal named w2
:
<span id="cb60-1"><a href="#cb60-1" aria-hidden="true" tabindex="-1"></a>inner <span class="wa">:w1</span></span>
<span id="cb60-2"><a href="#cb60-2" aria-hidden="true" tabindex="-1"></a><span class="kw">[</span><span class="dv">1</span><span class="kw">..</span><span class="dv">0</span><span class="kw">]</span><span class="at">.inner</span> <span class="wa">:w2</span></span>
If the signal is not meant to be changed, it can be declared using
the constant
keyword instead of inner
.
A connection between signals is done using the arrow operator
<=
as follows:
<span id="cb61-1"><a href="#cb61-1" aria-hidden="true" tabindex="-1"></a><span class="kw"><</span>destination<span class="kw">></span> <span class="kw"><=</span> <span class="kw"><</span>source<span class="kw">></span></span>
The <destination>
must be a reference to a signal,
and the <source>
can be any expression.
For example, the following code connects signal w1
to
signal ready
and signal clk
to the first bit
of signal w2
:
<span id="cb62-1"><a href="#cb62-1" aria-hidden="true" tabindex="-1"></a>ready <span class="kw"><=</span> w1</span>
<span id="cb62-2"><a href="#cb62-2" aria-hidden="true" tabindex="-1"></a>w2<span class="kw">[</span><span class="dv">0</span><span class="kw">]</span> <span class="kw"><=</span> clk</span>
As another example, the following code connects to the second bit of
w2
the output of an AND operation between clk
and rst
as follows:
<span id="cb63-1"><a href="#cb63-1" aria-hidden="true" tabindex="-1"></a>w2<span class="kw">[</span><span class="dv">1</span><span class="kw">]</span> <span class="kw"><=</span> clk <span class="kw">&</span> rst</span>
The signals of an instance can be connected through the arrow
operator too, provided they are properly referred to. One way to refer
to them is to use the dot operator .
on the instance as
follows:
<span id="cb64-1"><a href="#cb64-1" aria-hidden="true" tabindex="-1"></a><span class="kw"><</span>instance name<span class="kw">>.<</span>signal name<span class="kw">></span></span>
For example, the following code connects signal clk
of
instance mem8_16I
to signal clk
of the current
system:
<span id="cb65-1"><a href="#cb65-1" aria-hidden="true" tabindex="-1"></a>mem8_16I<span class="at">.clk</span> <span class="kw"><=</span> clk</span>
It is also possible to connect multiple signals of an instance using
the call operator .()
as follows, where each target can be
any expression:
<span id="cb66-1"><a href="#cb66-1" aria-hidden="true" tabindex="-1"></a><span class="kw"><</span>instance name<span class="kw">>.</span>(<span class="kw"><</span>signal name0<span class="kw">></span>: <span class="kw"><</span>target0<span class="kw">></span>, <span class="kw">...</span>)</span>
For example, the following code connects signals clk
and
rst
of instance mem8_16I
to signals
clk
and rst
of the current system. As seen in
this example, this method allows partial connection since the address
and the data buses are not connected yet.
<span id="cb67-1"><a href="#cb67-1" aria-hidden="true" tabindex="-1"></a>mem8_16I<span class="kw">.</span>(<span class="wa">clk: </span>clk, <span class="wa">rst: </span>rst)</span>
This last connection method can be used directly while declaring an
instance. For example, mem8_16I
could have been declared
and connected to clk
and rst
as follows:
<span id="cb68-1"><a href="#cb68-1" aria-hidden="true" tabindex="-1"></a>mem8_16(<span class="wa">:mem8_16I</span>)<span class="kw">.</span>(<span class="wa">clk: </span>clk, <span class="wa">rst: </span>rest)</span>
To summarize this section, here is a structural description of a 16-bit memory made of two 8-bit memories (or equivalent) sharing the same address bus, and using respectively the lower and the higher 8-bits of the data bus:
<span id="cb69-1"><a href="#cb69-1" aria-hidden="true" tabindex="-1"></a><span class="fu">system</span> <span class="wa">:mem16_16</span> <span class="cf">do</span></span>
<span id="cb69-2"><a href="#cb69-2" aria-hidden="true" tabindex="-1"></a> input <span class="wa">:clk</span>, <span class="wa">:rwb</span></span>
<span id="cb69-3"><a href="#cb69-3" aria-hidden="true" tabindex="-1"></a> <span class="kw">[</span><span class="dv">15</span><span class="kw">..</span><span class="dv">0</span><span class="kw">]</span><span class="at">.input</span> <span class="wa">:addr</span></span>
<span id="cb69-4"><a href="#cb69-4" aria-hidden="true" tabindex="-1"></a> <span class="kw">[</span><span class="dv">15</span><span class="kw">..</span><span class="dv">0</span><span class="kw">]</span><span class="at">.inout</span> <span class="wa">:data</span></span>
<span id="cb69-5"><a href="#cb69-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb69-6"><a href="#cb69-6" aria-hidden="true" tabindex="-1"></a> mem8_16(<span class="wa">:memL</span>)<span class="kw">.</span>(<span class="wa">clk: </span>clk, <span class="wa">rwb: </span>rwb, <span class="wa">addr: </span>addr, <span class="wa">data: </span>data<span class="kw">[</span><span class="dv">7</span><span class="kw">..</span><span class="dv">0</span><span class="kw">]</span>)</span>
<span id="cb69-7"><a href="#cb69-7" aria-hidden="true" tabindex="-1"></a> mem8_16(<span class="wa">:memH</span>)<span class="kw">.</span>(<span class="wa">clk: </span>clk, <span class="wa">rwb: </span>rwb, <span class="wa">addr: </span>addr, <span class="wa">data: </span>data<span class="kw">[</span><span class="dv">15</span><span class="kw">..</span><span class="dv">8</span><span class="kw">]</span>)</span>
<span id="cb69-8"><a href="#cb69-8" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
And here is an equivalent code using the arrow operator:
<span id="cb70-1"><a href="#cb70-1" aria-hidden="true" tabindex="-1"></a><span class="fu">system</span> <span class="wa">:mem16_16</span> <span class="cf">do</span></span>
<span id="cb70-2"><a href="#cb70-2" aria-hidden="true" tabindex="-1"></a> input <span class="wa">:clk</span>, <span class="wa">:rwb</span></span>
<span id="cb70-3"><a href="#cb70-3" aria-hidden="true" tabindex="-1"></a> <span class="kw">[</span><span class="dv">15</span><span class="kw">..</span><span class="dv">0</span><span class="kw">]</span><span class="at">.input</span> <span class="wa">:addr</span></span>
<span id="cb70-4"><a href="#cb70-4" aria-hidden="true" tabindex="-1"></a> <span class="kw">[</span><span class="dv">15</span><span class="kw">..</span><span class="dv">0</span><span class="kw">]</span><span class="at">.inout</span> <span class="wa">:data</span></span>
<span id="cb70-5"><a href="#cb70-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb70-6"><a href="#cb70-6" aria-hidden="true" tabindex="-1"></a> mem8_16 <span class="kw">[</span><span class="wa">:memL</span>, <span class="wa">:memH</span><span class="kw">]</span></span>
<span id="cb70-7"><a href="#cb70-7" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb70-8"><a href="#cb70-8" aria-hidden="true" tabindex="-1"></a> memL<span class="at">.clk</span> <span class="kw"><=</span> clk</span>
<span id="cb70-9"><a href="#cb70-9" aria-hidden="true" tabindex="-1"></a> memL<span class="at">.rwb</span> <span class="kw"><=</span> rwb</span>
<span id="cb70-10"><a href="#cb70-10" aria-hidden="true" tabindex="-1"></a> memL<span class="at">.addr</span> <span class="kw"><=</span> addr</span>
<span id="cb70-11"><a href="#cb70-11" aria-hidden="true" tabindex="-1"></a> memL<span class="at">.data</span> <span class="kw"><=</span> data<span class="kw">[</span><span class="dv">7</span><span class="kw">..</span><span class="dv">0</span><span class="kw">]</span></span>
<span id="cb70-12"><a href="#cb70-12" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb70-13"><a href="#cb70-13" aria-hidden="true" tabindex="-1"></a> memH<span class="at">.clk</span> <span class="kw"><=</span> clk</span>
<span id="cb70-14"><a href="#cb70-14" aria-hidden="true" tabindex="-1"></a> memH<span class="at">.rwb</span> <span class="kw"><=</span> rwb</span>
<span id="cb70-15"><a href="#cb70-15" aria-hidden="true" tabindex="-1"></a> memH<span class="at">.addr</span> <span class="kw"><=</span> addr</span>
<span id="cb70-16"><a href="#cb70-16" aria-hidden="true" tabindex="-1"></a> memH<span class="at">.data</span> <span class="kw"><=</span> data<span class="kw">[</span><span class="dv">15</span><span class="kw">..</span><span class="dv">8</span><span class="kw">]</span></span>
<span id="cb70-17"><a href="#cb70-17" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
Initialization of signals
Output, inner, and constant signals of a system can be initialized when declared using the following syntax in place of the usual name of the signal:
<span id="cb71-1"><a href="#cb71-1" aria-hidden="true" tabindex="-1"></a><span class="kw"><</span>signal name<span class="kw">></span>: <span class="kw"><</span>intial value<span class="kw">></span></span>
For example, a single-bit inner signal named sig
can be
initialized to 0 as follows:
<span id="cb72-1"><a href="#cb72-1" aria-hidden="true" tabindex="-1"></a>inner <span class="wa">sig: </span><span class="dv">0</span></span>
As another example, an 8-bit 8-word ROM could be declared and initialized as follows:
<span id="cb73-1"><a href="#cb73-1" aria-hidden="true" tabindex="-1"></a>bit<span class="kw">[</span><span class="dv">8</span><span class="kw">][-</span><span class="dv">8</span><span class="kw">]</span> <span class="wa">rom: </span><span class="kw">[</span> <span class="dv">0</span>,<span class="dv">1</span>,<span class="dv">2</span>,<span class="dv">3</span>,<span class="dv">4</span>,<span class="dv">5</span>,<span class="dv">6</span>,<span class="dv">7</span> <span class="kw">]</span></span>
Scope in a system
General scopes
The signals of the interface of signals are accessible from anywhere in an HDLRuby description. This is not the case for inner signals and instances: they are accessible only within the scope they are declared in.
A scope is a region of the code where locally declared objects are
accessible. Each system has its scope that cannot be accessible from
another part of an HDLRuby description. For example, in the following
code signals d
and qb
as well as instance
dffI
cannot be accessed from outside system
div2
:
<span id="cb74-1"><a href="#cb74-1" aria-hidden="true" tabindex="-1"></a><span class="fu">system</span> <span class="wa">:div2</span> <span class="cf">do</span></span>
<span id="cb74-2"><a href="#cb74-2" aria-hidden="true" tabindex="-1"></a> input <span class="wa">:clk</span></span>
<span id="cb74-3"><a href="#cb74-3" aria-hidden="true" tabindex="-1"></a> output <span class="wa">:q</span></span>
<span id="cb74-4"><a href="#cb74-4" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb74-5"><a href="#cb74-5" aria-hidden="true" tabindex="-1"></a> inner <span class="wa">:d</span>, <span class="wa">:qb</span></span>
<span id="cb74-6"><a href="#cb74-6" aria-hidden="true" tabindex="-1"></a> d <span class="kw"><=</span> qb</span>
<span id="cb74-7"><a href="#cb74-7" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb74-8"><a href="#cb74-8" aria-hidden="true" tabindex="-1"></a> dff_full(<span class="wa">:dffI</span>)<span class="kw">.</span>(<span class="wa">clk: </span>clk, <span class="wa">d: </span>d, <span class="wa">q: </span>q, <span class="wa">qb: </span>qb)</span>
<span id="cb74-9"><a href="#cb74-9" aria-hidden="true" tabindex="-1"></a> </span>
For robustness or, readability purposes, it is possible to add an
inner scope inside the existing scope using the sub
keyword
as follows:
<span id="cb75-1"><a href="#cb75-1" aria-hidden="true" tabindex="-1"></a><span class="fu">sub</span> <span class="cf">do</span></span>
<span id="cb75-2"><a href="#cb75-2" aria-hidden="true" tabindex="-1"></a> <span class="kw"><</span>code<span class="kw">></span></span>
<span id="cb75-3"><a href="#cb75-3" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
For example, in the code below signal sig
is not
accessible from outside the additional inner scope of system
sys
<span id="cb76-1"><a href="#cb76-1" aria-hidden="true" tabindex="-1"></a><span class="fu">system</span> <span class="wa">:sys</span> <span class="cf">do</span></span>
<span id="cb76-2"><a href="#cb76-2" aria-hidden="true" tabindex="-1"></a> <span class="kw">...</span></span>
<span id="cb76-3"><a href="#cb76-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">sub</span></span>
<span id="cb76-4"><a href="#cb76-4" aria-hidden="true" tabindex="-1"></a> inner <span class="wa">:sig</span></span>
<span id="cb76-5"><a href="#cb76-5" aria-hidden="true" tabindex="-1"></a> <span class="kw"><</span>sig is accessible here<span class="kw">></span></span>
<span id="cb76-6"><a href="#cb76-6" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span>
<span id="cb76-7"><a href="#cb76-7" aria-hidden="true" tabindex="-1"></a> <span class="kw"><</span>sig is <span class="cf">not</span> accessible from here<span class="kw">></span></span>
<span id="cb76-8"><a href="#cb76-8" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
It is also possible to add an inner scope within another inner scope as follows:
<span id="cb77-1"><a href="#cb77-1" aria-hidden="true" tabindex="-1"></a><span class="fu">system</span> <span class="wa">:sys</span> <span class="cf">do</span></span>
<span id="cb77-2"><a href="#cb77-2" aria-hidden="true" tabindex="-1"></a> <span class="kw">...</span></span>
<span id="cb77-3"><a href="#cb77-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">sub</span></span>
<span id="cb77-4"><a href="#cb77-4" aria-hidden="true" tabindex="-1"></a> inner <span class="wa">:sig0</span></span>
<span id="cb77-5"><a href="#cb77-5" aria-hidden="true" tabindex="-1"></a> <span class="kw"><</span>sig0 is accessible here<span class="kw">></span></span>
<span id="cb77-6"><a href="#cb77-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">sub</span></span>
<span id="cb77-7"><a href="#cb77-7" aria-hidden="true" tabindex="-1"></a> inner <span class="wa">:sig1</span></span>
<span id="cb77-8"><a href="#cb77-8" aria-hidden="true" tabindex="-1"></a> <span class="kw"><</span>sig0 <span class="cf">and</span> sig1 are accessible here<span class="kw">></span></span>
<span id="cb77-9"><a href="#cb77-9" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span>
<span id="cb77-10"><a href="#cb77-10" aria-hidden="true" tabindex="-1"></a> <span class="kw"><</span>sig1 is <span class="cf">not</span> accessible here<span class="kw">></span></span>
<span id="cb77-11"><a href="#cb77-11" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span>
<span id="cb77-12"><a href="#cb77-12" aria-hidden="true" tabindex="-1"></a> <span class="kw"><</span>neither sig0 nor sig1 are accessible here<span class="kw">></span></span>
<span id="cb77-13"><a href="#cb77-13" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
Within the same scope, it is not possible to declare multiple signals or instances with the same name. However, it is possible to declare a signal or an instance with a name identical to one previously declared outside the scope: the inner-most declaration will be used.
Named scopes
It is possible to declare a scope with a name as follows:
<span id="cb78-1"><a href="#cb78-1" aria-hidden="true" tabindex="-1"></a><span class="fu">sub</span> :<span class="kw"><</span>name<span class="kw">></span> <span class="cf">do</span></span>
<span id="cb78-2"><a href="#cb78-2" aria-hidden="true" tabindex="-1"></a> <span class="kw"><</span>code<span class="kw">></span></span>
<span id="cb78-3"><a href="#cb78-3" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
Where:
<name>
is the name of the scope.<code>
is the code within the scope.
Contrary to the case of scopes without a name, signals, and instances
declared within a named scope can be accessed outside using this name as
a reference. For example, in the code below signal sig
declared within scope named scop
is accessed outside it
using scop.sig
:
<span id="cb79-1"><a href="#cb79-1" aria-hidden="true" tabindex="-1"></a><span class="fu">sub</span> <span class="wa">:scop</span> <span class="cf">do</span></span>
<span id="cb79-2"><a href="#cb79-2" aria-hidden="true" tabindex="-1"></a> inner <span class="wa">:sig</span></span>
<span id="cb79-3"><a href="#cb79-3" aria-hidden="true" tabindex="-1"></a> <span class="kw">...</span></span>
<span id="cb79-4"><a href="#cb79-4" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
<span id="cb79-5"><a href="#cb79-5" aria-hidden="true" tabindex="-1"></a><span class="kw">...</span></span>
<span id="cb79-6"><a href="#cb79-6" aria-hidden="true" tabindex="-1"></a>scop<span class="at">.sig</span> <span class="kw"><=</span> <span class="kw">...</span></span>
Behavioral description in a system.
In a system, parallel behavioral descriptions are declared using the
par
keyword, and sequential behavioral descriptions are
declared using the seq
keyword. They are the equivalent of
the Verilog HDL always
blocks.
A behavior is made of a list of events (the sensitivity list) upon which it is activated, and a list of statements. A behavior is declared as follows:
<span id="cb80-1"><a href="#cb80-1" aria-hidden="true" tabindex="-1"></a>par <span class="kw"><</span>list of events<span class="kw">></span> <span class="cf">do</span></span>
<span id="cb80-2"><a href="#cb80-2" aria-hidden="true" tabindex="-1"></a> <span class="kw"><</span>list of statements<span class="kw">></span></span>
<span id="cb80-3"><a href="#cb80-3" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
In addition, it is possible to declare inner signals within an execution block. While such signals will be physically linked to the system, they are only accessible within the block they are declared into. This permits a tighter scope for signals, which improves the readability of the code and makes it possible to declare several signals with identical names provided their respective scopes are different.
An event represents a specific change in the state of a signal. For
example, a rising edge of a clock signal named clk
will be
represented by the event clk.posedge
. In HDLRuby, events
are obtained directly from expressions using the following methods:
posedge
for a rising edge, negedge
for a
falling edge, and edge
for any edge. Events are described
in more detail in section Events.
When one of the events of the sensitivity list of a behavior occurs, the behavior is executed, i.e., each of its statements is executed in sequence. A statement can represent a data transmission to a signal, a control flow, a nested execution block, or the declaration of an inner signal (as stated earlier). Statements are described in more detail in section statements. In this section, we focus on the transmission statements and the block statements.
A transmission statement is declared using the arrow operator
<=
as follows:
<span id="cb81-1"><a href="#cb81-1" aria-hidden="true" tabindex="-1"></a><span class="kw"><</span>destination<span class="kw">></span> <span class="kw"><=</span> <span class="kw"><</span>source<span class="kw">></span></span>
The <destination>
must be a reference to a signal,
and the <source>
can be any expression. A
transmission has therefore the same structure as a connection. However,
its execution model is different: whereas a connection is continuously
executed, a transmission is only executed during the execution of its
block.
A block comprises a list of statements. It is used for adding hierarchy to a behavior. Blocks can be either parallel or sequential, i.e., their transmission statements are respectively non-blocking or blocking. By default, a top block is created when declaring a behavior, and it inherits from its execution mode. For example, with the following code, the top block of the behavior is sequential.
<span id="cb82-1"><a href="#cb82-1" aria-hidden="true" tabindex="-1"></a><span class="fu">system</span> <span class="wa">:with_sequential_behavior</span> <span class="cf">do</span></span>
<span id="cb82-2"><a href="#cb82-2" aria-hidden="true" tabindex="-1"></a> seq <span class="cf">do</span></span>
<span id="cb82-3"><a href="#cb82-3" aria-hidden="true" tabindex="-1"></a> <span class="kw"><</span>list of statements<span class="kw">></span></span>
<span id="cb82-4"><a href="#cb82-4" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span>
<span id="cb82-5"><a href="#cb82-5" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
It is possible to declare new blocks within an existing block. For
declaring a sub-block with the same execution mode as the upper one, the
keyword sub
is used. For example, the following code
declares a sub-block within a sequential block, with the same execution
mode:
<span id="cb83-1"><a href="#cb83-1" aria-hidden="true" tabindex="-1"></a><span class="fu">system</span> <span class="wa">:with_sequential_behavior</span> <span class="cf">do</span></span>
<span id="cb83-2"><a href="#cb83-2" aria-hidden="true" tabindex="-1"></a> seq <span class="cf">do</span></span>
<span id="cb83-3"><a href="#cb83-3" aria-hidden="true" tabindex="-1"></a> <span class="kw"><</span>list of statements<span class="kw">></span></span>
<span id="cb83-4"><a href="#cb83-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">sub</span> <span class="cf">do</span></span>
<span id="cb83-5"><a href="#cb83-5" aria-hidden="true" tabindex="-1"></a> <span class="kw"><</span>list of statements<span class="kw">></span></span>
<span id="cb83-6"><a href="#cb83-6" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span>
<span id="cb83-7"><a href="#cb83-7" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span>
<span id="cb83-8"><a href="#cb83-8" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
A sub-block can also have a different execution mode if it is
declared using seq
, which will force sequential execution
mode, and par
which will force parallel execution mode. For
example, in the following code a parallel sub-block is declared within a
sequential one:
<span id="cb84-1"><a href="#cb84-1" aria-hidden="true" tabindex="-1"></a><span class="fu">system</span> <span class="wa">:with_sequential_behavior</span> <span class="cf">do</span></span>
<span id="cb84-2"><a href="#cb84-2" aria-hidden="true" tabindex="-1"></a> seq <span class="cf">do</span></span>
<span id="cb84-3"><a href="#cb84-3" aria-hidden="true" tabindex="-1"></a> <span class="kw"><</span>list of statements<span class="kw">></span></span>
<span id="cb84-4"><a href="#cb84-4" aria-hidden="true" tabindex="-1"></a> par <span class="cf">do</span></span>
<span id="cb84-5"><a href="#cb84-5" aria-hidden="true" tabindex="-1"></a> <span class="kw"><</span>list of statements<span class="kw">></span></span>
<span id="cb84-6"><a href="#cb84-6" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span>
<span id="cb84-7"><a href="#cb84-7" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span>
<span id="cb84-8"><a href="#cb84-8" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
Sunblocks have their scope so that it is possible to declare signals
without colliding with existing ones. For example, it is possible to
declare three different inner signals all called sig
as
follows:
<span id="cb85-1"><a href="#cb85-1" aria-hidden="true" tabindex="-1"></a><span class="kw">...</span></span>
<span id="cb85-2"><a href="#cb85-2" aria-hidden="true" tabindex="-1"></a>par(<span class="kw"><</span>sensibility list<span class="kw">></span>) <span class="cf">do</span></span>
<span id="cb85-3"><a href="#cb85-3" aria-hidden="true" tabindex="-1"></a> inner <span class="wa">:sig</span></span>
<span id="cb85-4"><a href="#cb85-4" aria-hidden="true" tabindex="-1"></a> <span class="kw">...</span></span>
<span id="cb85-5"><a href="#cb85-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">sub</span> <span class="cf">do</span></span>
<span id="cb85-6"><a href="#cb85-6" aria-hidden="true" tabindex="-1"></a> inner <span class="wa">:sig</span></span>
<span id="cb85-7"><a href="#cb85-7" aria-hidden="true" tabindex="-1"></a> <span class="kw">...</span></span>
<span id="cb85-8"><a href="#cb85-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">sub</span> <span class="cf">do</span></span>
<span id="cb85-9"><a href="#cb85-9" aria-hidden="true" tabindex="-1"></a> inner <span class="wa">:sig</span></span>
<span id="cb85-10"><a href="#cb85-10" aria-hidden="true" tabindex="-1"></a> <span class="kw">...</span></span>
<span id="cb85-11"><a href="#cb85-11" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span>
<span id="cb85-12"><a href="#cb85-12" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span>
<span id="cb85-13"><a href="#cb85-13" aria-hidden="true" tabindex="-1"></a> <span class="kw">...</span></span>
<span id="cb85-14"><a href="#cb85-14" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
To summarize this section, here is a behavioral description of a
16-bit shift register with asynchronous reset (hif
and
helse
are keywords used for specifying hardware if
and else control statements).
<span id="cb86-1"><a href="#cb86-1" aria-hidden="true" tabindex="-1"></a><span class="fu">system</span> <span class="wa">:shift16</span> <span class="cf">do</span></span>
<span id="cb86-2"><a href="#cb86-2" aria-hidden="true" tabindex="-1"></a> input <span class="wa">:clk</span>, <span class="wa">:rst</span>, <span class="wa">:din</span></span>
<span id="cb86-3"><a href="#cb86-3" aria-hidden="true" tabindex="-1"></a> output <span class="wa">:dout</span></span>
<span id="cb86-4"><a href="#cb86-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb86-5"><a href="#cb86-5" aria-hidden="true" tabindex="-1"></a> <span class="kw">[</span><span class="dv">15</span><span class="kw">..</span><span class="dv">0</span><span class="kw">]</span><span class="at">.inner</span> <span class="wa">:reg</span></span>
<span id="cb86-6"><a href="#cb86-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb86-7"><a href="#cb86-7" aria-hidden="true" tabindex="-1"></a> dout <span class="kw"><=</span> reg<span class="kw">[</span><span class="dv">15</span><span class="kw">]</span> <span class="co"># The output is the last bit of the register.</span></span>
<span id="cb86-8"><a href="#cb86-8" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb86-9"><a href="#cb86-9" aria-hidden="true" tabindex="-1"></a> par(clk<span class="at">.posedge</span>) <span class="cf">do</span></span>
<span id="cb86-10"><a href="#cb86-10" aria-hidden="true" tabindex="-1"></a> hif(rst) <span class="kw">{</span> reg <span class="kw"><=</span> <span class="dv">0</span> <span class="kw">}</span></span>
<span id="cb86-11"><a href="#cb86-11" aria-hidden="true" tabindex="-1"></a> helse <span class="cf">do</span></span>
<span id="cb86-12"><a href="#cb86-12" aria-hidden="true" tabindex="-1"></a> reg<span class="kw">[</span><span class="dv">0</span><span class="kw">]</span> <span class="kw"><=</span> din</span>
<span id="cb86-13"><a href="#cb86-13" aria-hidden="true" tabindex="-1"></a> reg<span class="kw">[</span><span class="dv">15</span><span class="kw">..</span><span class="dv">1</span><span class="kw">]</span> <span class="kw"><=</span> reg<span class="kw">[</span><span class="dv">14</span><span class="kw">..</span><span class="dv">0</span><span class="kw">]</span></span>
<span id="cb86-14"><a href="#cb86-14" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span>
<span id="cb86-15"><a href="#cb86-15" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span>
<span id="cb86-16"><a href="#cb86-16" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
In the example above, the order of the transmission statements is of
no consequence. This is not the case for the following example, which
implements the same register using a sequential block. In this second
example, putting the statement reg[0] <= din
in the last
place would have led to an invalid functionality for a shift
register.
<span id="cb87-1"><a href="#cb87-1" aria-hidden="true" tabindex="-1"></a><span class="fu">system</span> <span class="wa">:shift16</span> <span class="cf">do</span></span>
<span id="cb87-2"><a href="#cb87-2" aria-hidden="true" tabindex="-1"></a> input <span class="wa">:clk</span>, <span class="wa">:rst</span>, <span class="wa">:din</span></span>
<span id="cb87-3"><a href="#cb87-3" aria-hidden="true" tabindex="-1"></a> output <span class="wa">:dout</span></span>
<span id="cb87-4"><a href="#cb87-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb87-5"><a href="#cb87-5" aria-hidden="true" tabindex="-1"></a> <span class="kw">[</span><span class="dv">15</span><span class="kw">..</span><span class="dv">0</span><span class="kw">]</span><span class="at">.inner</span> <span class="wa">:reg</span></span>
<span id="cb87-6"><a href="#cb87-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb87-7"><a href="#cb87-7" aria-hidden="true" tabindex="-1"></a> dout <span class="kw"><=</span> reg<span class="kw">[</span><span class="dv">15</span><span class="kw">]</span> <span class="co"># The output is the last bit of the register.</span></span>
<span id="cb87-8"><a href="#cb87-8" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb87-9"><a href="#cb87-9" aria-hidden="true" tabindex="-1"></a> par(clk<span class="at">.posedge</span>) <span class="cf">do</span></span>
<span id="cb87-10"><a href="#cb87-10" aria-hidden="true" tabindex="-1"></a> hif(rst) <span class="kw">{</span> reg <span class="kw"><=</span> <span class="dv">0</span> <span class="kw">}</span></span>
<span id="cb87-11"><a href="#cb87-11" aria-hidden="true" tabindex="-1"></a> helse seq <span class="cf">do</span></span>
<span id="cb87-12"><a href="#cb87-12" aria-hidden="true" tabindex="-1"></a> reg<span class="kw">[</span><span class="dv">0</span><span class="kw">]</span> <span class="kw"><=</span> din</span>
<span id="cb87-13"><a href="#cb87-13" aria-hidden="true" tabindex="-1"></a> reg <span class="kw"><=</span> reg<span class="kw">[</span><span class="dv">14</span><span class="kw">..</span><span class="dv">0</span><span class="kw">]</span></span>
<span id="cb87-14"><a href="#cb87-14" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span>
<span id="cb87-15"><a href="#cb87-15" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span>
<span id="cb87-16"><a href="#cb87-16" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
Note:
helse seq
ensures that the block of the hardware else is in sequential mode.hif(rst)
could also have been set to sequential mode as follows:<span id="cb88-1"><a href="#cb88-1" aria-hidden="true" tabindex="-1"></a> hif rst, seq <span class="cf">do</span></span> <span id="cb88-2"><a href="#cb88-2" aria-hidden="true" tabindex="-1"></a> reg <span class="kw"><=</span> <span class="dv">0</span></span> <span id="cb88-3"><a href="#cb88-3" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span>
Parallel mode can be set the same way using
par
.
Extra features for the description of behaviors
Single-statement behaviors
It often happens that a behavior contains only one statement. In such
a case, the description can be shortened using the at
operator as follows:
<span id="cb89-1"><a href="#cb89-1" aria-hidden="true" tabindex="-1"></a>( statement )<span class="at">.at</span>(<span class="kw"><</span>list of events<span class="kw">></span>)</span>
For example, the following two code samples are equivalent:
<span id="cb90-1"><a href="#cb90-1" aria-hidden="true" tabindex="-1"></a>par(clk<span class="at">.posedge</span>) <span class="cf">do</span></span>
<span id="cb90-2"><a href="#cb90-2" aria-hidden="true" tabindex="-1"></a> a <span class="kw"><=</span> b<span class="kw">+</span><span class="dv">1</span></span>
<span id="cb90-3"><a href="#cb90-3" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
<span id="cb91-1"><a href="#cb91-1" aria-hidden="true" tabindex="-1"></a>( a <span class="kw"><=</span> b<span class="kw">+</span><span class="dv">1</span> )<span class="at">.at</span>(clk<span class="at">.posedge</span>)</span>
For the sake of consistency, this operator can also be applied to block statements as follows, but it is probably less readable than the standard declaration of behaviors:
<span id="cb92-1"><a href="#cb92-1" aria-hidden="true" tabindex="-1"></a>( seq <span class="cf">do</span></span>
<span id="cb92-2"><a href="#cb92-2" aria-hidden="true" tabindex="-1"></a> a <span class="kw"><=</span> b<span class="kw">+</span><span class="dv">1</span></span>
<span id="cb92-3"><a href="#cb92-3" aria-hidden="true" tabindex="-1"></a> c <span class="kw"><=</span> d<span class="kw">+</span><span class="dv">2</span></span>
<span id="cb92-4"><a href="#cb92-4" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span> )<span class="at">.at</span>(clk<span class="at">.posedge</span>)</span>
Insertion of statements at the beginning of a block
By default, the statements of a block are added in order of appearance in the code. However, it is also possible to insert statements at the top of the current block using the unshift command within a block as follows:
<span id="cb93-1"><a href="#cb93-1" aria-hidden="true" tabindex="-1"></a>unshift <span class="cf">do</span></span>
<span id="cb93-2"><a href="#cb93-2" aria-hidden="true" tabindex="-1"></a> <span class="kw"><</span>list of statements<span class="kw">></span></span>
<span id="cb93-3"><a href="#cb93-3" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
For example, the following code inserts two statements at the beginning of the current block:
<span id="cb94-1"><a href="#cb94-1" aria-hidden="true" tabindex="-1"></a>par <span class="cf">do</span></span>
<span id="cb94-2"><a href="#cb94-2" aria-hidden="true" tabindex="-1"></a> x <span class="kw"><=</span> y <span class="kw">+</span> z</span>
<span id="cb94-3"><a href="#cb94-3" aria-hidden="true" tabindex="-1"></a> unshift <span class="cf">do</span></span>
<span id="cb94-4"><a href="#cb94-4" aria-hidden="true" tabindex="-1"></a> a <span class="kw"><=</span> b <span class="kw">-</span> c</span>
<span id="cb94-5"><a href="#cb94-5" aria-hidden="true" tabindex="-1"></a> u <span class="kw"><=</span> v <span class="kw">&</span> w</span>
<span id="cb94-6"><a href="#cb94-6" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span>
<span id="cb94-7"><a href="#cb94-7" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
The code above will result in the following block:
<span id="cb95-1"><a href="#cb95-1" aria-hidden="true" tabindex="-1"></a>par <span class="cf">do</span></span>
<span id="cb95-2"><a href="#cb95-2" aria-hidden="true" tabindex="-1"></a> a <span class="kw"><=</span> b <span class="kw">-</span> c</span>
<span id="cb95-3"><a href="#cb95-3" aria-hidden="true" tabindex="-1"></a> u <span class="kw"><=</span> v <span class="kw">&</span> w</span>
<span id="cb95-4"><a href="#cb95-4" aria-hidden="true" tabindex="-1"></a> x <span class="kw"><=</span> y <span class="kw">+</span> z</span>
<span id="cb95-5"><a href="#cb95-5" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
Note: - While of no practical use for simple circuit descriptions, this feature can be used in advanced generic component descriptions.
Reconfiguration
In HDLRuby, dynamically reconfigurable devices are modeled by instances having more than one system. Adding systems to an instance is done as follows:
<span id="cb96-1"><a href="#cb96-1" aria-hidden="true" tabindex="-1"></a><span class="kw"><</span>instance<span class="kw">></span><span class="at">.choice</span>(<span class="kw"><</span>list of named systems<span class="kw">></span>)</span>
For example, assuming systems sys0
, sys1
,
and sys2
have been previously declared a device named
dev012
able to be reconfigured to one of these three
systems would be declared as follows (the connections of the instance,
omitted in the example, can be done as usual):
<span id="cb97-1"><a href="#cb97-1" aria-hidden="true" tabindex="-1"></a>sys0 <span class="wa">:dev012</span> <span class="co"># dev012 is at first a standard instance of sys0</span></span>
<span id="cb97-2"><a href="#cb97-2" aria-hidden="true" tabindex="-1"></a>dev012<span class="at">.choice</span>(<span class="wa">conf1: </span>sys1, <span class="wa">conf2: </span>sys2) <span class="co"># Now dev012 is reconfigurable</span></span>
After the code above, instance dev012
can be dynamically
reconfigured to sys0
, sys1
, and
sys2
with respective names dev012
,
conf1
, and conf2
.
Note: The name of the initial system in the reconfigurations is set to be the name of the instance.
A reconfigurable instance can then be reconfigured using the command
configure
as follows:
<span id="cb98-1"><a href="#cb98-1" aria-hidden="true" tabindex="-1"></a><span class="kw"><</span>instance<span class="kw">></span><span class="at">.configure</span>(<span class="kw"><</span>name <span class="cf">or</span> index<span class="kw">></span>)</span>
In the code above, the argument of configure
can either
be the name of the configuration as previously declared with
choice
, or its index in order of declaration. For example
in the following code, instance dev012
is reconfigured to
system sys1
, then system sys0
the system
sys2
:
<span id="cb99-1"><a href="#cb99-1" aria-hidden="true" tabindex="-1"></a>dev012<span class="at">.configure</span>(<span class="wa">:conf1</span>)</span>
<span id="cb99-2"><a href="#cb99-2" aria-hidden="true" tabindex="-1"></a>!<span class="dv">1000</span><span class="at">.ns</span></span>
<span id="cb99-3"><a href="#cb99-3" aria-hidden="true" tabindex="-1"></a>dev012<span class="at">.configure</span>(<span class="wa">:dev012</span>)</span>
<span id="cb99-4"><a href="#cb99-4" aria-hidden="true" tabindex="-1"></a>!<span class="dv">1000</span><span class="at">.ns</span></span>
<span id="cb99-5"><a href="#cb99-5" aria-hidden="true" tabindex="-1"></a>dev012<span class="at">.configure</span>(<span class="dv">2</span>)</span>
These reconfiguration commands are treated as regular RTL statements in HDLRuby and are supported by the simulator. However, in the current version of the HDLRuby, these statements are ignored when generating Verilog HDL or VHDL code.
Events
Each behavior of a system is associated with a list of events, called a sensitivity list, that specifies when the behavior is to be executed. An event is associated with a signal and represents the instant when the signal reaches a given state.
There are three kinds of events: positive edge events represent the
instants when their corresponding signals vary from 0 to 1, and negative
edge events represent the instants when their corresponding signals vary
from 1 to 0 and the change events represent the instants when their
corresponding signals vary. Events are declared directly from the
signals, using the posedge
operator for a positive edge,
the negedge
operator for a negative edge, and the
change
operator for change. For example, the following code
declares 3 behaviors activated respectively on the positive edge, the
negative edge, and any change of the clk
signal.
<span id="cb100-1"><a href="#cb100-1" aria-hidden="true" tabindex="-1"></a>inner <span class="wa">:clk</span></span>
<span id="cb100-2"><a href="#cb100-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb100-3"><a href="#cb100-3" aria-hidden="true" tabindex="-1"></a>par(clk<span class="at">.posedge</span>) <span class="cf">do</span></span>
<span id="cb100-4"><a href="#cb100-4" aria-hidden="true" tabindex="-1"></a><span class="kw">...</span></span>
<span id="cb100-5"><a href="#cb100-5" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
<span id="cb100-6"><a href="#cb100-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb100-7"><a href="#cb100-7" aria-hidden="true" tabindex="-1"></a>par(clk<span class="at">.negedge</span>) <span class="cf">do</span></span>
<span id="cb100-8"><a href="#cb100-8" aria-hidden="true" tabindex="-1"></a><span class="kw">...</span></span>
<span id="cb100-9"><a href="#cb100-9" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
<span id="cb100-10"><a href="#cb100-10" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb100-11"><a href="#cb100-11" aria-hidden="true" tabindex="-1"></a>par(clk<span class="at">.change</span>) <span class="cf">do</span></span>
<span id="cb100-12"><a href="#cb100-12" aria-hidden="true" tabindex="-1"></a><span class="kw">...</span></span>
<span id="cb100-13"><a href="#cb100-13" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
Note: - The change
keyword can be
omitted.
Statements
Statements are the basic elements of a behavioral description. They are regrouped in blocks that specify their execution mode (parallel or sequential). There are four kinds of statements: the transmit statement which computes expressions and sends the result to the target signals, the control statement which changes the execution flow of the behavior, the block statement (described earlier), and the inner signal declaration.
Note:
- There is a fifth type of statement, named the time statement, that will be discussed in section Time.
Transmit statement
A transmit statement is declared using the arrow operator
<=
within a behavior. Its right value is the expression
to compute and its left value is a reference to the target signals (or
parts of signals), i.e., the signals (or parts of signals) that receive
the computation result.
For example, the following code transmits the value 3
to
signal s0
and the sum of the values of signals
i0
and i1
to the first four bits of signal
s1
:
<span id="cb101-1"><a href="#cb101-1" aria-hidden="true" tabindex="-1"></a>s0 <span class="kw"><=</span> <span class="dv">3</span></span>
<span id="cb101-2"><a href="#cb101-2" aria-hidden="true" tabindex="-1"></a>s1<span class="kw">[</span><span class="dv">3</span><span class="kw">..</span><span class="dv">0</span><span class="kw">]</span> <span class="kw"><=</span> i0 <span class="kw">+</span> i1</span>
The behavior of a transmit statement depends on the execution mode of the enclosing block:
- If the mode is parallel, the target signals are updated when all the statements of the current block are processed.
- If the mode is sequential, the target signals are updated immediately after the right value of the statement is computed.
Control statements
There are only two possible control statements: the hardware if
hif
and the hardware case hcase
.
hif
The hif
construct is made of a condition and a block
that is executed if and only if the condition is met. It is declared as
follows, where the condition can be any expression:
<span id="cb102-1"><a href="#cb102-1" aria-hidden="true" tabindex="-1"></a>hif <span class="kw"><</span>condition<span class="kw">></span> <span class="cf">do</span></span>
<span id="cb102-2"><a href="#cb102-2" aria-hidden="true" tabindex="-1"></a> <span class="kw"><</span>block contents<span class="kw">></span></span>
<span id="cb102-3"><a href="#cb102-3" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
hcase
The hcase
construct is made of an expression and a list
of value-block pairs. A block is executed when the corresponding value
is equal to the value of the expression of the hcase
. This
construct is declared as follows:
<span id="cb103-1"><a href="#cb103-1" aria-hidden="true" tabindex="-1"></a>hcase <span class="kw"><</span>expression<span class="kw">></span></span>
<span id="cb103-2"><a href="#cb103-2" aria-hidden="true" tabindex="-1"></a>hwhen <span class="kw"><</span>value <span class="dv">0</span><span class="kw">></span> <span class="cf">do</span></span>
<span id="cb103-3"><a href="#cb103-3" aria-hidden="true" tabindex="-1"></a> <span class="kw"><</span>block contents <span class="dv">0</span><span class="kw">></span></span>
<span id="cb103-4"><a href="#cb103-4" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
<span id="cb103-5"><a href="#cb103-5" aria-hidden="true" tabindex="-1"></a>hwhen <span class="kw"><</span>value <span class="dv">1</span><span class="kw">></span> <span class="cf">do</span></span>
<span id="cb103-6"><a href="#cb103-6" aria-hidden="true" tabindex="-1"></a> <span class="kw"><</span>block contents <span class="dv">1</span><span class="kw">></span></span>
<span id="cb103-7"><a href="#cb103-7" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
<span id="cb103-8"><a href="#cb103-8" aria-hidden="true" tabindex="-1"></a><span class="kw">...</span></span>
helse
It is possible to add a block that is executed when the condition of
an hif
is not met, or when no case matches the expression
of a hcase
, using the helse
keyword as
follows:
<span id="cb104-1"><a href="#cb104-1" aria-hidden="true" tabindex="-1"></a><span class="kw"><</span>hif <span class="cf">or</span> hcase construct<span class="kw">></span></span>
<span id="cb104-2"><a href="#cb104-2" aria-hidden="true" tabindex="-1"></a>helse <span class="cf">do</span></span>
<span id="cb104-3"><a href="#cb104-3" aria-hidden="true" tabindex="-1"></a> <span class="kw"><</span>block contents<span class="kw">></span></span>
<span id="cb104-4"><a href="#cb104-4" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
helsif
In addition to helse
, it is possible to set additional
conditions to an hif
using the helsif
keyword
as follows:
<span id="cb105-1"><a href="#cb105-1" aria-hidden="true" tabindex="-1"></a>hif <span class="kw"><</span>condition <span class="dv">0</span><span class="kw">></span> <span class="cf">do</span></span>
<span id="cb105-2"><a href="#cb105-2" aria-hidden="true" tabindex="-1"></a> <span class="kw"><</span>block contents <span class="dv">0</span><span class="kw">></span></span>
<span id="cb105-3"><a href="#cb105-3" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
<span id="cb105-4"><a href="#cb105-4" aria-hidden="true" tabindex="-1"></a>helsif <span class="kw"><</span>condition <span class="dv">1</span><span class="kw">></span> <span class="cf">do</span></span>
<span id="cb105-5"><a href="#cb105-5" aria-hidden="true" tabindex="-1"></a> <span class="kw"><</span>block contents <span class="dv">1</span><span class="kw">></span></span>
<span id="cb105-6"><a href="#cb105-6" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
<span id="cb105-7"><a href="#cb105-7" aria-hidden="true" tabindex="-1"></a><span class="kw">...</span></span>
About loops
HDLRuby does not include any hardware construct for describing loops.
This might look poor compared to the other HDL, but it is important to
understand that the current synthesis tools do not synthesize hardware
from such loops but instead preprocess them (e.g., unroll them) to
synthesizable loopless hardware. In HDLRuby, such features are natively
supported by the Ruby loop constructs (for
,
while
, and so on), but also by advanced Ruby constructs
like the enumerators (each
, times
, and so
on).
Notes:
HDLRuby being based on Ruby, it is highly recommended to avoid
for
orwhile
constructs and to use enumerators instead.The Ruby
if
andcase
statements can also be used, but they do not represent any hardware. They are executed when the corresponding system is instantiated. For example, the following code will displayHello world!
when the described system is instantiated, provided the generic parameterparam
is not nil.<span id="cb106-1"><a href="#cb106-1" aria-hidden="true" tabindex="-1"></a><span class="fu">system</span> <span class="wa">:say_hello</span> <span class="cf">do</span> <span class="kw">|</span>param <span class="kw">=</span> <span class="dv">nil</span><span class="kw">|</span></span> <span id="cb106-2"><a href="#cb106-2" aria-hidden="true" tabindex="-1"></a> <span class="cf">if</span> param !<span class="kw">=</span> <span class="dv">nil</span> <span class="cf">then</span></span> <span id="cb106-3"><a href="#cb106-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">puts</span> <span class="st">"Hello world!"</span></span> <span id="cb106-4"><a href="#cb106-4" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span> <span id="cb106-5"><a href="#cb106-5" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
Types
Each signal and each expression is associated with a data type that describes the kind of value it can represent. In HDLRuby, the data types represent bit-vectors associated with the way they should be interpreted, i.e., as bit strings, unsigned values, signed values, or hierarchical contents.
Type Construction
There are five basic types, bit
, signed
,
unsigned
, integer
, and float
that
represent respectively single bit logical values, single-bit unsigned
values, single-bit signed values, Ruby integer values, and Ruby
floating-point values (double precision). The first three types are HW
and support four-valued logic, whereas the two last ones are SW (but are
compatible with HW) and only support Boolean logic. Ruby integers can
represent any element of Z (the mathematical integers)
and have for that purpose a variable bit-width.
The other types are built from them using a combination of the two following type operators.
The vector operator []
is used for
building types representing vectors of single or multiple other types. A
vector whose elements have all the same type is declared as follows:
<span id="cb107-1"><a href="#cb107-1" aria-hidden="true" tabindex="-1"></a><span class="kw"><</span>type<span class="kw">>[<</span>range<span class="kw">>]</span></span>
The <range>
of a vector type indicates the
position of the starting and ending bits. An n..0
range can
also be abbreviated to n+1
. For instance, the two following
types are identical:
<span id="cb108-1"><a href="#cb108-1" aria-hidden="true" tabindex="-1"></a>bit<span class="kw">[</span><span class="dv">7</span><span class="kw">..</span><span class="dv">0</span><span class="kw">]</span></span>
<span id="cb108-2"><a href="#cb108-2" aria-hidden="true" tabindex="-1"></a>bit<span class="kw">[</span><span class="dv">8</span><span class="kw">]</span></span>
A vector of multiple types, also called a tuple, is declared as follows:
<span id="cb109-1"><a href="#cb109-1" aria-hidden="true" tabindex="-1"></a><span class="kw">[<</span>type <span class="dv">0</span><span class="kw">></span>, <span class="kw"><</span>type <span class="dv">1</span><span class="kw">></span>, <span class="kw">...</span> <span class="kw">]</span></span>
For example, the following code declares the type of the vectors made of an 8-bit logical, a 16-bit signed, and a 16-bit unsigned values:
<span id="cb110-1"><a href="#cb110-1" aria-hidden="true" tabindex="-1"></a><span class="kw">[</span> bit<span class="kw">[</span><span class="dv">8</span><span class="kw">]</span>, signed<span class="kw">[</span><span class="dv">16</span><span class="kw">]</span>, unsigned<span class="kw">[</span><span class="dv">16</span><span class="kw">]</span> <span class="kw">]</span></span>
The structure operator {}
is used for
building hierarchical types made of named subtypes. This operator is
used as follows:
<span id="cb111-1"><a href="#cb111-1" aria-hidden="true" tabindex="-1"></a><span class="kw">{</span> <span class="kw"><</span>name <span class="dv">0</span><span class="kw">></span>: <span class="kw"><</span>type <span class="dv">0</span><span class="kw">></span>, <span class="kw"><</span>name <span class="dv">1</span><span class="kw">></span>: <span class="kw"><</span>type <span class="dv">1</span><span class="kw">></span>, <span class="kw">...</span> <span class="kw">}</span></span>
For instance, the following code declares a hierarchical type with an
8-bit subtype named header
and a 24-bit subtype named
data
:
<span id="cb112-1"><a href="#cb112-1" aria-hidden="true" tabindex="-1"></a><span class="kw">{</span> <span class="wa">header: </span>bit<span class="kw">[</span><span class="dv">7</span><span class="kw">..</span><span class="dv">0</span><span class="kw">]</span>, <span class="wa">data: </span>bit<span class="kw">[</span><span class="dv">23</span><span class="kw">..</span><span class="dv">0</span><span class="kw">]</span> <span class="kw">}</span></span>
Type definition
It is possible to give names to type constructs using the
typedef
keywords as follows:
<span id="cb113-1"><a href="#cb113-1" aria-hidden="true" tabindex="-1"></a><span class="kw"><</span>type construct<span class="kw">></span><span class="at">.typedef</span> :<span class="kw"><</span>name<span class="kw">></span></span>
For example, the following gives the name char
to a
signed 8-bit vector:
<span id="cb114-1"><a href="#cb114-1" aria-hidden="true" tabindex="-1"></a>signed<span class="kw">[</span><span class="dv">7</span><span class="kw">..</span><span class="dv">0</span><span class="kw">]</span><span class="at">.typedef</span> <span class="wa">:char</span></span>
After this statement, char
can be used like any other
type. For example, the following code sample declares a new input signal
sig
whose type is char
:
<span id="cb115-1"><a href="#cb115-1" aria-hidden="true" tabindex="-1"></a>char<span class="at">.input</span> <span class="wa">:sig</span></span>
Alternatively, a new type can also be defined using the following syntax:
<span id="cb116-1"><a href="#cb116-1" aria-hidden="true" tabindex="-1"></a>typedef :<span class="kw"><</span>type name<span class="kw">></span> <span class="cf">do</span></span>
<span id="cb116-2"><a href="#cb116-2" aria-hidden="true" tabindex="-1"></a> <span class="kw"><</span>code<span class="kw">></span></span>
<span id="cb116-3"><a href="#cb116-3" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
Where:
type name
is the name of the typecode
is a description of the content of the type
For example, the previous char
could have been declared
as follows:
<span id="cb117-1"><a href="#cb117-1" aria-hidden="true" tabindex="-1"></a>typedef <span class="wa">:char</span> <span class="cf">do</span></span>
<span id="cb117-2"><a href="#cb117-2" aria-hidden="true" tabindex="-1"></a> signed<span class="kw">[</span><span class="dv">7</span><span class="kw">..</span><span class="dv">0</span><span class="kw">]</span></span>
<span id="cb117-3"><a href="#cb117-3" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span> </span>
Type compatibility and conversion
The basis of all the types in HDLRuby is the vector of bits (bit vector) where each bit can have four values: 0, 1, Z, and X (for undefined). Bit vectors are by default unsigned but can be set to be signed. When performing computations between signals of different bit-vector types, the shorter signal is extended to the size of the larger one preserving its sign if it is signed.
While the underlying structure of any HDLRuby type is the bit vector, complex types can be defined. When using such types in computational expressions and assignments they are first implicitly converted to an unsigned bit vector of the same size.
Expressions
Expressions are any construct that represents a value associated with a type. They include immediate values, reference to signals and operations among other expressions using expression operators.
Immediate values
The immediate values of HDLRuby can represent vectors of
bit
, unsigned
, and signed
, and
integer or floating-point numbers. They are prefixed by a _
character and include a header that indicates the vector type and the
base used for representing the value, followed by a numeral representing
the value. The bit width of a value is obtained by default from the
width of the numeral, but it is also possible to specify it in the
header. In addition, the character _
can be put anywhere in
the number to increase the readability, but it will be ignored.
The vector type specifiers are the following:
b
:bit
type, can be omitted,u
:unsigned
type, (equivalent tob
and can be used for avoiding confusion with the binary specifier),s
:signed
type, the last figure is sign-extended if required by the binary, octal, and hexadecimal bases, but not for the decimal base.
The base specifiers are the following:
b
: binary,o
: octal,d
: decimal,h
: hexadecimal.
For example, all the following immediate values represent an 8-bit
hundred
(either in unsigned or signed representation):
<span id="cb118-1"><a href="#cb118-1" aria-hidden="true" tabindex="-1"></a>_bb01100100</span>
<span id="cb118-2"><a href="#cb118-2" aria-hidden="true" tabindex="-1"></a>_b8b110_0100</span>
<span id="cb118-3"><a href="#cb118-3" aria-hidden="true" tabindex="-1"></a>_b01100100</span>
<span id="cb118-4"><a href="#cb118-4" aria-hidden="true" tabindex="-1"></a>_u8d100</span>
<span id="cb118-5"><a href="#cb118-5" aria-hidden="true" tabindex="-1"></a>_s8d100</span>
<span id="cb118-6"><a href="#cb118-6" aria-hidden="true" tabindex="-1"></a>_uh64</span>
<span id="cb118-7"><a href="#cb118-7" aria-hidden="true" tabindex="-1"></a>_s8o144</span>
Finally, it is possible to omit either the type specifier, the
default being unsigned bit, or the base specifier, the default being
binary. For example, all the following immediate values represent an
8-bit unsigned hundred
:
<span id="cb119-1"><a href="#cb119-1" aria-hidden="true" tabindex="-1"></a>_b01100100</span>
<span id="cb119-2"><a href="#cb119-2" aria-hidden="true" tabindex="-1"></a>_h64</span>
<span id="cb119-3"><a href="#cb119-3" aria-hidden="true" tabindex="-1"></a>_o144</span>
Notes:
_01100100
used to be considered equivalent to_b01100100
, however, due to compatibility troubles with a recent version of Ruby it is considered deprecated.Ruby immediate values can also be used, their bit width is automatically adjusted to match the data type of the expression they are used in. Please notice this adjustment may change the value of the immediate, for example, the following code will set
sig
to 4 instead of 100:<span id="cb120-1"><a href="#cb120-1" aria-hidden="true" tabindex="-1"></a><span class="kw">[</span><span class="dv">3</span><span class="kw">..</span><span class="dv">0</span><span class="kw">]</span><span class="at">.inner</span> <span class="wa">:sig</span></span> <span id="cb120-2"><a href="#cb120-2" aria-hidden="true" tabindex="-1"></a>sig <span class="kw"><=</span> <span class="dv">100</span></span>
References
References are expressions used to designate signals or a part of signals.
The simplest reference is simply the name of a signal. It designates
the signal corresponding to this name in the current scope. For
instance, in the following code, inner signal sig0
is
declared, and therefore the name sig0 becomes a reference to
designate this signal.
<span id="cb121-1"><a href="#cb121-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Declaration of signal sig0.</span></span>
<span id="cb121-2"><a href="#cb121-2" aria-hidden="true" tabindex="-1"></a>inner <span class="wa">:sig0</span></span>
<span id="cb121-3"><a href="#cb121-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb121-4"><a href="#cb121-4" aria-hidden="true" tabindex="-1"></a><span class="co"># Access to signal sig0 using a name reference.</span></span>
<span id="cb121-5"><a href="#cb121-5" aria-hidden="true" tabindex="-1"></a>sig0 <span class="kw"><=</span> <span class="dv">0</span></span>
For designating a signal of another system, or a sub-signal in a
hierarchical signal, you can use the .
operator as
follows:
<span id="cb122-1"><a href="#cb122-1" aria-hidden="true" tabindex="-1"></a><span class="kw"><</span>parent name<span class="kw">>.<</span>signal name<span class="kw">></span></span>
For example, in the following code, input signal d
of
system instance dff0
is connected to sub-signal
sub0
of hierarchical signal sig
.
<span id="cb123-1"><a href="#cb123-1" aria-hidden="true" tabindex="-1"></a><span class="fu">system</span> <span class="wa">:dff</span> <span class="cf">do</span></span>
<span id="cb123-2"><a href="#cb123-2" aria-hidden="true" tabindex="-1"></a> input <span class="wa">:clk</span>, <span class="wa">:rst</span>, <span class="wa">:d</span></span>
<span id="cb123-3"><a href="#cb123-3" aria-hidden="true" tabindex="-1"></a> output <span class="wa">:q</span></span>
<span id="cb123-4"><a href="#cb123-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb123-5"><a href="#cb123-5" aria-hidden="true" tabindex="-1"></a> par(clk<span class="at">.posedge</span>) <span class="kw">{</span> q <span class="kw"><=</span> d <span class="kw">&</span> <span class="kw">~</span>rst <span class="kw">}</span></span>
<span id="cb123-6"><a href="#cb123-6" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
<span id="cb123-7"><a href="#cb123-7" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb123-8"><a href="#cb123-8" aria-hidden="true" tabindex="-1"></a><span class="fu">system</span> <span class="wa">:my_system</span> <span class="cf">do</span></span>
<span id="cb123-9"><a href="#cb123-9" aria-hidden="true" tabindex="-1"></a> input <span class="wa">:clk</span>, <span class="wa">:rst</span></span>
<span id="cb123-10"><a href="#cb123-10" aria-hidden="true" tabindex="-1"></a> <span class="kw">{</span> <span class="wa">sub0: </span>bit, <span class="wa">sub1: </span>bit<span class="kw">}</span><span class="at">.inner</span> <span class="wa">:sig</span></span>
<span id="cb123-11"><a href="#cb123-11" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb123-12"><a href="#cb123-12" aria-hidden="true" tabindex="-1"></a> dff(<span class="wa">:dff0</span>)<span class="kw">.</span>(<span class="wa">clk: </span>clk, <span class="wa">rst: </span>rst)</span>
<span id="cb123-13"><a href="#cb123-13" aria-hidden="true" tabindex="-1"></a> dff0<span class="at">.d</span> <span class="kw"><=</span> sig<span class="at">.sub0</span></span>
<span id="cb123-14"><a href="#cb123-14" aria-hidden="true" tabindex="-1"></a> <span class="kw">...</span></span>
<span id="cb123-15"><a href="#cb123-15" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
Expression operators
The following table gives a summary of the operators available in HDLRuby. More details are given for each group of operators in the subsequent sections.
Assignment operators (left-most operator of a statement):
symbol | description |
---|---|
:<= | connection, if outside behavior |
:<= | transmission, if inside behavior |
Arithmetic operators:
symbol | description |
---|---|
:+ | addition |
:- | subtraction |
:* | multiplication |
:/ | division |
:% | modulo |
:** | power |
:+@ | positive sign |
:-@ | negation |
Comparison operators:
symbol | description |
---|---|
:== | equality |
:!= | difference |
:> | greater than |
:< | smaller than |
:>= | greater or equal |
:<= | smaller or equal |
Logic and shift operators:
symbol | description |
---|---|
:& | bitwise / logical and |
: | |
:~ | bitwise / logical not |
:mux | multiplex |
:<< / :ls | left shift |
:>> / :rs | right shift |
:lr | left rotate |
:rr | right rotate |
Conversion operators:
symbol | description |
---|---|
:to_bit | cast to bit vector |
:to_unsigned | cast to unsigned vector |
:to_signed | cast to signed vector |
:to_big | cast to big-endian |
:to_little | cast to little endian |
:reverse | reverse the bit order |
:ljust | increase width from the left, preserves the sign |
:rjust | increase width from the right, preserves the sign |
:zext | zero extension, converts to unsigned if signed |
:sext | sign extension, converts to sign |
Selection /concatenation operators:
symbol | description |
---|---|
:[] | sub-vector selection |
:@[] | concatenation operator |
:. | field selection |
Notes:
The operator precedence is the one of Ruby.
Ruby does not allow to override the
&&
, the||
, and the?:
operators so they are not present in HDLRuby. Instead of the?:
operator, HDLRuby provides the more general multiplex operatormux
. However, HDLRuby does not provide any replacement for the&&
and the||
operators, please refer to section Logic operators for a justification for this issue.
Assignment operators
The assignment operators can be used with any type. They are the
connection and the transmission operators both being represented by
<=
.
Note:
- The first operator of a statement is necessarily an assignment
operator, while the other occurrences of
<=
represent the usualless than or equal to
operators.
Arithmetic operators
The arithmetic operators can only be used on vectors of
bit
, unsigned
or signed
values,
integer
, or float
values. These operators are
+
, -
, *
, %
and the
unary arithmetic operators are -
and +
. They
have the same meaning as their Ruby equivalents.
Comparison operators
Comparison operators are the operators whose result is either true or
false. In HDLRuby, true and false are represented by respectively
bit
value 1 and bit
value 0. These operators
are ==
, !=
, <
,
>
, <=
, >=
. They have
the same meaning as their Ruby equivalents.
Notes:
The
<
,>
,<=
and>=
operators can only be used on vectors ofbit
,unsigned
orsigned
values,integer
orfloat
values.When compared, values of a type different from the vector of
signed
and fromfloat
are considered as vectors ofunsigned
.
Logic and shift operators
In HDLRuby, the logic operators are all bitwise. For performing
Boolean computations, it is necessary to use single-bit values. The
bitwise logic binary operators are &
, |
,
and ^
, and the unary one is ~
. They have the
same meaning as their Ruby equivalents.
Note: there are two reasons why there are no Boolean operators
Ruby language does not support the redefinition of the Boolean operators
In Ruby, each value that is not
false
nornil
is assumed to be true. This is perfectly relevant for software, but not for hardware where the basic data types are bit vectors. Hence, it seemed preferable to support Boolean computation for one-bit values only, which can be done through bitwise operations.
The shift operators are <<
and
>>
and have the same meaning as their Ruby
equivalent. They do not change the bit width and preserve the sign for
signed
values.
The rotation operators are rl
and rr
for
respectively left and right bit rotations. Like the shifts, they do not
change the bit width and preserve the sign for the signed
values. However, since such operators do not exist in Ruby, they are
used like methods as follows:
<span id="cb124-1"><a href="#cb124-1" aria-hidden="true" tabindex="-1"></a><span class="kw"><</span>expression<span class="kw">></span><span class="at">.rl</span>(<span class="kw"><</span>other expression<span class="kw">></span>)</span>
<span id="cb124-2"><a href="#cb124-2" aria-hidden="true" tabindex="-1"></a><span class="kw"><</span>expression<span class="kw">></span><span class="at">.rr</span>(<span class="kw"><</span>other expression<span class="kw">></span>)</span>
For example, for rotating the left signal sig
3 times,
the following code can be used:
<span id="cb125-1"><a href="#cb125-1" aria-hidden="true" tabindex="-1"></a>sig<span class="at">.rl</span>(<span class="dv">3</span>)</span>
It is possible to perform other kinds of shifts or rotations using the selection and concatenation operators. Please refer to section Concatenation and selection operators for more details about these operators.
Conversion operators
The conversion operators are used to change the type of an expression. There are two kinds of such operators: the type pun that does not change the raw value of the expression and the type cast that changes the raw value.
The type puns include to_bit
, to_unsigned
,
and to_signed
that convert expressions of any type to
vectors of respectively bit
, unsigned
, and
signed
elements. For example, the following code converts
an expression of hierarchical type to an 8-bit signed vector:
<span id="cb126-1"><a href="#cb126-1" aria-hidden="true" tabindex="-1"></a><span class="kw">[</span> <span class="wa">up: </span>signed<span class="kw">[</span><span class="dv">3</span><span class="kw">..</span><span class="dv">0</span><span class="kw">]</span>, <span class="wa">down: </span>unsigned<span class="kw">[</span><span class="dv">3</span><span class="kw">..</span><span class="dv">0</span><span class="kw">]</span> <span class="kw">]</span><span class="at">.inner</span> <span class="wa">:sig</span></span>
<span id="cb126-2"><a href="#cb126-2" aria-hidden="true" tabindex="-1"></a>sig<span class="at">.to_bit</span> <span class="kw"><=</span> _b01010011</span>
The type casts change both the type and the value and are used to
adjust the width of the types. They can only be applied to vectors of
bit
, signed
, or unsigned
and can
only increase the bit width (bit width can be truncated using the
selection operator, please refer to the next section). These operators
comprise the bit width conversions: ljust
,
rjust
, zext
, and sext
.
More precisely, the bit width conversions operate as follows:
ljust
andrjust
increase the size from respectively the left or the right side of the bit vector. They take as an argument the width of the new type and the value (0 or 1) of the bits to add. For example, the following code increases the size ofsig0
to 12 bits by adding 1 on the right:<span id="cb127-1"><a href="#cb127-1" aria-hidden="true" tabindex="-1"></a><span class="kw">[</span><span class="dv">7</span><span class="kw">..</span><span class="dv">0</span><span class="kw">]</span><span class="at">.inner</span> <span class="wa">:sig0</span></span> <span id="cb127-2"><a href="#cb127-2" aria-hidden="true" tabindex="-1"></a><span class="kw">[</span><span class="dv">11</span><span class="kw">..</span><span class="dv">0</span><span class="kw">]</span><span class="at">.inner</span> <span class="wa">:sig1</span></span> <span id="cb127-3"><a href="#cb127-3" aria-hidden="true" tabindex="-1"></a>sig0 <span class="kw"><=</span> <span class="dv">25</span></span> <span id="cb127-4"><a href="#cb127-4" aria-hidden="true" tabindex="-1"></a>sig1 <span class="kw"><=</span> sig0<span class="at">.ljust</span>(<span class="dv">12</span>,<span class="dv">1</span>)</span>
zext
increases the size by adding several 0 bits on the most significant bit side, this side depending on the endianness of the expression. This conversion takes as an argument the width of the resulting type. For example, the following code increases the size ofsig0
to 12 bits by adding 0 on the left:<span id="cb128-1"><a href="#cb128-1" aria-hidden="true" tabindex="-1"></a>signed<span class="kw">[</span><span class="dv">7</span><span class="kw">..</span><span class="dv">0</span><span class="kw">]</span><span class="at">.inner</span> <span class="wa">:sig0</span></span> <span id="cb128-2"><a href="#cb128-2" aria-hidden="true" tabindex="-1"></a><span class="kw">[</span><span class="dv">11</span><span class="kw">..</span><span class="dv">0</span><span class="kw">]</span><span class="at">.inner</span> <span class="wa">:sig1</span></span> <span id="cb128-3"><a href="#cb128-3" aria-hidden="true" tabindex="-1"></a>sig0 <span class="kw"><=</span> <span class="kw">-</span><span class="dv">120</span></span> <span id="cb128-4"><a href="#cb128-4" aria-hidden="true" tabindex="-1"></a>sig1 <span class="kw"><=</span> sig0<span class="at">.zext</span>(<span class="dv">12</span>)</span>
sext
increases the size by duplicating the most significant bit, the side of the extension depending on the endianness of the expression. This conversion takes as an argument the width of the resulting type. For example, the following code increases the size ofsig0
to 12 bits by adding 1 on the right:<span id="cb129-1"><a href="#cb129-1" aria-hidden="true" tabindex="-1"></a>signed<span class="kw">[</span><span class="dv">0</span><span class="kw">..</span><span class="dv">7</span><span class="kw">]</span><span class="at">.inner</span> <span class="wa">:sig0</span></span> <span id="cb129-2"><a href="#cb129-2" aria-hidden="true" tabindex="-1"></a><span class="kw">[</span><span class="dv">0</span><span class="kw">..</span><span class="dv">11</span><span class="kw">]</span><span class="at">.inner</span> <span class="wa">:sig1</span></span> <span id="cb129-3"><a href="#cb129-3" aria-hidden="true" tabindex="-1"></a>sig0 <span class="kw"><=</span> <span class="kw">-</span><span class="dv">120</span></span> <span id="cb129-4"><a href="#cb129-4" aria-hidden="true" tabindex="-1"></a>sig1 <span class="kw"><=</span> sig0<span class="at">.sext</span>(<span class="dv">12</span>)</span>
Concatenation and selection operators
Concatenation and selection are done using the []
operator as follows:
when this operator takes as arguments several expressions, it concatenates them. For example, the following code concatenates
sig0
tosig1
:<span id="cb130-1"><a href="#cb130-1" aria-hidden="true" tabindex="-1"></a><span class="kw">[</span><span class="dv">3</span><span class="kw">..</span><span class="dv">0</span><span class="kw">]</span><span class="at">.inner</span> <span class="wa">:sig0</span></span> <span id="cb130-2"><a href="#cb130-2" aria-hidden="true" tabindex="-1"></a><span class="kw">[</span><span class="dv">7</span><span class="kw">..</span><span class="dv">0</span><span class="kw">]</span><span class="at">.inner</span> <span class="wa">:sig1</span></span> <span id="cb130-3"><a href="#cb130-3" aria-hidden="true" tabindex="-1"></a><span class="kw">[</span><span class="dv">11</span><span class="kw">..</span><span class="dv">0</span><span class="kw">]</span><span class="at">.inner</span> <span class="wa">:sig2</span></span> <span id="cb130-4"><a href="#cb130-4" aria-hidden="true" tabindex="-1"></a>sig0 <span class="kw"><=</span> <span class="dv">5</span></span> <span id="cb130-5"><a href="#cb130-5" aria-hidden="true" tabindex="-1"></a>sig1 <span class="kw"><=</span> <span class="dv">6</span></span> <span id="cb130-6"><a href="#cb130-6" aria-hidden="true" tabindex="-1"></a>sig2 <span class="kw"><=</span> <span class="kw">[</span>sig0, sig1<span class="kw">]</span></span>
when this operator is applied to an expression of
bit
,unsigned
, orsigned
vector type while taking as argument a range, it selects the bits corresponding to this range. If only one bit is to be selected, the offset of this bit can be used instead. For example, the following code selects bits from 3 to 1 ofsig0
and bit 4 ofsig1
:<span id="cb131-1"><a href="#cb131-1" aria-hidden="true" tabindex="-1"></a><span class="kw">[</span><span class="dv">7</span><span class="kw">..</span><span class="dv">0</span><span class="kw">]</span><span class="at">.inner</span> <span class="wa">:sig0</span></span> <span id="cb131-2"><a href="#cb131-2" aria-hidden="true" tabindex="-1"></a><span class="kw">[</span><span class="dv">7</span><span class="kw">..</span><span class="dv">0</span><span class="kw">]</span><span class="at">.inner</span> <span class="wa">:sig1</span></span> <span id="cb131-3"><a href="#cb131-3" aria-hidden="true" tabindex="-1"></a><span class="kw">[</span><span class="dv">3</span><span class="kw">..</span><span class="dv">0</span><span class="kw">]</span><span class="at">.inner</span> <span class="wa">:sig2</span></span> <span id="cb131-4"><a href="#cb131-4" aria-hidden="true" tabindex="-1"></a>bit<span class="at">.inner</span> <span class="wa">:sig3</span></span> <span id="cb131-5"><a href="#cb131-5" aria-hidden="true" tabindex="-1"></a>sig0 <span class="kw"><=</span> <span class="dv">5</span></span> <span id="cb131-6"><a href="#cb131-6" aria-hidden="true" tabindex="-1"></a>sig1 <span class="kw"><=</span> <span class="dv">6</span></span> <span id="cb131-7"><a href="#cb131-7" aria-hidden="true" tabindex="-1"></a>sig2 <span class="kw"><=</span> sig0<span class="kw">[</span><span class="dv">3</span><span class="kw">..</span><span class="dv">1</span><span class="kw">]</span></span> <span id="cb131-8"><a href="#cb131-8" aria-hidden="true" tabindex="-1"></a>sig3 <span class="kw"><=</span> sig1<span class="kw">[</span><span class="dv">4</span><span class="kw">]</span></span>
Implicit conversions
When there is no ambiguity, HDLRuby will automatically insert conversion operators when two types are not compatible with one another. The cases where such implicit conversions are applied are summarized in the following tables, where:
operator
is the operator in useresult width
is the width of the result’s typeresult base
is the base type of the result’s typeS
is the shortest operandL
is the longest operandS operand type
is the base type of the shortest operandL operand type
is the base type of the longest operandoperand conversion
is the conversions added to make the operands compatible.w
is the width of the operands after conversionlw
is the width of the left operand’s type before conversionrw
is the width of the right operand’s type before conversion
Additive and logical operators:
operator | result’s width |
---|---|
<= (assign) | w (error is raised if L.width < R.width) |
+, - | w+1 |
&, |, ^ | w |
== | 1 |
< | 1 |
> | 1 |
<= (comp.) | 1 |
>= | 1 |
S operand base | L operand base | result base | operand conversion |
---|---|---|---|
bit | bit | bit | S.zext(L.width) |
bit | unsigned | unsigned | S.zext(L.width).to_unsigned |
bit | signed | signed | S.zext(max(S.width+1,L.width).to_signed |
unsigned | bit | unsigned | S.zext(L.width), L.to_unsigned |
unsigned | unsigned | unsigned | S.zext(L.width) |
unsigned | signed | signed | S.zext(max(S.width+1,L.width).to_signed |
signed | bit | signed | S.sext(L.width+1), L.zext(L.width+1).to_signed |
signed | unsigned | signed | S.sext(L.width+1), L.zext(L.width+1).to_signed |
signed | signed | signed | S.sext(L.width) |
Multiplicative operators:
operator | result width |
---|---|
* | lw * rw |
/ | lw |
% | rw |
** | rw |
<< / ls | lw |
>> / rs | lw |
lr | lw |
rr | lw |
S operand base | L operand base | result base | operand conversion |
---|---|---|---|
bit | bit | bit | |
bit | unsigned | unsigned | S.to_unsigned |
bit | signed | signed | S.zext(S.width+1).to_signed |
unsigned | bit | unsigned | L.to_unsigned |
unsigned | unsigned | unsigned | |
unsigned | signed | signed | S.zext(S.width).to_signed |
signed | bit | signed | L.zext(L.width+1).to_signed |
signed | unsigned | signed | L.zext(L.width+1).to_signed |
signed | signed | signed |
Functions
HDLRuby functions
Like Verilog HDL, HDLRuby provides function constructs for reusing code. HDLRuby functions are declared as follows:
<span id="cb132-1"><a href="#cb132-1" aria-hidden="true" tabindex="-1"></a> hdef :<span class="kw"><</span>function name<span class="kw">></span> <span class="cf">do</span> <span class="kw">|<</span>arguments<span class="kw">>|</span></span>
<span id="cb132-2"><a href="#cb132-2" aria-hidden="true" tabindex="-1"></a> <span class="kw"><</span>code<span class="kw">></span></span>
<span id="cb132-3"><a href="#cb132-3" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span>
Where:
function name
is the name of the function.arguments
is the list of arguments of the function.code
is the code of the function.
Notes:
Functions have their scope, so any declaration within a function is local. It is also forbidden to declare interface signals (input, output, or inout) within a function.
Like the Ruby proc objects, the last statement of a function’s code serves as the return value. For instance, the following function returns
1
(in this example the function does not have any argument):<span id="cb133-1"><a href="#cb133-1" aria-hidden="true" tabindex="-1"></a>function <span class="wa">:one</span> <span class="kw">{</span> <span class="dv">1</span> <span class="kw">}</span></span>
Functions can accept any object as an argument, including variadic arguments or blocks of code as shown below with a function that applies the code passed as an argument to all the variadic arguments of
args
:<span id="cb134-1"><a href="#cb134-1" aria-hidden="true" tabindex="-1"></a>function <span class="wa">:apply</span> <span class="cf">do</span> <span class="kw">|*</span>args, <span class="kw">&</span>code<span class="kw">|</span></span> <span id="cb134-2"><a href="#cb134-2" aria-hidden="true" tabindex="-1"></a> args<span class="at">.each</span> <span class="kw">{</span> <span class="kw">|</span>arg<span class="kw">|</span> code<span class="at">.call</span>(args) <span class="kw">}</span></span> <span id="cb134-3"><a href="#cb134-3" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
Such a function can be used for example for connecting a signal to a set of other signals as follows (where
sig
is connected tox
,y
, andz
):ruby apply(x,y,z) { |v| v <= sig }
A function can be invoked anywhere in the code using its name and passing its argument between parentheses as follows:
<span id="cb135-1"><a href="#cb135-1" aria-hidden="true" tabindex="-1"></a><span class="kw"><</span>function name<span class="kw">></span>(<span class="kw"><</span>list of values<span class="kw">></span>)</span>
Ruby functions
HDLRuby functions are useful for reusing code, but they cannot
interact with the code they are called in. For example, it is not
possible to add interface signals through a function nor to modify a
control statement (e.g., hif
) with them. These high-level
generic operations can however be performed using the functions of the
Ruby language declared as follows:
<span id="cb136-1"><a href="#cb136-1" aria-hidden="true" tabindex="-1"></a><span class="cf">def</span> <span class="kw"><</span>function name<span class="kw">></span>(<span class="kw"><</span>arguments<span class="kw">></span>)</span>
<span id="cb136-2"><a href="#cb136-2" aria-hidden="true" tabindex="-1"></a> <span class="kw"><</span>code<span class="kw">></span></span>
<span id="cb136-3"><a href="#cb136-3" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
Where:
function name
is the name of the function.arguments
is the list of arguments of the function.code
is the code of the function.
These functions are called the same way HDLRuby functions are called, but this operation pastes the code of the function as is within the code. Moreover, these functions do not have any scope so any inner signal or instance declared within them will be added to the object they are invoked in.
For example, the following function will add input in0
to any system where it is invoked:
<span id="cb137-1"><a href="#cb137-1" aria-hidden="true" tabindex="-1"></a><span class="cf">def</span> add_in0</span>
<span id="cb137-2"><a href="#cb137-2" aria-hidden="true" tabindex="-1"></a> input <span class="wa">:in0</span></span>
<span id="cb137-3"><a href="#cb137-3" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
This function can be used as follows:
<span id="cb138-1"><a href="#cb138-1" aria-hidden="true" tabindex="-1"></a><span class="fu">system</span> <span class="wa">:sys</span> <span class="cf">do</span></span>
<span id="cb138-2"><a href="#cb138-2" aria-hidden="true" tabindex="-1"></a> <span class="kw">...</span></span>
<span id="cb138-3"><a href="#cb138-3" aria-hidden="true" tabindex="-1"></a> add_in0</span>
<span id="cb138-4"><a href="#cb138-4" aria-hidden="true" tabindex="-1"></a> <span class="kw">...</span></span>
<span id="cb138-5"><a href="#cb138-5" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
As another example, the following function will add an alternative
code that generates a reset to a condition statement (hif
or hcase
):
<span id="cb139-1"><a href="#cb139-1" aria-hidden="true" tabindex="-1"></a><span class="cf">def</span> too_bad</span>
<span id="cb139-2"><a href="#cb139-2" aria-hidden="true" tabindex="-1"></a> helse <span class="kw">{</span> rst <span class="kw"><=</span> <span class="dv">1</span> <span class="kw">}</span></span>
<span id="cb139-3"><a href="#cb139-3" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
This function can be used as follows:
<span id="cb140-1"><a href="#cb140-1" aria-hidden="true" tabindex="-1"></a><span class="fu">system</span> <span class="wa">:sys</span> <span class="cf">do</span></span>
<span id="cb140-2"><a href="#cb140-2" aria-hidden="true" tabindex="-1"></a> <span class="kw">...</span></span>
<span id="cb140-3"><a href="#cb140-3" aria-hidden="true" tabindex="-1"></a> par <span class="cf">do</span></span>
<span id="cb140-4"><a href="#cb140-4" aria-hidden="true" tabindex="-1"></a> hif(sig <span class="kw">==</span> <span class="dv">1</span>) <span class="cf">do</span></span>
<span id="cb140-5"><a href="#cb140-5" aria-hidden="true" tabindex="-1"></a> <span class="kw">...</span></span>
<span id="cb140-6"><a href="#cb140-6" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span>
<span id="cb140-7"><a href="#cb140-7" aria-hidden="true" tabindex="-1"></a> too_bad</span>
<span id="cb140-8"><a href="#cb140-8" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span>
<span id="cb140-9"><a href="#cb140-9" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
Ruby functions can be compared to the macros of the C languages: they are more flexible since they edit the code they are invoked in, but they are also dangerous to use. In general, it is not recommended to use them, unless when designing a library of generic code for HDLRuby.
Software code
It is possible to describe hardware-software components in HDLRuby
using the program
construct that encapsulates some software
code a provides an interface for communicating with the hardware. This
interface is composed of three kinds of components:
The activation events: 1-bit signal that triggers the execution of a given software function when the switch from 0 to 1 (or for negative ones, from 1 to 0).
The reading ports: bit-vector signals that can be read from a software function.
The writing ports: bit-vector signals that can be written from a software function.
Note:
The same signal can be used at the same time by multiple ports for both reading and writing, however, from the software point of view, it will correspond to two different ports.
Declaring a software component
A software component is declared like a hardware process within a system. The syntax is the following:
<span id="cb141-1"><a href="#cb141-1" aria-hidden="true" tabindex="-1"></a>program(<span class="kw"><</span>programming language<span class="kw">></span>, <span class="kw"><</span>function name<span class="kw">></span>) <span class="cf">do</span></span>
<span id="cb141-2"><a href="#cb141-2" aria-hidden="true" tabindex="-1"></a> <span class="kw"><</span>location of the software files <span class="cf">and</span> description of its interface<span class="kw">></span></span>
<span id="cb141-3"><a href="#cb141-3" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
In the code above, programming language
is a symbol
representing the programming language used for the software. For now,
only two languages are supported:
:ruby
: for programs in Ruby.:c
: for programs in C. However, for this case, any language that can be compiled to a shared library linkable with C is supported.
The function name
parameter indicates which function is
to be executed when an activation event occurs. There can be only one
such function per program, but any number of programs can be declared
inside the same module.
The
location of the software files and description of its interface
part can include the following declaration statements:
actport <list of events>
: for declaring the list of events that activates the program, i.e., that will trigger the execution of the program’s start function.inport <list of port names associated with a signal>
: for declaring the list of ports that the software code of the program can read.outport <list of port names associated with a signal>
: for declaring the list of ports that the software code of the program can write to.code <list of filenames>
: for declaring the source code files.
For example the following declares a program in the Ruby language
whose start function is echo
that is activated on the
positive edge of signal req
, has a read port called
inP
that is connected to signal count
and a
write port called outP
that is connected to signal
val
, finally the code of this program is given in a file
named echo.rb
:
<span id="cb142-1"><a href="#cb142-1" aria-hidden="true" tabindex="-1"></a><span class="fu">system</span> <span class="wa">:my_system</span> <span class="cf">do</span></span>
<span id="cb142-2"><a href="#cb142-2" aria-hidden="true" tabindex="-1"></a> inner <span class="wa">:req</span></span>
<span id="cb142-3"><a href="#cb142-3" aria-hidden="true" tabindex="-1"></a> <span class="kw">[</span><span class="dv">8</span><span class="kw">]</span><span class="at">.inner</span> <span class="wa">:count</span>, <span class="wa">:val</span></span>
<span id="cb142-4"><a href="#cb142-4" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb142-5"><a href="#cb142-5" aria-hidden="true" tabindex="-1"></a> <span class="kw">...</span></span>
<span id="cb142-6"><a href="#cb142-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb142-7"><a href="#cb142-7" aria-hidden="true" tabindex="-1"></a> program(<span class="wa">:ruby</span>,<span class="vs">'echo'</span>) <span class="cf">do</span></span>
<span id="cb142-8"><a href="#cb142-8" aria-hidden="true" tabindex="-1"></a> actport req<span class="at">.posedge</span></span>
<span id="cb142-9"><a href="#cb142-9" aria-hidden="true" tabindex="-1"></a> inport <span class="wa">inP: </span> count</span>
<span id="cb142-10"><a href="#cb142-10" aria-hidden="true" tabindex="-1"></a> outport <span class="wa">outP: </span>val</span>
<span id="cb142-11"><a href="#cb142-11" aria-hidden="true" tabindex="-1"></a> code <span class="st">"echo.rb"</span></span>
<span id="cb142-12"><a href="#cb142-12" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span>
<span id="cb142-13"><a href="#cb142-13" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
Note:
The size of the input and output ports is one of the signals they
give access to. However, from the software side, their values are
converted to long long
types.
About the software code used in HDLRuby programs
Location and format of the files
The file names indicating the software code to use must indicate the
path to these files relative to where the HDLRuby tools are used. In the
example above, that would mean that the echo.rb
program
must be in the same directory as the HDLRuby description. If this code
were to be into a ruby
directory under the current
directory, the declaration would become:
code "ruby/echo.rb"
. For the Ruby language, any number of
source files can be declared, and plain Ruby code can be used as is.
However, for the C language, the software code must first be compiled,
and it is the resulting file that must be declared in the code
declaration. For example, if for the example above, C had to be used,
then the program description would be the following:
<span id="cb143-1"><a href="#cb143-1" aria-hidden="true" tabindex="-1"></a> program(<span class="wa">:c</span>, <span class="wa">:echo</span>) <span class="cf">do</span></span>
<span id="cb143-2"><a href="#cb143-2" aria-hidden="true" tabindex="-1"></a> actport req<span class="at">.posedge</span></span>
<span id="cb143-3"><a href="#cb143-3" aria-hidden="true" tabindex="-1"></a> inport <span class="wa">inP: </span> count</span>
<span id="cb143-4"><a href="#cb143-4" aria-hidden="true" tabindex="-1"></a> outport <span class="wa">outP: </span>val</span>
<span id="cb143-5"><a href="#cb143-5" aria-hidden="true" tabindex="-1"></a> code <span class="st">"echo"</span></span>
<span id="cb143-6"><a href="#cb143-6" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span>
Then, for this program to work, the C code must be compiled to a file
named echo
. Please notice that, in the example, the
extension is omitted, to allow the system to look for the valid file
type (e.g., .so
for a Linux shared library).
Note:
The same software file can be used for several different program constructs, however the functions it contains will be unique for the whole device.
The hardware interface
From the software point of view, the hardware interface consists only of a list of ports that can either be read or written. However, the implementation of this interface depends on the language.
For Ruby
For ruby, the interface is accessed by requiring the
rubyHDL
library. It gives provides the RubyHDL
module that provides accessors to ports of the program. For example, the
following program reads on port inP
and writes the results
on port outP
:
<span id="cb144-1"><a href="#cb144-1" aria-hidden="true" tabindex="-1"></a><span class="fu">require</span> <span class="vs">'rubyHDL'</span></span>
<span id="cb144-2"><a href="#cb144-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb144-3"><a href="#cb144-3" aria-hidden="true" tabindex="-1"></a><span class="cf">def</span> echo</span>
<span id="cb144-4"><a href="#cb144-4" aria-hidden="true" tabindex="-1"></a> val <span class="kw">=</span> <span class="dt">RubyHDL</span><span class="at">.inP</span></span>
<span id="cb144-5"><a href="#cb144-5" aria-hidden="true" tabindex="-1"></a> <span class="dt">RubyHDL</span><span class="at">.outP</span> <span class="kw">=</span> val</span>
<span id="cb144-6"><a href="#cb144-6" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
Note:
As long as a port has been declared in the HDLRuby description of the program, it will be available to the software as a module accessor without the need for any additional declaration or configuration.
For C
For C (and C-compatible compiled languages), the interface is
accessed by including the cHDL.h
file. This file must be
generated using the following command:
<span id="cb145-1"><a href="#cb145-1" aria-hidden="true" tabindex="-1"></a><span class="ex">hdrcc</span> <span class="at">--ch</span> <span class="op"><</span>destination project<span class="op">></span></span>
In the command above, <destination project>
is the
directory where the C code is meant to be.
Once generated, this file provides the three following C functions:
void* c_get_port(const char* name)
: returns a pointer to the port whose name is passed as argument.int c_read_port(void* port)
: reads the port whose pointer is passed as argument and returns its value.int c_write_port(void* port, int val)
: write the valueval
to the port passed as argument.
For example, the following program reads on port inP
and
writes the results on port outP
:
<span id="cb146-1"><a href="#cb146-1" aria-hidden="true" tabindex="-1"></a><span class="pp">#include </span><span class="im">"cHDL.h"</span></span>
<span id="cb146-2"><a href="#cb146-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb146-3"><a href="#cb146-3" aria-hidden="true" tabindex="-1"></a><span class="dt">void</span> echo<span class="op">()</span> <span class="op">{</span></span>
<span id="cb146-4"><a href="#cb146-4" aria-hidden="true" tabindex="-1"></a> <span class="dt">void</span><span class="op">*</span> inP <span class="op">=</span> c_get_port<span class="op">(</span><span class="st">"inP"</span><span class="op">);</span></span>
<span id="cb146-5"><a href="#cb146-5" aria-hidden="true" tabindex="-1"></a> <span class="dt">void</span><span class="op">*</span> outP <span class="op">=</span> c_get_port<span class="op">(</span><span class="st">"outP"</span><span class="op">);</span></span>
<span id="cb146-6"><a href="#cb146-6" aria-hidden="true" tabindex="-1"></a> <span class="dt">int</span> val<span class="op">;</span></span>
<span id="cb146-7"><a href="#cb146-7" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb146-8"><a href="#cb146-8" aria-hidden="true" tabindex="-1"></a> val <span class="op">=</span> c_read_port<span class="op">(</span>inP<span class="op">);</span></span>
<span id="cb146-9"><a href="#cb146-9" aria-hidden="true" tabindex="-1"></a> c_write_port<span class="op">(</span>outP<span class="op">,</span>val<span class="op">);</span></span>
<span id="cb146-10"><a href="#cb146-10" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span>
Note:
The command for generating the C header file for using the HDLRuby hardware interface also generates files for helping to compile the source code. Please see compile for simulation.
Important: for windows, dynamically loaded functions must be declared with the following prefix:
__declspec(dllexport)
. If this prefix is not present before each function that is used as an HDLRuby program, the simulation will not work. For example, for Windows, the function echo must be written as follows:
<span id="cb147-1"><a href="#cb147-1" aria-hidden="true" tabindex="-1"></a><span class="pp">#include </span><span class="im">"cHDL.h"</span></span>
<span id="cb147-2"><a href="#cb147-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb147-3"><a href="#cb147-3" aria-hidden="true" tabindex="-1"></a>__declspec<span class="op">(</span>dllexport<span class="op">)</span> <span class="dt">void</span> echo<span class="op">()</span> <span class="op">{</span></span>
<span id="cb147-4"><a href="#cb147-4" aria-hidden="true" tabindex="-1"></a> <span class="dt">void</span><span class="op">*</span> inP <span class="op">=</span> c_get_port<span class="op">(</span><span class="st">"inP"</span><span class="op">);</span></span>
<span id="cb147-5"><a href="#cb147-5" aria-hidden="true" tabindex="-1"></a> <span class="dt">void</span><span class="op">*</span> outP <span class="op">=</span> c_get_port<span class="op">(</span><span class="st">"outP"</span><span class="op">);</span></span>
<span id="cb147-6"><a href="#cb147-6" aria-hidden="true" tabindex="-1"></a> <span class="dt">int</span> val<span class="op">;</span></span>
<span id="cb147-7"><a href="#cb147-7" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb147-8"><a href="#cb147-8" aria-hidden="true" tabindex="-1"></a> val <span class="op">=</span> c_read_port<span class="op">(</span>inP<span class="op">);</span></span>
<span id="cb147-9"><a href="#cb147-9" aria-hidden="true" tabindex="-1"></a> c_write_port<span class="op">(</span>outP<span class="op">,</span>val<span class="op">);</span></span>
<span id="cb147-10"><a href="#cb147-10" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span>
Hardware-software co-simulation
As long as your programs a correctly described and the software files provided (and compiled in the case of C), the hardware-software co-simulation will be automatically performed when executing the HDLRuby simulator.
Compiling the C code
While programs in Ruby can be used directly, programs in C must be
compiled first. For that purpose, the required files, including the
hardware interface cHDL.h
, must be generated. This is done
by using the following HDLRruby command:
<span id="cb148-1"><a href="#cb148-1" aria-hidden="true" tabindex="-1"></a><span class="ex">hdrcc</span> <span class="at">--ch</span> <span class="op"><</span>destination project<span class="op">></span></span>
In the command above, <destination project>
is
both the directory where the C code is and the name of the resulting
shared library.
For example, if you want to compile the code located in the directory
echo
you need first to execute:
<span id="cb149-1"><a href="#cb149-1" aria-hidden="true" tabindex="-1"></a><span class="ex">hdrcc</span> <span class="at">--ch</span> echo</span>
Then, you will have to put your C files into the resulting directory
and go inside it for compiling. If you have some specific needs for this
compiling, or if you do not want to rely on the Ruby environment, you
can compile your program there as a shared library like any other
project. For example, if you are using GCC, you could type (after
entering the echo
directory):
<span id="cb150-1"><a href="#cb150-1" aria-hidden="true" tabindex="-1"></a><span class="fu">gcc</span> <span class="at">-shared</span> <span class="at">-fPIC</span> <span class="at">-undefined</span> dynamic_lookup <span class="at">-o</span> c_program.so echo.c</span>
The command above is for compiling a single file project on a Linux system.
Otherwise, it may be easier to use the Ruby environment by first
installing rake-compiler
as follows:
<span id="cb151-1"><a href="#cb151-1" aria-hidden="true" tabindex="-1"></a><span class="ex">gem</span> install rake-compiler</span>
And simply type the following command (after entering the
echo
directory):
<span id="cb152-1"><a href="#cb152-1" aria-hidden="true" tabindex="-1"></a><span class="ex">rake</span> compile</span>
The rake tool will take care of everything for performing the compiling whatever your system may be.
Hardware generation
In the current stage, HDLRuby only generates the hardware part of a description. E.g., when generating Verilog, the programs are simply being ignored. It is therefore up to the user to provide additional code for implementing the hardware-software interface. The reason is that such interfaces are target-dependent, and often comprise licensed software and IP components that cannot be integrated into HDLruby.
This is less a limitation than it seems since it is possible to write program constructs that wrap such accesses so that the software and HDLRuby code can be used as is in the target system. As an illustration, you can consult the example given in the tutorial: 7.6. Hardware-software co-synthesis.
Extended co-simulation
Since HDLRuby programs can support any compiled software, these
components can also be used for executing any kind of application that
is not specifically meant to be executed on the target CPU. For
instance, some peripheral circuits like a keyboard or a monitor can be
modeled using an HDLRuby program, as illustrated in the HDLRuby sample
with_program_ruby_cpu.rb
.
Development board simulation graphical interface
HDLRuby provides a web browser-based GUI for the simulator as an extension of the co-design platform presented in this section. This GUI is to be declared as follows within a module:
<span id="cb153-1"><a href="#cb153-1" aria-hidden="true" tabindex="-1"></a>board(:<span class="kw"><</span>board name<span class="kw">></span>,<span class="kw"><</span>server port<span class="kw">></span>) <span class="cf">do</span></span>
<span id="cb153-2"><a href="#cb153-2" aria-hidden="true" tabindex="-1"></a> actport <span class="kw"><</span>event<span class="kw">></span></span>
<span id="cb153-3"><a href="#cb153-3" aria-hidden="true" tabindex="-1"></a> <span class="kw"><</span>description of the <span class="cn">GUI</span><span class="kw">></span></span>
<span id="cb153-4"><a href="#cb153-4" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
In the code above, board name
is the name of the board,
server port
is the local port the browser has to connect to
for accessing the GUI (by default it is set to 8000), and
event
is the event (e.g., the rising edge of a clock) that
activates the synchronization of the GUI with the simulator.
Then the description of the GUI consists of a list of the following possible development board-oriented elements. Active elements are to be given a name and attached to a HDLRuby signal as follows:
<span id="cb154-1"><a href="#cb154-1" aria-hidden="true" tabindex="-1"></a><span class="kw"><</span>element<span class="kw">></span> <span class="kw"><</span>element name<span class="kw">></span>: <span class="kw"><</span><span class="dt">HDLRuby</span> signal<span class="kw">></span></span>
The list of possible elements is as follows:
sw
: represents a set of slide switches, their number is set to match the bit-width of the attached signal.bt
: represents a set of push buttons, their number is set to match the bit-width of the attached signal.led
: represents a set of LEDs, their number is set to match the bit-width of the attached signal.hexa
: represents a hexadecimal number display, its character width is set to match the width of the largest possible value of the attached signal.digit
: represents a decimal number display, its character width is set to match the width of the largest possible positive or the smallest possible negative value of the attached signal.scope
: represents an oscilloscope display, the vertical axis represents the value of the attached signal, its range is determined by its data type, and the horizontal axis represents the time is number of synchronization of the GUI.row
: inserts a new line in the GUI.
For example, for a GUI presenting an interface to an adder with input
signals x
and y
and output signal
z
displayed as LEDs, a digit display, and an oscilloscope,
the following description can be used:
<span id="cb155-1"><a href="#cb155-1" aria-hidden="true" tabindex="-1"></a><span class="fu">system</span> <span class="wa">:adder_with_gui</span> <span class="cf">do</span></span>
<span id="cb155-2"><a href="#cb155-2" aria-hidden="true" tabindex="-1"></a> <span class="kw">[</span><span class="dv">8</span><span class="kw">]</span><span class="at">.inner</span> <span class="wa">:x</span>, <span class="wa">:y</span>, <span class="wa">:z</span></span>
<span id="cb155-3"><a href="#cb155-3" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb155-4"><a href="#cb155-4" aria-hidden="true" tabindex="-1"></a> z <span class="kw"><=</span> x <span class="kw">+</span> y</span>
<span id="cb155-5"><a href="#cb155-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb155-6"><a href="#cb155-6" aria-hidden="true" tabindex="-1"></a> inner <span class="wa">:gui_sync</span></span>
<span id="cb155-7"><a href="#cb155-7" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb155-8"><a href="#cb155-8" aria-hidden="true" tabindex="-1"></a> board(<span class="wa">:adder_gui</span>) <span class="cf">do</span></span>
<span id="cb155-9"><a href="#cb155-9" aria-hidden="true" tabindex="-1"></a> actport gui_sync<span class="at">.posedge</span></span>
<span id="cb155-10"><a href="#cb155-10" aria-hidden="true" tabindex="-1"></a> sw <span class="wa">x: </span>x</span>
<span id="cb155-11"><a href="#cb155-11" aria-hidden="true" tabindex="-1"></a> sw <span class="wa">y: </span>y</span>
<span id="cb155-12"><a href="#cb155-12" aria-hidden="true" tabindex="-1"></a> row</span>
<span id="cb155-13"><a href="#cb155-13" aria-hidden="true" tabindex="-1"></a> led <span class="wa">z_led: </span>z</span>
<span id="cb155-14"><a href="#cb155-14" aria-hidden="true" tabindex="-1"></a> digit <span class="wa">z_digit: </span>z</span>
<span id="cb155-15"><a href="#cb155-15" aria-hidden="true" tabindex="-1"></a> row</span>
<span id="cb155-16"><a href="#cb155-16" aria-hidden="true" tabindex="-1"></a> scope <span class="wa">z_scope: </span>z</span>
<span id="cb155-17"><a href="#cb155-17" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span>
<span id="cb155-18"><a href="#cb155-18" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb155-19"><a href="#cb155-19" aria-hidden="true" tabindex="-1"></a> timed <span class="cf">do</span></span>
<span id="cb155-20"><a href="#cb155-20" aria-hidden="true" tabindex="-1"></a> clk <span class="kw"><=</span> <span class="dv">0</span></span>
<span id="cb155-21"><a href="#cb155-21" aria-hidden="true" tabindex="-1"></a> repeat(<span class="dv">10000</span>) <span class="cf">do</span></span>
<span id="cb155-22"><a href="#cb155-22" aria-hidden="true" tabindex="-1"></a> !<span class="dv">10</span><span class="at">.ns</span></span>
<span id="cb155-23"><a href="#cb155-23" aria-hidden="true" tabindex="-1"></a> clk <span class="kw"><=</span> <span class="kw">~</span>clk</span>
<span id="cb155-24"><a href="#cb155-24" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span>
<span id="cb155-25"><a href="#cb155-25" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span>
<span id="cb155-26"><a href="#cb155-26" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
With the code above, the GUI will show a row containing two sets of
slide switches for input x
and y
, a row
containing a set of LEDs and a digital display for showing
z
, and a row containing an oscilloscope for showing the
evolution z
.
This code is simulated exactly like any other HDLRuby description, e.g., the following command will start the simulation and generate a VCD wave file:
<span id="cb156-1"><a href="#cb156-1" aria-hidden="true" tabindex="-1"></a><span class="ex">hdrcc</span> <span class="at">--sim</span> <span class="at">--vcd</span> my_adder.rb my_adder</span>
However, the simulator will wait until a browser connects to it. For
that, you can open a web browser, and go to the local url:
http://localhost:8000
. The simulation will then start and
you can interact with the GUI.
Time
Time values
In HDLRuby, time values can be created using the time operators:
s
for seconds, ms
for a millisecond,
us
for microseconds, ns
for nanoseconds,
ps
for picoseconds. For example, the following are all
indicating one second:
<span id="cb157-1"><a href="#cb157-1" aria-hidden="true" tabindex="-1"></a><span class="dv">1</span><span class="at">.s</span></span>
<span id="cb157-2"><a href="#cb157-2" aria-hidden="true" tabindex="-1"></a><span class="dv">1000</span><span class="at">.ms</span></span>
<span id="cb157-3"><a href="#cb157-3" aria-hidden="true" tabindex="-1"></a><span class="dv">1000000</span><span class="at">.us</span></span>
<span id="cb157-4"><a href="#cb157-4" aria-hidden="true" tabindex="-1"></a><span class="dv">1000000000</span><span class="at">.ns</span></span>
<span id="cb157-5"><a href="#cb157-5" aria-hidden="true" tabindex="-1"></a><span class="dv">1000000000000</span><span class="at">.ps</span></span>
Time behaviors and time statements
Like the other HDL, HDLRuby provides specific statements that model
the advance of time. These statements are not synthesizable and are used
for simulating the environment of a hardware component. For the sake of
clarity, such statements are only allowed in explicitly
non-synthesizable behavior declared using the timed
keyword
as follows.
<span id="cb158-1"><a href="#cb158-1" aria-hidden="true" tabindex="-1"></a>timed <span class="cf">do</span></span>
<span id="cb158-2"><a href="#cb158-2" aria-hidden="true" tabindex="-1"></a> <span class="kw"><</span>statements<span class="kw">></span></span>
<span id="cb158-3"><a href="#cb158-3" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
A time behavior does not have any sensitivity list, but it can include any statement supported by a standard behavior in addition to the time statements. There are two kinds of such statements:
The
wait
statements: such a statement blocks the execution of the behavior for the time given in the argument. For example, the following code waits for 10ns before proceeding:<span id="cb159-1"><a href="#cb159-1" aria-hidden="true" tabindex="-1"></a> wait(<span class="dv">10</span><span class="at">.ns</span>)</span>
This statement can also be abbreviated using the
!
operator as follows:<span id="cb160-1"><a href="#cb160-1" aria-hidden="true" tabindex="-1"></a> !<span class="dv">10</span><span class="at">.ns</span></span>
The
repeat
statements: such a statement takes as argument the number of iterations and a block. The execution of the block is repeated the given number of times. For example, the following code executes 10 times the inversion of theclk
signal every 10 nanoseconds:<span id="cb161-1"><a href="#cb161-1" aria-hidden="true" tabindex="-1"></a> repeat(<span class="dv">10</span>) <span class="cf">do</span> </span> <span id="cb161-2"><a href="#cb161-2" aria-hidden="true" tabindex="-1"></a> !<span class="dv">10</span><span class="at">.ns</span></span> <span id="cb161-3"><a href="#cb161-3" aria-hidden="true" tabindex="-1"></a> clk <span class="kw"><=</span> <span class="kw">~</span>clk</span> <span id="cb161-4"><a href="#cb161-4" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span>
Note:
This statement is not synthesizable and therefore can only be used in timed behaviors.
Parallel and sequential execution
Time behaviors are by default sequential, but they can include both parallel and sequential blocks. The execution semantic is the following:
A sequential block in a time behavior is executed sequentially.
A parallel block in a time behavior is executed in a semi-parallel fashion as follows:
Statements are grouped in sequence until a time statement is met.
The grouped sequences are executed in parallel.
The time statement is executed.
The subsequent statements are processed the same way.
High-level programming features
Using Ruby in HDLRuby
Since HDLRuby is pure Ruby code, the constructs of Ruby can be freely used without any compatibility issues. Moreover, this Ruby code will not interfere with the synthesizability of the design. It is then possible to define Ruby classes, methods, or modules whose execution generates constructs of HDLRuby.
Generic programming
Declaring
Declaring generic systems
Systems can be declared with generic parameters as follows:
<span id="cb162-1"><a href="#cb162-1" aria-hidden="true" tabindex="-1"></a><span class="fu">system</span> :<span class="kw"><</span><span class="fu">system</span> name<span class="kw">></span> <span class="cf">do</span> <span class="kw">|<</span>list of generic parameters<span class="kw">>|</span></span>
<span id="cb162-2"><a href="#cb162-2" aria-hidden="true" tabindex="-1"></a> <span class="kw">...</span></span>
<span id="cb162-3"><a href="#cb162-3" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
For example, the following code describes an empty system with two
generic parameters named respectively a
and
b
:
<span id="cb163-1"><a href="#cb163-1" aria-hidden="true" tabindex="-1"></a><span class="fu">system</span>(<span class="wa">:nothing</span>) <span class="kw">{</span> <span class="kw">|</span>a,b<span class="kw">|</span> <span class="kw">}</span></span>
The generic parameters can be anything: values, data types, signals,
systems, Ruby variables, and so on. For example, the following system
uses generic argument t
as a type for an input signal,
generic argument w
as a bit range for an output signal, and
generic argument s
as a system used for creating instance
sI
whose input and output signals i
and
o
are connected respectively to signals isig
and osig
.
<span id="cb164-1"><a href="#cb164-1" aria-hidden="true" tabindex="-1"></a><span class="fu">system</span> <span class="wa">:something</span> <span class="cf">do</span> <span class="kw">|</span>t,w,s<span class="kw">|</span></span>
<span id="cb164-2"><a href="#cb164-2" aria-hidden="true" tabindex="-1"></a> t<span class="at">.input</span> isig</span>
<span id="cb164-3"><a href="#cb164-3" aria-hidden="true" tabindex="-1"></a> <span class="kw">[</span>w<span class="kw">]</span><span class="at">.output</span> osig</span>
<span id="cb164-4"><a href="#cb164-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb164-5"><a href="#cb164-5" aria-hidden="true" tabindex="-1"></a> s <span class="wa">:sI</span><span class="kw">.</span>(<span class="wa">i: </span>isig, <span class="wa">o: </span>osig)</span>
<span id="cb164-6"><a href="#cb164-6" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
It is also possible to use a variable number of generic parameters
using the variadic operator *
like in the following
example. In this example, args
is an array containing an
indefinite number of parameters.
<span id="cb165-1"><a href="#cb165-1" aria-hidden="true" tabindex="-1"></a><span class="fu">system</span>(<span class="wa">:variadic</span>) <span class="kw">{</span> <span class="kw">|*</span>args<span class="kw">|</span> <span class="kw">}</span></span>
Declaring generic types
Data types can be declared with generic parameters as follows:
<span id="cb166-1"><a href="#cb166-1" aria-hidden="true" tabindex="-1"></a>typedef :<span class="kw"><</span>type name<span class="kw">></span> <span class="cf">do</span> <span class="kw">|<</span>list of generic parameters<span class="kw">>|</span></span>
<span id="cb166-2"><a href="#cb166-2" aria-hidden="true" tabindex="-1"></a> <span class="kw">...</span></span>
<span id="cb166-3"><a href="#cb166-3" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
For example, the following code describes a bit-vector type with a
generic number of bits width
:
<span id="cb167-1"><a href="#cb167-1" aria-hidden="true" tabindex="-1"></a>type(<span class="wa">:bitvec</span>) <span class="kw">{</span> <span class="kw">|</span>width<span class="kw">|</span> bit<span class="kw">[</span>width<span class="kw">]</span> <span class="kw">}</span></span>
Like with the systems, the generic parameters of types can be any kind of object, and it is also possible to use variadic arguments.
Specializing
Specializing generic systems
A generic system is specialized by invoking its name and passing as an argument the values corresponding to the generic arguments as follows:
<span id="cb168-1"><a href="#cb168-1" aria-hidden="true" tabindex="-1"></a><span class="kw"><</span><span class="fu">system</span> name<span class="kw">></span>(<span class="kw"><</span>generic argument value<span class="vs">'s list>)</span></span>
If fewer values are provided than the number of generic arguments, the system is partially specialized. However, only a fully specialized system can be instantiated.
A specialized system can also be used for inheritance. For example,
assuming system sys
has 2 generic arguments, it can be
specialized and used for building system subsys
as
follows:
<span id="cb169-1"><a href="#cb169-1" aria-hidden="true" tabindex="-1"></a><span class="fu">system</span> <span class="wa">:subsys</span>, sys(<span class="dv">1</span>,<span class="dv">2</span>) <span class="cf">do</span></span>
<span id="cb169-2"><a href="#cb169-2" aria-hidden="true" tabindex="-1"></a> <span class="kw">...</span></span>
<span id="cb169-3"><a href="#cb169-3" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
This way of inheriting can only be done with fully specialized
systems though. For partially specialized systems, include
must be used instead. For example, if sys
is specialized
with only one value, can be used in generic subsys_gen
as
follows:
<span id="cb170-1"><a href="#cb170-1" aria-hidden="true" tabindex="-1"></a><span class="fu">system</span> <span class="wa">:subsys_gen</span> <span class="cf">do</span> <span class="kw">|</span>param<span class="kw">|</span></span>
<span id="cb170-2"><a href="#cb170-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">include</span> sys(<span class="dv">1</span>,param)</span>
<span id="cb170-3"><a href="#cb170-3" aria-hidden="true" tabindex="-1"></a> <span class="kw">...</span></span>
<span id="cb170-4"><a href="#cb170-4" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
Note:
- In the example above, the generic parameter
param
ofsubsys_gen
is used for specializing systemsys
.
Specializing generic types
A generic type is specialized by invoking its name and passing as an argument the values corresponding to the generic arguments as follows:
<span id="cb171-1"><a href="#cb171-1" aria-hidden="true" tabindex="-1"></a><span class="kw"><</span>type name<span class="kw">></span>(<span class="kw"><</span>generic argument value<span class="vs">'s list>)</span></span>
If fewer values are provided than the number of generic arguments, the type is partially specialized. However, only a fully specialized type can be used for declaring signals.
Use of signals as generic parameters
Signals passed as generic arguments to systems can be used for making generic connections to the instance of the system. For that purpose, the generic argument has to be declared as input, output, or inout port in the body of the system as follows:
<span id="cb172-1"><a href="#cb172-1" aria-hidden="true" tabindex="-1"></a><span class="fu">system</span> :<span class="kw"><</span>system_name<span class="kw">></span> <span class="cf">do</span> <span class="kw">|</span>sig<span class="kw">|</span></span>
<span id="cb172-2"><a href="#cb172-2" aria-hidden="true" tabindex="-1"></a> sig<span class="at">.input</span> <span class="wa">:my_sig</span></span>
<span id="cb172-3"><a href="#cb172-3" aria-hidden="true" tabindex="-1"></a> <span class="kw">...</span></span>
<span id="cb172-4"><a href="#cb172-4" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
In the code above, sig
is a generic argument assumed to
be a signal. The second line declares the port to which sig will be
connected to when instantiating. From there, port my_sig
can be used like any other port of the system. Such a system is then
instantiated as follows:
<span id="cb173-1"><a href="#cb173-1" aria-hidden="true" tabindex="-1"></a>system_name(some_sig) :<span class="kw"><</span>instance_name<span class="kw">></span></span>
In the code above, some_sig
is a signal available in the
current context. This instantiation automatically connects
some_sig
to the instance.
Inheritance
Basics
In HDLRuby, a system can inherit from the content of one or several
other parent systems using the include
command as follows:
include <list of systems>
. Such an include can be put
anywhere in the body of a system, but the resulting content will be
accessible only after this command.
For example, the following code describes first a simple D-FF, and
then uses it to describe a FF with an additional reversed output
(qb
):
<span id="cb174-1"><a href="#cb174-1" aria-hidden="true" tabindex="-1"></a><span class="fu">system</span> <span class="wa">:dff</span> <span class="cf">do</span></span>
<span id="cb174-2"><a href="#cb174-2" aria-hidden="true" tabindex="-1"></a> input <span class="wa">:clk</span>, <span class="wa">:rst</span>, <span class="wa">:d</span></span>
<span id="cb174-3"><a href="#cb174-3" aria-hidden="true" tabindex="-1"></a> output <span class="wa">:q</span></span>
<span id="cb174-4"><a href="#cb174-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb174-5"><a href="#cb174-5" aria-hidden="true" tabindex="-1"></a> par(clk<span class="at">.posedge</span>) <span class="kw">{</span> q <span class="kw"><=</span> d <span class="kw">&</span> <span class="kw">~</span>rst <span class="kw">}</span></span>
<span id="cb174-6"><a href="#cb174-6" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
<span id="cb174-7"><a href="#cb174-7" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb174-8"><a href="#cb174-8" aria-hidden="true" tabindex="-1"></a><span class="fu">system</span> <span class="wa">:dff_full</span> <span class="cf">do</span></span>
<span id="cb174-9"><a href="#cb174-9" aria-hidden="true" tabindex="-1"></a> output <span class="wa">:qb</span></span>
<span id="cb174-10"><a href="#cb174-10" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb174-11"><a href="#cb174-11" aria-hidden="true" tabindex="-1"></a> <span class="fu">include</span> dff</span>
<span id="cb174-12"><a href="#cb174-12" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb174-13"><a href="#cb174-13" aria-hidden="true" tabindex="-1"></a> qb <span class="kw"><=</span> <span class="kw">~</span>q</span>
<span id="cb174-14"><a href="#cb174-14" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
It is also possible to declare inheritance in a more object-oriented fashion by listing the parents of a system just after declaring its name as follows:
<span id="cb175-1"><a href="#cb175-1" aria-hidden="true" tabindex="-1"></a><span class="fu">system</span> :<span class="kw"><</span>new <span class="fu">system</span> name<span class="kw">></span>, <span class="kw"><</span>list of parent systems<span class="kw">></span> <span class="cf">do</span></span>
<span id="cb175-2"><a href="#cb175-2" aria-hidden="true" tabindex="-1"></a> <span class="kw"><</span>additional <span class="fu">system</span> code<span class="kw">></span></span>
<span id="cb175-3"><a href="#cb175-3" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
For example, the following code is another to describe
dff_full
:
<span id="cb176-1"><a href="#cb176-1" aria-hidden="true" tabindex="-1"></a><span class="fu">system</span> <span class="wa">:dff_full</span>, dff <span class="cf">do</span></span>
<span id="cb176-2"><a href="#cb176-2" aria-hidden="true" tabindex="-1"></a> output <span class="wa">:qb</span></span>
<span id="cb176-3"><a href="#cb176-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb176-4"><a href="#cb176-4" aria-hidden="true" tabindex="-1"></a> qb <span class="kw"><=</span> <span class="kw">~</span>q</span>
<span id="cb176-5"><a href="#cb176-5" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
Note:
- As a matter of implementation, HDLRuby systems can be viewed as sets of methods used for accessing various constructs (signals, instances). Hence inheritance in HDLRuby is closer to the Ruby mixin mechanism than to a true software inheritance.
About inner signals and system instances
By default, inner signals and instances of a parent system are not
accessible by its child systems. They can be made accessible using the
export
keyword as follows:
export <symbol 0>, <symbol 1>, ...
. For
example, the following code exports signals clk
and
rst
and instance dff0
of system
exporter
so that they can be accessed in child system
importer
.
<span id="cb177-1"><a href="#cb177-1" aria-hidden="true" tabindex="-1"></a><span class="fu">system</span> <span class="wa">:exporter</span> <span class="cf">do</span></span>
<span id="cb177-2"><a href="#cb177-2" aria-hidden="true" tabindex="-1"></a> input <span class="wa">:d</span></span>
<span id="cb177-3"><a href="#cb177-3" aria-hidden="true" tabindex="-1"></a> inner <span class="wa">:clk</span>, <span class="wa">:rst</span></span>
<span id="cb177-4"><a href="#cb177-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb177-5"><a href="#cb177-5" aria-hidden="true" tabindex="-1"></a> dff(<span class="wa">:dff0</span>)<span class="kw">.</span>(<span class="wa">clk: </span>clk, <span class="wa">rst: </span>rst, <span class="wa">d: </span>d)</span>
<span id="cb177-6"><a href="#cb177-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb177-7"><a href="#cb177-7" aria-hidden="true" tabindex="-1"></a> export <span class="wa">:clk</span>, <span class="wa">:rst</span>, <span class="wa">:dff0</span> </span>
<span id="cb177-8"><a href="#cb177-8" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
<span id="cb177-9"><a href="#cb177-9" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb177-10"><a href="#cb177-10" aria-hidden="true" tabindex="-1"></a><span class="fu">system</span> <span class="wa">:importer</span>, exporter <span class="cf">do</span></span>
<span id="cb177-11"><a href="#cb177-11" aria-hidden="true" tabindex="-1"></a> input <span class="wa">:clk0</span>, <span class="wa">:rst0</span></span>
<span id="cb177-12"><a href="#cb177-12" aria-hidden="true" tabindex="-1"></a> output <span class="wa">:q</span></span>
<span id="cb177-13"><a href="#cb177-13" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb177-14"><a href="#cb177-14" aria-hidden="true" tabindex="-1"></a> clk <span class="kw"><=</span> clk0</span>
<span id="cb177-15"><a href="#cb177-15" aria-hidden="true" tabindex="-1"></a> rst <span class="kw"><=</span> rst0</span>
<span id="cb177-16"><a href="#cb177-16" aria-hidden="true" tabindex="-1"></a> dff0<span class="at">.q</span> <span class="kw"><=</span> q</span>
<span id="cb177-17"><a href="#cb177-17" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
Note: - export takes as arguments the symbols (or the strings) representing the name of the components to export and not a reference to them. For instance, the following code is invalid:
<span id="cb178-1"><a href="#cb178-1" aria-hidden="true" tabindex="-1"></a><span class="fu">system</span> <span class="wa">:exporter</span> <span class="cf">do</span></span>
<span id="cb178-2"><a href="#cb178-2" aria-hidden="true" tabindex="-1"></a> input <span class="wa">:d</span></span>
<span id="cb178-3"><a href="#cb178-3" aria-hidden="true" tabindex="-1"></a> inner <span class="wa">:clk</span>, <span class="wa">:rst</span></span>
<span id="cb178-4"><a href="#cb178-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb178-5"><a href="#cb178-5" aria-hidden="true" tabindex="-1"></a> dff(<span class="wa">:dff0</span>)<span class="kw">.</span>(<span class="wa">clk: </span>clk, <span class="wa">rst: </span>rst, <span class="wa">d: </span>d)</span>
<span id="cb178-6"><a href="#cb178-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb178-7"><a href="#cb178-7" aria-hidden="true" tabindex="-1"></a> export clk, rst, dff0 </span>
<span id="cb178-8"><a href="#cb178-8" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
Conflicts when inheriting
Signals and instances cannot be overridden, this is also the case for
signals and instances accessible through inheritance. For example, the
following code is invalid since rst
has already been
defined in dff
:
<span id="cb179-1"><a href="#cb179-1" aria-hidden="true" tabindex="-1"></a> <span class="fu">system</span> <span class="wa">:dff_bad</span>, dff <span class="cf">do</span></span>
<span id="cb179-2"><a href="#cb179-2" aria-hidden="true" tabindex="-1"></a> input <span class="wa">:rst</span></span>
<span id="cb179-3"><a href="#cb179-3" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span>
Conflicts among several inherited systems can be avoided by renaming the signals and instances that collide with one another as shown in the next section.
Shadowed signals and instances
It is possible in HDLRuby to declare a signal or an instance whose name is identical to the one used in one of the included systems. In such a case, the corresponding construct of the included system is still present, but it is not directly accessible even if exported, they are said to be shadowed.
To access the shadowed signals or instances, a system must be
reinterpreted as the relevant parent system using the as
operator as follows: as(system)
.
For example, in the following code signal, db
of system
dff_db
is shadowed by signal db
of system
dff_shadow
, but it is accessed using the as
operator.
<span id="cb180-1"><a href="#cb180-1" aria-hidden="true" tabindex="-1"></a><span class="fu">system</span> <span class="wa">:dff_db</span> <span class="cf">do</span></span>
<span id="cb180-2"><a href="#cb180-2" aria-hidden="true" tabindex="-1"></a> input <span class="wa">:clk</span>,<span class="wa">:rst</span>,<span class="wa">:d</span></span>
<span id="cb180-3"><a href="#cb180-3" aria-hidden="true" tabindex="-1"></a> inner <span class="wa">:db</span></span>
<span id="cb180-4"><a href="#cb180-4" aria-hidden="true" tabindex="-1"></a> output <span class="wa">:q</span></span>
<span id="cb180-5"><a href="#cb180-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb180-6"><a href="#cb180-6" aria-hidden="true" tabindex="-1"></a> db <span class="kw"><=</span> <span class="kw">~</span>d</span>
<span id="cb180-7"><a href="#cb180-7" aria-hidden="true" tabindex="-1"></a> (q <span class="kw"><=</span> d <span class="kw">&</span> <span class="kw">~</span>rst)<span class="at">.at</span>(clk<span class="at">.posedge</span>)</span>
<span id="cb180-8"><a href="#cb180-8" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
<span id="cb180-9"><a href="#cb180-9" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb180-10"><a href="#cb180-10" aria-hidden="true" tabindex="-1"></a><span class="fu">system</span> <span class="wa">:dff_shadow</span>, dff_db <span class="cf">do</span></span>
<span id="cb180-11"><a href="#cb180-11" aria-hidden="true" tabindex="-1"></a> output <span class="wa">:qb</span>, <span class="wa">:db</span></span>
<span id="cb180-12"><a href="#cb180-12" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb180-13"><a href="#cb180-13" aria-hidden="true" tabindex="-1"></a> db <span class="kw"><=</span> <span class="kw">~</span>d</span>
<span id="cb180-14"><a href="#cb180-14" aria-hidden="true" tabindex="-1"></a> qb <span class="kw"><=</span> as(dff_db)<span class="at">.db</span></span>
<span id="cb180-15"><a href="#cb180-15" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
Opening a system
It is possible to pursue the definition of a system after it has been
declared using the open
methods as follows:
<span id="cb181-1"><a href="#cb181-1" aria-hidden="true" tabindex="-1"></a><span class="kw"><</span><span class="fu">system</span><span class="kw">></span><span class="at">.open</span> <span class="cf">do</span></span>
<span id="cb181-2"><a href="#cb181-2" aria-hidden="true" tabindex="-1"></a> <span class="kw"><</span>additional <span class="fu">system</span> description<span class="kw">></span></span>
<span id="cb181-3"><a href="#cb181-3" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
For example, dff
, a system describing a D-FF, can be
modified to have an inverted output as follows:
<span id="cb182-1"><a href="#cb182-1" aria-hidden="true" tabindex="-1"></a>dff<span class="at">.open</span> <span class="cf">do</span></span>
<span id="cb182-2"><a href="#cb182-2" aria-hidden="true" tabindex="-1"></a> output <span class="wa">:qb</span></span>
<span id="cb182-3"><a href="#cb182-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb182-4"><a href="#cb182-4" aria-hidden="true" tabindex="-1"></a> qb <span class="kw"><=</span> <span class="kw">~</span>q</span>
<span id="cb182-5"><a href="#cb182-5" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
Opening an instance
When there is a modification to apply to an instance, it is sometimes preferable to modify this sole instance rather than declaring a new system to derivate the instance from. For that purpose, it is possible to open an instance for modification as follows:
<span id="cb183-1"><a href="#cb183-1" aria-hidden="true" tabindex="-1"></a><span class="kw"><</span>instance name<span class="kw">></span><span class="at">.open</span> <span class="cf">do</span></span>
<span id="cb183-2"><a href="#cb183-2" aria-hidden="true" tabindex="-1"></a> <span class="kw"><</span>additional description <span class="cf">for</span> the instance<span class="kw">></span></span>
<span id="cb183-3"><a href="#cb183-3" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
For example, an instance of the previous dff
system can
be extended with an inverted output as follows:
<span id="cb184-1"><a href="#cb184-1" aria-hidden="true" tabindex="-1"></a><span class="fu">system</span> <span class="wa">:some_system</span> <span class="cf">do</span></span>
<span id="cb184-2"><a href="#cb184-2" aria-hidden="true" tabindex="-1"></a> <span class="kw">...</span></span>
<span id="cb184-3"><a href="#cb184-3" aria-hidden="true" tabindex="-1"></a> dff <span class="wa">:dff0</span></span>
<span id="cb184-4"><a href="#cb184-4" aria-hidden="true" tabindex="-1"></a> dff0<span class="at">.open</span> <span class="cf">do</span></span>
<span id="cb184-5"><a href="#cb184-5" aria-hidden="true" tabindex="-1"></a> output <span class="wa">:qb</span></span>
<span id="cb184-6"><a href="#cb184-6" aria-hidden="true" tabindex="-1"></a> qb <span class="kw"><=</span> <span class="kw">~</span>q</span>
<span id="cb184-7"><a href="#cb184-7" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span>
<span id="cb184-8"><a href="#cb184-8" aria-hidden="true" tabindex="-1"></a> <span class="kw">...</span></span>
<span id="cb184-9"><a href="#cb184-9" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
Overloading of operators
Operators can be overloaded for specific types. This allows for instance to support seamlessly fixed-point computations without requiring explicit readjustment of the position of the decimal point.
An operator is redefined as follows:
<span id="cb185-1"><a href="#cb185-1" aria-hidden="true" tabindex="-1"></a><span class="kw"><</span>type<span class="kw">></span><span class="at">.define_operator</span>(:<span class="kw"><</span>op<span class="kw">></span>) <span class="cf">do</span> <span class="kw">|<</span>args<span class="kw">>|</span></span>
<span id="cb185-2"><a href="#cb185-2" aria-hidden="true" tabindex="-1"></a> <span class="kw"><</span>operation description<span class="kw">></span></span>
<span id="cb185-3"><a href="#cb185-3" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
Where:
type
is the type from which the operation is overloaded.op
is the operator that is overloaded (e.g.,+
)args
are the arguments of the operation.operation description
is an HDLRuby expression of the new operation.
For example, for fix32
a 32-bit (decimal point at
16-bit) fixed-point type defined as follows:
<span id="cb186-1"><a href="#cb186-1" aria-hidden="true" tabindex="-1"></a>signed<span class="kw">[</span><span class="dv">31</span><span class="kw">..</span><span class="dv">0</span><span class="kw">]</span><span class="at">.typedef</span>(<span class="wa">:fix32</span>)</span>
The multiplication operator can be overloaded as follows to ensure the decimal point have always the right position:
<span id="cb187-1"><a href="#cb187-1" aria-hidden="true" tabindex="-1"></a>fix32<span class="at">.define_operator</span>(:<span class="kw">*</span>) <span class="cf">do</span> <span class="kw">|</span>left,right<span class="kw">|</span></span>
<span id="cb187-2"><a href="#cb187-2" aria-hidden="true" tabindex="-1"></a> (left<span class="at">.as</span>(signed<span class="kw">[</span><span class="dv">31</span><span class="kw">..</span><span class="dv">0</span><span class="kw">]</span>) <span class="kw">*</span> right) <span class="kw">>></span> <span class="dv">16</span></span>
<span id="cb187-3"><a href="#cb187-3" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
Please notice, that in the code above, the left value has been cast
to a plain bit-vector to avoid the infinite recursive call of the
*
operator.
Operators can also be overloaded with generic types. However, in such a case, the generic argument must also be present in the list of arguments of the overloaded operators. For instance, let us consider the following fixed-point type of variable width (whose decimal point is set at half of its bit range):
<span id="cb188-1"><a href="#cb188-1" aria-hidden="true" tabindex="-1"></a>typedef(<span class="wa">:fixed</span>) <span class="cf">do</span> <span class="kw">|</span>width<span class="kw">|</span></span>
<span id="cb188-2"><a href="#cb188-2" aria-hidden="true" tabindex="-1"></a> signed<span class="kw">[</span>(width<span class="dv">-1</span>)<span class="kw">..</span><span class="dv">0</span><span class="kw">]</span></span>
<span id="cb188-3"><a href="#cb188-3" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
The multiplication operator would be overloaded as follows:
<span id="cb189-1"><a href="#cb189-1" aria-hidden="true" tabindex="-1"></a>fixed<span class="at">.define_operator</span> <span class="cf">do</span> <span class="kw">|</span>width,left,right<span class="kw">|</span></span>
<span id="cb189-2"><a href="#cb189-2" aria-hidden="true" tabindex="-1"></a> (left<span class="at">.as</span>(signed<span class="kw">[</span>(width<span class="dv">-1</span>)<span class="kw">..</span><span class="dv">0</span><span class="kw">]</span>) <span class="kw">*</span> right) <span class="kw">>></span> width<span class="kw">/</span><span class="dv">2</span></span>
<span id="cb189-3"><a href="#cb189-3" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
Predicate and access methods
To get information about the current state of the hardware description HDLRuby provides the following predicates:
predicate name | predicate type | predicate meaning |
---|---|---|
is_block? |
bit | tells if in execution block |
is_par? |
bit | tells if current parallel block is parallel |
is_seq? |
bit | tells if current parallel block is sequential |
is_clocked? |
bit | tells if current behavior is clocked (activated on a sole rising or falling edge of a signal) |
cur_block |
block | gets the current block |
cur_behavior |
behavior | gets the current behavior |
cur_systemT |
system | gets the current system |
top_block |
block | gets the top block of the current behavior |
one_up |
block/system | gets the upper construct (block or system) |
last_one |
any | last declared construct |
Several enumerators are also provided for accessing the internals of the current construct (in the current state):
enumerator name | accessed elements |
---|---|
each_input |
input signals of the current system |
each_output |
output signals of the current system |
each_inout |
inout signals of the current system |
each_behavior |
behaviors of the current system |
each_event |
events of the current behavior |
each_block |
blocks of the current behavior |
each_statement |
statements of the current block |
each_inner |
inner signals of the current block (or system if not within a block) |
Defining and executing Ruby methods within HDLRuby constructs
Like with any Ruby program, it is possible to define and execute methods anywhere in HDLRuby using the standard Ruby syntax. When defined, a method is attached to the enclosing HDLRuby construct. For instance, when defining a method when declaring a system, it can be used within this system only, while when defining a method outside any construct, it can be used everywhere in the HDLRuby description.
A method can include HDLRuby code in which case the resulting
hardware is appended to the current construct. For example, the
following code adds a connection between sig0
and
sig1
in the system sys0
, and transmission
between sig0
and sig1
in the behavior of
sys1
.
<span id="cb190-1"><a href="#cb190-1" aria-hidden="true" tabindex="-1"></a><span class="cf">def</span> some_arrow</span>
<span id="cb190-2"><a href="#cb190-2" aria-hidden="true" tabindex="-1"></a> sig1 <span class="kw"><=</span> sig0</span>
<span id="cb190-3"><a href="#cb190-3" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
<span id="cb190-4"><a href="#cb190-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb190-5"><a href="#cb190-5" aria-hidden="true" tabindex="-1"></a><span class="fu">system</span> <span class="wa">:sys0</span> <span class="cf">do</span></span>
<span id="cb190-6"><a href="#cb190-6" aria-hidden="true" tabindex="-1"></a> input <span class="wa">:sig0</span></span>
<span id="cb190-7"><a href="#cb190-7" aria-hidden="true" tabindex="-1"></a> output <span class="wa">:sig1</span></span>
<span id="cb190-8"><a href="#cb190-8" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb190-9"><a href="#cb190-9" aria-hidden="true" tabindex="-1"></a> some_arrow</span>
<span id="cb190-10"><a href="#cb190-10" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
<span id="cb190-11"><a href="#cb190-11" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb190-12"><a href="#cb190-12" aria-hidden="true" tabindex="-1"></a><span class="fu">system</span> <span class="wa">:sys1</span> <span class="cf">do</span></span>
<span id="cb190-13"><a href="#cb190-13" aria-hidden="true" tabindex="-1"></a> input <span class="wa">:sig0</span>, <span class="wa">:clk</span></span>
<span id="cb190-14"><a href="#cb190-14" aria-hidden="true" tabindex="-1"></a> output <span class="wa">:sig1</span></span>
<span id="cb190-15"><a href="#cb190-15" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb190-16"><a href="#cb190-16" aria-hidden="true" tabindex="-1"></a> par(clk<span class="at">.posedge</span>) <span class="cf">do</span></span>
<span id="cb190-17"><a href="#cb190-17" aria-hidden="true" tabindex="-1"></a> some_arrow</span>
<span id="cb190-18"><a href="#cb190-18" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span>
<span id="cb190-19"><a href="#cb190-19" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
Warning:
In the above example, the semantic of
some_arrow
changes depending on where it is invoked from, e.g., within a system, it is a connection, within a behavior, it is a transmission.Using Ruby methods for describing hardware might lead to weak code, for example, in the following code, the method declares
in0
as an input signal. Hence, while used insys0
no problem happens, an exception will be raised forsys1
because a signalin0
is already declared and will also be raised forsys2
because it is not possible to declare an input from within a behavior.<span id="cb191-1"><a href="#cb191-1" aria-hidden="true" tabindex="-1"></a><span class="cf">def</span> in_decl</span> <span id="cb191-2"><a href="#cb191-2" aria-hidden="true" tabindex="-1"></a> input <span class="wa">:in0</span></span> <span id="cb191-3"><a href="#cb191-3" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span> <span id="cb191-4"><a href="#cb191-4" aria-hidden="true" tabindex="-1"></a></span> <span id="cb191-5"><a href="#cb191-5" aria-hidden="true" tabindex="-1"></a><span class="fu">system</span> <span class="wa">:sys0</span> <span class="cf">do</span></span> <span id="cb191-6"><a href="#cb191-6" aria-hidden="true" tabindex="-1"></a> in_decl</span> <span id="cb191-7"><a href="#cb191-7" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span> <span id="cb191-8"><a href="#cb191-8" aria-hidden="true" tabindex="-1"></a></span> <span id="cb191-9"><a href="#cb191-9" aria-hidden="true" tabindex="-1"></a><span class="fu">system</span> <span class="wa">:sys1</span> <span class="cf">do</span></span> <span id="cb191-10"><a href="#cb191-10" aria-hidden="true" tabindex="-1"></a> input <span class="wa">:in0</span></span> <span id="cb191-11"><a href="#cb191-11" aria-hidden="true" tabindex="-1"></a> in_decl</span> <span id="cb191-12"><a href="#cb191-12" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span> <span id="cb191-13"><a href="#cb191-13" aria-hidden="true" tabindex="-1"></a></span> <span id="cb191-14"><a href="#cb191-14" aria-hidden="true" tabindex="-1"></a><span class="fu">system</span> <span class="wa">:sys2</span> <span class="cf">do</span></span> <span id="cb191-15"><a href="#cb191-15" aria-hidden="true" tabindex="-1"></a> par <span class="cf">do</span></span> <span id="cb191-16"><a href="#cb191-16" aria-hidden="true" tabindex="-1"></a> in_decl</span> <span id="cb191-17"><a href="#cb191-17" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span> <span id="cb191-18"><a href="#cb191-18" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
Like any other Ruby method, methods defined in HDLRuby support variadic arguments named arguments, and block arguments. For example, the following method can be used to connect a driver to multiple signals:
<span id="cb192-1"><a href="#cb192-1" aria-hidden="true" tabindex="-1"></a><span class="cf">def</span> mconnect(driver, <span class="kw">*</span>signals)</span>
<span id="cb192-2"><a href="#cb192-2" aria-hidden="true" tabindex="-1"></a> signals<span class="at">.each</span> <span class="cf">do</span> <span class="kw">|</span>signal<span class="kw">|</span></span>
<span id="cb192-3"><a href="#cb192-3" aria-hidden="true" tabindex="-1"></a> signal <span class="kw"><=</span> driver</span>
<span id="cb192-4"><a href="#cb192-4" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span>
<span id="cb192-5"><a href="#cb192-5" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
<span id="cb192-6"><a href="#cb192-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb192-7"><a href="#cb192-7" aria-hidden="true" tabindex="-1"></a><span class="fu">system</span> <span class="wa">:sys0</span> <span class="cf">do</span></span>
<span id="cb192-8"><a href="#cb192-8" aria-hidden="true" tabindex="-1"></a> input <span class="wa">:i0</span></span>
<span id="cb192-9"><a href="#cb192-9" aria-hidden="true" tabindex="-1"></a> input <span class="wa">:o0</span>, <span class="wa">:o1</span>, <span class="wa">:o2</span>, <span class="wa">:o3</span></span>
<span id="cb192-10"><a href="#cb192-10" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb192-11"><a href="#cb192-11" aria-hidden="true" tabindex="-1"></a> mconnect(i0,o0,o1,o2,o3)</span>
<span id="cb192-12"><a href="#cb192-12" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
While requiring caution, a properly designed method can be very useful for clean code reuse. For example, the following method allows to start the execution of a block after a given number of cycles:
<span id="cb193-1"><a href="#cb193-1" aria-hidden="true" tabindex="-1"></a><span class="cf">def</span> after(cycles, rst, <span class="kw">&</span>code)</span>
<span id="cb193-2"><a href="#cb193-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">sub</span> <span class="cf">do</span></span>
<span id="cb193-3"><a href="#cb193-3" aria-hidden="true" tabindex="-1"></a> inner <span class="wa">:count</span></span>
<span id="cb193-4"><a href="#cb193-4" aria-hidden="true" tabindex="-1"></a> hif rst <span class="kw">==</span> <span class="dv">1</span> <span class="cf">do</span></span>
<span id="cb193-5"><a href="#cb193-5" aria-hidden="true" tabindex="-1"></a> count <span class="kw"><=</span> <span class="dv">0</span></span>
<span id="cb193-6"><a href="#cb193-6" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span>
<span id="cb193-7"><a href="#cb193-7" aria-hidden="true" tabindex="-1"></a> helse <span class="cf">do</span></span>
<span id="cb193-8"><a href="#cb193-8" aria-hidden="true" tabindex="-1"></a> hif count <span class="kw"><</span> cycles <span class="cf">do</span></span>
<span id="cb193-9"><a href="#cb193-9" aria-hidden="true" tabindex="-1"></a> count <span class="kw"><=</span> count <span class="kw">+</span> <span class="dv">1</span></span>
<span id="cb193-10"><a href="#cb193-10" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span>
<span id="cb193-11"><a href="#cb193-11" aria-hidden="true" tabindex="-1"></a> helse <span class="cf">do</span></span>
<span id="cb193-12"><a href="#cb193-12" aria-hidden="true" tabindex="-1"></a> instance_eval(<span class="kw">&</span>code)</span>
<span id="cb193-13"><a href="#cb193-13" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span>
<span id="cb193-14"><a href="#cb193-14" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span>
<span id="cb193-15"><a href="#cb193-15" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span>
<span id="cb193-16"><a href="#cb193-16" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
In the code above:
sub
ensures that thecount
signal does not conflict with another signal with the same name.the
instance_eval
keyword is a standard Ruby method that executes the block passed as an argument in context.
The following is an example that switches an LED on after 1000000
clock cycles using the previously defined after
ruby
method:
<span id="cb194-1"><a href="#cb194-1" aria-hidden="true" tabindex="-1"></a><span class="fu">system</span> <span class="wa">:led_after</span> <span class="cf">do</span></span>
<span id="cb194-2"><a href="#cb194-2" aria-hidden="true" tabindex="-1"></a> output <span class="wa">:led</span></span>
<span id="cb194-3"><a href="#cb194-3" aria-hidden="true" tabindex="-1"></a> input <span class="wa">:clk</span>, <span class="wa">:rst</span></span>
<span id="cb194-4"><a href="#cb194-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb194-5"><a href="#cb194-5" aria-hidden="true" tabindex="-1"></a> par(clk<span class="at">.posedge</span>) <span class="cf">do</span></span>
<span id="cb194-6"><a href="#cb194-6" aria-hidden="true" tabindex="-1"></a> (led <span class="kw"><=</span> <span class="dv">0</span>)<span class="at">.hif</span>(rst)</span>
<span id="cb194-7"><a href="#cb194-7" aria-hidden="true" tabindex="-1"></a> after(<span class="dv">100000</span>,rst) <span class="kw">{</span> led <span class="kw"><=</span> <span class="dv">1</span> <span class="kw">}</span></span>
<span id="cb194-8"><a href="#cb194-8" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span>
<span id="cb194-9"><a href="#cb194-9" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
Note:
- Ruby’s closure still applies in HDLRuby, hence, the block sent to
after
can use the signals and instances of the current block. Moreover, the signals declared in this method will not collide with them.
Extending HDLRuby
Like any Ruby class, the constructs of HDLRuby can be dynamically extended. If it is not recommended to change their internal structure, it is possible to add methods to them for an extension.
Extending HDLRuby constructs globally
By global extension of hardware constructs, we mean the classical extension of Ruby classes by monkey patching the corresponding class. For example, it is possible to add a method giving the number of signals in the interface of a system instance as follows:
<span id="cb195-1"><a href="#cb195-1" aria-hidden="true" tabindex="-1"></a><span class="cf">class</span> <span class="dt">SystemI</span></span>
<span id="cb195-2"><a href="#cb195-2" aria-hidden="true" tabindex="-1"></a> <span class="cf">def</span> interface_size</span>
<span id="cb195-3"><a href="#cb195-3" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> each_input<span class="at">.size</span> <span class="kw">+</span> each_output<span class="at">.size</span> <span class="kw">+</span> each_inout<span class="at">.size</span></span>
<span id="cb195-4"><a href="#cb195-4" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span>
<span id="cb195-5"><a href="#cb195-5" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
From there, the method interface_size
can be used on any
system instance as follows:
<system instance>.interface_size
.
The following table gives the class of each construct of HDLRuby.
construct | class |
---|---|
data type | Type |
system | SystemT |
scope | Scope |
system instance | SystemI |
signal | Signal |
connection | Connection |
par/seq | Behavior |
timed | TimeBehavior |
event | Event |
par/seq/sub | Block |
transmit | Transmit |
hif | If |
hcase | Case |
program | Program |
Extending HDLRuby constructs locally
By local extension of a hardware construct, we mean that while the
construct will be changed, all the other constructs will remain
unchanged. This is achieved like with Ruby by accessing the Eigen class
using the singleton_class
method and extending it using the
class_eval
method. For example, with the following code,
only system dff
will respond to method
interface_size
:
<span id="cb196-1"><a href="#cb196-1" aria-hidden="true" tabindex="-1"></a>dff<span class="at">.singleton_class.class_eval</span> <span class="cf">do</span></span>
<span id="cb196-2"><a href="#cb196-2" aria-hidden="true" tabindex="-1"></a> <span class="cf">def</span> interface_size</span>
<span id="cb196-3"><a href="#cb196-3" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> each_input<span class="at">.size</span> <span class="kw">+</span> each_output<span class="at">.size</span> <span class="kw">+</span> each_inout<span class="at">.size</span></span>
<span id="cb196-4"><a href="#cb196-4" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span>
<span id="cb196-5"><a href="#cb196-5" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
It is also possible to extend locally an instance using the same
methods. For example, with the following code, only instance
dff0
will respond to method
interface_size
:
<span id="cb197-1"><a href="#cb197-1" aria-hidden="true" tabindex="-1"></a>dff <span class="wa">:dff0</span></span>
<span id="cb197-2"><a href="#cb197-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb197-3"><a href="#cb197-3" aria-hidden="true" tabindex="-1"></a>dff0<span class="at">.singleton_class.class_eval</span> <span class="cf">do</span></span>
<span id="cb197-4"><a href="#cb197-4" aria-hidden="true" tabindex="-1"></a> <span class="cf">def</span> interface_size</span>
<span id="cb197-5"><a href="#cb197-5" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> each_input<span class="at">.size</span> <span class="kw">+</span> each_output<span class="at">.size</span> <span class="kw">+</span> each_inout<span class="at">.size</span></span>
<span id="cb197-6"><a href="#cb197-6" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span>
<span id="cb197-7"><a href="#cb197-7" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
Finally, it is possible to extend locally all the instances of a
system using method singleton_instance
in place of method
singleton_class
. For example, with the following code, all
the instances of system dff
will respond to method
interface_size
:
<span id="cb198-1"><a href="#cb198-1" aria-hidden="true" tabindex="-1"></a>dff<span class="at">.singleton_instance.class_eval</span> <span class="cf">do</span></span>
<span id="cb198-2"><a href="#cb198-2" aria-hidden="true" tabindex="-1"></a> <span class="cf">def</span> interface_size</span>
<span id="cb198-3"><a href="#cb198-3" aria-hidden="true" tabindex="-1"></a> <span class="cf">return</span> each_input<span class="at">.size</span> <span class="kw">+</span> each_output<span class="at">.size</span> <span class="kw">+</span> each_inout<span class="at">.size</span></span>
<span id="cb198-4"><a href="#cb198-4" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span>
<span id="cb198-5"><a href="#cb198-5" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
Modifying the generation behavior
The main purpose of allowing global and local extensions for hardware constructs is to give the user the possibility to implement its synthesis methods. For example, one may want to implement some algorithm for a given kind of system. For that purpose, the user can define an abstract system (without any hardware content), that holds the specific algorithm as follows:
<span id="cb199-1"><a href="#cb199-1" aria-hidden="true" tabindex="-1"></a><span class="fu">system</span>(<span class="wa">:my_base</span>) <span class="kw">{}</span></span>
<span id="cb199-2"><a href="#cb199-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb199-3"><a href="#cb199-3" aria-hidden="true" tabindex="-1"></a>my_base<span class="at">.singleton_instance.class_eval</span> <span class="cf">do</span></span>
<span id="cb199-4"><a href="#cb199-4" aria-hidden="true" tabindex="-1"></a> <span class="cf">def</span> my_generation</span>
<span id="cb199-5"><a href="#cb199-5" aria-hidden="true" tabindex="-1"></a> <span class="kw"><</span>some code<span class="kw">></span></span>
<span id="cb199-6"><a href="#cb199-6" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span>
<span id="cb199-7"><a href="#cb199-7" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
Then, when this system named my_base
is included in
another system, this latter will inherit from the algorithms implemented
inside method my_generation
as shown in the following
code:
<span id="cb200-1"><a href="#cb200-1" aria-hidden="true" tabindex="-1"></a><span class="fu">system</span> <span class="wa">:some_system</span>, my_base <span class="cf">do</span></span>
<span id="cb200-2"><a href="#cb200-2" aria-hidden="true" tabindex="-1"></a> <span class="kw"><</span>some <span class="fu">system</span> description<span class="kw">></span></span>
<span id="cb200-3"><a href="#cb200-3" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
However, when generation the low-level description of this system,
code like the following will have to be written for applying
my_generation
:
<span id="cb201-1"><a href="#cb201-1" aria-hidden="true" tabindex="-1"></a>some_system <span class="wa">:instance0</span></span>
<span id="cb201-2"><a href="#cb201-2" aria-hidden="true" tabindex="-1"></a>instance0<span class="at">.my_generation</span></span>
<span id="cb201-3"><a href="#cb201-3" aria-hidden="true" tabindex="-1"></a>low <span class="kw">=</span> instance0<span class="at">.to_low</span></span>
This can be avoided by redefining the to_low
method as
follows:
<span id="cb202-1"><a href="#cb202-1" aria-hidden="true" tabindex="-1"></a><span class="fu">system</span>(<span class="wa">:my_base</span>) <span class="kw">{}</span></span>
<span id="cb202-2"><a href="#cb202-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb202-3"><a href="#cb202-3" aria-hidden="true" tabindex="-1"></a>my_base<span class="at">.singleton_instance.class_eval</span> <span class="cf">do</span></span>
<span id="cb202-4"><a href="#cb202-4" aria-hidden="true" tabindex="-1"></a> <span class="cf">def</span> my_generation</span>
<span id="cb202-5"><a href="#cb202-5" aria-hidden="true" tabindex="-1"></a> <span class="kw"><</span>some code<span class="kw">></span></span>
<span id="cb202-6"><a href="#cb202-6" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span>
<span id="cb202-7"><a href="#cb202-7" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb202-8"><a href="#cb202-8" aria-hidden="true" tabindex="-1"></a> <span class="kw">alias</span> <span class="wa">:_to_low</span> <span class="wa">:to_low</span></span>
<span id="cb202-9"><a href="#cb202-9" aria-hidden="true" tabindex="-1"></a> <span class="cf">def</span> to_low</span>
<span id="cb202-10"><a href="#cb202-10" aria-hidden="true" tabindex="-1"></a> my_generation</span>
<span id="cb202-11"><a href="#cb202-11" aria-hidden="true" tabindex="-1"></a> _to_low</span>
<span id="cb202-12"><a href="#cb202-12" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span>
<span id="cb202-13"><a href="#cb202-13" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
This way, calling directly to_low
will automatically use
my_generation
.
Standard libraries
The standard libraries are included in the module Std
.
They can be loaded as follows, where <library name>
is the name of the library:
<span id="cb203-1"><a href="#cb203-1" aria-hidden="true" tabindex="-1"></a><span class="fu">require</span> <span class="vs">'std/<library name>'</span> </span>
After the libraries are loaded, the module Std
must be
included as follows:
<span id="cb204-1"><a href="#cb204-1" aria-hidden="true" tabindex="-1"></a><span class="fu">include</span> <span class="dt">HDLRuby</span><span class="kw">::</span><span class="dt">High</span><span class="kw">::</span><span class="dt">Std</span></span>
However,
hdrcc
loads the stable components of the standard library by default, so you do not need to require nor include anything more to use them. In the current version, the stable components are the following:
std/clocks.rb
std/fixpoint.rb
std/decoder.rb
std/fsm.rb
std/sequencer.rb
std/sequencer_sync.rb
Clocks: std/clocks.rb
The clocks
library provides utilities for easier
handling of clock synchronizations.
It adds the possibility to multiply events by an integer. The result is a new event whose frequency is divided by the integer multiplicand. For example, the following code describes a D-FF that memorizes each three clock cycles.
<span id="cb205-1"><a href="#cb205-1" aria-hidden="true" tabindex="-1"></a><span class="fu">require</span> <span class="vs">'std/clocks'</span></span>
<span id="cb205-2"><a href="#cb205-2" aria-hidden="true" tabindex="-1"></a><span class="fu">include</span> <span class="dt">HDLRuby</span><span class="kw">::</span><span class="dt">High</span><span class="kw">::</span><span class="dt">Std</span></span>
<span id="cb205-3"><a href="#cb205-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb205-4"><a href="#cb205-4" aria-hidden="true" tabindex="-1"></a><span class="fu">system</span> <span class="wa">:dff_slow</span> <span class="cf">do</span></span>
<span id="cb205-5"><a href="#cb205-5" aria-hidden="true" tabindex="-1"></a> input <span class="wa">:clk</span>, <span class="wa">:rst</span></span>
<span id="cb205-6"><a href="#cb205-6" aria-hidden="true" tabindex="-1"></a> input <span class="wa">:d</span></span>
<span id="cb205-7"><a href="#cb205-7" aria-hidden="true" tabindex="-1"></a> output <span class="wa">:q</span></span>
<span id="cb205-8"><a href="#cb205-8" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb205-9"><a href="#cb205-9" aria-hidden="true" tabindex="-1"></a> ( q <span class="kw"><=</span> d <span class="kw">&</span> <span class="kw">~</span>rst )<span class="at">.at</span>(clk<span class="at">.posedge</span> <span class="kw">*</span> <span class="dv">3</span>)</span>
<span id="cb205-10"><a href="#cb205-10" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
Note: this library generates all the RTL code for the circuit handling the frequency division.
Counters:
std/counters.rb
This library provides two new constructs for implementing synthesizable wait statements.
The first construct is the after
statement that
activates a block after a given number of clock cycles is passed. Its
syntax is the following:
<span id="cb206-1"><a href="#cb206-1" aria-hidden="true" tabindex="-1"></a>after(<span class="kw"><</span>number<span class="kw">></span>,<span class="kw"><</span>clock<span class="kw">></span>,<span class="kw"><</span>reset<span class="kw">></span>)</span>
Where:
<number>
is the number of cycles to wait.<clock>
is the clock to use, this argument can be omitted.<reset>
is the signal used to reset the counter used for waiting, this argument can be omitted.
This statement can be used inside a clocked behavior where the clock event of the behavior is used for the counter unless specified otherwise.
The second construct is the before
statement that
activates a block until a given number of clock cycles is passed. Its
syntax and usage are identical to the after
statement.
Decoder: std/decoder.rb
This library provides a new set of control statements for easily describing an instruction decoder.
A decoder can be declared anywhere in the code describing a system
using the decoder
keyword as follows:
<span id="cb207-1"><a href="#cb207-1" aria-hidden="true" tabindex="-1"></a>decoder(<span class="kw"><</span>signal<span class="kw">></span>) <span class="kw"><</span>block<span class="kw">></span></span>
Where signal
is the signal to decode and
block
is a procedure block (i.e., Ruby proc
)
describing the decoding procedure. This procedure block can contain any
code supported by a standard behavior but also supports the
entry
statement that describes a pattern of a bit vector to
decode and the corresponding action to perform when the signal matches
this pattern. The syntax of the entry
statement is the
following:
<span id="cb208-1"><a href="#cb208-1" aria-hidden="true" tabindex="-1"></a>entry(<span class="kw"><</span>pattern<span class="kw">></span>) <span class="kw"><</span>block<span class="kw">></span></span>
Where pattern
is a string describing the pattern to
match the entry, and block
is a procedure block describing
the actions (some HDLRuby code) that are performed when the entry
matches. The string describing the pattern can include 0
and 1
characters for specifying a specific value for the
corresponding bit, or any alphabetical character for specifying a field
in the pattern. The fields in the pattern can then be used by name in
the block describing the action. When a letter is used several times
within a pattern, the corresponding bits are concatenated and used as a
multi-bit signal in the block.
For example, the following code describes a decoder for signal
ir
with two entries, the first one computing the sum of
fields x
and y
and assigning the result to
signal s
and the second one computing the sum of fields
x
y
and z
and assigning the
result to signal s
:
<span id="cb209-1"><a href="#cb209-1" aria-hidden="true" tabindex="-1"></a>decoder(ir) <span class="cf">do</span></span>
<span id="cb209-2"><a href="#cb209-2" aria-hidden="true" tabindex="-1"></a> entry(<span class="st">"000xx0yy"</span>) <span class="kw">{</span> s <span class="kw"><=</span> x <span class="kw">+</span> y <span class="kw">}</span></span>
<span id="cb209-3"><a href="#cb209-3" aria-hidden="true" tabindex="-1"></a> entry(<span class="st">"10zxxzyy"</span>) <span class="kw">{</span> s <span class="kw"><=</span> x <span class="kw">+</span> y <span class="kw">+</span> z <span class="kw">}</span></span>
<span id="cb209-4"><a href="#cb209-4" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
It can be noticed for field z
in the example above that
the bits are not required to be contiguous.
FSM: std/fsm.rb
This library provides a new set of control statements for easily describing a finite state machine (FSM).
A finite state machine can be declared anywhere in a system provided
it is outside a behavior using the fsm
keyword as
follows:
<span id="cb210-1"><a href="#cb210-1" aria-hidden="true" tabindex="-1"></a>fsm(<span class="kw"><</span>event<span class="kw">></span>,<span class="kw"><</span>reset<span class="kw">></span>,<span class="kw"><</span>mode<span class="kw">></span>) <span class="kw"><</span>block<span class="kw">></span></span>
Where event
is the event (rising or falling edge of a
signal) activating the state transitions, rst
is the reset
signal, mode
is the default execution mode, and
block
is the execution block describing the states of the
FSM. This last parameter can be either :sync
for
synchronous (Moore type) or :async
for asynchronous (Mealy
type).
The states of an FSM are described as follows:
<span id="cb211-1"><a href="#cb211-1" aria-hidden="true" tabindex="-1"></a><span class="kw"><</span>kind<span class="kw">></span>(<span class="kw"><</span>name<span class="kw">></span>) <span class="kw"><</span>block<span class="kw">></span></span>
Where kind
is the kind of state, name
is
the name of the state, and block
is the actions to execute
for the corresponding state. The kinds of states are the following:
- reset: the state reached when resetting the FSM. This state can be
forced to be asynchronous by setting the
name
argument to:async
and forced to be synchronous by setting thename
argument to:sync
. By default, thename
argument is to be omitted. - state: the default kind of state, will be synchronous if the FSM is synchronous or asynchronous otherwise.
- sync: the synchronous kind of state, will be synchronous whatever the kind of FSM is used.
- async: the asynchronous kind of state, will be asynchronous whatever the kind of FSM is used.
In addition, it is possible to define a default action that will be executed whatever the state is using the following statement:
<span id="cb212-1"><a href="#cb212-1" aria-hidden="true" tabindex="-1"></a>default <span class="kw"><</span>block<span class="kw">></span></span>
Where block
is the action to execute.
State transitions are by default set to be from one state to the
following in the description order. If no more transition is declared
the next one is the first declared transition. A specific transition is
defined using the goto
statement as the last statement of
the action block as follows:
<span id="cb213-1"><a href="#cb213-1" aria-hidden="true" tabindex="-1"></a>goto(<span class="kw"><</span>condition<span class="kw">></span>,<span class="kw"><</span>names<span class="kw">></span>)</span>
Where condition
is a signal whose value is used as an
index for selecting the target state among the ones specified in the
names
list. For example, the following statement indicates
to go to the state named st_a
if the cond
is
0, st_b
if the condition is 1, and st_c
if the
condition is 2, otherwise this specific transition is ignored:
<span id="cb214-1"><a href="#cb214-1" aria-hidden="true" tabindex="-1"></a>goto(cond,<span class="wa">:st_a</span>,<span class="wa">:st_b</span>,<span class="wa">:st_c</span>)</span>
Several goto statements can be used, the last one having priority provided it is taken (i.e., its condition corresponds to one of the target states). If no goto is taken, the next transition is the next declared one.
For example, the following code describes an FSM describing a circuit
that checks if two buttons (but_a
and but_b
)
are pressed and released in sequence for activating an output signal
(ok
):
<span id="cb215-1"><a href="#cb215-1" aria-hidden="true" tabindex="-1"></a>fsm(clk<span class="at">.posedge</span>,rst,<span class="wa">:sync</span>) <span class="cf">do</span></span>
<span id="cb215-2"><a href="#cb215-2" aria-hidden="true" tabindex="-1"></a> default <span class="kw">{</span> ok <span class="kw"><=</span> <span class="dv">0</span> <span class="kw">}</span></span>
<span id="cb215-3"><a href="#cb215-3" aria-hidden="true" tabindex="-1"></a> reset <span class="cf">do</span></span>
<span id="cb215-4"><a href="#cb215-4" aria-hidden="true" tabindex="-1"></a> goto(but_a, <span class="wa">:reset</span>, but_a_on)</span>
<span id="cb215-5"><a href="#cb215-5" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span>
<span id="cb215-6"><a href="#cb215-6" aria-hidden="true" tabindex="-1"></a> state(<span class="wa">:but_a_on</span>) <span class="cf">do</span></span>
<span id="cb215-7"><a href="#cb215-7" aria-hidden="true" tabindex="-1"></a> goto(but_a, <span class="wa">:but_a_off</span>, <span class="wa">:but_a_on</span>)</span>
<span id="cb215-8"><a href="#cb215-8" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span>
<span id="cb215-9"><a href="#cb215-9" aria-hidden="true" tabindex="-1"></a> state(<span class="wa">:but_a_off</span>) <span class="cf">do</span></span>
<span id="cb215-10"><a href="#cb215-10" aria-hidden="true" tabindex="-1"></a> goto(but_b, <span class="wa">:but_a_off</span>, <span class="wa">:but_b_on</span>)</span>
<span id="cb215-11"><a href="#cb215-11" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span>
<span id="cb215-12"><a href="#cb215-12" aria-hidden="true" tabindex="-1"></a> state(<span class="wa">:but_b_on</span>) <span class="cf">do</span></span>
<span id="cb215-13"><a href="#cb215-13" aria-hidden="true" tabindex="-1"></a> goto(but_b, <span class="wa">:but_b_off</span>, <span class="wa">:but_b_on</span>)</span>
<span id="cb215-14"><a href="#cb215-14" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span>
<span id="cb215-15"><a href="#cb215-15" aria-hidden="true" tabindex="-1"></a> state(<span class="wa">:but_b_off</span>) <span class="cf">do</span></span>
<span id="cb215-16"><a href="#cb215-16" aria-hidden="true" tabindex="-1"></a> ok <span class="kw"><=</span> <span class="dv">1</span></span>
<span id="cb215-17"><a href="#cb215-17" aria-hidden="true" tabindex="-1"></a> goto(<span class="wa">:but_b_off</span>)</span>
<span id="cb215-18"><a href="#cb215-18" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span>
<span id="cb215-19"><a href="#cb215-19" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
Note: the goto statements act globally, i.e., they
are independent of the place where they are declared within the state.
For example for both following statements, the next state will always be
st_a
whatever cond
may be:
<span id="cb216-1"><a href="#cb216-1" aria-hidden="true" tabindex="-1"></a> state(<span class="wa">:st_0</span>) <span class="cf">do</span></span>
<span id="cb216-2"><a href="#cb216-2" aria-hidden="true" tabindex="-1"></a> goto(<span class="wa">:st_a</span>)</span>
<span id="cb216-3"><a href="#cb216-3" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span>
<span id="cb216-4"><a href="#cb216-4" aria-hidden="true" tabindex="-1"></a> state(<span class="wa">:st_1</span>) <span class="cf">do</span></span>
<span id="cb216-5"><a href="#cb216-5" aria-hidden="true" tabindex="-1"></a> hif(cond) <span class="kw">{</span> goto(<span class="wa">:st_a</span>) <span class="kw">}</span></span>
<span id="cb216-6"><a href="#cb216-6" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span>
That is to say, for a conditional goto
for
st_1
the code should have been written as follows:
<span id="cb217-1"><a href="#cb217-1" aria-hidden="true" tabindex="-1"></a> state(<span class="wa">:st_1</span>) <span class="cf">do</span></span>
<span id="cb217-2"><a href="#cb217-2" aria-hidden="true" tabindex="-1"></a> goto(cond,<span class="wa">:st_a</span>)</span>
<span id="cb217-3"><a href="#cb217-3" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span>
The use of goto
makes the design of FSM shorter for a
majority of the cases, be sometimes, a finer control is required. For
that purpose, it is also possible to configure the FSM in
static
mode where the next_state
statement
indicates implicitly the next state. Putting in static mode is done by
passing :static
as an argument when declaring the FSM. For
example, the following FSM uses next_state
to specify
explicitly the next states depending on some condition signals
cond0
and cond1
:
<span id="cb218-1"><a href="#cb218-1" aria-hidden="true" tabindex="-1"></a>fsm(clk<span class="at">.posedge</span>,rst,<span class="wa">:static</span>)</span>
<span id="cb218-2"><a href="#cb218-2" aria-hidden="true" tabindex="-1"></a> state(<span class="wa">:st_0</span>) <span class="cf">do</span></span>
<span id="cb218-3"><a href="#cb218-3" aria-hidden="true" tabindex="-1"></a> next_state(<span class="wa">:st_1</span>)</span>
<span id="cb218-4"><a href="#cb218-4" aria-hidden="true" tabindex="-1"></a> state(<span class="wa">:st_1</span>) <span class="cf">do</span></span>
<span id="cb218-5"><a href="#cb218-5" aria-hidden="true" tabindex="-1"></a> hif(cond) <span class="kw">{</span> next_state(<span class="wa">:st_1</span>) <span class="kw">}</span></span>
<span id="cb218-6"><a href="#cb218-6" aria-hidden="true" tabindex="-1"></a> helse <span class="kw">{</span> next_state(<span class="wa">:st_0</span>) <span class="kw">}</span></span>
<span id="cb218-7"><a href="#cb218-7" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span>
<span id="cb218-8"><a href="#cb218-8" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
Sequencer
(software-like hardware coding):: std/sequencer.rb
This library provides a new set of control statements for describing the behavior of a circuit. Behind the curtain, these constructs build a finite state machine where states are deduced from the control points within the description.
A sequencer can be declared anywhere in a system provided it is
outside a behavior using the sequencer
keyword as
follows:
<span id="cb219-1"><a href="#cb219-1" aria-hidden="true" tabindex="-1"></a>sequencer(<span class="kw"><</span>clock<span class="kw">></span>,<span class="kw"><</span>start<span class="kw">></span>) <span class="kw"><</span>block<span class="kw">></span></span>
Where clock
is the clock signal advancing the execution
of the sequence, start
is the signal starting the
execution, and block
is the description of the sequence to
be executed. Both clock
and start
can also be
events (i.e., posedge
or negedge
).
A sequence is a specific case of a seq
block that
includes the following software-like additional constructs:
step
: wait until the next event (given argumentevent
of the sequencer).steps(<num>)
: performnum
timesstep
(num
can be any expression).sif(<condition>) <block>
: executesblock
ifcondition
is met.selsif(<condition>) <block>
: executesblock
if the previoussif
orselsif
condition is not met and if the currentcondition
is met.selse <block>
: executesblock
if the condition of the previoussif
statement is not met.swait(<condition>)
: waits until thatcondition
is met.swhile(<condition>) <block>
: executesblock
whilecondition
is met.sfor(<enumerable>) <block>
: executesblock
on each element ofenumerable
. This latter can be any enumerable Ruby object or any signal. If the signal is not hierarchical (e.g., bit vector), the iteration will be over each bit.sbreak
: ends current iteration.scontinue
: goes to the next step of the iteration.sterminate
: ends the execution of the sequence.
It is also possible to use enumerators (iterators) similar to the
Ruby each
using the following methods within sequences:
<object>.seach
:object
any enumerable Ruby object or any signal. If a block is given, it works likesfor
, otherwise, it returns a HDLRuby enumerator (please see enumerator for details about them).<object>.stimes
: can be used on integers and is equivalent toseach
on each integer from 0 up toobject-1
.<object>.supto(<last>)
: can be used on integers and is equivalent toseach
on each integer fromobject
up tolast
.<object>.sdownto(<last>)
: can be used on an integer and is equivalent toseach
on each integer fromobject
down tolast
.
The objects that support these methods are called enumerable objects. They include the HDLRuby signals, the HDLRuby enumerators, and all the Ruby enumerable objects (e.g., ranges, arrays).
Here are a few examples of sequencers synchronized in the positive
edge of clk
and starting when start
becomes
one. The first one computes the Fibonacci series until 100, producing a
new term in signal v
at each cycle:
<span id="cb220-1"><a href="#cb220-1" aria-hidden="true" tabindex="-1"></a><span class="fu">require</span> <span class="vs">'std/sequencer.rb'</span></span>
<span id="cb220-2"><a href="#cb220-2" aria-hidden="true" tabindex="-1"></a><span class="fu">include</span> <span class="dt">HDLRuby</span><span class="kw">::</span><span class="dt">High</span><span class="kw">::</span><span class="dt">Std</span></span>
<span id="cb220-3"><a href="#cb220-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb220-4"><a href="#cb220-4" aria-hidden="true" tabindex="-1"></a><span class="fu">system</span> <span class="wa">:a_circuit</span> <span class="cf">do</span></span>
<span id="cb220-5"><a href="#cb220-5" aria-hidden="true" tabindex="-1"></a> inner <span class="wa">:clk</span>, <span class="wa">:start</span></span>
<span id="cb220-6"><a href="#cb220-6" aria-hidden="true" tabindex="-1"></a> <span class="kw">[</span><span class="dv">16</span><span class="kw">]</span><span class="at">.inner</span> <span class="wa">:a</span>, <span class="wa">:b</span></span>
<span id="cb220-7"><a href="#cb220-7" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb220-8"><a href="#cb220-8" aria-hidden="true" tabindex="-1"></a> sequencer(clk<span class="at">.posedge</span>,start) <span class="cf">do</span></span>
<span id="cb220-9"><a href="#cb220-9" aria-hidden="true" tabindex="-1"></a> a <span class="kw"><=</span> <span class="dv">0</span></span>
<span id="cb220-10"><a href="#cb220-10" aria-hidden="true" tabindex="-1"></a> b <span class="kw"><=</span> <span class="dv">1</span></span>
<span id="cb220-11"><a href="#cb220-11" aria-hidden="true" tabindex="-1"></a> swhile(v <span class="kw"><</span> <span class="dv">100</span>) <span class="cf">do</span></span>
<span id="cb220-12"><a href="#cb220-12" aria-hidden="true" tabindex="-1"></a> b <span class="kw"><=</span> a <span class="kw">+</span> b</span>
<span id="cb220-13"><a href="#cb220-13" aria-hidden="true" tabindex="-1"></a> a <span class="kw"><=</span> b <span class="kw">-</span> a</span>
<span id="cb220-14"><a href="#cb220-14" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span>
<span id="cb220-15"><a href="#cb220-15" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span>
<span id="cb220-16"><a href="#cb220-16" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
The second one computes the square of the integers from 10 to 100,
producing one result per cycle in signal a
:
<span id="cb221-1"><a href="#cb221-1" aria-hidden="true" tabindex="-1"></a>inner <span class="wa">:clk</span>, <span class="wa">:start</span></span>
<span id="cb221-2"><a href="#cb221-2" aria-hidden="true" tabindex="-1"></a><span class="kw">[</span><span class="dv">16</span><span class="kw">]</span><span class="at">.inner</span> <span class="wa">:a</span></span>
<span id="cb221-3"><a href="#cb221-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb221-4"><a href="#cb221-4" aria-hidden="true" tabindex="-1"></a>sequencer(clk<span class="at">.posedge</span>,start) <span class="cf">do</span></span>
<span id="cb221-5"><a href="#cb221-5" aria-hidden="true" tabindex="-1"></a> <span class="dv">10</span><span class="at">.supto</span>(<span class="dv">100</span>) <span class="kw">{</span> <span class="kw">|</span>i<span class="kw">|</span> a <span class="kw"><=</span> i<span class="kw">*</span>i <span class="kw">}</span></span>
<span id="cb221-6"><a href="#cb221-6" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
The third one reverses the content of memory mem
(the
result will be “!dlrow olleH”):
<span id="cb222-1"><a href="#cb222-1" aria-hidden="true" tabindex="-1"></a>inner <span class="wa">:clk</span>, <span class="wa">:start</span></span>
<span id="cb222-2"><a href="#cb222-2" aria-hidden="true" tabindex="-1"></a>bit<span class="kw">[</span><span class="dv">8</span><span class="kw">][-</span><span class="dv">12</span><span class="kw">]</span><span class="at">.inner</span> <span class="wa">mem: </span><span class="st">"Hello world!"</span></span>
<span id="cb222-3"><a href="#cb222-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb222-4"><a href="#cb222-4" aria-hidden="true" tabindex="-1"></a>sequencer(clk<span class="at">.posedge</span>,start) <span class="cf">do</span></span>
<span id="cb222-5"><a href="#cb222-5" aria-hidden="true" tabindex="-1"></a> mem<span class="at">.size.stimes</span> <span class="cf">do</span> <span class="kw">|</span>i<span class="kw">|</span></span>
<span id="cb222-6"><a href="#cb222-6" aria-hidden="true" tabindex="-1"></a> <span class="kw">[</span><span class="dv">8</span><span class="kw">]</span><span class="at">.inner</span> <span class="wa">:tmp</span></span>
<span id="cb222-7"><a href="#cb222-7" aria-hidden="true" tabindex="-1"></a> tmp <span class="kw"><=</span> mem<span class="kw">[</span>i<span class="kw">]</span></span>
<span id="cb222-8"><a href="#cb222-8" aria-hidden="true" tabindex="-1"></a> mem<span class="kw">[</span>i<span class="kw">]</span> <span class="kw"><=</span> mem<span class="kw">[-</span>i<span class="dv">-1</span><span class="kw">]</span></span>
<span id="cb222-9"><a href="#cb222-9" aria-hidden="true" tabindex="-1"></a> mem<span class="kw">[-</span>i<span class="dv">-1</span><span class="kw">]</span> <span class="kw"><=</span> tmp</span>
<span id="cb222-10"><a href="#cb222-10" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span>
<span id="cb222-11"><a href="#cb222-11" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
The fourth one computes the sum of all the elements of memory
mem
but stops if the sum is larger than 16:
<span id="cb223-1"><a href="#cb223-1" aria-hidden="true" tabindex="-1"></a>inner <span class="wa">:clk</span>, <span class="wa">:start</span></span>
<span id="cb223-2"><a href="#cb223-2" aria-hidden="true" tabindex="-1"></a>bit<span class="kw">[</span><span class="dv">8</span><span class="kw">][-</span><span class="dv">8</span><span class="kw">]</span><span class="at">.inner</span> <span class="wa">mem: </span><span class="kw">[</span> _h02, _h04, _h06, _h08, _h0A, _h0C, _h0E <span class="kw">]</span></span>
<span id="cb223-3"><a href="#cb223-3" aria-hidden="true" tabindex="-1"></a>bit<span class="kw">[</span><span class="dv">8</span><span class="kw">]</span> <span class="wa">:sum</span></span>
<span id="cb223-4"><a href="#cb223-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb223-5"><a href="#cb223-5" aria-hidden="true" tabindex="-1"></a>sequencer(clk<span class="at">.posedge</span>,start) <span class="cf">do</span></span>
<span id="cb223-6"><a href="#cb223-6" aria-hidden="true" tabindex="-1"></a> sum <span class="kw"><=</span> <span class="dv">0</span></span>
<span id="cb223-7"><a href="#cb223-7" aria-hidden="true" tabindex="-1"></a> sfor(mem) <span class="cf">do</span> <span class="kw">|</span>elem<span class="kw">|</span></span>
<span id="cb223-8"><a href="#cb223-8" aria-hidden="true" tabindex="-1"></a> sum <span class="kw"><=</span> sum <span class="kw">+</span> elem</span>
<span id="cb223-9"><a href="#cb223-9" aria-hidden="true" tabindex="-1"></a> sif(sum <span class="kw">></span> <span class="dv">16</span>) <span class="kw">{</span> sterminate <span class="kw">}</span></span>
<span id="cb223-10"><a href="#cb223-10" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span>
<span id="cb223-11"><a href="#cb223-11" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
HDLRuby
enumerators and enumerable objects: std/sequencer.rb
HDLRuby enumerators are objects for generating iterations within
sequencers. They are created using the method seach
on
enumerable objects as presented in the previous section.
The enumerators can be controlled using the following methods:
size
: returns the number of elements the enumerator can access.type
: returns the type of the elements accessed by the enumerator.seach
: returns the current enumerator. If a block is given, it performs the iteration instead of returning an enumerator.seach_with_index
: returns an enumerator over the elements of the current enumerator associated with their index position. If a block is given, it performs the iteration instead of returning an enumerator.seach_with_object(<obj>)
: returns an enumerator over the elements of the current enumerator associated with objectobj
(any object, HDLRuby or not, can be used). If a block is given, it performs the iteration instead of returning an enumerator.with_index
: identical toseach_with_index
.with_object(<obj>)
: identical toseach_with_object
.clone
: create a new enumerator on the same elements.speek
: returns the current element pointed by the enumerator without advancing it.snext
: returns the current element pointed by the enumerator and goes to the next one.srewind
: restart the enumeration.+
: concatenation of enumerators.
It is also possible to define a custom enumerator using the following command:
<span id="cb224-1"><a href="#cb224-1" aria-hidden="true" tabindex="-1"></a><span class="kw"><</span>enum<span class="kw">></span> <span class="kw">=</span> senumerator(<span class="kw"><</span>typ<span class="kw">></span>,<span class="kw"><</span>size<span class="kw">></span>) <span class="kw"><</span>block<span class="kw">></span></span>
Where enum
is a Ruby variable referring to the
enumerator, typ
is the data type, size
is the
number of the elements to enumerate, and block
is the block
that implements the access to an element by index. For example, an
enumerator on a memory could be defined as follows:
<span id="cb225-1"><a href="#cb225-1" aria-hidden="true" tabindex="-1"></a> bit<span class="kw">[</span><span class="dv">8</span><span class="kw">][-</span><span class="dv">8</span><span class="kw">]</span><span class="at">.inner</span> <span class="wa">mem: </span><span class="kw">[</span> _h01, _h02, _h03, _h04, _h30, _h30, _h30, _h30 <span class="kw">]</span></span>
<span id="cb225-2"><a href="#cb225-2" aria-hidden="true" tabindex="-1"></a> <span class="kw">[</span><span class="dv">3</span><span class="kw">]</span><span class="at">.inner</span> <span class="wa">:addr</span></span>
<span id="cb225-3"><a href="#cb225-3" aria-hidden="true" tabindex="-1"></a> <span class="kw">[</span><span class="dv">8</span><span class="kw">]</span><span class="at">.inner</span> <span class="wa">:data</span></span>
<span id="cb225-4"><a href="#cb225-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb225-5"><a href="#cb225-5" aria-hidden="true" tabindex="-1"></a> data <span class="kw"><=</span> mem<span class="kw">[</span>addr<span class="kw">]</span></span>
<span id="cb225-6"><a href="#cb225-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb225-7"><a href="#cb225-7" aria-hidden="true" tabindex="-1"></a> mem_enum <span class="kw">=</span> senumerator(bit<span class="kw">[</span><span class="dv">8</span><span class="kw">]</span>,<span class="dv">8</span>) <span class="cf">do</span> <span class="kw">|</span>i<span class="kw">|</span></span>
<span id="cb225-8"><a href="#cb225-8" aria-hidden="true" tabindex="-1"></a> addr <span class="kw"><=</span> i</span>
<span id="cb225-9"><a href="#cb225-9" aria-hidden="true" tabindex="-1"></a> step</span>
<span id="cb225-10"><a href="#cb225-10" aria-hidden="true" tabindex="-1"></a> data</span>
<span id="cb225-11"><a href="#cb225-11" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span>
In the code above, mem_enum
is the variable referring to
the resulting enumerator built for accessing memory mem
.
For the access, it is assumed that one cycle must be waited for after
the address is set, and therefore a step
command is added
in the access procedure before data
can be returned.
With this basis, several algorithms have been implemented using
enumerators and are usable for all the enumerable objects. All these
algorithms are HW implantation of the Ruby Enumerable methods. They are
accessible using the corresponding ruby method prefixed by character
s
. For example, the HW implementation of the ruby
all?
method is generated by the sall?
method.
In details:
sall?
: HW implementation of the Rubyall?
method. Returns a single-bit signal. When 0 this value means false and when 1 it means true.sany?
: HW implementation of the Rubyany?
method. Returns a single-bit signal. When 0 this value means false and when 1 it means true.schain
: HW implementation of the Rubychain
.smap
: HW implementation of the Rubymap
method. When used with a block returns a vector signal containing each computation result.scompact
: HW implementation of the Rubycompact
method. However, since there is no nil value in HW, use 0 instead for compacting. Returns a vector signal containing the compaction result.scount
: HW implementation of the Rubycount
method. Returns a signal whose bit width matches the size of the enumerator containing the count result.scycle
: HW implementation of the Rubycycle
method.sfind
: HW implementation of the Rubyfind
method. Returns a signal containing the found element, or 0 if not found.sdrop
: HW implementation of the Rubydrop
method. Returns a vector signal containing the remaining elements.sdrop_while
: HW implementation of the Rubydrop_while
method. Returns a vector signal containing the remaining elements.seach_cons
: HW implementation of the Rubyeach_cons
method.seach_slice
: HW implementation of the Rubyeach_slice
method.seach_with_index
: HW implementation of the Rubyeach_with_index
method.seach_with_object
: HW implementation of the Rubyeach_with_object
method.sto_a
: HW implementation of the Rubyto_a
method. Returns a vector signal containing all the elements of the enumerator.sselect
: HW implementation of the Rubyselect
method. Returns a vector signal containing the selected elements.sfind_index
: HW implementation of the Rubyfind_index
method. Returns the index of the found element or -1 if not.sfirst
: HW implementation of the Rubyfirst
method. Returns a vector signal containing the first elements.sinclude?
: HW implementation of the Rubyinclude?
method. Returns a single-bit signal. When 0 this value means false and when 1 it means true.sinject
: HW implementation of the Rubyinject
method. Return a signal of the type of elements containing the computation result.smax
: HW implementation of the Rubymax
method. Return a vector signal containing the found max values.smax_by
: HW implementation of the Rubymax_by
method. Return a vector signal containing the found max values.smin
: HW implementation of the Rubymin
method. Return a vector signal containing the found min values.smin_by
: HW implementation of the Rubymin_by
method. Return a vector signal containing the found min values.sminmax
: HW implementation of the Rubyminmax
method. Returns a 2-element vector signal containing the resulting min and max values.sminmax_by
: HW implementation of the Rubyminmax_by
method. Returns a 2-element vector signal containing the resulting min and max values.snone?
: HW implementation of the Rubynone?
method. Returns a single-bit signal. When 0 this value means false and when 1 it means true.sone?
: HW implementation of the Rubyone?
method. Returns a single-bit signal. When 0 this value means false and when 1 it means true.sreject
: HW implementation of the Rubyreject
method. Returns a vector signal containing the remaining elements.sreverse_each
: HW implementation of the Rubyreverse_each
method.ssort
: HW implementation of the Rubysort
method. Returns a vector signal containing the sorted elements.ssort_by
: HW implementation of the Rubysort_by
method. Returns a vector signal containing the sorted elements.ssum
: HW implementation of the Rubysum
method. Returns a signal with the type of elements containing the sum result.stake
: HW implementation of the Rubytake
method. Returns a vector signal containing the taken elements.stake_while
: HW implementation of the Rubytake_while
method. Returns a vector signal containing the taken elements.suniq
: HW implementation the Rubyuniq
method. Returns a vector signal containing the selected elements.
Shared
signals, arbiters, and monitors: std/sequencer_sync.rb
Shared signals
Like any other process, several sequencers can’t write to the same signal. This is because there would be race competition that may physically destroy the device if such operations were authorized. In standard RTL design, this limitation is overcome by implementing three-state buses, multiplexers, and arbiters. However, HDLRuby sequencers support another kind of signal called the shared signals that abstract the implementation details for avoiding race competition.
The shared signals are declared like the other kind of signals from their type. The syntax is the following:
<span id="cb226-1"><a href="#cb226-1" aria-hidden="true" tabindex="-1"></a><span class="kw"><</span>type<span class="kw">></span><span class="at">.shared</span> <span class="kw"><</span>list of names<span class="kw">></span></span>
They can also have an initial (and default) value when declared as follows:
<span id="cb227-1"><a href="#cb227-1" aria-hidden="true" tabindex="-1"></a><span class="kw"><</span>type<span class="kw">></span><span class="at">.shared</span> <span class="kw"><</span>list of names with initialization<span class="kw">></span></span>
For example, the following code declares two 8-bit shared signals
x
and y
, and two signed 16-bit shared signals
initialized to 0 u
and v
:
<span id="cb228-1"><a href="#cb228-1" aria-hidden="true" tabindex="-1"></a><span class="kw">[</span><span class="dv">8</span><span class="kw">]</span><span class="at">.shared</span> <span class="wa">:x</span>, <span class="wa">:y</span></span>
<span id="cb228-2"><a href="#cb228-2" aria-hidden="true" tabindex="-1"></a>signed<span class="kw">[</span><span class="dv">8</span><span class="kw">]</span><span class="at">.shared</span> <span class="wa">u: </span><span class="dv">0</span>, <span class="wa">v: </span><span class="dv">0</span></span>
A shared signal can then be read and written to by any sequencer from anywhere in the subsequent code of the current scope. However, they cannot be written outside a sequencer. For example, the following code is valid:
<span id="cb229-1"><a href="#cb229-1" aria-hidden="true" tabindex="-1"></a>input <span class="wa">:clk</span>, <span class="wa">:start</span></span>
<span id="cb229-2"><a href="#cb229-2" aria-hidden="true" tabindex="-1"></a><span class="kw">[</span><span class="dv">8</span><span class="kw">]</span><span class="at">.inner</span> <span class="wa">:val0</span>, <span class="wa">:val1</span></span>
<span id="cb229-3"><a href="#cb229-3" aria-hidden="true" tabindex="-1"></a><span class="kw">[</span><span class="dv">8</span><span class="kw">]</span><span class="at">.shared</span> <span class="wa">:x</span>, <span class="wa">:y</span></span>
<span id="cb229-4"><a href="#cb229-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb229-5"><a href="#cb229-5" aria-hidden="true" tabindex="-1"></a>val0 <span class="kw"><=</span> x<span class="kw">+</span>y</span>
<span id="cb229-6"><a href="#cb229-6" aria-hidden="true" tabindex="-1"></a>par(clk<span class="at">.posedge</span>) <span class="kw">{</span> val1 <span class="kw"><=</span> x<span class="kw">+</span>y <span class="kw">}</span></span>
<span id="cb229-7"><a href="#cb229-7" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb229-8"><a href="#cb229-8" aria-hidden="true" tabindex="-1"></a>sequencer(clk<span class="at">.posedge</span>,start) <span class="cf">do</span></span>
<span id="cb229-9"><a href="#cb229-9" aria-hidden="true" tabindex="-1"></a> <span class="dv">10</span><span class="at">.stimes</span> <span class="kw">{</span> <span class="kw">|</span>i<span class="kw">|</span> x <span class="kw"><=</span> i <span class="kw">}</span></span>
<span id="cb229-10"><a href="#cb229-10" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
<span id="cb229-11"><a href="#cb229-11" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb229-12"><a href="#cb229-12" aria-hidden="true" tabindex="-1"></a>sequencer(clk<span class="at">.posedge</span>,start) <span class="cf">do</span></span>
<span id="cb229-13"><a href="#cb229-13" aria-hidden="true" tabindex="-1"></a> <span class="dv">5</span><span class="at">.stimes</span> <span class="kw">{</span> <span class="kw">|</span>i<span class="kw">|</span> x <span class="kw"><=</span> i<span class="kw">*</span><span class="dv">2</span> ; y <span class="kw"><=</span> i<span class="kw">*</span><span class="dv">2</span> <span class="kw">}</span></span>
<span id="cb229-14"><a href="#cb229-14" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
But the following code is not valid:
<span id="cb230-1"><a href="#cb230-1" aria-hidden="true" tabindex="-1"></a><span class="kw">[</span><span class="dv">8</span><span class="kw">]</span><span class="at">.shared</span> <span class="wa">w: </span><span class="dv">0</span></span>
<span id="cb230-2"><a href="#cb230-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb230-3"><a href="#cb230-3" aria-hidden="true" tabindex="-1"></a>par(clk<span class="at">.posedge</span>) <span class="kw">{</span> w <span class="kw"><=</span> w <span class="kw">+</span> <span class="dv">1</span> <span class="kw">}</span></span>
By default, a shared signal acknowledges writing from the first
sequencer that accesses it in order of declaration, the others are
ignored. In the first example given above, that means for signal
x
the value is always the one written by the first
sequencer, i.e., from 0 to 9 changing once per clock cycle. However, the
value of signal y
is set by the second sequencer since it
is this one only that writes to this signal.
This default behavior of shared signal avoids race competition but is not very useful in practice. For better control, it is possible to select which sequencer is to be acknowledged for writing. This is done by setting the number of the sequencer which can write the signal that controls the sharing accessed as follows:
<span id="cb231-1"><a href="#cb231-1" aria-hidden="true" tabindex="-1"></a><span class="kw"><</span>shared signal<span class="kw">></span><span class="at">.select</span></span>
The select value starts from 0 for the first sequencer writing to the
shared signal and is increased by one per writing sequencer. For
example, in the first example, for selecting the second sequencer for
writing to x
the following code can be added after this
signal is declared:
<span id="cb232-1"><a href="#cb232-1" aria-hidden="true" tabindex="-1"></a> x<span class="at">.select</span> <span class="kw"><=</span> <span class="dv">1</span></span>
This value can be changed at runtime too. For example, instead of setting the second sequencer, it is possible to switch the sequencer every clock cycle as follows:
<span id="cb233-1"><a href="#cb233-1" aria-hidden="true" tabindex="-1"></a> par(clk<span class="at">.posedge</span>) <span class="kw">{</span> x<span class="at">.select</span> <span class="kw"><=</span> x<span class="at">.select</span> <span class="kw">+</span> <span class="dv">1</span> <span class="kw">}</span></span>
Note: this select sub-signal is a standard RTL signal that has the same properties and limitations as the other ones, i.e., this is not a shared signal itself.
Arbiters
Usually, it is not the signals that we want to share, but the
resources they drive. For example, with a CPU, it is the ALU that is
shared as a whole rather than each of its inputs separately. To support
such cases and ease the handling of shared signals, the library also
provides the arbiter components. This component is instantiated
like a standard module as follows, where name
is the name
of the arbiter instance:
<span id="cb234-1"><a href="#cb234-1" aria-hidden="true" tabindex="-1"></a>arbiter(:<span class="kw"><</span>name<span class="kw">></span>)<span class="kw">.</span>(<span class="kw"><</span>list of shared signal<span class="kw">></span>)</span>
When instantiated, an arbiter will take control of the select sub-signals of the shared signals (hence, you cannot control the selection yourself for them any longer). In return, it provides the possibility of requiring or releasing access to the shared signals. Requiring access is done by sending the value 1 to the arbiter, and releasing is done by sending the value 0. If a sequencer writes to a shared signal under arbitration without requiring access first, the write will simply be ignored.
The following is an example of an arbiter that controls access to
shared signals x
and y
and two sequencers
acquiring and releasing accesses to them:
<span id="cb235-1"><a href="#cb235-1" aria-hidden="true" tabindex="-1"></a>input <span class="wa">:clk</span>, <span class="wa">:start</span></span>
<span id="cb235-2"><a href="#cb235-2" aria-hidden="true" tabindex="-1"></a><span class="kw">[</span><span class="dv">8</span><span class="kw">]</span><span class="at">.shared</span> x, y</span>
<span id="cb235-3"><a href="#cb235-3" aria-hidden="true" tabindex="-1"></a>arbiter(<span class="wa">:ctrl_xy</span>)<span class="kw">.</span>(x,y)</span>
<span id="cb235-4"><a href="#cb235-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb235-5"><a href="#cb235-5" aria-hidden="true" tabindex="-1"></a>sequencer(clk<span class="at">.posedge</span>,start) <span class="cf">do</span></span>
<span id="cb235-6"><a href="#cb235-6" aria-hidden="true" tabindex="-1"></a> ctrl_xy <span class="kw"><=</span> <span class="dv">1</span></span>
<span id="cb235-7"><a href="#cb235-7" aria-hidden="true" tabindex="-1"></a> x <span class="kw"><=</span> <span class="dv">0</span> ; y <span class="kw"><=</span> <span class="dv">0</span></span>
<span id="cb235-8"><a href="#cb235-8" aria-hidden="true" tabindex="-1"></a> <span class="dv">5</span><span class="at">.stime</span> <span class="cf">do</span> <span class="kw">|</span>i<span class="kw">|</span></span>
<span id="cb235-9"><a href="#cb235-9" aria-hidden="true" tabindex="-1"></a> x <span class="kw"><=</span> x <span class="kw">+</span> <span class="dv">1</span></span>
<span id="cb235-10"><a href="#cb235-10" aria-hidden="true" tabindex="-1"></a> y <span class="kw"><=</span> y <span class="kw">+</span> <span class="dv">2</span></span>
<span id="cb235-11"><a href="#cb235-11" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span>
<span id="cb235-12"><a href="#cb235-12" aria-hidden="true" tabindex="-1"></a> ctrl_xy <span class="kw"><=</span> <span class="dv">0</span></span>
<span id="cb235-13"><a href="#cb235-13" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
<span id="cb235-14"><a href="#cb235-14" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb235-15"><a href="#cb235-15" aria-hidden="true" tabindex="-1"></a>sequencer(clk<span class="at">.posedge</span>,start) <span class="cf">do</span></span>
<span id="cb235-16"><a href="#cb235-16" aria-hidden="true" tabindex="-1"></a> ctrl_xy <span class="kw"><=</span> <span class="dv">1</span></span>
<span id="cb235-17"><a href="#cb235-17" aria-hidden="true" tabindex="-1"></a> x <span class="kw"><=</span> <span class="dv">2</span>; y <span class="kw"><=</span> <span class="dv">1</span></span>
<span id="cb235-18"><a href="#cb235-18" aria-hidden="true" tabindex="-1"></a> <span class="dv">10</span><span class="at">.stime</span> <span class="cf">do</span> <span class="kw">|</span>i<span class="kw">|</span></span>
<span id="cb235-19"><a href="#cb235-19" aria-hidden="true" tabindex="-1"></a> x <span class="kw"><=</span> x <span class="kw">+</span> <span class="dv">2</span></span>
<span id="cb235-20"><a href="#cb235-20" aria-hidden="true" tabindex="-1"></a> y <span class="kw"><=</span> y <span class="kw">+</span> <span class="dv">1</span></span>
<span id="cb235-21"><a href="#cb235-21" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span>
<span id="cb235-22"><a href="#cb235-22" aria-hidden="true" tabindex="-1"></a> ctrl_xy <span class="kw"><=</span> <span class="dv">0</span></span>
<span id="cb235-23"><a href="#cb235-23" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
In the example, both sequencers require access to signals
x
and y
before accessing them and then
releasing the access.
Requiring access does not guarantee that the access will be granted
by the arbiter though. In the access is not granted, the write access
will be ignored. The default access granting policy of an arbiter is the
priority in the order of sequencer declaration. I.e., if several
sequencers are requiring one at the same time, then the one declared the
earliest in the code gains write access. For example, with the code
given above, the first sequencer has to write access to x
and y
, and since after five write cycles it releases
access, the second sequencer can then write to these signals. However,
not obtaining write access does not block the execution of the
sequencer, simply, its write access to the corresponding shared signals
is ignored. In the example, the second sequencer will do its first five
loop cycles without any effect and have only its five last ones that
change the shared signals. To avoid such a behavior, it is possible to
check if the write access is granted using arbiter sub signal
acquired
: if this signal is one in the current sequencer,
that means the access is granted, otherwise it is 0. For example the
following will increase signal x
only if write access is
granted:
<span id="cb236-1"><a href="#cb236-1" aria-hidden="true" tabindex="-1"></a>hif(ctrl_xy<span class="at">.acquired</span>) <span class="kw">{</span> x <span class="kw"><=</span> x <span class="kw">+</span> <span class="dv">1</span> <span class="kw">}</span></span>
The policy of an arbiter can be changed using command policy. You can either provide a new priority table, containing the number of the sequencers in order of priority (the first one having higher priority. The number of a sequencer is assigned in order of declaration provided it uses the arbiter. For example, in the previous code, the second sequencer can be given higher priority by adding the following code after having declared the arbiter:
<span id="cb237-1"><a href="#cb237-1" aria-hidden="true" tabindex="-1"></a>ctrl_xy<span class="at">.policy</span>(<span class="kw">[</span><span class="dv">1</span>,<span class="dv">0</span><span class="kw">]</span>)</span>
It is also possible to set a more complex policy by providing to the policy method a block of code whose argument is a vector indicating which sequencer is currently requiring write access to the shared signals and whose result will be the number of the sequencer to grant the access. This code will be executed each time write access is performed. For example, in the previous code, a policy switch priorities at each access can be implemented as follows:
<span id="cb238-1"><a href="#cb238-1" aria-hidden="true" tabindex="-1"></a>inner <span class="wa">priority_xy: </span><span class="dv">0</span></span>
<span id="cb238-2"><a href="#cb238-2" aria-hidden="true" tabindex="-1"></a>inner grant_xy</span>
<span id="cb238-3"><a href="#cb238-3" aria-hidden="true" tabindex="-1"></a>ctrl_xy<span class="at">.policy</span> <span class="cf">do</span> <span class="kw">|</span>acq<span class="kw">|</span></span>
<span id="cb238-4"><a href="#cb238-4" aria-hidden="true" tabindex="-1"></a> hcase(acq)</span>
<span id="cb238-5"><a href="#cb238-5" aria-hidden="true" tabindex="-1"></a> hwhen(_b01) <span class="cf">do</span></span>
<span id="cb238-6"><a href="#cb238-6" aria-hidden="true" tabindex="-1"></a> grant_xy <span class="kw"><=</span> <span class="dv">0</span></span>
<span id="cb238-7"><a href="#cb238-7" aria-hidden="true" tabindex="-1"></a> priority_xy <span class="kw"><=</span> <span class="kw">~</span>priority_xy</span>
<span id="cb238-8"><a href="#cb238-8" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span>
<span id="cb238-9"><a href="#cb238-9" aria-hidden="true" tabindex="-1"></a> hwhen(_b10) <span class="cf">do</span></span>
<span id="cb238-10"><a href="#cb238-10" aria-hidden="true" tabindex="-1"></a> grant_xy <span class="kw"><=</span> <span class="dv">1</span></span>
<span id="cb238-11"><a href="#cb238-11" aria-hidden="true" tabindex="-1"></a> priority_xy <span class="kw"><=</span> <span class="kw">~</span>priority_xy</span>
<span id="cb238-12"><a href="#cb238-12" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span>
<span id="cb238-13"><a href="#cb238-13" aria-hidden="true" tabindex="-1"></a> hwhen(_b11) <span class="cf">do</span></span>
<span id="cb238-14"><a href="#cb238-14" aria-hidden="true" tabindex="-1"></a> grant_xy <span class="kw"><=</span> priority_xy</span>
<span id="cb238-15"><a href="#cb238-15" aria-hidden="true" tabindex="-1"></a> priority_xy <span class="kw"><=</span> <span class="kw">~</span>priority_xy</span>
<span id="cb238-16"><a href="#cb238-16" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span>
<span id="cb238-17"><a href="#cb238-17" aria-hidden="true" tabindex="-1"></a> grant_xy</span>
<span id="cb238-18"><a href="#cb238-18" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
As seen in the code above, each bit of the acq
vector is
one when the corresponding sequencer requires access or 0 otherwise, bit
0 corresponds to sequencer 0, bit 1 to sequencer 1, and so on.
Monitors
Arbiters are especially useful when we can ensure that the sequencers accessing the same resource do not overlap or when they do not need to synchronize with each other. If such synchronizations are required, instead of arbiters, it is possible to use the monitor components.
The monitor component is instantiated like the arbiters as follows:
<span id="cb239-1"><a href="#cb239-1" aria-hidden="true" tabindex="-1"></a>monitor(:<span class="kw"><</span>name<span class="kw">></span>)<span class="kw">.</span>(<span class="kw"><</span>list of shared signals<span class="kw">></span>)</span>
Monitors are used the same ways as arbiters (including the write
access granting policies) but block the execution of the sequencers that
require write access until the access is granted. If we take the example
of code with two sequencers given as an illustration of arbiter usage,
replacing the arbiter with a monitor as follows will lock the second
sequencer until it can write to shared variables x
and
y
ensuring that all its loop cycles have the specified
result:
<span id="cb240-1"><a href="#cb240-1" aria-hidden="true" tabindex="-1"></a>monitor(<span class="wa">:ctrl_xy</span>)<span class="kw">.</span>(x,y)</span>
Since monitors lock processes, they automatically insert a step.
Hence to avoid confusion, acquiring access to a monitor is done by using
the method lock
instead of assigning 1, and releasing is
done by using the method unlock
instead of assigning 0.
Hence, when using a monitor, the previous arbiter-based code should be
rewritten as follows:
<span id="cb241-1"><a href="#cb241-1" aria-hidden="true" tabindex="-1"></a>input <span class="wa">:clk</span>, <span class="wa">:start</span></span>
<span id="cb241-2"><a href="#cb241-2" aria-hidden="true" tabindex="-1"></a><span class="kw">[</span><span class="dv">8</span><span class="kw">]</span><span class="at">.shared</span> x, y</span>
<span id="cb241-3"><a href="#cb241-3" aria-hidden="true" tabindex="-1"></a>monitor(<span class="wa">:ctrl_xy</span>)<span class="kw">.</span>(x,y)</span>
<span id="cb241-4"><a href="#cb241-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb241-5"><a href="#cb241-5" aria-hidden="true" tabindex="-1"></a>sequencer(clk<span class="at">.posedge</span>,start) <span class="cf">do</span></span>
<span id="cb241-6"><a href="#cb241-6" aria-hidden="true" tabindex="-1"></a> ctrl_xy<span class="at">.lock</span></span>
<span id="cb241-7"><a href="#cb241-7" aria-hidden="true" tabindex="-1"></a> x <span class="kw"><=</span> <span class="dv">0</span> ; y <span class="kw"><=</span> <span class="dv">0</span></span>
<span id="cb241-8"><a href="#cb241-8" aria-hidden="true" tabindex="-1"></a> <span class="dv">5</span><span class="at">.stime</span> <span class="cf">do</span> <span class="kw">|</span>i<span class="kw">|</span></span>
<span id="cb241-9"><a href="#cb241-9" aria-hidden="true" tabindex="-1"></a> x <span class="kw"><=</span> x <span class="kw">+</span> <span class="dv">1</span></span>
<span id="cb241-10"><a href="#cb241-10" aria-hidden="true" tabindex="-1"></a> y <span class="kw"><=</span> y <span class="kw">+</span> <span class="dv">2</span></span>
<span id="cb241-11"><a href="#cb241-11" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span>
<span id="cb241-12"><a href="#cb241-12" aria-hidden="true" tabindex="-1"></a> ctrl_xy<span class="at">.unlock</span></span>
<span id="cb241-13"><a href="#cb241-13" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
<span id="cb241-14"><a href="#cb241-14" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb241-15"><a href="#cb241-15" aria-hidden="true" tabindex="-1"></a>sequencer(clk<span class="at">.posedge</span>,start) <span class="cf">do</span></span>
<span id="cb241-16"><a href="#cb241-16" aria-hidden="true" tabindex="-1"></a> ctrl_xy<span class="at">.lock</span></span>
<span id="cb241-17"><a href="#cb241-17" aria-hidden="true" tabindex="-1"></a> x <span class="kw"><=</span> <span class="dv">2</span>; y <span class="kw"><=</span> <span class="dv">1</span></span>
<span id="cb241-18"><a href="#cb241-18" aria-hidden="true" tabindex="-1"></a> <span class="dv">10</span><span class="at">.stime</span> <span class="cf">do</span> <span class="kw">|</span>i<span class="kw">|</span></span>
<span id="cb241-19"><a href="#cb241-19" aria-hidden="true" tabindex="-1"></a> x <span class="kw"><=</span> x <span class="kw">+</span> <span class="dv">2</span></span>
<span id="cb241-20"><a href="#cb241-20" aria-hidden="true" tabindex="-1"></a> y <span class="kw"><=</span> y <span class="kw">+</span> <span class="dv">1</span></span>
<span id="cb241-21"><a href="#cb241-21" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span>
<span id="cb241-22"><a href="#cb241-22" aria-hidden="true" tabindex="-1"></a> ctrl_xy<span class="at">.unlock</span></span>
<span id="cb241-23"><a href="#cb241-23" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
Sequencer-specific
function: std/sequencer_func.rb
HDLRuby function defined by hdef
can be used in a
sequencer like any other HDLRuyby construct. But like the process
constructs hif
and so on, the body of these functions
cannot include any sequencer-specific constructs.
However, it is possible to define functions that do support the
sequencer constructs using sdef
instead of
hdef
as follows:
<span id="cb242-1"><a href="#cb242-1" aria-hidden="true" tabindex="-1"></a> sdef :<span class="kw"><</span>function name<span class="kw">></span> <span class="cf">do</span> <span class="kw">|<</span>arguments<span class="kw">>|</span></span>
<span id="cb242-2"><a href="#cb242-2" aria-hidden="true" tabindex="-1"></a> <span class="kw"><</span>sequencer code<span class="kw">></span></span>
<span id="cb242-3"><a href="#cb242-3" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span>
Such functions can be defined anywhere in a HDLRuby description, but can only be called within a sequencer.
As additional features, since the sdef
function is made
to support the software-like code description of the sequencers, it also
supports recursion. For example, a function describing a factorial can
be described as follows:
<span id="cb243-1"><a href="#cb243-1" aria-hidden="true" tabindex="-1"></a>sdef(<span class="wa">:fact</span>) <span class="cf">do</span> <span class="kw">|</span>n<span class="kw">|</span></span>
<span id="cb243-2"><a href="#cb243-2" aria-hidden="true" tabindex="-1"></a> sif(n <span class="kw">></span> <span class="dv">1</span>) <span class="kw">{</span> sreturn(n<span class="kw">*</span>fact(n<span class="dv">-1</span>)) <span class="kw">}</span></span>
<span id="cb243-3"><a href="#cb243-3" aria-hidden="true" tabindex="-1"></a> selse <span class="kw">{</span> sreturn(<span class="dv">1</span>) <span class="kw">}</span></span>
<span id="cb243-4"><a href="#cb243-4" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
As seen in the code above, a new construct sreturn
can
be used for returning a value from anywhere inside the function.
When a recursion is present, HDLRuby automatically defines a stack
for storing the return state and the arguments of the function. The size
of the stack is heuristically set to the maximum number of bits of the
arguments of the function when it is recursively called. For example,
for the previous fact
function, if when called,
n
is 16-bit, the stack will be able to hold 16 recursions.
If this heuristic does not match the circuit’s needs, the size can be
forced as a second argument when defining the function. For example, the
following code sets the size to 32 whatever the arguments are:
<span id="cb244-1"><a href="#cb244-1" aria-hidden="true" tabindex="-1"></a>sdef(<span class="wa">:fact</span>,<span class="dv">32</span>) <span class="cf">do</span> <span class="kw">|</span>n<span class="kw">|</span></span>
<span id="cb244-2"><a href="#cb244-2" aria-hidden="true" tabindex="-1"></a> sif(n <span class="kw">></span> <span class="dv">1</span>) <span class="kw">{</span> sreturn(n<span class="kw">*</span>fact(n<span class="dv">-1</span>)) <span class="kw">}</span></span>
<span id="cb244-3"><a href="#cb244-3" aria-hidden="true" tabindex="-1"></a> selse <span class="kw">{</span> sreturn(<span class="dv">1</span>) <span class="kw">}</span></span>
<span id="cb244-4"><a href="#cb244-4" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
Notes:
A call to such a function and a return take respectively one and two cycles of the sequencer.
For now, there is no tail call optimization.
In case of stack overflow (the number of recursive calls exceeds the size of the sack), the current recursion is terminated and the sequencer goes on its execution. It is possible to add a process that is to be executed in such a case as follows:
<span id="cb245-1"><a href="#cb245-1" aria-hidden="true" tabindex="-1"></a> sdef(:<span class="kw"><</span>name<span class="kw">></span>,<span class="kw"><</span>depth<span class="kw">></span>, <span class="fu">proc</span> <span class="kw"><</span>block<span class="kw">></span>) <span class="cf">do</span></span> <span id="cb245-2"><a href="#cb245-2" aria-hidden="true" tabindex="-1"></a> <span class="kw"><</span>function code<span class="kw">></span></span> <span id="cb245-3"><a href="#cb245-3" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span>
Where
block
contains the code of the stack overflow process. For now, this process cannot contain a sequencer construct. For example, the previous factorial function can be modified as follows so that signalstack_overflow
is set to 1 in case of overflow:<span id="cb246-1"><a href="#cb246-1" aria-hidden="true" tabindex="-1"></a>sdef(<span class="wa">:fact</span>,<span class="dv">32</span>, <span class="fu">proc</span> <span class="kw">{</span> stack_overflow <span class="kw"><=</span> <span class="dv">1</span> <span class="kw">}</span>) <span class="cf">do</span> <span class="kw">|</span>n<span class="kw">|</span></span> <span id="cb246-2"><a href="#cb246-2" aria-hidden="true" tabindex="-1"></a> sif(n <span class="kw">></span> <span class="dv">1</span>) <span class="kw">{</span> sreturn(n<span class="kw">*</span>fact(n<span class="dv">-1</span>)) <span class="kw">}</span></span> <span id="cb246-3"><a href="#cb246-3" aria-hidden="true" tabindex="-1"></a> selse <span class="kw">{</span> sreturn(<span class="dv">1</span>) <span class="kw">}</span></span> <span id="cb246-4"><a href="#cb246-4" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
With the code above, the only restriction is that the signal
stack_overflow
is declared before the functionfact
is called.
Fixed-point (fixpoint):
std/fixpoint.rb
This library provides a new fixed point set of data types. These new data types can be bit vectors, unsigned or signed values and are declared respectively as follows:
<span id="cb247-1"><a href="#cb247-1" aria-hidden="true" tabindex="-1"></a>bit<span class="kw">[<</span>integer part range<span class="kw">></span>,<span class="kw"><</span>fractional part range<span class="kw">>]</span></span>
<span id="cb247-2"><a href="#cb247-2" aria-hidden="true" tabindex="-1"></a>unsigned<span class="kw">[<</span>integer part range<span class="kw">></span>,<span class="kw"><</span>fractional part range<span class="kw">>]</span></span>
<span id="cb247-3"><a href="#cb247-3" aria-hidden="true" tabindex="-1"></a>signed<span class="kw">[<</span>integer part range<span class="kw">></span>,<span class="kw"><</span>fractional part range<span class="kw">>]</span></span>
For example, a signed 4-bit integer part 4-bit fractional part fixed
point inner signal named sig
can be declared as
follows:
<span id="cb248-1"><a href="#cb248-1" aria-hidden="true" tabindex="-1"></a>bit<span class="kw">[</span><span class="dv">4</span>,<span class="dv">4</span><span class="kw">]</span><span class="at">.inner</span> <span class="wa">:sig</span></span>
When performing computation with fixed-point types, HDLRuby ensures that the result’s decimal point position is correct.
In addition to the fixed point data type, a method is added to the literal objects (Numeric) to convert them to fixed-point representation:
<span id="cb249-1"><a href="#cb249-1" aria-hidden="true" tabindex="-1"></a><span class="kw"><</span>litteral<span class="kw">></span><span class="at">.to_fix</span>(<span class="kw"><</span>number of bits after the decimal point<span class="kw">></span>)</span>
For example, the following code converts a floating-point value to a fixed-point value with 16 bits after the decimal point:
3.178.to_fix(16)
Channel: std/channel.rb
This library provides a unified interface to complex communication protocols through a new kind of component called the channels that abstract the details of communication protocols. The channels can be used similarly to the ports of a system and are used through a unified interface so that changing the kind of channel, i.e., the communication protocol, does not require any modification of the code.
Using a channel
A channel is used similarly to a pipe: it has an input where data can be written and an output where data can be read. The ordering of the data and the synchronization depend on the internals of the channel, e.g., a channel can be FIFO or LIFO. The interaction with the channel is done using the following methods:
write(<args>) <block>
: write to the channel and executeblock
whenwrite
completes.args
is a list of arguments required for performing the write that depends on the channel.read(<args>) <block>
: read the channel and executeblock
when the read completes.args
is a list of arguments required for performing the write that depends on the channel.
For example, a system sending successive 8-bit values through a channel can be described as follows:
<span id="cb251-1"><a href="#cb251-1" aria-hidden="true" tabindex="-1"></a><span class="fu">system</span> <span class="wa">:producer8</span> <span class="cf">do</span> <span class="kw">|</span>channel<span class="kw">|</span></span>
<span id="cb251-2"><a href="#cb251-2" aria-hidden="true" tabindex="-1"></a> <span class="co"># Inputs of the producer: clock and reset.</span></span>
<span id="cb251-3"><a href="#cb251-3" aria-hidden="true" tabindex="-1"></a> input <span class="wa">:clk</span>, <span class="wa">:rst</span></span>
<span id="cb251-4"><a href="#cb251-4" aria-hidden="true" tabindex="-1"></a> <span class="co"># Inner 8-bit counter for generating values.</span></span>
<span id="cb251-5"><a href="#cb251-5" aria-hidden="true" tabindex="-1"></a> <span class="kw">[</span><span class="dv">8</span><span class="kw">]</span><span class="at">.inner</span> <span class="wa">:counter</span></span>
<span id="cb251-6"><a href="#cb251-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb251-7"><a href="#cb251-7" aria-hidden="true" tabindex="-1"></a> <span class="co"># The value production process</span></span>
<span id="cb251-8"><a href="#cb251-8" aria-hidden="true" tabindex="-1"></a> par(clk<span class="at">.posedge</span>) <span class="cf">do</span></span>
<span id="cb251-9"><a href="#cb251-9" aria-hidden="true" tabindex="-1"></a> hif(rst) <span class="kw">{</span> counter <span class="kw"><=</span> <span class="dv">0</span> <span class="kw">}</span></span>
<span id="cb251-10"><a href="#cb251-10" aria-hidden="true" tabindex="-1"></a> helse <span class="cf">do</span></span>
<span id="cb251-11"><a href="#cb251-11" aria-hidden="true" tabindex="-1"></a> channel<span class="at">.write</span>(counter) <span class="kw">{</span> counter <span class="kw"><=</span> counter <span class="kw">+</span> <span class="dv">1</span> <span class="kw">}</span></span>
<span id="cb251-12"><a href="#cb251-12" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span>
<span id="cb251-13"><a href="#cb251-13" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span>
<span id="cb251-14"><a href="#cb251-14" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
Note: In the code above, the channel is passed as a generic argument of the system.
The access points to a channel can also be handled individually by declaring ports using the following methods:
input <name>
: declares a port for reading from the channel and associates them toname
if anyoutput <name>
: declares a port for writing to the channel and associates them toname
if anyinout <name>
: declares a port for reading and writing to the channel and associates them toname
if any
Such a port can then be accessed using the same read
and
write
method of a channel, the difference being that they
can also be configured for new access procedures using the
wrap
method:
wrap(<args>) <code>
: creates a new port whose read or write procedure has the elements of<args>
and the ones produced by<code>
assigned to the arguments of the read or write procedure.
For example, assuming mem
is a channel whose read and
write access have as argument the target address and data signals, the
following code creates a port for always accessing at address 0:
<span id="cb252-1"><a href="#cb252-1" aria-hidden="true" tabindex="-1"></a> addr0 <span class="kw">=</span> channel<span class="at">.input.wrap</span>(<span class="dv">0</span>) </span>
Channel branches
Some channels may include several branches, they are accessed by name using the following method:
branch(<name>)
: gets branch namedname
from the channel. This name can be any ruby object (e.g., a number) but it will be converted internally to a ruby symbol.
A branch is a full-fledged channel and is used identically. For
instance, the following code gets access to branch number 0 of channel
ch
, gets its inputs port, reads it, and put the result in
signal val
on the rising edges of signal
clk
:
<span id="cb253-1"><a href="#cb253-1" aria-hidden="true" tabindex="-1"></a>br <span class="kw">=</span> ch<span class="at">.branch</span>(<span class="dv">0</span>)</span>
<span id="cb253-2"><a href="#cb253-2" aria-hidden="true" tabindex="-1"></a>br<span class="at">.input</span></span>
<span id="cb253-3"><a href="#cb253-3" aria-hidden="true" tabindex="-1"></a>par(clk<span class="at">.posedge</span>) <span class="kw">{</span> br<span class="at">.read</span>(val) <span class="kw">}</span></span>
Declaring a channel
A new channel is declared using the keyword channel
as
follows:
<span id="cb254-1"><a href="#cb254-1" aria-hidden="true" tabindex="-1"></a>channel <span class="kw"><</span>name<span class="kw">></span> <span class="kw"><</span>block<span class="kw">></span></span>
Where name
is the name of the channel and
block
is a procedure block describing the channel. This
block can contain any HDLRuby code, and is comparable to the content of
a block describing a system with the difference that it does not have
standard input, output, and inout ports are declared differently, and
that it supports the following additional keywords:
reader_input <list of names>
: declares the input ports on the reader side. The list must give the names of the inner signals of the channel that can be read using the reader procedure.reader_output <list of names>
: declares the output ports on the reader side. The list must give the names of the inner signals of the channel that can be written using the reader procedure.reader_inout <list of names>
: declares the inout ports on the reader side. The list must give the names of the inner signals of the channel that can be written using the reader procedure.writer_input <list of names>
: declares the input ports on the writer side. The list must give the names of the inner signals of the channel that can be read using the writer procedure.writer_output <list of names>
: declares the output ports on the writer side. The list must give the names of the inner signals of the channel that can be written using the writer procedure.writer_inout <list of names>
: declares the inout ports on the writer side. The list must give the names of the inner signals of the channel that can be written using the writer procedure.accesser_input <list of names>
: declares the input ports on both the reader and writer sides. The list must give the names of the inner signals of the channel that can be read using the writer procedure.accesser_output <list of names>
: declares the output ports on both the reader and writer sides. The list must give the names of the inner signals of the channel that can be written using the writer procedure.accesser_inout <list of names>
: declares the inout ports on both the reader and writer sides. The list must give the names of the inner signals of the channel that can be written using the writer procedure.reader <block>
: defines the reader’s access procedure. This procedure is invoked by the methodread
of the channel (please refer to the previous example). The first argument of the block must be the following:blk
: the block to execute when the read completes. Other arguments can be freely defined and will be required by theread
method.
writer < block>
: defines the writer’s access procedure. This procedure is invoked by the methodwrite
of the channel (please refer to the previous example). The first argument of the block must be the following:blk
: the block to execute when the write completes. Other arguments can be freely defined and will be required by thewrite
command.
brancher(name) <block>
: defines branch named +name+ described inblock
. The content of the block can be any content valid for a channel, with the additional possibility to access the internals of the upper channel.
For example, a channel implemented by a simple register of generic
type typ
, that can be set to 0 using the reset
command can be described as follows:
<span id="cb255-1"><a href="#cb255-1" aria-hidden="true" tabindex="-1"></a>channel <span class="wa">:regch</span> <span class="cf">do</span> <span class="kw">|</span>typ<span class="kw">|</span></span>
<span id="cb255-2"><a href="#cb255-2" aria-hidden="true" tabindex="-1"></a> <span class="co"># The register.</span></span>
<span id="cb255-3"><a href="#cb255-3" aria-hidden="true" tabindex="-1"></a> typ<span class="at">.inner</span> <span class="wa">:reg</span></span>
<span id="cb255-4"><a href="#cb255-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb255-5"><a href="#cb255-5" aria-hidden="true" tabindex="-1"></a> <span class="co"># The reader procedure can read reg</span></span>
<span id="cb255-6"><a href="#cb255-6" aria-hidden="true" tabindex="-1"></a> reader_input <span class="wa">:reg</span></span>
<span id="cb255-7"><a href="#cb255-7" aria-hidden="true" tabindex="-1"></a> <span class="co"># The writer procedure can write reg</span></span>
<span id="cb255-8"><a href="#cb255-8" aria-hidden="true" tabindex="-1"></a> writer_output <span class="wa">:reg</span></span>
<span id="cb255-9"><a href="#cb255-9" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb255-10"><a href="#cb255-10" aria-hidden="true" tabindex="-1"></a> <span class="co"># Declares a reset</span></span>
<span id="cb255-11"><a href="#cb255-11" aria-hidden="true" tabindex="-1"></a> command(<span class="wa">:reset</span>) <span class="kw">{</span> reg <span class="kw"><=</span> <span class="dv">0</span> <span class="kw">}</span></span>
<span id="cb255-12"><a href="#cb255-12" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb255-13"><a href="#cb255-13" aria-hidden="true" tabindex="-1"></a> <span class="co"># Defines the reader procedure.</span></span>
<span id="cb255-14"><a href="#cb255-14" aria-hidden="true" tabindex="-1"></a> reader <span class="cf">do</span> <span class="kw">|</span>blk,target<span class="kw">|</span></span>
<span id="cb255-15"><a href="#cb255-15" aria-hidden="true" tabindex="-1"></a> target <span class="kw"><=</span> reg</span>
<span id="cb255-16"><a href="#cb255-16" aria-hidden="true" tabindex="-1"></a> blk<span class="at">.call</span> <span class="cf">if</span> blk</span>
<span id="cb255-17"><a href="#cb255-17" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span>
<span id="cb255-18"><a href="#cb255-18" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb255-19"><a href="#cb255-19" aria-hidden="true" tabindex="-1"></a> <span class="co"># Defines the writer procedure.</span></span>
<span id="cb255-20"><a href="#cb255-20" aria-hidden="true" tabindex="-1"></a> writer <span class="cf">do</span> <span class="kw">|</span>blk,target<span class="kw">|</span></span>
<span id="cb255-21"><a href="#cb255-21" aria-hidden="true" tabindex="-1"></a> reg <span class="kw"><=</span> target</span>
<span id="cb255-22"><a href="#cb255-22" aria-hidden="true" tabindex="-1"></a> blk<span class="at">.call</span> <span class="cf">if</span> blk</span>
<span id="cb255-23"><a href="#cb255-23" aria-hidden="true" tabindex="-1"></a> <span class="cf">end</span></span>
<span id="cb255-24"><a href="#cb255-24" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
Notes:
- The described channel assumes that the
write
method of the channel is invoked within a clocked process (otherwise, the register will become a latch). - The described channel supports the
read
andwrite
methods to be invoked with or without a block.
Like systems, a channel must be instantiated for being used, and the instantiation procedure is identical:
<span id="cb256-1"><a href="#cb256-1" aria-hidden="true" tabindex="-1"></a><span class="kw"><</span>channel name<span class="kw">></span> :<span class="kw"><</span>instance name<span class="kw">></span></span>
And in case there is a generic parameter, the instantiation procedure is as follows:
<span id="cb257-1"><a href="#cb257-1" aria-hidden="true" tabindex="-1"></a><span class="kw"><</span>channel name<span class="kw">></span>(:<span class="kw"><</span>instance name<span class="kw">></span>)<span class="kw">.</span>(<span class="kw"><</span>generic parameters<span class="kw">></span>)</span>
After a channel is instantiated, it must be linked to the circuits that will communicate through it. This is done when instantiating these circuits. If a circuit reads or writes on the channel, it will be instantiated as follows:
<span id="cb258-1"><a href="#cb258-1" aria-hidden="true" tabindex="-1"></a><span class="kw"><</span><span class="fu">system</span> name<span class="kw">></span>(<span class="kw"><</span>channel instance<span class="kw">></span>)<span class="kw">.</span>(:<span class="kw"><</span>instance name<span class="kw">></span>)<span class="kw">.</span>(<span class="kw"><</span>circuit standard connections<span class="kw">></span>)</span>
Notes:
- It is possible for a circuit to access several channels. For that
purpose, each channel must be passed as generic arguments, and their
corresponding
reader_signals
andwriter_signals
are to be put in the order of declaration. - It is also possible for a circuit to read and write on the same
channel. For that purpose, the channel will be passed several times as
generic arguments, and the corresponding
reader_signals
andwriter_signals
are to be put in the order of declaration.
The following code is an example instantiating the register channel
presented above for connecting an instance of producer8
and
another circuit called consumer8
:
<span id="cb259-1"><a href="#cb259-1" aria-hidden="true" tabindex="-1"></a><span class="co"># System wrapping the producer and the consumer circuits.</span></span>
<span id="cb259-2"><a href="#cb259-2" aria-hidden="true" tabindex="-1"></a><span class="fu">system</span> <span class="wa">:producer_consumer8</span> <span class="cf">do</span></span>
<span id="cb259-3"><a href="#cb259-3" aria-hidden="true" tabindex="-1"></a> <span class="co"># The clock and reset of the circuits</span></span>
<span id="cb259-4"><a href="#cb259-4" aria-hidden="true" tabindex="-1"></a> input <span class="wa">:clk</span>, <span class="wa">:rst</span></span>
<span id="cb259-5"><a href="#cb259-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb259-6"><a href="#cb259-6" aria-hidden="true" tabindex="-1"></a> <span class="co"># Instance of the channel (using 8-bit data).</span></span>
<span id="cb259-7"><a href="#cb259-7" aria-hidden="true" tabindex="-1"></a> regch(<span class="kw">[</span><span class="dv">8</span><span class="kw">]</span>)<span class="kw">.</span>(<span class="wa">:regchI</span>)</span>
<span id="cb259-8"><a href="#cb259-8" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb259-9"><a href="#cb259-9" aria-hidden="true" tabindex="-1"></a> <span class="co"># Reset the channel on positive edges of signal rst.</span></span>
<span id="cb259-10"><a href="#cb259-10" aria-hidden="true" tabindex="-1"></a> regchI<span class="at">.reset.at</span>(rst<span class="at">.posedge</span>)</span>
<span id="cb259-11"><a href="#cb259-11" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb259-12"><a href="#cb259-12" aria-hidden="true" tabindex="-1"></a> <span class="co"># Instantiate the producer.</span></span>
<span id="cb259-13"><a href="#cb259-13" aria-hidden="true" tabindex="-1"></a> producer8(regch)<span class="kw">.</span>(<span class="wa">:producerI</span>)<span class="kw">.</span>(clk,rst)</span>
<span id="cb259-14"><a href="#cb259-14" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb259-15"><a href="#cb259-15" aria-hidden="true" tabindex="-1"></a> <span class="co"># Instantiate the consumer.</span></span>
<span id="cb259-16"><a href="#cb259-16" aria-hidden="true" tabindex="-1"></a> consumer8(regch)<span class="kw">.</span>(<span class="wa">:consumerI</span>)<span class="kw">.</span>(clk<span class="at">.rst</span>)</span>
<span id="cb259-17"><a href="#cb259-17" aria-hidden="true" tabindex="-1"></a><span class="cf">end</span></span>
Note:
- The code of the circuits, in the examples
producer8
,consumer8
, andproducer_consummer8
is independent of the content of the channel. For example, the samplewith_channel.rb
(please see samples) uses the same circuits with a channel implementing handshaking.
Sample HDLRuby descriptions
Several samples HDLRuby descriptions are available in the following directory:
path/to/HDLRuby/lib/HDLRuby/hdr_samples
For the gem install, the path to HDLRuby can be found using the following:
<span id="cb260-1"><a href="#cb260-1" aria-hidden="true" tabindex="-1"></a><span class="ex">gem</span> which HDLRuby</span>
But you can also import the samples to your local directory with the following command (recommended):
<span id="cb261-1"><a href="#cb261-1" aria-hidden="true" tabindex="-1"></a><span class="ex">hdrcc</span> <span class="at">--get-samples</span></span>
The naming convention of the samples is the following:
<name>.rb
: default type of sample.<name>_gen.rb
: generic parameters are required for processing the sample.<name>_bench.rb
: sample including a simulation benchmark, these are the only samples that can be simulated usinghdrcc -S
. Please notice that such a sample cannot be converted to VHDL or Verilog HDL yet.with_<name>.rb
: sample illustrating a single aspect of HDLRuby or one of its libraries, usually includes a benchmark.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/civol/HDLRuby.
To do
- Find and fix the (maybe) terrifying number of bugs.
- Add a GUI (any volunteer to do it?).
License
The gem is available as open-source under the terms of the MIT License.