HDLBits 系列(24)进入FSM(有限状态机)的世界入口

举报
李锐博恩 发表于 2021/07/15 02:14:29 2021/07/15
【摘要】 目录 Fsm1 Fsm1s Fsm2 Fsm3comb Fsm1 This is a Moore state machine with two states, one input, and one output. Implement this state machine. Notice that the reset state is B. This exerc...

目录

Fsm1

Fsm1s

Fsm2

Fsm3comb


Fsm1

This is a Moore state machine with two states, one input, and one output. Implement this state machine. Notice that the reset state is B.

This exercise is the same as fsm1s, but using asynchronous reset.

Module Declaration


  
  1. module top_module(
  2. input clk,
  3. input areset, // Asynchronous reset to state B
  4. input in,
  5. output out);

这个题目给出了一个最简单的摩尔型的有限状态机,仅仅有两个状态,让你描述这个有限状态机:

采用三段式,给出设计如下:


  
  1. module top_module(
  2. input clk,
  3. input areset, // Asynchronous reset to state B
  4. input in,
  5. output out);//
  6. parameter A=0, B=1;
  7. reg state, next_state;
  8. always @(posedge clk, posedge areset) begin // This is a sequential always block
  9. // State flip-flops with asynchronous reset
  10. if(areset) state <= B;
  11. else state <= next_state;
  12. end
  13. always @(*) begin // This is a combinational always block
  14. // State transition logic
  15. case(state)
  16. B: begin
  17. if(in == 1) next_state = B;
  18. else next_state = A;
  19. end
  20. A: begin
  21. if(in == 1) next_state = A;
  22. else next_state = B;
  23. end
  24. endcase
  25. end
  26. // Output logic
  27. // assign out = (state == ...);
  28. assign out = (state == A)?0:1;
  29. endmodule


Fsm1s

This is a Moore state machine with two states, one input, and one output. Implement this state machine. Notice that the reset state is B.

This exercise is the same as fsm1, but using synchronous reset.

Module Declaration


  
  1. // Note the Verilog-1995 module declaration syntax here:
  2. module top_module(clk, reset, in, out);
  3. input clk;
  4. input reset; // Synchronous reset to state B
  5. input in;
  6. output out;

这个题目和上一句几乎一模一样,就是复位方式变成了同步复位;

可恨的是,题目还给了模板提示,提示用的是一段式,并且是Verilog1995,天了噜,这种风格,告辞了,三段式继续走起:


  
  1. module top_module(
  2. input clk,
  3. input reset, // Asynchronous reset to state B
  4. input in,
  5. output out);//
  6. parameter A=0, B=1;
  7. reg state, next_state;
  8. always @(posedge clk) begin // This is a sequential always block
  9. // State flip-flops with asynchronous reset
  10. if(reset) state <= B;
  11. else state <= next_state;
  12. end
  13. always @(*) begin // This is a combinational always block
  14. // State transition logic
  15. case(state)
  16. B: begin
  17. if(in == 1) next_state = B;
  18. else next_state = A;
  19. end
  20. A: begin
  21. if(in == 1) next_state = A;
  22. else next_state = B;
  23. end
  24. endcase
  25. end
  26. // Output logic
  27. // assign out = (state == ...);
  28. assign out = (state == A)?0:1;
  29. endmodule

Fsm2

This is a Moore state machine with two states, two inputs, and one output. Implement this state machine.

This exercise is the same as fsm2s, but using asynchronous reset.

Module Declaration


  
  1. module top_module(
  2. input clk,
  3. input areset, // Asynchronous reset to OFF
  4. input j,
  5. input k,
  6. output out);

  
  1. module top_module(
  2. input clk,
  3. input areset, // Asynchronous reset to OFF
  4. input j,
  5. input k,
  6. output out); //
  7. parameter OFF=0, ON=1;
  8. reg state, next_state;
  9. always @(*) begin
  10. // State transition logic
  11. case(state)
  12. OFF: begin
  13. if(j == 1) next_state = ON;
  14. else next_state = OFF;
  15. end
  16. ON: begin
  17. if(k == 1) next_state = OFF;
  18. else next_state = ON;
  19. end
  20. endcase
  21. end
  22. always @(posedge clk, posedge areset) begin
  23. // State flip-flops with asynchronous reset
  24. if(areset) state <= OFF;
  25. else state <= next_state;
  26. end
  27. // Output logic
  28. // assign out = (state == ...);
  29. assign out = (state == ON)? 1 : 0;
  30. endmodule

Fsm3comb

The following is the state transition table for a Moore state machine with one input, one output, and four states. Use the following state encoding: A=2'b00, B=2'b01, C=2'b10, D=2'b11.

Implement only the state transition logic and output logic (the combinational logic portion) for this state machine. Given the current state (state), compute the next_state and output (out) based on the state transition table.

State Next state Output
in=0 in=1
A A B 0
B C B 0
C A D 0
D C B 1

Module Declaration


  
  1. module top_module(
  2. input in,
  3. input [1:0] state,
  4. output [1:0] next_state,
  5. output out);

本题和写全状态机没什么区别,只不过少了一个always时序逻辑块,当前状态由顶层模块给出,作为输入,下一个状态作为输出。


  
  1. module top_module(
  2. input in,
  3. input [1:0] state,
  4. output [1:0] next_state,
  5. output out); //
  6. parameter A=0, B=1, C=2, D=3;
  7. // State transition logic: next_state = f(state, in)
  8. always@(*) begin
  9. case(state)
  10. A: begin
  11. if(in == 0) next_state = A;
  12. else next_state = B;
  13. end
  14. B: begin
  15. if(in == 0) next_state = C;
  16. else next_state = B;
  17. end
  18. C: begin
  19. if(in == 0) next_state = A;
  20. else next_state = D;
  21. end
  22. D: begin
  23. if(in == 0) next_state = C;
  24. else next_state = B;
  25. end
  26. endcase
  27. end
  28. // Output logic: out = f(state) for a Moore state machine
  29. assign out = (state == D) ? 1:0;
  30. endmodule

 

 

 

文章来源: reborn.blog.csdn.net,作者:李锐博恩,版权归原作者所有,如需转载,请联系作者。

原文链接:reborn.blog.csdn.net/article/details/103297473

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。