Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

4 Bit Up - Down Counter

Verilog Code (.v file)


module counter(clk,m,rst,count);
input clk,m,rst;
output reg [3:0] count;
always@(posedge clk or negedge rst)
begin
if(!rst)
count=0;
else if(m)
count=count+1;
else
count=count-1;
end
endmodule

Test Bench (.v file)


module counter_test;
reg clk, rst,m;
wire [3:0] count;
initial
begin
clk=0;
rst=0;#35;
rst=1;
end
initial
begin
m=1;
#180 m=0;
end
initial $sdf_annotate ("delays.sdf" , counter_test.counter1, ,"sdf.log");
counter counter1(clk,m,rst, count);
always #5 clk=~clk;
initial $monitor("Time=%t rst=%b clk=%b count=%b", $time,rst,clk,count);
initial
#330 $finish;
// dumping waves
initial begin
$recordfile("test.trn");
$recordvars;
end
endmodule
32 Bit Up - Down Counter

Verilog Code (.v file)


module counter(clk,m,rst,count);
input clk,m,rst;
output reg [31:0] count;
always@(posedge clk or negedge rst)
begin
if(!rst)
count=0;
else if(m)
count=count+1;
else
count=count-1;
end
endmodule

Test Bench (.v file)


module counter_test;
reg clk, rst,m;
wire [31:0] count;
initial
begin
clk=0;
rst=0;#35;
rst=1;
end
initial
begin
m=1;
#180 m=0;
end
initial $sdf_annotate ("delays.sdf" , counter_test.counter1, ,"sdf.log");
counter counter1(clk,m,rst, count);
always #5 clk=~clk;
initial $monitor("Time=%t rst=%b clk=%b count=%b", $time,rst,clk,count);
initial
#330 $finish;
// dumping waves
initial begin
$recordfile("test.trn");
$recordvars;
end
endmodule

You might also like