What & Why?

To accomplish a particular electronics application, a specific combination of hardware and software is needed. For instance, the CPU is a processor based system which is essentially just a silicon chip with software that enables serial execution of instruction sets. For larger applications, our silicon chips are usually PLDs(programmable logic devices) like PAL, CPLD and FPGA.

The structure of an FPGA device - to be coded in VHDL

The structure of an FPGA device - to be coded in VHDL

Now, the software required for these chips(and many others – including ASIC devices) is coded in a HARDWARE DESCRIPTION LANGUAGE(HDL) like VHDL or Verilog. VHDL is a nested acronym that stands for Very High Speed Integrated circuits(VHSIC) Hardware Description Language.

VHDL allows us to view a design at various levels of abstraction – Simulation gives us the waveforms of the circuit inputs and outputs & Synthesis gives us the possible combinations of gates/transistors to achieve the required operation.

In VHDL, any circuit/system is viewed as an entity(or a set of entities). The internal working of an entity is called its architecture. For instance, to design a half adder using VHDL, the entity would be the half adder itself with its input and output ports & the architecture would tell VHDL what happens between the input & the output ports.

A Sample Code in VHDL

Here is a sample code to design a half adder using VHDL. It is just to see how entity & architecture together make up the whole circuit design.

<span style="color:#200080; font-weight:bold; ">entity</span> half_adder <span style="color:#200080; font-weight:bold; ">is</span>
<span style="color:#200080; font-weight:bold; ">port</span> <span style="color:#308080; ">(</span>A<span style="color:#308080; ">,</span> B <span style="color:#308080; ">:</span> <span style="color:#200080; font-weight:bold; ">in</span> <span style="color:#7779bb; ">BIT</span><span style="color:#406080; ">;</span> sum<span style="color:#308080; ">,</span> carry <span style="color:#308080; ">:</span> <span style="color:#200080; font-weight:bold; ">out</span> <span style="color:#7779bb; ">BIT</span><span style="color:#308080; ">)</span><span style="color:#406080; ">;</span>
<span style="color:#200080; font-weight:bold; ">end</span> half_adder<span style="color:#406080; ">;</span>

<span style="color:#200080; font-weight:bold; ">architecture</span> adder_architecture <span style="color:#200080; font-weight:bold; ">of</span> half_adder <span style="color:#200080; font-weight:bold; ">is</span>
<span style="color:#200080; font-weight:bold; ">begin</span>
sum <span style="color:#308080; ">&lt;</span><span style="color:#308080; ">=</span> A <span style="color:#200080; font-weight:bold; ">xor</span> B<span style="color:#406080; ">;</span>
carry <span style="color:#308080; ">&lt;</span><span style="color:#308080; ">=</span> A <span style="color:#200080; font-weight:bold; ">and</span> B<span style="color:#406080; ">;</span>
<span style="color:#200080; font-weight:bold; ">end</span> adder_architecture<span style="color:#406080; ">;</span>

VHDL Libraries & Packages

Just like all other languages, VHDL has libraries that allow us to reuse certain frequently used pieces of code. The three basic libraries that go into almost all VHDL coding are –

<span style="color:#200080; font-weight:bold; ">library</span> library_name<span style="color:#406080; ">;</span>
<span style="color:#200080; font-weight:bold; ">use</span> library_name<span style="color:#308080; ">.</span>package_name<span style="color:#308080; ">.</span>package_part<span style="color:#406080; ">;</span>   <span style="color:#595979; ">--the syntax</span>

<span style="color:#200080; font-weight:bold; ">library</span> ieee<span style="color:#406080; ">;</span>
<span style="color:#200080; font-weight:bold; ">use</span> ieee<span style="color:#308080; ">.</span>std_logic_1164<span style="color:#308080; ">.</span><span style="color:#200080; font-weight:bold; ">all</span><span style="color:#406080; ">;</span>     <span style="color:#595979; ">
--use all parts of the std_logic_1164 package</span>

<span style="color:#200080; font-weight:bold; ">library</span> std<span style="color:#406080; ">;</span>
<span style="color:#200080; font-weight:bold; ">use</span> std<span style="color:#308080; ">.</span>standard<span style="color:#308080; ">.</span><span style="color:#200080; font-weight:bold; ">all</span><span style="color:#406080; ">;</span>    <span style="color:#595979; ">--added by default</span>

<span style="color:#200080; font-weight:bold; ">library</span> wprk<span style="color:#406080; ">;</span>
<span style="color:#200080; font-weight:bold; ">use</span> work<span style="color:#308080; ">.</span><span style="color:#200080; font-weight:bold; ">all</span><span style="color:#406080; ">;</span>            <span style="color:#595979; ">--added by default</span>

