当前位置:   article > 正文

verilog基础设计3- 2022乐鑫数字ic提前批编程题_verilog如和找次小值

verilog如和找次小值

     我看有网友用了其它方法实现,今天就试了用状态机实现,可是统计次小值次数还没想好怎么弄,改天补充吧........

1、题目要求

使用Verilog/SV撰写如下功能模块;求输入信号序列din在din_vld为高电平的时间段内的次小值和次小值

出现的次数。接口信号如下,

module sec_min(
input clk, //时钟信号
input rst_n, //复位信号
input [9:0] din, //10bit无符号数
input din_vld, //输入数据有效信号
output [9:0] dout, //次小值
output [8:0] cnt //次小值出现的次数,溢出时重新计数
);

endmodule

2、根据题目要求编写程序,目前只实现了找出次小值、还没实现统计次小值次数,改天再来补充

  1. module sec_min_mod(
  2. input wire clk,
  3. input wire rst_n,
  4. input wire [9:0] din,//10bit number
  5. input wire din_vld, // valid
  6. output reg [9:0] dout,// 次小之
  7. output reg [8:0] cnt // 次小值次数
  8. );
  9. reg [9:0] din_reg0;
  10. reg [9:0] sec_min;
  11. reg [9:0] fir_min ;
  12. reg [1:0] din_vld_reg;
  13. wire din_neg_flag;
  14. reg [4:0] current_state;
  15. reg [4:0] next_state;
  16. parameter IDLE = 5'b00001,
  17. C_IDLE = 5'b00010,
  18. C0 = 5'b00100,
  19. C1 = 5'b01000,
  20. C2 = 5'b10000;
  21. always @(posedge clk or negedge rst_n) begin
  22. if (!rst_n)
  23. din_vld_reg <= 2'd0;
  24. else
  25. din_vld_reg <= {din_vld_reg[0],din_vld};
  26. end
  27. assign din_neg_flag = (din_vld_reg[1] && ~din_vld_reg[0])? 1'b1:1'b0;
  28. always @(posedge clk or negedge rst_n) begin
  29. if (!rst_n)
  30. din_reg0 <= 10'd0;
  31. else
  32. din_reg0 <= din;
  33. end
  34. always @(posedge clk or negedge rst_n)
  35. if(!rst_n)
  36. current_state <= IDLE;
  37. else
  38. current_state <= next_state;
  39. always @(*) begin
  40. case(current_state)
  41. IDLE: if(din_vld)
  42. next_state = C_IDLE;
  43. else
  44. next_state =IDLE;
  45. C_IDLE: next_state =C0;
  46. C0: next_state = C1;
  47. C1: if(din_neg_flag)
  48. next_state = C2;
  49. else
  50. next_state = C1;
  51. C2: next_state =IDLE;
  52. default: next_state =IDLE;
  53. endcase
  54. end
  55. always @(posedge clk or negedge rst_n) begin
  56. if (!rst_n) begin
  57. fir_min <= 10'd0;
  58. sec_min <= 10'd0;
  59. end
  60. else if (current_state == C0) begin
  61. if (din <= din_reg0) begin
  62. fir_min <= din;
  63. sec_min <= din_reg0;
  64. end
  65. else begin
  66. fir_min <= din_reg0;
  67. sec_min <= din;
  68. end
  69. end
  70. else if(current_state == C1) begin
  71. if( sec_min <= din) begin
  72. fir_min <= fir_min;
  73. sec_min <= sec_min;
  74. end
  75. else begin
  76. if(din >= fir_min) begin
  77. fir_min <= din;
  78. sec_min <= sec_min;
  79. end
  80. else begin
  81. fir_min <= din;
  82. sec_min <= fir_min;
  83. end
  84. end
  85. end
  86. else begin
  87. fir_min <= fir_min;
  88. sec_min <= sec_min;
  89. end
  90. end
  91. //输出次小值
  92. always @(posedge clk or negedge rst_n) begin
  93. if (!rst_n)
  94. dout <= 10'd0;
  95. else if (current_state == C2)
  96. dout <= sec_min;
  97. else
  98. dout <= dout;
  99. end
  100. endmodule

3、tb仿真

  1. `timescale 1ns/1ps
  2. module tb_expressif();
  3. reg clk ;
  4. reg rst_n;
  5. reg [9:0] din;
  6. reg din_vld;
  7. wire [9:0] dout;
  8. wire [8:0] cnt;
  9. integer i;
  10. initial begin
  11. clk =0;
  12. rst_n = 1'b0;
  13. din = 10'd0;
  14. din_vld = 1'b0;
  15. #100;
  16. rst_n =1;
  17. #20;
  18. din_vld = 1'b1;
  19. for(i=0;i< 20;i=i+1) begin
  20. @(posedge clk) begin
  21. din = {$random} % 5;
  22. end
  23. end
  24. din_vld =1'b0;
  25. #100;
  26. //$stop;
  27. end
  28. always #5 clk = ~clk;
  29. sec_min_mod sec_min_inst(
  30. .clk(clk),
  31. .rst_n(rst_n),
  32. .din(din),//10bit number
  33. .din_vld(din_vld), // valid
  34. .dout(dout),// 次小之
  35. .cnt(cnt) // 次小值次数
  36. );
  37. endmodule

4、modelsim 波形图

       根据波形所示,输入数据为0,1,2,3,4 ,最好dout正确输出次小值1,仿真正确,用状态机,统计次数还未想到怎么处理......就这样吧

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

闽ICP备14008679号