Просмотр исходного кода

product-fix: correct PWM channel and tick arithmetic

Codex Resource Selftest 4 месяцев назад
Родитель
Сommit
72600bc672
2 измененных файлов с 9 добавлено и 7 удалено
  1. 4 6
      src/modules/pwm_scheduler.v
  2. 5 1
      src/modules/shared_500us_tick.v

+ 4 - 6
src/modules/pwm_scheduler.v

@@ -20,7 +20,6 @@ module pwm_scheduler #(
     localparam integer SLOT_BITS = (PWM_SLOT_CLKS <= 1) ? 1 : $clog2(PWM_SLOT_CLKS);
 
     reg [CHANNEL_BITS-1:0] current_channel;
-    reg [CHANNEL_BITS-1:0] data_channel;
     reg [PHASE_BITS-1:0] phase;
     reg [SLOT_BITS-1:0] slot_counter;
     wire [PWM_DUTY_WIDTH-1:0] selected_duty = target_rd_data[PWM_DUTY_WIDTH-1:0];
@@ -31,7 +30,6 @@ module pwm_scheduler #(
     always @(posedge clk or negedge rst_n) begin
         if (!rst_n) begin
             current_channel <= {CHANNEL_BITS{1'b0}};
-            data_channel <= {CHANNEL_BITS{1'b0}};
             phase <= {PHASE_BITS{1'b0}};
             slot_counter <= {SLOT_BITS{1'b0}};
             logical_pwm_on <= {CHANNEL_COUNT{1'b0}};
@@ -39,10 +37,10 @@ module pwm_scheduler #(
             slot_counter <= slot_counter + 1'b1;
         end else begin
             slot_counter <= {SLOT_BITS{1'b0}};
-            // target_rd_data belongs to the address requested during the
-            // preceding slot because output_target_ram has one-cycle latency.
-            logical_pwm_on[data_channel] <= logical_on;
-            data_channel <= current_channel;
+            // The synchronous RAM has already seen current_channel during
+            // the preceding clock in this slot, so the returned data belongs
+            // to the channel currently being completed.
+            logical_pwm_on[current_channel] <= logical_on;
             if (current_channel == CHANNEL_COUNT - 1) begin
                 current_channel <= {CHANNEL_BITS{1'b0}};
                 if (phase == PWM_PHASE_COUNT - 1)

+ 5 - 1
src/modules/shared_500us_tick.v

@@ -8,7 +8,11 @@ module shared_500us_tick #(
     input  wire rst_n,
     output reg  tick
 );
-    localparam integer TICK_CLKS = (CLOCK_HZ * TICK_US) / 1_000_000;
+    // Split the product before multiplying so 25 MHz * 500 us does not
+    // overflow a 32-bit Verilog integer before the microsecond conversion.
+    localparam integer TICK_CLKS =
+        (CLOCK_HZ / 1_000_000) * TICK_US +
+        ((CLOCK_HZ % 1_000_000) * TICK_US) / 1_000_000;
     localparam integer COUNTER_BITS = (TICK_CLKS <= 1) ? 1 : $clog2(TICK_CLKS);
     reg [COUNTER_BITS-1:0] counter;