| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- `timescale 1ns/1ps
- module ptc_output_bank_tb;
- reg clk;
- reg rst_n;
- reg pid_valid;
- reg [4:0] pid_channel;
- reg signed [17:0] pid_ctrl_q;
- reg pid_fault;
- reg global_enable;
- reg fault_latched;
- reg [3:0] fault_code;
- wire ptc_enable;
- wire [4:0] ptc_channel;
- wire [17:0] ptc_duty_or_level;
- wire ptc_force_off;
- wire [3:0] ptc_reason;
- wire [23:0] ptc_bank_enable;
- wire [23:0] ptc_bank_force_off;
- wire [24*18-1:0] ptc_bank_duty_or_level;
- wire [23:0] ptc_pwm_out;
- integer high_count;
- integer sample_idx;
- ptc_output_bank #(
- .PWM_PERIOD_TICKS(127),
- .PWM_COUNTER_WIDTH(7)
- ) dut (
- .clk(clk),
- .rst_n(rst_n),
- .pid_valid(pid_valid),
- .pid_channel(pid_channel),
- .pid_ctrl_q(pid_ctrl_q),
- .pid_fault(pid_fault),
- .global_enable(global_enable),
- .fault_latched(fault_latched),
- .fault_code(fault_code),
- .ptc_enable(ptc_enable),
- .ptc_channel(ptc_channel),
- .ptc_duty_or_level(ptc_duty_or_level),
- .ptc_force_off(ptc_force_off),
- .ptc_reason(ptc_reason),
- .ptc_bank_enable(ptc_bank_enable),
- .ptc_bank_force_off(ptc_bank_force_off),
- .ptc_bank_duty_or_level(ptc_bank_duty_or_level),
- .ptc_pwm_out(ptc_pwm_out)
- );
- initial begin
- clk = 1'b0;
- forever #5 clk = ~clk;
- end
- task pulse_pid;
- input [4:0] channel;
- input signed [17:0] ctrl;
- begin
- pid_channel = channel;
- pid_ctrl_q = ctrl;
- pid_valid = 1'b1;
- @(negedge clk);
- pid_valid = 1'b0;
- @(negedge clk);
- end
- endtask
- task wait_cycles;
- input integer cycles;
- integer n;
- begin
- for (n = 0; n < cycles; n = n + 1) begin
- @(negedge clk);
- end
- end
- endtask
- task wait_pwm_rollover;
- begin
- while (dut.pwm_counter != 0) begin
- @(negedge clk);
- end
- end
- endtask
- initial begin
- rst_n = 1'b0;
- pid_valid = 1'b0;
- pid_channel = 5'd0;
- pid_ctrl_q = 18'sd0;
- pid_fault = 1'b0;
- global_enable = 1'b1;
- fault_latched = 1'b0;
- fault_code = 4'd0;
- repeat (4) @(negedge clk);
- rst_n = 1'b1;
- if (ptc_pwm_out !== 24'd0) begin
- $display("ptc pwm outputs must reset off");
- $fatal;
- end
- pulse_pid(5'd5, 18'sd200);
- if (!ptc_enable || ptc_duty_or_level !== 18'd127 || !ptc_bank_enable[5] ||
- ptc_bank_duty_or_level[(5*18) +: 18] !== 18'd127) begin
- $display("ptc output bank should clamp and store channel duty");
- $fatal;
- end
- if (!ptc_pwm_out[5]) begin
- $display("ptc pwm at 100 percent should request external switch on");
- $fatal;
- end
- pulse_pid(5'd6, 18'sd64);
- wait_pwm_rollover();
- high_count = 0;
- for (sample_idx = 0; sample_idx < 127; sample_idx = sample_idx + 1) begin
- if (ptc_pwm_out[6]) begin
- high_count = high_count + 1;
- end
- @(negedge clk);
- end
- if (high_count !== 64) begin
- $display("ptc pwm at duty 64 should have 64 high ticks per 127 tick period, got %0d", high_count);
- $fatal;
- end
- pid_fault = 1'b1;
- pulse_pid(5'd5, 18'sd80);
- if (!ptc_force_off || ptc_reason !== 4'd2 || ptc_bank_enable[5] || !ptc_bank_force_off[5] || ptc_pwm_out[5]) begin
- $display("ptc output bank should force off channel on pid fault");
- $fatal;
- end
- pid_fault = 1'b0;
- fault_latched = 1'b1;
- fault_code = 4'd5;
- @(negedge clk);
- if (!ptc_force_off || ptc_bank_enable !== 24'd0 || ptc_bank_force_off !== 24'hffffff || ptc_pwm_out !== 24'd0) begin
- $display("ptc output bank should force all PTC outputs off directly from central safety state");
- $fatal;
- end
- $display("PASS: ptc_output_bank_tb");
- $finish;
- end
- endmodule
|