VHDL Programs
VHDL Programs
VHDL
library ieee;
use ieee.std_logic_1164.all;
entity priority is
port ( sel : in std_logic_vector (7 downto 0);
code :out std_logic_vector (2 downto 0));
end priority;
architecture archi of priority is
begin
code <= "000" when sel(0) = '1' else
"001" when sel(1) = '1' else
"010" when sel(2) = '1' else
"011" when sel(3) = '1' else
"100" when sel(4) = '1' else
"101" when sel(5) = '1' else
"110" when sel(6) = '1' else
"111" when sel(7) = '1' else
"---";
end archi;
VHDL (One-Hot)
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;
VHDL (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;
IO pins Description
s[2:0] Selector
res Data Output
VHDL
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
-- unused decoder output
"XXXXXXXX" 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;
IO pins Description
s[2:0] Selector
res Data Output
VHDL
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
-- 110 and 111 selector values are unused
"XXXXXXXX";
end archi;
VHDL Code
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;
4-to-1 MUX Using CASE Statement
The following table shows pin definitions for a 4-to-1 1-bit MUX using a Case statement.
IO Pins Description
a, b, c, d Data Inputs
s[1:0] MUX selector
o Data Output
VHDL Code
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;
This section shows VHDL and Verilog examples for a 4-to-1 Mux using tristate buffers
The following table shows pin definitions for a 4-to-1 1-bit MUX using tristate buffers.
IO Pins Description
a, b, c, d Data Inputs
s[3:0] MUX Selector
o Data Output
VHDL Code
Following is the VHDL code for a 4-to-1 1-bit MUX using tristate buffers.
library ieee;
use ieee.std_logic_1164.all;
entity mux is
port (a, b, c, d : in std_logic;
s : in std_logic_vector (3 downto 0);
o : out std_logic);
end mux;
end archi;
No 4-to-1 MUX
The following example does not generate a 4-to-1 1-bit MUX, but 3-to-1 MUX with 1-bit
latch. The reason is that not all selector values were described in the If statement. It is
supposed that for the s=11 case, "O" keeps its old value, and therefore a memory element
is needed.
The following table shows pin definitions for a 3-to-1 1-bit MUX with a 1-bit latch.
IO Pins Description
a, b, c, d Data Inputs
s[1:0] Selector
o Data Output
VHDL Code
Following is the VHDL code for a 3-to-1 1-bit MUX with a 1-bit latch.
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;
end if;
end process;
end archi;
Logical Shifters
Example 1
IO pins Description
VHDL
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
entity lshift is
port(DI : in unsigned(7 downto 0);
SEL : in unsigned(1 downto 0);
SO : out unsigned(7 downto 0));
end lshift;
architecture archi of lshift is
begin
with SEL select
SO <= DI when "00",
DI sll 1 when "01",
DI sll 2 when "10",
DI sll 3 when others;
end archi;
Example 2
XST will not infer a Logical Shifter for this example, as not all of the selector values are
presented.
IO pins Description
VHDL
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
entity lshift is
port(DI : in unsigned(7 downto 0);
SEL : in unsigned(1 downto 0);
SO : out unsigned(7 downto 0));
end lshift;
architecture archi of lshift is
begin
with SEL select
SO <= DI when "00",
DI sll 1 when "01",
DI sll 2 when others;
end archi;
Example 3
XST will not infer a Logical Shifter for this example, as the value is not incremented by 1
for each consequent binary value of the selector.
IO pins Description
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
entity lshift is
port(DI : in unsigned(7 downto 0);
SEL : in unsigned(1 downto 0);
SO : out unsigned(7 downto 0));
end lshift;
architecture archi of lshift is
begin
with SEL select
SO <= DI when "00",
DI sll 1 when "01",
DI sll 3 when "10",
DI sll 2 when others;
end archi;
Arithmetic Operations
• Adders with:
o Carry In
o Carry Out
o Carry In/Out
• Subtractors
• Adders/subtractors
• Comparators (=, /=,<, <=, >, >=)
• Multipliers
• Dividers
Adders, Subtractors, Comparators and Multipliers are supported for signed and unsigned
operations.
Please refer to the "Signed/Unsigned Support" section of this chapter for more
information on the signed/unsigned operations support in VHDL.
Moreover, XST performs resource sharing for adders, subtractors, adders/subtractors and
multipliers.
This subsection contains a VHDL and Verilog description of an unsigned 8-bit Adder
The following table shows pin descriptions for an unsigned 8-bit Adder.
IO pins Description
VHDL
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
entity adder is
port(A,B : in std_logic_vector(7 downto 0);
SUM : out std_logic_vector(7 downto 0));
end adder;
architecture archi of adder is
begin
SUM <= A + B;
end archi;
This section contains VHDL and Verilog descriptions of an unsigned 8-bit adder with
Carry In.
The following table shows pin descriptions for an unsigned 8-bit adder with carry.
IO pins Description
VHDL
Following is the VHDL code for an unsigned 8-bit adder with carry in.
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
entity adder is
port(A,B : in std_logic_vector(7 downto 0);
CI : in std_logic;
SUM : out std_logic_vector(7 downto 0));
end adder;
architecture archi of adder is
begin
SUM <= A + B + CI;
end archi;
The following table shows pin descriptions for an unsigned 8-bit adder with carry
IO pins Description
VHDL
Following is the VHDL code for an unsigned 8-bit adder with carry out.
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
use ieee.std_logic_unsigned.all;
entity adder is
port(A,B : in std_logic_vector(7 downto 0);
SUM : out std_logic_vector(7 downto 0);
CO : out std_logic);
end adder;
architecture archi of adder is
signal tmp: std_logic_vector(8 downto 0);
begin
tmp <= conv_std_logic_vector(
(conv_integer(A) +
conv_integer(B)),9);
SUM <= tmp(7 downto 0);
CO <= tmp(8);
end archi;
In the preceding example, two arithmetic packages are used:
This section contains VHDL and Verilog code for an unsigned 8-bit adder with Carry In
and Carry Out.
The following table shows pin descriptions for an unsigned 8-bit adder with carry.
IO pins Description
VHDL
Following is the VHDL code for an unsigned 8-bit adder with carry in and carry out.
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
use ieee.std_logic_unsigned.all;
entity adder is
port(A,B : in std_logic_vector(7 downto 0);
CI : in std_logic;
SUM : out std_logic_vector(7 downto 0);
CO : out std_logic);
end adder;
architecture archi of adder is
signal tmp: std_logic_vector(8 downto 0);
begin
tmp <= conv_std_logic_vector(
(conv_integer(A) +
conv_integer(B) +
conv_integer(CI)),9);
SUM <= tmp(7 downto 0);
CO <= tmp(8);
end archi;
Simple Signed 8-bit Adder
The following table shows pin descriptions for a simple signed 8-bit adder.
IO pins Description
VHDL
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_signed.all;
entity adder is
port(A,B : in std_logic_vector(7 downto 0);
SUM : out std_logic_vector(7 downto 0));
end adder;
architecture archi of adder is
begin
SUM <= A + B;
end archi;
The following table shows pin descriptions for an unsigned 8-bit subtractor.
IO pins Description
VHDL
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
entity subtr is
port(A,B : in std_logic_vector(7 downto 0);
RES : out std_logic_vector(7 downto 0));
end subtr;
architecture archi of subtr is
begin
RES <= A - B;
end archi;
The following table shows pin descriptions for an unsigned 8-bit adder/subtractor.
IO pins Description
VHDL
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
entity addsub is
port(A,B : in std_logic_vector(7 downto 0);
OPER: in std_logic;
RES : out std_logic_vector(7 downto 0));
end addsub;
architecture archi of addsub is
begin
RES <= A + B when OPER='0'
else A - B;
end archi;
endmodule
This section contains a VHDL and Verilog description for an unsigned 8-bit greater or
equal comparator.
IO pins Description
VHDL
Following is the VHDL code for an unsigned 8-bit greater or equal comparator.
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
entity compar is
port(A,B : in std_logic_vector(7 downto 0);
CMP : out std_logic);
end compar;
architecture archi of compar is
begin
CMP <= '1' when A >= B
else '0';
end archi;
Multipliers
When implementing a multiplier, the size of the resulting signal is equal to the sum of 2
operand lengths. If you multiply A (8-bit signal) by B (4-bit signal), then the size of the
result must be declared as a 12-bit signal.
This section contains VHDL and Verilog descriptions of an unsigned 8x4-bit multiplier.
The following table shows pin descriptions for an unsigned 8x4-bit multiplier.
IO pins Description
VHDL
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
entity mult is
port(A : in std_logic_vector(7 downto 0);
B : in std_logic_vector(3 downto 0);
RES : out std_logic_vector(11 downto 0));
end mult;
architecture archi of mult is
begin
RES <= A * B;
end archi;
Dividers
Divisions are only supported, when the divisor is a constant and is a power of 2. In that
case, the operator is implemented as a shifter; otherwise, an error message will be issued
by XST.
Division By Constant 2
The following table shows pin descriptions for a Division By Constant 2 divider.
IO pins Description
VHDL
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
entity divider is
port(DI : in unsigned(7 downto 0);
DO : out unsigned(7 downto 0));
end divider;
architecture archi of divider is
begin
DO <= DI / 2;
end archi;
Resource Sharing
The goal of resource sharing (also known as folding) is to minimize the number of
operators and the subsequent logic in the synthesized design. This optimization is based
on the principle that two similar arithmetic resources may be implemented as one single
arithmetic operator if they are never used at the same time. XST performs both resource
sharing and, if required, reduces of the number of multiplexers that are created in the
process.
XST supports resource sharing for adders, subtractors, adders/subtractors and multipliers.
Related Constraint
Example
For the following VHDL/Verilog example, XST will give the following solution:
IO pins Description
VHDL
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
entity addsub is
port(A,B,C : in std_logic_vector(7 downto 0);
OPER : in std_logic;
RES : out std_logic_vector(7 downto 0));
end addsub;
architecture archi of addsub is
begin
RES <= A + B when OPER='0'
else A - C;
end archi;
adder
LIBRARY ieee ;
USE ieee.std_logic_1164.all ;
USE ieee.std_logic_arith.all ;
ENTITY adder IS
PORT(in1: IN std_logic_vector(15 DOWNTO 0) ;
in2: IN std_logic_vector(15 DOWNTO 0) ;
c_out: OUT std_logic ;
sum: OUT std_logic_vector(15 DOWNTO 0)
) ;
END adder ;
ENTITY counter IS
PORT(clk: IN std_logic ;
input: IN std_logic_vector(11 DOWNTO 0) ;
output: OUT std_logic_vector(11 DOWNTO 0) ;
ld: IN std_logic ;
inc: IN std_logic ;
clr: IN std_logic
) ;
END counter ;
--
-- Design a 2-bit count-down counter
--
LIBRARY ieee ;
USE ieee.std_logic_1164.all ;
USE ieee.std_logic_arith.all ;
USE ieee.std_logic_signed.all ;
USE ieee.std_logic_unsigned.all ;
ENTITY down_counter IS
PORT(SIGNAL x: IN std_logic ;
SIGNAL count : OUT std_logic_vector(1 DOWNTO 0) ;
SIGNAL reset: IN std_logic ;
SIGNAL clk: IN std_logic
) ;
END down_counter ;
--
-- 8 to 3 priority encoder
--
LIBRARY ieee ;
USE ieee.std_logic_1164.all ;
ENTITY enc8to3 IS
PORT(SIGNAL input: IN std_logic_vector(7 DOWNTO 0) ;
SIGNAL output: OUT std_logic_vector(2 DOWNTO 0)
) ;
END enc8to3 ;
--
-- Here is a case where we really need the WHEN - ELSE
-- I don't think the WITH select will work because
-- we want a priority encoder
--
ARCHITECTURE arch1 OF enc8to3 IS
BEGIN
output <= "111" WHEN (input(7) = '1') ELSE
"110" WHEN (input(6) = '1') ELSE
"101" WHEN (input(5) = '1') ELSE
"100" WHEN (input(4) = '1') ELSE
"011" WHEN (input(3) = '1') ELSE
"010" WHEN (input(2) = '1') ELSE
"001" WHEN (input(1) = '1') ELSE
"000" ;
END arch1 ;
-- fa.vhd
--
-- A 1-bit full-adder
--
-- George L. Engel, SIUE
--
LIBRARY ieee ;
USE ieee.std_logic_1164.all ;
ENTITY fa IS
PORT( a, b : in std_logic ;
cin : in std_logic ;
cout : out std_logic ;
sum : out std_logic
) ;
END fa ;
ARCHITECTURE arch1 OF fa IS
BEGIN
sum <= (a XOR b) XOR cin ;
cout <= (a AND b) OR ((a OR b) AND cin) ;
END arch1 ;
--REGISTER
LIBRARY ieee ;
USE ieee.std_logic_1164.all ;
ENTITY reg IS
PORT(clk: IN std_logic ;
input: IN std_logic_vector(15 DOWNTO 0) ;
output: OUT std_logic_vector(15 DOWNTO 0) ;
ld: IN std_logic
) ;
END reg ;
---------------------------------------------
-- D Flip-Flop (ESD book Chapter 2.3.1)
-- by Weijun Zhang, 04/2001
--
-- Flip-flop is the basic component in
-- sequential logic design
-- we assign input signal to the output
-- at the clock rising edge
---------------------------------------------
library ieee ;
use ieee.std_logic_1164.all;
use work.all;
---------------------------------------------
entity dff is
port( data_in: in std_logic;
clock: in std_logic;
data_out: out std_logic
);
end dff;
----------------------------------------------
process(data_in, clock)
begin
end process;
end behv;
----------------------------------------------
----------------------------------------------
-- JK Flip-Flop with reset
-- (ESD book Chapter 2.3.1)
-- by Weijun Zhang, 04/2001
--
-- the description of JK Flip-Flop is based
-- on functional truth table
-- concurrent statement and signal assignment
-- are using in this example
----------------------------------------------
library ieee;
use ieee.std_logic_1164.all;
----------------------------------------------
entity JK_FF is
port ( clock: in std_logic;
J, K: in std_logic;
reset: in std_logic;
Q, Qbar: out std_logic
);
end JK_FF;
-----------------------------------------------
p: process(clock, reset) is
begin
if (reset='1') then
state <= '0';
elsif (rising_edge(clock)) then
end process;
-- concurrent statements
Q <= state;
Qbar <= not state;
end behv;
-------------------------------------------------
---------------------------------------------------
-- n-bit Register (ESD book figure 2.6)
-- by Weijun Zhang, 04/2001
--
-- KEY WORD: concurrent, generic and range
---------------------------------------------------
library ieee ;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
---------------------------------------------------
entity reg is
----------------------------------------------------
begin
end process;
-- concurrent statement
Q <= Q_tmp;
end behv;
---------------------------------------------------
---------------------------------------------------
-- 3-bit Shift-Register/Shifter
-- (ESD book figure 2.6)
-- by Weijun Zhang, 04/2001
--
-- reset is ignored according to the figure
---------------------------------------------------
library ieee ;
use ieee.std_logic_1164.all;
---------------------------------------------------
entity shift_reg is
port( I: in std_logic;
clock: in std_logic;
shift: in std_logic;
Q: out std_logic
);
end shift_reg;
---------------------------------------------------
architecture behv of shift_reg is
begin
end process;
-- concurrent assignment
Q <= S(0);
end behv;
----------------------------------------------------
----------------------------------------------------
-- VHDL code for n-bit counter (ESD figure 2.6)
-- by Weijun Zhang, 04/2001
--
-- this is the behavior description of n-bit counter
-- another way can be used is FSM model.
----------------------------------------------------
library ieee ;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
----------------------------------------------------
entity counter is
----------------------------------------------------
end behv;
-----------------------------------------------------
VHDL Code for Shift registers
8-bit Shift-Left Register with Positive-Edge Clock, Serial In, and Serial
Out
The following table shows pin definitions for an 8-bit shift-left register with a positive-
edge clock, serial in, and serial out.
IO Pins Description
C Positive-Edge Clock
SI Serial In
SO Serial Output
VHDL Code
Following is the VHDL code for an 8-bit shift-left register with a positive-edge clock,
serial in, and serial out.
library ieee;
use ieee.std_logic_1164.all;
entity shift is
port(C, SI : in std_logic;
SO : out std_logic);
end shift;
architecture archi of shift is
signal tmp: std_logic_vector(7 downto 0);
begin
process (C)
begin
if (C'event and C='1') then
for i in 0 to 6 loop
tmp(i+1) <= tmp(i);
end loop;
tmp(0) <= SI;
end if;
end process;
SO <= tmp(7);
end archi;
8-bit Shift-Left Register with Negative-Edge Clock, Clock Enable, Serial
In, and Serial Out
The following table shows pin definitions for an 8-bit shift-left register with a negative-
edge clock, clock enable, serial in, and serial out.
IO Pins Description
C Negative-Edge Clock
SI Serial In
CE Clock Enable (active High)
SO Serial Output
VHDL Code
Following is the VHDL code for an 8-bit shift-left register with a negative-edge clock,
clock enable, serial in, and serial out.
library ieee;
use ieee.std_logic_1164.all;
entity shift is
port(C, SI, CE : in std_logic;
SO : out std_logic);
end shift;
architecture archi of shift is
signal tmp: std_logic_vector(7 downto 0);
begin
process (C)
begin
if (C'event and C='0') then
if (CE='1') then
for i in 0 to 6 loop
tmp(i+1) <= tmp(i);
end loop;
tmp(0) <= SI;
end if;
end if;
end process;
SO <= tmp(7);
end archi;
8-bit Shift-Left Register with Positive-Edge Clock, Asynchronous Clear,
Serial In, and Serial Out
Note Because this example includes an asynchronous clear, XST will not infer SRL16.
The following table shows pin definitions for an 8-bit shift-left register with a positive-
edge clock, asynchronous clear, serial in, and serial out.
IO Pins Description
C Positive-Edge Clock
SI Serial In
CLR Asynchronous Clear (active High)
SO Serial Output
VHDL Code
Following is the VHDL code for an 8-bit shift-left register with a positive-edge clock,
asynchronous clear, serial in, and serial out.
library ieee;
use ieee.std_logic_1164.all;
entity shift is
port(C, SI, CLR : in std_logic;
SO : out std_logic);
end shift;
architecture archi of shift is
signal tmp: std_logic_vector(7 downto 0);
begin
process (C, CLR)
begin
if (CLR='1') then
tmp <= (others => '0');
elsif (C'event and C='1') then
tmp <= tmp(6 downto 0) & SI;
end if;
end process;
SO <= tmp(7);
end archi;
The following table shows pin definitions for an 8-bit shift-left register with a positive-
edge clock, synchronous set, serial in, and serial out.
IO Pins Description
C Positive-Edge Clock
SI Serial In
S synchronous Set (active High)
SO Serial Output
VHDL Code
Following is the VHDL code for an 8-bit shift-left register with a positive-edge clock,
synchronous set, serial in, and serial out.
library ieee;
use ieee.std_logic_1164.all;
entity shift is
port(C, SI, S : in std_logic;
SO : out std_logic);
end shift;
architecture archi of shift is
signal tmp: std_logic_vector(7 downto 0);
begin
process (C, S)
begin
if (C'event and C='1') then
if (S='1') then
tmp <= (others => '1');
else
tmp <= tmp(6 downto 0) & SI;
end if;
end if;
end process;
SO <= tmp(7);
end archi;
8-bit Shift-Left Register with Positive-Edge Clock, Serial In, and Parallel
Out
Note For this example XST will infer SRL16.
The following table shows pin definitions for an 8-bit shift-left register with a positive-
edge clock, serial in, and serial out.
IO Pins Description
C Positive-Edge Clock
SI Serial In
PO[7:0] Parallel Output
VHDL Code
Following is the VHDL code for an 8-bit shift-left register with a positive-edge clock,
serial in, and serial out.
library ieee;
use ieee.std_logic_1164.all;
entity shift is
port(C, SI : in std_logic;
PO : out std_logic_vector(7 downto 0));
end shift;
architecture archi of shift is
signal tmp: std_logic_vector(7 downto 0);
begin
process (C)
begin
if (C'event and C='1') then
tmp <= tmp(6 downto 0)& SI;
end if;
end process;
PO <= tmp;
end archi;
The following table shows pin definitions for an 8-bit shift-left register with a positive-
edge clock, asynchronous parallel load, serial in, and serial out.
IO Pins Description
C Positive-Edge Clock
SI Serial In
ALOAD Asynchronous Parallel Load (active High)
D[7:0] Data Input
SO Serial Output
VHDL Code
Following is VHDL code for an 8-bit shift-left register with a positive-edge clock,
asynchronous parallel load, serial in, and serial out.
library ieee;
use ieee.std_logic_1164.all;
entity shift is
port(C, SI, ALOAD : in std_logic;
D : in std_logic_vector(7 downto 0);
SO : out std_logic);
end shift;
architecture archi of shift is
signal tmp: std_logic_vector(7 downto 0);
begin
process (C, ALOAD, D)
begin
if (ALOAD='1') then
tmp <= D;
elsif (C'event and C='1') then
tmp <= tmp(6 downto 0) & SI;
end if;
end process;
SO <= tmp(7);
end archi;
The following table shows pin definitions for an 8-bit shift-left register with a positive-
edge clock, synchronous parallel load, serial in, and serial out.
IO Pins Description
C Positive-Edge Clock
SI Serial In
SLOAD Synchronous Parallel Load (active High)
D[7:0] Data Input
SO Serial Output
VHDL Code
Following is the VHDL code for an 8-bit shift-left register with a positive-edge clock,
synchronous parallel load, serial in, and serial out.
library ieee;
use ieee.std_logic_1164.all;
entity shift is
port(C, SI, SLOAD : in std_logic;
D : in std_logic_vector(7 downto 0);
SO : out std_logic);
end shift;
architecture archi of shift is
signal tmp: std_logic_vector(7 downto 0);
begin
process (C)
begin
if (C'event and C='1') then
if (SLOAD='1') then
tmp <= D;
else
tmp <= tmp(6 downto 0) & SI;
end if;
end if;
end process;
SO <= tmp(7);
end archi;
The following table shows pin definitions for an 8-bit shift-left/shift-right register with a
positive-edge clock, serial in, and serial out.
IO Pins Description
C Positive-Edge Clock
SI Serial In
LEFT_RIGHT Left/right shift mode selector
PO[7:0] Parallel Output
VHDL Code
Following is the VHDL code for an 8-bit shift-left/shift-right register with a positive-edge
clock, serial in, and serial out.
library ieee;
use ieee.std_logic_1164.all;
entity shift is
port(C, SI, LEFT_RIGHT : in std_logic;
PO : out std_logic_vector(7 downto 0));
end shift;
architecture archi of shift is
signal tmp: std_logic_vector(7 downto 0);
begin
process (C)
begin
if (C'event and C='1') then
if (LEFT_RIGHT='0') then
tmp <= tmp(6 downto 0) & SI;
else
tmp <= SI & tmp(7 downto 1);
end if;
end if;
end process;
PO <= tmp;
end archi;
Counters
The following table shows pin definitions for a 4-bit unsigned up counter with
asynchronous clear.
IO Pins Description
C Positive-Edge Clock
CLR Asynchronous Clear (active High)
Q[3:0] Data Output
VHDL Code
Following is VHDL code for a 4-bit unsigned up counter with asynchronous clear.
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;
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
tmp <= tmp + 1;
end if;
end process;
Q <= tmp;
end archi;
IO Pins Description
C Positive-Edge Clock
S Synchronous Set (active High)
Q[3:0] Data Output
VHDL Code
Following is the VHDL code for a 4-bit unsigned down counter with synchronous set.
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;
The following table shows pin definitions for a 4-bit unsigned up counter with
asynchronous load from primary input.
IO Pins Description
C Positive-Edge Clock
ALOAD Asynchronous Load (active High)
D[3:0] Data Input
Q[3:0] Data Output
VHDL Code
Following is the VHDL code for a 4-bit unsigned up counter with asynchronous load
from primary input.
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
entity counter is
port(C, ALOAD : in std_logic;
D : in std_logic_vector(3 downto 0);
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, ALOAD, D)
begin
if (ALOAD='1') then
tmp <= D;
elsif (C'event and C='1') then
tmp <= tmp + 1;
end if;
end process;
Q <= tmp;
end archi;
The following table shows pin definitions for a 4-bit unsigned up counter with
synchronous load with a constant.
IO Pins Description
C Positive-Edge Clock
SLOAD Synchronous Load (active High)
Q[3:0] Data Output
VHDL Code
Following is the VHDL code for a 4-bit unsigned up counter with synchronous load with
a constant.
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
entity counter is
port(C, SLOAD : 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 (SLOAD='1') then
tmp <= "1010";
else
tmp <= tmp + 1;
end if;
end if;
end process;
Q <= tmp;
end archi;
The following table shows pin definitions for a 4-bit unsigned up counter with
asynchronous clear and clock enable.
IO Pins Description
C Positive-Edge Clock
CLR Asynchronous Clear (active High)
CE Clock Enable
Q[3:0] Data Output
VHDL Code
Following is the VHDL code for a 4-bit unsigned up counter with asynchronous clear and
clock enable.
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
entity counter is
port(C, CLR, CE : 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 (CE='1') then
tmp <= tmp + 1;
end if;
end if;
end process;
Q <= tmp;
end archi;
The following table shows pin definitions for a 4-bit unsigned up/down counter with
asynchronous clear.
IO Pins Description
C Positive-Edge Clock
CLR Asynchronous Clear (active High)
UP_DOWN up/down count mode selector
Q[3:0] Data Output
VHDL Code
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;
end archi;
The following table shows pin definitions for a 4-bit signed up counter with
asynchronous reset.
IO Pins Description
C Positive-Edge Clock
CLR Asynchronous Clear (active High)
Q[3:0] Data Output
VHDL Code
Following is the VHDL code for a 4-bit signed up counter with asynchronous reset.
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_signed.all;
entity counter is
port(C, CLR : 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
tmp <= tmp + 1;
end if;
end process;
Q <= tmp;
end archi;
Registers
The following table shows pin definitions for a flip-flop with positive edge clock.
IO Pins Description
D Data Input
C Positive Edge Clock
Q Data Output
VHDL Code
Following is the equivalent VHDL code sample for the flip-flop 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;
architecture archi of flop is
begin
process (C)
begin
if (C'event and C='1') then
Q <= D;
end if;
end process;
end archi;
Note When using VHDL, for a positive-edge clock instead of using
if (rising_edge(C)) then
if (falling_edge(C)) then
The following figure shows a flip-flop with negative-edge clock and asynchronous clear.
The following table shows pin definitions for a flip-flop with negative edge clock and
asynchronous clear.
IO Pins Description
D Data Input
C Negative-Edge Clock
CLR Asynchronous Clear (active High)
Q Data Output
VHDL Code
Following is the equivalent VHDL code for a flip-flop with a negative-edge clock and
asynchronous clear.
library ieee;
use ieee.std_logic_1164.all;
entity flop is
port(C, D, CLR : in std_logic;
Q : out std_logic);
end flop;
architecture archi of flop is
begin
process (C, CLR)
begin
if (CLR = '1')then
Q <= '0';
elsif (C'event and C='0')then
Q <= D;
end if;
end process;
end archi;
The following figure shows a flip-flop with positive-edge clock and synchronous set.
The following table shows pin definitions for a flip-flop with positive edge clock and
synchronous set.
IO Pins Description
D Data Input
C Positive-Edge Clock
S Synchronous Set (active High)
Q Data Output
VHDL Code
Following is the equivalent VHDL code for the flip-flop with a positive-edge clock and
synchronous set.
library ieee;
use ieee.std_logic_1164.all;
entity flop is
port(C, D, S : in std_logic;
Q : out std_logic);
end flop;
architecture archi of flop is
begin
process (C)
begin
if (C'event and C='1') then
if (S='1') then
Q <= '1';
else
Q <= D;
end if;
end if;
end process;
end archi;
The following figure shows a flip-flop with positive-edge clock and clock enable.
The following table shows pin definitions for a flip-flop with positive edge clock and
clock enable.
IO Pins Description
D Data Input
C Positive-Edge Clock
CE Clock Enable (active High)
Q Data Output
VHDL Code
Following is the equivalent VHDL code for the flip-flop with a positive-edge clock and
clock Enable.
library ieee;
use ieee.std_logic_1164.all;
entity flop is
port(C, D, CE : in std_logic;
Q : out std_logic);
end flop;
architecture archi of flop is
begin
process (C)
begin
if (C'event and C='1') then
if (CE='1') then
Q <= D;
end if;
end if;
end process;
end archi;
The following figure shows a 4-bit register with positive-edge clock, asynchronous set
and clock enable.
The following table shows pin definitions for a 4-bit register with positive-edge clock,
asynchronous set and clock enable.
IO Pins Description
VHDL Code
Following is the equivalent VHDL code for a 4-bit register with a positive-edge clock,
asynchronous set and clock enable.
library ieee;
use ieee.std_logic_1164.all;
entity flop is
port(C, CE, PRE : in std_logic;
D : in std_logic_vector (3 downto 0);
Q : out std_logic_vector (3 downto 0));
end flop;
architecture archi of flop is
begin
process (C, PRE)
begin
if (PRE='1') then
Q <= "1111";
elsif (C'event and C='1')then
if (CE='1') then
Q <= D;
end if;
end if;
end process;
end archi;
IO Pins Description
D Data Input
G Positive Gate
Q Data Output
VHDL Code
Following is the equivalent VHDL code for a latch with a positive gate.
library ieee;
use ieee.std_logic_1164.all;
entity latch is
port(G, D : in std_logic;
Q : out std_logic);
end latch;
architecture archi of latch is
begin
process (G, D)
begin
if (G='1') then
Q <= D;
end if;
end process;
end archi;
3-Bit 1-of-9 Priority Encoder
Note For this example XST may infer a priority encoder. You must use the
priority_extract constraint with a value force to force its inference.
Related Constraint
VHDL
library ieee;
use ieee.std_logic_1164.all;
entity priority is
port ( sel : in std_logic_vector (7 downto 0);
code :out std_logic_vector (2 downto 0));
end priority;
architecture archi of priority is
begin
code <= "000" when sel(0) = '1' else
"001" when sel(1) = '1' else
"010" when sel(2) = '1' else
"011" when sel(3) = '1' else
"100" when sel(4) = '1' else
"101" when sel(5) = '1' else
"110" when sel(6) = '1' else
"111" when sel(7) = '1' else
"---";
end archi;
IO pins Description
VHDL
Following is the VHDL code for an unsigned 8-bit greater or equal comparator.
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
entity compar is
port(A,B : in std_logic_vector(7 downto 0);
CMP : out std_logic);
end compar;
architecture archi of compar is
begin
CMP <= '1' when A >= B
else '0';
end archi;