| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279 |
- `timescale 1ns/1ps
- module pid_contract_integration_tb;
- reg clk;
- reg rst_n;
- reg sensor_valid;
- reg sensor_fault;
- reg [4:0] sensor_channel;
- reg signed [17:0] sensor_temp_q;
- reg [15:0] sensor_sample_seq;
- reg host_enable;
- reg signed [17:0] host_target_q;
- reg signed [17:0] host_a0;
- reg signed [17:0] host_a1;
- reg signed [17:0] host_a2;
- reg [1:0] host_mode;
- reg [15:0] host_config_seq;
- wire sensor_ready;
- wire pid_valid;
- wire [4:0] pid_channel;
- wire signed [17:0] pid_error_q;
- wire signed [17:0] pid_ctrl_q;
- wire pid_done;
- wire pid_fault;
- wire [15:0] pid_sample_seq;
- wire signed [17:0] pid_ctrl_prev_q;
- wire signed [17:0] pid_delta_q;
- wire pid_clamped;
- wire pid_history_cleared;
- wire [1:0] pid_state;
- wire [2:0] pid_cycle_phase;
- wire pid_busy;
- wire clear_history_all = 1'b0;
- wire act_enable;
- wire [4:0] act_channel;
- wire [17:0] act_duty_or_level;
- wire act_force_off;
- wire [1:0] act_reason;
- pid_24ch_scheduler #(
- .DW(18),
- .CHW(5),
- .CHANNEL_COUNT(24),
- .OUT_MIN(18'sd0),
- .OUT_MAX(18'sd127)
- ) u_pid_24ch_scheduler (
- .clk(clk),
- .rst_n(rst_n),
- .sensor_valid(sensor_valid),
- .sensor_ready(sensor_ready),
- .sensor_fault(sensor_fault),
- .sensor_channel(sensor_channel),
- .sensor_temp_q(sensor_temp_q),
- .sensor_sample_seq(sensor_sample_seq),
- .host_enable(host_enable),
- .host_target_q(host_target_q),
- .host_a0(host_a0),
- .host_a1(host_a1),
- .host_a2(host_a2),
- .host_mode(host_mode),
- .host_config_seq(host_config_seq),
- .clear_history_all(clear_history_all),
- .pid_valid(pid_valid),
- .pid_channel(pid_channel),
- .pid_error_q(pid_error_q),
- .pid_ctrl_q(pid_ctrl_q),
- .pid_done(pid_done),
- .pid_fault(pid_fault),
- .pid_sample_seq(pid_sample_seq),
- .pid_ctrl_prev_q(pid_ctrl_prev_q),
- .pid_delta_q(pid_delta_q),
- .pid_clamped(pid_clamped),
- .pid_history_cleared(pid_history_cleared),
- .pid_state(pid_state),
- .pid_cycle_phase(pid_cycle_phase),
- .busy(pid_busy)
- );
- actuator_sink u_actuator_sink (
- .clk(clk),
- .rst_n(rst_n),
- .host_enable(host_enable),
- .pid_valid(pid_valid),
- .pid_channel(pid_channel),
- .pid_ctrl_q(pid_ctrl_q),
- .pid_fault(pid_fault),
- .act_enable(act_enable),
- .act_channel(act_channel),
- .act_duty_or_level(act_duty_or_level),
- .act_force_off(act_force_off),
- .act_reason(act_reason)
- );
- initial begin
- clk = 1'b0;
- forever #5 clk = ~clk;
- end
- task send_sample;
- input [4:0] channel;
- input signed [17:0] temp_q;
- input fault;
- begin
- @(negedge clk);
- sensor_channel = channel;
- sensor_temp_q = temp_q;
- sensor_fault = fault;
- sensor_sample_seq = sensor_sample_seq + 16'd1;
- sensor_valid = 1'b1;
- @(negedge clk);
- sensor_valid = 1'b0;
- wait(pid_done == 1'b1);
- @(posedge clk);
- @(negedge clk);
- end
- endtask
- task send_sample_while_busy;
- input [4:0] channel;
- input signed [17:0] temp_q;
- input fault;
- begin
- @(negedge clk);
- sensor_channel = 5'd7;
- sensor_temp_q = 18'sd90;
- sensor_fault = 1'b0;
- sensor_sample_seq = sensor_sample_seq + 16'd1;
- sensor_valid = 1'b1;
- @(negedge clk);
- sensor_channel = channel;
- sensor_temp_q = temp_q;
- sensor_fault = fault;
- sensor_sample_seq = sensor_sample_seq + 16'd1;
- expect_ready(1'b0);
- repeat (2) @(negedge clk);
- expect_ready(1'b1);
- expect_actuator(1'b1, 1'b0, 2'd0);
- expect_pid(5'd7, 18'sd10, 18'sd60, 1'b0);
- @(negedge clk);
- sensor_valid = 1'b0;
- wait(pid_done == 1'b1);
- @(posedge clk);
- @(negedge clk);
- end
- endtask
- task expect_ready;
- input expected_ready;
- begin
- if (sensor_ready !== expected_ready) begin
- $display("sensor_ready mismatch ready=%0b expected=%0b", sensor_ready, expected_ready);
- $fatal;
- end
- end
- endtask
- task expect_actuator;
- input expected_enable;
- input expected_force_off;
- input [1:0] expected_reason;
- begin
- if (act_enable !== expected_enable || act_force_off !== expected_force_off || act_reason !== expected_reason) begin
- $display("actuator mismatch enable=%0b force_off=%0b reason=%0d", act_enable, act_force_off, act_reason);
- $fatal;
- end
- end
- endtask
- task expect_pid;
- input [4:0] expected_channel;
- input signed [17:0] expected_error_q;
- input signed [17:0] expected_ctrl_q;
- input expected_fault;
- begin
- if (pid_channel !== expected_channel || pid_error_q !== expected_error_q ||
- pid_ctrl_q !== expected_ctrl_q || pid_fault !== expected_fault) begin
- $display(
- "pid mismatch ch=%0d err=%0d ctrl=%0d fault=%0b expected ch=%0d err=%0d ctrl=%0d fault=%0b",
- pid_channel,
- pid_error_q,
- pid_ctrl_q,
- pid_fault,
- expected_channel,
- expected_error_q,
- expected_ctrl_q,
- expected_fault
- );
- $fatal;
- end
- end
- endtask
- initial begin
- rst_n = 1'b0;
- sensor_valid = 1'b0;
- sensor_fault = 1'b0;
- sensor_channel = 5'd0;
- sensor_temp_q = 18'sd0;
- sensor_sample_seq = 16'd0;
- host_enable = 1'b1;
- host_target_q = 18'sd100;
- host_a0 = 18'sd6;
- host_a1 = -18'sd2;
- host_a2 = 18'sd1;
- host_mode = 2'd0;
- host_config_seq = 16'd1;
- repeat (4) @(negedge clk);
- rst_n = 1'b1;
- expect_ready(1'b1);
- send_sample(5'd0, 18'sd80, 1'b0);
- expect_actuator(1'b1, 1'b0, 2'd0);
- expect_pid(5'd0, 18'sd20, 18'sd120, 1'b0);
- expect_ready(1'b1);
- send_sample(5'd1, 18'sd95, 1'b0);
- expect_actuator(1'b1, 1'b0, 2'd0);
- expect_pid(5'd1, 18'sd5, 18'sd30, 1'b0);
- send_sample(5'd0, 18'sd80, 1'b0);
- expect_actuator(1'b1, 1'b0, 2'd0);
- expect_pid(5'd0, 18'sd20, 18'sd127, 1'b0);
- send_sample(5'd23, 18'sd114, 1'b0);
- expect_actuator(1'b1, 1'b0, 2'd0);
- expect_pid(5'd23, -18'sd14, 18'sd0, 1'b0);
- send_sample(5'd3, 18'sd0, 1'b1);
- expect_actuator(1'b0, 1'b1, 2'd2);
- expect_pid(5'd3, 18'sd100, 18'sd0, 1'b1);
- send_sample(5'd24, 18'sd88, 1'b0);
- expect_actuator(1'b0, 1'b1, 2'd3);
- expect_pid(5'd24, 18'sd12, 18'sd0, 1'b1);
- send_sample(5'd4, 18'sd80, 1'b0);
- expect_actuator(1'b1, 1'b0, 2'd0);
- expect_pid(5'd4, 18'sd20, 18'sd120, 1'b0);
- host_enable = 1'b0;
- send_sample(5'd4, 18'sd80, 1'b0);
- expect_actuator(1'b0, 1'b1, 2'd1);
- expect_pid(5'd4, 18'sd20, 18'sd0, 1'b0);
- // host disable clears channel history before the next enable.
- host_enable = 1'b1;
- send_sample(5'd4, 18'sd80, 1'b0);
- expect_actuator(1'b1, 1'b0, 2'd0);
- expect_pid(5'd4, 18'sd20, 18'sd120, 1'b0);
- // sensor fault clears channel history before the next valid sample.
- send_sample(5'd5, 18'sd90, 1'b0);
- expect_actuator(1'b1, 1'b0, 2'd0);
- expect_pid(5'd5, 18'sd10, 18'sd60, 1'b0);
- send_sample(5'd5, 18'sd90, 1'b0);
- expect_actuator(1'b1, 1'b0, 2'd0);
- expect_pid(5'd5, 18'sd10, 18'sd100, 1'b0);
- send_sample(5'd5, 18'sd0, 1'b1);
- expect_actuator(1'b0, 1'b1, 2'd2);
- expect_pid(5'd5, 18'sd100, 18'sd0, 1'b1);
- send_sample(5'd5, 18'sd90, 1'b0);
- expect_actuator(1'b1, 1'b0, 2'd0);
- expect_pid(5'd5, 18'sd10, 18'sd60, 1'b0);
- // sensor_valid must be held while busy and accepted when ready returns.
- send_sample_while_busy(5'd6, 18'sd90, 1'b0);
- expect_actuator(1'b1, 1'b0, 2'd0);
- expect_pid(5'd6, 18'sd10, 18'sd60, 1'b0);
- $display("PASS: pid_contract_integration_tb");
- $finish;
- end
- endmodule
|