Hi,
As part of a larger project, the aim of my Verilog is, when a button is pressed, load a 72-bit value into a register and then shift that data onto a wire one bit at a time and have a flag that goes high for one clock cycle when there is new data available on this line.
Here is my code:
module Main(
input wire CLK100MHZ,
output wire [15:0] LED,
input wire [15:0] SW,
input wire BTNC,
output wire CA, CB, CC, CD, CE, CF, CG, DP,
output wire [7:0] AN);
reg [71:0] DATA; //the data we are shifting
reg NEW_DATA; //flag indicating new data available on DATA[71]
reg [31:0] data7; //output to the seven segment display for debugging
reg [11:0] new_counter;
seven_seg_decoder seven_seg(.clk(CLK100MHZ), //seven segment display driver module
.number(data7),
.CA(CA),
.CB(CB),
.CC(CC),
.CD(CD),
.CE(CE),
.CF(CF),
.CG(CG),
.DP(DP),
.AN(AN));
reg last_BTNC;
reg [31:0] counter;
assign LED[1] = (DATA == 72'h000000000000000000);
always @(posedge CLK100MHZ) begin
//mux data to seven segment display for debugging
case(SW[3:1])
0: data7 <= DATA[31:0];
1: data7 <= DATA[63:32];
2: data7 <= {24'h000000, DATA[71:64]};
3: data7 <= {20'h00000, new_counter};
4: data7 <= counter;
default: data7 <= {32'd0};
endcase
if(counter < 36863) begin //keep counting until we have bit-shifted 72 times
counter <= counter + 1;
end
//on button press, load data and reset counter
if(BTNC == 1 && last_BTNC != BTNC && DATA == 72'h000000000000000000) begin
DATA <= 72'hAABBCCDDEEFFAABBCC;
counter <= 0;
end
//every 256 clock cycles turn on NEW_DATA for one clock indicating that DATA[71] has changed
if(counter[8:0] == 9'h100) begin
NEW_DATA <= 1;
end else begin
NEW_DATA <= 0;
end
//every 256 clock cycles, shift bit shift DATA
if(counter[8:0] == 9'h100) begin
DATA[71:0] <= {DATA[70:0] , 1'b0};
end
//count the number of times NEW_DATA is asserted
if(NEW_DATA == 1) begin
new_counter <= new_counter + 1;
end
last_BTNC <= BTNC;
end
endmodule
CA, CB, CC, CD, CE, CF, CG, DP, AN are outputs for a 32-bit seven segment display. SW[15:0] are latching switch inputs. LED[15:0] are LED outputs. BTNC is a tactile switch.
What should happen is, when BTNC is pressed, DATA becomes 0xAABBCCDDEEFFAABBCC and counter gets set to zero. The counter then counts up and every 256 clock cycles DATA is shifted to the left by one bit and the lowest bit becomes a zero until DATA is entirely zeros.
In reality, this happens most of the time. But If I keep pressing the button, eventually DATA gets stuck at a non-zero value e.g. 0x4.
I can't find any way to explain this. Please help!!