فهرست منبع

fix: run pwm defaults at 20hz slot cadence

Codex Resource Selftest 4 ماه پیش
والد
کامیت
fdbbdeb10f
3فایلهای تغییر یافته به همراه130 افزوده شده و 37 حذف شده
  1. 32 0
      runs/2b309dc4377143179824707be0f7a06b/agents/pwm/task_report.json
  2. 26 22
      src/modules/pwm_scheduler.v
  3. 72 15
      tests/sim/pwm_scheduler_tb.v

+ 32 - 0
runs/2b309dc4377143179824707be0f7a06b/agents/pwm/task_report.json

@@ -0,0 +1,32 @@
+{
+  "schema": "prod2u-task-report-v1",
+  "run_id": "2b309dc4377143179824707be0f7a06b",
+  "task": "pwm-scheduler",
+  "branch": "codex/2b309dc4377143179824707be0f7a06b/pwm",
+  "status": "pass",
+  "changed_paths": [
+    "src/modules/pwm_scheduler.v",
+    "tests/sim/pwm_scheduler_tb.v"
+  ],
+  "commands": [
+    {
+      "command": "iverilog -g2012 -s pwm_scheduler_tb ...; vvp ...",
+      "status": "pass",
+      "result": "pwm_scheduler_tb PASS"
+    }
+  ],
+  "coverage": [
+    "reset inactive polarity",
+    "default PTC off",
+    "default FAN connector active-high through FPGA active-low output",
+    "default FAN duty 2/4 in reduced phase test",
+    "slot boundary channel hold",
+    "phase increment only after final channel",
+    "loaded RAM targets"
+  ],
+  "hardware_access": {
+    "com": false,
+    "jtag": false,
+    "programming": false
+  }
+}

+ 26 - 22
src/modules/pwm_scheduler.v

@@ -4,6 +4,9 @@ module pwm_scheduler #(
     parameter integer PTC_COUNT = 24,
     parameter integer FAN_COUNT = 4,
     parameter integer PWM_BITS = 8,
