DDCO Lab Solutions From 02-08
DDCO Lab Solutions From 02-08
Experiment-02:
Design a 4-bit full adder and simulate the same using basic gates.
module adder_4bit(
input [3:0] a,
input [3:0] b,
input c0,
output [3:0] sum,
output carry_out );
full_adder f1(.a_fa(a[0]),.b_fa(b[0]),.c_in_fa(c0),.sum_fa(sum[0]),.carry_fa(c1));
//Instantiated
full_adder f2 (.a_fa(a[1]),.b_fa(b[1]),.c_in_fa(c1),.sum_fa(sum[1]),.carry_fa(c2));
full_adder f3(.a_fa(a[2]),.b_fa(b[2]),.c_in_fa(c2),.sum_fa(sum[2]),.carry_fa(c3));
full_adder f4(.a_fa(a[3]),.b_fa(b[3]),.c_in_fa(c3),.sum_fa(sum[3]),.carry_fa(carry_out));
endmodule
module full_adder(
input [3:0] a_fa, b_fa,
input c_in_fa,
output [3:0] sum_fa,
output carry_fa );
half_adder h1 (.a_ha(a_fa),.b_ha(b_fa),.sum_ha(temp1),.carry_ha(temp3));
half_adder h2 (.a_ha(temp1),.b_ha(c_in_fa),.sum_ha(sum_fa),.carry_ha(temp2));
endmodule
endmodule
Testbench for 4-bit binary adder:
module tb_binary;
// Inputs
reg [3:0] a;
reg [3:0] b;
reg carry_in;
// Outputs
wire [3:0] sum;
wire carry;
initial
begin
a = 0; b = 0; carry_in=0;
#10;
a = 6; b = 9; carry_in=0;
#10;
a = 3; b = 3; carry_in=1;
#10;
a = 4; b = 5; carry_in=0;
#10;
a = 8; b = 2; carry_in=0;
#10;
a = 9; b = 9; carry_in=1;
#10;
end
endmodule
Experiment-03:
Design Verilog HDL to implement simple circuits using data flow, structural (gate
level), and behavioural model.
// Dataflow modelling for AND gate
module and_data_flow(
output wire Y,
input wire A,
input wire B );
assign Y = A & B;
endmodule
Experiment-04:
Design Verilog HDL to implement binary adder-subtractor – Half and full adder, Half
and full subtractor.
//half adder
module half_adder_dataflow(
output wire carry,
output wire sum,
input wire A,
input wire B );
assign sum = A^B;
assign carry = A&B;
endmodule
//full adder
module full_adder_structural(
output wire carry,
output wire sum,
input wire A,
input wire B,
input wire C_in);
// half adder model should be used
half_adder_dataflow g1(c1, s1, A, B);
half_adder_behavioural g2(c2, sum, s1, C_in);
xor g3(carry, c1, c2);
endmodule
module half_adder_dataflow(
output wire carry,
output wire sum,
input wire A,
input wire B );
assign sum = A^B;
assign carry = A&B;
endmodule
// half subtractor
module half_subtractor(
output wire borrow,
output wire difference,
input wire x,
input wire y);
assign {borrow, difference} = x - y;
endmodule
// full subtractor
module full_subtractor(
output wire borrow_out,
output wire difference,
input wire x,
input wire y,
input wire borrow_in);
wire b1, d1, b2;
half_subtractor g1(b1, d1, x, y);
half_subtractor g2(b2, difference, d1, borrow_in);
xor g3(borrow_out, b1, b2);
endmodule
module half_subtractor(
output wire borrow,
output wire difference,
input wire x,
input wire y);
assign {borrow, difference} = x - y;
endmodule
Experiment-05:
Design Verilog HDL to implement decimal/ BCD adder.
Fig. 4:1 Multiplexer (TT and expr’n) Fig. 4:1 Multiplexer (logical)
Verilog code for 4:1 mux using behavioural modelling:
Module 4_1_mux (i,s,y);
input [3:0]i;
input [1:0]s;
output reg y;
always @ (*)
begin
case (s)
2’b00: begin y=i[0]; end
2’b01: begin y=i[1]; end
2’b10: begin y=i[2]; end
2’b11: begin y=i[3]; end
Default: y=1’b0;
endcase
end
endmodule
module mux_tb();
reg [3:0]i;
reg [1:0]s;
wire y;
4_1_mux m1 (.i(i), .s(s), .y(y));
Initial
begin
i[3]=1'b0; i[2]=1'b0; i[1]=1'b0; i[0]=1'b1; s[1]=1'b0; s[0]=1'b0; #10;
i[3]=1'b0; i[2]=1'b0; i[1]=1'b1; i[0]=1'b0; s[1]=1'b0; s[0]=1'b1; #10;
i[3]=1'b0; i[2]=1'b1; i[1]=1'b0; i[0]=1'b0; s[1]=1'b1; s[0]=1'b0; #10;
i[3]=1'b1; i[2]=1'b1; i[1]=1'b0; i[0]=1'b0; s[1]=1'b1; s[0]=1'b1; #10;
end
endmodule
Experiment-07:
always @(*)
begin
case (s)
2'b00 : begin y[0] = din; end
2'b01 : begin y[1] = din; end
2'b10 : begin y[2] = din; end
2'b11 : begin y[3] = din; end
default: y[3:0]=4’b0000;
endcase
end
endmodule
Experiment-08:
1. SR flip-flop:
Verilog code:
input s,r,clk;
output reg q, qbar;
always@(posedge clk)
begin
if(s == 0 && r==1)
begin
q = 0;
qbar = 1;
end
else if(s==1 && r==0)
begin
q = 1;
qbar =0;
end
else if(s == 0 & r == 0)
begin
q <= q;
qbar <= qbar
end
end
endmodule
module dff_tb;
reg S,R, CLK;
wire Q, QBAR;
initial
begin
S= 1; R= 0; #100;
S= 0; R= 0; #100;
S= 0; R= 1; #100;
S= 0; R= 0; #100;
S= 1; R=1; #100;
end
endmodule
2. D flip-flop:
Verilog code:
module dff_behavioral(d,clk,clear,q,qbar);
input d, clk, reset;
output reg q, qbar;
always@(posedge clk)
begin
if(reset== 1)
q <= 0;
qbar <= 1;
else
q <= d;
qbar = !d;
end
endmodule
Test bench:
module dff_tb;
reg D, CLK,reset;
wire Q, QBAR;
initial
begin
clk=0;
forever #10 clk = ~clk;
end
initial
begin
reset=1; D <= 0; #100;
reset=0; D <= 1; #100;
D <= 0; #100;
D <= 1; #100;
end
endmodule
3. JK flip-flop:
module jkff_behave(clk,j,knq,qbar);
input clk,j,k;
output reg q,qbar;
always@(posedge clk)
begin
if(k = =0 && j==1)
begin
q <= 0;
qbar <= 1;
end
else if(k==1 && j==0)
begin
q <= 1;
qbar <= 0;
end
else if(j = 0 & k = 0)
begin
q <= q;
qbar <= qbar;
end
else if(j = 1 & k = 1)
begin
q <= ~q;
qbar <= ~qbar;
end
end
endmodule
Test bench:
initial
begin
clk=0;
forever #10 clk = ~clk;
end
initial
begin
J= 1; K=1; #100;
end endmodule