FSM verilog代码语法错误

时间:2013-11-17 13:59:10

标签: verilog fsm

我正在尝试设计一个用于显示旋转的fsm,它运行4位7段LED显示单元,以顺时针或逆时针方式产生循环方块的旋转模式。我试图修复我的case块中的语法错误,但我在verilog编码,我找不到我的错误。这是代码:

module fsm( EN, CW, clk, AN1,AN2,AN3,AN4, leds );

//inputs and outputs
input EN, CW, clk;
output AN1,AN2,AN3,AN4;
output reg leds[6:0];

//state register and parameters
reg state[3:0];

parameter s0 = 3'b000;
parameter s1 = 3'b001;
parameter s2 = 3'b010;
parameter s3 = 3'b011;
parameter s4 = 3'b100;
parameter s5 = 3'b101;
parameter s6 = 3'b110;
parameter s7 = 3'b111;


//states and outputs according to the states

    always @ (posedge clk)
    begin
            if (EN == 1)
            begin
                    case(state)
                            s0: leds<=7'b1100011; if(CW)begin  state <= s1;  end else begin state <= s7;  end
                            s1: leds<=7'b1100011; if(CW)begin  state <= s2;  end else begin state <= s0;  end
                            s2: leds<=7'b1100011; if(CW)begin  state <= s3;  end else begin state <= s1;  end
                            s3: leds<=7'b1100011; if(CW)begin  state <= s4;  end else begin state <= s2;  end
                            s4: leds<=7'b1011100; if(CW)begin  state <= s5;  end else begin state <= s3;  end
                            s5: leds<=7'b1011100; if(CW)begin  state <= s6;  end else begin state <= s4;  end
                            s6: leds<=7'b1011100; if(CW)begin  state <= s7;  end else begin state <= s5;  end
                            s7: leds<=7'b1011100; if(CW)begin  state <= s0;  end else begin state <= s6;  end
                    endcase
            end
    end

//output logic
assign AN1 = ~((state == s0) | (state == s7));
assign AN2 = ~((state == s1) | (state == s6));
assign AN3 = ~((state == s2) | (state == s5));
assign AN4 = ~((state == s3) | (state == s4));        

endmodule

1 个答案:

答案 0 :(得分:2)

一些事情。

你应该声明你的矢量信号:

output reg [6:0] leds;

//state register and parameters
reg [3:0] state;

并且,您需要使用begin / end包装每个案例项。我还将这些陈述分散到几行,这可能使它更具可读性:

                    s0: begin 
                            leds <= 7'b1100011; 
                            if (CW) begin
                                state <= s1;
                            end else begin 
                                state <= s7;
                            end
                        end

或者,您可以用三元组替换if / else:

                    s0: begin 
                            leds  <= 7'b1100011; 
                            state <= (CW) ? s1 : s7;
                        end