| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- `timescale 1ns/1ps
- module fan_tach_domain_tb;
- reg clk;
- reg rst_n;
- reg global_enable;
- reg fault_latched;
- reg [3:0] fault_code;
- reg [5:0] fan_tach_in;
- wire fan_enable;
- wire [17:0] fan_duty_or_level;
- wire fan_force_off;
- wire [3:0] fan_reason;
- wire [5:0] fan_pwm_enable;
- wire [5:0] fan_pwm_logic_high;
- wire [5:0] fan_pwm_drive_low;
- wire [6*16-1:0] fan_tach_count;
- wire [6*16-1:0] fan_tach_window_edges;
- wire [6*16-1:0] fan_rpm_estimate;
- fan_tach_domain #(
- .PWM_PERIOD_TICKS(1000),
- .PWM_COUNTER_WIDTH(10),
- .TACH_WINDOW_TICKS(1000),
- .TACH_COUNTER_WIDTH(10)
- ) dut (
- .clk(clk),
- .rst_n(rst_n),
- .global_enable(global_enable),
- .fault_latched(fault_latched),
- .fault_code(fault_code),
- .fan_tach_in(fan_tach_in),
- .fan_enable(fan_enable),
- .fan_duty_or_level(fan_duty_or_level),
- .fan_force_off(fan_force_off),
- .fan_reason(fan_reason),
- .fan_pwm_enable(fan_pwm_enable),
- .fan_pwm_logic_high(fan_pwm_logic_high),
- .fan_pwm_drive_low(fan_pwm_drive_low),
- .fan_tach_count(fan_tach_count),
- .fan_tach_window_edges(fan_tach_window_edges),
- .fan_rpm_estimate(fan_rpm_estimate)
- );
- initial begin
- clk = 1'b0;
- forever #5 clk = ~clk;
- end
- task wait_cycles;
- input integer cycles;
- integer n;
- begin
- for (n = 0; n < cycles; n = n + 1) begin
- @(negedge clk);
- end
- end
- endtask
- initial begin
- rst_n = 1'b0;
- global_enable = 1'b1;
- fault_latched = 1'b0;
- fault_code = 4'd0;
- fan_tach_in = 6'd0;
- repeat (4) @(negedge clk);
- rst_n = 1'b1;
- fan_tach_in[0] = 1'b1;
- @(negedge clk);
- fan_tach_in[0] = 1'b0;
- @(negedge clk);
- fan_tach_in[0] = 1'b1;
- @(negedge clk);
- if (fan_tach_count[15:0] !== 16'd2) begin
- $display("fan tach domain should count rising tach edges");
- $fatal;
- end
- if (!fan_enable || fan_force_off || fan_duty_or_level !== 18'd38 || fan_pwm_enable !== 6'h3f) begin
- $display("fan tach domain should run fixed low background duty when safe");
- $fatal;
- end
- if (!fan_pwm_logic_high[0] || fan_pwm_drive_low[0]) begin
- $display("fan pwm should start high/high-z for normal 4-wire pwm");
- $fatal;
- end
- wait_cycles(299);
- if (fan_pwm_logic_high[0] || !fan_pwm_drive_low[0]) begin
- $display("fan pwm normal duty should pull low after 30 percent high-time");
- $fatal;
- end
- wait_cycles(701);
- if (!fan_pwm_logic_high[0] || fan_pwm_drive_low[0]) begin
- $display("fan pwm should restart high at 25 kHz period");
- $fatal;
- end
- fault_latched = 1'b1;
- fault_code = 4'd5;
- @(negedge clk);
- if (!fan_enable || fan_force_off || fan_duty_or_level !== 18'd127 ||
- fan_pwm_enable !== 6'h3f || fan_reason !== 4'd5) begin
- $display("fan tach domain should enable fallback on overtemp");
- $fatal;
- end
- wait_cycles(1000);
- if (!fan_pwm_logic_high[0] || fan_pwm_drive_low[0]) begin
- $display("fan fallback 100 percent should stay high/high-z for full speed request");
- $fatal;
- end
- repeat (100) begin
- fan_tach_in[1] = 1'b1;
- @(negedge clk);
- fan_tach_in[1] = 1'b0;
- @(negedge clk);
- end
- wait_cycles(1000);
- if (fan_tach_window_edges[(1*16) +: 16] !== 16'd100 || fan_rpm_estimate[(1*16) +: 16] !== 16'd3000) begin
- $display("fan tach window should convert 100 edges per second to 3000 rpm");
- $fatal;
- end
- fault_latched = 1'b0;
- global_enable = 1'b0;
- fault_code = 4'd1;
- @(negedge clk);
- if (!fan_enable || fan_force_off || fan_duty_or_level !== 18'd38 || fan_pwm_enable !== 6'h3f) begin
- $display("fan tach domain should keep background duty on host disable");
- $fatal;
- end
- global_enable = 1'b1;
- fault_latched = 1'b1;
- fault_code = 4'd7;
- @(negedge clk);
- if (!fan_enable || fan_force_off || fan_duty_or_level !== 18'd38 || fan_pwm_enable !== 6'h3f) begin
- $display("fan tach domain should not stop fan for motor interlock");
- $fatal;
- end
- $display("PASS: fan_tach_domain_tb");
- $finish;
- end
- endmodule
|