HDLBits 系列(32)Sequence recognition(序列检测)

举报
李锐博恩 发表于 2021/07/15 04:15:25 2021/07/15
【摘要】 目录 原题复现 审题 状态转移图 我的设计 原题复现 原题复现: Synchronous HDLC framing involves decoding a continuous bit stream of data to look for bit patterns that indicate the beginning and end of frames (p...

目录

原题复现

审题

状态转移图

我的设计


原题复现

原题复现:

Synchronous HDLC framing involves decoding a continuous bit stream of data to look for bit patterns that indicate the beginning and end of frames (packets). Seeing exactly 6 consecutive 1s (i.e., 01111110) is a "flag" that indicate frame boundaries. To avoid the data stream from accidentally containing "flags", the sender inserts a zero after every 5 consecutive 1s which the receiver must detect and discard. We also need to signal an error if there are 7 or more consecutive 1s.

Create a finite state machine to recognize these three sequences:

  • 0111110: Signal a bit needs to be discarded (disc).
  • 01111110: Flag the beginning/end of a frame (flag).
  • 01111111...: Error (7 or more 1s) (err).

When the FSM is reset, it should be in a state that behaves as though the previous input were 0.

Here are some example sequences that illustrate the desired operation.

审题

根据题目来看,这是一个序列识别的一个问题,如果识别到序列有连续5个1(即1111_10),则disc有效;

如果有连续6个1(即1111_110),则flag有效;

如果有连续7个1或者更多的1,则err有效。

状态转移图

就是这样一个问题,我们根据上述波形图来画出状态转移图:

我的设计

根据状态转移图,很容易得到设计:


  
  1. module top_module(
  2. input clk,
  3. input reset, // Synchronous reset
  4. input in,
  5. output disc,
  6. output flag,
  7. output err);
  8. localparam S0 = 0, S1 = 1, S2 = 2, S3 = 3, S4 = 4, S5 = 5, S6 = 6, DISC = 7, FLAG = 8, ERR = 9;
  9. reg [3:0] state, next_state;
  10. always@(*) begin
  11. case(state)
  12. S0: begin
  13. if(in) next_state = S1;
  14. else next_state = S0;
  15. end
  16. S1: begin
  17. if(in) next_state = S2;
  18. else next_state = S0;
  19. end
  20. S2: begin
  21. if(in) next_state = S3;
  22. else next_state = S0;
  23. end
  24. S3: begin
  25. if(in) next_state = S4;
  26. else next_state = S0;
  27. end
  28. S4: begin
  29. if(in) next_state = S5;
  30. else next_state = S0;
  31. end
  32. S5: begin
  33. if(in) next_state = S6;
  34. else next_state = DISC;
  35. end
  36. S6: begin
  37. if(in) next_state = ERR;
  38. else next_state = FLAG;
  39. end
  40. DISC: begin
  41. if(in) next_state = S1;
  42. else next_state = S0;
  43. end
  44. FLAG: begin
  45. if(in) next_state = S1;
  46. else next_state = S0;
  47. end
  48. ERR: begin
  49. if(in) next_state = ERR;
  50. else next_state = S0;
  51. end
  52. default: begin
  53. next_state = S0;
  54. end
  55. endcase
  56. end
  57. always@(posedge clk)begin
  58. if(reset) state <= S0;
  59. else state <= next_state;
  60. end
  61. assign disc = (state == DISC)?1:0;
  62. assign flag = (state == FLAG)?1:0;
  63. assign err = (state == ERR)?1:0;
  64. endmodule

测试成功!

这是序列检测的一个典型做法,校招可没少做这种题目,只是好像都还要比这个简单。

 

 

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

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

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

评论(0

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

全部回复

上滑加载中

设置昵称

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

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

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