Jelajahi Sumber

mrdimm: eliminate 544 staging DFFs in mrdimm_transaction_core to fit EF2 MSlice limit

Prod2U Recovery 2 bulan lalu
induk
melakukan
dbcc7a9aed
1 mengubah file dengan 124 tambahan dan 108 penghapusan
  1. 124 108
      src/control/mrdimm_transaction_core.v

+ 124 - 108
src/control/mrdimm_transaction_core.v

@@ -1,47 +1,39 @@
-`timescale 1ns/1ps
+`timescale 1ns / 1ps
+// =============================================================================
+// Product:       Prod2U MRDIMM V1.2
+// Module:        mrdimm_transaction_core
+// Description:   Transaction admission, atomic SET_ALL validation, STOP_CLEAR,
+//                watchdog reload, and GET_ALL dispatch for MRDIMM.
+//                Implements strict SET_ACCEPT instant without staging arrays
+//                in flip-flops, reading and verifying directly from req_ram.
+// =============================================================================
 
 
-// MRDIMM Transaction Core (2026 contract, section 10 / AGENTS.md).
-//
-// Protocol requests:
-//   - SET_ALL (0x01, LEN 68):
-//       0..3:  FAN0..3 (0..100)
-//       4..7:  logical PTC0..3 (0..100)
-//       8..67: twelve 5-byte axis slots: {action:u8, steps:u16LE, half_period_us:u16LE}
-//       Actions: 00 NOTHING, 01 PUSH_DOWN, 02 PUSH_UP, FF STOP_CLEAR.
-//   - GET_ALL (0x02, LEN 0):
-//       Triggers GET_ACK (0x82, LEN 0) + GET_DATA (0x42, LEN 63) group.
-//   - Any other TYPE -> NACK (0xC0, LEN 2, reason 01 UNSUPPORTED).
-//   - Wrong length -> NACK (reason 02 BAD_LENGTH).
-//   - Invalid duty, unknown action, steps=0, half_period=0 -> NACK (reason 03 BAD_VALUE).
-//   - PUSH when axis queue full -> NACK (reason 04 QUEUE_FULL).
-//
-// Atomic SET_ACCEPT / GET_ACCEPT:
-//   Irrevocably admitted only after all checks pass.
-//   Reloads watchdog; publishes duty bank & motor segments.
 module mrdimm_transaction_core #(
 module mrdimm_transaction_core #(
-    parameter integer ADDR_WIDTH = 8
-) (
+    parameter ADDR_WIDTH = 8
+)(
     input  wire        clk,
     input  wire        clk,
     input  wire        rst_n,
     input  wire        rst_n,
 
 
-    // Request interface from host_uart_endpoint
+    // Host Request Interface (from host_uart_endpoint)
     input  wire        req_valid,
     input  wire        req_valid,
-    output reg         req_ready,
     input  wire [7:0]  req_type,
     input  wire [7:0]  req_type,
     input  wire [7:0]  req_length,
     input  wire [7:0]  req_length,
+    output reg         req_ready,
+
+    // Request RAM Read Port (512x8 BRAM in host_uart_endpoint)
     output reg  [7:0]  req_ram_rd_addr,
     output reg  [7:0]  req_ram_rd_addr,
     input  wire [7:0]  req_ram_rd_data,
     input  wire [7:0]  req_ram_rd_data,
 
 
-    // Watchdog reload pulse
+    // Watchdog reload (asserted at SET_ACCEPT / legal GET_ACCEPT instant)
     output reg         watchdog_reload,
     output reg         watchdog_reload,
 
 
-    // Duty staging & commit interface (to duty_register_bank)
+    // Duty Register Bank staging interface (PWM duties: 4 FAN, 4 PTC)
     output reg         stage_we,
     output reg         stage_we,
     output reg  [2:0]  stage_addr,
     output reg  [2:0]  stage_addr,
     output reg  [7:0]  stage_data,
     output reg  [7:0]  stage_data,
     output reg         stage_commit,
     output reg         stage_commit,
 
 
-    // Actuator enqueue & stop interface (to mrdimm_actuator_core)
+    // Actuator Core Queue & Command Interface (12 axes)
     output reg         act_enqueue_we,
     output reg         act_enqueue_we,
     output reg  [3:0]  act_enqueue_axis,
     output reg  [3:0]  act_enqueue_axis,
     output reg  [15:0] act_enqueue_steps,
     output reg  [15:0] act_enqueue_steps,
@@ -70,32 +62,30 @@ module mrdimm_transaction_core #(
     output reg  [ADDR_WIDTH-1:0] second_base,
     output reg  [ADDR_WIDTH-1:0] second_base,
     input  wire        group_done
     input  wire        group_done
 );
 );
