Introduction of VHDL: Shomal University Fall 2010
Introduction of VHDL: Shomal University Fall 2010
Introduction of VHDL
Shomal University
Fall 2010
Rahim Soleymanpour
Introduction
Two major HDLs:
VHDL
standardized by IEEE in 1987,1993, 2002,2006
object-oriented, very widely used
Has Verbose syntax like ADA
Verilog
standardized by IEEE in 1995, 2001, 2005
has concise syntax like C
Introduction
VHDL example :
-- An N-bit multiplier
entity MULTIPLY is
port (
a, b : in bit_vector( 7 downto 0 );
z : out bit_vector( 15 downto 0 )
);
end entity;
architecture rtl of MULTIPLY is
begin
z <= a * b;
end rtl;
Introduction
Verilog Example:
// An N-bit multiplier
module MULTIPLY (a,b,z);
input [7:0] a,b;
output [15:0] z;
assign z = a * b;
endmodule
VHDL Design Flow
VHDL Constructs
Entity definition
Architecture definition
Configuration
Process
Subprogram
Package
A VHDL design can be broken into multiple files.
Each file contains entity and architecture
definitions, or packages.
Design Entity
A component or a system to be designed is a VHDL
design entity
VHDL divides entities into two parts:
External (interface): how it connects to other
components
Internal (architecture): how it behaves and how it is
implemented
Behavioral
Structural
Hybrid
Entity definition
To define an entity
First an entity declaration is given. This
specifies one or more input, output, or input-output
ports that are wired to neighboring
entities.
Then one or more architecture definition(s) is
given. This determines how the entity functions
and how it is implemented.
Example
Two input NAND gate
entity NAND2 is
port ( a : in bit;
b : in bit;
z : out bit );
end entity;
-- A dataflow description
architecture dataflow of NAND2 is
begin
z <= not (a and b);
end dataflow;
Example
-- A behavioral description
architecture behavioral of NAND2 is
begin
process(a,b)
begin
if a=1 and b=1 then
z <= 0;
else
z <= 1;
end process;
end behavioral;
Entities and Architectures
ENTITY entity_name IS
input and output ports
END ENTITY entity_name;
Concat. &
Operators
Logic value system
Value Representing
'U' Uninitialized
'X' Forcing Unknown
'0' Forcing 0
'1' Forcing 1
'Z' High Impedance
'W' Weak Unknown
'L' Weak 0
'H' Weak 1
'-' Dont care
Example
Entity:
ENTITY aCircuit IS
PORT (a, b : IN BIT;
c : INOUT BIT;
av, bv : IN BIT_VECTOR (7 DOWNTO 0);
cv : INOUT BIT_VECTOR (7 DOWNTO 0);
w : OUT BIT;
wv : OUT BIT_VECTOR (7 DOWNTO 0));
END ENTITY aCircuit;
Example
ARCHITECTURE four_assignments OF aCircuit IS
SIGNAL d : BIT;
SIGNAL iv, jv, kv : BIT_VECTOR (7 DOWNTO 0);
BEGIN
iv <= av AND cv;
jv <= bv AND cv;
kv <= av NOR bv;
wv <= iv XOR jv WHEN c = 1 ELSE iv NAND kv;
END ARCHITECTURE four_assignments;
Example
ARCHITECTURE indexing_slicing OF aCircuit IS
SIGNAL d : BIT;
SIGNAL dv : BIT_VECTOR (7 DOWNTO 0);
BEGIN
wv (3 DOWNTO 0) <= av (7 DOWNTO 4) AND cv (7
DOWNTO 4);
w <= cv (4);
cv (7) <= av (0);
END ARCHITECTURE indexing_slicing;
Example
-- xor example
ENTITY xor2 IS
PORT (i1, i2: IN std_logic; o1: OUT
std_logic);
END ENTITY xor2;
--
ARCHITECTURE expression OF xor2 IS
BEGIN
o1 <= i1 XOR i2 AFTER 3 NS;
END ARCHITECTURE expression
Example
-- full adder Example
ENTITY full_adder IS
PORT (a, b, cin : IN std_logic;
sum, cout : OUT std_logic);
END ENTITY full_adder;
--
ARCHITECTURE expression OF full_adder IS
BEGIN
sum <= a XOR b XOR cin AFTER 0.3 NS;
cout <= (a AND b) OR (a AND cin) OR (b AND
cin) AFTER 0.2 NS;
END ARCHITECTURE expression;
Modeling Concurrency
Delay and concurrency is inherent in any real circuit and must be
simulated.
Event-driven simulation method is used.
Example Simulation
VHDL Statements
VHDL has two kinds of statements:
Sequential statements: they are executed one after
another, like C or Pascal
Signal and variable assignments, flow control constructs (if,
case, while, loop, ), subprogram call, wait
Concurrent statements: all are executed at the
same time.
Processes, block statements, component instantiations,
generate statements, concurrent assignments and concurrent
procedures
Syntax
Entity:
entity entity_name is
[ generic (generic_declarations); ]
[ port (port_declarations); ]
end [entity] [entity_name];
Generic:
generic ( [constant_name : type [:=value] ]
{ ; constant_name : type [:=value] } );
Port:
port ( [ port_name : port_mode type
{ ; port_name : port_mode type } ] );
Syntax
Port Modes:
In : port can only be read
Out : port can only be assigned
Inout : Can be read and assigned a value. The value read is
not the assigned value.
Buffer : Similar to out, but can be read
Example
entity adder is
generic ( N: integer :=8 );
port ( x, y : in bit_vector(N-1 downto 0);
sum : out bit _vector(N downto 0) );
end entity adder;
Syntax
Architecture
architecture architecture_name of entity_name is
{ declarative_item }
begin
{ concurrent_statement }
end [architecture_name];
A declarative item can be any of these:
Use clause
Subprogram declaration and body
Type or subtype declaration
Constant declaration
Signal declaration
Component declaration
Example
architecture dataflow of adder is
begin
process(x,y)
begin
sum <= x + y;
end process;
end dataflow;
Syntax
Process
[label:] process [( sensitivity_list )]
{ process_declarative_item }
begin
{ sequential_statement }
end process;
The sensitivity list is a list of signals read by the process.
The process declarative items can include:
Use clause
Subprogram declaration and body
Variable and constant declaration
Type and subtype declaration
Signals
Signals carry information among processes and
components in an architecture.
Signal declaration syntax
signal signal_name : signal_type [:=expression];
signal address : bit_vector(31 downto 0);
signal mem_read : bit := 0;
Signal assignment syntax
[Label:] signal_name <= value [after time_expression]
{, value [after time_expression]};
address <= x18FE5900 after 20 ns;
mem_read <= 1 after 25 ns, 0 after 50 ns, 1 after
110 ns;
data_reg <= 1101_0010;
Wait Statement
Wait causes a process to wait for an event
Syntax
wait [on signal_name {,signal_name}];
wait [until boolean_expression];
wait [for time_expression];
Example
wait on a,b;
wait until Q=1;
wait until clk=1 and clkevent;
wait for 100 us;
wait;
Wait in Process
If Statement
Syntax
[label]:
if boolean_expression then
{sequential_statement}
elsif boolean_expression then
{sequential_statement}
[else
{sequential_statement} ]
end if [lable];
Example
--Check_opcode:
If opcode = 0101 then
data_reg <= rega;
elsif opcode = 0011 then
data_reg <= regb;
else
data_reg <= regc;
flag <= 1;
End if;
Case Statement
Syntax:
[Label:]
case expression is
{ when choice => {sequential_statement} }
end case [label];
Example
Mux: case sel is
when 00 =>
oreg <= ireg1 after 10 ns;
when 01 =>
oreg <= ireg2 after 10 ns;
when 10 =>
oreg <= ireg3 after 10 ns;
when 11 =>
oreg <= ireg4 after 10 ns;
end case;
Loop, While & For Statement
Syntax
[label:] loop
{sequential_statement}
end loop [label];
Syntax
[label:] while condition loop
{sequential_statement}
end loop [label];
Syntax
[label:] for identifier in discrete_range loop
{sequential_statement}
end loop [label];
Example
Next, Exit, Null
Syntax
[label:] next [when boolean_expression];
[label:] exit [when boolean_expression];
[label:] null;
Example
Concurrent Signal Assignments
Conditional signal assignment
[label:] Signal_name <= { value when
boolean_expression else }
value [when boolean_expression];
Selected signal assignment
[label:] with expression select
signal_name <= { value when choices}
value when choices;
Example
Zmux: z <= d0 when sel1=0 and sel2=0 else
d1 when sel1=0 and sel2=1 else
d2 when sel1=1 and sel2=0 else
d3;
------------------------------------------------
Zmux: with sel select
z <= d0 when 00,
d1 when 01,
d2 when 10,
d3 when others;
Subprograms
Procedures
Generalize a statement
Encapsulate a collection of sequential statements that are
executed for their effect
Functions
Generalize an expression
Encapsulate a collection of sequential statements that compute
a result
Procedure
Syntax
procedure identifier [ ( parameter_list ) ] is
{ declarations }
begin
{ sequential statements }
end [procedure];
Syntax of parameter list
[constant|variable|signal] identifier {, } : [mode] type
:= static_expression] {; }
Modes: in | out | inout
Defaults: in is constant, out and inout are variables
Example
PROCEDURE consecutive_data
(SIGNAL target : OUT BIT_VECTOR;
CONSTANT ti : TIME; CONSTANT n : INTEGER)
IS
VARIABLE data : BIT_VECTOR (target'RANGE);
VARIABLE sum, carry : BIT;
BEGIN
FOR i IN 1 TO n LOOP
carry := '1';
FOR j IN data'REVERSE_RANGE LOOP
sum := data (j) XOR carry;
carry := data (j) AND carry;
data (j) := sum;
END LOOP;
target <= TRANSPORT data AFTER ti * i;
END LOOP;
END PROCEDURE consecutive_data;
Example
ARCHITECTURE procedural OF multiplexer8_tester IS
PROCEDURE consecutive_data
(SIGNAL target : OUT BIT_VECTOR;
CONSTANT ti : TIME; CONSTANT n : INTEGER)
IS .
.
END PROCEDURE consecutive_data;
SIGNAL a, b, w2 : BIT_VECTOR (7 DOWNTO 0);
SIGNAL s : BIT;
SIGNAL sel : BIT_VECTOR (0 TO 0);
BEGIN
UUT2: ENTITY WORK.multiplexer8 (direct)
PORT MAP (a, b, s, w2);
consecutive_data (a, 123 NS, 6);
consecutive_data (b, 79 NS, 6);
consecutive_data (sel, 119 NS, 8);
s <= sel(0);
END ARCHITECTURE procedural;
Nesting Procedure
PROCEDURE inc (VARIABLE invec : INOUT BIT_VECTOR) IS
VARIABLE sum, carry : BIT;
BEGIN
carry := '1';
FOR j IN invec'REVERSE_RANGE LOOP
sum := invec (j) XOR carry;
carry := invec (j) AND carry;
invec (j) := sum;
END LOOP;
END PROCEDURE inc;
--
PROCEDURE consecutive_data
(SIGNAL target : OUT BIT_VECTOR;
CONSTANT ti : TIME; CONSTANT n : INTEGER) IS
VARIABLE data : BIT_VECTOR (target'RANGE);
BEGIN
FOR i IN 1 TO n LOOP
inc (data);
target <= TRANSPORT data AFTER ti * i;
END LOOP;
END PROCEDURE consecutive_data;
Functions
Syntax
[pure|impure] function identifier [ (
parameter_list ) ]
return type_name is
{ declarations }
begin
{ sequential statements }
end [function] [identifier];
Mode of parameters is restricted to in.
Example
function limit ( value, min, max : integer ) return
integer is
begin
if value > max then
return max;
elsif value < min then
return min;
else
return value;
end if;
end function;
Example
ARCHITECTURE functional OF multiplexer IS
FUNCTION mux (databits : BIT_VECTOR; . . .
.
.
.
END FUNCTION mux;