Professional Documents
Culture Documents
Ad-hoc
Structured
- Top-Down
- Bottom-Up
- Mixed
Why structured design?
Algorithmic design
Architecture design
Tape-out
Why using HDL?
Off-the-shelf
Low development
High performance
costs
VHDL description
Functional sim.
Specs
System Level
Design Synthesis Post-synthesis
Front End sim.
RTL
Description
Functional
Verification Implementation
Timing sim.
Gate Level
Synthesis
Simulation
ASIC FPGA
Logic synthesis
Gate-level
RTL code
Logic netlist
(Verilog/
synthesis (Verilog/
VHDL)
VHDL)
Area report
Timing report
Constraints Power consumption report
Logic synthesis(Cont.)
LIBRARY ieee ;
USE ieee.std_logic_1164.all ;
ENTITY flipflop IS
PORT ( D, Clock : IN STD_LOGIC ;
Q : OUT STD_LOGIC
);
END flipflop ;
Synthesis
ARCHITECTURE Behavior_1 OF flipflop IS
BEGIN
PROCESS ( Clock )
BEGIN
IF Clock'EVENT AND Clock = '1' THEN
Q <= D ;
END IF ;
END PROCESS ; D Q
END Behavior_1 ;
Clock
Review of digital system
structure
Structure of a typical digital system
Data Inputs Control Inputs
Control
Signals
Execution Control
Unit Unit
(Data path) (Control)
Output Outputs
function
Finite state machines refresher(Cont.)
Mealy FSM
Output is a function of a present state and inputs
Output Outputs
function
Finite state machines refresher(Cont.)
Moore vs. Mealy FSM
transition
Moore and Mealy condition 1
FSMs can be
state 2 /
functionally equivalent state 1 /
output 2
output 1
Equivalent Mealy FSM transition
can be derived from condition 2
Moore FSM and vice
versa
Mealy FSM has richer transition condition 1 /
description and usually output 1
requires smaller
number of states state 1 state 2
Smaller circuit area transition condition 2 /
output 2
Finite state machines refresher(Cont.)
Moore vs. Mealy FSM(Cont.)
reset
0
S0: No S1: 1 S2: 10
Meaning elements observed observed
of states: of the
sequence
observed
Finite state machines refresher(Cont.)
Sequence 10 recognizer example(Cont.)
Mealy FSM that recognizes sequence 10
0/0 1/0 1/0
S0 S1
reset 0/1
S0: No S1: 1
Meaning elements observed
of states: of the
sequence
observed
Finite state machines refresher(Cont.)
Sequence 10 recognizer example(Cont.)
clock
0 1 0 0 0
input
S0 S1 S2 S0 S0
Moore
S0 S1 S0 S0 S0
Mealy
Finite state machines refresher(Cont.)
Sequence
resetn
11 recognizer example
w = 1
w = 0 A z = 0 B z = 0
w = 0
w = 0 w = 1
C z = 1
resetn
w = 1 w = 1 z = 0
w = 0 z = 0 A B w = 1 z = 1
w = 0 z = 0
VHDL
VHDL
Technology/vendor independent
Portable
Reusable
Two points of view for a given component
Architecture 1
One entity can have
many different architectures.
Architecture 2
Architecture 3
Entity declaration
Entity Declaration describes the interface of the
component, i.e. input and output ports.
ENTITY entity_name IS
PORT (
port_name : signal_mode signal_type;
port_name : signal_mode signal_type;
.
port_name : signal_mode signal_type);
END entity_name;
Architecture
a Signal can
z a be read
inside the
c Cant read entity
out within an
entity
Driver resides
Driver resides inside the entity c <= z
outside the entity Driver may reside
both inside and outside
the entity
IN mode OUT mode INOUT mode
Port modes(Cont.)
Entity Entity
Port signal Port signal
z x z
Port signal Z can Signal can be
c be read inside the c read inside the
entity entity
Driver resides c <= z Driver resides z <= x
inside the entity inside the entity
c <= x
In: Data comes in this port and can only be read within the entity.
It can appear only on the right side of a signal or variable
assignment.
Out: The value of an output port can only be updated within the
entity. It cannot be read. It can only appear on the left side of a
signal assignment.
LIBRARY library_name;
USE library_name.package_name.package_parts;
Fundamental parts of a library
LIBRARY
PACKAGE 1 PACKAGE 2
TYPES TYPES
CONSTANTS CONSTANTS
FUNCTIONS FUNCTIONS
PROCEDURES PROCEDURES
COMPONENTS COMPONENTS
Libraries
Std
Specifies pre-defined data types
(BIT, BOOLEAN, INTEGER, REAL,
SIGNED, UNSIGNED, etc.), arithmetic
operations, basic type conversion Visible by default
functions, basic text i/o functions, etc.
work
Current designs after compilation
STD_LOGIC type
LIBRARY ieee;
USE ieee.std_logic_1164.all;
ENTITY nand_gate IS
PORT(
a : IN STD_LOGIC;
b : IN STD_LOGIC;
z : OUT STD_LOGIC
);
END nand_gate;
1
X
Contention on the bus
X
0
STD_LOGIC type demystified(Cont.)
0
STD_LOGIC type demystified(Cont.)
VDD
VDD
H
1
0
L
STD_LOGIC type demystified(Cont.)
Do not care.
Can be assigned to outputs for the case of invalid
- inputs(may produce significant improvement in resource
utilization after synthesis).
Use with caution
Resolving of STD_LOGIC levels
X 0 1 Z W L H -
X X X X X X X X X
0 X 0 X 0 0 0 0 X
1 X X 1 1 1 1 1 X
Z X 0 1 Z W L H X
W X 0 1 W W W W X
L X 0 1 L W L W X
H X 0 1 H W W H X
- X X X X X X X X
Signals
SIGNAL a : STD_LOGIC;
a
1 wire
b
8 bus
Standard logic vectors
SIGNAL a: STD_LOGIC ;
SIGNAL b: STD_LOGIC_VECTOR( 3 DOWNTO 0);
SIGNAL c: STD_LOGIC_VECTOR( 3 DOWNTO 0);
SIGNAL d: STD_LOGIC_VECTOR( 7 DOWNTO 0);
SIGNAL e: STD_LOGIC_VECTOR(15 DOWNTO 0);
SIGNAL f: STD_LOGIC_VECTOR( 8 DOWNTO 0);
.
a <= 1;
b <= 0000; -- Binary base assumed by default
c <= B0000; -- Binary base explicitly specified
d <= 0110_0111; -- You can use _ to increase readability
e <= XAF67; -- Hexadecimal base
f <= O723; -- Octal base
Vectors and concatenation
SIGNAL a : STD_LOGIC_VECTOR(3 DOWNTO 0);
SIGNAL b : STD_LOGIC_VECTOR(3 DOWNTO 0);
SIGNAL c, d, e: STD_LOGIC_VECTOR(7 DOWNTO 0);
a <= 0000;
b <= 1111;
c <= a & b; -- c <= 00001111
e <= 0 & 0 & 0 & 0 & 1 & 1 &1 & 1; -- e <= 00001111
Fixed 16-bit rotation example
a(15) a(14) a(13) a(12) a(11) a(10) a(9) a(8) a(7) a(6) a(5) a(4) a(3) a(2) a(1) a(0)
<<< 3
a(12) a(11) a(10) a(9) a(8) a(7) a(6) a(5) a(4) a(3) a(2) a(1) a(0) a(15) a(14) a(13)
a(15) a(14) a(13) a(12) a(11) a(10) a(9) a(8) a(7) a(6) a(5) a(4) a(3) a(2) a(1) a(0)
<<< 5
a(10) a(9) a(8) a(7) a(6) a(5) a(4) a(3) a(2) a(1) a(0) a(15) a(14) a(13) a(12) a(11)
Fixed 16-bit rotation example(Cont.)
LIBRARY ieee ;
USE ieee.std_logic_1164.all ;
ENTITY fixed_rotator_left_16 IS
GENERIC ( L : INTEGER := 1);
PORT ( a : IN STD_LOGIC_VECTOR (15 DOWNTO 0);
y : OUT STD_LOGIC_VECTOR (15 DOWNTO 0)
);
END fixed_rotator_left_16 ;
BEGIN
END XOR3_DATAFLOW;
Dataflow description
COMPONENT XOR2 IS
PORT(
I1 : IN STD_LOGIC;
I2 : IN STD_LOGIC;
Y : OUT STD_LOGIC
);
END COMPONENT;
BEGIN
U1: XOR2 PORT MAP (I1 => A,
I2 => B,
Y => U1_OUT);
Simulated Outputs
Manual Calculations
or
expected results
Reference Software
Implementation
(C, Java, Matlab )
Test bench(Cont.)
The same test bench can be used to test multiple
implementations of the same circuit (multiple architectures)
Test bench
Design entity
ARCHITECTURE arch_TB OF TB IS
Order of execution
statement in the process. test_vector<=01;
After execution of the last WAIT FOR 10 ns;
statement, the control is again
passed to the beginning of the test_vector<=10;
process. WAIT FOR 10 ns;
test_vector<=11;
WAIT FOR 10 ns;
END PROCESS;
Program control is passed to the first
statement after BEGIN
PROCESS with a WAIT Statement
Order of execution
WAIT statement is executed.
This form of WAIT can be used in WAIT FOR 10 ns;
a process included in a test bench test_vector<=10;
when all possible combinations of WAIT FOR 10 ns;
inputs have been tested or a non- test_vector<=11;
periodical signal has to be
generated. WAIT;
END PROCESS;
0 1 2 3 0 1 2 3
Test bench signals generation
Generating selected values of one
input
SIGNAL test_vector : STD_LOGIC_VECTOR(2 DOWNTO 0);
BEGIN
.......
testing: PROCESS
BEGIN
test_vector <= "000";
WAIT FOR 10 ns;
test_vector <= "001";
WAIT FOR 10 ns;
test_vector <= "010";
WAIT FOR 10 ns;
test_vector <= "011";
WAIT FOR 10 ns;
test_vector <= "100";
WAIT FOR 10 ns;
END PROCESS testing;
........
END behavioral;
Test bench signals generation(Cont.)
Generating all values of one input
BEGIN
.......
testing: PROCESS
BEGIN
WAIT FOR 10 ns;
test_vector <= test_vector + 1;
END PROCESS testing;
........
END behavioral;
Test bench signals generation(Cont.)
Generating all possible values of two
inputs
SIGNAL test_ab : STD_LOGIC_VECTOR (1 DOWNTO 0);
SIGNAL test_sel : STD_LOGIC_VECTOR (1 DOWNTO 0);
BEGIN
.......
double_loo: PPROCESS
BEGIN
test_ab <="00";
test_sel <="00";
FOR I IN 0 TO 3 LOOP
FOR J IN 0 TO 3 LOOP
WAIT FOR 10 ns;
test_ab <= test_ab + 1;
END LOOPl;
test_sel <= test_sel + 1;
END LOOP;
END PROCESS double_loo;
........
END behavioral;
Test bench signals generation(Cont.)
Generating periodical signals
CONSTANT clk1_period : TIME := 20 ns;
CONSTANT clk2_period : TIME := 200 ns;
SIGNAL clk1 : STD_LOGIC;
SIGNAL clk2 : STD_LOGIC := 0;
BEGIN
.......
clk1_generator: PROCESS
clk1 <= 0;
WAIT FOR clk1_period/2;
clk1 <= 1;
WAIT FOR clk1_period/2;
END PROCESS;
BEGIN
.......
reset1_generator: PROCESS
reset1 <= 1;
WAIT FOR reset_width;
reset1 <= 0;
WAIT;
END PROCESS reset1_generator;
reset2_generator: PROCESS
WAIT FOR reset_width;
reset2 <= 0;
WAIT;
END PROCESS;
.......
END behavioral;
ASSERTs and REPORTs
ASSERT
ASSERT is a non-synthesizable statement
whose purpose is to write out messages on
the screen when problems are found during
simulation.
Logic operators
and or nand nor xor not xnor
Correct
y <= (a and b) or (c and d) ;
Arithmetic operators
To use basic arithmetic operations involving std logic vectors you need to
include the following library packages:
LIBRARY ieee;
USE ieee.std_logic_1164.all;
USE ieee.std_logic_unsigned.all;
or
USE ieee.std_logic_signed.all;
You can use standard +, -, * operators to perform addition, subtraction, and
multiplication:
C <= A + B;
Dataflow design style(Cont.)
16-bit Unsigned adder example
LIBRARY ieee ;
USE ieee.std_logic_1164.all ;
USE ieee.std_logic_unsigned.all ;
ENTITY adder16 IS
PORT ( Cin : IN STD_LOGIC ;
X, Y : IN STD_LOGIC_VECTOR (15 DOWNTO 0) ;
S : OUT STD_LOGIC_VECTOR (15 DOWNTO 0) ;
Cout : OUT STD_LOGIC
);
END adder16 ;
ENTITY adder16 IS
PORT ( Cin : IN STD_LOGIC ;
X, Y : IN SIGNED (15 DOWNTO 0) ;
S : OUT SIGNED (15 DOWNTO 0) ;
Cout, Overflow : OUT STD_LOGIC
);
END adder16 ;
ENTITY adder16 IS
PORT ( X, Y : IN INTEGER RANGE -32768 TO 32767 ;
S : OUT INTEGER RANGE -32768 TO 32767
);
END adder16 ;
Value N
Value N-1
Value 2 Value 1
Condition N-1
Condition 2 Condition 1
Relational operators
Relational operators
= /= < <= > >=
compare a = bc
Incorrect
WHEN a = b and c ELSE
equivalent to
WHEN (a = b) and c ELSE
Correct
WHEN a = (b and c) ELSE
VHDL operators
Dataflow design style(Cont.)
2-to-1 Multiplexer example
LIBRARY ieee ;
USE ieee.std_logic_1164.all ;
ENTITY mux2to1 IS
PORT ( w0, w1, s : IN STD_LOGIC ;
f : OUT STD_LOGIC
);
END mux2to1 ; w0 w1
BEGIN f
f <= w0 WHEN s = '0' ELSE w1 ;
END Behavior ;
Dataflow design style(Cont.)
Tri-state buffer example
LIBRARY ieee;
USE ieee.std_logic_1164.all;
ENTITY tri_state IS
PORT (
ena : IN STD_LOGIC;
input : IN STD_LOGIC_VECTOR (7 DOWNTO 0);
output : OUT STD_LOGIC_VECTOR (7 DOWNTO 0)
); input output
END tri_state; ena
ENTITY compare IS
PORT ( A, B : IN STD_LOGIC_VECTOR (3 DOWNTO 0) ;
AeqB, AgtB, AltB : OUT STD_LOGIC
);
END compare ;
ENTITY compare IS
PORT ( A, B : IN STD_LOGIC_VECTOR (3 DOWNTO 0) ;
AeqB, AgtB, AltB : OUT STD_LOGIC
);
END compare ;
ENTITY priority IS
PORT ( w : IN STD_LOGIC_VECTOR (3 DOWNTO 0) ;
y : OUT STD_LOGIC_VECTOR (1 DOWNTO 0) ;
z : OUT STD_LOGIC ) ;
END priority ;
expression1 choices_1
expression2 choices_2
target_signal
expressionN choices_N
choice expression
Dataflow design style(Cont.)
WITH-SELECT-WHEN(Cont.)
Allowed formats of choices expressions
WHEN value
WHEN value_1 to value_2
WHEN value_1 | value_2 | .... | value N
ENTITY mux4to1 IS
PORT ( w0, w1, w2, w3 : IN STD_LOGIC ;
s : IN STD_LOGIC_VECTOR (1 DOWNTO 0) ;
f : OUT STD_LOGIC
);
END mux4to1 ;
ENTITY dec2to4 IS
PORT ( w : IN STD_LOGIC_VECTOR (1 DOWNTO 0) ;
En : IN STD_LOGIC ;
y : OUT STD_LOGIC_VECTOR (0 TO 3)
);
END dec2to4 ;
ENTITY parity IS
PORT(
parity_in : IN STD_LOGIC_VECTOR (7 DOWNTO 0);
parity_out : OUT STD_LOGIC
);
END parity;
Dataflow design style(Cont.)
8-Bits parity example(Cont.)
BEGIN
END parity_dataflow_1;
Dataflow design style(Cont.)
8-Bits parity example(Cont.)
BEGIN
END parity_dataflow_2;
Structural design style
VHDL Design
Styles
Component instantiation
(PORT MAP)
Generate scheme for component instantiations
(FOR-GENERATE)
Component instantiation with generic
(GENERIC MAP, PORT MAP)
Structural design style(Cont.)
Example
s(0)
r(0) p(0)
r(1)
w0 q(0) w y
p(1) y0 0 0 z(0)
r(2) w1 q(1)
p(2) y1 w y z(1)
r(3) w2 1 1
ena y
w3 z 2 z(2)
r(4) p(3) priority y
En 3 z(3)
r(5) dec2to4
s(1)
Structural design style(Cont.)
Example(Cont.)
LIBRARY ieee ;
USE ieee.std_logic_1164.all ;
ENTITY priority_resolver IS
PORT (r : IN STD_LOGIC_VECTOR (5 DOWNTO 0) ;
s : IN STD_LOGIC_VECTOR (1 DOWNTO 0) ;
z : OUT STD_LOGIC_VECTOR (3 DOWNTO 0)
);
END priority_resolver;
COMPONENT priority
PORT (w : IN STD_LOGIC_VECTOR (3 DOWNTO 0) ;
y : OUT STD_LOGIC_VECTOR (1 DOWNTO 0) ;
z : OUT STD_LOGIC
);
END COMPONENT ;
COMPONENT dec2to4
PORT (w : IN STD_LOGIC_VECTOR (1 DOWNTO 0) ;
En : IN STD_LOGIC ;
y : OUT STD_LOGIC_VECTOR (0 TO 3) ) ;
END COMPONENT ;
Structural design style(Cont.)
Example(Cont.)
BEGIN
u1: mux2to1 PORT MAP (w0 => r(0) ,
w1 => r(1),
s => s(0),
f => p(0));
p(1) <= r(2);
p(1) <= r(3);
BEGIN
data <= memory(addr);
END rom;
Package declaration
Example
LIBRARY ieee ;
USE ieee.std_logic_1164.all ;
PACKAGE GatesPkg IS
COMPONENT mux2to1
PORT (w0, w1, s : IN STD_LOGIC ;
f : OUT STD_LOGIC
);
END COMPONENT ;
COMPONENT priority
PORT (w : IN STD_LOGIC_VECTOR (3 DOWNTO 0) ;
y : OUT STD_LOGIC_VECTOR (1 DOWNTO 0) ;
z : OUT STD_LOGIC ) ;
END COMPONENT ;
COMPONENT dec2to4
PORT (w : IN STD_LOGIC_VECTOR (1 DOWNTO 0) ;
En : IN STD_LOGIC ;
y : OUT STD_LOGIC_VECTOR (0 TO 3) ) ;
END COMPONENT ;
Package declaration(Cont.)
Example(Cont.)
CONSTANT ADDAB : STD_LOGIC_VECTOR (3 DOWNTO 0) := "0000";
CONSTANT ADDAM : STD_LOGIC_VECTOR (3 DOWNTO 0) := "0001";
CONSTANT SUBAB : STD_LOGIC_VECTOR (3 DOWNTO 0) := "0010";
CONSTANT SUBAM : STD_LOGIC_VECTOR (3 DOWNTO 0) := "0011";
CONSTANT NOTA : STD_LOGIC_VECTOR (3 DOWNTO 0) := "0100";
CONSTANT NOTB : STD_LOGIC_VECTOR (3 DOWNTO 0) := "0101";
CONSTANT NOTM : STD_LOGIC_VECTOR (3 DOWNTO 0) := "0110";
CONSTANT ANDAB : STD_LOGIC_VECTOR (3 DOWNTO 0) := "0111";
END GatesPkg;
Package declaration(Cont.)
Example(Cont.)
LIBRARY ieee ;
USE ieee.std_logic_1164.all ;
USE work.GatesPkg.all;
ENTITY priority_resolver IS
PORT (r : IN STD_LOGIC_VECTOR (5 DOWNTO 0) ;
s : IN STD_LOGIC_VECTOR (1 DOWNTO 0) ;
z : OUT STD_LOGIC_VECTOR (3 DOWNTO 0)
);
END priority_resolver;
FOR structural
END FOR;
END SimpleCfg;
Structural design style(Cont.)
16-to-1 Multiplexer example
s0
s1
w0
w3
w4 s2
s3
w7
f
w8
w 11
w 12
w 15
Structural design style(Cont.)
16-to-1 Multiplexer example(Cont.)
Straightforward code
LIBRARY ieee ;
USE ieee.std_logic_1164.all ;
ENTITY Example1 IS
PORT ( w : IN STD_LOGIC_VECTOR (0 TO 15) ;
s : IN STD_LOGIC_VECTOR (3 DOWNTO 0) ;
f : OUT STD_LOGIC
);
END Example1 ;
Structural design style(Cont.)
16-to-1 Multiplexer example(Cont.)
Straightforward code(Cont.)
COMPONENT mux4to1
PORT ( w0, w1, w2, w3 : IN STD_LOGIC ;
s : IN STD_LOGIC_VECTOR (1 DOWNTO 0) ;
f : OUT STD_LOGIC
);
END COMPONENT ;
SIGNAL m : STD_LOGIC_VECTOR (0 TO 3) ;
BEGIN
Mux1: mux4to1 PORT MAP ( w(0), w(1), w(2), w(3), s(1 DOWNTO 0), m(0) ) ;
Mux2: mux4to1 PORT MAP ( w(4), w(5), w(6), w(7), s(1 DOWNTO 0), m(1) ) ;
Mux3: mux4to1 PORT MAP ( w(8), w(9), w(10), w(11), s(1 DOWNTO 0), m(2) ) ;
Mux4: mux4to1 PORT MAP ( w(12), w(13), w(14), w(15), s(1 DOWNTO 0), m(3) ) ;
Mux5: mux4to1 PORT MAP ( m(0), m(1), m(2), m(3), s(3 DOWNTO 2), f ) ;
END Structure ;
Structural design style(Cont.)
16-to-1 Multiplexer example(Cont.)
Modified code
ARCHITECTURE Structure OF Example1 IS
COMPONENT mux4to1
PORT ( w0, w1, w2, w3 : IN STD_LOGIC ;
s : IN STD_LOGIC_VECTOR (1 DOWNTO 0) ;
f : OUT STD_LOGIC ) ;
END COMPONENT ;
SIGNAL m : STD_LOGIC_VECTOR (0 TO 3) ;
BEGIN
G1: FOR i IN 0 TO 3 GENERATE
Muxes: mux4to1 PORT MAP (
w(4*i), w(4*i+1), w(4*i+2), w(4*i+3), s(1 DOWNTO 0), m(i) ) ;
END GENERATE ;
Mux5: mux4to1 PORT MAP ( m(0), m(1), m(2), m(3), s(3 DOWNTO 2), f ) ;
END Structure ;
Structural design style(Cont.)
Variable 16-bit rotator example
LIBRARY ieee ;
USE ieee.std_logic_1164.all ;
ENTITY variable_rotator_16 is
PORT(
A : IN STD_LOGIC_VECTOR (15 DOWNTO 0); A
B : IN STD_LOGIC_VECTOR ( 3 DOWNTO 0);
C : OUT STD_LOGIC_VECTOR (15 DOWNTO 0) 16
);
END variable_rotator_16;
4
B A <<< B
16
C
Structural design style(Cont.)
Variable 16-bit rotator example(Cont.)
LIBRARY ieee ;
USE ieee.std_logic_1164.all ;
COMPONENT mux2to1_16
PORT ( w0 : IN STD_LOGIC_VECTOR (15 DOWNTO 0);
w1 : IN STD_LOGIC_VECTOR (15 DOWNTO 0);
s : IN STD_LOGIC ;
f : OUT STD_LOGIC_VECTOR(15 DOWNTO 0)
);
END COMPONENT ;
COMPONENT fixed_rotator_left_16
GENERIC ( L : INTEGER := 1);
PORT ( a : IN STD_LOGIC_VECTOR (15 DOWNTO 0);
y : OUT STD_LOGIC_VECTOR (15 DOWNTO 0)
);
END COMPONENT ;
Structural design style(Cont.)
Variable 16-bit rotator example(Cont.)
TYPE array1 IS ARRAY (0 TO 4) OF STD_LOGIC_VECTOR (15 DOWNTO 0);
TYPE array2 IS ARRAY (0 TO 3) OF STD_LOGIC_VECTOR (15 DOWNTO 0);
SIGNAL Al : array1;
SIGNAL Ar : array2;
BEGIN
Al(0) <= A;
G: FOR i IN 0 TO 3 GENERATE
ROT_I: fixed_rotator_left_16
GENERIC MAP (L => 2** i)
PORT MAP ( a => Al(i) ,
y => Ar(i));
MUX_I: mux2to1_16 PORT MAP (w0 => Al(i),
w1 => Ar(i),
s => B(i),
f => Al(i+1));
END GENERATE;
C <= Al(4);
END variable_rotator_16;
Behavioral design style
VHDL Design
Styles
CASE condition IS
CASE statement WHEN choice_1 =>
Choices have to cover statements
all possible values of WHEN choice_2 =>
statements
the condition
Use OTHERS to specify
all remaining cases WHEN OTHERS =>
statements
END CASE;
Behavioral design style(Cont.)
Statement Part(Cont.)
Loop Statement
FOR i IN range LOOP
statements
END LOOP;
ENTITY latch IS
PORT ( D, Clock : IN STD_LOGIC ;
Q : OUT STD_LOGIC
);
END latch ;
ENTITY flipflop IS
PORT ( D, Clock : IN STD_LOGIC ;
Q : OUT STD_LOGIC
);
END flipflop ;
ENTITY flipflop IS
PORT ( D, Clock : IN STD_LOGIC ;
Q : OUT STD_LOGIC
);
END flipflop ;
ENTITY flipflop IS
PORT ( D, Resetn, Clock : IN STD_LOGIC ;
Q : OUT STD_LOGIC
);
END flipflop ;
ENTITY reg8 IS
PORT ( D : IN STD_LOGIC_VECTOR (7 DOWNTO 0) ;
Resetn, Clock : IN STD_LOGIC ;
Q : OUT STD_LOGIC_VECTOR (7 DOWNTO 0)
);
END reg8 ;
ENTITY upcount IS
PORT ( Clock, Resetn, Enable : IN STD_LOGIC ;
Q : OUT STD_LOGIC_VECTOR (3 DOWNTO 0)
);
END upcount ;
Enable 4
Q
Clock
upcount
Resetn
Behavioral design style(Cont.)
4-bit Up-counter with asynchronous reset(Cont.)
ARCHITECTURE Behavior OF upcount IS
SIGNAL Count : STD_LOGIC_VECTOR (3 DOWNTO 0) ;
BEGIN
PROCESS ( Clock, Resetn )
BEGIN
IF Resetn = '0' THEN
Count <= "0000" ;
ELSIF (Clock'EVENT AND Clock = '1') THEN
IF Enable = '1' THEN
Count <= Count + 1 ;
END IF ;
END IF ;
END PROCESS ;
Q <= Count ; Enable 4
END Behavior ; Q
Clock
upcount
Resetn
Behavioral design style(Cont.)
4-bit Shift register with parallel load
Load
D(3)
D(2) D(1) D(0)
Sin
D Q D Q D Q D Q
Clock
Enable
ENTITY shiftn IS
GENERIC ( N : INTEGER := 8 ) ;
PORT ( D : IN STD_LOGIC_VECTOR (N-1 DOWNTO 0) ;
Enable : IN STD_LOGIC ;
Load : IN STD_LOGIC ;
Sin : IN STD_LOGIC ;
Clock : IN STD_LOGIC ;
Q : BUFFER STD_LOGIC_VECTOR (N-1 DOWNTO 0)
);
END shiftn ;
Behavioral design style(Cont.)
N-bit Shift register with parallel load(Cont.)
cont ...
Behavioral design style(Cont.)
Manual state assignment using constants
ARCHITECTURE Behavior OF simple IS
SUBTYPE ABC_STATE IS STD_LOGIC_VECTOR (1 DOWNTO 0);
Reset 000
Idle
0xx 1xx
reset
gnt1 g 1 = 1
clock xx1
Mixed design style
ARCHITECTURE ARCHITECTURE_NAME OF ENTITY_NAME IS
BEGIN
Concurrent statements:
Concurrent simple signal assignment
Conditional signal assignment
Selected signal assignment
Generate statement
Concurrent Statements
Component instantiation statement
Process statement
inside process you can use only sequential statements
END ARCHITECTURE_NAME;
SIGNALs and concurrent statements
global
FUNCTION / ENTITY
PROCEDURE
local for all architectures
of a given entity
ARCHITECTURE
Declarative part
x <= conv_integer(a);
BEGIN
END behavioral;
FUNCTIONs(Cont.)
Example: PACKAGE containing a
FUNCTION
LIBRARY IEEE;
USE IEEE.std_logic_1164.all;
PACKAGE specialFunctions IS
END specialFunctions
FUNCTIONs(Cont.)
Example: PACKAGE containing a FUNCTION(Cont.)
PACKAGE BODY specialFunctions IS
BEGIN
FOR i IN 1 TO Exp LOOP
Result := Result * N;
END LOOP;
RETURN( Result );
END Pow;
END specialFunctions
FUNCTIONs(Cont.)
Example: Type conversion FUNCTION
LIBRARY ieee;
USE ieee.std_logic_1164.all;
--------------------------------------------------------------------------------------
PACKAGE my_package IS
FUNCTION conv_integer ( SIGNAL vector: STD_LOGIC_VECTOR)
RETURN INTEGER;
END my_package;
FUNCTIONs(Cont.)
Example: Type conversion FUNCTION(Cont.)
PACKAGE BODY my_package IS
FUNCTION conv_integer (SIGNAL vector: STD_LOGIC_VECTOR)
RETURN INTEGER;
VARIABLE result: INTEGER RANGE 0 TO 2**vectorLENGTH - 1;
VARIABLE carry: STD_LOGIC;
BEGIN
IF (vector(vectorHIGH)=1 THEN result:=1;
ELSE result := 0;
FOR i IN (vectorHIGH-1) DOWNTO (vectorLOW) LOOP
result := result*2;
IF (vector(i) = 1 THEN result := result+1;
END IF;
RETURN result;
END conv_integer;
END my_package;
FUNCTIONs(Cont.)
Example: Type conversion FUNCTION(Cont.)
LIBRARY ieee;
USE ieee.std_logic_1164.all;
USE work.my_package.all;
----------------------------------------------------------
ENTITY conv_int2 IS
PORT ( a: IN STD_LOGIC_VECTOR (0 TO 3);
y: OUT INTEGER RANGE 0 TO 15);
END conv_int2;
----------------------------------------------------------
ARCHITECTURE my_arch OF conv_int2 IS
BEGIN
y <= conv_integer(a);
END my_arch;
PROCEDUREs
PROCEDUREs do not return a value
PROCEDUREs are called using formal and actual parameters
the same way as components
PROCEDUREs may modify parameters passed to them
Each parameter must have a mode: IN, OUT, INOUT
Parameters can be constants (including generics), signals
(including ports), and variables; the default for inputs (mode
in) is a constant, the default for outputs (modes out and inout)
is a variable
When passing parameters, range specification should be
included (for example RANGE for INTEGERS, and TO/DOWNTO
for STD_LOGIC_VECTOR)
Procedure calls are statements on their own
PROCEDUREs(Cont.)
PROCEDURE syntax
IF (a > b) THEN
compute_min_max(in1, in2, in3, out1, out2);
.......
PROCEDUREs(Cont.)
Example
LIBRARY ieee;
USE ieee.std_logic_1164.all;
ENTITY min_max IS
GENERIC (limit: INTEGER := 255);
PORT (
ena : IN BIT;
inp1, inp2 : IN INTEGER RANGE 0 TO limit;
min_out, max_out: OUT INTEGER RANGE 0 TO limit
);
END min_max;
PROCEDUREs(Cont.)
Example(Cont.)
ARCHITECTURE my_architecture OF min_max IS
PROCEDURE sort (SIGNAL in1, in2: IN INTEGER RANGE 0 TO limit;
SIGNAL min, max: OUT INTEGER RANGE 0 TO limit) IS
BEGIN
BEGIN
PROCESS (ena)
BEGIN
IF (ena=1) THEN
sort (inp1, inp2, min_out, max_out);
END IF;
END PROCESS;
END my_architecture;
Operator as a FUNCTION
LIBRARY ieee;
USE ieee.std_logic_1164.al;
----------------------------------------------------------
PACKAGE my_package IS
FUNCTION "+" (a, b: STD_LOGIC_VECTOR)
RETURN STD_LOGIC_VECTOR;
END my_package;
Operator as a FUNCTION(Cont.)
FUNCTION + ( L: STD_LOGIC_VECTOR;
R: STD_LOGIC_VECTOR)
RETURN STD_LOGIC_VECTOR;
FUNCTION + ( L: STD_LOGIC_VECTOR;
R: integer)
RETURN STD_LOGIC_VECTOR;
FUNCTION + ( L: STD_LOGIC_VECTOR;
R: STD_LOGIC)
RETURN STD_LOGIC_VECTOR;
Operator overloading(Cont.)
Examples
SIGNAL count: STD_LOGIC_VECTOR (7 DOWNTO 0);
Array attributes
Records
Physical data types
Recommended rules for Synthesis
Combinatorial
reg1 Logic1 &
Logic2& Logic3
reg2
Recommended practice
reg1
reg2 reg3
reg1
reg3
Time
reg1 reg1 may be driven by time
critical path
critical function, hence will
have different optimization
constraints
Slow Logic
reg3
Recommended rules for Synthesis(Cont.)
Separate FSM with random logic
Top
Use FSM optimization tool
Random
Logic reg3
Recommended rules for Synthesis(Cont.)
Maintain a reasonable block size