-    localparam [3:0] ST_IDLE          = 4'd0;
-    localparam [3:0] ST_VAL_DUTY_PRE  = 4'd1;
-    localparam [3:0] ST_VAL_DUTY_READ = 4'd2;
-    localparam [3:0] ST_VAL_AXIS_PRE  = 4'd3;
-    localparam [3:0] ST_VAL_AXIS_READ = 4'd4;
-    localparam [3:0] ST_NACK_WRITE    = 4'd5;
-    localparam [3:0] ST_APPLY_DUTY    = 4'd6;
-    localparam [3:0] ST_APPLY_AXES    = 4'd7;
-    localparam [3:0] ST_WAIT_GET_DATA = 4'd8;
-    localparam [3:0] ST_WAIT_GROUP    = 4'd9;
+    localparam [3:0] ST_IDLE            = 4'd0;
+    localparam [3:0] ST_VAL_DUTY_PRE    = 4'd1;
+    localparam [3:0] ST_VAL_DUTY_READ   = 4'd2;
+    localparam [3:0] ST_VAL_AXIS_PRE    = 4'd3;
+    localparam [3:0] ST_VAL_AXIS_READ   = 4'd4;
+    localparam [3:0] ST_APPLY_DUTY_PRE  = 4'd5;
+    localparam [3:0] ST_APPLY_DUTY_READ = 4'd6;
+    localparam [3:0] ST_APPLY_AXIS_PRE  = 4'd7;
+    localparam [3:0] ST_APPLY_AXIS_READ = 4'd8;
+    localparam [3:0] ST_WAIT_GET_DATA   = 4'd9;
+    localparam [3:0] ST_NACK_WRITE      = 4'd10;
+    localparam [3:0] ST_WAIT_GROUP      = 4'd11;
 
 
-    reg [3:0] state;
-    reg [7:0] active_req_type;
-    reg [7:0] nack_reason;
+    reg [3:0]  state;
+    reg [7:0]  active_req_type;
+    reg [7:0]  nack_reason;
+    reg [3:0]  idx;
+    reg [2:0]  slot_byte;
+    reg        nack_write_step;
 
 
-    // Staged SET_ALL values
-    reg [7:0]  staged_duties [0:7];
-    reg [7:0]  staged_action [0:11];
-    reg [15:0] staged_steps  [0:11];
-    reg [15:0] staged_half   [0:11];
-
-    reg [3:0] idx;
-    reg [2:0] slot_byte;
-    reg       nack_write_step;
-
-    integer i;
+    // Single active slot registers (shared across axes, only 40 bits total)
+    reg [7:0]  slot_action;
+    reg [15:0] slot_steps;
+    reg [15:0] slot_half;
 
 
     always @(posedge clk or negedge rst_n) begin
     always @(posedge clk or negedge rst_n) begin
         if (!rst_n) begin
         if (!rst_n) begin
@@ -130,12 +120,9 @@ module mrdimm_transaction_core #(
             idx <= 4'd0;
             idx <= 4'd0;
             slot_byte <= 3'd0;
             slot_byte <= 3'd0;
             nack_write_step <= 1'b0;
             nack_write_step <= 1'b0;
