Descrip (On Styles in Verilog
Descrip (On Styles in Verilog
2. Behavioral
• Procedural assignment Using procedural statements
– Blocking similar to a program in high-level
language.
– Non-blocking
1
02/09/17
2
02/09/17
• Point to note:
– Whenever there is an array reference on the RHS with a variable index, a
MUX is generated by the synthesis tool.
– If the index is a constant, just a wire will be generated.
Example: assign out = data[2];!
3
02/09/17
Condi>onal operator
generates a MUX
• Point to note:
– Whenever a condi>onal is encountered in the RHS of an expression, a 2-
to-1 MUX is generated.
– In the previous example, since the variables “a”, “b” and “f” are vectors, an
array of 2-to-1 MUX-es are generated.
– What hardware will be generated by the following?
assign f = (a==0) ? (c+d) : (c–d); !
4
02/09/17
Non-constant index in
expression on LHS
generates a decoder
• Point to note:
– A constant index in the expression on the LHS will not generate a decoder.
– Example: assign out[5] = in;!
This will simply generate a wire connec>on.
– As a rule of thumb, whenever the synthesis tool detects a variable index in
the LHS, a decoder is generated.
5
02/09/17
En D Qn
module level_sensitive_latch (D, Q, En);!
0 x Qn-1
input D, En;!
1 0 0
output Q;!
1 1 1
assign Q = En ? D : Q;!
Generates a D-type latch
endmodule!
Here is an example to
describe a sequen>al
logic element using
“assign” statement.
R S R Qn
Q
1 1 Qn-1
0 1 0
1 0 1
S Qbar 0 0 ?
6
02/09/17
Simula>on Output
0 S=0, R=1, Q=0, Qbar=1!
5 S=1, R=1, Q=0, Qbar=1!
10 S=1, R=0, Q=1, Qbar=0!
15 S=1, R=1, Q=1, Qbar=0!
20 S=0, R=0, Q=1, Qbar=1!
and then the simulator hangs!
7
02/09/17
END OF LECTURE 12
8
02/09/17
9
02/09/17
module testbench_example;!
reg a, b, cin, sum, cout;!
initial!
cin = 1’b0;!
initial!
• The three “ini>al” blocks execute
begin! concurrently.
#5 a = 1’b1; b=1’b1;! • The first block executes at >me 0.
#5 b = 1’b0;!
• The third block terminates
end!
simula>on at >me 25 units.
initial!
#25 $finish;!
endmodule!
10
02/09/17
module generating_clock;!
output reg clk;! • “ini>al” and “always
blocks can coexist within
initial!
clk = 1’b0; // initialized to 0 at time 0! the same Verilog module.
• They all execute
always!
concurrently; “ini>al”
#5 clk = ~clk; // Toggle after time 5 units!
only once and “always”
initial! repeatedly.
#500 $finish;!
endmodule!
11
02/09/17
• Only “reg” type variable can be assigned within an “ini>al” or ‘always” block.
• Basic reason:
– The sequen>al “always” block executes only when the event expression triggers.
– At other >mes the block is doing nothing.
– An object being assigned to must therefore remember the last value assigned (not
con>nuously driven).
– So, only “reg” type variables can be assigned within the “always” block.
– Of course, any kind of variable may appear in the event expression (reg, wire, etc.).
12
02/09/17
13
02/09/17
(c) case
case (<expression>)! • Each sequen>al_statement can be
expr1: sequential_statement;! a single statement or a group of
expr2: sequential_statement;! statements within “begin … end”.
...! • Can replace a complex “if … else”
statement for mul>way branching.
exprn: sequential_statement;!
• The expression is compared to the
default: default_statement;!
alterna>ves (expr1, expr2, etc.) in
endcase!
the order they are wrijen.
• If none of the alterna>ves matches,
the default statement is executed.
14
02/09/17
END OF LECTURE 13
15
02/09/17
16
02/09/17
Example:
integer mycount;!
reg [100:1] data;!
integer i;!
initial!
for (mycount=0; mycount<=255; mycount=mycount+1)!
$display (“My count:%d”, mycount);!
initial!
for (i=1; i<=100; i=i+1)!
data[i] = 1’b0;!
17
02/09/17
Example:
reg clock;! Exactly 100 clock pulses
initial! are generated.
begin!
clock = 1’b0;!
repeat (100)!
#5 clock = ~clock;!
end!
18
02/09/17
19
02/09/17
@ (event_expression)
• The event expression specifies the event that is required to resume execu>on
of the procedural block.
• The event can be any one of the following:
a) Change of a signal value.
b) Posi>ve or nega>ve edge occurring on signal (posedge or negedge).
c) List of above-men>oned events, separated by “or” or comma.
• A “posedge” is any transi>on from {0, x, z} to 1, and from 0 to {z, x}.
• A “negedge” is any transi>on from {1, x, z} to 0, and from 1 to {z, x}.
20
02/09/17
• Examples:
– @ (in) // “in” changes
– @ (a or b or c) // any of “a”, “b”, “c” changes
– @ (a, b, c) // -- do --
– @ (posedge clk) // posi>ve edge of “clk”
– @ (posedge clk or negedge reset) // posi>ve edge of “clk” or nega>ve
edge of “reset”
– @ (*) // any variable changes
21
02/09/17
22
02/09/17
END OF LECTURE 14
23
02/09/17
24
02/09/17
25
02/09/17
26
02/09/17
// A small modification!
!
module incomp_state_spec (curr_state, flag);!
input [0:1] curr_state;!
output reg [0:1] flag;!
!
always @(curr_state)!
begin! Here the variable “flag” is
flag = 0;! defined for all the possible
case (curr_state)! values of “curr_state”.
0,1 : flag = 2;! • A pure combina>onal circuit
3 : flag = 0;! will be generated.
endcase! • The latch is avoided.
end!
endmodule!
27
02/09/17
28
02/09/17
// An n-bit comparator!
module compare (A, B, lt, gt, eq);!
parameter word_size = 16;!
input [word_size-1:0] A, B;! For actual synthesis, it is
output reg lt, gt, eq;! common to have a structured
design representa>on of the
always @ (*)!
comparator.
begin!
gt = 0; lt = 0; eq = 0;!
if (A > B) gt = 1;!
else if (A < B) lt = 1;!
else eq = 1!
end!
endmodule!
29
02/09/17
// A 2-bit comparator!
module compare (A1, A0, B1, B0, lt, gt, eq);!
input A1, A0, B1, B0;!
output reg lt, gt, eq;!
always @ (A1, A0, B1, B0)!
begin!
lt = ({A1,A0) < {B1,B0});!
gt = ({A1,A0) > {B1,B0});!
eq = ({A1,A0) == {B1,B0});!
end!
endmodule!
30
02/09/17
END OF LECTURE 15
31