I have the task of designing a ring counter in verilog using shift operator. Here is the code so far along with test bench :
module ring (
input wire [3:0] data,
input wire clk,
input wire rst,
input wire load,
output reg [3:0] q
) ;
/*When the load pin is High, the data is loaded in the counter on clock transition.
* Otherwise the data is shifted upon each clock.
* Last output is fed back to the first input.
*/
initial q = 4'b1010 ;
reg tmp = 1'b0 ;
always @ (posedge clk, posedge rst)
begin
if(rst)
begin
q <= 4'b0000 ;
end
else if(load)
begin
q[3] <= data[3] ;
q[2] <= data[2] ;
q[1] <= data[1] ;
q[0] <= data[0] ;
end
else
begin
tmp = q[0] ;
q = q >> 1 ;
q[3] = tmp ;
end
end
endmodule
Test bench
`timescale 1 ns / 1 ns
module ringTest ;
//Signals
reg [3:0] data = 4'b1010 ;
wire [3:0] q = 4'b0000 ;
reg clk = 1'b0 ;
reg rst = 1'b0 ;
reg load = 1'b0 ;
//Instantiate uut
ring uut ( .data(data),
.q(q),
.clk(clk),
.rst(rst),
.load(load)
);
always #10 clk = ~clk ;
/*Load data every 100 s
always
begin
#95 load = 1'b1 ;
#5 load = 1'b0 ;
end
*/
//Data to be loaded
always
begin
if ( data == 4'b1111) data <= 4'b0000 ;
else begin
# 50 data <= data + 4'b0001 ;
end
end
endmodule
Note : I have temporarily disabled load functionality bcoz the Counter wasnt working. So instead I chose to shift the data with an initial value, which also did not work. The output is always X.
Where am I going wrong ?
Thank you.
rst
. Also, I'd advise resetting to something other than 4'b0000 or 4'b1111 if you actually want to see that the counter is doing something. \$\endgroup\$ – The Photon Feb 16 '15 at 17:51q
in both modules?q
is an output from your counter. Don't assign it in your testbench. \$\endgroup\$ – The Photon Feb 16 '15 at 17:57