<span style="color:#595979; ">--these are comment lines, by the way.</span>

All packages contain useful data types & keywords. A full specification/definition of the packages can be seen at http://www.csee.umbc.edu/portal/help/VHDL/packages/

Data Objects & Data Types in VHDL

In VHDL, data can be in the form of  a Constant, Variable or a  Signal – which are also the keywords for declaring the same. “File” in VHDL is a sequence of values and hence, is also considered a data object. Each of these objects are of some specified data type.

<span style="color:#200080; font-weight:bold; ">constant</span> RISE <span style="color:#308080; ">:</span> <span style="color:#7779bb; ">TIME</span><span style="color:#406080; ">;</span>      <span style="color:#595979; ">--of type time</span>
<span style="color:#200080; font-weight:bold; ">variable</span> COUNT <span style="color:#308080; ">:</span> <span style="color:#7779bb; ">INTEGER</span><span style="color:#406080; ">;</span>  <span style="color:#595979; ">--of integer type</span>
<span style="color:#200080; font-weight:bold; ">signal</span> CLOCK <span style="color:#308080; ">:</span> <span style="color:#7779bb; ">BIT</span><span style="color:#406080; ">;</span>        <span style="color:#595979; ">--bit type</span>

The following data types can be used in VHDL –

Scalar Types – enumeration, integer and floating point types including BOOLEAN, BIT, CHARACTER, INTEGER, NATURAL, REAL, etc.

Physical Literals – values representing physical quantities like CURRENT, VOLTAGE, etc. TIME is the only predefined literal(in the std package)

Composite types - contain a collection of values that may belong to different types like ARRAY and RECORD.

File Types – represents files in the host environment – help communicate with the host.

Operators in VHDL

Assignment Operators – used to assign a values to a data object. <= assigns a value to a signal & := assigns a value to a variable while => is used to assign values to individual/other vector elements.

<span style="color:#200080; font-weight:bold; ">signal</span> x <span style="color:#308080; ">:</span> <span style="color:#7779bb; ">bit</span><span style="color:#406080; ">;</span>
x <span style="color:#308080; ">&lt;</span><span style="color:#308080; ">=</span> <span style="color:#1060b6; ">'1'</span><span style="color:#406080; ">;</span>

<span style="color:#200080; font-weight:bold; ">variable</span> y <span style="color:#308080; ">:</span> <span style="color:#7779bb; ">std_logic_vector</span> <span style="color:#308080; ">(</span><span style="color:#008c00; ">0</span> <span style="color:#200080; font-weight:bold; ">to</span> <span style="color:#008c00; ">3</span><span style="color:#308080; ">)</span><span style="color:#406080; ">;</span>  <span style="color:#595979; ">
--y is a vector with 4 elements</span>
y<span style="color:#308080; ">:</span><span style="color:#308080; ">=</span><span style="color:#1060b6; ">"0000"</span><span style="color:#406080; ">;</span>

<span style="color:#200080; font-weight:bold; ">signal</span> w <span style="color:#308080; ">:</span> <span style="color:#7779bb; ">std_logic_vector</span> <span style="color:#308080; ">(</span><span style="color:#008c00; ">0</span> <span style="color:#200080; font-weight:bold; ">to</span> <span style="color:#008c00; ">7</span><span style="color:#308080; ">)</span><span style="color:#406080; ">;</span>
w <span style="color:#308080; ">&lt;</span><span style="color:#308080; ">=</span> <span style="color:#308080; ">(</span><span style="color:#008c00; ">0</span><span style="color:#308080; ">=</span><span style="color:#308080; ">&gt;</span><span style="color:#1060b6; ">'1'</span><span style="color:#308080; ">,</span> <span style="color:#200080; font-weight:bold; ">others</span><span style="color:#308080; ">=</span><span style="color:#308080; ">&gt;</span><span style="color:#1060b6; ">'0'</span><span style="color:#308080; ">)</span><span style="color:#406080; ">;</span>   <span style="color:#595979; ">
--0th element is 1 and others are 0</span>

Logical Operators – boolean logic ; all the basic logic gates. Data to be operated must be of type BIT, STD_LOGIC or STD_ULOGIC or their vector extentions.

Arithmetic Operators –  for arithmetic (not boolean) operations including +, -, *, / , **, MOD, REM, ABS

Relational Operators –  used for comparisons ( =, /=,  > , < , <= & >= )

Shift Operators –  shift logic left(sll), shift logic right(slr), shift left arithmetic(sla), shift right arithmetic(slr), rotate left(rol) & rotate right(ror) –

