HDL Assignment-1
HDL Assignment-1
Language)
Assignment-1
1. Write the complete syntax of a Verilog design source code and testbench
code. Clearly explain the structure and purpose of each component in the
code.
$display("Nidal");//displays my id
$display("CS24B1039");
endmodule//module's end
//We require this because we don’t have a "Global Space" like we do have in
other languages
//This is used and is based on user how much copies are necessary
//display only prints the parameters one time while monitor does it
whenever a change is occurred in the function.
#1;//Time Delay
a=0;b=1;
#1;
a=1;b=0;
#1
a=1;b=1;
endmodule//end of modules
module number_representations;
initial begin
$display("%d",4'b1010);//Binary Representation of 10
$display("%d",8'o77);//octal of 77
end
endmodule
module number_representation;
end
endmodule
assign a=1'b1;//a as 1
assign b=1'b0;//b as 2
wire negate;
not G2not_(not_,a);
wire or_;
or G3or_(or_,a,b);
wire nand_;
nand G4nand_(nand_,a,b);
wire and_;
and G5_and_(and_,a,b);
wire nor_;
nor G6nor_(nor_,a,b);
3. Write the design and testbench code for all seven basic logic gates: AND,
OR, NOT, NAND, NOR, XOR, and XNOR. Simulate the code and include
screenshots of the output.
y = a & b;
end
endmodule
y = a | b;
end
endmodule
y = ~a;
end
endmodule
end
endmodule
y = ~(a | b);
end
endmodule
y = a ^ b;
end
endmodule
module xnor_gate(input a, input b, output reg y);
y = ~(a ^ b);
end
endmodule
//Testbench Codes
module test_gates;
reg a, b;
initial begin
$monitor("%0t %b %b | %b %b %b %b %b %b %b",
$time, a, b, and_out_, or_out_, not_out_, nand_out_, nor_out_, xor_out_,
xnor_out_);
a = 0; b = 0; #10;//Delay of 10ns
a = 0; b = 1; #10;
a = 1; b = 0; #10;
a = 1; b = 1; #10;
end
endmodule
//Dataflow Modelling
assign y = a & b;
endmodule
assign y = a | b;
endmodule
assign y = ~a;
endmodule
endmodule
endmodule
assign y = a ^ b;
endmodule
endmodule
module test_gates;
reg a, b;
initial begin
$display("Nidal Noushad Roshan CS24B1039");
$monitor("%0t %b %b | %b %b %b %b %b %b %b",
a = 0; b = 0; #10;//Delay of 10ns
a = 0; b = 1; #10;
a = 1; b = 0; #10;
a = 1; b = 1; #10;
end
endmodule
endmodule
or G2(y, a, b);
endmodule
endmodule
endmodule
endmodule
endmodule
module test_gates;
reg a, b;
$monitor("%0t %b %b | %b %b %b %b %b %b %b",
a = 0; b = 0; #10;//Delay of 10ns
a = 0; b = 1; #10;
a = 1; b = 0; #10;
a = 1; b = 1; #10;
end
endmodule
4. Write the design and testbench code for: • Half Adder • Full Adder • Half
Subtractor • Full Subtractor Execute the code, capture the output, and
include screenshots.
module half_adder(
input B,
);
Sum = A ^ B;
Carry = A & B;
end
endmodule
module full_adder(
input A,
input B,
input Cin,
);
Sum = A ^ B ^ Cin;
end
endmodule
module half_subtractor(
input A,
input B,
);
Diff = A ^ B;
end
endmodule
module full_subtractor(
input A,
input B,
input Bin,
);
Diff = A ^ B ^ Bin;
end
endmodule
module test_behav;
initial begin
$display("Time | HA: A B -> Sum Carry | HS: A B -> Diff Borrow | FA: A B Cin
-> Sum Carry | FS: A B Bin -> Diff Borrow");
end
endmodule
endmodule
module full_adder(
input A,
input B,
input Cin,
output Sum,
output Carry
);
endmodule
module half_subtractor(
input A,
input B,
output Diff,
output Borrow
);
assign Diff = A ^ B;
endmodule
module full_subtractor(
input A,
input B,
input Bin,
output Diff,
output Borrow
);
endmodule
module test_behav;
initial begin
$display("Time | HA: A B -> Sum Carry | HS: A B -> Diff Borrow | FA: A B Cin
-> Sum Carry | FS: A B Bin -> Diff Borrow");
end
endmodule
module half_adder(
input A,
input B,
output Sum,
output Carry
);
endmodule
module full_adder(
input A,
input B,
input Cin,
output Sum,
output Carry
);
wire xor1, and1, and2, and3;//Since gate -level Model is not as sophisticated
as other models in multi bit operations
xor (Sum, xor1, Cin);//adding up cin and adding up with previous xor1
endmodule
module half_subtractor(
input A,
input B,
output Diff,
output Borrow
);
wire nA;
endmodule
module full_subtractor(
input A,
input B,
input Bin,
output Diff,
output Borrow
);
endmodule
module test_behav;
initial begin
$display("Nidal Noushad Roshan CS24B1039");
$display("Time | HA: A B -> Sum Carry | HS: A B -> Diff Borrow | FA: A B Cin
-> Sum Carry | FS: A B Bin -> Diff Borrow");
endmodule
5. Write the design and testbench code for: • 2x1 Multiplexer • 4x1 Multiplexer
Simulate the code, take a screenshot of the results, and include them in your
document.
//Multiplexer 2x1
module mux2x1(
output y // Output
);
endmodule
//Multiplexer 4x1
module mux4x1(
output y // Output
);
(sel == 2'b01) ? m1 :
endmodule
//TestBench Code
module test_mux;
wire mux2_out;
wire mux4_out;
mux2x1 M1(
.m0(mux2_m0),
.m1(mux2_m1),
.sel(mux2_sel),
.y(mux2_out)
);
mux4x1 M2(
.m0(mux4_m0),
.m1(mux4_m1),
.m2(mux4_m2),
.m3(mux4_m3),
.sel(mux4_sel),
.y(mux4_out)
);
initial begin
$display("Time | 2x1 MUX: d0 d1 sel -> y | 4x1 MUX: d0 d1 d2 d3 sel -> y");
#10;
mux2_sel = 1; // this should select m1 by the same test inputs given before
OR CAN SAY IT SWITCHED
#10;
mux2_m0 = 1; mux2_m1 = 0;
#10;
mux2_sel = 0;
#10;
end
endmodule
//Multiplexer 2x1
module mux2x1(
input m0,
input m1,
input sel,
output y
);
endmodule
//Multiplexer 4x1
module mux4x1(
input m0,
input m1,
input m2,
input m3,
output y
);
endmodule
//TestBench Code
module test_mux;
wire mux2_out;
wire mux4_out;
.m0(mux2_m0),
.m1(mux2_m1),
.sel(mux2_sel),
.y(mux2_out)
);
mux4x1 M2(
.m0(mux4_m0),
.m1(mux4_m1),
.m2(mux4_m2),
.m3(mux4_m3),
.sel(mux4_sel),
.y(mux4_out)
);
initial begin
$display("Time | 2x1 MUX: d0 d1 sel -> y | 4x1 MUX: d0 d1 d2 d3 sel -> y");
mux2_sel = 1; // this should select m1 by the same test inputs given before
OR CAN SAY IT SWITCHED
#10;
mux2_m0 = 1; mux2_m1 = 0;
#10;
mux2_sel = 0;
#10;
end
endmodule
//Multiplexer 2x1
module mux2x1(
);
if (sel)
y = m1;
else
y = m0;
end
endmodule
//Multiplexer 4x1
module mux4x1(
);
case(sel)
2'b00: y = m0;
2'b01: y = m1;
2'b10: y = m2;
2'b11: y = m3;
endcase
end
endmodule
//TestBench Code
module test_mux;
wire mux2_out;
wire mux4_out;
mux2x1 M1(
.m0(mux2_m0),
.m1(mux2_m1),
.sel(mux2_sel),
.y(mux2_out)
);
mux4x1 M2(
.m0(mux4_m0),
.m1(mux4_m1),
.m2(mux4_m2),
.m3(mux4_m3),
.sel(mux4_sel),
.y(mux4_out)
);
initial begin
#10;
mux2_sel = 1; // this should select m1 by the same test inputs given before
OR CAN SAY IT SWITCHED
#10;
mux2_m0 = 1; mux2_m1 = 0;
#10;
mux2_sel = 0;
#10;
end
endmodule