Verilog HDL Lectuer 5
Verilog HDL Lectuer 5
Verilog HDL Lectuer 5
LANGUAGE
LECTUER-5
DIVYA SHAH
03/03/09
Behavioral Modeling:
PROCEDURAL ASSIGNMENT
The procedural block defines
A region of code containing sequential
statements.
The statements execute in the order they are
written.
2. if (expression)
sequential_statement
else
sequential_statement
end if
3. case (expression)
expr: sequential_statement
…….
default: sequential_statement
endcase
4. forever
sequential_statement
5. repeat (expression)
sequential_statement
6. while (expression)
sequential_statement
8. # (time_value)
Makes a block suspend for “time_value” time
units.
• Blocking assignment
Uses the ‘=’ operator
• Non-blocking assignment
Uses the ‘<=’ operator
Blocking Assignment (using ‘=’)
a = 1; a <= 1;
b = a; b <= a;
c = b; c <= b;
//Blocking assignment: // Nonblocking assignment:
a=b=c=1 a=1
b = old value of a
c = old value of b
• RHS of nonblocking taken from latches
• RHS of blocking taken from wires
Some Rules to be Followed
A en
COMPLEMENTOR
BOUT
c_in Add_sub
ADDER
carry sum
PARITY CHECKER P
module complementor (Y, X, comp);
input [7:0] X;
input comp;
output [7:0] Y; reg [7:0] Y;
always @ (X or comp)
if (comp)
Y = ~X;
else
Y = X;
endmodule
module adder (sum, cy_out, in1, in2, cy_in);
input [7:0] in1, in2;
input cy_in;
output [7:0] sum; reg [7:0] sum;
output cy_out; reg cy_out;
always @ (in1 or in2 or cy_in)
{cy_out, sum} = in1 + in2 + cy_in;
endmodule
module parity_checker (out_par, in_word);
input [8:0] in_word;
output out_par;
always @ (in_word)
out_par = ^ (in_word);
endmodule
Top level module
module add_sub_parity (p, a, b, add_sub);
input [7:0] a, b;
input add_sub; // 0 for add, 1 for subtract
output p; // parity of the result
wire [7:0] Bout, sum;
wire carry;
complementor M1 (Bout, B, add_sub);
adder M2 (sum, carry, A, Bout, add_sub);
parity_checker M3 (p, {carry, sum});
endmodule
Verilog Test Bench
module shifter_toplevel;
reg clk, clear, shift;
wire [7:0] data;
shift_register S1 (clk, clear, shift, data);
initial
begin
clk = 0; clear = 0; shift = 0;
end
always
#10 clk = !clk;
endmodule
Testbench: More Complete Version
module shifter_toplevel;
reg clk, clear, shift;
wire [7:0] data;
shift_register S1 (clk, clear, shift, data);
initial
begin
clk = 0; clear = 0; shift = 0;
end
always
#10 clk = !clk;
//contd
initial
begin
$dumpfile (“shifter.vcd”);
$dumpvars;
end
initial
begin
$display (“\ttime, \tclk, \tclr, \tsft, \tdata);
$monitor (“%d, %d, %d, %d, %d”, $time,
clk, reset, clear, shift, data);
end
initial
#400 $finish;
***** REMAINING CODE HERE ******
endmodule
A Complete Example
module testbench;
wire w1, w2, w3;
xyz m1 (w1, w2, w3);
test_xyz m2 (w1, w2, w3);
endmodule
module xyz (f, A, B);
input A, B; output f;
nor #1 (f, A, B);
endmodule
module test_xyz (f, A, B);
input f;
output A, B;
reg A, B;
initial
begin
$monitor ($time, “A=%b”, “B=%b”, f=%b”,A, B, f);
#10 A = 0; B = 0;
#10 A = 1; B = 0;
#10 A = 1; B = 1;
#10 $finish;
end
endmodule