Verilog New
Verilog New
What is Verilog
Verilog is a hardware description language (HDL) that describes the
functionality of hardware design
❖Digital and Analog design language
❖Latest Verilog standard: IEEE Standard 1364-2005
❖Verilog is also used in Analog & Mixed Signal Language
Application area of Verilog
Modelling Styles
❖ Data flow model
❖ Structural model
❖ Behavioral model
Continued…
• Dataflow level – The module implementation depends on data flow
specification i.e. how data flows and processes in the design circuit.
• Continuous assignment
a
• Data Flow model f
b AND
module and_gate (a, b, f);
input a, b;
output f;
assign f = a & b;
endmodule
Continued…
• Structural model -Pure predefined gates
• Pure predefined gates
a
• Structural model f
b AND
module and_gate (a, b, f);
input a, b;
output f;
and a1 (f, a, b);
endmodule
Continued..
• Behavioral model- A behavioral description describes a systems
behavior (or) function in an algorithmic fashion
• Procedural assignment (always, initial) • Behavioral model
• Blocking ( = ) module and_gate (a, b, f);
input a, b;
• Non-blocking ( <= ) output reg f;
always @ (a, b) begin
if (a == 1’b1 && b == 1’b1)
a f = 1’b1;
f else
b AND
f = 1’b0;
end
endmodule
Design Styles
• A system can be designed by either bottom up or top down approach
• Bottom up approach :
• Design happens at the lowest of modules and by adding lower modules,
complex modules are made.
• Not preferred for huge systems.
• Top down approach
• It is the preferred method for designing a huge design.
Verilog Module Declaration :
module<module_name>(<port_list>);
In Verilog, the basic unit of hardware is called ...
a module. <implementation>
...
• A module cannot contain definition of endmodule
other modules.
• A module can, however, be instantiated
within another module. // This is illegal to write
module dut_1;
• Instantiation allows the creation of a ...
hierarchy in Verilog description. module dut_2;
...
endmodule
endmodule
input MODULE output
Verilog Module
Example1 :
module and (f, x, y); a
input x, y; f
AND
b
output f;
assign f = x & y;
endmodule
Example2:
Module half_adder(input a,b, output sum,carry);
assign sum =a^b;
assign carry=a&b;
endmodule
Verilog Syntax
• Keywords in lowercase.
• Identifiers
Case sensitive
Alphanumeric, underscore and $ characters can be used
• Comments
Single line – Represented with //
Multiple line – Represented with /* */
my_identifier
my_identifier2
3my_identifier
$my_identifier
my_identifier$
DUT & TB Connections
• Test bench must know hardware but don’t need to be hardware
A test bench is a
completely closed
system i.e. no
inputs go in or
outputs go out
hence TB has no
ports
D q1 q2 out
module shift_register (input d, clk, output out ); module shift_register (input d, clk, output out );
reg q1, q2; reg q1, q2;
d_flipflop D_FF1(q1, d, clk); d_flipflop D_FF1 (.q(q1), .d(d), .clock(clk));
d_flipflop D_FF2(q2, q1, clk); d_flipflop D_FF2 (.d(q1), .q(q2), .clock(clk));
d_flipflop D_FF3(out, q2, clk); d_flipflop D_FF3 (.clock(clk), .q(out), .d(q2));;
endmodule endmodule
Test bench for Half_adder
module half_adder_tb;
reg a, b;
wire s, c_out;
half_adder ha(a, b, s, c_out);
initial begin
$monitor("At time %0t: a=%b b=%b, sum=%b,carry=%b",$time, a,b,s,c_out);
a = 0; b = 0;
#1;
a = 0; b = 1;
#1;
a = 1; b = 0;
#1;
a = 1; b = 1;
end
endmodule
Data Types
A storage format having a specific range or type is called data type.
They can be divided into two groups.
1.Net type group: The net-type group represents physical connections
between digital circuits. Ex. wire, wand, wor, etc.
2.Variable type group: The variable type group represents the storage of
values in digital circuits. They are used as variables. Ex. reg, integer
Data types and Default values
Data values:evel
Represents
• 0 - Logic 0 state
• 1 - Logic 1 state
• x - Unknown logic state
• z - High impedance state
Continued…
• Wire- wire is continuously driven by combinational logic.
endmodule
Continued…
module test (input i1,i2, output out);
• integer : Used for loop counting
integer i;
(typical use)
real b = 6.4;
• real : Used to store floating-point time c;
numbers initial
• time : Keeps track of simulation begin
time (not used in synthesis) for(i = 0; i < 54; i++)
• $time - returns integer value begin
(6) out = i; // out = i[0];
• $realtime – returns floating c = $time;
value(6.4) $display(“output=%b, time=%0t”, out, c);
end
end
%b – binary
%d – decimal endmodule
%h – hexadecimal 0 0 1 . . . 0 1 0
%s – string MSB LSB
%t – time 31st 1st 0th
%f - floating bit bit bit
Continued…
String-An ordered collection of characters is called a string. Each
character in a string is stored as 8 bit ASCII values. They are stored
as reg type variable.
module str_mod (input a, ouput b);
reg [8*20:1] string_var;
// reg [160:1] string_var;
// reg [160 - 1:0] string_var;
initial
begin
string_var = “hello world!”;
$display(“string value = %0s”, string_var);
end
endmodule
Arrays
• Two types of arrays – one dimensional & two dimensional
• One dimensional array Syntax: a[3] a[2] a[1] a[0]
• wire/reg [3:0] a = 4’b1111; // register width = 4 1 1 1 1
• wire/reg a [3:0]; // register depth = 4
• a[3] = 1’b1;
1 a[3]
• a[2] = 1’b1;
• a[1] = 1’b1; 1 a[2]
Type of value representation Value assigned
• a[0] = 1’b1; 1 a[1] of value 15
Binary 4 bit 4’b1111
1 a[0]
Decimal 4 bit 15
• ram[0] = 8’hff; ram[7] ram[6] ram[5] ram[4] ram[3] ram[2] ram[1] ram[0]
• ram[1][5] = 1’b0; ram[0] ram[0] ram[0][6] ram[0][5] ram[0][4] ram[0][3] ram[0][2] ram[0][1] ram[0][0]
[7]
ram[1] ram[1] ram[1][6] ram[1][5] ram[1][4] ram[1][3] ram[1][2] ram[1][1] ram[1][0]
[7]
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
Example:
module design_ip #(parameter WIDTH = 8,parameter DEPTH = 128) ( input [BUS_WIDTH-1 : 0 ]
addr,…);
reg [(WIDTH-1):0] mem [(DEPTH-1):0]; // declaring the 128X8 memory using parameters.
………………….
……………….
endmodule
module tb ();
…..
design_ip DUT (…);
defparam DUT.WIDTH = 64;
…..
endmodule
Operators
• Logical operators
• Bitwise operators
• Reduction operators
• Arithmetic operators
• Relational operators
• Shift operators
• Equality operators
• Concatenation operator
• Conditional operator
• Replication operator
• Arithmetic Operators: • Logical Operators:
• + unary (sign) plus • ! logical negation
• – unary (sign) minus • && logical AND
• + binary plus (add) • || logical OR
• – binary minus (subtract)
• * multiply
• / divide
• % modulus
• ** exponentiation • The value 0 is treated as logical FALSE while any non-
zero value is treated as TRUE.
• Logical operators return either 0 (FALSE) or 1 (TRUE).
• Relational Operators: • Bitwise Operators:
• != not equal Examples: • ~ bitwise NOT Examples:
• (a != b) • wire a, b, c, d, f1, f2, f3, f4;
• == equal • & bitwise AND • assign f1 = ~a | b;
• ((a + b) == (c – d))
• >= greater or equal • ((a > b) && (c < d))
• | bitwise OR • assign f2 = (a & b) | (b & c) | (c & a)
• <= less or equal • (count <= 0) • ^ bitwise exclusive-OR • assign f3 = a ^ b ^ c;
• assign f4 = (a & ~b) | (b & c & ~d);
• > greater • ~^ bitwise exclusive-NOR
• < less
• Relational operators operate on numbers, and • Bitwise operators operate on bits, and return a value that
return a Boolean value (true or false). is also a bit.
Reg [3:0]a,b;
Reg y1,y2,y3,y4,y5,y6,y7;
Initial begin
a = 4'b0010;
b = 4'b0011;
y1 = (a == b); //logical equality will return 0
y2 = (a != b); // logical inequality will return 1
#10; a = 4'b101x;
b = 4'b1010;
y3 = (a === b); // case equality will return 0
y4 = (a !== b); // case inequality will return 1
y5 = (a == b); // logical equality will return x
y6 = (a != b); //logical inequality will return x
#10;
a = 4'b101x;
b = 4'b101x;
y7 = (a === b); //case equality will return 1
end
Operator precedence
• Operators on same line have the same precedence.
• All operators associate left to right in an + – ! ~ (unary)
expression, except ?:
**
• Parentheses can be used to change the precedence */%
• 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.
?:
Verilog Processes
Processes
Continuous Procedural
Specifies the event which flags off the execution of the block
(only for always block
Type of block @(sensitivity list)
The block can be assigned a name which can be
begin: name_of_block
referred(optional )
local variable declarations;
procedural assignment statements; All variables ect., local to the block are declared at the
end beginning of the block
initial begin
data=y;
Output:
$display(“2nd block: data=%0h and y=%0h”,data,y);
1st block: data=5 and y=5
end
2nd block: data =5 and y=5
endmodule
Non-blocking assignments
• Nonblocking assignments are executed in parallel. Since the execution of next statement is not
blocked due to execution of current statement. Represented with the sign “<=“.
• Nonblocking assignments in a block, the right hand sides are evaluated first. Subsequently the
specified assignments are scheduled.
• Nonblocking assignments are illegal to use in a continuous assignment statement or in a net
declaration
Example :
module blocking;
reg [3:0] data =4’h5;
reg[3:0] y =4’h3;
initial
begin
y<=data;
$display(“1st block: data=%0h and y=%0h”,data,y);
end
initial begin
data<=y; Output:
$display(“2nd block: data=%0h and y=%0h”,data,y); 1st block: data=3 and y=5
end 2nd block: data =3 and y=5
endmodule
Stratified event queue/event scheduler/time
regions
• Every change in value of a net or variable in the circuit being simulated is considered an event. Used in
the procedural blocks (always, initial).
• Events/regions can occur at different times. They are as follows
• Active
• Statements from the previous time slot
• Evaluation of
• Blocking assignments
• RHS of non blocking assignments
• Continuous assignment
• $display & $write
• Inactive
• #0 delay statements
• Non blocking/NBA
• Non-blocking statement LHS update
• Postponed
• $strobe & $monitor
• Statements to next time slot
• Example
always @ (enable, a, b)
begin
a = 0;
a <= 1;
$display(“display a = %d”, a); // output: display a = 0
end
a= 0
Active a <= 1 (rhs of non blocking)
$display
Inactive
always @ (enable, a, b) NBA a <= 1 (lhs update of the non blocking)
a = 0;
a <= 1;
$monitor(“monitor a = %d”, a); // output: monitor a = 1
end
Delays in procedural assignments
Delays(inter, intra)
• Inter delay – the statement itself is executed after the delay expires
• Syntax:
out1 = a + b;
#10 out2 = c+ d;
• Intra delay – the RHS is captured first and assigned to LHS after the delay
expires
• Syntax:
out1 = a + b;
out2 = #10 c + d;
Delays in continuous concurrent assignments
• Inertial delay
in this modelling, the input pulses less than the delay are filtered, remaining
pulses are passed with mentioned delay
Example:
assign #10 out = in; // input pulse widths less than 10ns will be ignored
• Transport delay
it is like a wire delay, it propagates all input pulses with the delay specified.
Example:
always@(*) begin
out <= #10 in;
end
Testbench:
• Output:
• The out = (a == b) will wait till c = 5, and it will be executed at time = 25 units;
• Event based timings
• Event based timing control statement(TCS) is based on the transitions in the
sensitivity list
• Example
module event_based_tcs (input clk, rst, d, output out);
initial begin
@(posedge clk);
rst = 1;
@(negedge clk);
rst = 0;
out = d;
end
endmodule
Types of coding in Procedural blocks(sequential,
parallel)
• Sequential
• Statements are executed one after the other
• Example
module ex (…);
…
Initial begin
#1 $display(“time = %0t”, $time);//st 1
#1 $display(“time = %0t”, $time);//st 2
#1 $display(“time = %0t”, $time);//st 3
end
….
endmodule
• Parallel
• All statements are executed at the same time – t = 0ns
• Example
module ex (…);
…
initial
fork
#1 $display(“time = %0t”, $time);//st 1
#1 $display(“time = %0t”, $time);//st 2
#1 $display(“time = %0t”, $time);//st 3
join
….
endmodule
Branching Construct
If else Statement:
• The if construct checks a specific condition and decides execution
based on the result.
Assignment 1
always @(. . . . );
begin
assignment 1;
if (condition) Yes No
begin //Alternative 1 condition
assignment 2;
end
else Assignment 2 Assignment 3
begin // Alternative 2
assignment 3;
end
assignment 4;
Assignment 4
end
If condition module test; //assignment
module test; module test;
reg [3:0] a, b, c, out;
reg a, b, c, out;
initial reg [3:0] a, b, c, out;
begin
initial if (c == 0) begin
begin a = b + 5;
out = a;
If (c == 0) assign a = 1;
end
out = a; else begin
else b = a +10;
assign b = 10;
out = b; Out = b;
end
end
end initial
initial
begin
begin
initial a = 0; b = 1; c = 1; // out -> b -> 11
a = 0; b = 1; c = 1; // out -> b -> 1
begin #1 a = 0; b = 1; c = 0; // out -> a ->
#1 a = 0; b = 1; c = 0; // out -> a ->
a = 0; b = 1; c = 1; // out -> b -> 11 5
0
#1 a = 0; b = 1; c = 0; // out -> a -> 5
end
end
end
endmodule
endmodule endmodule
Case Statement:
• Multiway decision statement that tests whether an expression matches
one of a number of other expressions and branches accordingly.
Syntax :
reg [3:0] w, x, y, z; case (expression)
integer i; case_item_1 : statement_1;
always @( w or x or y or case_item_2 : statement_2;
i) case_item_3 : statement_3;
case(i) --------------
0: z=w; default : default_statement;
1:z=x; endcase
default :z=y;
endcase
Case Statement with don’t cares:
always @(a) always @(a) a=3’b10x matches the second
case(a) casex(a) case item for casex
3’b000 : z=3’b000; 3’b000 : z=3’b000;
3’b101 : z=3’b010; 3’b101 : z=3’b010;
default : z=3’b111; default : z=3’b111;
endcase endcase
always @*
begin
case(a_in)
3’b000 : d_out=8’b00000001;
3’b001 : d_out=8’b00000010;
3’b010 : d_out=8’b00000100;
3’b011 : d_out=8’b00001000;
default : d_out=8’b00000000;
endcase
end
endmodule
Parallel case :
• A “parallel case” statement is a case statement in which it is only
possible to match a case expression to one and only one case item.
initial
initial
begin
begin
for (c = 0; c < 10; c++) begin
for (c = 0; c < 10; c++) a = b + c;
//out = a + b+ c; #1 out = a * 3;
#1 out = a + b+ c; end
end end
initial initial
begin begin
$monitor(“a = %0d, b = %0d, c = %0d, out = %0d”, $monitor(“a = %0d, b = %0d, c = %0d, out = %0d”, a, b,
a, b, c, out); c, out);
a = 0; b = 1; c = 1;
a = 0; b = 1; c = 1;
end
end
endmodule
endmodule
While loop:
Example: Example:
• $value$plusargs
• It will take the value assigned to the switch
• Example Run command:
module test (…)
Run … +switch_name=10
…..
reg [3:0] value;
if ($value$plusargs(“switch_name=%d”, value)) Output:
$display(“switch value is = %0d”, value); Switch value is = 10
….
endmodule
Stratified event queue/event scheduler/time regions
• Every change in value of a net or variable in the circuit being simulated is considered an event. Used in the
procedural blocks (always, initial).
• Events/regions can occur at different times. They are as follows
• Active
• Statements from the previous time slot
• Evaluation of
• Blocking assignments (s = 10;)
• RHS of non blocking assignments
(s <= a + b;)
• Continuous assignment (assign s = 10;)
• $display & $write
• Inactive
• #0 delay statements Inactive region ex:
• #0 a = 1;
Fork
• Non blocking/NBA
A = 0; // output – X
• Non-blocking statement LHS update
• (s <= a + b;)
// #0 A = 0;// output - 0
• Postponed A = 1;
• $strobe & $monitor Join
• Statements to next time slot
• Example
always @ (enable, a, b)
begin
a = 0;
a <= 1;
$display(“display a = %d”, a); // output: display a = 0
end
always @ (enable, a, b)
begin
a = 0; // active region
a <= 1; // lhs update in NBA region
$monitor(“monitor a = %d”, a); // output: monitor a = 1
end
• `timescale 1ns/1ns