LAB#2
LAB#2
er
SystemsEngineering
DigitalSystemDesign
Handout#02
VerilogOperators
LabLearningObjectives:
Aftercompletingthissession,studentshouldbeableto:
useVerilogHDLoperatorswithease.
Note:Submitthelabreport(solvedactivitiesandexercises)beforethenextlab.
LabHardwareandSoftwareRequired:
BackgroundTheory:
Table1:VerilogOperators
LabActivity:
You are required to make a hardware design that uses pre-defined operators in
VerilogHDL.Toachievethisgoal,youwillmakeamodulethattakessomeinputs,usessomeofth
eavailableoperators(listedintable#1),andproducesanoutput.Later,writeatest
benchprogramtoanalyzethebehaviorofyourhardwaredesignagainsttheinputtestvectors.Th
e usage of all the operators is explained in chapter#3 of the textbook. Please
usesyntaxesfromyourtextbook for thisactivity.
Exercise:
1. Explain the working of the four shift operators through a Verilog HDL design.
Youmayuse2places forshifting.
2. Makea2-bitsmaxcomparatorHDLdesignusing‘?’operator.Youmaytakethree2-bit
inputs (a, b, c), and a 2-bit output should be a max number of all the threeinputs.
Note:Youalsoneedtowriteatestbenchcodefortheaboveexercises.Pleaseprovideyourexplanationwithsupportin
gwaveformsobtainedfromyourdesign.
Testbench:
module lab_2_test;
reg a,b;
wire plus, sub, mul, div, mod, expo;
lab_2_des f1(.a(a),
.b(b),
.plus(plus),
.sub(sub),
.mul(mul),
.div(div),
.mod(mod),
.expo(expo)
);
initial begin
a=1'b0; b=1'b0;
#1;
a=1'b0; b=1'b1;
#1;
a=1'b1; b=1'b0;
#1;
a=1'b1; b=1'b1;
#1;
$finish;
end
endmodule
Design code:
module lab_2_des(
input wire a, b,
endmodule
Shift operators:
Design code:
module shift_op_des(
input wire [3:0] data,
Testbench:
module shift_op_test;
reg [3:0] data;
shift_op_des uut(
.data(data),
.shift_left(shift_left),
.shift_right(shift_right),
.shift_left_arth(shift_left_arth),
.shift_right_arth(shift_right_arth)
);
initial begin
data = 4'b1001;
#1;
data = 4'b1100;
#1;
data = 4'b1101;
#1;
data = 4'b0011;
#1;
$finish;
end
endmodule
Relational Operator:
Design code:
module relational_des(
endmodule
Testbench:
reg [3:0] a, b;
.a(a),
.b(b),
.less_than(less_than),
.greater_than(greater_than),
.less_or_equal(less_or_equal),
.greater_or_equal(greater_or_equal)
);
initial begin
a = 4'b1010; b = 4'b1011;
#1;
$display("a = %b, b = %b, less_than = %b, greater_than = %b, less_or_equal = %b, greater_or_equal =
%b", a, b, less_than, greater_than, less_or_equal, greater_or_equal);
$finish;
end
endmodule
Equality:
Design code:
);
endmodule
Testbench:
reg [3:0] a, b;
relational_des uut (
.a(a),
.b(b),
.equal(equal),
.equal_not(equal_not),
.case_equal(case_equal),
.case_equal_not(case_equal_not)
);
initial begin
a = 4'b1010; b = 4'b1011;
#1;
$finish;
end
endmodule
Bitwise:
Design code:
);
assign bitwise_or = a | b;
assign bitwise_neg= ~a;
assign bitwise_xor= ^b;
endmodule
Testbench:
reg [3:0] a, b;
wire [3:0] bitwise_and, bitwise_or, bitwise_neg, bitwise_xor;
// Instantiate the design module
relational_des uut (
.a(a),
.b(b),
.bitwise_and(bitwise_and),
.bitwise_or(bitwise_or),
.bitwise_neg(bitwise_neg),
.bitwise_xor(bitwise_xor)
);
initial begin
a = 4'b1010; b = 4'b0101;
#1;
$display("a = %b, b = %b, bitwise_and = %b, bitwise_or = %b, bitwise_neg
bitwise_xor);
$finish;
end
endmodule
Reduction:
Design code:
module relational_des(
);
endmodule
Testbench:
reg [3:0] a;
.a(a),
.and_reduce(and_reduce),
.or_reduce(or_reduce),
.xor_reduce(xor_reduce)
);
initial begin
a = 4'b1111;
#1;
$display("a = %b, and_reduce = %b, or_reduce = %b, xor_reduce = %b, ", a, and_reduce,
or_reduce, xor_reduce);
$finish;
end
endmodule
Conditional:
Design code:
endmodulemodule relational_des(
);
endmodule
Testbench:
reg [3:0] a, b;
relational_des uut (
.a(a),
.b(b),
.cond(cond)
);
initial begin
a = 4'b1010; b = 4'b0101;
#1;
$finish;
end
endmodule
Logical operator:
Design code:
module relational_des(
input wire [3:0] a,
);
assign logical_or = a || b;
endmodule
Testbench:
module relational_test;
reg [3:0] a, b;
relational_des uut (
.a(a),
.b(b),
.logical_and(logical_and),
.logical_or(logical_or),
.logical_neg(logical_neg)
);
initial begin
a = 4'b1010; b = 4'b0000;
#1;
$finish;
end
endmodule
Concatenation Operation:
Design code:
module relational_des(
input wire [3:0] a,
input wire [3:0] b,
output wire [7:0] concat
);
assign concat = {a, b};
endmodule
Testbench:
`timescale 1ns / 1ps
module relational_test;
reg [3:0] a, b;
wire [7:0] concat;
relational_des uut (
.a(a),
.b(b),
.concat(concat)
);
initial begin
a = 4'b1010; b = 4'b0101;
#1;
$finish;
end
endmodule