HDLBits 系列(39)求解带有奇校验的串口接收数据的简化电路设计

举报
李锐博恩 发表于 2021/07/15 01:55:54 2021/07/15
【摘要】 目录 求助原题 我的方案 状态转移图 我的设计 等待你的方案? 求助原题 先给出原题:(蓝色字体,即是链接本身) We want to add parity checking to the serial receiver. Parity checking adds one extra bit after each data byte. We will use...

目录

求助原题

我的方案

状态转移图

我的设计

等待你的方案?


求助原题

先给出原题:(蓝色字体,即是链接本身)

We want to add parity checking to the serial receiver. Parity checking adds one extra bit after each data byte. We will use odd parity, where the number of 1s in the 9 bits received must be odd. For example, 101001011 satisfies odd parity (there are 5 1s), but 001001011 does not.

Change your FSM and datapath to perform odd parity checking. Assert the done signal only if a byte is correctly received and its parity check passes. Like the serial receiver FSM, this FSM needs to identify the start bit, wait for all 9 (data and parity) bits, then verify that the stop bit was correct. If the stop bit does not appear when expected, the FSM must wait until it finds a stop bit before attempting to receive the next byte.

You are provided with the following module that can be used to calculate the parity of the input stream (It's a TFF with reset). The intended use is that it should be given the input bit stream, and reset at appropriate times so it counts the number of 1 bits in each byte.


  
  1. module parity (
  2. input clk,
  3. input reset,
  4. input in,
  5. output odd);
  6. always @(posedge clk)
  7. if (reset) odd <= 0;
  8. else if (in) odd <= ~odd;
  9. endmodule

Note that the serial protocol sends the least significant bit first, and the parity bit after the 8 data bits.

我的方案

如何用状态机设计这个电路呢?

求助路过的同行尝试做下这个题?

如果能Success,希望能告知一下,感谢。

我已经把不带奇偶校验的设计给出,如下:

https://blog.csdn.net/Reborn_Lee/article/details/103438860

https://blog.csdn.net/Reborn_Lee/article/details/103439297

最后给出我的不成功的方案:

状态转移图

我的设计


  
  1. module top_module(
  2. input clk,
  3. input in,
  4. input reset, // Synchronous reset
  5. output [7:0] out_byte,
  6. output done
  7. ); //
  8. // Use FSM from Fsm_serial
  9. localparam START = 0, B1 = 1, B2 = 2, B3 = 3, B4 = 4, B5 = 5, B6 = 6, B7 = 7, B8 = 8, B9 = 9, STOP = 10, D0 = 11, D1 = 12, D2 = 13;
  10. reg [3:0] state, next_state;
  11. wire odd;
  12. wire en;
  13. reg [7:0] out_byte_mid;
  14. always@(*) begin
  15. case(state)
  16. START: begin
  17. if(~in) next_state = B1;
  18. else next_state = START;
  19. end
  20. B1: begin
  21. next_state = B2;
  22. end
  23. B2: begin
  24. next_state = B3;
  25. end
  26. B3: begin
  27. next_state = B4;
  28. end
  29. B4: begin
  30. next_state = B5;
  31. end
  32. B5: begin
  33. next_state = B6;
  34. end
  35. B6: begin
  36. next_state = B7;
  37. end
  38. B7: begin
  39. next_state = B8;
  40. end
  41. B8: begin
  42. next_state = B9;
  43. end
  44. B9: begin
  45. next_state = STOP;
  46. end
  47. STOP: begin
  48. if(in&&odd) next_state = D0;
  49. else if(in&&(~odd)) next_state = D2;
  50. else next_state = D1;
  51. end
  52. D0: begin
  53. if(in == 1) next_state = START;
  54. else next_state = B1;
  55. end
  56. D1: begin
  57. if(in == 0) next_state = D1;
  58. else next_state = START;
  59. end
  60. D2: begin
  61. if(in) next_state = D2;
  62. else next_state = B1;
  63. end
  64. default: begin
  65. next_state = START;
  66. end
  67. endcase
  68. end
  69. always@(posedge clk) begin
  70. if(reset) state <= START;
  71. else state <= next_state;
  72. end
  73. always@(*) begin
  74. case(state)
  75. START: begin
  76. ;
  77. end
  78. B1: begin
  79. out_byte_mid[0] = in;
  80. end
  81. B2: begin
  82. out_byte_mid[1] = in;
  83. end
  84. B3: begin
  85. out_byte_mid[2] = in;
  86. end
  87. B4: begin
  88. out_byte_mid[3] = in;
  89. end
  90. B5: begin
  91. out_byte_mid[4] = in;
  92. end
  93. B6: begin
  94. out_byte_mid[5] = in;
  95. end
  96. B7: begin
  97. out_byte_mid[6] = in;
  98. end
  99. B8: begin
  100. out_byte_mid[7] = in;
  101. end
  102. B9: begin
  103. ;
  104. end
  105. STOP: begin
  106. ;
  107. end
  108. D0: begin
  109. ;
  110. end
  111. D1: begin
  112. ;
  113. end
  114. D2: begin
  115. ;
  116. end
  117. default: begin
  118. ;
  119. end
  120. endcase
  121. end
  122. assign done = (state == D0) ? 1 : 0;
  123. assign en = (state == B1)||(state == B2)||(state == B3)||(state == B4) ||(state == B5)||(state == B6)||(state == B7)||(state == B8)||(state == B9) ? 1: 0;
  124. assign out_byte = done ? out_byte_mid : 8'b0;
  125. // New: Add parity checking.
  126. parity inst_parity(
  127. .clk(clk),
  128. .reset(reset),
  129. .in(in&en),
  130. //.en(en),
  131. .odd(odd)
  132. );
  133. endmodule

等待你的方案?

20200107更新,下面是群里的一位老哥的答案,十分感谢,还没来得及看,先放这里,以免忘记。


  
  1. module top_module(
  2. input clk,
  3. input in,
  4. input reset, // Synchronous reset
  5. output [7:0] out_byte,
  6. output done
  7. );
  8. reg odd;
  9. reg[4:0] state,nstate;
  10. parameter start=0,d0=1,d1=2,d2=3,d3=4,d4=5,d5=6,d6=7,d7=8,stop=9,done1=10,done2=11,w=13,p=12;
  11. always@(*)
  12. begin
  13. case(state)
  14. start:begin if(in==0)nstate=d0;else nstate=start;end
  15. d0:begin nstate=d1;out_byte[0]=in;end
  16. d1:begin nstate=d2;out_byte[1]=in;end
  17. d2:begin nstate=d3;out_byte[2]=in;end
  18. d3:begin nstate=d4;out_byte[3]=in;end
  19. d4:begin nstate=d5;out_byte[4]=in;end
  20. d5:begin nstate=d6;out_byte[5]=in;end
  21. d6:begin nstate=d7;out_byte[6]=in;end
  22. d7:begin nstate=p;out_byte[7]=in;end
  23. p: begin nstate=stop; odd=^out_byte[7:0]^in; end
  24. stop: begin if(in==1) nstate=done1;else nstate=w; end
  25. done1:begin if(in==1) nstate=start;else nstate=d0; end
  26. w:begin if(in==1) nstate=start;else nstate=w; end
  27. endcase
  28. end
  29. always@(posedge clk)
  30. begin
  31. if(reset==1)
  32. state<=start;
  33. else
  34. state<=nstate;
  35. end
  36. assign done=(state==done1&&odd)?1:0;
  37. endmodule

 

 

 

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

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

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

评论(0

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

全部回复

上滑加载中

设置昵称

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

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

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