|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?注册
x
本帖最后由 kevindas 于 2017-8-13 23:06 编辑
一般状态机的写法是这样的:
- module tradition_state_machine(
- input clk ,
- input rst_n ,
- output reg [3:0] state
- );
- reg [3:0] next_state;
- always @(posedge clk or negedge rst_n) begin
- if(rst_n == 1'b0)
- state <= 4'b0001;
- else
- state <= next_state;
- end
- always @(*) begin
- case(state)
- 4'b0001: next_state = 4'b0010;
- 4'b0010: next_state = 4'b0100;
- 4'b0100: next_state = 4'b1000;
- 4'b1000: next_state = 4'b0001;
- default: next_state = 4'b0001;
- endcase
- end
- endmodule
复制代码
如果是这样写呢:
- module new_state_machine(
- input clk ,
- input rst_n ,
- output reg [3:0] state
- );
- reg [3:0] next_state;
- always @(posedge clk or negedge rst_n) begin
- if(rst_n == 1'b0)
- state <= 4'b0001;
- else
- state <= next_state;
- end
- always @(*) begin
- case(1'b1)
- state[0]: next_state = 4'b0010;
- state[1]: next_state = 4'b0100;
- state[2]: next_state = 4'b1000;
- state[3]: next_state = 4'b0001;
- default: next_state = 4'b0001;
- endcase
- end
- endmodule
复制代码
这种写法是不是和这样是等价的呢?
- module ifelse_state_machine(
- input clk ,
- input rst_n ,
- output reg [3:0] state
- );
- reg [3:0] next_state;
- always @(posedge clk or negedge rst_n) begin
- if(rst_n == 1'b0)
- state <= 4'b0001;
- else
- state <= next_state;
- end
- always @(*) begin
- if(state[0]) next_state = 4'b0010;
- else if(state[1]) next_state = 4'b0100;
- else if(state[2]) next_state = 4'b1000;
- else if(state[3]) next_state = 4'b0001;
- else next_state = 4'b0001;
- end
- endmodule
复制代码 |
|