`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; wire [4:0] addr; reg [7:0] duty_data; wire [31:0] bitmap; wire boundary; wire miss; wire [6:0] phase; wire idle; wire [23:0] ptc_pins = bitmap[23:0]; wire [7:0] fan_pins = ~bitmap[31:24]; 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; 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), .duty_rd_addr(addr), .duty_rd_data(duty_data), .logical_bitmap(bitmap), .phase_boundary(boundary), .deadline_miss(miss), .phase_value(phase), .scan_idle(idle) ); // 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 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 high_count[channel] = 0; last_edge_cycle[channel] = -1; end for (channel = 0; channel < 8; channel = channel + 1) fan_active_count[channel] = 0; repeat (4) @(negedge clk); if (bitmap !== 32'hff000000 || ptc_pins !== 24'd0 || fan_pins !== 8'd0) $fatal(1, "reset polarity mismatch bitmap=%h ptc=%h fan=%h", bitmap, ptc_pins, fan_pins); rst_n = 1; // Start at phase zero and count one exact 2 ms / 50,000-clock carrier. wait_for_phase_zero(); 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_pins[channel]) fan_active_count[channel] = fan_active_count[channel] + 1; if (fan_pins[channel] !== ~bitmap[channel + 24]) $fatal(1, "fan polarity 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] = 100; wait_for_boundary(); wait_for_boundary(); if (bitmap !== 32'hff000000) $fatal(1, "atomic safety bitmap mismatch: %h", bitmap); if (ptc_pins !== 24'd0 || fan_pins !== 8'd0) $fatal(1, "safe physical polarity mismatch ptc=%h fan=%h", ptc_pins, fan_pins); if (miss) $fatal(1, "scan deadline miss after safety transition"); $display("PASS: L6 PWM 32ch 500Hz duties=0,1,2,50,99,100 atomic safety polarity"); $finish; end endmodule