Professional Documents
Culture Documents
With VHDL
1
Synthesis is used for decreasing the area
increase the speed
and
It is used for decreasing the power consumption.
VHDL / Verilog
RTL Optimization
(i)Constant folding
(ii)Loop Unrolling:
All loop statements are unrolled to a series
of individual statements.
Different implementation of Arithmetic operations have different area and timing constarints
• Depending of our constraints we decide which is best for him
• Suppose “+” operation can be performed using different adders user will pick which
suits for him.
Y1= A+B+X
Y2=A+Y+B
Y1= A+B+X Y2= A+Y+B
A B
A Y
X
+ +
B
+ +
Y1
Y2
• Common Sub-Expression Sharing
A B
Y
x +
+ +
Y1
Y2
If (s) + +
Y=A+B
Else
Y=C+D
MUX
Y
S
A B
MUX MUX
D
C
S S
Y
Synthesis
• Converts behavioral model to structural model
--Behavioral model --Structural model
architecture behav of mux is architecture behav of mux is
begin signal Sbar:std_logic
p1: process(A,B,S) Synthesis begin
begin g1: not port map (Sbar,S);
if (S = '0') then g2: and port map (ASbar,A,Sbar);
Y <= A; g3: and port map (BS,B,S);
else g4: or port map (Y,ASbar,BS);
Y <= B; end;
end if; A
end process p1;
end; S Y
10
Why is Understanding of
Synthesis Process Important?
Behavioral model Expected synthesized design
architecture behav of ckt1 is
begin
p1: process(A,B,S1,S2)
begin
Synthesis
if (S1 = '0') and (S2 = '1') then
Y <= A;
elsif (S2 = '0') then
Y <= B; Actual synthesized design (oops!!!)
--else I do not care
--what is assigned to Y
end if;
end process p1;
end;
11
Why is Understanding of
Synthesis Process Important?
--Code snippet
if (S1 = '0') and (S2 = '1') then
Y <= A;
elsif (S2 = '0') then
Y <= B;
--else do not care
end if;
Prev.
1
0 Stored
Value
A
B A
B
Latch
disabled
0
-1 1
-0
1
0
1
0 10
1
0
1
0 0
1
12
Issues with extra hardware
13
Why is Understanding of
Synthesis Process Important?
Corrected behavioral code
Actual synthesized design
architecture behav of ckt1 is
begin
p1: process(A,B,S1,S2)
begin
if (S1 = '0') and (S2 = '1') then
Y <= A;
elsif (S2 = '0') then
Y <= B;
else
--a value is assigned to Y in the “else” clause
Y <= ‘X’; Thumb rule:
end if; Ensure all cases taken care of, using
end process p1; “else” clause for combinational scenarios
end;
14
Types of Synthesized Circuits
16
Multiplexer: Using “case” Statement
entity Mux4 is
port (i: in BIT_VECTOR(3 downto 0);
sel: in BIT_VECTOR(1 downto 0);
s: out BIT);
end Mux4;
17
Multiplexer using concurrent signal
assignment
architecture Synthesis_2 of Mux4 is
begin
with sel select
s <= i(0) when "00",
i(1) when "01",
i(2) when "10",
i(3) when "11"; Thumb rule:
Cover all conditions
end Synthesis_2;
18
Multiplexer using an array
entity Mux8 is
port ( InBus : in STD_LOGIC_VECTOR(7 downto 0);
Sel : in INTEGER range 0 to 7;
OutBit : out STD_LOGIC);
end Mux8;
19
Synthesizing arithmetic circuits
22
Resource sharing for
mutually-exclusive operations
23
Equivalent model
process (a,b,c,sel) begin
variable tmp: unsigned(0 downto 0) ;
begin
if (sel=‘0’) then
tmp:= b ; --mux will select b or c
else
tmp:= c ;
end if ;
z <= a + tmp; --mux output added to ‘a’ Synthesis output
end process ;
24
Latches and Flip-flops
• Latches
process (EN, D) --Sensitivity list
begin
if (EN = ‘1’) then
Q <= D ;
end if;
end process;
• Flip-flops
process (clk)
begin
if (clk’event and clk= ‘1’) then
Q <= D ;
end if;
end process;
25
3-bit shift register example
Unexpected resulting structure
process (clk)
variable a,b: bit;
begin Synthesis output
if (clk’eventand clk= ‘1’) then
a := din;
b := a;
dout<= b;
end if;
end process;
--a, b changed before used so values are not stored, so they
become “wires”. (Only one flip flop from din -> dout)
26
Finite state machines (FSM)
• Model states as enumerated type
• Model “present_state” and “next_state”
signals/variables
• Construct two processes to model FSM
– One process updates state with next_state
– One process updates next_state
• Allow synthesis tool to encode states
(binary, one hot, random, gray code, etc.)
• Consider how initial state will be forced
27
State machine synthesis issues
• Two-types of FSM models
– Mealy model: outputs = f ( inputs, state)
– Moore model: outputs = f ( state )
• “case” more efficient than “if-then-elsif…” to test
present_state
(later builds a priority encoder)
• Signal assignment consideration
– Assign outputs & next_state for every case/condition.
– If an output or next_state is not assigned something under a certain
condition in the case statement, the synthesis tools will have to
preserve the value with extra latches/flip-flops.
• Left-most value of enumeration type is default simulation
starting value (use reset to initialize real circuit)
28
Design capture tools :
• One of the first tasks a designer needs to do is capture a description of the design
• As design capture can be a very time consuming and error prone process, there
are a number of tools that can help a designer improve productivity and enhance
the quality of the HDL code produced at this stage.
These tools can be classified as:
• Text editors
• Linters
• Graphical editors
• Rule-based checkers
(i) Text Editors:
• Basic text editors offer simple facilities such as the ability to copy and paste text,
search and replace text strings and format text using tabs (tabulation characters)
or spaces.
• Some of the more advanced text editors enable text to be automatically indented
and color-coded whenever certain keywords or constructs are detected.
• Although this facility is useful, because it shows the structure of the captured
code, it does not help in isolating and identifying any coding problems or
syntax errors.
• There are also programmable editors that have language specific modes of
operation that automate and enforce a standard set of coding standards.
• As the information held in the template is fixed it is not very easy for the user to
make changes to the defined coding standards.
(ii) Linters:
• The original concept of `linting'' comes from languages like C and C++ where
linters are used to check for inconsistencies within the code that had been
written.
• For example, checking that a value passed to a procedure or function was the
correct type (i.e. integer, Boolean, string, etc.) and that it matched what had
been declared in the main body of the program.
• Verilog and VHDL have a syntax very similar to C and Pascal respectively, it
was natural for linters to appear for these two HDL languages.
• VHDL is a strongly typed language and this means it is relatively easy task for a
linting tool to check for any inconsistencies in the source code.
• Verilog on the other hand is a loosely typed language that allows registers to be
overloaded or array boundaries to be exceeded.
• So here the linting tool has to detect "logical" coding errors that would otherwise
be
acceptable to Verilog.
• A simple example of this type of coding error is shown below where a vectored net
is
assigned to a signal of a differenta[0]
width.
<= #1 b[1:0]
• Depending upon the sophistication of the linting tool, other forms of checks that may
be applied include: range and index checking, checking whether a variable is used
before it has been assigned and width checking.
(iii) Graphical Editors:
• Consider the task of capturing a complex FSM (Finite State Machine) containing
a large number of states and conditional transitions.
• In this situation a designer is normally faced with the task of writing a series of
involved `case’ constructs where each `case term’ defines a state and the
conditions that must be satisfied in order to move onto the next state.
• Although a designer may be willing to undertake this task at the initial design
stage, it can get very tedious and time-consuming altering the HDL code whenever
entity NAME_OF_ENTITY is
port (signal_names: mode type;
signal_names: mode type;
:
signal_names: mode type);
end [NAME_OF_ENTITY] ;
Page 43
Operator Precedence
Page 44
Body Signal Declarations
• Similar to interface ‘port’ declaration
– must define identifier, type
– signals are internal to body; direction not needed
• Keyword is ‘signal’; declared in declarations part
of body
• Equivalent to defining intermediate circuit signals
for symbolic analysis
E.G.
Page 45
Concurrent Operation Example
entity XOR2_OP is
port
(A, B : in BIT;
Z : out BIT);
end XOR2_OP;
Page 46
Dataflow VHDL
Page 47
Bit Vectors
A and B: 7 6 5 4 3 2 1 0
Z:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Note! The first bit and last bit index numbers define
the number of bits in the vector (i.e. max - min + 1)
Page 49
Vector Literals
Page 50
Vector Logical Operations
Page 51
Vector Operations
Given:
Z <= A and B;
Is equivalent to:
for i 0 to 7
Zi A i and Bi ;
Page 52
Relational Operators
Page 54
Overview
Page 55
Schematic Vs. VHDL
Page 56
Example Schematic
A
A1 Z
B INT1
A_IN
A
A INT2
B_IN A2 Z B O1 Z Z_OUT
B
C
C_IN
A INT3
A3 Z
B
Page 57
Example Structural VHDL Interface
-- Define the Interface
entity MAJORITY is
port
(A_IN, B_IN, C_IN: in BIT;
Z_OUT : out BIT);
end MAJORITY;
Page 58
Example VHDL Body
architecture STRUCTURE of MAJORITY is
component AND2_OP
port (A, B : in BIT; Z : out BIT);
end component;
component OR3_OP
port (A, B, C : in BIT; Z : out BIT);
end component;
Page 59
Example VHDL Statement Part
begin
end STRUCTURE;
Page 60
CASE
case <expression> is
end case;
Like the select assignment, the choices may be a single value,a group (c1 | c2
| c3) or a range (c1 to c3)
Page 61
entity CASE_STATEMENT is
port (A, B, C, X : in integer range 0 to 15;
Z : out integer range 0 to 15;
end CASE_STATEMENT;
architecture EXAMPLE of CASE_STATEMENT is
begin
process (A, B, C, X)
begin
case X is
when 0 =>
Z <= A;
when 7 | 9 =>
Z <= B;
when 1 to 5 =>
Z <= C;
when others =>
Z <= 0;
end case;
end process;
end EXAMPLE
Page 62
IF THEN ELSE
If a condition is true the associate
If <condition> then
statements are executed and the rest of
the group are skipped.
seq. Statements
NOTE: the “else if” case is
elsif <condition> then
ELSIF (one word, e missing)
seq. Statements
else
seq. Statements
end if;
Page 63
entity FF is
port (D, CLK : in bit;
Q : out bit);
end FF;
begin Begin
process process
begin begin
wait on CLK; wait until CLK=`1`;
if (CLK = '1') then Q <= D;
Q <= D; end process;
end if; end BEH_2;
end process;
end BEH_1;
Page 64
Following is the VHDL code for a 4-to-1 1-bit MUX using an IF Statement.
library ieee;
use ieee.std_logic_1164.all;
entity mux is
port (a, b, c, d : in std_logic;
s : in std_logic_vector (1 downto 0);
o : out std_logic);
end mux;
architecture archi of mux is
begin
process (a, b, c, d, s)
begin
if (s = "00") then o = a;
elsif (s = "01") then o = b;
elsif (s = "10") then o = c;
else o = d;
end if;
end process;
end archi;
Page 65
Following is the VHDL code for a 4-to-1 1-bit MUX using a Case Statement.
library ieee;
use ieee.std_logic_1164.all;
entity mux is
port (a, b, c, d : in std_logic;
s : in std_logic_vector (1 downto 0);
o : out std_logic);
end mux;
library ieee;
use ieee.std_logic_1164.all;
entity dec is
port (sel: in std_logic_vector (2 downto 0);
res: out std_logic_vector (7 downto 0));
end dec;
architecture archi of dec is
begin
res = "00000001" when sel = "000" else
"00000010" when sel = "001" else
"00000100" when sel = "010" else
"00001000" when sel = "011" else
"00010000" when sel = "100" else
"00100000" when sel = "101" else
"01000000" when sel = "110" else
"10000000";
end archi;
Following is the VHDL code for a 1-of-8 decoder. ( ONE COLD)
library ieee;
use ieee.std_logic_1164.all;
entity dec is
port (sel: in std_logic_vector (2 downto 0);
res: out std_logic_vector (7 downto 0));
end dec;
architecture archi of dec is
begin
res = "11111110" when sel = "000" else
"11111101" when sel = "001" else
"11111011" when sel = "010" else
"11110111" when sel = "011" else
"11101111" when sel = "100" else
"11011111" when sel = "101" else
"10111111" when sel = "110" else
"01111111";
end archi;
Following is the equivalent VHDL code sample for the DFF with a positive-edge
clock.
library ieee;
use ieee.std_logic_1164.all;
entity flop is
port(C, D : in std_logic;
Q : out std_logic);
end flop;
library ieee;
use ieee.std_logic_1164.all;
entity flop is
port(C, D, CLR : in std_logic;
Q : out std_logic);
end flop;
library ieee;
use ieee.std_logic_1164.all;
entity flop is
port(C, D, S : in std_logic;
Q : out std_logic);
end flop;
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
entity counter is
port(C, CLR : in std_logic;
Q : out std_logic_vector(3 downto 0));
end counter;
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
entity counter is
port(C, S : in std_logic;
Q : out std_logic_vector(3 downto 0));
end counter;
architecture archi of counter is
signal tmp: std_logic_vector(3 downto 0);
begin
process (C)
begin
if (C'event and C='1') then
if (S='1') then
tmp = "1111";
else
tmp = tmp - 1;
end if;
end if;
end process;
Q = tmp;
end archi;
Following is the VHDL code for a 4-bit unsigned Up/Down counter with asynchronous
clear.
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
entity counter is
port(C, CLR, up_down : in std_logic;
Q : out std_logic_vector(3 downto 0));
end counter;
architecture archi of counter is
signal tmp: std_logic_vector(3 downto 0);
begin
process (C, CLR)
begin
if (CLR='1') then tmp = "0000";
elsif (C'event and C='1') then
if (up_down='1') then tmp = tmp + 1;
else
tmp = tmp - 1;
end if;
end if;
end process;
Q = tmp;