当前位置:   article > 正文

HDLBits 系列(10)——Building Larger Circuits_build a four-bit shift register that also acts as

build a four-bit shift register that also acts as a down counter. data is sh

目录

3.3  Building Larger Circuits

1 . Counter with period 1000

2. 4-bit shift register and down counter

3. FSM: Sequence 1101 recognizer

4. FSM: Enable shift register

 5. FSM: The complete FSM

6. The complete timer

7. FSM: One-hot logic equations


3.3  Building Larger Circuits

1 . Counter with period 1000

Build a counter that counts from 0 to 999, inclusive, with a period of 1000 cycles. The reset input is synchronous, and should reset the counter to 0.

  1. module top_module (
  2. input clk,
  3. input reset,
  4. output [9:0] q);
  5. always @(posedge clk)begin
  6. if(reset) q<=10'd0;
  7. else if(q=='d999) q<=10'd0;
  8. else q<=q+1'b1;
  9. end
  10. endmodule

2. 4-bit shift register and down counter

Build a four-bit shift register that also acts as a down counter. Data is shifted in most-significant-bit first when shift_ena is 1. The number currently in the shift register is decremented when count_ena is 1. Since the full system doesn't ever use shift_ena and count_ena together, it does not matter what your circuit does if both control inputs are 1 (This mainly means that it doesn't matter which case gets higher priority).

shift_ena 为1的时候,接收数据往左移, count_ena 为1的时候,数据递减,其他时刻保持不变。不考虑这两个使能信号的优先级,选择case语句。

  1. module top_module (
  2. input clk,
  3. input shift_ena,
  4. input count_ena,
  5. input data,
  6. output reg[3:0] q);
  7. always @(posedge clk)begin
  8. case({shift_ena,count_ena})
  9. 2'b00:q<=q;
  10. 2'b01:q<=q-1'b1;
  11. 2'b10:q<={q[2:0],data};
  12. 2'b11:q<=q;
  13. endcase
  14. end
  15. endmodule

3. FSM: Sequence 1101 recognizer

Build a finite-state machine that searches for the sequence 1101 in an input bit stream. When the sequence is found, it should set start_shiftingto 1, forever, until reset. Getting stuck in the final state is intended to model going to other states in a bigger FSM that is not yet implemented. We will be extending this FSM in the next few exercises.

序列检测:把状态转移图画出来就好写代码了。

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

4. FSM: Enable shift register

As part of the FSM for controlling the shift register, we want the ability to enable the shift register for exactly 4 clock cycles whenever the proper bit pattern is detected. We handle sequence detection in Exams/review2015_fsmseq, so this portion of the FSM only handles enabling the shift register for 4 cycles.

Whenever the FSM is reset, assert shift_ena for 4 cycles, then 0 forever (until reset).

复位信号有效后,shift_ena拉高持续4个时钟周期,其他时刻拉低。

  1. module top_module (
  2. input clk,
  3. input reset, // Synchronous reset
  4. output shift_ena);
  5. reg [2:0]cnt;
  6. always @(posedge clk)begin
  7. if(reset)begin
  8. cnt<='d0;
  9. shift_ena<=1'b1;
  10. end
  11. else if(shift_ena==1'b1 && cnt==3'd3)begin
  12. shift_ena<=1'b0;
  13. cnt<='d0;
  14. end
  15. else begin
  16. cnt<=cnt+1'b1;
  17. end
  18. end
  19. endmodule

 5. FSM: The complete FSM

You may wish to do FSM: Enable shift register and FSM: Sequence recognizer first.

We want to create a timer that:

  1. is started when a particular pattern (1101) is detected,
  2. shifts in 4 more bits to determine the duration to delay,
  3. waits for the counters to finish counting, and
  4. notifies the user and waits for the user to acknowledge the timer.

In this problem, implement just the finite-state machine that controls the timer. The data path (counters and some comparators) are not included here.

The serial data is available on the data input pin. When the pattern 1101 is received, the state machine must then assert output shift_ena for exactly 4 clock cycles.

After that, the state machine asserts its counting output to indicate it is waiting for the counters, and waits until input done_counting is high.

At that point, the state machine must assert done to notify the user the timer has timed out, and waits until input ack is 1 before being reset to look for the next occurrence of the start sequence (1101).

The state machine should reset into a state where it begins searching for the input sequence 1101.

