Verilog Presentation
Verilog Presentation
Outline
Array Instantiation
Behavioral Modeling
Verilog Operators
Behavioral Description of an Adder
Always block
Procedural Assignment
If Statements
Case Statements
Comparator, Arithmetic & Logic Unit
Multiplexor, Encoder, Priority Encoder, Decoder
Full Adder Module (Gate-Level Description)
module Full_Adder(input A, B, Cin, output Cout, Sum);
endmodule
Modeling 4-bit Adder using Module
Instantiation
module Adder_4 (input [3:0] A, B, input Cin,
output [3:0] Sum, output Cout);
wire [4:0] C; // carry bits
assign C[0] = Cin; // carry input
assign Cout = C[4]; // carry output
Full_Adder FA0 (A[0], B[0], C[0], C[1], Sum[0]);
Full_Adder FA1 (A[1], B[1], C[1], C[2], Sum[1]);
Full_Adder FA2 (A[2], B[2], C[2], C[3], Sum[2]);
Full_Adder FA3 (A[3], B[3], C[3], C[4], Sum[3]);
endmodule
Modeling 4-bit Adder using Array
Instantiation
module Adder_4 (input [3:0] A, B, input Cin,
output [3:0] Sum, output Cout);
wire [4:0] C; // carry bits
assign C[0] = Cin; // carry input
assign Cout = C[4]; // carry output
Full_Adder FA [3:0] (A[3:0], B[3:0], C[3:0], C[4:1],
Sum[3:0]);
endmodule
To pass parameter:
• adder #(32) M1 (Cout, Sum, A, B, Cin);
Procedural Blocks
There are two types of procedural blocks in Verilog
1. The initial block
ALL the signals that are read inside the always block
The default case is required if not all case values are listed
A statement can be simple or compound
A compound statement is surrounded by begin ... end
Case Statement
case (expression)
case_choice1:
module ALU2 #(parameter n=8)
begin
(output reg [n-1:0] c, input [1:0] s,
...statements...
input [n-1:0] a, b);
end
case_choice2: always @(s, a, b) // begin
begin case (s)
...statements... 2'b00: c = a + b;
end 2'b01: c = a - b;
...more case choices blocks... 2'b10: c = a & b;
default: c = a | b;
default:
endcase
begin
// end
...statements... endmodule
end
endcase
Modeling a Magnitude Comparator
module Comparator #(parameter n=32)
(input [n-1:0] A, B,
output GT, EQ, LT);
endcase sel
endmodule
Modeling a 3x8 Decoder
module Decoder3x8 (input [2:0] A, output reg [7:0] D);
// Sensitivity list = @(A)
always @(A) // begin
if (A == 0) D = 8'b00000001;
else if (A == 1) D = 8'b00000010;
else if (A == 2) D = 8'b00000100;
else if (A == 3) D = 8'b00001000;
else if (A == 4) D = 8'b00010000;
else if (A == 5) D = 8'b00100000;
else if (A == 6) D = 8'b01000000;
else D = 8'b10000000;
// end
endmodule
Modeling an Encoder
module encoder (output reg [2:0] Code, input [7:0] Data);
always @(Data)
if (Data==8'b00000001) Code = 0; else
if (Data==8'b00000010) Code = 1; else
if (Data==8'b00000100) Code = 2; else
if (Data==8'b00001000) Code = 3; else
if (Data==8'b00010000) Code = 4; else
if (Data==8'b00100000) Code = 5; else
if (Data==8'b01000000) Code = 6; else
if (Data==8'b10000000) Code = 7; else
Code = 'bx;
endmodule
Modeling a Priority Encoder
module priority_encoder (output reg [2:0] Code, output valid_data, input
[7:0] Data);
assign valid_data = | Data;
always @(Data)
if (Data[7]) Code = 7; else
if (Data[6]) Code = 6; else
if (Data[5]) Code = 5; else
if (Data[4]) Code = 4; else
if (Data[3]) Code = 3; else
if (Data[2]) Code = 2; else
if (Data[1]) Code = 1; else
if (Data[0]) Code = 0; else Code = 'bx;
endmodule
Modeling a Priority Encoder
module priority_encoder2 (output reg [2:0] Code, output valid_data, input [7:0]
Data);
assign valid_data = | Data;
always @(Data)
casex (Data)
8'b1xxxxxxx : Code = 7; 8'b01xxxxxx : Code = 6;
8'b001xxxxx : Code = 5; 8'b0001xxxx : Code = 4;
8'b00001xxx : Code = 3; 8'b000001xx : Code = 2;
8'b0000001x : Code = 1;8'b00000001 : Code = 0;
default: Code = 'bx;
endcase
endmodule
Casex treats x values in the inputs as don’t care
Seven Segment Display Decoder
module Seven_Segment_Display (output reg [6:0] Display, input [3:0] BCD);
parameter BLANK = 7’b111_1111; parameter ZERO= 7’b000_0001; //abc_defg
parameter ONE= 7’b100_1111; parameter TWO= 7’b001_0010;
parameter THREE= 7’b000_0110; parameter FOUR= 7’b100_1100;
parameter FIVE= 7’b010_0100; parameter SIX= 7’b010_0000;
parameter SEVEN= 7’b000_1111; parameter EIGHT= 7’b000_0000;
parameter NINE= 7’b000_0100;
always @(BCD)
case (BCD)
0: Display = ZERO; 1: Display = ONE;
2: Display = TWO; 3: Display = THREE;
4: Display = FOUR; 5 : Display = FIVE;
6: Display = SIX; 7: Display = SEVEN;
8: Display = EIGHT; 9: Display = NINE; default: DISPLAY = BLANK;
endcase
endmodule
A piece of hardware is described as a Verilog module. One
of the given below Verilog codes is the correct description
of this piece:
Indicate which of these codes is valid and which is
invalid fully justifying your answer?