ADE Lecture5
ADE Lecture5
Example:
CASE control IS
WHEN "00" => x<=a; y<=b;
WHEN "01" => x<=b; y<=c;
WHEN OTHERS => x<="0000"; y<="ZZZZ";
END CASE;
CASE
The CASE statement (sequential) is very similar to WHEN
(combinational). Here too all permutations must be tested, so
the keyword OTHERS is often helpful. Another important
keyword is NULL (the counterpart of UNAFFECTED), which should
be used when no action is to take place. For example, WHEN
OTHERS => NULL;
CASE allows multiple assignments for each test condition
(as shown in the example above), while WHEN allows only one.
Like in the case of WHEN, here too ‘‘WHEN value’’ can
take up three forms:
WHEN value -- single value
WHEN value1 to value2 -- range, for enumerated data
-- types only
WHEN value1 | value2 |... -- value1 or value2 or ...
Example 6: DFF with Asynchronous Reset #3
The code below implements the same DFF of example 1
here CASE was used instead of IF only. Notice that a few
unnecessary declarations were intentionally included in the
code to illustrate their usage.
LIBRARY ieee; -- Unnecessary declaration, because
-- BIT was used instead of STD_LOGIC
USE ieee.std_logic_1164.all;
ENTITY dff IS
PORT (d, clk, rst: IN BIT; q: OUT BIT);
END dff;
ARCHITECTURE dff3 OF dff IS
BEGIN
PROCESS (clk, rst)
BEGIN
CASE rst IS
WHEN '1'=> q<='0';
WHEN '0'=> IF(clk'EVENT AND clk='1')THEN q<= d;
END IF;
WHEN OTHERS => NULL;--Unnecessary ,rst is BIT
END CASE;
END PROCESS;
END dff3;
LOOP
As the name says, LOOP is useful when a piece of code must be
instantiated several times. Like IF, WAIT, and CASE, LOOP is
intended exclusively for sequential code, so it too can only be
used inside a PROCESS, FUNCTION, or PROCEDURE.
There are several ways of using LOOP, as shown in the syntaxes:
FOR / LOOP: The loop is repeated a fixed number of times.
[label:] FOR identifier IN range LOOP
(sequential statements)
END LOOP [label];
WHILE / LOOP: The loop is repeated until a condition no longer
holds.
[label:] WHILE condition LOOP
(sequential statements)
END LOOP [label];
EXIT: Used for ending the loop.
[label:] EXIT [label] [WHEN condition];
NEXT: Used for skipping loop steps.
[label:] NEXT [loop_label] [WHEN condition];
LOOP: Examples
Example:
-- the loop will be repeated unconditionally until i -
-- reaches 5 (that is, six times).
FOR i IN 0 TO 5 LOOP -- the range must be static
x(i) <= enable AND w(i+2);
y(0, i) <= w(i);
END LOOP;
Example
--LOOP will keep repeating while i < 10.
WHILE (i < 10) LOOP
WAIT UNTIL clk'EVENT AND clk='1';
(other statements)
END LOOP;
LOOP: Examples
Example
-- the loop will end as soon as a value different from
‘0’ is found in the data vector.
FOR i IN data'RANGE LOOP
CASE data(i) IS
WHEN '0' => count:=count+1;
WHEN OTHERS => EXIT;
END CASE;
END LOOP;
Example
--NEXT causes LOOP to skip one iteration when i=skip
FOR i IN 0 TO 15 LOOP
NEXT WHEN i=skip; -- jumps to next iteration
)...(
END LOOP;
Example 7: Carry Ripple Adders
Figure below shows an 8-bit unsigned carry ripple adder. The top-
level diagram shows the inputs and outputs of the circuit: a and b
are the input vectors to be added, cin is the carry-in bit, s is the
sum vector, and cout is the carry-out bit. The one-level below-top
diagram shows how the carry bits propagate (ripple).
Simulation results:
Example 8: RAM
ENTITY ram IS
GENERIC ( bits: INTEGER := 8; -- # of bits per word
words: INTEGER:= 16); -- # of words in the memory
PORT ( wr_ena, clk:IN STD_LOGIC;
addr:IN INTEGER RANGE 0 TO words-1;
data_in:IN STD_LOGIC_VECTOR (bits-1 DOWNTO 0);
data_out:OUT STD_LOGIC_VECTOR (bits-1 DOWNTO 0));
END ram;
---------------------------------------------------
ARCHITECTURE ram OF ram IS
TYPEv_aryISARRAY(0TOwords-1)OFSTD_LOGIC_VECTOR(bits-1DOWNTO0);
SIGNAL memory: v_ary;
BEGIN
PROCESS (clk, wr_ena)
BEGIN
IF (wr_ena='1') THEN
IF(clk'EVENT AND clk='1')THEN memory(addr) <= data_in;
END IF;
END IF;
END PROCESS;
data_out <= memory(addr);
END ram;
Using Sequential Code to Design
Combinational Circuits
The sequential code can be used to implement either
sequential or combinational circuits. the following rules should
be observed:
Rule 1: Make sure that all input signals used (read) in the
PROCESS appear in its sensitivity list.
Rule 2: Make sure that all combinations of the input/output
signals are included in the code; that is, make sure that,
by looking at the code, the circuit’s complete truth-table
can be obtained (indeed, this is true for both sequential
as well as concurrent code).
Example 9: Bad Combinational Design
ENTITY example IS
PORT (a, b, c, d: IN STD_LOGIC;
sel: IN INTEGER RANGE 0 TO 3;
x, y: OUT STD_LOGIC);
END example;
--------------------------------------
ARCHITECTURE example OF example IS
BEGIN
PROCESS (a, b, c, d, sel)
BEGIN
IF (sel=0) THEN x<=a; y<='0';
ELSIF (sel=1) THEN x<=b; y<='1';
ELSIF (sel=2) THEN x<=c;
ELSE x<=d;
END IF;
END PROCESS;
END example;
Example 9: Bad Combinational Design
After compiling, the report files show that no flip-flops were
inferred (as expected). but, when we look at the simulation
results :