Chapter 03 Basic of Verilog
Chapter 03 Basic of Verilog
5
Verilog HDL Basics
Popularity of Verilog
Behavioral
RTL
Gate
• Concurrency
• Structure
• /* Multiple line
comment */
<size>’<radix> <value>
– 8’h ax = 1010xxxx
– 12’o 3zx7 = 011zzzxxx111
– 12’o 07_24
• Bit extension
– MS bit = 0, x or z ⇒ extend this
• 4’b x1 = 4’b xx_x1
– MS bit = 1 ⇒ zero extension
• 4’b 1x = 4’b 00_1x
• If size is ommitted it
– is inferred from the value or
– takes the simulation specific number of bits or
– takes the machine specific number of bits
wand Y; // declaration
assign Y = A;
A assign Y = B;
Y
B
wor Y; // declaration
assign Y = A;
assign Y = B;
dr
tri Y; // declaration
A Y
assign Y = (dr) ? A : z;
• Declaration
integer i, k;
real r;
• No multi-dimentional arrays
reg var[1:10] [1:100]; // WRONG!!
• Escaped chars:
– \n newline
– \t tab
– %% %
– \\ \
– \“ “
• a = 4’b1010;
b = 4’b1100;
c = a ^ b;
• a = 4’b1010;
b = 2’b11;
a = 4’b1010;
...
d = a >> 2; // d = 0010
c = a << 1; // c = 0100
A
1
Y
Y = (sel)? A : B;
B 0
sel
• Negative integers:
– can be assigned negative values
– different treatment depending on base specification or not
reg [15:0] regA;
integer intA;
..
intA = -12/3; // evaluates to -4 (no base spec)
intA = -’d12/3; // evaluates to 1431655761 (base spec)
Use parentheses to
enforce your
priority
Top Level
E.g.
Module
Full Adder
Sub-Module Sub-Module
1 2
f .. // declarations
inN outM .. // description of f (maybe
.. // sequential)
endmodule
assign S = A ^ B;
A S
Half assign C = A & B;
B Adder C
endmodule
cin
module full_adder(sum, cout, in1, in2, cin);
output sum, cout;
input in1, in2, cin;
endmodule
60 Verilog HDL Basics
Hierarchical Names
ha2.A
cin
module
• Inputs reg or net net
module
module
net net
• Inouts
• Usage:
nand (out, in1, in2); 2-input NAND without delay
and #2 (out, in1, in2, in3); 3-input AND with 2 t.u. delay
not #1 N1(out, in); NOT with 1 t.u. delay and instance name
xor X1(out, in1, in2); 2-input XOR with instance name
initial
$display(“I’m first”); Will be displayed
at sim time 0
initial begin
#50;
$display(“Really?”); Will be displayed
end at sim time 50
endmodule
• wait (expr)
always begin
wait (ctrl) execution loops every
#10 cnt = cnt + 1; time ctrl = 1 (level
#10 cnt2 = cnt2 + 2; sensitive timing control)
end
d
initial begin
#5 c = 1; c
#5 b = 0;
#5 d = c; b
end
0 5 10 15
Time
Each assignment is
blocked by its previous one
d
initial begin
fork c
#5 c = 1;
#5 b = 0; b
#5 d = c;
join 0 5 10 15
end Time
Assignments are
not blocked here
reg out;
else if (expr2) wire [3:0] in;
wire [1:0] sel;
true_stmt2;
.. always @(in or sel)
if (sel == 0)
else out = in[0];
def_stmt; else if (sel == 1)
out = in[1];
else if (sel == 2)
out = in[2];
else
out = in[3];
endmodule
80 Verilog HDL Basics
Procedural Statements: case
E.g. 4-to-1 mux:
module mux4_1(out, in, sel);
case (expr) output out;
input [3:0] in;
input [1:0] sel;
item_1, .., item_n: stmt1;
reg out;
item_n+1, .., item_m: stmt2; wire [3:0] in;
.. wire [1:0] sel;
reg [3:0] Y;
wire start;
integer i;
initial
Y = 0;
reg [3:0] Y;
wire start;
integer i;
while (expr) stmt;
initial
Y = 0;
E.g.
module count(Y, start);
output [3:0] Y;
input start;
initial
Can be either an Y = 0;
integer or a variable
always @(posedge start)
repeat (4) #10 Y = Y + 1;
endmodule
Typical example:
clock generation in test modules
module test;
endmodule
reg Y;
wire c, clk, res;
res wire n;
c n Y not(n, c); // gate-level
clk
always @(res or posedge clk)
if (res)
Y = 0;
else
Y = n;
endmodule
86 Verilog HDL Basics
Outline
• Verilog?
• Module
• Instances
• User Identifiers
• Numbers in Verilog
• Signal and Nets in Verilog
• Registers
• Vectors and Arrays
• Logical vs Bitwise Operators
• Conditional Operator
• Continuous Assignments
• Structural Model (Gate Level)
• Behavioral Model – Procedures: Initial vs Always
• Procedural Statements: If – case – for
• System Tasks
87 Verilog HDL Basics
System Tasks
Always written inside procedures
• $display(“..”, arg2, arg3, ..); → much like printf(), displays formatted string
in std output when encountered
• $monitor(“..”, arg2, arg3, ..); → like $display(), but .. displays string each
time any of arg2, arg3, .. Changes
• $stop; → suspends sim when encountered
• $finish; → finishes sim when encountered
• $fopen(“filename”); → returns file descriptor (integer); then, you can use
$fdisplay(fd, “..”, arg2, arg3, ..); or $fmonitor(fd, “..”, arg2, arg3, ..); to write
to file
• $fclose(fd); → closes file
• $random(seed); → returns random integer; give her an integer as a seed
50ns
endmodule endmodule
91 Verilog HDL Basics
Parameters
(ii)
module top(out, in, clk);
output [1:0] out;
A. Implelementation input [3:0] in;
input clk;
without parameters (cont.)
wire [1:0] out;
wire [3:0] in;
wire clk;
endmodule
92 Verilog HDL Basics
Parameters
(iii)
module top(out, in, clk);
B. Implelementation output [1:0] out;
with parameters input [3:0] in;
input clk;
wire [1:0] out;
module dff(Q, D, clk); wire [3:0] in;
wire clk;
parameter WIDTH = 4;
output [WIDTH-1:0] Q; wire [3:0] p_in;
input [WIDTH-1:0] D; wire wu, wd;
input clk;
assign wu = p_in[3] & p_in[2];
reg [WIDTH-1:0] Q; assign wd = p_in[1] & p_in[0];
wire [WIDTH-1:0] D;
wire clk; dff instA(p_in, in, clk);
// WIDTH = 4, from declaration
always @(posedge clk) dff instB(out, {wu, wd}, clk);
Q = D; defparam instB.WIDTH = 2;
// We changed WIDTH for instB only
endmodule
endmodule
93 Verilog HDL Basics
Outline
• Verilog?
• Module
• Instances
• User Identifiers
• Numbers in Verilog
• Signal and Nets in Verilog
• Registers
• Vectors and Arrays
• Logical vs Bitwise Operators
• Conditional Operator
• Continuous Assignments
• Structural Model (Gate Level)
• Behavioral Model – Procedures: Initial vs Always
• Procedural Statements: If – case – for
• System Tasks
94 Verilog HDL Basics
• EXAMPLES
Encoder - Using if-else Statement
• http://www.asic-
world.com/examples/verilog/encoder.html#Encod
er_-_Using_if-else_Statement
• http://www.asic-
world.com/examples/verilog/encoder.html#Encod
er_-_Using_if-else_Statement
• http://www.asic-
world.com/examples/verilog/pri_encoder.html#Pri
ority_Encoders
• http://www.asic-
world.com/examples/verilog/pri_encoder.html#Pri
ority_Encoders
• http://www.asic-
world.com/examples/verilog/decoder.html#Decod
er_-_Using_case_Statement
• http://www.asic-
world.com/examples/verilog/decoder.html#Decod
er_-_Using_case_Statement
Input output
endmodule