03-Verilog Modules and Ports-Merged
03-Verilog Modules and Ports-Merged
Verilog® HDL
Modules and Ports
Outline
•Components of a Verilog Module
endmodule
Ports
•Interface of the module to the environment
• Internals of the module are not visible to the
environment
• Internals can be changed as long as the interface
(ports) is not changed
Port Declaration
• All ports in the list of ports must be declared in the
module
• Verilog keyword Type (mode) of port
• Example:
module fulladd4(sum, c_out, a, b, c_in);
output [3:0] sum;
output c_out;
input [3:0] a, b;
input c_in;
...
endmodule
Port Declaration
•Note: all ports are wire by default
• No need to declare it again as wire
• If expected to be reg, the port needs to be declared again
Example: the q port in DFF module
• Example:
// assuming module ripple_carry_counter(q, clk, reset);
• Unconnected ports
• Allowed in Verilog
• Example:
// module fulladd4(sum, c_out, a, b, c_in);
fulladd4 fa0(SUM, , A, B, C_IN); // output port c_out is
unconnected
• Example of illegal port connection Port Connection
Rules
module Top;
// Declare connection variables
reg [3:0] A, B;
reg C_IN;
reg [3:0] SUM;
wire C_OUT;
...
endmodule // Top
Connecting Ports to External Signals
• Two ways for port mapping
• Connecting by ordered list
• More intuitive for beginners
• Mostly used when having few ports
• Connecting by name
• Used when having more ports
• Gives independence from order of ports
• The order of ports in the port list of a module can be changed without
changing the instantiations
Connecting by Ordered List
module fulladd4(sum, c_out, a, b, c_in);
...
endmodule
module Top;
// Declare connection variables
reg [3:0] A, B;
reg C_IN;
wire [3:0] SUM;
wire C_OUT;
module Top;
// Declare connection variables
reg [3:0] A, B;
reg C_IN;
wire [3:0] SUM;
wire C_OUT;
<top-module-name>.<instance-name>.<identifier>
Hierarchical Names
Stimulus
m1
S
set q
Q
qbar
reset R Qbar
Primitive Gates in Verilog
Primitive Gates in Verilog
•Syntax
• Similar to module instantiations
•Examples
wire out, in1, in2, in3;
// Internal nets
wire c1, c2, c3;
endmodule
Gate Delays
• Rise Delay
• Fall Delay
Gate Delays
• Turn-off Delay:
//same as
wire out;
assign #10 out = in1 & in2;
//same as
Wire out;
assign #10 out = in1 & in2;
Expression, Operand, Operator
real a, b, c;
c = a - b; // real operands
reg ret_value;
ret_value = calculate_parity(A, B); // function type operand
Operator category Operators symbol
Arithmetic * / + - % **
Logical ! && ||
Relational > < <= >=
Equality == != === !===
Bitwise ~ & | ^ ^~ ~^
Reduction & ~& | ~| ^ ~^ ^~
Shift >> << >>> <<<
Reduction & ~& | ~| ^~ or ~^
Concatenation {}
Replication {{}}
Conditional ?:
Arithmetic Operators
A = 4'b0011; +5
-4
B = 4'b0100;
D = 6; E = 4; F=2; -10/5
-6’d10/5 // Do NOT use
A * B // =(2's complement of 10)/5
// =(232 - 10)/5
D / E
A + B • 4-valued logic issue:
B - A x and z values
F = E ** F; If any operand bit has a value x, then
result of the entire expression is x
13 % 3
16 % 4 in1 = 4'b101x;
-7 % 2 /* takes the
in2 = 4'b1010;
sign of first operand*/
sum = in1 + in2;
//sum will be evaluated
7 % -2 to the value 4’bx
Logical Operators
Relational Operators
Equality Operators
// A = 4, B = 3
// X = 4'b1010, Y = 4'b1101
A == B // Z = 4'b1xxz, M = 4'b1xxz
X != Y // N = 4'b1xxx
X == Z
Z === M
Z === N
M !== N
Bitwise Operators
// X = 4'b1010
// Y = 4'b1101
// Z = 4'b10x1
~X
X & Y
X | Y
X ^ Y
X ^~ Y
X & Z
• // X = 4'b1010
&X
|X
^X
Shift Operators
// X = 4'b1100
Y = X >> 1;
Y = X << 1;
Y = X << 2;
integer a, b, c;
a = 0;
b = -10;
c = a + (b >>> 3);
Concatenation Operator
•Unsized operands not allowed
// A = 1'b1, B = 2'b00,
// C = 2'b10, D = 3'b110
Y = {B , C}
Y = {A , B , C , D , 3'b001}
Y = {A , B[0], C[1]}
Replication Operator
reg A;
reg [1:0] B, C;
reg [2:0] D;
A = 1'b1;
B = 2'b00;
C = 2'b10;
D = 3'b110;
Y = { 4{A} }
Y = { 4{A} , 2{B} }
Y = { 4{A} , 2{B} , C }
Conditional
Operator
endmodule
Gate Level vs. Dataflow Modeling
Dataflow Model
Dataflow Model
and (y0, i0, s1n, s0n);
and (y1, i1, s1n, s0); module mux4_to_1 (output out,
and (y2, i2, s1, s0n); input i0, i1, i2, i3, s1, s0);
and (y3, i3, s1, s0);
assign out = (~s1 & ~s0 & i0)|
or (out, y0, y1, y2, y3); (~s1 & s0 & i1)|
( s1 & ~s0 & i2)|
endmodule ( s1 & s0 & i3);
endmodule
Another alternative for Mux 4-to-1
• Use conditional operator
module mux4_to_1 (out, i0, i1, i2, i3, s1, s0);
//port declaration
Output out;
input i0, i1, i2, i3;
input s1, s0;
endmodule
4-bit Full-adder Example
Full-adder Example
module fulladd (sum, c_out, a, b, c_in);
input a, b, c_in;
endmodule
module fulladd (sum, c_out, a, b, c_in); 4-bit Full-adder
Example
// I/O port declarations
output c_out;
input [3:0] a, b,
input c_in;
endmodule
Example: Carry Look ahead
Adder
module fulladd4(sum, c_out, assign c1 = g0 | (p0 & c_in),
a, b, c_in); c2 = g1 | (p1 & g0) |
(p1 & p0 & c_in),
output [3:0] sum;
c3 = g2 | (p2 & g1) |
output c_out;
(p2 & p1 & g0) |
input [3:0] a,b;
(p2 & p1 & p0 & c_in),
input c_in;
c4 = g3 | (p3 & g2) |
wire p0,g0, p1,g1, p2,g2, (p3 & p2 & g1) |
p3,g3, c4, c3, c2, c1; (p3 & p2 & p1 & g0) |
(p3 & p2 & p1 & p0 & c_in);
assign p0 = a[0] ^ b[0],
p1 = a[1] ^ b[1], assign sum[0] = p0 ^ c_in,
p2 = a[2] ^ b[2], sum[1] = p1 ^ c1,
p3 = a[3] ^ b[3]; sum[2] = p2 ^ c2,
sum[3] = p3 ^ c3;
assign g0 = a[0] & b[0],
g1 = a[1] & b[1], assign c_out = c4;
g2 = a[2] & b[2],
endmodule
g3 = a[3] & b[3];
Example: 4-bit Ripple Carry
Counter
Dataflow Modeling of D Flip flop
• Negative Edge Triggered D-FF
Concepts
Constructs
Gate-level modeling
Netlist of gates
Dataflow modeling
Boolean function assigned to a net
always
initial
All behavioral statements can appear only inside these blocks
Cannot be nested
Verilog HDL 2005
Structured Procedures: initial statement
Starts at time 0
initial
begin
#5 a=1’b1;
#25 b=1’b0;
end
initial
begin
#10 x=1’b0;
#25 y=1’b1;
end 2005 Verilog HDL
Structured Procedures: always statement
Start at time 0
Example
module clock_gen;
reg clock;
initial
#1000 $finish;
endmodule
variables
<lvalue> can be
reg, integer, real, time
RHS wider than LHS => RHS is truncated (Least significant part is kept)
Verilog HDL 2005
Procedural Assignments
Blocking assignments
Non-blocking assignments
Example:
reg x, y, z;
reg [15:0] reg_a, reg_b;
integer count;
initial All executed at time 0
begin
x=0; y=1; z=1;
count=0;
reg_a= 16’b0; reg_b = reg_a;
#15 reg_a[2] = 1’b1; executed at time 15
#10 reg_b[15:13] = {x, y, z};
count = count + 1;
end All executed at time 25
Race condition
When the final result of simulating two (or more) concurrent
processes depends on their order of execution
Example:
always @(posedge clock)
b = a;
always @(posedge clock)
a = b;
always @(posedge clock)
Solution: begin
always @(posedge clock) temp_b = b;
temp_a = a;
b <= a; b = temp_a;
always @(posedge clock) a = temp_b;
end
a <= b;
Verilog HDL 2005
Procedural Assignments
Recommendation
Disadvantage:
mintypmax expression
assignments
1. Regular delay control
Non-zero delay is specified to the left of procedural assignment
2. Intra-assignment delay control
Non-zero delay is specified to the right of assignment operator
3. Zero delay control
Procedural statements in different always –initial blocks may be
or a block of statements
Four types
Event OR control
Syntax:
if (<expression>) true_statement;
if (<expression>) true_statement;
else false_statement;
if (<expression>) true_statement1;
else if (<expression>) true_statement2;
else if (<expression>) true_statement3;
else default_statement;
True is 1 or non-zero
False is 0 or ambiguous (x or z)
Examples:
if (!lock) buffer = data;
if (alu_control==0)
y = x+z;
else if (alu_control==1)
y = x-z;
else if (alu_control==2)
y = x*z;
else
$display(“Invalid ALU control signal.”);
Verilog HDL 2005
Behavioral Modeling Statements:
Multiway Branching
Similar to switch-case statement in C
Syntax:
case (<expression>)
alternative1: statement1;
alternative2: statement2;
...
default: default_statement; // optional
endcase
Notes:
<expression> is compared to the alternatives in the order
specified.
Default statement is optional
always @(*)
case ({s1,s0}) // {s0,s1} = s0s1= 00, 01, 10, 11
2’d0: out = i0;
2’d1: out = i1;
2’d2: out = i2;
2’d3: out = i3;
default: out = 1’bz;
endcase
endmodule
endcase
endmodule
Loops in Verilog
while, for, repeat, forever
Similar to C
Syntax:
for( init_expr; cond_expr; change_expr)
statement;
Example:
integer count;
$display(“count=%d”,count);
Verilog HDL 2005
The repeat loop Loops
Syntax:
repeat( number_of_iterations )
statement;
The number is evaluated only when the loop is first
encoutered
Example:
integer count=0;
initial
begin
repeat(127)
begin
$display(“count=%d”, count);
count=count+1;
end
Verilog HDL
end 2005
The forever loop Loops
Syntax:
forever
statement;
Equivalent to while(1)
Sequential block
Parallel block
Sequential Block
Keywords begin and end are used to group statements into sequential
blocks
Parallel Block
parallel blocks
Nested Blocks
Named Blocks
can be stopped
Example: Named Blocks
Disabling Named Blocks
referenced hierarchically.
Methods to create generate
statements:
Generate loop
Generate conditional
Generate case
Generate Loop
A generate loop permits following to be instantiated
Variable Declarations
Modulus
Continuous assignments
synthesis.
parameter N = 8;
input [N-1:0] a, b;
input carry_in;
reg out;
always @(*)
case ({s1,s0})
2’b00: out = i0;
2’b01: out = i1;
2’b10: out = i2;
2’b11: out = i3;
default: out = 1’bx;
endcase
endmodule
Compare with other modeling styles
module counter (Q, clk, clear);
output reg [3:0] count;
4-bit counter
input clk, clear
reg[3:0] Q;
Reusing code
programming languages
Verilog equivalence:
Functions
Tasks
<input declaration(s)>
<variable_declaration(s)>
begin // if more than one statement needed
<statements>
endfunction
Verilog HDL 2005
Functions
Invocation syntax:
<func_name> (<argument(s)>);
always @(addr)
begin
left_addr =shift(addr, `LEFT_SHIFT);
right_addr =shift(addr,`RIGHT_SHIFT);
end
no input arguments
Declaration syntax
task <task_name>;
<I/O declarations>
<statement(s)>
end // if begin used!
endtask
Verilog HDL 2005
Tasks
Task declaration and invocation
<task_name> (<arguments>);
initial endtask
begin
… task asymmetric_sequence;
end
begin
#3 clock = 1'b0;
always
asymmetric_sequence; #10 clock = 1'b1;
end
endtask
Verilog HDL 2005
DIFFERENCES BETWEEN
TASKS AND FUNCTIONS
Differences between...
Tasks
Functions
Can enable other tasks and
Can enable (call) just
functions
another function (not task)
May execute in non-zero
Execute in 0 simulation
simulation time
time
May contain any timing
No timing control
control statements
statements allowed
May have arbitrary input,
At lease one input
output, or inout
Return only a single value
Verilog HDL Do not return any value 2005
Differences between…
Both
are defined in a module
can have local variables (registers, but not nets) and events
is purely combinational