| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- `timescale 1ns/1ps
- // L6 acceptance: exercise the production 12500-clock phase (20 Hz) and all 100 phases.
- // Event-driven monitoring on phase boundaries and bitmap transitions verifies exact
- // 20 Hz timing (1,250,000 clocks/period) without per-clock loop overhead.
- module pwm_bitmap_engine_tb;
- localparam integer PHASE_CLKS = 12500;
- localparam integer PERIOD_CLKS = 100 * PHASE_CLKS;
- 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 last_boundary_cycle;
- integer boundary_count;
- integer channel;
- integer period;
- reg previous_valid;
- reg idle_seen;
- reg check_safe_mode = 0;
- reg check_runtime_mode = 0;
- integer safe_fan0_count = 0;
- always #20 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)
- );
- always @(posedge idle) begin
- if (rst_n) idle_seen = 1;
- end
- always @(negedge rst_n) begin
- previous_bitmap = bitmap;
- previous_valid = 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
- // Any bitmap transition must occur exclusively on a phase boundary.
- always @(bitmap) begin
- #1;
- if (check_safe_mode) begin
- if (ptc_pins !== 0) $fatal(1, "PTC high during safe mode check");
- if (fan_connector_pwm !== {8{fan_connector_pwm[0]}})
- $fatal(1, "safe mode fan channels diverged: %b", fan_connector_pwm);
- end
- if (rst_n && previous_valid && bitmap !== previous_bitmap) begin
- if (!boundary)
- $fatal(1, "bitmap changed away from phase boundary at cycle %0d", $time / 40);
- for (channel = 0; channel < 32; channel = channel + 1) begin
- if (bitmap[channel] !== previous_bitmap[channel]) begin
- if (last_edge_cycle[channel] >= 0 &&
- ($time / 40) - last_edge_cycle[channel] < PHASE_CLKS)
- $fatal(1, "channel %0d pulse narrower than one phase", channel);
- last_edge_cycle[channel] = $time / 40;
- end
- end
- end
- if (rst_n) begin
- previous_bitmap = bitmap;
- previous_valid = 1;
- end
- end
- always @(posedge boundary) begin
- #1;
- if (check_safe_mode) begin
- if (ptc_pins !== 0) $fatal(1, "PTC high during safe mode check");
- if (fan_connector_pwm !== {8{fan_connector_pwm[0]}})
- $fatal(1, "safe mode fan channels diverged: %b", fan_connector_pwm);
- if (fan_connector_pwm[0])
- safe_fan0_count = safe_fan0_count + PHASE_CLKS;
- end
- if (check_runtime_mode) begin
- if (miss) $fatal(1, "unexpected scan deadline miss");
- if (fan_connector_pwm !== bitmap[31:24])
- $fatal(1, "fan connector polarity mismatch");
- if (fan_balls[3:0] !== ~bitmap[27:24])
- $fatal(1, "front fan board inversion mismatch");
- if (fan_balls[7:4] !== bitmap[31:28])
- $fatal(1, "rear fan direct path mismatch");
- if (ptc_pins !== bitmap[23:0])
- $fatal(1, "PTC polarity mismatch");
- for (channel = 0; channel < 32; channel = channel + 1)
- if (bitmap[channel]) high_count[channel] = high_count[channel] + PHASE_CLKS;
- for (channel = 0; channel < 8; channel = channel + 1)
- if (fan_connector_pwm[channel])
- fan_active_count[channel] = fan_active_count[channel] + PHASE_CLKS;
- end
- if (rst_n) begin
- if (last_boundary_cycle >= 0 &&
- ($time / 40) - last_boundary_cycle != PHASE_CLKS)
- $fatal(1, "phase boundary spacing=%0d expected=%0d",
- ($time / 40) - last_boundary_cycle, PHASE_CLKS);
- last_boundary_cycle = $time / 40;
- boundary_count = boundary_count + 1;
- previous_bitmap = bitmap;
- previous_valid = 1;
- end
- end
- task wait_for_boundary;
- begin
- @(posedge boundary);
- @(negedge clk);
- end
- endtask
- task wait_for_phase_zero;
- begin
- wait_for_boundary();
- while (phase != 0) wait_for_boundary();
- end
- endtask
- initial begin
- duty_data = 0;
- previous_bitmap = 0;
- previous_valid = 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 held for three complete 20 Hz carriers: connector FAN20 / PTC0.
- wait_for_phase_zero();
- for (period = 0; period < 3; period = period + 1) begin
- safe_fan0_count = fan_connector_pwm[0] ? PHASE_CLKS : 0;
- check_safe_mode = 1;
- repeat (99) @(posedge boundary);
- #10;
- check_safe_mode = 0;
- for (channel = 0; channel < 8; channel = channel + 1)
- if (safe_fan0_count != 20 * PHASE_CLKS)
- $fatal(1, "reset period %0d fan %0d high=%0d", period, channel, safe_fan0_count);
- wait_for_boundary();
- end
- rst_n = 1;
- // POR released while control output RAM is not ready: two complete safe carriers.
- wait_for_phase_zero();
- for (period = 0; period < 2; period = period + 1) begin
- safe_fan0_count = fan_connector_pwm[0] ? PHASE_CLKS : 0;
- check_safe_mode = 1;
- repeat (99) @(posedge boundary);
- #10;
- check_safe_mode = 0;
- for (channel = 0; channel < 8; channel = channel + 1)
- if (safe_fan0_count != 20 * PHASE_CLKS)
- $fatal(1, "pre-ready period %0d fan %0d high=%0d", period, channel, safe_fan0_count);
- wait_for_boundary();
- 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 50 ms / 1,250,000-clock carrier (20 Hz).
- wait_for_phase_zero();
- for (channel = 0; channel < 32; channel = channel + 1)
- high_count[channel] = bitmap[channel] ? PHASE_CLKS : 0;
- for (channel = 0; channel < 8; channel = channel + 1)
- fan_active_count[channel] = fan_connector_pwm[channel] ? PHASE_CLKS : 0;
- check_runtime_mode = 1;
- repeat (99) @(posedge boundary);
- #10;
- check_runtime_mode = 0;
- for (channel = 0; channel < 32; channel = channel + 1) begin
- if (high_count[channel] != duty[channel] * PHASE_CLKS)
- $fatal(1, "channel %0d duty=%0d high=%0d expected=%0d",
- channel, duty[channel], high_count[channel], duty[channel] * PHASE_CLKS);
- end
- for (channel = 0; channel < 8; channel = channel + 1) begin
- if (fan_active_count[channel] != duty[channel + 24] * PHASE_CLKS)
- $fatal(1, "active-low fan %0d duty=%0d active=%0d expected=%0d",
- channel, duty[channel + 24], fan_active_count[channel],
- duty[channel + 24] * PHASE_CLKS);
- 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 global safe image before next boundary.
- 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");
- $display("pwm_bitmap_engine_tb PASS");
- $finish;
- end
- endmodule
|