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

optimize: narrow frame_rx gap and index counters

- frame_rx: reduce gap_counter from 32-bit to the minimum width for
  INTER_BYTE_TIMEOUT_CYCLES; reduce payload_index to the minimum width
  for MAX_PAYLOAD.
- frame_tx: reduce payload_index to 5 bits (maximum temperature response
  length 27 bytes).

Generated with Devin

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Codex Resource Selftest 4 месяцев назад
Родитель
Сommit
fe1a0a2a19
2 измененных файлов с 13 добавлено и 10 удалено
  1. 9 7
      src/common/frame_rx.v
  2. 4 3
      src/common/frame_tx.v

+ 9 - 7
src/common/frame_rx.v

@@ -30,11 +30,13 @@ module frame_rx #(
     localparam [3:0] ST_CRC_LOW = 4'd8;
     localparam [3:0] ST_CRC_HIGH = 4'd9;
     localparam [3:0] ST_HOLD = 4'd10;
+    localparam integer GAP_BITS = (INTER_BYTE_TIMEOUT_CYCLES <= 1) ? 1 : $clog2(INTER_BYTE_TIMEOUT_CYCLES + 1);
+    localparam integer PAYLOAD_IDX_BITS = (MAX_PAYLOAD <= 1) ? 1 : $clog2(MAX_PAYLOAD + 1);
 
     reg [3:0] state;
-    reg [7:0] payload_index;
+    reg [PAYLOAD_IDX_BITS-1:0] payload_index;
     reg [7:0] received_crc_low;
-    reg [31:0] gap_counter;
+    reg [GAP_BITS-1:0] gap_counter;
 
     wire crc_busy;
     wire crc_data_done;
@@ -55,9 +57,9 @@ module frame_rx #(
             frame_valid <= 1'b0;
             frame_type <= 8'd0;
             frame_length <= 8'd0;
-            payload_index <= 8'd0;
+            payload_index <= {PAYLOAD_IDX_BITS{1'b0}};
             received_crc_low <= 8'd0;
-            gap_counter <= 32'd0;
+            gap_counter <= {GAP_BITS{1'b0}};
             payload_wr_en <= 1'b0;
             payload_wr_addr <= 8'd0;
             payload_wr_data <= 8'd0;
@@ -77,13 +79,13 @@ module frame_rx #(
             if (state != ST_WAIT_A5 && state != ST_WAIT_5A && !byte_valid) begin
                 if (INTER_BYTE_TIMEOUT_CYCLES <= 1 || gap_counter >= INTER_BYTE_TIMEOUT_CYCLES - 1) begin
                     state <= ST_WAIT_A5;
-                    gap_counter <= 32'd0;
+                    gap_counter <= {GAP_BITS{1'b0}};
                 end else begin
                     gap_counter <= gap_counter + 1'b1;
                 end
             end
             if (byte_valid) begin
-                gap_counter <= 32'd0;
+                gap_counter <= {GAP_BITS{1'b0}};
                 case (state)
                     ST_WAIT_A5: begin
                         if (byte_data == 8'hA5) state <= ST_WAIT_5A;
@@ -137,7 +139,7 @@ module frame_rx #(
                         if (frame_length > MAX_PAYLOAD) state <= ST_WAIT_A5;
                         else if (frame_length == 0) state <= ST_CRC_LOW;
                         else begin
-                            payload_index <= 8'd0;
+                            payload_index <= {PAYLOAD_IDX_BITS{1'b0}};
                             state <= ST_PAYLOAD;
                         end
                     end

+ 4 - 3
src/common/frame_tx.v

@@ -25,11 +25,12 @@ module frame_tx (
     localparam [3:0] ST_PAYLOAD_CRC_WAIT = 4'd8;
     localparam [3:0] ST_CRC_LOW = 4'd9;
     localparam [3:0] ST_CRC_HIGH = 4'd10;
+    localparam integer PAYLOAD_IDX_BITS = 5;
 
     reg [3:0] state;
     reg [7:0] type_reg;
     reg [7:0] length_reg;
-    reg [7:0] payload_index;
+    reg [PAYLOAD_IDX_BITS-1:0] payload_index;
     reg crc_start_reg;
     wire crc_busy;
     wire crc_data_done;
@@ -51,7 +52,7 @@ module frame_tx (
             state <= ST_IDLE;
             type_reg <= 8'd0;
             length_reg <= 8'd0;
-            payload_index <= 8'd0;
+            payload_index <= {PAYLOAD_IDX_BITS{1'b0}};
             busy <= 1'b0;
             byte_valid <= 1'b0;
             byte_data <= 8'd0;
@@ -92,7 +93,7 @@ module frame_tx (
                 case (state)
                     ST_TYPE_CRC_WAIT: state <= ST_LENGTH;
                     ST_LENGTH_CRC_WAIT: begin
-                        payload_index <= 8'd0;
+                        payload_index <= {PAYLOAD_IDX_BITS{1'b0}};
                         state <= (length_reg == 0) ? ST_CRC_LOW : ST_PAYLOAD;
                     end
                     ST_PAYLOAD_CRC_WAIT: begin