Course2 - Week4 Adder Subtractor
Course2 - Week4 Adder Subtractor
1 ARITHMETIC BLOCKS
Jean-Pierre Deschamps
University Rovira i Virgili, Tarragona, Spain
1
4 .1
Arithmetic blocks constitute an important part of practically any digital circuit. They
deserve a particular treatment.
Four operations:
1. Addition
2. Subtraction
3. Multiplication
4. Division
2
1. Binary adder
4 .1
Algorithm - n-bit binary adder, s = x + y + carry
cy(0) <= carry;
See lessons 2.2 and 2.3.
for i in 0 to n-1 loop
s(i) <= x(i) xor y(i) xor cy(i);
Each FA (full adder = 1-bit adder)
cy(i+1) <= (x(i) and y(i)) or (x(i) and cy(i))
block implements two Boolean
or (y(i) and cy(i));
equations:
end loop;
s(n) <= cy(n);
s(i) <= x(i) xor y(i) xor cy(i);
cy(i+1) <=
(x(i) and y(i)) or (x(i) and cy(i)) S = sn2n + sn-12n-1 + sn-22n-2 + ··· + s12 + s0.
or (y(i) and cy(i));
xn-1 yn-1 x1 y1 x0 y0
3
sn-1 s1 s0
2. Binary subtractor
4 .1
A binary subtractor computes
Representation of D?
D = X - Y - borrow,
D can be represented as a 2'complement (n+1)-bit
X and Y: n-bit numbers,
number:
borrow: initial carry borrow.
Maximum value of D: 2n-1 D = -dn2n + dn-12n-1 + dn-22n-2 + ··· + d12 + d0.
(X = 2n-1, Y = borrow = 0).
Minimum value of D:-2n
(X = 0, Y = 2n-1, borrow = 1).
4
2. Binary subtractor
4 .1
“Pencil and paper" algorithm: Examples:
d = (x - y - bIN) mod 2;
Algorithm - n-bit binary subtractor, s = x - y - borrow
an outgoing borrow bit b(0) <= borrow;
for i in 0 to n-1 loop
bOUT = 1 iff x - y - bIN < 0. d(i) <= (x(i) + y(i) + b(i)) mod 2;
b(i+1) <= sign(x(i) - y(i) - b(i));
end loop;
Comment:
s(n) <= b(n);
(x - y - bIN) mod 2 = (x + y + bIN) mod 2 5
2. Binary subtractor
4 .1
Algorithm - n-bit binary subtractor, s = x - y - borrow
b(0) <= borrow;
for i in 0 to n-1 loop
d(i) <= (x(i) + y(i) + b(i)) mod 2;
b(i+1) <= sign(x(i) - y(i) - b(i));
end loop; Algorithm - n-bit binary subtractor, s = x - y - borrow
s(n) <= b(n); b(0) <= borrow;
for i in 0 to n-1 loop
d(i) <= x(i) xor y(i) xor b(i);
b(i+1) <=
(not(x(i)) and y(i)) or (not(x(i)) and b(i))
Iteration body in terms of or (y(i) and b(i));
Boolean operations => end loop;
s(n) <= b(n);
6
2. Binary subtractor
4 .1
Complete circuit.
b n-1 b2 b1 b0 = borrow
dn =bn FS .... FS FS
7
dn-1 d1 d0
4 .1
(Exercise)
Define a circuit that generate the difference D = x - y under the form (sign and
magnitude) D = (-1)sign·|D|.
Hint: compute in parallel x – y and y – x and select |D| in function of the sign of x – y.
8
4 .1
(Solution)
Define a circuit that generate the difference D = x - y under the form (sign and
magnitude) D = (-1)sign·|D|.
Hint: compute in parallel x – y and y – x and select |D| in function of the sign of x – y.
x y y x
subtract. 0 subtract. 0
d’ d’’
0 1
sign d 9
3. Binary multiplier
4 .1
A binary multiplier computes If Y = ym-1·2m-1 + ym-2·2m-2 + ··· + y1·2 + y0 then
10
3. Binary multiplier
4 .1
p0 = X·y0;
p1 = X·y1·2;
p2 = X·y2·22;
Example:
···
pm-1 = X·ym-1·2m-1;
101101
x 1011
P = p0 + p1 + p2 + ··· + pm-1 .
---------
101101 p0
101101 p1
000000 p2
101101 p3
------------------
111101111 P
11
3. Binary multiplier
4 .1
0 X
acc_in X
p0 = X·y0; acc_in X
y
p1 = X·y1·2; y y(0)
acc_out
p2 = X·y2·22;
X·2
···
adder
pm-1 = X·ym-1·2m-1; acc_in X
y y(1)
acc_out acc_out
P = p0 + p1 + p2 + ··· + pm-1 .
X·22
acc_in X
Algorithm - Multiplication, y y(2)
right to left algorithm acc_out
12
P
4. Binary divider
4 .1
X = Q·Y + R,
X, Y: naturals, with X < Y, In other words:
Q: multiple of 2-p ,
X/Y = Q + (R/Y) where Q is a multiple of 2-p and
R < Y·2-p. error = (X/Y) - Q = (R/Y) < 2-p.
13
4. Binary divider
4 .1
Algorithm - Binary division Example: X = 21, Y = 35, p = 6
r(0) <= x;
for i in 1 to p loop 2·21 = 1·35 + 7
d <= 2*r(i-1) - y; 2·7 = 0·35 + 14
if d < 0 then q(i) <= 0; r(i) <= 2*r(i-1); 2·14 = 0·35 + 28
else q(i) <= 1; r(i) <= d; 2·28 = 1·35 + 21
end if; 2·21 = 1·35 + 7
end loop; 2·7 = 0·35 + 14
14
4. Binary divider X Y
4 .1
r Y
0
r Y
Algorithm - Binary division q1 q sign
r+ q subtractor
r(0) <= x;
2·r
for i in 1 to p loop
d <= 2*r(i-1) - y; r Y 1 0
if d < 0 then q(i) <= 0; r(i) <= 2*r(i-1); q2 q
r+
else q(i) <= 1; r(i) <= d;
r+
end if;
end loop; r Y
q3 q
r+
····
r Y
qp q
r+
rp 15
4 .1
SUMMARY
16
4.2 Introduction to VHDL: Lexicon, syntax
and structure
Juan Antonio Martinez1, Lluís Terés1,2
1Universitat Autònoma de Barcelona (UAB)
2Instituto de Microelectrónica de Barcelona, IMB-CNM (CSIC)
HDLs: what are and what are they used for? 4 .2
What HDLs are?
• High level abstraction languages oriented to describe/model Hardware
• Write formal, non ambiguous models instead of using natural language or drawings
• Understandable by computers and humans
• Like programming languages but to model Hw instead of developing Sw
synthesis
20
1. Basic on VHDL Lexical & Syntax 4 .2
Lexical elements: Reserved Words, Identifiers, Symbols and Literals
Language Reserved Words
abs access after alias all and architecture
array assert attribute begin block body buffer
bus case component else elsif end entity
exit file for function generate generic guarded
if in inout is label library nand
new next nor not null of on
open or others out process procedure ...
Identifiers to provide specific names to VHDL elements and objects
- Based on character set {„a‟…‟z‟, ‟A‟…‟Z‟, ‟0‟…‟9‟, ‟_‟}
- First character shall be alphabetical and „_‟ at the end or two „__‟ are forbidden
- Upper/lower-case are indifferent and reserved words are forbidden
Examples: COUNT, aBc, X, f123, VHDL, VH_DL, ABC, q1, Q0
Symbols Literals
- 1 or 2 characters Base Character Physical
- operators, punctuation, comments, part of 2#110_1010# „a‟ „A‟ „@‟ 10 ns
sentences: + - * / ( ) . , : & 16#CA# String 2.2 V
„ < > = | # ; -- => ** := 16#f.ff#e+2 “Tiempo” 50 pF
Decimal “110101” Bit String
/= >= <= <> “
12 0 1E Bit:‟0‟,‟1‟ X”F0f” B”111_100”
1.1. VHDL Objects 4 .2
• VHDL object is any language element able to contain a value
• Types of VHDL objects:
Constant
Variable Object definition:
<Object type> <identifier> : <data type> [:= Initial value];
Signal
File
constant PI : real := 3.1415927; variable Counter : integer :=0; Variables
constant WordBits : natural := 8; variable Increment : integer;
declaration
constant NumWords : natural := 1024;
constant TotBits : natural := WordBits * NumWords; Increment := 2; Variables
Counter := Counter + Increment; assignment
Signal declaration
signal Clk : bit := „0‟;
... & initialization
Clk <= „1‟; Signal assignment
Primary
Entity Configuration Package
Declaration Declaration Declaration
Architecture 1 Package
Secondary
Body
Architecture 2
Architecture n
26
2.1. ENTITY 4 .2
• Like a “black-box” just describing external interface for a module while
hiding its internal behaviour or functionality
• Syntax:
Entity name (module name)
entity <id> is Generic parameters
[<generics>];
[<ports>]; Input/Output ports (electrical interface)
[<declarations>];
[begin <sentences>];
Global declarations (common to any potential architecture of this entity)
end [entity] [<id>];
Passive sentences (common to any potential architecture of this entity)
entity MUX21 is
port( A : in bit; A
B : in bit;
Ctrl : in bit; B MUX21 Z
Z : out bit;
end MUX21;
Ctrl
27
2.1. ENTITY 4 .2
• Like a “black-box” just describing external interface for a module while
hiding its internal behaviour and architecture
• Syntax:
Entity name (module name)
entity <id> is Generic parameters
[<generics>];
[<ports>]; Input/Output ports (electrical interface)
[<declarations>];
[begin <sentences>];
Global declarations (common to any potential architecture of this entity)
end [entity] [<id>];
Passive sentences (common to any potential architecture of this entity)
Concurrent sentences:
• Concurrent assignments
• Instances to components
• Processes
• Blocks
29
2.2.1 Entities & Architectures 4 .2
entity MUX21 is entity MUX21n is
port( A, B, Ctrl : in bit; generic ( n: natural);
Z : out bit); port( A : in bit_vector(n-1 downto 0);
end MUX21; B : in bit_vector(n-1 downto 0);
Ctrl : in bit;
Z : out bit_vector(n-1 downto 0));
architecture Functional of MUX21 is
end MUX21;
begin
process(A, B, Ctrl)
begin architecture Functional of MUX21n is
if Ctrl = „0‟ then begin
Z <= A; process(A, B, Ctrl)
else begin
Z <= B; if Ctrl = „0‟ then
end if; Z <= A;
end process; else
Z <= B;
end Functional; n
end if;
end process; A
A end Functional; B
n
MUX21n
n
Z
B MUX21
Z Ctrl
Ctrl
2.2.1 Entities & Architectures 4 .2
entity MUX21 is
port( A : in bit;
B : in bit;
architecture structural of MUX21 is Ctrl : in bit;
signal Ctrl_n, As, Bs : bit; Z : out bit;
component INV end MUX21;
port( Y : in bit;
Z : out bit);
end component;
component AND2
port( X, Y : in bit;
Z : out bit);
end component;
component OR2
port( X, Y : in bit;
Z : out bit); architecture DataFlow of MUX21 is
As Bs
end component; signal Ctrl_n, N1, N2 : bit;
begin begin
U0: INV port map (Ctrl, Ctrl_n); Ctrl_n <= Ctrl_n
N1 not Ctrl;
and a;
U1: AND2 port map (Ctrl_n, A, As); N1
Z <= (N1
Ctrl_n
or N2);
and a;
U2: AND2 port map (Ctrl, B, Bs); N2 <= Ctrl and b;
U3: OR2 port map (As, Bs, Z); Z
Ctrl_n <= (N1
not Ctrl;
or N2);
end structural; end DataFlow;
Sentencias Concurrentes
2.3. PACKAGE 4 .2
• Useful for code reuse as it could contain definitions of data types, functions and
language objects (constants, variables, signals or files) for its use on different codes
• Two units: Package name
“Package declaration” package <identifier> Declarations of:
[<declarations>]; • Data types
end [package] [<identifier>] • VHDL objects
• Functions & procedures
Package name
“Package body” package body <identifier> Assignments & definitions of:
[<Assignments and • Constants
Detailed definitions>];
• Functions & procedures
end [package body] [<identifier>]
• Any other hidden information
Usual packages:
• Package usage - STANDARD & TEXTIO
- Std_logic_1164
use <library>.<package name>.[<identifier> | all]; - Std_logic_arith
- Used defined
2.3. PACKAGE 4 .2
Example of user defined package
library IEEE; Comment
use IEEE.std_logic_1164.all;
use IEEE.std_logic_arith.all;
package main_parameters is
constant m: natural := 8; -- m-bit processor
-- Value „0‟ in m-bits
constant zero: std_logic_vector(m-1 downto 0):= conv_std_logic_vector(0, m);
-- Value „1‟ in m-bits
constant one: std_logic_vector(m-1 downto 0) := conv_std_logic_vector(1, m);
-- Instruction codes for our simple processor instruction set
constant ASSIGN_VALUE: std_logic_vector(3 downto 0) := "0000";
constant DATA_INPUT: std_logic_vector(3 downto 0) := "0010";
constant DATA_OUTPUT: std_logic_vector(3 downto 0) := "1010";
constant OUTPUT_VALUE: std_logic_vector(3 downto 0) := "1000";
constant OPERATION_ADD: std_logic_vector(3 downto 0) := "0100";
constant OPERATION_SUB: std_logic_vector(3 downto 0) := "0101";
constant JUMP: std_logic_vector(3 downto 0) := "1110";
constant JUMP_POS: std_logic_vector(3 downto 0) := "1100";
constant JUMP_NEG: std_logic_vector(3 downto 0) := "1101";
end main_parameters;
Constant Constant
VHDL constant object Constant Id. 33
data type value
2.3. PACKAGE 4 .2
IEEE standard packages
More complex arithmetic operations & functions with “std_logic” 'L' weak drive, logic zero
data types 'H' weak drive, logic one
'-' don't care
Data type conversion functions (i.e integer <=> std_logic)
• Examples of simplified formal VHDL design units descriptions have been used
36
4.3 Introduction to VHDL: sequential
sentences
Juan Antonio Martinez1, Lluís Terés1,2
1Universitat Autònoma de Barcelona (UAB)
2Instituto de Microelectrónica de Barcelona, IMB-CNM (CSIC)
Preliminary: Sequential vs. Concurrent VHDL worlds 4 .3
Sequential vs. Concurrent Sentences
• Sequential
Algorithmic sentences like for SW languages (if, case, loop, exit, return, …)
Interpreted sequentially Order of sentences is important for the results
Only used in functions, procedures and Processes
• Concurrent
Devoted to express hardware structure (Hw components and blocks are doing concurrently) and
processes working simultaneously along the time
Some sequential sentences have its equivalent concurrent ones
Selected sentences: Process, Signal assignments, Components instantiation
Mainly used in Architectures
• Concurrent to Sequential for simulation
Each concurrent statement could be translated to its equivalent Process based on sequential
statements.
For simulation purposes all the concurrent statements are translated to its equivalent processes.
VHDL event driven simulation will manage just a lot of processes.
38
Preliminary: Sequential vs. Concurrent VHDL worlds 4 .3
General structure of a VHDL model
entity X is
begin
…
end X;
…
S <= A when Sel=„0‟ else B;
Clock <= not clock after 20 ns;
architecture Y of X is …
…
begin P1: process (clock, S);
… begin
…
only concurrent sentences only sequential sentences
end process;
end Y;
P2: process …
39
1. Sequential Sentences (selection) 4 .3
• Where are possible?
• Examples:
Var := „0‟;
Vector := “00011100”;
string := “Message is: ”;
A := B + C;
B := my_function(3,databus)
C := my_function(4,adrbus) + A;
41
1.2. Signal Assignment 4 .3
• Projects a new event (time, value) on the signal driver.
“Event”: “time-value” pair – the signal takes value vi at time ti. t t0 t1 … ti
“Signal Driver”: sequence of time ordered events of a signal. v v0 v1 … vi
• Syntax:
[label:] <signal_name> <= [delay_type] <expression> {after <delay>};
?
y/n y/n
y/n y/n Initialize Signals
DT
Signals Initialization
Drivers
Process-1
Process-2
Process-n
Update signals ...
drivers
d delay
STOP
Wait till all resumed
processes are stopped
0 1 3 4 5 Time
1.4. If … then … else … endif; 4 .3
• Selects the group of sentences to execute depending on a Boolean condition.
• Syntax:
[label:] if <condicion> then
<sentencias secuenciales> FlipFlop: process
{elsif <condicion> then begin clk
<sentencias secuenciales} if rst = „1‟ then
[else Q <= „0‟;
<sentencias secuenciales>] elsif (clk‟even and clk=„1‟) then
end if [label]; Q <= D;
end if; end if;
wait on rst, clk;
• Examples: end process; Flip-flop
process
Process (A, B, Ctrl) begin
Begin if Enable =„1‟ then
if Ctrl = „0‟ then Salida <= Entrada; Latch: process begin
Q <= A; else if load =„1‟ then
else Salida <= „Z‟; Q <= D;
Q <= B; end if; end if;
end if; wait on Enable, Entrada; wait on load, D;
end process; end process; end process;
Mux Buffer Triestate Latch
1.5. Case 4 .3
• Selects the group of sentences to execute
depending on a expression value. [label:] case <expresion> is
{when <value> =>
• Syntax: <sequential sentences>;}
Or of values: when “00” | “01” => ... [when others =>
<sequential sentences>;]
Values range: when 5 to 12 (integers) => ... end case [label];
Last option: when others => ...
• Examples: type weekdays : (Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday);
type typeofday : (Workingday, Holiday);
Signal Day : Weekdays; process
process Signal Daytype : typeofday;
begin begin
case Day is case ValEnt is
when Monday to Friday => when 0 => Res := 5;
Daytype <= Workingday; when 1 | 2 | 8 => Res := ValEnt;
when Saturday | Saunday => when 3 to 7 => Res := ValEnt + 5;
Daytype <= Holiday; when others => Res := 0;
end case; end case;
wait on Day; wait on ValEnt;
end process; end process;
1.6. If & Case entity LatMux is
port(
4 .3
Load : in bit;
• Example: model the behaviour of next block A, B, C, D : in bit;
Ctrl : in bit_vector(0 to 1);
with two “Process”: Y : out bit);
Mux end LatMux;
Ctrl ?
when “01” => Y <= B;
when “10” => Y <= C;
when “11” => Y <= D;
Load end case;
end if;
end process UnicP;
end OneProc; 48
Exercise (solution) 4 .3
• For the same module “LatMux”, could you entity LatMux is
port(
describe a new architecture using just one Load : in bit;
process? A, B, C, D : in bit;
Ctrl : in bit_vector(0 to 1);
Y : out bit);
LatMux end LatMux;
end OneProc; 49
1.7. Loop 4 .3
• Sequential sentences in the loop region are repeated for a number of times.
• Types of loops: “while”, “for” and “without iterations control (infinite loop)”.
• Syntax: [label:] [while <boolean_condition> | for <repetition_control>]
loop
<sequential sentences>}
end loop [label]; achitecture Functional of ParallelAdder is
begin
process (X, Y, Cin);
• Examples: “Full-adder” variable C : std_logic_vector(n downto 0);
entity ParallelAdder is variable tmp : std_logic;
generic (n : natural :=4 ); variable I : integer;
port ( X, Y : in begin
std_logic_vector(n-1 downto 0); C(0) := Cin;
Cin : in std_logic; for I in 0 to n-1 loop
Z : out std_logic_vector(n-1 downto 0); tmp := X(I) xor Y(I);
Cout : out std_logic); Z(I) <= tmp xor C(I);
End ParallelAdder ; C(I+1) := (tmp and C(I)) or (X(I) and Y(I));
end loop;
Cout <= C(n);
• Exit & Next sentences: conditional loop end process;
end Functional;
breakings
1.8. Function 4 .3
• A piece of code devoted to specific computation of input parameters to return a value.
• Syntax for function declaration:
function <name> [(<parameters list>)] return <data_type>;
• Example:
function bv2int (bs: bit_vector(7 downto 0)) return integer; Function declaration
• Example:
procedure bv2int (bs: bit_vector(7 downto 0); x: out integer ); Procedure
declration
• Examples:
assert not(addr < X"00001000" or addr > X"0000FFFF“)
report “Address in range" severity note;
• Example:
report “Check point 13”;
= assert FALSE “Check point 13” severity note;
RESUMEN 4 .3
• Sequential vs. Concurrent worlds in VHDL
• VHDL sequential sentences (inside a process, function or procedure)
Selected sentences:
• Variable & signal assignments • If … then … else … endif • Loop, Exit and Next
• Wait • Case • Assert, Functions & Procedures
54
4.4 Introduction to VHDL: Concurrent
sentences
Juan Antonio Martinez1, Lluís Terés1,2
1Universitat Autònoma de Barcelona (UAB)
2Instituto de Microelectrónica de Barcelona, IMB-CNM (CSIC)
Previous comment 4 .4
General structure of a VHDL model
entity X is
begin
…
end X;
…
S <= A when Sel=„0‟ else B;
Clock <= not clock after 20 ns;
architecture Y of X is …
…
begin P1: process (clock, S);
… begin
…
only concurrent sentences only sequential sentences
… …
end process;
end Y;
P2: process …
56
1. Concurrent sentences (selection) 4 .4
• All the concurrent sentences are being evaluated simultaneously
process
process (<sensitivity signals list>) begin
begin <sequential sentences>
<sequential sentences> = wait on <sensitivity signals list>;
end process; end process;
1.2. Signal Assignment 4 .4
• Syntax:
[label:] <signal> <= [delay_type] <expresion | waveform> {after <delay_time>};
• Examples: A B
Tmp <= A xor B after 10 ns;
Z <= Tmp xor Cin after 10 ns; Cout Full Cin
Cout <= (A and B) or (Tmp and Cin) after 15 ns; Adder
W <= „0‟, „1‟ after 10 ns, „0‟ after 15 ns, „1‟ after 20 ns, Z
„0‟ after 28 ns, „1‟ after 40 ns, „0‟ after 50 ns;
Waveform
W t 0 10 15 20 28 40 50
driver v 0 1 0 1 0 1 0
W
(ns)
59
0 10 20 30 40 50 60 70
1.3. Conditional Signal Assignment 4 .4
• Syntax:
[<label>:] <signal> <= [delay_type]
{<expression|waveform> when <boolean expression> else}
< expression|waveform> [when <boolean expression>];
• Examples:
process (Op1, Op2, Operation)
with Operation select begin
Result <= Op1 + Op2 when add, case Operation is
Op1 - Op2 when subs, when add => Result <= Op1 + Op2;
Op1 and Op2 when andL, = when subs => Result <= Op1 - Op2;
Op1 or Or2 when orL; when andL => Result <= Op1 and Op2;
when orL => Result <= Op1 or Op2;
end case;
Equivalent end process;
Process
Type opcode is (add, subs, andL, orL);
Signal operation : opcode; 61
1.5. Components 4 .4
• Structural and hierarchical descriptions by using components defined somewhere else.
• Syntax for component declaration:
component <idname> [is]
[generic (<generic parameters list>);]
[port (<ports list>);]
end [component] [<idname>];
• Examples:
Association lists by:
• position
component example is U2 : example port map (X, Y, W, Z)
port(a, b, c : in bit;
• name
d : out bit); U4 : example port map
end component example; (d=>Z, a=>X, b=>Y, c=>W);
1.5. Componentes 4 .4
entity FullAdder is
begin
port(X, Y, CIn : in bit; U1 U3
Cout, Sum : out bit); A
end FullAdder;
X Half OrG COut
Y Adder B C
architecture structural of FullAdder is Half
component HalfAdder Adder
port(I1, I2 : in std_logic; CIn Sum
COut, Sum : out std_logic); U2
end component;
component OrG
port(I1, I2 : in std_logic; I1 I2 Sum Cout
O : out std_logic); 0 0 0 0
end component; 0 1 1 0
Positional association list
signal A, B, C : std_logic; 1 0 1 0
begin 1 1 0 1
U1: HalfAdder port map (X, Y, A, B);
U2: HalfAdder port map (B, CIn, C, Sum); Sum <= I1 xor I2; Cout <= I1 and I2;
U3: OrG port map (O => COut, I1 => A, I2 => C);
end structural; 63
Nominal association list
1.6. Generate 4 .4
• Generally used to create “arrays” of component instances, but it could include any other
concurrent sentence.
• Syntax: <label>: {[for <range specification> | if <condition> ]}
generate
entity Register is Parallel in/out
{<concurrent sentences>}
generic (N: positive);
end generate; N-bits Register
port( Clk : in std_logic;
E : in std_logic_vector(N-1 downto 0);
S out std_logic_vector(N-1 downto 0));
• Example: end Register;
architecture structural of Register is
component DFF
E(N-1) E(N-2) E(0) port (Clk : in std_logic;
E : in std_logic;
S : out std_logic);
DFF DFF DFF end component;
variable I : integer;
begin
GenReg: for I in N-1 downto 0 generate
Clk S(N-1) S(N-2) S(0) Reg: DFF port map(Clk, E(I), S(I));
end generate;
end structural;
1.7. Assert, Procedure call & Function call 4 .4
• Same usage and behaviour as the equivalent sequential ones, but in the concurrent world.
[label:] assert <boolean expression>
[report <string of characters>]
[severity (note | warning | error |failure);
• Concurrent “Procedure call” is just like a process with procedure parameters in the sensitivity
list. [label:] <Procedure name> [(<parameters>)];
Signal rst, ck, d, q: std_logic; procedure FFD (signal rst, clk, d: in std_logic;
…/… …/… signal q: out std_logic);
FFD: process (rst, clk); begin
begin if rst = „1‟ then
if rst = „1‟ then Q <= „0‟;
Formal parameters
Q <= „0‟; elsif (clk‟event and clk=„1‟) then
elsif (clk‟event and clk=„1‟) then Q <= D;
Q <= D; end if; end if;
end if; end if; end procedure; D Flip-flop
end process; D Flip-flop
o Declared in a package accessed by “Use” sentence
o Simultaneous definition and usage o Used by instantiation with actual parameters easy reuse
o Repeated usage Cut & Paste of the process o Before simulation the procedures are moved to its
Component & references to it equivalent processes 67
2 Modelling, Simulation and Synthesis 4 .4
package my_package is
type operation is (add,sub);
end my_package;
use work.my_package.all;
entity AddSub is
Port (signal a, b: in std_logic_vector(3 downto 0);
signal opcode : in operation;
signal sign : out std_logic;
signal z : out std_logic_vector(4 downto 0));
End entity AddSub;
VHDL
VHDL
Results Output
Analysis results
69
2 Modelling, Simulation and Synthesis 4 .4
Modulo a verificar (MuT) Banco de pruebas
Module under Test
use work.my_package.all; (Test_Bench)
entity AddSub is
Port (signal a, b: in std_logic_vector(3 downto 0);
signal opcode : in operation;
signal sign : out std_logic;
signal z : out std_logic_vector(4 downto 0));
End entity AddSub;
when add
Results thenOutput
signo z <= „0‟&x
sign + „0‟&y; sign <= „0‟;
d <= “1001”; wait for 100 ns;
Analysis res
when sub thenresults
z <= „0‟&x – „0‟&y;
z VHDL sign <= s; <= add; wait;
oper
end case end process Stimuli;
end process Operate;
End Architecture Functional; end Test_Bench; 71
2 Modelling, Simulation and Synthesis 4 .4
Banco de pruebas
(Test_Bench)
use work.my_package.all;
entity TB_AddSub is end;
architecture Test_Bench of TB_AddSub is
signal c, d : std_logic_vector(3 downto 0);
signal oper : operation; signal signo : std_logic;
signal res : std_logic_vector(4 downto 0);
Begin
Module under Test
MuT: entity work.AddSub(Functional)
port map (c, d, oper, signo, res);
This ends our short introduction to VHDL language for its usage in the current…
“Digital Systems Course”
78