Verilog by Example
Verilog by Example
Example
Presenter - Soumyaroop Roy
September 17, 2007
Objectives
The objectives of this tutorial are to:
introduce some basic constructs in Verilog
elaborate on structural and behavioral design
styles
introduce testbenches
enable attendees to design the ALU from Lab 1
and its testbench.
2
Verilog HDL
The designers of Verilog wanted a language with
syntax similar to the C programming language so
that it would be familiar to engineers and readily
accepted
Article on Verilog, Wikipedia
However, since Verilog is used to describe
hardware, its behavior is different from that of the C
language.
3
Inverter Example
// module definition for the inverter
module inv (inp, op);
// port definitions
input inp; // input port inp
output op; // input port op
// concurrent assignment statement
assign op = ~inp; // ~ is the unary not operator
// indicates the end of a module
endmodule
Multiplexer Example
// module definition
module mux2_1_bo(mux_in, sel, mux_out);
// port definitions
output mux_out;
input sel;
input [1:0] mux_in;
// concurrent assignment statement
assign mux_out = (~sel & mux_in[0]) & (sel & mux_in[1]);
endmodule
in
the
Event triggered
always @(clk)
Sensitive to both 10
and 01 transitions of
clk
11
Testbench
Testbench is a behavioral Verilog code that
supplies stimuli to the design under test (DUT) and
tests its outputs (how it behaves) based on its
functional specifications (how it should behave).
The next example shall illustrate it.
12
Inverter Testbench
// testbench module definition
module test_inv;
// stimulus for inverter input
reg inv_inp;
// output of inverter
wire inv_op;
// initial block
initial
begin
inv_inp = 0;
#10 inv_inp = 1;
#10 inv_inp = 0;
// inv_op will be driven by u_inv
// inverter instantiation
inv u_inv (.inp(inv_inp)),
.op(inv_op));
Miscellaneous Examples
// full adder using half adders - structural
ha u1_ha(c1, s1, a_in, b_in);
a
s
HA
b
c
ha u2_ha(c2, sum, c_in, s1);
or (carry, c1, c2);
// note that the or primitive does not need an instance name
// parameters; similar to arguments to functions in C
parameter width = 4; // parameter values can be configured
wire [width-1:0] inp; // from the instantiating module
// generate clock
always #10 clock = ~clock // where clock is defined as a reg
14
15
16
Q&A