| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- `timescale 1ns/1ps
- module pid_core_tb;
- reg clk;
- reg rst_n;
- reg sample_tick;
- reg signed [17:0] error_in;
- wire signed [17:0] ctrl_out;
- wire done;
- wire busy;
- pid_core #(
- .DW(18),
- .A0(18'sd6),
- .A1(-18'sd2),
- .A2(18'sd1),
- .OUT_MIN(18'sd0),
- .OUT_MAX(18'sd127)
- ) dut (
- .clk(clk),
- .rst_n(rst_n),
- .sample_tick(sample_tick),
- .error_in(error_in),
- .ctrl_out(ctrl_out),
- .done(done),
- .busy(busy)
- );
- initial begin
- clk = 1'b0;
- forever #5 clk = ~clk;
- end
- task sample_once;
- input signed [17:0] err;
- begin
- @(negedge clk);
- error_in = err;
- sample_tick = 1'b1;
- @(negedge clk);
- sample_tick = 1'b0;
- wait(done == 1'b1);
- @(negedge clk);
- end
- endtask
- task expect_ctrl;
- input signed [17:0] expected;
- begin
- if (ctrl_out !== expected) begin
- $display("expected ctrl_out=%0d actual=%0d", expected, ctrl_out);
- $fatal;
- end
- end
- endtask
- integer i;
- initial begin
- rst_n = 1'b0;
- sample_tick = 1'b0;
- error_in = 18'sd0;
- repeat (4) @(negedge clk);
- rst_n = 1'b1;
- sample_once(18'sd10);
- expect_ctrl(18'sd60);
- sample_once(18'sd10);
- expect_ctrl(18'sd100);
- sample_once(18'sd10);
- expect_ctrl(18'sd127);
- for (i = 0; i < 20; i = i + 1) begin
- sample_once(-18'sd10);
- end
- expect_ctrl(18'sd0);
- sample_once(18'sd0);
- expect_ctrl(18'sd10);
- rst_n = 1'b0;
- repeat (4) @(negedge clk);
- rst_n = 1'b1;
- sample_once(18'sd0);
- expect_ctrl(18'sd0);
- $display("PASS: pid_core_tb");
- $finish;
- end
- endmodule
|