<span style="color:#1060b6; ">"1001010"</span> <span style="color:#200080; font-weight:bold; ">sll</span> <span style="color:#008c00; ">2</span> <span style="color:#200080; font-weight:bold; ">is</span> <span style="color:#1060b6; ">"0101000"</span>

<span style="color:#595979; ">--shift left by 2 and replace missing digits on right by zeros</span>

<span style="color:#1060b6; ">"1001010"</span> <span style="color:#200080; font-weight:bold; ">srl</span> <span style="color:#008c00; ">4</span> <span style="color:#200080; font-weight:bold; ">is</span> <span style="color:#1060b6; ">"0000100"</span>

<span style="color:#595979; ">--shift right by 4 and replace missing digits on left by zeros</span>

<span style="color:#1060b6; ">"1001010"</span> <span style="color:#200080; font-weight:bold; ">sla</span> <span style="color:#008c00; ">2</span> <span style="color:#200080; font-weight:bold; ">is</span> <span style="color:#1060b6; ">"0101000"</span>

<span style="color:#595979; ">--shift left by 2 and replace missing digits by rightmost bit
(here, 0)</span>

<span style="color:#1060b6; ">"1001010"</span> <span style="color:#200080; font-weight:bold; ">sra</span> <span style="color:#008c00; ">4</span> <span style="color:#200080; font-weight:bold; ">is</span> <span style="color:#1060b6; ">"1111100"</span>

<span style="color:#595979; ">--shift right by 4 and replace missing digits by leftmost bit
(here, 1)</span>

<span style="color:#1060b6; ">"1001010"</span> <span style="color:#200080; font-weight:bold; ">rol</span> <span style="color:#008c00; ">2</span> <span style="color:#200080; font-weight:bold; ">is</span> <span style="color:#1060b6; ">"0101010"</span>

<span style="color:#595979; ">--shift left by 2 while moving overlapped digits to the right
(here, 10)</span>

<span style="color:#1060b6; ">"1001010"</span> <span style="color:#200080; font-weight:bold; ">ror</span> <span style="color:#008c00; ">1</span> <span style="color:#200080; font-weight:bold; ">is</span> <span style="color:#1060b6; ">"0100101"</span>

<span style="color:#595979; ">--shift right by 1 while moving overlapped digits to the left
(here, 0)</span>

Note : rol -1 is the same as ror 1, sla -2 is the same as sra 2 and so on.

Concurrent & Sequential Design Models

Unlike the sequential statements in other programming languages, VHDL code is concurrent code – which is good enough to build combinational circuits . However, to build sequential circuits, we need sequential code. We can implement sequential code in VHDL using some specific statements.

To write concurrent code, use WHEN, GENERATE & BLOCK statements & to write sequential code, use the PROCESS, FUNCTION & PROCEDURE statements.

Example – A 4:1 MUX using Concurrent Code

<span style="color:#200080; font-weight:bold; ">library</span> ieee<span style="color:#406080; ">;</span>
<span style="color:#200080; font-weight:bold; ">use</span> ieee<span style="color:#308080; ">.</span>std_logic_1164<span style="color:#308080; ">.</span><span style="color:#200080; font-weight:bold; ">all</span><span style="color:#406080; ">;</span>

<span style="color:#200080; font-weight:bold; ">entity</span> mux <span style="color:#200080; font-weight:bold; ">is</span>
<span style="color:#200080; font-weight:bold; ">port</span><span style="color:#308080; ">(</span>a<span style="color:#308080; ">,</span>b<span style="color:#308080; ">,</span>c<span style="color:#308080; ">,</span>d <span style="color:#308080; ">:</span> <span style="color:#200080; font-weight:bold; ">in</span> <span style="color:#7779bb; ">std_logic</span><span style="color:#406080; ">;</span>
sel <span style="color:#308080; ">:</span> <span style="color:#200080; font-weight:bold; ">in</span> <span style="color:#7779bb; ">std_logic_vector</span><span style="color:#308080; ">(</span><span style="color:#008c00; ">1</span> <span style="color:#200080; font-weight:bold; ">downto</span> <span style="color:#008c00; ">0</span><span style="color:#308080; ">)</span><span style="color:#406080; ">;</span>
y <span style="color:#308080; ">:</span> <span style="color:#200080; font-weight:bold; ">out</span> <span style="color:#7779bb; ">std_logic</span><span style="color:#308080; ">)</span><span style="color:#406080; ">;</span>
<span style="color:#200080; font-weight:bold; ">end</span> mux<span style="color:#406080; ">;</span>

