Verilog Hardware Description Language (HDL) : Why Use A HDL
Verilog Hardware Description Language (HDL) : Why Use A HDL
/conversion/tmp/scratch/420889249.doc
Page 1 of 16
4 Levels of Abstraction
Behavioral
1. Describe the algorithm without concern for the actual logic required to implement
it.
2. For example, a Behavioral description for an 8 bit, 2 input multiplexer is shown
below in bold:
// Mux2To1.v
// Behavioral description of 2 input multiplexer with
// parameter Width
`resetall
`timescale 1ns/10ps
module Mux2To1(A0,A1,Y,Sel);
parameter Width = 8;
input [Width-1:0] A0, A1;
output [Width-1:0] Y;
input Sel;
/conversion/tmp/scratch/420889249.doc
Page 2 of 16
3. What’s all this:
// - comment character
`resetall - resets all compiler directives to default values. Note that this is `
(underneath the ~) and not ‘ underneath the “.
`timescale 1ns / 10ps - specifies time unit/precision – Important in your
Testbenches when you want to wait 20 ns before changing the stimulus.
Create Verilog component with a module statement.
1. parameter is used to set constants in Verilog just like the #define
is used in C. However, the parameter can be overridden during
instantiation. This way, DataflowMux2 can be used for any size
vectors.
2. Bus size is indicated using [].
3. Specify direction of ports with input, output or inout.
4. Declare ports and other signals:
o wire or reg - Assumes 1 bit wire if you don’t specify.
case statement used to describe Mux.
Blocks of code are grouped together using begin/end like you use {} in C.
More about always, wire and reg later.
Dataflow
1. Describe the algorithm in terms of logical data flow.
2. For example, the Dataflow description for an 8 bit, 2 input multiplexer is shown
below (this time in a complete Verilog module):
// Mux2To1DFlow.v
// Dataflow description of 1 bit, 2 input multiplexor
`resetall
`timescale 1ns/10ps
module Mux2To1DFlow(A0,A1,Y,Sel);
endmodule
4. Verilog code that combines Dataflow and Behavioral coding styles is commonly
referred to as RTL (Register Transfer Language).
/conversion/tmp/scratch/420889249.doc
Page 3 of 16
Gate Level
1. Describe design in a Netlist of the actual logic gates and the interconnection
between them. This is usually generated from the RTL by the Synthesis tool.
2. For example:
From Cadence’s synthesis tool AMBIT targeting AMI 0.5um standard cell
library)
Instantiation of 2 - mx21’s and 2 - df202’s
Ports connected by name here.
1. mx21 has 4 ports: Q, I0, I1 and S.
2. I0_I0_U599 is the name of an instance of a mx21.
o I0_I0_n602 is a wire connected to the Q input.
You can also connect without the name if you go in order.
Switch Level
1. Describe design in a Netlist of switches (FETs), and the interconnect between
them.
2. Description of a 2 input Nor gate is shown below:
/conversion/tmp/scratch/420889249.doc
Page 4 of 16
// Nor2Switch.v
module Nor2Switch(A,B, Out) ;
input A, B ;
output Out ;
wire C ;
supply1 Pwr ;
supply0 Gnd ;
pmos (C,Pwr,B) ;
pmos (Out,C,A) ;
nmos(Out,Gnd,A) ;
nmos(Out,Gnd,B) ;
endmodule
/conversion/tmp/scratch/420889249.doc
Page 5 of 16
Structural Verilog
Structural Verilog modules are used to instantiate and connect other Verilog modules
together.
Consider the 8 bit, 3 input multiplexer is shown below:
// Mux3To1
// Structural HDL implementation of 3 input, 10 bit mux using 2 Mux2To1’s
// parameterized by Width
`resetall
`timescale 1ns/10ps
module Mux3To1( A0, A1, A2, Sel, Y);
parameter Width = 10;
Mux2To1 #(Width) U_0( .A0 (YInt), .A1 (A2), .Y (Y), .Sel (Sel[1:1]));
Mux2To1 #(Width) U_1(A0,A1,YInt,Sel[0:0]);
endmodule // Mux3To1
/conversion/tmp/scratch/420889249.doc
Page 6 of 16
Combination Logic in Verilog
For example:
o Behavioral description of Full Adder
o assign {COut,Sum} = a + b + CIn ;
/conversion/tmp/scratch/420889249.doc
Page 7 of 16
o Dataflow description of Full Adder
o assign Sum = CIn ^ A ^ B ;
o assign COut = (A&B) | (CIn & (A | B)) ;
OR
/conversion/tmp/scratch/420889249.doc
Page 8 of 16
Another behavioral always block for Mux2To1
always @ (A0 or A1 or Sel)
begin
Y = A0 ;
if (Sel == 1)
Y = A1 ;
end
/conversion/tmp/scratch/420889249.doc
Page 9 of 16
Sequential Logic in Verilog
// DReg.v
// Register - Reset to 0, Latches on LE
`resetall
`timescale 1ns/10ps
// synopsys template
parameter Width = 8;
// Internal Declarations
input [Width-1:0] D;
input LE, Reset, Clk;
output [Width-1:0] Q;
wire [Width-1:0] D;
wire LE, Reset, Clk;
reg [Width-1:0] Q;
endmodule
/conversion/tmp/scratch/420889249.doc
Page 10 of 16
o RTL for an up/down counter is shown below:
// synopsys template
parameter Width = 8;
// Internal Declarations
input [Width-1:0] D;
input LE, CE, Up, Reset, Clk;
output [Width-1:0] Q;
wire [Width-1:0] D;
wire LE, CE, Up, Reset, Clk;
reg [Width-1:0] Q;
/conversion/tmp/scratch/420889249.doc
Page 11 of 16
o RTL for a shift register:
// ShiftReg.v
// Shift Register - shifts ShiftIn into the MSB of Q.
`resetall
`timescale 1ns/10ps
module ShiftReg(ShiftIn, LE, Reset, Clk, Q) ;
parameter Width = 8 ;
input ShiftIn, LE, Reset, Clk ;
output [Width-1:0] Q ;
reg [Width-1:0] Q ;
wire [Width-1:0] NewQ = {ShiftIn,Q[Width-1:1]} ;
/conversion/tmp/scratch/420889249.doc
Page 12 of 16
wire vs. reg
Case Sensitive
Names must begin with alpha character
No special characters in a name except _
/conversion/tmp/scratch/420889249.doc
Page 13 of 16
Testbenches in Verilog
Generate stimulus to test your design. View the response to the stimulus in the simulator.
Never gets synthesized – only used for test.
Always use = for assignment (Blocking).
Instantiate DUT (Device Under Test) and drive it with your testbench.
Use the following Verilog constructs
o initial block – execute at the beginning of the simulation
Use = for assignment (Blocking).
o always block
Use = for assignment (Blocking).
o All outputs are reg since they are in initial or always blocks.
o integer (32 bit) and real (64 bit IEEE double precision) data types available.
o Use for and while loops inside always and initial blocks. For example:
integer i ;
initial
begin
for (i=0;i>10;i=i+1)
begin
// Verilog code here
End
i = 0 ;
while (i < 10)
begin
// Verilog code here
i ++ ;
end
end
/conversion/tmp/scratch/420889249.doc
Page 14 of 16
//CntrUpDown_tb.v
`resetall
`timescale 1ns/10ps
module CntrUpDown_tb() ;
parameter Width = 16 ;
integer i ;
reg [Width-1:0] D ;
reg LE, CE, Up, Reset, Clk ;
wire [Width-1:0] Q ;
CntrUpDown #(Width) CntrUpDown1(D, LE, CE, Up, Reset, Clk, Q);
initial
begin
D = 0 ; LE = 0 ; CE = 0 ; Up = 0 ; Reset = 1 ; Clk = 0 ;
#8 ;
Reset = 0 ; LE = 1 ; D = 1000 ;
#10 ;
LE = 0 ;
for (i=0;i<16;i=i+1)
begin
CE = 1 ;
#10 ;
CE = 0 ;
#10 ;
end
Up = 1 ;
for (i=0;i<16;i=i+1)
begin
CE = 1 ;
#10 ;
CE = 0 ;
#10 ;
end
$stop ;
end
always
begin
#5 Clk = ~Clk ;
end
endmodule
/conversion/tmp/scratch/420889249.doc
Page 15 of 16
Modelsim results for CntrUpDown_tb:
/conversion/tmp/scratch/420889249.doc
Page 16 of 16