Verilog HDL
Verilog HDL
Sridhar P
VLSI Design Process
• Design Complexity Increasing Rapidly
• IC-SSI-MSI-LSI-VLSI. Chips with more than million transistors
• Need for better design flows/ process/ representation – CAD tools/ design methodology – Standardized
design flow
• Conflicting requirements – feature set, area, power, speed of operation…
• Moore’s Law the number of transistors in an integrated circuit (IC) doubles about every two years.
[18months]
8085. 6500 transistors, 3-6 Mhz Pentium 4, 42 million Transistors, 1.3 – 3.8Ghz
VLSI Design Process – Design Flow
Physical Layout [Floor Planning, Place &
Design Specification
Route]
Behavioural Description
Timing Verification, Post layout
RTL Description
• Advantages of HDL
• Designs are described at abstract level – without a choice of a specific
fabrication technology.
• Functional verification at HDL level is easier, thereby reducing the product life
cycle
• A textual representation of the design with comments helps in having a better
documented design
History of HDL
• Gateway design Automation in 1984
• VHDL was developed by US department of defense to Document ASIC
behaviorß
• Verilog based Synthesis developed by Synopsys 1987
• Initial version of VHDL, designed to IEEE standard IEEE 1076-
1987
• Cadence Purchased Verilog in 1989
• Verilog was inducted as the IEEE 1364 standard in 1995
Design Representation – Three Points of View
Behavioral Structural
Behavioral Physical Domain Domain
always @ (posedge clk) begin
if (! rstn)
out <= 0; Programs Gates
else Specifications Adders
out <= out + 1; Trust Tables Registers
end
Structural Transistors
layouts
Cells
Physical
Domain
Behavioral representation
module counter ( input clk, // Declare input port for clock to allow counter to count up
input rstn, // Declare input port for reset to allow the counter to be reset to 0
when required
output reg[3:0] out); // Declare 4-bit output port to get the counter values
// This always block will be triggered at the rising edge of clk (0->1)
// Once inside this block, it checks if the reset is 0, if yes then change out to zero
// If reset is 1, then design should be allowed to count up, so increment counter
always @ (posedge clk) begin
if (! rstn)
out <= 0;
else
out <= out + 1;
end
endmodule
Structural Representation
module ripple_carry_counter(q, clk, reset); module T_FF(q, clk, reset);
output q;
output [3:0] q;
input clk, reset;
input clk, reset; wire d;
//4 instances of the module T_FF are created. D_FF dff0(q, d, clk, reset);
T_FF tff0(q[0],clk, reset); not n1(d, q);
endmodule
T_FF tff1(q[1],q[0], reset);
T_FF tff2(q[2],q[1], reset);
T_FF tff3(q[3],q[2], reset);
endmodule
Structural Representation
module T_FF(q, clk, reset); module D_FF(q, d, clk, reset);
output q; output q;
input clk, reset; input d, clk, reset;
wire d;
reg q;
D_FF dff0(q, d, clk, reset);
not n1(d, q); always @(posedge reset or negedge clk)
endmodule if (reset)
q <= 1'b0;
else
q <= d;
endmodule
Levels of Abstraction
• Each Module internals can be defined as
• Behavioral – Highest level of Abstraction, similar to C programming
Top Level
Block
Leaf Cell Leaf Cell Leaf Cell Leaf Cell Leaf Cell Leaf Cell
Design Methodologies – Bottom Up
Top Level
Block
Leaf Cell Leaf Cell Leaf Cell Leaf Cell Leaf Cell Leaf Cell
Modules
module <Module Name> (<module terminal list>) ;
module
Inputs
module internals ; Module 1
endmodule
module
endmodule
Module 2
Design Block
endmodule
module
Module 3
endmodule
Outputs
Stimulus Block Top Level Block
Stimulus Block
Stimulus Block
Design Block
Outputs
Outputs
Simulate
• Free Software Available
• ICARUS Verilog
• GTKWave
$monitor task provides a mechanism to monitor a signal when its value changes.
$monitor(p1,p2,p3,....,pn);
$monitoron; $monitoroff;
Declarations of Dataflow
wires, regs and statements
other variables assign
endmodule
Ports Verilog Keyword Type of Port
Input Input Port
Output Output Port
Inout Bidirectional Port
IN [3:0]
Out
Mux4to1 net
SEL[1:0]
net inout
Port
reg or net
input Connection output
net
net Rules reg or net
`include "mux4to1.v"
module tb_mux4to1;
wire tout;
Mux4to1 reg [3:0] tin ;
reg [1:0] tsel ;
mux4to1 m0 ( .out(tout),
module mux4to1 ( output reg out, input [3:0] in, .in(tin),
input [1:0] sel); .sel(tsel));
initial begin
always @ (sel or in)
$dumpfile("waveform_mux.vcd” );
begin $dumpvars(0, tb_mux4to1);
case (sel) tin <= 4'b0000;
2'b00: out = in[0]; tsel <= 2'b00;
2'b01: out = in[1]; #20 tsel <= 2'b01;
2'b10: out = in[2]; #20 tin <= 4'b0101;
#40 tsel <= 2'b10;
2'b11: out = in[3];
#60 tsel <= 2'b11;
default: out = 1'bx; #80 tin <= 4'b1010;
endcase #100 tsel <= 2'b01;
end #120 tsel <= 2'b10;
endmodule #140 tsel <= 2'b11;
#20 $finish;
end
Port Connections
• Connections ports by ordered list
• mux4to1 m0 (tout, tin, tsel);
tb_mux4to1.m0.out
Tb_mux4to1.tout mux4to1 tout, tin[3:0], tsel[1:0]
mo
• Rise delay
0, x, z
t_rise
1, x, z
• Fall delay
0
t_fall
1, x
Casex, Casez
Loops
• While Loop • For Loop • Repeat
integer count; integer count; integer count;
initial initial initial
begin begin
begin
count = 0; Repeat(128)
for(i = 0; i < 32; i = i + 1)
while (count < 128) begin
Statements ;
Statements ; Statements ;
end end
count = count + 1;
end end
Forever Loop
Tasks and Functions
• Task • Function
• Declared with keyword Task • Declared with keyword function and
and Endtask endfunction
• Used when there is delay, • Used when there is no delay, event
event control or timing control or timing constructs in procedure
constructs in procedure • Procedure returns a single value
• Has either none or more • At least one input
than 1 output args • No output or inout
• No input • No nonblocking assignments
Task & Functions
• Task • Function
task bitwise_oper; function calc_parity;
output [15:0] ab_and, ab_or, ab_xor;
input [31:0] address;
input [15:0] a, b;
begin
begin
ab_and = a & b; calc_parity = ^address;
ab_or = a | b; end
ab_xor = a ^ b; endfunction
end
endtask parity = calc_parity(addr);
A function can enable another function but not A task can enable other tasks and functions.
another task.
Functions always execute in 0 simulation time. Tasks may execute in non-zero simulation time.
Functions must not contain any delay, event, or timing Tasks may contain delay, event, or timing control
control statements. statements.
Functions must have at least one input argument. Tasks may have zero or more arguments of type input,
They can have more than one input. output, or inout.
Functions always return a single value. They cannot Tasks do not return with a value, but can pass multiple
have output or inout arguments. values through output and inout arguments
Sequential Logic Design
Clock Output
Inputs
Reset
Combinatorial
Moore Model Logic Next State
State Current State
Clock Register
Reset
Timing Checks
Clock
Data
Setup hold
Use of Blocking/ Non-Blocking assignments in
Sequential logic Non-blocking
always @(posedge clk) begin
module test_reg (clk, in1, out1) ; reg1 <= in1;
reg2 <= reg1;
input clk, in1;
out1 <= reg2 ;
output out1; end
blocking
reg reg1, reg2, out1;
always @(posedge clk) begin
reg1 = in1; in1
in1 d q d q d q
d q out1 out1
reg2 = reg1; clk
out1 = reg2 ; clk clk clk clk clk
end
endmodule
Encoder
module encoder (in0, in1, in2, in3, sel);
input in0, in1, in2, in3;
output [1:0] sel ;
reg [1:0] sel ;
always @(in0, in1, in2, in3) begin
sel = 2b’00; Use Case statement or in System Verilog unique if
if (in0) sel = 2’b00;
elseif(in1) sel = 2’b01;
elseif(in2) sel = 2’b10;
elseif(in3) sel = 2’b11;
end
endmodule;
Priority Encoder
Latch
module encoder (in1, out, sel); module mux4to1 ( output reg out, input [3:0] in,
input [1:0] sel);
input in, sel; always @ (sel or in)
output out1 ; begin
case (sel)
reg out1; 2'b00: out = in[0];
always @(in1, sel) begin 2'b01: out = in[1];
2'b10: out = in[2];
if sel = 1’b1;
// 2'b11: out = in[3];
out = in1; // default: out = 1'bx;
endcase
end
end
endmodule endmodule
Re-Usable Coding Practices
• Register all the outputs of crucial design blocks. This will make the timing
interface easy during system level integration
• Avoid snake paths, as it will make both debugging tedious and synthesis
inefficient.
• Partition the design considering the clock domains and the functional
goals.
• Follow lexical and naming conventions that are self-descriptive and
facilitate future product maintenance.
• Avoid instantiation of technology specific gates
• Use parameters instead of hard-coded values in the design
• Avoid clocks and resets that are generated internal to the design
• Avoid glue logic during top level inter-module instantiations
Some Resources
• Verilog HDL: A Guide to Digital Design and Synthesis, Second
Edition By Samir Palnitkar