当前位置:   article > 正文

自动贩卖机的Verilog代码设计以及测试_verilog自动售货机

verilog自动售货机

1.问题

设计一个自动贩卖机,设商品售价2.5元,可使用5角和一元硬币,有找零功能。

2.功能框图和接口定义

 clk: 时钟输入;reset:复位信号;half:投入五角硬币;one:投入一元硬币;out:机器弹出商品;cout:机器找零。

3.状态转换图(基于摩尔型状态机)

 4.RTL代码编写

  1. module sell (
  2. input one,
  3. input half,
  4. input clk,
  5. input rst_n,
  6. output reg out,
  7. output reg cout
  8. );
  9. reg [2:0] curr_state, next_state;
  10. parameter S0=3'd0,
  11. S1=3'd1,
  12. S2=3'd2,
  13. S3=3'd3,
  14. S4=3'd4,
  15. S5=3'd5,
  16. S6=3'd6;
  17. always @(posedge clk or negedge rst_n) begin
  18. if(!rst_n)
  19. curr_state<=S0;
  20. else
  21. curr_state<=next_state;
  22. end
  23. always @(*) begin
  24. case (curr_state)
  25. S0:begin
  26. if(half)
  27. next_state=S1;
  28. else if (one)
  29. next_state=S2;
  30. else
  31. next_state=curr_state;
  32. end
  33. S1:begin
  34. if(half)
  35. next_state=S2;
  36. else if (one)
  37. next_state=S3;
  38. else
  39. next_state=curr_state;
  40. end
  41. S2:begin
  42. if(half)
  43. next_state=S3;
  44. else if (one)
  45. next_state=S4;
  46. else
  47. next_state=curr_state;
  48. end
  49. S3:begin
  50. if(half)
  51. next_state=S4;
  52. else if (one)
  53. next_state=S5;
  54. else
  55. next_state=curr_state;
  56. end
  57. S4:begin
  58. if(half)
  59. next_state=S5;
  60. else if (one)
  61. next_state=S6;
  62. else
  63. next_state=curr_state;
  64. end
  65. S5: next_state=S0;
  66. S6: next_state=S0;
  67. default: next_state=S0;
  68. endcase
  69. end
  70. always @(*) begin
  71. if(!rst_n)begin
  72. out<=0;
  73. cout<=0;
  74. end
  75. else if(curr_state==S5) begin
  76. out<=1;
  77. cout<=0;
  78. end
  79. else if(curr_state==S6) begin
  80. out<=1;
  81. cout<=1;
  82. end
  83. else begin
  84. out<=0;
  85. cout<=0;
  86. end
  87. end
  88. endmodule

 5.测试代码编写

本测试仅仿真了两个购买情景

测试情景1:S0→S2→S4→S6;即连续投了三次一元钱,机器弹出饮料并找零(out=1,cout=1)。

测试情景2:S0→S1→S3→S5;即先投了五角钱,然后又投了一元钱,最后又投了一元钱,机器弹出饮料不找零(out=1,cout=0)。

  1. module sell_tb();
  2. logic one,half,clk,rst_n;
  3. logic cout,out;
  4. sell u1(.clk(clk),.rst_n(rst_n),.half(half),.one(one),.cout(cout),.out(out) );
  5. initial begin
  6. clk=0;
  7. rst_n<=0;
  8. #40;
  9. rst_n<=1;
  10. end
  11. always #10 clk=~clk;
  12. initial begin
  13. half<=0;
  14. one<=0;
  15. #50;
  16. half<=0;
  17. one<=1;
  18. #20;
  19. half<=0;
  20. one<=1;
  21. #20;
  22. half<=0;
  23. one<=1;
  24. #20;//测试场景1cout=1,out=1
  25. half<=0;
  26. one<=0;
  27. #50;
  28. half<=1;
  29. one<=0;
  30. #20;
  31. half<=0;
  32. one<=1;
  33. #20;
  34. half<=0;
  35. one<=1;
  36. #50; //测试场景2cout=0,out=1;
  37. $finish;
  38. end
  39. initial
  40. begin
  41. $fsdbDumpfile("test.fsdb");
  42. $fsdbDumpvars(0,sell_tb);
  43. end
  44. endmodule

6.仿真波形

仿真工具:VCS、Verdi

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

闽ICP备14008679号