Here is an example of the expected inputs and outputs. The 'x' states may be slightly confusing to read. They indicate that the FSM should not care about that particular input signal in that cycle. For example, once a 1101 pattern is detected, the FSM no longer looks at the data input until it resumes searching after everything else is done.

状态转移图:

 参考时序图:

  1. module top_module (
  2. input clk,
  3. input reset, // Synchronous reset
  4. input data,
  5. output shift_ena,
  6. output counting,
  7. input done_counting,
  8. output done,
  9. input ack );
  10. parameter S=0,S1=1,S11=2,S110=3;
  11. parameter B0=4,B1=5,B2=6,B3=7,COUNT=8,WAIT=9;
  12. reg [3:0] state,next_state;
  13. reg [3:0]cnt;
  14. always @(*)begin
  15. case(state)
  16. S:begin
  17. if(data) next_state=S1;
  18. else next_state=S;
  19. end
  20. S1:begin
  21. if(data) next_state=S11;
  22. else next_state=S;
  23. end
  24. S11:begin
  25. if(data) next_state=S11;
  26. else next_state=S110;
  27. end
  28. S110:begin
  29. if(data) next_state=B0;
  30. else next_state=S;
  31. end
  32. B0:begin
  33. next_state=B1;
  34. end
  35. B1:begin
  36. next_state=B2;
  37. end
  38. B2:begin
  39. next_state=B3;
  40. end
  41. B3:begin
  42. next_state=COUNT;
  43. end
  44. COUNT:begin
  45. if(done_counting) next_state=WAIT;
  46. else next_state=COUNT;
  47. end
  48. WAIT:begin
  49. if(ack)next_state=S;
  50. else next_state=WAIT;
  51. end
  52. default:begin
  53. next_state=S;
  54. end
  55. endcase
  56. end
  57. always @(posedge clk)begin
  58. if(reset) state<=S;
  59. else state<=next_state;
  60. end
  61. assign shift_ena=(state==B0|state==B1|state==B2|state==B3);
  62. assign counting=(state==COUNT);
  63. assign done=(state==WAIT);
  64. endmodule

6. The complete timer

We want to create a timer with one input that:

  1. is started when a particular input pattern (1101) is detected,
  2. shifts in 4 more bits to determine the duration to delay,
  3. waits for the counters to finish counting, and
  4. notifies the user and waits for the user to acknowledge the timer.

The serial data is available on the data input pin. When the pattern 1101 is received, the circuit must then shift in the next 4 bits, most-significant-bit first. These 4 bits determine the duration of the timer delay. I'll refer to this as the delay[3:0].

After that, the state machine asserts its counting output to indicate it is counting. The state machine must count for exactly (delay[3:0] + 1) * 1000 clock cycles. e.g., delay=0 means count 1000 cycles, and delay=5 means count 6000 cycles. Also output the current remaining time. This should be equal to delay for 1000 cycles, then delay-1 for 1000 cycles, and so on until it is 0 for 1000 cycles. When the circuit isn't counting, the count[3:0] output is don't-care (whatever value is convenient for you to implement).

At that point, the circuit must assert done to notify the user the timer has timed out, and waits until input ack is 1 before being reset to look for the next occurrence of the start sequence (1101).

The circuit should reset into a state where it begins searching for the input sequence 1101.

Here is an example of the expected inputs and outputs. The 'x' states may be slightly confusing to read. They indicate that the FSM should not care about that particular input signal in that cycle. For example, once the 1101 and delay[3:0] have been read, the circuit no longer looks at the data input until it resumes searching after everything else is done. In this example, the circuit counts for 2000 clock cycles because the delay[3:0] value was 4'b0001. The last few cycles starts another count with delay[3:0] = 4'b1110, which will count for 15000 cycles.