<span style="color:#200080; font-weight:bold; ">architecture</span> mux_a <span style="color:#200080; font-weight:bold; ">of</span> mux <span style="color:#200080; font-weight:bold; ">is</span>
<span style="color:#200080; font-weight:bold; ">begin</span>
y <span style="color:#308080; ">&lt;</span><span style="color:#308080; ">=</span> a <span style="color:#200080; font-weight:bold; ">when</span> sel <span style="color:#308080; ">=</span> <span style="color:#1060b6; ">"00"</span>
<span style="color:#200080; font-weight:bold; ">else</span> b <span style="color:#200080; font-weight:bold; ">when</span> sel <span style="color:#308080; ">=</span> <span style="color:#1060b6; ">"01"</span>
<span style="color:#200080; font-weight:bold; ">else</span> c <span style="color:#200080; font-weight:bold; ">when</span> sel <span style="color:#308080; ">=</span> <span style="color:#1060b6; ">"10"</span>
<span style="color:#200080; font-weight:bold; ">else</span> d<span style="color:#406080; ">;</span>
<span style="color:#200080; font-weight:bold; ">end</span> mux1<span style="color:#406080; ">;</span>

Example – A Rising Edge-Triggered Delay FF using Sequential Code

<span style="color:#200080; font-weight:bold; ">library</span> ieee<span style="color:#406080; ">;</span>
<span style="color:#200080; font-weight:bold; ">use</span> ieee<span style="color:#308080; ">.</span>std_logic_1164<span style="color:#308080; ">.</span><span style="color:#200080; font-weight:bold; ">all</span><span style="color:#406080; ">;</span>

<span style="color:#200080; font-weight:bold; ">entity</span> dff <span style="color:#200080; font-weight:bold; ">is</span>
<span style="color:#200080; font-weight:bold; ">port</span><span style="color:#308080; ">(</span>clock<span style="color:#308080; ">,</span> reset<span style="color:#308080; ">,</span> d <span style="color:#308080; ">:</span> <span style="color:#200080; font-weight:bold; ">in</span> <span style="color:#7779bb; ">std_logic</span><span style="color:#406080; ">;</span> y <span style="color:#308080; ">:</span> <span style="color:#200080; font-weight:bold; ">out</span> <span style="color:#7779bb; ">std_logic</span><span style="color:#308080; ">)</span><span style="color:#406080; ">;</span>
<span style="color:#200080; font-weight:bold; ">end</span> dff<span style="color:#406080; ">;</span>

<span style="color:#200080; font-weight:bold; ">architecture</span> dff_a <span style="color:#200080; font-weight:bold; ">of</span> dff <span style="color:#200080; font-weight:bold; ">is</span>
<span style="color:#200080; font-weight:bold; ">begin</span>

<span style="color:#200080; font-weight:bold; ">process</span><span style="color:#308080; ">(</span>reset<span style="color:#308080; ">,</span> clock<span style="color:#308080; ">)</span>
<span style="color:#595979; ">--the sensitivity list contains objects that can
cause the process to execute.</span>
<span style="color:#200080; font-weight:bold; ">begin</span>

<span style="color:#200080; font-weight:bold; ">if</span><span style="color:#308080; ">(</span>reset <span style="color:#308080; ">&lt;</span><span style="color:#308080; ">=</span> <span style="color:#1060b6; ">'1'</span><span style="color:#308080; ">)</span> <span style="color:#200080; font-weight:bold; ">then</span> q <span style="color:#308080; ">&lt;</span><span style="color:#308080; ">=</span> <span style="color:#1060b6; ">'0'</span>
elseif <span style="color:#308080; ">(</span>clock<span style="color:#074726; ">'EVENT</span> <span style="color:#200080; font-weight:bold; ">and</span> clock <span style="color:#308080; ">=</span> <span style="color:#1060b6; ">'1'</span><span style="color:#308080; ">)</span> <span style="color:#200080; font-weight:bold; ">then</span> q <span style="color:#308080; ">&lt;</span><span style="color:#308080; ">=</span> d<span style="color:#406080; ">;</span>
<span style="color:#595979; ">--if an event occurs on clock and the event is a rising edge</span>
<span style="color:#200080; font-weight:bold; ">end</span> <span style="color:#200080; font-weight:bold; ">if</span><span style="color:#406080; ">;</span>

<span style="color:#200080; font-weight:bold; ">end</span> <span style="color:#200080; font-weight:bold; ">process</span><span style="color:#406080; ">;</span>

<span style="color:#200080; font-weight:bold; ">end</span> dff_a<span style="color:#406080; ">;</span>