+    parameter integer PWM_SLOT_CLKS = 174,
+    parameter [PWM_BITS-1:0] PTC_DEFAULT_TARGET = {PWM_BITS{1'b0}},
+    parameter [PWM_BITS-1:0] FAN_DEFAULT_TARGET = {PWM_BITS{1'b0}},
     parameter integer PTC_ACTIVE_HIGH = 1,
     parameter integer FAN_ACTIVE_HIGH = 0
 ) (
@@ -11,48 +14,49 @@ module pwm_scheduler #(
     input  wire                         rst_n,
     input  wire                         target_loaded,
     output wire [(PTC_COUNT+FAN_COUNT <= 1 ? 1 : $clog2(PTC_COUNT+FAN_COUNT))-1:0] target_rd_addr,
-    input  wire [7:0]                   target_rd_data,
+    input  wire [PWM_BITS-1:0]           target_rd_data,
     output reg  [PTC_COUNT-1:0]         ptc_pwm_out,
     output reg  [FAN_COUNT-1:0]         fan_pwm_out
 );
     localparam integer TOTAL_COUNT = PTC_COUNT + FAN_COUNT;
     localparam integer CHANNEL_BITS = (TOTAL_COUNT <= 1) ? 1 : $clog2(TOTAL_COUNT);
-    reg [CHANNEL_BITS-1:0] channel;
+    localparam integer SLOT_BITS = (PWM_SLOT_CLKS <= 1) ? 1 : $clog2(PWM_SLOT_CLKS);
+
+    reg [CHANNEL_BITS-1:0] current_channel;
     reg [PWM_BITS-1:0] phase;
-    integer index;
+    reg [SLOT_BITS-1:0] slot_counter;
+    wire [PWM_BITS-1:0] selected_target;
+    wire logical_on;
 
-    assign target_rd_addr = channel;
+    assign target_rd_addr = current_channel;
+    assign selected_target = target_loaded ? target_rd_data :
+        ((current_channel < PTC_COUNT) ? PTC_DEFAULT_TARGET : FAN_DEFAULT_TARGET);
+    assign logical_on = selected_target > phase;
 
     always @(posedge clk or negedge rst_n) begin
         if (!rst_n) begin
-            channel <= {CHANNEL_BITS{1'b0}};
-            phase <= {PWM_BITS{1'b0}};
-            ptc_pwm_out <= {PTC_COUNT{(PTC_ACTIVE_HIGH == 0)}};
-            fan_pwm_out <= {FAN_COUNT{(FAN_ACTIVE_HIGH == 0)}};
-        end else if (!target_loaded) begin
-            channel <= {CHANNEL_BITS{1'b0}};
+            current_channel <= {CHANNEL_BITS{1'b0}};
             phase <= {PWM_BITS{1'b0}};
+            slot_counter <= {SLOT_BITS{1'b0}};
             ptc_pwm_out <= {PTC_COUNT{(PTC_ACTIVE_HIGH == 0)}};
             fan_pwm_out <= {FAN_COUNT{(FAN_ACTIVE_HIGH == 0)}};
+        end else if (slot_counter < PWM_SLOT_CLKS - 1) begin
+            slot_counter <= slot_counter + 1'b1;
         end else begin
-            if (channel < PTC_COUNT) begin
-                ptc_pwm_out[channel] <= (PTC_ACTIVE_HIGH != 0) ?
-                    (target_rd_data > phase) : !(target_rd_data > phase);
+            slot_counter <= {SLOT_BITS{1'b0}};
+
+            if (current_channel < PTC_COUNT) begin
+                ptc_pwm_out[current_channel] <= (PTC_ACTIVE_HIGH != 0) ? logical_on : !logical_on;
             end else begin
-                fan_pwm_out[channel - PTC_COUNT] <= (FAN_ACTIVE_HIGH != 0) ?
-                    (target_rd_data > phase) : !(target_rd_data > phase);
+                fan_pwm_out[current_channel - PTC_COUNT] <= (FAN_ACTIVE_HIGH != 0) ? logical_on : !logical_on;
             end
 
-            if (channel == TOTAL_COUNT - 1) begin
-                channel <= {CHANNEL_BITS{1'b0}};
+            if (current_channel == TOTAL_COUNT - 1) begin
+                current_channel <= {CHANNEL_BITS{1'b0}};
                 phase <= phase + 1'b1;
             end else begin
-                channel <= channel + 1'b1;
+                current_channel <= current_channel + 1'b1;
             end
         end
     end
-
-    always @* begin
-        index = channel;
-    end
 endmodule

+ 72 - 15
tests/sim/pwm_scheduler_tb.v

@@ -1,25 +1,33 @@
 `timescale 1ns/1ps
+
 module pwm_scheduler_tb;
     reg clk = 1'b0;
     reg rst_n = 1'b0;
-    reg target_wr_en = 1'b0;
+    reg target_loaded = 1'b0;
+    reg [1:0] target_rd_addr;
+    wire [1:0] target_rd_data;
+    wire [7:0] ram_rd_data;
     reg [1:0] target_wr_addr = 2'd0;
     reg [7:0] target_wr_data = 8'd0;
-    reg target_loaded = 1'b0;
-    wire [1:0] target_rd_addr;
-    wire [7:0] target_rd_data;
+    reg target_wr_en = 1'b0;
     wire [1:0] ptc_pwm_out;
     wire fan_pwm_out;
+    integer event_index;
+    integer expected_phase;
+    integer connector_high_count;
 
     always #1 clk = ~clk;
 
-    output_target_ram #(.DEPTH(3)) ram (
+    output_target_ram #(.DEPTH(3), .ADDR_WIDTH(2)) ram (
         .clk(clk), .wr_en(target_wr_en), .wr_addr(target_wr_addr), .wr_data(target_wr_data),
-        .rd_addr(target_rd_addr), .rd_data(target_rd_data)
+        .rd_addr(target_rd_addr), .rd_data(ram_rd_data)
     );
+    assign target_rd_data = ram_rd_data[1:0];
 
     pwm_scheduler #(
-        .PTC_COUNT(2), .FAN_COUNT(1), .PWM_BITS(2), .PTC_ACTIVE_HIGH(1), .FAN_ACTIVE_HIGH(0)
+        .PTC_COUNT(2), .FAN_COUNT(1), .PWM_BITS(2), .PWM_SLOT_CLKS(2),
+        .PTC_DEFAULT_TARGET(8'h00), .FAN_DEFAULT_TARGET(8'h02),
+        .PTC_ACTIVE_HIGH(1), .FAN_ACTIVE_HIGH(0)
     ) dut (
         .clk(clk), .rst_n(rst_n), .target_loaded(target_loaded),
         .target_rd_addr(target_rd_addr), .target_rd_data(target_rd_data),
@@ -40,17 +48,66 @@ module pwm_scheduler_tb;
     endtask
 
     initial begin
-        repeat (3) @(posedge clk);
+        #0.1;
         rst_n = 1'b1;
-        write_target(0, 0);
-        write_target(1, 4);
-        write_target(2, 2);
+        #0.1;
+        rst_n = 1'b0;
+        #0.1;
+        if (ptc_pwm_out !== 2'b00) $fatal(1, "PTC reset output is active");
+        if (fan_pwm_out !== 1'b1) $fatal(1, "FAN reset output is not inactive");
+
+        repeat (2) @(posedge clk);
+        rst_n = 1'b1;
+
+        repeat (2) @(posedge clk);
+        if (dut.current_channel !== 2'd1) $fatal(1, "channel did not advance at slot boundary");
+        @(posedge clk);
+        if (dut.current_channel !== 2'd1) $fatal(1, "channel advanced before slot completed");
+        @(posedge clk);
+        if (dut.current_channel !== 2'd2) $fatal(1, "channel did not advance after second slot");
+        repeat (2) @(posedge clk);
+        if (dut.current_channel !== 2'd0) $fatal(1, "last channel did not wrap");
+        if (dut.phase !== 2'd1) $fatal(1, "phase did not increment exactly once");
+
+        rst_n = 1'b0;
+        @(posedge clk);
+        if (ptc_pwm_out !== 2'b00 || fan_pwm_out !== 1'b1) $fatal(1, "reset polarity mismatch");
+        rst_n = 1'b1;
+        connector_high_count = 0;
+        for (event_index = 0; event_index < 12; event_index = event_index + 1) begin
+            repeat (2) @(posedge clk);
+            @(negedge clk);
+            if (event_index % 3 == 2) begin
+                expected_phase = event_index / 3;
+                if ((!fan_pwm_out) !== (2 > expected_phase)) begin
+                    $fatal(1, "default FAN connector polarity/duty mismatch phase=%0d out=%b", expected_phase, fan_pwm_out);
+                end
+                if (!fan_pwm_out) connector_high_count = connector_high_count + 1;
+            end
+            if (ptc_pwm_out !== 2'b00) $fatal(1, "default PTC is not continuously off");
+        end
+        if (connector_high_count !== 2) $fatal(1, "default FAN connector duty mismatch: %0d", connector_high_count);
+
+        write_target(0, 8'h03);
+        write_target(1, 8'h00);
+        write_target(2, 8'h00);
         target_loaded = 1'b1;
-        repeat (20) @(posedge clk);
-        if (ptc_pwm_out[0] !== 1'b0) $fatal(1, "PTC zero target is active");
-        if (ptc_pwm_out[1] !== 1'b1) $fatal(1, "PTC full target is inactive");
-        if (fan_pwm_out !== 1'b0 && fan_pwm_out !== 1'b1) $fatal(1, "FAN output unknown");
+        repeat (2) @(posedge clk);
+        @(negedge clk);
+        if (ptc_pwm_out[0] !== 1'b1) $fatal(1, "loaded PTC target was not used");
+        repeat (4) @(posedge clk);
+        @(negedge clk);
+        if (ptc_pwm_out[1] !== 1'b0) $fatal(1, "loaded PTC zero target was not used");
+        repeat (2) @(posedge clk);
+        @(negedge clk);
+        if (fan_pwm_out !== 1'b1) $fatal(1, "loaded FAN zero target polarity mismatch");
+
         $display("pwm_scheduler_tb PASS");
         $finish;
     end
+
+    initial begin
+        #2000;
+        $fatal(1, "PWM timeout channel=%0d phase=%0d slot=%0d", dut.current_channel, dut.phase, dut.slot_counter);
+    end
 endmodule