Professional Documents
Culture Documents
Verilog Lecture Notes
Verilog Lecture Notes
CS G553
Introduction to Verilog
Hardware Description Languages (HDL)
• The 'capture language'—often beginning with a high-level algorithmic
description.
• Resemble concurrent programming languages.
• HDL design generally ends at the synthesis stage
• HDLs, 'compiler' refers to synthesis, a process of transforming the HDL
code listing into a physically realizable gate netlist
• Simulation by a test bench. Even driven.
• Two widely used hardware description languages
— VHDL
— Verilog
• HDL languages can describe circuits from two perspectives
— function
— structure
• Verilog introduced by Gateway Design automation in 1985.
Levels of Abstraction
• Switch Level: Module implemented with switches and
interconnects. Lowest level of Abstraction
• Gate Level: Module implemented in terms of logic gates
and interconnection between gates
• Dataflow Level: Module designed by specifying dataflow.
Designer is aware of how data flows between hardware
registers and how the data is processed in the design
• Behavioral Level :Module can be implemented in terms of
the desired design algorithm without concern for the
hardware implementation details. Very similar to C
programming.
Module
• Basic building block in
Verilog.
• module name (port_list)
• A module definition starts Interface
• port declarations
with the keyword • parameter declarations
module ends with the Optional add-ons
• include directives
keyword endmodule • variable declarations
Body • assignments
• low-level module instantiation
• initial and always blocks
• task and function
• End module
Ex: Module
module toggle(q, clk, reset);
… reset toggle
q
<functionality of module>
…
endmodule clk
<size>’<base format><number>
specifies the
number of bits
in the number d or D for decimal Number
Examples: h or H for hexadecimal depends on
• 4’b1111 b or B for binary the base
• 12’habc o or O for octal
• 16’d235
• 12’h13x X or x: don’t care
• -6’d3 Z or z: high impedence
• 12’b1111_0000_1010 _ : used for readability
Net
• Represents connections between
hardware elements.
• Continuously driven by output of
connected devices.
• Declared using the keyword
wire.
…… addr
Processor
wire r_w; // scalar signal
Memory
data
wire [7:0] data; // vector signal
wire [9:0] addr; // vector signal r_w
……
Port
Provide interface by
which a module can
communicate with
its environment
Verilog Type of
keyword port
inout Bi-
directional
Ex: Port
pc
module pc (clk, rst, status, i_o);
input clk, rst; clk
output [3:0] status;
addr[9:0]
inout [7:0] i_o;
rst
Processor
wire r_w;
Memory
wire [7:0] data; data[7:0]
wire [9:0] addr; status[3:0]
…… r_w
endmodule i_o[7:0]
Register
Registers represent data storage elements.
They retain value until another value is placed
onto them.
In Verilog, a register is merely a variable that can
hold a value.
They do not need a clock as hardware registers
do.
reg reset;
initial
begin
reset = 1’b1;
#100 reset=1’b0;
end
Register declaration
•real delta;
• initial
• begin
• delta = 4e10;
• delta = 2.13;
• end
• integer i;
• initial
• i = delta;
Arrays
Arrays are multiple elements that are 1-bit or n-bits wide.
Possible to have arrays of type reg, integer, real
Arrays of nets can also be used to connect ports of generated
instances
• integer count[0:7];
• reg [4:0] port_id[0:7]; // Array of 8 port_ids; each port_id is 5 bits wide
• integer matrix[4:0][0:255]; // Two dimensional array of integers
Memory
Used to model register files, RAMs and ROMs.
A memory component can be defined using reg variables
Modeled in Verilog as a one-dimensional array of registers.
myMem[0]
myMem[1]
myMem[3]
Parameters
Define constants .
Makes code easy to read and modify
Can’t be used as variables.
• parameter port_id=5;
• parameter bussize = 8;
• reg [bussize-1 : 0] databus1;
• reg [bussize-1 : 0] databus2;
Strings
module Top;
reg [3:0] A, B;
reg C_IN;//externally, inputs can be a reg or a wire; internally must be wires
wire [3:0] SUM; //externally must be wires
wire C_OUT;
// one way
fulladd4 FA1(SUM, C_OUT, A, B, CIN);
// another possible way
fulladd4 FA2(.c_out(C_OUT), .sum(SUM), .b(B), .c_in(C_IN), .a(A));
…
endmodule
inout net
output net
Module instantiation…
Signal assignment follows port list order
Module instantiation..
Signal assignment by port names
by name
Gate level modeling (structural)
.
wire Z, Z1, OUT, OUT1, OUT2, IN1, IN2;
and a1(OUT1, IN1, IN2);
nand na1(OUT2, IN1, IN2);
xor x1(OUT, OUT1, OUT2);
not (Z, OUT);
buf final (Z1, Z);
.
Predefined gate primitives
supports basic logic gates as predefined primitives.
instantiated like any other modules.
a
and (d, a, b, c) b d
c
buf (a, b) b a
a
not (a, b, c) c
b
Predefined gate primitives
e.g. c
bufif1 (a, b, c) b a
c
e.g.
notif0 (a, b, c) a
b
Example of structural Verilog code
Example of using predefined gate primitives
Array of gate instances
Defining UDPs
— Cannot be defined
within modules.
— Can be defined after
or before the module in
the same file.
— Can be defined
in a separate file
and use include
directive to include
to the code.
Dataflow modeling
• Arithmetic: *, /, +, -, % and **
• Logical: !, && and ||
• Relational: >, <, >= and <=
• Equality: ==, !=, === and !==
• Bitwise: ~, &, |, ^ and ^~
• Reduction: &, ~&, |, ~|, ^ and ^~
• Shift: <<, >>, >>> and <<<
• Concatenation: { }
• Replication: {{}}
• Conditional: ?:
Example
// OR THIS WAY
assign out = s1 ? (s0 ? i3:i2) : (s0 ? i1:i0);
endmodule
Example: A 2-to-4 decoder
Circuit schematic
reg [3:0] A;
1 1 0 1 A << 2 0 1 0 0
reg [3:0] A;
1 1 0 1 A >> 2 0 0 1 1
Concatenation operators
Example
Data 1 1 0 1 0 0 0 0
c c
A[3:0] B[7:6]
Continuous assignment
Continuous assignment starts with keyword
assign.
The left hand side of a continuous assignment
command must be a net-type signal
Example
a x
b AND
OR o
c
Example
Module delayTest;
integer a, b, c;
initial begin
a = 2; b = 3; Change a from 2 to 4
end after 3 time unit
initial #3 a = 4;
initial #5 c = a+b; Execution order:
endmodule 1. delay
2. evaluation
3. assignment
Result: c=7
Delay in procedural assignments
Module delayTest;
integer a, b, c;
initial begin
a = 2; b = 3; Change a from 2 to 4
end after 3 time unit
initial #3 a = 4;
initial c = #5 a+b; Execution order:
endmodule 1. evaluation
2. delay
3. assignment
Result: c=5
Behavioral or algorithmic modeling
• Design is expressed in algorithmic level, .
which frees designers from thinking in .
terms of logic gates or data flow. reg a, b, c;
• Designing at this model is very similar to
programming in C. initial a=1’b0;
• All algorithmic statements in Verilog can .
appear only inside two statements: .
always and initial. always
begin
• Each always and initial statement b = a ^ 1’b1;
represents a separate activity flow in c = a + b;
Verilog. end
• Activity flows in Verilog run in parallel. .
• You can have multiple initial and .
always statements but you can’t nest
them.
Behavioral blocks
reg x, y, m;
initial m=1’b0;
initial
begin
x=1’b0;
y=1’b1;
end
Example
integer count;
count=0;
always
begin
count=count+1;
end
Events-based timing control
• An event is the change in the value on a register or a net.
• Events can be utilized to trigger the execution of a statement of a
block of statements.
• The @ symbol is used to specify an event control.
• Statements can be executed on changes in signal value or at a
positive (posedge) or negative (negedge) transition of the signal.
count=0; count=0;
initial begin
a = #1 1; // assignment at time 1
b = #3 0; // assignment at time 4 (3+1)
c = #6 1; // assignment at time 10 (6+3+1)
end
initial begin
#1 a < = 1; // assignment at time 1
#3 b <= 0; // assignment at time 3
#6 c <= 1; // assignment at time 6
end
Parallel blocks
Parallel block
Event control statements
An event occurs when a net or register changes it value.
The event can be further specified as a rising edge (by posedge) or
falling edge (by negedge) of a signal.
An event control statement always starts with symbol @
Must contain a
number or a
signal value;
initial initial only evaluated
once at the
count = 0; count = 0; beginning
module testWait;
integer a, b, c;
reg en;
initial a = 0;
initial #3 a = 3;
intial #6 a = 7;
wait (a==7) b = 1; // assign 1 to b when a=7
wait (en) c = 2; // assign 2 to c when en is true
endmodule
Case statement
Functions Tasks
Simulation result
Example: clock display
minutes seconds
HEX3 HEX2 HEX1 HEX0
Task to display digits
end
end Alt-2
task digit2sev; Alt-1
…
endtask
endmodule
Resource utilization=Alt 1