-            for (i = 0; i < 8; i = i + 1) staged_duties[i] <= 8'd0;
-            for (i = 0; i < 12; i = i + 1) begin
-                staged_action[i] <= 8'd0;
-                staged_steps[i] <= 16'd0;
-                staged_half[i] <= 16'd0;
-            end
+            slot_action <= 8'd0;
+            slot_steps <= 16'd0;
+            slot_half <= 16'd0;
         end else begin
         end else begin
             watchdog_reload <= 1'b0;
             watchdog_reload <= 1'b0;
             stage_we <= 1'b0;
             stage_we <= 1'b0;
@@ -183,9 +170,8 @@ module mrdimm_transaction_core #(
                     end
                     end
                 end
                 end
 
 
-                // --- Validation of SET_ALL: Duties 0..7 ---
+                // --- Validation Phase: Duties 0..7 ---
                 ST_VAL_DUTY_PRE: begin
                 ST_VAL_DUTY_PRE: begin
-                    // BRAM address registered, settling
                     state <= ST_VAL_DUTY_READ;
                     state <= ST_VAL_DUTY_READ;
                 end
                 end
 
 
@@ -195,7 +181,6 @@ module mrdimm_transaction_core #(
                         nack_write_step <= 1'b0;
                         nack_write_step <= 1'b0;
                         state <= ST_NACK_WRITE;
                         state <= ST_NACK_WRITE;
                     end else begin
                     end else begin
