Verilog: Hardware Description Language
Verilog: Hardware Description Language
About Verilog
1
• VHDL
– Provides some high-level constructs
not available in Verilog (user defined
types, configurations, etc.).
• Verilog
– Provides comprehensive support for
low-level digital design.
– Not available in native VHDL
• Range of type definitions and
supporting functions (called
packages) needs to be included.
3
2
Basic Syntax of Module Definition
input/output declarations
Parallel statements
endmodule
3
Example 2 :: two-level circuit
module two_level (a, b, c, d, f);
input a, b, c, d;
output f;
wire t1, t2;
assign t1 = a & b;
assign t2 = ~ (c | d);
assign f = t1 ^ t2;
endmodule
4
Specifying Connectivity
• There are two alternate ways of
specifying connectivity:
– Positional association
• The connections are listed in the
same order
add A1 (c_out, sum, a, b, c_in);
– Explicit association
• May be listed in any order
add A1 (.in1(a), .in2(b), .cin(c_in),
.sum(sum), .cout(c_out));
10
5
Net data type
– Different ‘net’ types supported for
synthesis:
• wire, wor, wand, tri, supply0, supply1
– ‘wire’ and ‘tri’ are equivalent; when
there are multiple drivers driving them,
the outputs of the drivers are shorted
together.
– ‘wor’ / ‘wand’ inserts an OR / AND gate
at the connection.
– ‘supply0’ / ‘supply1’ model power
supply connections.
11
assign f = A & B;
assign f = C | D;
endmodule
12
6
module using_wired_and (A, B, C, D, f);
input A, B, C, D;
output f;
wand f; // net f declared as ‘wand’
assign f = A & B;
assign f = C | D;
endmodule
13
14
7
Register data type
– Different ‘register’ types supported for
synthesis:
• reg, integer
– The ‘reg’ declaration explicitly specifies the
size.
reg x, y; // single-bit register variables
reg [15:0] bus; // 16-bit bus, bus[15] MSB
– For ‘integer’, it takes the default size,
usually 32-bits.
• Synthesizer tries to determine the size.
15
Other differences:
– In arithmetic expressions,
• An ‘integer’ is treated as a 2’s
complement signed integer.
• A ‘reg’ is treated as an unsigned
quantity.
– General rule of thumb
• ‘reg’ used to model actual hardware
registers such as counters,
accumulator, etc.
• ‘integer’ used for situations like loop
counting.
16
8
module simple_counter (clk, rst, count);
input clk, rst;
output count;
reg [31:0] count;
• Example:
wire [1:10] A, B;
integer C;
C = A + B;
18
9
Specifying Constant Values
• A value may be specified in either the
‘sized’ or the ‘un-sized’ form.
– Syntax for ‘sized’ form:
<size>’<base><number>
• Examples:
8’b01110011 // 8-bit binary number
12’hA2D // 1010 0010 1101 in binary
12’hCx5 // 1100 xxxx 0101 in binary
25 // signed number, 32 bits
1’b0 // logic 0
1’b1 // logic 1
19
Parameters
• A parameter is a constant with a name.
• No size is allowed to be specified for a
parameter.
– The size gets decided from the constant
itself (32-bits if nothing is specified).
• Examples:
parameter HI = 25, LO = 5;
parameter up = 2b’00, down = 2b’01,
steady = 2b’10;
20
10
Logic Values
• The common values used in modeling
hardware are:
0 :: Logic-0 or FALSE
1 :: Logic-1 or TRUE
x :: Unknown (or don’t care)
z :: High impedance
• Initialization:
– All unconnected nets set to ‘z’
– All register variables set to ‘x’
21
22
11
Primitive Gates
• Primitive logic gates (instantiations):
and G (out, in1, in2);
nand G (out, in1, in2);
or G (out, in1, in2);
nor G (out, in1, in2);
xor G (out, in1, in2);
xnor G (out, in1, in2);
not G (out1, in);
buf G (out1, in);
23
24
12
Some Points to Note
• For all primitive gates,
– The output port must be connected to a
net (a wire).
– The input ports may be connected to
nets or register type variables.
– They can have a single output but any
number of inputs.
– An optional delay may be specified.
• Logic synthesis tools ignore time
delays.
25
`timescale 1 ns / 1ns
module exclusive_or (f, a, b);
input a, b;
output f;
wire t1, t2, t3;
nand #5 m1 (t1, a, b);
and #5 m2 (t2, a, t1);
and #5 m3 (t3, t1, b);
or #5 m4 (f, t2, t3);
endmodule
26
13
Hardware Modeling Issues
• The values computed can be held in
– A ‘wire’
– A ‘flip-flop’ (edge-triggered storage cell)
– A ‘latch’ (level-sensitive storage cell)
• A variable in Verilog can be of
– ‘net data type
• Maps to a ‘wire’ during synthesis
– ‘register’ data type
• Maps either to a ‘wire’ or to a ‘storage
cell’ depending on the context under
which a value is assigned.
27
28
14
module a_problem_case (A, B, C, f1, f2);
input A, B, C;
output f1, f2;
wire A, B, C;
reg f1, f2;
always @(A or B or C)
begin
f2 = f1 ^ f2;
f1 = ~(A & B); The synthesis system
end will not generate a
endmodule storage cell for f1
29
15
Verilog Operators
• Arithmetic operators
*, /, +, -, %
• Logical operators
! logical negation
&& logical AND
|| logical OR
• Relational operators
>, <, >=, <=, ==, !=
• Bitwise operators
~, &, |, ^, ~^
31
32
16
module operator_example (x, y, f1, f2);
input x, y;
output f1, f2;
wire [9:0] x, y; wire [4:0] f1; wire f2;
33
34
17
Some Points
• The presence of a ‘z’ or ‘x’ in a reg or wire
being used in an arithmetic expression
results in the whole expression being
unknown (‘x’).
• The logical operators (!, &&, | |) all
evaluate to a 1-bit result (0, 1 or x).
• The relational operators (>, <, <=, >=, ~=,
==) also evaluate to a 1-bit result (0 or 1).
• Boolean false is equivalent to 1’b0
Boolean true is equivalent to 1’b1.
35
36
18
Description Styles in Verilog
37
38
19
• A Verilog module can contain any number
of continuous assignment statements.
• For an “assign” statement,
– The expression on RHS may contain
both “register” or “net” type variables.
– The LHS must be of “net” type, typically
a “wire”.
• Several examples of “assign” illustrated
already.
39
Non-constant index in
expression on RHS
generates a MUX
40
20
module generate_decoder (out, in, select);
input in;
input [0:1] select;
output [0:3] out;
Non-constant index in
expression on LHS
generates a decoder
41
assign f = sel ? a : b;
endmodule
Conditional operator
generates a MUX
42
21
module level_sensitive_latch (D, Q, En);
input D, En;
output Q;
assign Q = en ? D : Q;
endmodule
43
22
• A module can contain any number of
“always” blocks, all of which execute
concurrently.
• Basic syntax of “always” block:
always @ (event_expression)
begin
statement;
Sequential
statement; statements
end
23
Sequential Statements in Verilog
1. begin
sequential_statements begin...end
end not required
2. if (expression) if there
is only 1 stmt.
sequential_statement
[else
sequential_statement]
3. case (expression)
expr: sequential_statement
…….
default: sequential_statement
endcase
47
4. forever
sequential_statement
5. repeat (expression)
sequential_statement
6. while (expression)
sequential_statement
7. for (expr1; expr2; expr3)
sequential_statement
48
24
8. # (time_value)
• Makes a block suspend for
“time_value” time units.
9. @ (event_expression)
• Makes a block suspend until
event_expression triggers.
49
50
25
// A sequential logic example
always @ (curr_state)
case (curr_state)
0, 1 : flag = 2; The variable ‘flag’ is not
3 : flag = 0; assigned a value in all the
endcase branches of case.
endmodule Latch is inferred
52
26
// A small change made
module incomp_state_spec (curr_state, flag);
input [0:1] curr_state;
output [0:1] flag;
reg [0:1] flag;
always @ (curr_state)
flag = 0;
case (curr_state)
0, 1 : flag = 2;
3 : flag = 0; ‘flag’ defined for all
endcase values of curr_state.
Latch is avoided
endmodule
53
27
module priority_encoder (in, code);
input [0:3] in;
output [0:1] code;
reg [0:1] code;
always @ (in)
case (1’b1)
input[0] : code = 2’b00;
input[1] : code = 2’b01;
input[2] : code = 2’d10;
input[3] : code = 2’b11;
endcase
endmodule
55
56
28
Blocking Assignment (using ‘=’)
• Most commonly used type.
• The target of assignment gets updated
before the next sequential statement in the
procedural block is executed.
• A statement using blocking assignment
blocks the execution of the statements
following it, until it gets completed.
• Recommended style for modeling
combinational logic.
57
29
Some Rules to be Followed
• Verilog synthesizer ignores the delays
specified in a procedural assignment
statement.
– May lead to functional mismatch
between the design model and the
synthesized netlist.
• A variable cannot appear as the target of
both a blocking and a non-blocking
assignment.
– Following is not permissible:
value = value + 1;
value <= init;
59
30
// Parameterized design:: an N-bit counter
31
// Using multiple edges of the same clock
32
A Ring Counter Example (Modified--1)
module ring_counter_modi1 (clk, init, count);
input clk, init; output [7:0] count;
reg [7:0] count;
always @ (posedge clk)
begin
if (init)
count = 8’b10000000;
else begin
count <= count << 1;
count[0] <= count[7];
end
end
endmodule
65
66
33
About “Loop” Statements
• Verilog supports four types of loops:
– ‘while’ loop
– ‘for’ loop
– ‘forever’ loop
– ‘repeat’ loop
• Many Verilog synthesizers supports only
‘for’ loop for synthesis:
– Loop bound must evaluate to a constant.
– Implemented by unrolling the ‘for’ loop, and
replicating the statements.
67
Modeling Memory
• Synthesis tools are usually not very efficient in
synthesizing memory.
– Best modeled as a component.
– Instantiated in a design.
• Implementing memory as a two-dimensional
register file is inefficient.
68
34
module memory_example (en, clk, adbus, dbus,
rw);
parameter N = 16;
input en, rw, clk;
input [N-1:0] adbus;
output [N-1:0] dbus;
…………
ROM Mem1 (clk, en, rw, adbus, dbus);
…………
endmodule
69
70
35
Modeling Finite State Machines
• Two types of FSMs
– Moore Machine
NS NS PS O/p
F/F
Logic Logic
– Mealy Machine
NS NS O/p
F/F PS Logic
Logic
71
RED
clk GREEN
YELLOW
72
36
module traffic_light (clk, light);
input clk;
output [0:2] light; reg [0:2] light;
parameter S0=0, S1=1, S2=2;
parameter RED=3’b100, GREEN=3’b010,
YELLOW=3’b001;
reg [0:1] state;
always @ (posedge clk)
case (state)
S0: begin // S0 means RED
light <= YELLOW;
state <= S1;
end
73
37
• Comment on the solution
– Five flip-flops are synthesized
• Two for ‘state’
• Three for ‘light’ (outputs are also
latched into flip-flops)
– If we want non-latched outputs, we have
to modify the Verilog code.
• Assignment to ‘light’ made in a
separate ‘always’ block.
• Use blocking assignment.
75
38
always @ (state)
case (state)
S0: light = RED;
S1: light = YELLOW;
S2: light = GREEN;
default: light = RED;
endcase
endmodule
77
x=1
x=0 x=0
EVEN ODD
x=1
78
39
module parity_gen (x, clk, z);
input x, clk;
output z; reg z;
reg even_odd; // The machine state
parameter EVEN=0, ODD=1;
ODD: begin
z <= x ? 0 : 1;
even_odd <= x ? EVEN : ODD;
end
endcase
endmodule
80
40
Mealy Machine: Example
• Sequence detector for the pattern ‘0110’.
x
z
clk
1/0
1/0 0/0
S0 0/0 S1 1/0 S2 1/0 S3
0/0
0/1
81
82
41
always @ (PS or x)
case (PS)
S0: begin
z = x ? 0 : 0;
NS = x ? S0 : S1;
end;
S1: begin
z = x ? 0 : 0;
NS = x ? S2 : S1;
end;
S2: begin
z = x ? 0 : 0;
NS = x ? S3 : S1;
end;
83
S3: begin
z = x ? 0 : 1;
NS = x ? S0 : S1;
end;
endcase
endmodule
84
42
Example with Multiple Modules
• A simple example showing multiple module
definitions.
B
A
en
Complementor
Bout
c_in
Adder
add_sub
carry sum
Parity Checker P
85
always @ (X or comp)
if (comp)
Y = ~X;
else
Y = X;
endmodule
86
43
module adder (sum, cy_out, in1, in2, cy_in);
input [7:0] in1, in2;
input cy_in;
output [7:0] sum; reg [7:0] sum;
output cy_out; reg cy_out;
87
always @ (in_word)
out_par = ^ (in_word);
endmodule
88
44
// Top level module
module add_sub_parity (p, a, b, add_sub);
input [7:0] a, b;
input add_sub; // 0 for add, 1 for subtract
output p; // parity of the result
wire [7:0] Bout, sum; wire carry;
89
90
45
Typical Example
module memory_model ( …….. )
endmodule
91
46
An Example
module memory_model ( …….. )
reg [7:0] mem [0:1023];
initial
begin
$readmemh (“mem.dat”, mem);
end
endmodule
93
47
A Specific Example :: Single Port RAM
with Asynchronous Read-Write
48
Verilog Test Bench
97
Stimulus Module
Under Compare
Test logic
Test Bench
98
49
How to Write Testbench?
• Create a dummy template
– Declare inputs to the module-under-test (MUT)
as “reg”, and the outputs as “wire”
– Instantiate the MUT.
• Initialization
– Assign some known values to the MUT inputs.
• Clock generation logic
– Various ways to do so.
• May include several simulator directives
– Like $display, $monitor, $dumpfile,
$dumpvars, $finish.
99
• $display
– Prints text or variables to stdout.
– Syntax same as “printf”.
• $monitor
– Similar to $display, but prints the value whenever
the value of some variable in the given list
changes.
• $finish
– Terminates the simulation process.
• $dumpfile
– Specify the file that will be used for storing the
waveform.
• $dumpvars
– Starts dumping all the signals to the specified file.
100
50
Example Testbench
module shifter_toplevel;
reg clk, clear, shift;
wire [7:0] data;
101
102
51
initial
begin
$dumpfile (“shifter.vcd”);
$dumpvars;
end
initial
begin
$display (“\ttime, \tclk, \tclr, \tsft, \tdata);
$monitor (“%d, %d, %d, %d, %d”, $time,
clk, reset, clear, shift, data);
end
initial
#400 $finish;
***** REMAINING CODE HERE ******
endmodule
103
A Complete Example
module testbench;
wire w1, w2, w3;
xyz m1 (w1, w2, w3);
test_xyz m2 (w1, w2, w3);
endmodule
104
52
module test_xyz (f, A, B);
input f;
output A, B;
reg A, B;
initial
begin
$monitor ($time, “A=%b”, “B=%b”, f=%b”,
A, B, f);
#10 A = 0; B = 0;
#10 A = 1; B = 0;
#10 A = 1; B = 1;
#10 $finish;
end
endmodule
105
53