VHDL Programming
VHDL Programming
Introduction
Golden Rules of VHDL
VHDL Invariants
Case Sensitivity
White Space
Parentheses
Comments
Identifiers
Reserved Words
VHDL Design Units
VHDL Standard Libraries
Entity
Architecture
VHDL Programming Paradigm
Kaustubh V Sakhare
Introduction
VHDL (Very High Speed IC Hardware description Language) is one of
the standard hardware description language used to design digital
systems.
VHDL can be used to design the lowest level (gate level) of a digital
system to the highest level (VLSI module).
Other than VHDL there are many hardware description languages
available in the market for the digital designers such as Verilog, ABEL,
PALASM, CUPL, and etc
VHDL and Verilog are the most widely used HDLs. The major difference
between hardware description programming languages and others is the
integration of time.
Timing specifications are used to incorporate propagation delays present
in the system.
Kaustubh V Sakhare
Golden Rules of VHDL
Kaustubh V Sakhare
VHDL Invariants
Case Sensitivity
White Space
Kaustubh V Sakhare
VHDL Invariants
Parentheses
VHDL is relatively lax on its requirement for using parentheses.
Like other computer languages, there are a few precedence rules
associated with the various operators in the VHDL language.
But a better idea is to practice liberal use of parentheses to ensure
the human reader of your source code understands the purpose of
the code.
Example of parentheses that can improve clarity.
VHDL Statements
Kaustubh V Sakhare
VHDL Invariants
if, case and loop Statements
Identifiers
Kaustubh V Sakhare
VHDL Invariants
Identifiers
Kaustubh V Sakhare
VHDL Invariants
Valid identifiers Invalid identifiers
Kaustubh V Sakhare
VHDL Design Units
The “black-box" approach to any type of design implies a
hierarchical structure in which varying amounts of detail are
available at each of the different levels of the hierarchy.
In the black-box approach, units of action which share a similar
purpose are grouped together and abstracted to a higher level.
Once this is done, the module is referred to by its inherently more
simple black-box representation rather than by the details of the
circuitry that actually performs that functionality.
This approach has two main advantages.
1.It simplifies the design from a systems standpoint.
2.The black-box approach allows for the reuse of previously written
code.
Kaustubh V Sakhare
VHDL Standard Libraries
-- library declaration
library IEEE;
use IEEE.std_logic_1164.all; -- basic IEEE library
use IEEE.numeric_std.all;
std_logic_signed
std_logic_unsigned
std_logic_arith
Kaustubh V Sakhare
Entity
The VHDL entity construct provides a method to abstract the
functionality of a circuit description to a higher level. It
provides a simple wrapper for the lower-level circuitry. This
wrapper effectively describes how the black box interfaces
with the outside world.
entity my_entity is
port(
port_name_1 : in std_logic ;
port_name_2 : out std_logic;
port_name_3 : inout std_logic ); -- do not forget the
semicolon
end my_entity; -- do not forget this semicolon either
Kaustubh V Sakhare
Bundles are easily described in the VHDL entity. All that is
needed is a new data type and a special notation to indicate when a
signal is a bundle or not.
The std logic data type has now been replaced bythe word
std logic vector
to indicate that each signal name contains more than one signal.
The signals in the bus can be listed in one of two orders
which are specified by the to and downto keywords. If you want
the most significant bit of your bundle to be on the the first bit
on the left you use downto keyword.
Kaustubh V Sakhare
entity mux4 is
port ( a_data : in std_logic_vector(0 to 7);
b_data : in std_logic_vector(0 to 7);
c_data : in std_logic_vector(0 to 7);
d_data : in std_logic_vector(0 to 7);
sel1,sel0 : in std_logic;
data_out : out std_logic_vector(0 to 7));
end mux4;
Kaustubh V Sakhare
Architecture
An architecture can be written by means of three modeling
techniques plus any combination of these three. There is the
dataflow model, the behavioral model, the structural model and
the hybrid models.
entity ckt_c is
port (
bun_a,bun_b_bun_c : in std_logic_vector(7 downto 0);
lda,ldb,ldc : in std_logic;
reg_a, reg_b, reg_c : out std_logic_vector(7 downto 0);
end ckt_c;
Kaustubh V Sakhare
Assignment
2. The following two entity declarations contain two of the
most common syntax errors made in VHDL. What are they?
a) b)
entity ckt_a is entity ckt_b is
port ( port (
J,K : in std_logic; mr_fluffy : in std_logic_vector(15
CLK : in std_logic downto 0);
Q : out std_logic;) mux_ctrl : in std_logic_vector(3
end ckt_a; downto 0);
byte_out : out std_logic_vector(3
downto 0);
end ckt_b;
Kaustubh V Sakhare
VHDL Programming Paradigm
The VHDL programming paradigm is built around the
concept of expression parallelism and concurrency with
textual descriptions of circuits. The heart of VHDL
programming is the concurrent statement.
Kaustubh V Sakhare
VHDL Programming Paradigm
VHDL code for the circuit of Figure
-- entity
entity my_circuit is
port (
A_1,A_2,B_1,B_2,D_1 : in std_logic;
E_out : out std_logic;
end my_circuit;
-- architecture
architecture my_circuit_arc of my_circuit is
signal A_out, B_out, C_out : std_logic;
begin
A_out <= A_1 and A_2;
B_out <= B_1 or B_2;
C_out <= (not D_1) and B_2;
E_out <= A_out or B_out or C_out;
Kaustubh V Sakhare
end my_circuit_arc;
VHDL Programming Paradigm
Concurrent Signal Assignment Statements
Kaustubh V Sakhare
VHDL Programming Paradigm
Concurrent Signal Assignment Statements
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
-- entity
entity my_nand3 is
port ( A,B,C : in std_logic;
F : out std_logic);
end my_nand3;
-- architecture
architecture exa_nand3 of my_nand3 is
begin
F <= NOT (A AND B AND C);
end exa_nand3;
-- another architecture
architecture exb_nand3 of my_nand3 is
begin
F <= A NAND B NAND C; Kaustubh V Sakhare
end exb_nand3;
VHDL Programming Paradigm
Concurrent Signal Assignment Statements
Write the VHDL code to implement the function expressed by
the following logic equation: F3 = LMN + LM
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
-- entity
entity my_ckt_f3 is
port ( L,M,N : in std_logic;
F3 : out std_logic);
end my_ckt_f3;
-- architecture
architecture f3_2 of my_ckt_f3 is
begin
F3 <= ((NOT L)AND(NOT M)AND N)OR(L AND M);
end f3_2; Kaustubh V Sakhare
VHDL Programming Paradigm
Concurrent Signal Assignment Statements
The intermediate signals must be declared within the body of the architecture
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
-- entity
entity my_ckt_f3 is
port ( L,M,N : in std_logic;
F3 : out std_logic);
end my_ckt_f3;
-- architecture
architecture f3_1 of my_ckt_f3 is
signal A1, A2 : std_logic; -- intermediate signals
begin
A1 <= ((NOT L)AND(NOT M)AND N)
A2 <= L AND M;
F3 <= A1 OR A2 Kaustubh V Sakhare
end f3_1;
VHDL Programming Paradigm
Conditional Signal Assignment
The syntax for the conditional signal assignment statement.
<target> <= <expression> when <condition> else
<expression> when <condition> else
<expression>; library IEEE;
Write the VHDL code to implement the function F3 = LMN +LM
-- entity
entity my_ckt_f3 is
port ( L,M,N : in std_logic;
F3 : out std_logic);
end my_ckt_f3;
-- architecture
architecture f3_3 of my_ckt_f3 is
begin
F3 <= ’1’ when (L= ’0’ AND M = ’0’ AND N = ’1’) else
’1’ when (L= ’1’ AND M = ’1’) else ’0’;
end f3_3;
Kaustubh V Sakhare
VHDL Programming Paradigm
Conditional Signal Assignment
It is not much of an improvement over the VHDL code written
using concurrent signal assignment.
In fact, it looks a bit less efficient in terms of the number of
instructions.
The associated expressions are the single digits surrounded by
single quotes; the associated conditions follow the when keyword.
The last expression in the signal assignment statement is the
catch-all condition.
If none of the conditions listed above the signal expression
evaluate as true, the last expression is assigned to the target.
The solution uses relational operators. There are actually six
different relational operators available in VHDL.
Two of the more common relational operators are the \=" and
\/=" relational operators which are the \is equal to" and the \is
not equal to" operators, respectively.
Kaustubh V Sakhare
VHDL Programming Paradigm
Write the VHDL code that implements a 4:1 MUX using a
single conditional signal assignment statement. The inputs to
the MUX are data inputs D3, D2, D1, D0 and a two-input
control bus SEL. The single output is MX OUT.
Kaustubh V Sakhare
VHDL Programming Paradigm
library IEEE;
-- entity
entity my_4t1_mux is
Port (D3,D2,D1,D0 : in std_logic;
SEL : in std_logic_vector(1 downto 0);
MX_OUT : out std_logic);
end my_4t1_mux;
-- architecture
architecture mux4t1 of my_4t1_mux
begin
MX_OUT <= D3 when (SEL = "11")
D2 when (SEL = "10")
D1 when (SEL = "01")
D0 when (SEL = "00")
’0’; end mux4t1;
Kaustubh V Sakhare
VHDL Programming Paradigm
Alternative solution to Example accessing individual signals.
-- entity and architecture of 4:1 Multiplexor implemented using
-- conditional signal assignment. The conditions access the
-- individual signals of the SEL bundle in this model.
entity my_4t1_mux is
port (D3,D2,D1,D0 : in std_logic;
SEL : in std_logic_vector(1 downto 0);
MX_OUT : out std_logic);
end my_4t1_mux;
-- architecture
architecture mux4t1 of my_4t1_mux is
begin
MX_OUT <= D3 when (SEL(1) = ’1’ and SEL(0) =’1’) else
D2 when (SEL(1) = ’1’ and SEL(0) =’0’) else
D1 when (SEL(1) = ’0’ and SEL(0) =’1’) else
D0 when (SEL(1) = ’0’ and SEL(1) =’0’) else
’0’;
end mux4t1; Kaustubh V Sakhare
VHDL Programming Paradigm
For the following function descriptions, write VHDL
models that implement these functions using concurrent
signal assignment.
a) F(A;B) = AB + A + AB
b) F(A;B;C;D) = ACD + BC + BCD
c) F(A;B;C;D) = (A + B) (B + C + D) (A + D)
Kaustubh V Sakhare
VHDL Programming Paradigm
Selected Signal Assignment
Kaustubh V Sakhare
VHDL Programming Paradigm
Selected Signal Assignment
Write VHDL code to implement the function expressed by
the
following logic equation: F3 = LMN + LM. Use only selected
signal assignment
statements in your VHDL code.
--Solution of Example
-- yet another solution to the Example
architecture f3_4 of my_ckt_f3 is
begin
with ((L =’0’ and M =’0’and N =’1’)or(L=’1’ and M=’1’))
select
F3 <= ’1’ when ’1’,
’0’ when ’0’,
’0’ when others; Kaustubh V Sakhare
end f3_4;
VHDL Programming Paradigm
Selected Signal Assignment
Write the VHDL code that implements a 4:1 MUX using a single
selected signal assignment statement. The inputs to the MUX are data
inputs D3, D2, D1, D0 and a two-input control bus SEL. The single
output is MX OUT.
library IEEE;
-- entity
entity my_4t1_mux is
port (D3,D2,D1,D0 : in std_logic;
SEL : in std_logic_vector(1 downto 0);
MX_OUT : out std_logic);
end my_4t1_mux;
-- architecture
architecture mux4t1_2 of my_4t1_mux is
begin
with SEL select
MX_OUT <= D3 when "11",
D2 when "10", Kaustubh V Sakhare
D1 when "01“,D0 when "00“,’0’ when others; end mux4t1_2;
VHDL Programming Paradigm
Selected Signal Assignment
Write the VHDL code that implements the following circuit. The
circuit contains an input bundle of four signals and an output bundle of
three signals. The input bundle, D IN, represents a 4-bit binary number.
The output bus, SZ OUT, is used to indicate the magnitude of the 4-bit
binary input number.
Kaustubh V Sakhare
VHDL Programming Paradigm
------------------------------------------------------------
-- A decoder-type circuit using selected signal assignment
------------------------------------------------------------
entity my_sz_ckt is
port ( D_IN : in std_logic_vector(3 downto 0);
SX_OUT : out std_logic_vector(2 downto 0));
end my_sz_ckt;
-- architecture
architecture spec_dec of my_sz_ckt is
begin
with D_IN select
SX_OUT<="100"when "0000"|"0001"|"0010"|"0011",
"010"when "0100"|"0101"|"0110"|"0111"|"1000"|"1001",
"001"when "1010"|"1011"|"1100"|"1101"|"1110"|"1111",
"000"when others;
end spec_dec;
Kaustubh V Sakhare
VHDL Programming Paradigm
Write the VHDL code that implements a 4:1 MUX using a single
selected signal assignment statement. The inputs to the MUX are data
inputs D3,
D2, D1, D0 and a two-input control bus SEL. The single output is MX
OUT.
Kaustubh V Sakhare
VHDL Programming Paradigm
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
-- entity
entity my_4t1_mux is
port (D3,D2,D1,D0 : in std_logic;
SEL : in std_logic_vector(1 downto 0);
MX_OUT : out std_logic);
end my_4t1_mux;
-- architecture
architecture mux4t1_2 of my_4t1_mux is
begin
with SEL select
MX_OUT <= D3 when "11",
D2 when "10",
D1 when "01",
D0 when "00",
’0’ when others;
end mux4t1_2; Kaustubh V Sakhare
VHDL Programming Paradigm
Provide a VHDL model of an 8-input AND gate using
concurrent, conditional and selected signal assignment
Provide a VHDL model of an 8-input OR gate using
concurrent, conditional and selected signal assignment
Provide a VHDL model of an 8:1 MUX using conditional
signal assignment selected signal assignment
Provide a VHDL model of a 3:8 decoder using conditional
signal assignment, selected signal assignment
consider the decoder's outputs to be active-high.
Kaustubh V Sakhare
VHDL Programming Paradigm
Dataflow Style Architecture
•A dataflow style architecture specifies a circuit as a
concurrent representation of the flow of data through the
circuit.
•In the dataflow approach, circuits are described by
showing the input and output relationships between the
various built-in components of the VHDL language.
•The built-in components of VHDL include operators such
as AND, OR, XOR, etc.
Kaustubh V Sakhare
VHDL Programming Paradigm
Behavioral Style Architecture
•In comparison to the dataflow style architecture, the
behavioral style architecture provides no details as to how the
design is implemented in actual hardware.
• VHDL code written in a behavioral style does not necessarily
reflect how the circuit is implemented when it is synthesized.
Instead, the behavioral style models how the circuit outputs will
react to the circuit inputs.
•In other words, dataflow modeling describes how the circuit
should look in terms of logic gates whereas behavioral
modeling describes how the circuit should behave.
•The heart of the behavioral style architecture is the process
statement.
Kaustubh V Sakhare
VHDL Programming Paradigm
Process Statement
Kaustubh V Sakhare
VHDL Programming Paradigm
Kaustubh V Sakhare
VHDL Programming Paradigm
Signal Assignment Statement
F <= A XOR B;"
Syntax of the if statement.
if (condition) then
<statements>
elsif (condition) then
<statements>
else
<statements>
end if;
statement. These two statements essentially do the same thing but the if
statement is a sequential statement found inside a process body while the
conditional signal assignment statement is one form of concurrent signal
assignment.
Kaustubh V Sakhare
VHDL Programming Paradigm
Write some VHDL code using an if statement that implements
the following logic function: F OUT(A;B;C) = ABC + BC
entity my_ex is
port (A,B,C : in std_logic;
F_OUT : out std_logic);
end my_ex;
architecture dumb_example of my_ex is
begin
proc1: process(A,B,C) is
begin
if (A = ’1’ and B = ’0’ and C = ’0’) then
F_OUT <= ’1’;
elsif (B = ’1’ and C = ’1’) then
F_OUT <= ’1’;
else
F_OUT <= ’0’;
end if;
end process proc1; Kaustubh V Sakhare
end dumb_example;
VHDL Programming Paradigm
Alternative solution to Example
Kaustubh V Sakhare
VHDL Programming Paradigm
Write some VHDL
code that implements the 8:1 MUX
shown below. Use an if statement
in your implementation.
Kaustubh V Sakhare
VHDL Programming Paradigm
entity mux_8t1 is
port ( Data_in : in std_logic_vector (7 downto 0);
SEL : in std_logic_vector (2 downto 0);
F_CTRL : out std_logic);
end mux_8t1;
architecture mux_8t1_arc of mux_8t1 is
begin
my_mux: process (Data_in,SEL)
begin
if (SEL = "111") then F_CTRL <= Data_in(7);
elsif (SEL = "110") then F_CTRL <= Data_in(6);
elsif (SEL = "101") then F_CTRL <= Data_in(5);
elsif (SEL = "100") then F_CTRL <= Data_in(4);
elsif (SEL = "011") then F_CTRL <= Data_in(3);
elsif (SEL = "010") then F_CTRL <= Data_in(2);
elsif (SEL = "001") then F_CTRL <= Data_in(1);
elsif (SEL = "000") then F_CTRL <= Data_in(0);
else F_CTRL <= ’0’;
end if;
end process my_mux; Kaustubh V Sakhare
end mux_8t1_arc;
VHDL Programming Paradigm
Write some VHDL code that implements the 8:1 MUX shown
here.
Use as many if statements as you deem necessary to
implement your design. In the black-box diagram, the CE
input is a chip enable. When CE = '1', the output acts like the
MUX of Example 10. When CE is '0', the output of the MUX is
'0'.
Kaustubh V Sakhare
VHDL Programming Paradigm
entity mux_8to1_ce is
port ( Data_in : in std_logic_vector (7 downto 0);
SEL : in std_logic_vector (2 downto 0);
CE : in std_logic;
F_CTRL : out std_logic);
end mux_8to1_ce;
architecture mux_8to1_ce_arch of mux_8to1_ce is
begin
my_mux: process (Data_in,SEL,CE)
begin
if (CE = ’0’) then
F_CTRL <= ’0’;
else
if (SEL = "111") then F_CTRL <= Data_in(7);
elsif (SEL = "110") then F_CTRL <= Data_in(6);
elsif (SEL = "101") then F_CTRL <= Data_in(5);
elsif (SEL = "100") then F_CTRL <= Data_in(4);
elsif (SEL = "011") then F_CTRL <= Data_in(3);
elsif (SEL = "010") then F_CTRL <= Data_in(2);
elsif (SEL = "001") then F_CTRL <=Kaustubh
Data_in(1);
V Sakhare
elsif (SEL = "000") then F_CTRL <= Data_in(0);
else F_CTRL <= ’0’;
VHDL Programming Paradigm
Case Statement
Kaustubh V Sakhare
VHDL Programming Paradigm
Case Statement
Write some VHDL code that implements the following function
using the case statement: F OUT(A;B;C) = ABC + BC
entity my_example is
port (A,B,C : in std_logic;
F_OUT : out std_logic);
end my_example;
architecture my_soln_exam of my_example is
signal ABC: std_logic_vector(2 downto 0);
begin
ABC <= A & B & C; -- group signals for case statement
my_proc: process (ABC)
begin
case (ABC) is
when "100" => F_OUT <= ’1’;
when "011" => F_OUT <= ’1’;
when "111" => F_OUT <= ’1’;
when others => F_OUT <= ’0’;
end case;
end process my_proc; Kaustubh V Sakhare
end my_soln_exam;
VHDL Programming Paradigm
The three main flavors of VHDL modeling styles include
dataflow, behavioral and structural models.
VHDL behavioral models, by definition, use process statements.
VHDL dataflow models by definition use concurrent signal
assignment, conditional signal assignment and/or selected signal
assignment.
The process statement is a concurrent statement. Statements
appearing within the process statement are sequential statements.
The case statement has a direct analogy to the selected signal
assignment statement used in dataflow modeling.
Both the case statement and the if statement can be nested.
Concurrent, conditional and selected signal assignment
statements can not be nested.
The simplest concurrent statement is the concurrent signal
assignment statement (e.g. \F <= A;"). Its sequential equivalent is
the sequential signal assignment statement and it looks identical.
Kaustubh V Sakhare
VHDL Operators
Kaustubh V Sakhare
Simple Storage Elements using VHDL
Write the VHDL code that describes a D flip flop shown in fig.
Use a behavioral model in your description.
Kaustubh V Sakhare
Simple Storage Elements using VHDL
----------------------------------------------
-- Model of a simple D Flip-Flop
----------------------------------------------
entity d_ff_x is
port ( D, CLK : in std_logic;
Q : out std_logic);
end d_ff_x;
architecture my_d_ff of d_ff_x is
begin
dff: process (D, CLK)
begin
if (rising_edge(CLK)) then
Q <= D;
end if;
end process dff; end my_d_ff; Kaustubh V Sakhare
Simple Storage Elements using VHDL
Write the VHDL code that describes a D flip flop shown in
the fig. Use a behavioral model in your description.
Consider the S input to be an active-low, synchronous
input that sets the D flip flop outputs when asserted.
Kaustubh V Sakhare
Simple Storage Elements using VHDL
-- D Flip-flop model with active-low synchronous set input.
entity d_ff_ns is
port ( D,S : in std_logic;
CLK : in std_logic;
Q : out std_logic);
end d_ff_ns;
architecture my_d_ff_ns of d_ff_ns is
begin
dff: process (D,S,CLK)
begin
if (rising_edge(CLK)) then
if (S = ’0’) then
Q <= ’1’;
else
Q <= D;
end if;
end if;
end process dff; end my_d_ff_ns;
Kaustubh V Sakhare
Simple Storage Elements using VHDL
Write the VHDL code that describes a D flip flop shown in
the fig. Use a behavioral model in your description.
Consider the R input to be an active-high, asynchronous
input that resets the D flip flop outputs when asserted.
Kaustubh V Sakhare
Simple Storage Elements using VHDL
-- D Flip-flop model with active-high asynchronous reset input.
entity d_ff_r is
port ( D,R : in std_logic;
CLK : in std_logic;
Q : out std_logic);
end d_ff_r;
architecture my_d_ff_r of d_ff_r is
begin
dff: process (D,R,CLK)
begin
if (R = ’1’) then
Q <= ’0’;
elsif (falling_edge(CLK)) then
Q <= D;
end if;
end process dff;
end my_d_ff_r; Kaustubh V Sakhare
Excercise
* For the following function, write VHDL behavioral models that
implement these functions using both case statements and if
statements (two separate models for each function).
a) F(A;B) = AB + A + AB
b) F(A;B;C;D) = ACD + BC + BCD
* For the circuit below, write the VHDL behavioral model that
implements it using both case statements and if statements (two
separate models).
Kaustubh V Sakhare
Structural Modeling
The similarities between these two approaches of
programming are listed in Table
Kaustubh V Sakhare
Structural Modeling
Kaustubh V Sakhare
Structural Modeling
Design a 3-bit comparator using a VHDL structural modeling.
The interface to this circuit is described in the
diagram below.
Kaustubh V Sakhare
Structural Modeling
Kaustubh V Sakhare
Structural Modeling
Kaustubh V Sakhare
Structural Modeling
Generate the top-level entity declaration
Kaustubh V Sakhare
Structural Modeling
entity big_xnor is
Port ( A,B : in std_logic;
F : out std_logic);
end big_xnor;
component big_xnor
Port ( A,B : in std_logic;
F : out std_logic);
end component;
Kaustubh V Sakhare
Structural Modeling
entity big_and3 is
Port ( A,B,C : in std_logic;
F : out std_logic);
end big_and3;
component big_and3
Port ( A,B,C : in std_logic;
F : out std_logic);
end component
entity my_compare is
Port ( A_IN : in std_logic_vector(2 downto 0);
B_IN : in std_logic_vector(2 downto 0);
EQ_OUT : out std_logic);
end my_compare;
architecture ckt1 of my_compare is
-- XNOR gate --------------------
component big_xnor is
Port ( A,B : in std_logic;
F : out std_logic);
end component;
Kaustubh V Sakhare
Structural Modeling
entity my_compare is
Port ( A_IN : in std_logic_vector(2 downto 0);
B_IN : in std_logic_vector(2 downto 0);
EQ_OUT : out std_logic);
end my_compare;
architecture ckt2 of my_compare is
component big_xnor is
Port ( A,B : in std_logic;
F : out std_logic);
end component;
component big_and3 is
Port ( A,B,C : in std_logic;
F : out std_logic);
end component;
signal p1_out,p2_out,p3_out : std_logic;
begin
x1: big_xnor port map (A_IN(2),B_IN(2),p1_out);
x2: big_xnor port map (A_IN(1),B_IN(1),p2_out);
x3: big_xnor port map (A_IN(0),B_IN(0),p3_out);
a1: big_and3 port map (p1_out,p2_out,p3_out,EQ_OUT);
Kaustubh V Sakhare
end ckt2;
Structural Modeling
-- 3-input AND gate -------------
component big_and3 is
Port ( A,B,C : in std_logic;
F : out std_logic);
end component;
-- intermediate signal declaration
signal p1_out,p2_out,p3_out : std_logic;
begin
x1: big_xnor port map (A => A_IN(2),
B => B_IN(2),
F => p1_out);
x2: big_xnor port map (A => A_IN(1),
B => B_IN(1),
F => p2_out);
x3: big_xnor port map (A => A_IN(0),
B => B_IN(0),
F => p3_out);
a1: big_and3 port map (A => p1_out,
B => p2_out, C => p3_out,
F => EQ_OUT);end ckt1; Kaustubh V Sakhare
Structural Modeling
entity full_adder is
Port ( a,b,cin : in STD_LOGIC;
sum,cout : out STD_LOGIC);
end full_adder;
Kaustubh V Sakhare
Structural Modeling
component and2
port(a1,a2:in std_logic;
u:out std_logic);
end component;
signal s1,s2,s3,s4,s5:std_logic;
begin
x1:xor2 port map(a,b,s1);
x2:xor2 port map(s1,cin,sum);
r1:and2 port map(a,b,s2);
r2:and2 port map(b,cin,s3);
r3:and2 port map(a,cin,s4);
o1:or2 port map(s2,s3,s5);
o2:or2 port map(s4,s5,cout);
end fa_str;
Kaustubh V Sakhare
D Flip-op (Behavioral Model)
-------------------------------------------------------------------
-- D flip-flop: RET D flip-flop with single output
-- Required signals:
---------------------------------------------------
-- CLK,D: in STD_LOGIC;
-- Q: out STD_LOGIC;
-------------------------------------------------------------------
Kaustubh V Sakhare
D Flip-op (Behavioral Model)
process (CLK,D)
begin
if (rising_edge(CLK)) then
Q <= D;
end if;
end process;
Kaustubh V Sakhare
D Flip-op with Active-low Asynchronous Preset
(Behavioral Model)
-------------------------------------------------------------------
-- D flip-flop: FET D flip-flop with asynchronous preset.
The preset input takes precedence over the synchronous
input.
--
-- Required signals:
---------------------------------------------------
-- CLK,D,S: in STD_LOGIC;
-- Q: out STD_LOGIC;
Kaustubh V Sakhare
-------------------------------------------------------------------
process (CLK,D,S)
begin
if (S = ’0’) then
Q <= ’1’;
elsif (falling_edge(CLK)) then
Q <= D;
end if;
end process;
--
Kaustubh V Sakhare
8-Bit Register with Load Enable (Behavioral Model)
-------------------------------------------------------------------
-- Register: 8-bit Register with load enable.
--
-- Required signals:
---------------------------------------------------
-- CLK,LD: in STD_LOGIC;
-- D_IN: in STD_LOGIC_VECTOR(7 downto 0);
-- D_OUT: out STD_LOGIC_VECTOR(7 downto 0);
-------------------------------------------------------------------
Kaustubh V Sakhare
process (CLK,LD)
begin
if (rising_edge(CLK)) then
if (LD = ’1’) then -- positive logic for LD
D_OUT <= D_IN;
end if;
end if;
end process;
Kaustubh V Sakhare
Synchronous Up/Down Counter (Behavioral Model)
-------------------------------------------------------------------
-- Counter: synchronous up/down counter with
asynchronous
-- reset and synchronous parallel load.
---------------------------------------------------
Kaustubh V Sakhare
Synchronous Up/Down Counter (Behavioral Model)
-------------------------------------------------------------------
-- Counter: synchronous up/down counter with
asynchronous
-- reset and synchronous parallel load.
---------------------------------------------------
entity COUNT_8B is
port ( RESET,CLK,LD,UP : in std_logic;
DIN : in std_logic_vector (7 downto 0);
COUNT : out std_logic_vector (7 downto 0));
end COUNT_8B;
Kaustubh V Sakhare
architecture my_count of COUNT_8B is
signal t_cnt : std_logic_vector(7 downto 0);
begin
process (CLK, RESET)
begin
if (RESET = ’1’) then
t_cnt <= (others => ’0’); -- clear
elsif (rising_edge(CLK)) then
if (LD = ’1’) then t_cnt <= DIN; -- load
else
if (UP = ’1’) then t_cnt <= t_cnt + 1; -- incr
else t_cnt <= t_cnt - 1; -- decr
end if;
end if;
end if;
end process;
COUNT <= t_cnt;
end my_count; Kaustubh V Sakhare
8-Bit Comparator (Behavioral Model)
-------------------------------------------------------------------
-- Comparator: Implemented as a behavioral model. The outputs
-- include equals, less than and greater than status.
-- Required signals:
-- CLK: in STD_LOGIC;
-- A_IN, B_IN : in STD_LOGIC_VECTOR(7 downto 0);
-- ALB, AGB, AEB : out STD_LOGIC
Kaustubh V Sakhare
process(CLK)
begin
if ( A_IN < B_IN ) then ALB <= ’1’;
else ALB <= ’0’;
end if;
if ( A_IN > B_IN ) then AGB <= ’1’;
else AGB <= ’0’;
end if;
if ( A_IN = B_IN ) then AEB <= ’1’;
else AEB <= ’0’;
end if; end process;
Kaustubh V Sakhare
BCD to 7-Segment Decoder (Data-Flow Model)
-------------------------------------------------------------------
-- BCD to 7-Segment Decoder: Implemented as combinatorial
circuit.
-- Outputs are active low; Hex outputs are included. The
SSEG format
-- is ABCDEFG (segA, segB etc.)
--
-- Required signals:
---------------------------------------------------
-- BCD_IN : in STD_LOGIC_VECTOR(3 downto 0);
-- SSEG : out STD_LOGIC_VECTOR(6 downto 0);
-------------------------------------------------------------------
Kaustubh V Sakhare
with BCD_IN select
SSEG <= "0000001" when "0000", -- 0
"1001111" when "0001", -- 1
"0010010" when "0010", -- 2
"0000110" when "0011", -- 3
"1001100" when "0100", -- 4
"0100100" when "0101", -- 5
"0100000" when "0110", -- 6
"0001111" when "0111", -- 7
"0000000" when "1000", -- 8
"0000100" when "1001", -- 9
"0001000" when "1010", -- A
"1100000" when "1011", -- b
"0110001" when "1100", -- C
"1000010" when "1101", -- d; "0110000" when "1110", -- E
"0111000" when "1111", -- F
"1111111" when others; -- turn off all LEDs
Kaustubh V Sakhare