Altera_Forum
Honored Contributor
17 years agoVerilog FSM not working on FPGA
Hi,
Please accept my appologies if I have posted this is the incorrect place, but im new to this and this post seems kind of relevant. I am trying to create a format for my FSM, To test this I wrote a simple clock_divider. This verilog works perfect in RTL simulation but when tested on an FPGA (Cyclone III) the generated clock is very random. I have attached the "count" register to LED's and every second clock some lights go dim - makes me think they may be flashing on an off extreemly quickly. Ive tried everything i can think off, even my uni lecturer is stumped. Thanks in advance for your help. Paul module Clock_Gen(rst_n, clk_out, clk_in, count); //Input Ports input rst_n; input clk_in; //**************************************************** //Output Ports output [3:0] count; output clk_out; //**************************************************** //Output Registers reg clk_out; //**************************************************** //Internal Registers reg [3:0] count; //**************************************************** //State Definition parameter S0 = 4'd0, S1 = 4'd1, S2 = 4'd2, S3 = 4'd3, S4 = 4'd4, S5 = 4'd5, S6 = 4'd6; //**************************************************** //Internal state variables reg [3:0] state; reg [3:0] next_state; //**************************************************** //Set initial reg values //**************************************************** //State changes only at positive edge of clock (synchronous) always @(posedge clk_in) if (!rst_n) state <= S0; else state <= next_state; //State Change //**************************************************** //State actions always @(state) begin case(state) S0: begin clk_out = 1'b1; count = 4'b0000; end S1: clk_out = 1'd1; S2: begin count <= (count + 1'b1); clk_out = 1'b1; end S3: begin clk_out = 1'b0; count = 4'b0000; end S4: clk_out = 1'b0; S5: begin clk_out = 1'b0; count <= (count + 1'b1); end endcase end //**************************************************** //State machine using case statement always @(state) begin case(state) S0: next_state = S1; S1: begin if (count == 14) next_state = S3; else next_state = S2; end S2: next_state = S1; S3: next_state = S4; S4: begin if (count == 14) next_state = S0; else next_state = S5; end S5: next_state = S4; endcase end endmodule