Chapter 4
Chapter 4
Memory
Elements
states
⚫ Sequential components contain memory elements
⚫ The output values of sequential components depend on the input values
and the values stored in the memory elements
If-Else Statements (2/4)
Module Latch(In, Enable, Out); Watch for unintentional Latches
input Enable; Module Latch(In, Enable, Out);
Input [3:0] In; input Enable;
output [3:0] Out; input [3:0] In;
always @(In or Enable) output [3:0] Out;
begin
if(Enable) No latch inference always @(In or Enable)
Out=In;
else begin
Out=0; if(Enable)
end Out=In;
endmodule end
Always@ (In or endmodule
Enable) If Enable ==1
begin Out (new) = In
Out=0; If Enable==0
if(Enable) Out (new) = Out (old)
Out=In;
end // no latch
If-Else Statements (3/4)
module IF_ELSE_VALUE(IN , OUT); module IF_ELSE_VALUE(IN , OUT);
input [1 : 0]IN; input [1 : 0]IN;
output [1 : 0]OUT; IN=0,OUT=11 output [1 : 0]OUT;
reg [1 : 0]OUT; IN=0,OUT=11
reg [1 : 0]OUT; IN=1,OUT=00
always @(IN) always @(IN) IN=1,OUT=00
IN=2,OUT=11 begin IN=2,OUT=00
begin
IN=3,OUT=11 if(IN) IN=3,OUT=00
if(IN==1)
OUT = 2'b00; true OUT = 2'b00; nonzero true
else else
OUT = 2'b11; false OUT = 2'b11; zero false
end end
endmodule endmodule
What is the difference between them?
If-Else Statements (4/4)
d 0
always @(sel or a or b or c or d) c 1
if (sel[2] == 1’b1)
sel sel[0] = ‘1’
out = a; //sel=1XX 0
else if (sel[1] == 1’b1) b 1
out = b; //sel=01X
sel[1] = ‘1’
else if (sel[0] == 1’b1) 0
out
out = c; //sel=001 a 1
else
sel[2] = ‘1’
out = d; //sel=000
Resource Sharing (1/2)
◼ Assign similar operations to a common netlist cell
◼ reduce hardware
a 00
b 01 out
c 10
d 11
The more inputs, the longer delay use one comparator and a suitable FSM
Always Block (1/3)
An always block can imply latches or flip-flops, or it can specify purely
combinational logic. An always block can contain logic triggered in
response to a change in a level (asynchronous triggers) or the rising or
falling edge of a signal (synchronous triggers).
The syntax of an always block is
always @ (event-expression )
begin
Completely specify sensitivity lists to avoid error
. . . statements . . .
end (combinational circuit)
Asynchronous triggers
x is recalculated as soon as always@ (a or b or c)
any input (a or b or c) has a begin
level transition (0 to 1 or 1 to 0). x=a | b | c;
end
Always Block (2/3)
Synchronous triggers
always@ (posedge c)
x is recalculated as soon as begin
c changes from 0 to 1 x=a +b;
end storage unit
Always Block (3/3)
module D_FF(Clk, D, Q);
module ALWAYS_BLOCK(IN , OUT);
input Clk, D;
input [3 : 0]IN; output OUT; reg OUT;
output Q;
Reg Q;
always @(IN)
begin
always @(posedge Clk)
OUT = (IN[0] | IN[1]) & (IN[2] | IN[3]);
begin
end
Q=D;
endmodule
end
endmodule
D Q
Clk
Three variables (register, integer and parameter) are declared globally at the
module level, or locally at the function level and begin-end block.
Verilog allows you to assign the value of a reg variable only within a function
or an always block.
Function
function [range] name_of_function;
function declaration
statement
endfunction
Function declaration:
Input declaration: specify the input signals for a function
Output: The output from a function is assigned to the function name.
Use concatenation operation to bundle several values for multi-outputs
Function Statements (1/4)
Procedure assignments are assignment statements used inside a function.
(It is similar to C language, Note: it cannot be used in module)
They are similar to the continuous assignments, except that the left side of
a procedural assignment can contain only reg and integer variables.
always @(A or B)
begin
C1 = A +B; // insert “0” in the first two bits
end
begin
C2 = A + B ; // discard the first two bits
end
Function Statements (3/4)
module test_n(a, b, x, y);
input a, b; output x, y; module test_n(a, b, x, y);
reg x, y; input a, b;
function Fn1; output x, y;
input a, b; assign x=a & b;
Fn1 = a & b; assign y=a | b;
/* begin-end is required endmodule
for more statements */
endfunction
module test_n(a, b, x, y);
function Fn2; input a, b;
input a, b; output x, y;
Fn2 = a | b; reg x, y;
endfunction always @(a or b)
always @(a or b) begin C language
begin x = a & b; x = a&b;
x = Fn1(a, b); y = a | b;
y = Fn2(a, b); end y = a | b;
end endmodule
endmodule
Function Statements (4/4)
module test_n(a1, a, b, x, y);
module test_n(a1, a, b, x, y);
input [7:0] a1;
input [7:0] a1;
input a, b;
input a, b;
output x, y;
output x, y;
reg x, y;
reg x, y;
function Fn1;
function Fn1;
parameter width=8;
input [width-1:0] a1;
input [width-1:0] a1; // OK
parameter width=8;//error message
input a, b;
input a, b;
Fn1 = a & b;
Fn1 = a & b;
endfunction
endfunction
always @(a or b)
always @(a or b)
begin
begin
x = Fn1(a1, a, b);
x = Fn1(a1, a, b);
end
end
endmodule
endmodule
Can we input width with scanf or input ??
Why?
Example-Function
function [4 : 0] Fn1;
input [3 : 0]F1 , F2;
module FUN_ALL(A , B , Y1 , Y2); begin
input [3 : 0] A,B;output [4 : 0] Y1, Y2; Fn1 = Fn2(F1 , F2) + 2;
reg [4 : 0] Y1, Y2; end
endfunction
function [4 : 0] Fn2;
input [3 : 0] F1 , F2; always @(A or B)
begin begin Y1=A+B+2
Fn2 = F1 + F2; Y1 = Fn1(A , B); Y2=A+B
end Y2 = Fn2(A , B);
endfunction end
endmodule
Example-Function with Many Outputs
function [9 : 0]Fn1;
input [3 : 0] F1 , F2;
reg [4 : 0] Y1_1; reg [4 : 0] Y2_1;
begin
Y1_1 = F1 + F2 + 5;
Y2 _1= F1 + F2 + 2;
Fn1 = {Y1_1 , Y2_1};
Y1=A+B+5
end
Y2=A+B+2
endfunction
assign {Y1 , Y2} = Fn1(A , B); Y1_1 and Y2_1 can be declared as
endmodule reg or integer, not wire