-                        staged_duties[idx[2:0]] <= req_ram_rd_data;
                         if (idx == 4'd7) begin
                         if (idx == 4'd7) begin
                             idx <= 4'd0;
                             idx <= 4'd0;
                             slot_byte <= 3'd0;
                             slot_byte <= 3'd0;
@@ -209,9 +194,8 @@ module mrdimm_transaction_core #(
                     end
                     end
                 end
                 end
 
 
-                // --- Validation of SET_ALL: 12 Axis slots ---
+                // --- Validation Phase: 12 Axis slots ---
                 ST_VAL_AXIS_PRE: begin
                 ST_VAL_AXIS_PRE: begin
-                    // Wait for BRAM address to settle
                     state <= ST_VAL_AXIS_READ;
                     state <= ST_VAL_AXIS_READ;
                 end
                 end
 
 
@@ -225,39 +209,34 @@ module mrdimm_transaction_core #(
                                 nack_write_step <= 1'b0;
                                 nack_write_step <= 1'b0;
                                 state <= ST_NACK_WRITE;
                                 state <= ST_NACK_WRITE;
                             end else begin
                             end else begin
-                                staged_action[idx] <= req_ram_rd_data;
+                                slot_action <= req_ram_rd_data;
                                 slot_byte <= 3'd1;
                                 slot_byte <= 3'd1;
                                 req_ram_rd_addr <= req_ram_rd_addr + 8'd1;
                                 req_ram_rd_addr <= req_ram_rd_addr + 8'd1;
                                 state <= ST_VAL_AXIS_PRE;
                                 state <= ST_VAL_AXIS_PRE;
                             end
                             end
                         end
                         end
                         3'd1: begin
                         3'd1: begin
-                            staged_steps[idx][7:0] <= req_ram_rd_data;
+                            slot_steps[7:0] <= req_ram_rd_data;
                             slot_byte <= 3'd2;
                             slot_byte <= 3'd2;
                             req_ram_rd_addr <= req_ram_rd_addr + 8'd1;
                             req_ram_rd_addr <= req_ram_rd_addr + 8'd1;
                             state <= ST_VAL_AXIS_PRE;
                             state <= ST_VAL_AXIS_PRE;
                         end
                         end
                         3'd2: begin
                         3'd2: begin
-                            staged_steps[idx][15:8] <= req_ram_rd_data;
+                            slot_steps[15:8] <= req_ram_rd_data;
                             slot_byte <= 3'd3;
                             slot_byte <= 3'd3;
                             req_ram_rd_addr <= req_ram_rd_addr + 8'd1;
                             req_ram_rd_addr <= req_ram_rd_addr + 8'd1;
                             state <= ST_VAL_AXIS_PRE;
                             state <= ST_VAL_AXIS_PRE;
                         end
                         end
                         3'd3: begin
                         3'd3: begin
-                            staged_half[idx][7:0] <= req_ram_rd_data;
+                            slot_half[7:0] <= req_ram_rd_data;
                             slot_byte <= 3'd4;
                             slot_byte <= 3'd4;
                             req_ram_rd_addr <= req_ram_rd_addr + 8'd1;
                             req_ram_rd_addr <= req_ram_rd_addr + 8'd1;
                             state <= ST_VAL_AXIS_PRE;
                             state <= ST_VAL_AXIS_PRE;
                         end
                         end
                         3'd4: begin
                         3'd4: begin
-                            staged_half[idx][15:8] <= req_ram_rd_data;
-                            // Check segment validity for PUSH_DOWN / PUSH_UP
-                            if (staged_action[idx] == 8'h01 || staged_action[idx] == 8'h02) begin
-                                if ({staged_steps[idx][15:8], staged_steps[idx][7:0]} == 16'd0) begin
-                                    nack_reason <= 8'd3; // BAD_VALUE
-                                    nack_write_step <= 1'b0;
-                                    state <= ST_NACK_WRITE;
-                                end else if ({req_ram_rd_data, staged_half[idx][7:0]} == 16'd0) begin
+                            slot_half[15:8] <= req_ram_rd_data;
+                            if (slot_action == 8'h01 || slot_action == 8'h02) begin
+                                if (slot_steps == 16'd0 || {req_ram_rd_data, slot_half[7:0]} == 16'd0) begin
                                     nack_reason <= 8'd3; // BAD_VALUE
                                     nack_reason <= 8'd3; // BAD_VALUE
                                     nack_write_step <= 1'b0;
                                     nack_write_step <= 1'b0;
                                     state <= ST_NACK_WRITE;
                                     state <= ST_NACK_WRITE;
@@ -266,14 +245,13 @@ module mrdimm_transaction_core #(
                                     nack_write_step <= 1'b0;
                                     nack_write_step <= 1'b0;
                                     state <= ST_NACK_WRITE;
                                     state <= ST_NACK_WRITE;
                                 end else begin
                                 end else begin
-                                    // Move to next axis or finish validation
                                     if (idx == 4'd11) begin
                                     if (idx == 4'd11) begin
-                                        // All-or-nothing validation completely passed!
-                                        // SET_ACCEPT instant!
+                                        // All checks passed: SET_ACCEPT instant!
                                         watchdog_reload <= 1'b1;
                                         watchdog_reload <= 1'b1;
                                         req_ready <= 1'b1;
                                         req_ready <= 1'b1;
                                         idx <= 4'd0;
                                         idx <= 4'd0;
-                                        state <= ST_APPLY_DUTY;
+                                        req_ram_rd_addr <= 8'd0;
+                                        state <= ST_APPLY_DUTY_PRE;
                                     end else begin
                                     end else begin
                                         idx <= idx + 4'd1;
                                         idx <= idx + 4'd1;
                                         slot_byte <= 3'd0;
                                         slot_byte <= 3'd0;
@@ -284,12 +262,12 @@ module mrdimm_transaction_core #(
                             end else begin
                             end else begin
                                 // NOTHING or STOP_CLEAR
                                 // NOTHING or STOP_CLEAR
                                 if (idx == 4'd11) begin
                                 if (idx == 4'd11) begin
-                                    // All-or-nothing validation completely passed!
-                                    // SET_ACCEPT instant!
+                                    // All checks passed: SET_ACCEPT instant!
                                     watchdog_reload <= 1'b1;
                                     watchdog_reload <= 1'b1;
                                     req_ready <= 1'b1;
                                     req_ready <= 1'b1;
                                     idx <= 4'd0;
                                     idx <= 4'd0;
-                                    state <= ST_APPLY_DUTY;
+                                    req_ram_rd_addr <= 8'd0;
+                                    state <= ST_APPLY_DUTY_PRE;
                                 end else begin
                                 end else begin
                                     idx <= idx + 4'd1;
                                     idx <= idx + 4'd1;
                                     slot_byte <= 3'd0;
                                     slot_byte <= 3'd0;
@@ -298,62 +276,99 @@ module mrdimm_transaction_core #(
                                 end
                                 end
                             end
                             end
                         end
                         end
+                        default: state <= ST_IDLE;
                     endcase
                     endcase
                 end
                 end
 
 
-                // --- Apply Duties to Duty Bank ---
-                ST_APPLY_DUTY: begin
+                // --- Application Phase: Duties 0..7 ---
+                ST_APPLY_DUTY_PRE: begin
+                    state <= ST_APPLY_DUTY_READ;
+                end
+
+                ST_APPLY_DUTY_READ: begin
                     stage_we <= 1'b1;
                     stage_we <= 1'b1;
                     stage_addr <= idx[2:0];
                     stage_addr <= idx[2:0];
-                    stage_data <= staged_duties[idx[2:0]];
+                    stage_data <= req_ram_rd_data;
                     if (idx == 4'd7) begin
                     if (idx == 4'd7) begin
                         stage_commit <= 1'b1;
                         stage_commit <= 1'b1;
-                        // Launch SET_ACK reply so it claims TX before any new segment can abort/finish
+                        // Assert SET_ACK group_valid so SET_ACK takes TX wire priority
                         group_type <= 8'h81;
                         group_type <= 8'h81;
                         group_length <= 8'd0;
                         group_length <= 8'd0;
                         group_second <= 1'b0;
                         group_second <= 1'b0;
                         group_base <= {ADDR_WIDTH{1'b0}};
                         group_base <= {ADDR_WIDTH{1'b0}};
                         group_valid <= 1'b1;
                         group_valid <= 1'b1;
                         idx <= 4'd0;
                         idx <= 4'd0;
-                        state <= ST_APPLY_AXES;
+                        slot_byte <= 3'd0;
+                        req_ram_rd_addr <= 8'd8;
+                        state <= ST_APPLY_AXIS_PRE;
                     end else begin
                     end else begin
                         idx <= idx + 4'd1;
                         idx <= idx + 4'd1;
+                        req_ram_rd_addr <= req_ram_rd_addr + 8'd1;
+                        state <= ST_APPLY_DUTY_PRE;
                     end
                     end
                 end
                 end
 
 
-                // --- Apply Segments / STOP_CLEAR to Actuator ---
-                ST_APPLY_AXES: begin
+                // --- Application Phase: 12 Axis slots ---
+                ST_APPLY_AXIS_PRE: begin
                     act_enqueue_we <= 1'b0;
                     act_enqueue_we <= 1'b0;
                     act_stop_clear_mask <= 12'd0;
                     act_stop_clear_mask <= 12'd0;
-                    if (staged_action[idx] == 8'hFF) begin
-                        act_stop_clear_mask[idx] <= 1'b1;
-                    end else if (staged_action[idx] == 8'h01 || staged_action[idx] == 8'h02) begin
-                        act_enqueue_we <= 1'b1;
-                        act_enqueue_axis <= idx;
-                        act_enqueue_steps <= staged_steps[idx];
-                        act_enqueue_half_period_us <= staged_half[idx];
-                        act_enqueue_dir <= (staged_action[idx] == 8'h02);
-                    end
-                    if (idx == 4'd11) begin
-                        state <= ST_WAIT_GROUP;
-                    end else begin
-                        idx <= idx + 4'd1;
-                    end
+                    state <= ST_APPLY_AXIS_READ;
                 end
                 end
 
 
-                ST_WAIT_GROUP: begin
+                ST_APPLY_AXIS_READ: begin
                     act_enqueue_we <= 1'b0;
                     act_enqueue_we <= 1'b0;
                     act_stop_clear_mask <= 12'd0;
                     act_stop_clear_mask <= 12'd0;
-                    if (group_done) begin
-                        group_valid <= 1'b0;
-                        state <= ST_IDLE;
-                    end
+                    case (slot_byte)
+                        3'd0: begin
+                            slot_action <= req_ram_rd_data;
+                            slot_byte <= 3'd1;
+                            req_ram_rd_addr <= req_ram_rd_addr + 8'd1;
+                            state <= ST_APPLY_AXIS_PRE;
+                        end
+                        3'd1: begin
+                            slot_steps[7:0] <= req_ram_rd_data;
+                            slot_byte <= 3'd2;
+                            req_ram_rd_addr <= req_ram_rd_addr + 8'd1;
+                            state <= ST_APPLY_AXIS_PRE;
+                        end
+                        3'd2: begin
+                            slot_steps[15:8] <= req_ram_rd_data;
+                            slot_byte <= 3'd3;
+                            req_ram_rd_addr <= req_ram_rd_addr + 8'd1;
+                            state <= ST_APPLY_AXIS_PRE;
+                        end
+                        3'd3: begin
+                            slot_half[7:0] <= req_ram_rd_data;
+                            slot_byte <= 3'd4;
+                            req_ram_rd_addr <= req_ram_rd_addr + 8'd1;
+                            state <= ST_APPLY_AXIS_PRE;
+                        end
+                        3'd4: begin
+                            if (slot_action == 8'hFF) begin
+                                act_stop_clear_mask[idx] <= 1'b1;
+                            end else if (slot_action == 8'h01 || slot_action == 8'h02) begin
+                                act_enqueue_we <= 1'b1;
+                                act_enqueue_axis <= idx;
+                                act_enqueue_steps <= slot_steps;
+                                act_enqueue_half_period_us <= {req_ram_rd_data, slot_half[7:0]};
+                                act_enqueue_dir <= (slot_action == 8'h02);
+                            end
+                            if (idx == 4'd11) begin
+                                state <= ST_WAIT_GROUP;
+                            end else begin
+                                idx <= idx + 4'd1;
+                                slot_byte <= 3'd0;
+                                req_ram_rd_addr <= req_ram_rd_addr + 8'd1;
+                                state <= ST_APPLY_AXIS_PRE;
+                            end
+                        end
+                        default: state <= ST_IDLE;
+                    endcase
                 end
                 end
 
 
                 // --- Wait for GET_DATA assembly ---
                 // --- Wait for GET_DATA assembly ---
                 ST_WAIT_GET_DATA: begin
                 ST_WAIT_GET_DATA: begin
                     if (get_data_done) begin
                     if (get_data_done) begin
-                        // Launch GET_ACK (0x82, 0) + GET_DATA (0x42, 63)
                         group_type <= 8'h82;
                         group_type <= 8'h82;
                         group_length <= 8'd0;
                         group_length <= 8'd0;
                         group_second <= 1'b1;
                         group_second <= 1'b1;
@@ -377,7 +392,6 @@ module mrdimm_transaction_core #(
                     end else begin
                     end else begin
                         nack_reply_addr <= 8'd1;
                         nack_reply_addr <= 8'd1;
                         nack_reply_data <= nack_reason;
                         nack_reply_data <= nack_reason;
-                        // Launch NACK (0xC0, 2)
                         group_type <= 8'hC0;
                         group_type <= 8'hC0;
                         group_length <= 8'd2;
                         group_length <= 8'd2;
                         group_second <= 1'b0;
                         group_second <= 1'b0;
@@ -389,6 +403,8 @@ module mrdimm_transaction_core #(
 
 
                 // --- Wait for Arbiter Group Completion ---
                 // --- Wait for Arbiter Group Completion ---
                 ST_WAIT_GROUP: begin
                 ST_WAIT_GROUP: begin
+                    act_enqueue_we <= 1'b0;
+                    act_stop_clear_mask <= 12'd0;
                     if (group_done) begin
                     if (group_done) begin
                         group_valid <= 1'b0;
                         group_valid <= 1'b0;
                         state <= ST_IDLE;
                         state <= ST_IDLE;