`timescale 1ns/1ps // L6 acceptance: exercise the production 500-clock phase and all 100 phases. module pwm_bitmap_engine_tb; reg clk = 0; reg rst_n = 0; reg runtime_duty_ready = 0; wire [4:0] addr; reg [7:0] duty_data; wire [31:0] bitmap; wire boundary; wire boundary_advance; wire miss; wire [6:0] phase; wire idle; wire [23:0] ptc_pins = bitmap[23:0]; wire [3:0] front_fan_balls; wire [3:0] rear_fan_balls; wire [7:0] fan_balls = {rear_fan_balls, front_fan_balls}; wire [7:0] fan_connector_pwm = {rear_fan_balls, ~front_fan_balls}; reg [7:0] duty [0:31]; reg [31:0] previous_bitmap; integer high_count [0:31]; integer fan_active_count [0:7]; integer last_edge_cycle [0:31]; integer cycle_index; integer last_boundary_cycle; integer boundary_count; integer channel; integer sample; integer period; reg previous_valid; reg idle_seen; always #5 clk = ~clk; always @(posedge clk) duty_data <= duty[addr]; pwm_bitmap_engine dut ( .clk(clk), .rst_n(rst_n), .runtime_duty_ready(runtime_duty_ready), .duty_rd_addr(addr), .duty_rd_data(duty_data), .logical_bitmap(bitmap), .phase_boundary(boundary), .phase_boundary_advance(boundary_advance), .deadline_miss(miss), .phase_value(phase), .scan_idle(idle) ); fan_board_output_adapter board_adapter ( .logical_fan_on(bitmap[31:24]), .front_fan_ball(front_fan_balls), .rear_fan_ball(rear_fan_balls) ); // Outputs may change only on the common phase boundary. Therefore every // physical pulse is an integer multiple of the 500-clock phase and no // safety update can create a narrow asynchronous pulse. always @(negedge clk) begin if (!rst_n) begin previous_bitmap = bitmap; previous_valid = 0; cycle_index = 0; last_boundary_cycle = -1; boundary_count = 0; idle_seen = 0; for (channel = 0; channel < 32; channel = channel + 1) last_edge_cycle[channel] = -1; end else begin cycle_index = cycle_index + 1; if (idle) idle_seen = 1; if (boundary) begin if (last_boundary_cycle >= 0 && cycle_index - last_boundary_cycle != 500) $fatal(1, "phase boundary spacing=%0d expected=500", cycle_index - last_boundary_cycle); last_boundary_cycle = cycle_index; boundary_count = boundary_count + 1; end if (previous_valid && bitmap !== previous_bitmap) begin if (!boundary) $fatal(1, "bitmap changed away from phase boundary"); for (channel = 0; channel < 32; channel = channel + 1) begin if (bitmap[channel] !== previous_bitmap[channel]) begin if (last_edge_cycle[channel] >= 0 && cycle_index - last_edge_cycle[channel] < 500) $fatal(1, "channel %0d pulse narrower than one phase", channel); last_edge_cycle[channel] = cycle_index; end end end previous_bitmap = bitmap; previous_valid = 1; end end task wait_for_boundary; begin @(negedge clk); while (!boundary) @(negedge clk); end endtask task wait_for_phase_zero; begin @(negedge clk); while (!(boundary && phase == 0)) @(negedge clk); end endtask initial begin duty_data = 0; previous_bitmap = 0; previous_valid = 0; cycle_index = 0; last_boundary_cycle = -1; boundary_count = 0; idle_seen = 0; for (channel = 0; channel < 32; channel = channel + 1) begin if (channel < 24) begin case (channel % 6) 0: duty[channel] = 0; 1: duty[channel] = 1; 2: duty[channel] = 2; 3: duty[channel] = 50; 4: duty[channel] = 99; default: duty[channel] = 100; endcase end else begin case (channel) 24: duty[channel] = 20; 25: duty[channel] = 50; 26: duty[channel] = 100; 27: duty[channel] = 0; 28: duty[channel] = 50; 29: duty[channel] = 20; 30: duty[channel] = 0; default: duty[channel] = 100; endcase end high_count[channel] = 0; last_edge_cycle[channel] = -1; end for (channel = 0; channel < 8; channel = channel + 1) fan_active_count[channel] = 0; // Control reset is held for three complete carriers. The independent // safe carrier must continue at connector FAN20 / PTC0. repeat (2) @(negedge clk); for (period = 0; period < 3; period = period + 1) begin for (channel = 0; channel < 8; channel = channel + 1) fan_active_count[channel] = 0; for (sample = 0; sample < 50000; sample = sample + 1) begin if (ptc_pins !== 0) $fatal(1, "PTC high during control reset"); for (channel = 0; channel < 8; channel = channel + 1) if (fan_connector_pwm[channel]) fan_active_count[channel] = fan_active_count[channel] + 1; @(negedge clk); end for (channel = 0; channel < 8; channel = channel + 1) if (fan_active_count[channel] != 10000) $fatal(1, "reset period %0d fan %0d high=%0d", period, channel, fan_active_count[channel]); end rst_n = 1; // POR is released while the control output RAM is not ready. Even // though the RAM model contains non-safe values, two complete // carriers must remain connector-safe and no runtime scan may commit. for (period = 0; period < 2; period = period + 1) begin for (channel = 0; channel < 8; channel = channel + 1) fan_active_count[channel] = 0; for (sample = 0; sample < 50000; sample = sample + 1) begin if (ptc_pins !== 0) $fatal(1, "PTC high before runtime ready"); for (channel = 0; channel < 8; channel = channel + 1) if (fan_connector_pwm[channel]) fan_active_count[channel] = fan_active_count[channel] + 1; @(negedge clk); end for (channel = 0; channel < 8; channel = channel + 1) if (fan_active_count[channel] != 10000) $fatal(1, "pre-ready period %0d fan %0d high=%0d", period, channel, fan_active_count[channel]); end if (dut.runtime_scan_complete) $fatal(1, "runtime scan completed before runtime_duty_ready"); runtime_duty_ready = 1; wait_for_boundary(); if (dut.runtime_scan_complete) $fatal(1, "runtime scan marked complete at scan-start boundary"); while (!dut.runtime_scan_complete) begin if (ptc_pins !== 0) $fatal(1, "runtime bitmap committed before complete scan"); @(negedge clk); end wait_for_boundary(); // Start at phase zero and count one exact 2 ms / 50,000-clock carrier. wait_for_phase_zero(); for (channel = 0; channel < 32; channel = channel + 1) high_count[channel] = 0; for (channel = 0; channel < 8; channel = channel + 1) fan_active_count[channel] = 0; for (sample = 0; sample < 50000; sample = sample + 1) begin if (miss) $fatal(1, "unexpected scan deadline miss"); for (channel = 0; channel < 32; channel = channel + 1) if (bitmap[channel]) high_count[channel] = high_count[channel] + 1; for (channel = 0; channel < 8; channel = channel + 1) begin if (fan_connector_pwm[channel]) fan_active_count[channel] = fan_active_count[channel] + 1; if (fan_connector_pwm[channel] !== bitmap[channel + 24]) $fatal(1, "fan connector polarity mismatch channel=%0d", channel); if (channel < 4 && fan_balls[channel] !== ~bitmap[channel + 24]) $fatal(1, "front fan board inversion mismatch channel=%0d", channel); if (channel >= 4 && fan_balls[channel] !== bitmap[channel + 24]) $fatal(1, "rear fan direct path mismatch channel=%0d", channel); end if (ptc_pins !== bitmap[23:0]) $fatal(1, "PTC polarity mismatch"); if (sample != 49999) @(negedge clk); end for (channel = 0; channel < 32; channel = channel + 1) begin if (high_count[channel] != duty[channel] * 500) $fatal(1, "channel %0d duty=%0d high=%0d expected=%0d", channel, duty[channel], high_count[channel], duty[channel] * 500); end for (channel = 0; channel < 8; channel = channel + 1) begin if (fan_active_count[channel] != duty[channel + 24] * 500) $fatal(1, "active-low fan %0d duty=%0d active=%0d expected=%0d", channel, duty[channel + 24], fan_active_count[channel], duty[channel + 24] * 500); end if (!idle_seen) $fatal(1, "32-channel scan never completed before deadline"); if (boundary_count < 100) $fatal(1, "full 100-phase carrier was not observed"); // Apply a global safe image immediately before the phase-zero boundary. // The already-built phase zero remains intact; the following phase // commits all 32 new values together with no mid-phase output change. for (channel = 0; channel < 24; channel = channel + 1) duty[channel] = 0; for (channel = 24; channel < 32; channel = channel + 1) duty[channel] = 20; wait_for_boundary(); wait_for_boundary(); if (bitmap[23:0] !== 0) $fatal(1, "atomic safety bitmap mismatch: %h", bitmap); if (ptc_pins !== 24'd0) $fatal(1, "safe physical polarity mismatch ptc=%h balls=%h connector=%h", ptc_pins, fan_balls, fan_connector_pwm); if (miss) $fatal(1, "scan deadline miss after safety transition"); $display("PASS: PWM reset 3 periods FAN20 plus 0/20/50/100 connector polarity"); $finish; end endmodule