pid_core_tb.v 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. `timescale 1ns/1ps
  2. module pid_core_tb;
  3. reg clk;
  4. reg rst_n;
  5. reg sample_tick;
  6. reg signed [17:0] error_in;
  7. wire signed [17:0] ctrl_out;
  8. wire done;
  9. wire busy;
  10. pid_core #(
  11. .DW(18),
  12. .A0(18'sd6),
  13. .A1(-18'sd2),
  14. .A2(18'sd1),
  15. .OUT_MIN(18'sd0),
  16. .OUT_MAX(18'sd127)
  17. ) dut (
  18. .clk(clk),
  19. .rst_n(rst_n),
  20. .sample_tick(sample_tick),
  21. .error_in(error_in),
  22. .ctrl_out(ctrl_out),
  23. .done(done),
  24. .busy(busy)
  25. );
  26. initial begin
  27. clk = 1'b0;
  28. forever #5 clk = ~clk;
  29. end
  30. task sample_once;
  31. input signed [17:0] err;
  32. begin
  33. @(negedge clk);
  34. error_in = err;
  35. sample_tick = 1'b1;
  36. @(negedge clk);
  37. sample_tick = 1'b0;
  38. wait(done == 1'b1);
  39. @(negedge clk);
  40. end
  41. endtask
  42. task expect_ctrl;
  43. input signed [17:0] expected;
  44. begin
  45. if (ctrl_out !== expected) begin
  46. $display("expected ctrl_out=%0d actual=%0d", expected, ctrl_out);
  47. $fatal;
  48. end
  49. end
  50. endtask
  51. integer i;
  52. initial begin
  53. rst_n = 1'b0;
  54. sample_tick = 1'b0;
  55. error_in = 18'sd0;
  56. repeat (4) @(negedge clk);
  57. rst_n = 1'b1;
  58. sample_once(18'sd10);
  59. expect_ctrl(18'sd60);
  60. sample_once(18'sd10);
  61. expect_ctrl(18'sd100);
  62. sample_once(18'sd10);
  63. expect_ctrl(18'sd127);
  64. for (i = 0; i < 20; i = i + 1) begin
  65. sample_once(-18'sd10);
  66. end
  67. expect_ctrl(18'sd0);
  68. sample_once(18'sd0);
  69. expect_ctrl(18'sd10);
  70. rst_n = 1'b0;
  71. repeat (4) @(negedge clk);
  72. rst_n = 1'b1;
  73. sample_once(18'sd0);
  74. expect_ctrl(18'sd0);
  75. $display("PASS: pid_core_tb");
  76. $finish;
  77. end
  78. endmodule