Professional Documents
Culture Documents
Redg no : 9201120
Dld assignment
Verilog code:
Test bench:
module d_flipflop_syncronous_cleartest;
// Inputs
reg d;
reg clk;
reg clear;
// Outputs
wire q;
wire qbar;
initial begin
// Initialize Inputs
clk = 0;
forever #20 clk=~clk;
end
initial begin
d=0;
clear=0;
#50;
d=1;
clear=0;
#50;
d=0;
clear=1;
#50;
d=1;
clear=1;
#50;
end
endmodule
RTL Schematic :
Simulation :
Verilog code :
Test bench :
module d_flipflop_asyncronoustest;
// Inputs
reg d;
reg clk;
reg clear;
// Outputs
wire q;
wire qbar;
initial begin
// Initialize Inputs
clk = 0;
forever #20 clk=~clk;
end
initial begin
d=0;
clear=0;
#50;
d=1;
clear=0;
#50;
d=0;
clear=1;
#50;
d=1;
clear=1;
#50;
end
endmodule
RTL Schematic :
Simulation :
D flip flop Gate level modelling
Verilog code :
endmodule
Test bench :
module d_flipflop_structuraltest;
// Inputs
reg d;
reg clk;
// Outputs
wire q;
wire qbar;
initial begin
// Initialize Inputs
// Initialize Inputs
clk = 0;
forever #20 clk=~clk;
end
initial begin
d=0;
#50;
d=1;
#50;
end
endmodule
RTL Schematic :
Simulation :
Behavioural modelling for T flip flop
module tflipflop_syncronous(t,clk,rst,q,qbar);
input t,clk,rst;
output qbar;
output reg q;
always@(posedge clk )
begin
if(rst)
q<=1'b0;
else if(t)
q<=~q;
end
assign qbar = ~q;
endmodule
Test bench :
module tflipflop_syncronoustest;
// Inputs
reg t;
reg clk;
reg rst;
// Outputs
wire q;
wire qbar;
initial begin
// Initialize Inputs
clk = 0;
forever #20 clk=~clk;
end
initial begin
t=0;
rst=1;
#30;
rst=0;
t=1;
#30;
t=0;
#30;
t=1;
#30;t=0;
#30;t=1;
#30;t=0;
#100;t=0;
#100;t=1;
end
endmodule
RTL Schematic :
Simulation :
module tflipflop(t,clk,rst,q,qbar);
input t,clk,rst;
output qbar;
output reg q;
always@(posedge clk or posedge rst)
begin
if(rst)
q<=1'b0;
else if(t)
q<=~q;
end
assign qbar = ~q;
endmodule
Test bench:
module tflipfloptest;
// Inputs
reg t;
reg clk;
reg rst;
// Outputs
wire q;
wire qbar;
initial begin
// Initialize Inputs
clk = 0;
forever #50 clk=~clk;
end
initial begin
t=0;
rst=1;
#30;
rst=0;
t=1;
#30;
t=0;
#30;
t=1;
#30;t=0;
#30;t=1;
#30;t=0;
#100;t=0;
#100;t=1;
end
endmodule
RTL Schematic :
SIMULATION :
SR flip flop using case statement
Verilog code :
module sr_flipflop_casestatement_method(s,r,clk,q,qbar);
input s,r,clk;
output reg q;
output qbar;
always@(posedge clk)
case({s,r})
2'b01: q=0;
2'b10: q=1;
//2'b11: q=z;
2'b00: q=q;
endcase
assign qbar=~q;
endmodule
Test bench :
module sr_flipflop_casestatement_methodtest;
// Inputs
reg s;
reg r;
reg clk;
// Outputs
wire q;
wire qbar;
initial begin
// Initialize Inputs
clk = 0;
forever #50 clk=~clk;
end
initial begin
s=1;
r=0;
#50;
s=0;
r=0;
#50;
s=0;
r=1;
#50;
s=1;
r=0;
#50;
s=0;
r=0;
#50;
end
endmodule
RTL Schematic :
Simulation :
Verilog code:
module sr_flipflop_ifelse_method(s,r,clk,q,qbar);
input s,r,clk;
output reg q,qbar;
always @(posedge clk)
begin
if(s==1)
begin
q=1;
qbar=0;
end
else if(r==1)
begin
q=0;
qbar=1;
end
else if(s==0&r==0)
begin
q=q;
qbar=qbar;
end
end
endmodule
Test bench :
module sr_flipflop_ifelse_methodtest;
// Inputs
reg s;
reg r;
reg clk;
// Outputs
wire q;
wire qbar;
initial begin
// Initialize Inputs
clk = 0;
forever #50 clk=~clk;
end
initial begin
s=1;
r=0;
#50;
s=0;
r=0;
#50;
s=0;
r=1;
#50;
s=1;
r=0;
#50;
s=0;
r=0;
#50;
// Wait 100 ns for global reset to finish
#100;
// Add stimulus here
end
endmodule
RTL Schematic :
Simulation :
Verilog code :
module jkflipflop_casestatement_method(j,k,clk,q,qbar);
input j,k,clk;
output reg q;
output qbar;
always@(negedge clk)
case({j,k})
2'b01: q=0;
2'b10: q=1;
2'b11: q=~q;
2'b00: q=q;
endcase
assign qbar=~q;
endmodule
Test bench :
module jkflipflop_casestatement_methodtest;
// Inputs
reg j;
reg k;
reg clk;
// Outputs
wire q;
wire qbar;
initial begin
// Initialize Inputs
clk = 0;
forever #50 clk=~clk;
end
initial begin
j=1;
k=0;
#100;
j=0;k=0;
#100;
j=0;k=1;
#100;
j=1;k=0;
#100;
j=1;k=1;
#100;
j=0;k=0;
#100;
j=1;k=1;
#100 j=0;
end
endmodule
RTL Schematic :
Simulation :
JK flip flop using if-else statements
Verilog code :
module jkflipflop_ifelse_method(j,k,clk,q,qbar);
input j,k,clk;
output reg q,qbar;
always@(negedge clk)
begin
if(j==0&k==1)
begin
q=0;
qbar=1;
end
else if(j==1&k==0)
begin
q=1;
qbar=0;
end
else if(j==1&k==1)
begin
q=~q;
qbar=q;
end
else //(j==0&k==0)
begin
q=q;
qbar=~q;
end
end
endmodule
Test bench :
module jkflipflop_ifelse_methodtest;
// Inputs
reg j;
reg k;
reg clk;
// Outputs
wire q;
wire qbar;
initial begin
// Initialize Inputs
clk = 0;
forever #50 clk=~clk;
end
initial begin
j=1;
k=0;
#100;
j=0;k=0;
#100;
j=0;k=1;
#100;
j=1;k=0;
#100;
j=1;k=1;
#100;
j=0;k=0;
#100;
j=1;k=1;
#100 j=0;
end
endmodule
RTL Schematic :
Simulation :
Verilog code for up counters in behavioural modelling
Verilog code :
reg [3:0]counter_up;
always@(posedge clk or posedge reset)
begin
if(reset)
counter_up<=4'd0;
else
counter_up<=counter_up+4'd1;
end
assign counter = counter_up;
endmodule
Test bench :
module up_countertest;
// Inputs
reg clk;
reg reset;
// Outputs
wire [3:0] counter;
initial begin
// Initialize Inputs
reset = 1;
clk = 0;
#100;
clk=1;
reset=0;
#100;
clk=0;
reset=0;
#100;
clk=1;
reset=0;
#100;
clk=0;
reset=0;
#100;
clk=0;
reset=0;
#100;
clk=1;
reset=1;
// Wait 100 ns for global reset to finish
#100;
end
endmodule
RTL Schematic :
Simulation :
Verilog code :
endmodule
Test bench :
module down_countertest;
// Inputs
reg clk;
reg reset;
// Outputs
wire [3:0] counter;
initial begin
// Initialize Inputs
reset = 1;
clk = 0;
#100;
clk=1;
reset=0;
#100;
clk=0;
reset=0;
#100;
clk=1;
reset=0;
#100;
clk=0;
reset=0;
#100;
clk=0;
reset=0;
#100;
clk=1;
reset=1;
// Wait 100 ns for global reset to finish
#100;
end
endmodule
RTL Schematic :
Simulation :