Verilog Overview 4200
Verilog Overview 4200
Verilog Overview 4200
ELEC 4200
Victor P. Nelson
Hardware Description Languages
• Verilog – created in 1984 by Philip Moorby of Gateway Design
Automation (merged with Cadence)
• IEEE Standard 1364-1995/2001/2005
• Based on the C language
• Verilog-AMS – analog & mixed-signal extensions
• IEEE Std. 1800-2012 “System Verilog” – Unified hardware design, spec,
verification
• VHDL = VHSIC Hardware Description Language
(VHSIC = Very High Speed Integrated Circuits)
• Developed by DOD from 1983 – based on ADA language
• IEEE Standard 1076-1987/1993/2002/2008
• VHDL-AMS supports analog & mixed-signal extensions
HDLs in Digital System Design
• Model and document digital systems
• Behavioral model
• describes I/O responses & behavior of design
• Register Transfer Level (RTL) model
• data flow description at the register level
• Structural model
• components and their interconnections (netlist)
• hierarchical designs
• Simulation to verify circuit/system design
• Synthesis of circuits from HDL models
• using components from a technology library
• output is primitive cell-level netlist (gates, flip flops, etc.)
Verilog Modules
The module is the basic Verilog building block
Module name List of I/O signals (ports)
functional description
endmodule
Module “ports”
• A port is a module input, output or both
module full_adder (ai, bi, cini, si, couti);
input ai, bi, cini; //declare direction and type
output si, couti; //default type is wire
• Verilog 2001: Signal port direction and data type can be combined
module dff (d, clk, q, qbar); //port list
input d, clk;
output reg q, qbar; // direction and type
• Verilog 2001: Can include port direction and data type in the port list (ANSI C format)
module dff (input d,
input clk,
output reg q, qbar);
Data types
• Nets connect components and are continuously assigned values
• wire is main net type (tri also used, and is identical)
• Variables store values between assignments
• reg is main variable type
• Also integer, real, time variables
// Equivalent to:
wire a = b | (c & d);
Examples: 2-to-1 multiplexer
// function modeled by its “behavior”
module MUX2 (A,B,S,Z);
input A,B,S; //input ports
output Z; //output port A, B, Z could
always //evaluate block continuously also be vectors
begin
if (S == 0) Z = A; //select input A
(of equal # bits)
else Z = B; //select input B
end
endmodule
output Z; z2
S1
d
wire z1,z2;
MUX2 M1(A,B,S0,z1); //instance M1 of MUX2 S0
MUX2 M2(c,d,S0,z2); //instance M2 of MUX2
MUX2 M3(.S(S1), .Z(Z), .A(z1),.B(z2)); //connect signal to port: .port(signal)
// more descriptive, less error-prone
endmodule
Intra-assignment:
x = #5 y; //Equivalent to the following
hold = y; //capture y at t = 0
#5; //delay until t = 5
x = hold; //update x
Delayed assignment:
#5 x = y; //Equivalent to the following
#5; //delay from t = 0 until t = 5
x = y; //copy value of y to x
//Non-blocking example
x = 0; //execute at t = 0
a = 1; //execute at t = 0
c <= #15 3; //evaluate at t = 0, schedule c to change at t = 15
d <= #10 4; //evaluate at t = 0, schedule d to change at t = 10
c <= c + 1; //evaluate at t = 0, schedule c to change at t = 0
Example of blocking/non-blocking delays
initial begin
a = 1; b = 0; //block until after change at t=0
#1 b = 1; //delay until t=1; then block until b=1 Results:
c = #1 1; //block until t=2, c=val from t=1 t a b c d e f g
0 1 0 x x x x x
#1; //delay to t=3
1 1 1 x x x x x
d = 1; //block until change at t=3
2 1 1 1 x x x x
e <= #1 1; //non-blocking, update e at t=4 3 1 1 1 1 x x x
#1 f <= 1; //delay to t=4, non-blocking f update 4 1 1 1 1 1 1 1
g <= 1; //still t=4, non-blocking g update
end
Example
• Blocking: (a-b end up with same value – race condition)
always @(posedge clock)
a = b; //change a NOW
always @(posedge clock)
b = a; //change b to new a value
b
• Non-blocking: (a-b swap values) D Q a D Q
always @(posedge clock)
a <= b; //read b at t=0, schedule a to change
always @(posedge clock)
b <= a; //read a at t=0, schedule b to change
A B C
Non-blocking Anext A_r
+1
B_r
+1
C_r
endmodule
//-----------------------------------------------------------------------------
// Title Lab 6 test bench : RegFile_tb
//-----------------------------------------------------------------------------
`timescale 1ns / 1ns
module RegFile_tb;
endmodule
Producing a clock signal
initial x = 0; //set initial value
always begin //block is repeated (assume t=0 initially)
#25 x = 1; //delay to t=25, then continue by assigning x=1
#10 x = 0; //delay to t=35, then continue by assigning x=0
#5; //delay to t=40, then continue
end
40
25 10 5
Example – D flip flop
module example
reg Q, Clk;
wire D;
assign D = 1; //D=1 for this example
always @(posedge Clk) Q = D; //normal flip flop clocking
initial Clk = 0; //initial state of Clk reg
always #10 Clk = ~Clk; //toggle clock for period of 20
initial begin
#50;
$finish; //simulation control – end simulation
end
always begin
$display(“T=“,%2g, $time,” D=“,D,” Clk =“,Clk,” Q=“,Q); //generate output listing every 10 time units
#10;
end
endmodule
Verilog built-in primitive gates
• Verilog has 8 gate types that are primitive components:
and, or, nand, nor, xor, xnor, not, buf
• Format:
gate INSTANCE_NAME (Z,I1,I2,…IN); // list output first, followed by inputs
module carry_out(A,B,Cin,Cout)
input A,B,Cin;
output Cout;
wire w1,w2,w3;
and A1 (w1,A,B); //primitive and gate instances
and A2 (w2,A,Cin);
and A3 (w3,B,Cin);
or O1 (Cout,w1,w2,w3); //primitive or gate instance
endmodule
Lists of assign/gate instance statements
• Can specify a comma-separated list of gates of one type
• Likewise for “assign” statements
module carry_out(A,B,Cin,Cout)
input A,B,Cin;
output Cout;
wire w1,w2,w3,w4,w5;
and A1 (w1,A,B), // list of three and gate instances
A2 (w2,A,Cin),
A3 (w3,B,Cin);
assign w4 = w1 & w2, // list of two assign statements
Cout = w4 & w3;
endmodule
Specifying delays
• Net delays:
assign #8 a = b & c; //a changes 8 time units after b/c change
wire #8 a = b & c; //equivalent to the following statement pair
wire a;
assign #8 a = b & c;
//above also equivalent to the following statement pair
wire #8 a; //8 units of delay to any assignment to net a
assign a = b & c;