在上题的基础上增加了计数时间数据的接收和计算,并检测计数结束信号触发状态转移。

  1. module top_module (
  2. input clk,
  3. input reset, // Synchronous reset
  4. input data,
  5. output [3:0] count,
  6. output counting,
  7. output done,
  8. input ack );
  9. parameter S=0,S1=1,S11=2,S110=3;
  10. parameter B0=4,B1=5,B2=6,B3=7,COUNT=8,WAIT=9;
  11. reg [3:0] state,next_state;
  12. reg [3:0] delay;
  13. reg [13:0]cnt_delay;
  14. reg done_counting=0;
  15. //状态机
  16. always @(*)begin
  17. case(state)
  18. S:begin
  19. if(data) next_state=S1;
  20. else next_state=S;
  21. end
  22. S1:begin
  23. if(data) next_state=S11;
  24. else next_state=S;
  25. end
  26. S11:begin
  27. if(data) next_state=S11;
  28. else next_state=S110;
  29. end
  30. S110:begin
  31. if(data) next_state=B0;
  32. else next_state=S;
  33. end
  34. B0:begin
  35. next_state=B1;
  36. end
  37. B1:begin
  38. next_state=B2;
  39. end
  40. B2:begin
  41. next_state=B3;
  42. end
  43. B3:begin
  44. next_state=COUNT;
  45. end
  46. COUNT:begin
  47. if(done_counting) next_state=WAIT;
  48. else next_state=COUNT;
  49. end
  50. WAIT:begin
  51. if(ack)next_state=S;
  52. else next_state=WAIT;
  53. end
  54. default:begin
  55. next_state=S;
  56. end
  57. endcase
  58. end
  59. always @(posedge clk)begin
  60. if(reset) state<=S;
  61. else state<=next_state;
  62. end
  63. //延时数据接收并存储
  64. always@(posedge clk)begin
  65. if(reset) delay<='d0;
  66. else begin
  67. case(state)
  68. B0:begin
  69. delay[3]<=data;
  70. end
  71. B1:begin
  72. delay[2]<=data;
  73. end
  74. B2:begin
  75. delay[1]<=data;
  76. end
  77. B3:begin
  78. delay[0]<=data;
  79. end
  80. default:begin
  81. delay<=delay;
  82. end
  83. endcase
  84. end
  85. end
  86. //done_counting==1'b1的条件
  87. always @(posedge clk)begin
  88. if(reset)begin
  89. cnt_delay<='d0;
  90. end
  91. else begin
  92. case(state)
  93. COUNT:begin
  94. cnt_delay<=cnt_delay+1'b1;
  95. end
  96. default:begin
  97. cnt_delay<='d0;
  98. end
  99. endcase
  100. end
  101. end
  102. assign done_counting = (cnt_delay == (delay + 1) * 1000 - 1);
  103. //out signals
  104. assign count = delay-cnt_delay/1000;
  105. assign counting=(state==COUNT);
  106. assign done=(state==WAIT);
  107. endmodule

7. FSM: One-hot logic equations

Given the following state machine with 3 inputs, 3 outputs, and 10 states:

 Derive next-state logic equations and output logic equations by inspection assuming the following one-hot encoding is used: (S, S1, S11, S110, B0, B1, B2, B3, Count, Wait) = (10'b0000000001, 10'b0000000010, 10'b0000000100, ... , 10'b1000000000)

Derive state transition and output logic equations by inspection assuming a one-hot encoding. Implement only the state transition logic and output logic (the combinational logic portion) for this state machine. (The testbench will test with non-one hot inputs to make sure you're not trying to do something more complicated).

Write code that generates the following equations:

  • B3_next -- next-state logic for state B1
  • S_next
  • S1_next
  • Count_next
  • Wait_next
  • done -- output logic
  • counting
  • shift_ena

根据状态转移的结果进行反推由来过程。

  1. module top_module(
  2. input d,
  3. input done_counting,
  4. input ack,
  5. input [9:0] state, // 10-bit one-hot current state
  6. output B3_next,
  7. output S_next,
  8. output S1_next,
  9. output Count_next,
  10. output Wait_next,
  11. output done,
  12. output counting,
  13. output shift_ena
  14. ); //
  15. // You may use these parameters to access state bits using e.g., state[B2] instead of state[6].
  16. parameter S=0, S1=1, S11=2, S110=3, B0=4, B1=5, B2=6, B3=7, Count=8, Wait=9;
  17. assign B3_next =state[B2];
  18. assign S_next =(~d&&state[S])|(~d&&state[S1])|(~d&&state[S110])|(ack&&state[Wait]);
  19. assign S1_next=d&&state[S];
  20. assign Count_next=state[B3]|(state[Count]&&~done_counting);
  21. assign Wait_next=(state[Count]&&done_counting)|(~ack&&state[Wait]);
  22. assign done=state[Wait];
  23. assign counting=state[Count];
  24. assign shift_ena=state[B0]|state[B1]|state[B2]|state[B3];
  25. endmodule

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/648839
推荐阅读
相关标签
  

闽ICP备14008679号