# Snapshot: verilog/clean7c/rtl/array/mlpv/e3b_packed_dot.sv

Copied 2026-11-17T17:32:37Z from the clean7c tree (verilog repo commit 5998a7f1, md5 ac36900db902c871feb73668c5057834). Read-only copy for review; the live file is in the RTL repo.

```systemverilog
// =====================================================================================================================
// e3b_packed_dot   (clean7c rtl/array/mlpv, WP3)
// =====================================================================================================================
// Imported from the shipped packaged copy ref/mlpv_1361/packaged_ip/src/e3b_packed_dot.sv md5 4b48ee7b83277efedb62c53bbb8835b8 (MLPV 0xE4B71361 (M1361, PDI e24deb3f)).
// Code = the shipped code; only this header, the items under 'clean7c edits' and the role include differ.
// Regenerate / verify: tools/wp3_import.py [--check]. Architecture + diagrams: docs/ARRAY.md.
//
// Purpose  : cross-lane PACKED DSP58 16-term dot product for the two lanes of a pair that share one activation chunk:
//            s0 = sum_i a_i*w0_i, s1 = sum_i a_i*w1_i, w = raw - (qz+1) in [-4, 2] (gptq2_dequant16). One DSP58 multiply per
//            element forms a_i*(w0_i + 2^23*w1_i); |sum| <= 2^21 so the low 23-bit field never overflows except by the
//            borrow of a negative F0, which the unpack corrects (F0 = sext(S[22:0]), F1 = S[45:23] + S[22]). Schedule: three
//            element groups (6 / 6 / 4) of e3b_dsp58_mac with the DSP's own A/B input registers (REG 0/1/2) as the systolic
//            skew and the W-mux C input as a second in-cascade addend, one e3b_dsp58_add (ONE58) joining them:
//            16 MAC + 1 adder = 17 DSP58 per 32 products, no fabric skew registers.
// Users    : u_array/g_grp[*].g_lane[li even].g_dual_mo.g_e3.g_pair.u_e3_e / u_e3_o (256 = 128 pairs x even/odd chunk).
// Latency  : LAT = 6: out_valid = in_valid delayed 6; s0/s1 registered at t = 6.
// Reset    : synchronous active-low on the valid pipe and on s0/s1 (the DSP58 registers have no reset: RST* tied 0).
// Instances in the image: 256 (resolved parameters: rtl/array/SHIPPED_PARAMS.tsv, rows 'mlpv e3b_packed_dot').
// Ports (generated from the port list; widths as declared, resolved values are in SHIPPED_PARAMS.tsv):
//   input  clk        1
//   input  rst_n      1
//   input  in_valid   1
//   input  a_chunk    [255:0]
//   input  qw0        [31:0]
//   input  qw1        [31:0]
//   input  qz         [1:0]
//   output s0         signed [25:0]
//   output s1         signed [25:0]
//   output out_valid  1
// clean7c edits:
//   - `ifdef E3_BEHAV arm removed (23 lines; no shipped build defines E3_BEHAV), `else text kept verbatim
//   - default RED_NOBORROW `E3B_RED_DEFAULT -> 1'b0 (the file-level `define E3B_RED_DEFAULT 1'b0 is gone: no `define outside config/)
// =====================================================================================================================
`timescale 1ns/1ps
`default_nettype none

(* keep_hierarchy = "yes" *)
module e3b_packed_dot #(
    parameter bit RED_NOBORROW = 1'b0   // RED arm only (sim): drop the F1 borrow correction
) (
    input  wire                 clk,
    input  wire                 rst_n,
    input  wire                 in_valid,
    input  wire [255:0]         a_chunk,      // 16 x signed INT16
    input  wire [31:0]          qw0, qw1,     // lane L, lane L+1 raw 2-bit weights
    input  wire [1:0]           qz,           // broadcast raw zero point
    output reg  signed [25:0]   s0, s1,
    output reg                  out_valid
);
    localparam int LAT = 6;

    // valid pipe: out_valid is in_valid delayed LAT cycles
    logic [LAT-2:0] vsr;
    always_ff @(posedge clk) begin
        if (!rst_n) begin vsr <= '0; out_valid <= 1'b0; end
        else begin vsr <= {vsr[LAT-3:0], in_valid}; out_valid <= vsr[LAT-2]; end
    end

    // dequant (same contract as gptq2_dequant16: w = raw - (qz+1), 3-bit signed)
    wire [2:0] zp1 = {1'b0, qz} + 3'd1;
    function automatic logic signed [2:0] deq(input logic [1:0] raw, input logic [2:0] z1);
        logic signed [3:0] d;
        d = $signed({2'b00, raw}) - $signed({1'b0, z1});
        return d[2:0];
    endfunction

    // ------------------------------------------------------------------
    // DSP58 primitive datapath.
    // ------------------------------------------------------------------
    // packed A operand per element: sext27(w0) + (w1 << 23)
    //   = {w1 - (w0<0) (4 bits), 20 x w0[2], w0 (3 bits)}   (the E3 packing)
    // dequant through the project's gptq2_dequant16 (same contract; keeps the
    // GPTQ_QZERO_RAW netlist rule's "gptq2_dequant16 present" tooth valid)
    wire [47:0] w0p, w1p;
    gptq2_dequant16 u_dq0 (.qw_word(qw0), .qz_word({30'b0, qz}), .n_idx(4'd0), .w_s_packed(w0p));
    gptq2_dequant16 u_dq1 (.qw_word(qw1), .qz_word({30'b0, qz}), .n_idx(4'd0), .w_s_packed(w1p));
    wire [26:0] A [0:15];
    for (genvar e = 0; e < 16; e = e + 1) begin : g_pack
        wire signed [2:0] w0 = $signed(w0p[e*3 +: 3]);
        wire signed [2:0] w1 = $signed(w1p[e*3 +: 3]);
        wire signed [3:0] top = $signed({w1[2], w1}) - $signed({3'b0, w0[2]});
        assign A[e] = {top, {20{w0[2]}}, w0};
    end

    // element operand helpers
    function automatic [33:0] opa(input [26:0] x); return {{7{x[26]}}, x}; endfunction
    `define E3B_B(e) {{8{a_chunk[(e)*16+15]}}, a_chunk[(e)*16 +: 16]}
    localparam [8:0] OP_M        = 9'b00_000_01_01;   // P = M
    localparam [8:0] OP_M_PC     = 9'b00_001_01_01;   // P = M + PCIN
    localparam [8:0] OP_M_PC_C   = 9'b11_001_01_01;   // P = M + PCIN + C  (W = C)
    localparam [8:0] OP_ABC_PCIN = 9'b00_001_11_11;   // P = A:B + C + PCIN

    wire [57:0] gp  [0:2];      // group partial P (valid t=4)
    wire [57:0] gpc [0:2];      // group partial PCOUT
    for (genvar g = 0; g < 3; g = g + 1) begin : g_grp
        localparam int E0 = 6*g;                 // groups: e0..5, e6..11, e12..15
        wire [57:0] m0_pc, s1_p, m1_pc, x0_pc, x1_p, nc_p0, nc_pc1, nc_p1, nc_pcx0, nc_px1, nc_pcx1, nc_ps1;
        e3b_dsp58_mac #(.REG(0), .OPM(OP_M))      u_m0 (.clk(clk), .a(opa(A[E0+0])), .b(`E3B_B(E0+0)), .c(58'b0), .pcin(58'b0), .p(nc_p0), .pcout(m0_pc));
        e3b_dsp58_mac #(.REG(0), .OPM(OP_M))      u_s1 (.clk(clk), .a(opa(A[E0+1])), .b(`E3B_B(E0+1)), .c(58'b0), .pcin(58'b0), .p(s1_p),  .pcout(nc_ps1));
        e3b_dsp58_mac #(.REG(1), .OPM(OP_M_PC_C)) u_m1 (.clk(clk), .a(opa(A[E0+2])), .b(`E3B_B(E0+2)), .c(s1_p),  .pcin(m0_pc), .p(nc_p1), .pcout(m1_pc));
        if (g < 2) begin : g_six
            e3b_dsp58_mac #(.REG(0), .OPM(OP_M))      u_x0 (.clk(clk), .a(opa(A[E0+3])), .b(`E3B_B(E0+3)), .c(58'b0), .pcin(58'b0), .p(), .pcout(x0_pc));
            e3b_dsp58_mac #(.REG(1), .OPM(OP_M_PC))   u_x1 (.clk(clk), .a(opa(A[E0+4])), .b(`E3B_B(E0+4)), .c(58'b0), .pcin(x0_pc), .p(x1_p), .pcout(nc_pcx1));
            e3b_dsp58_mac #(.REG(2), .OPM(OP_M_PC_C)) u_m2 (.clk(clk), .a(opa(A[E0+5])), .b(`E3B_B(E0+5)), .c(x1_p),  .pcin(m1_pc), .p(gp[g]), .pcout(gpc[g]));
        end else begin : g_four
            e3b_dsp58_mac #(.REG(2), .OPM(OP_M_PC))   u_m2 (.clk(clk), .a(opa(A[E0+3])), .b(`E3B_B(E0+3)), .c(58'b0), .pcin(m1_pc), .p(gp[g]), .pcout(gpc[g]));
        end
    end
    `undef E3B_B

    wire [57:0] r1p, nc_r1c;
    e3b_dsp58_add #(.REG(0), .OPM(OP_ABC_PCIN)) u_r1 (.clk(clk),
        .a(gp[0][57:24]), .b(gp[0][23:0]), .c(gp[1]), .pcin(gpc[2]), .p(r1p), .pcout(nc_r1c));

    always_ff @(posedge clk) begin
        if (!rst_n) begin s0 <= '0; s1 <= '0; end
        else begin
            s0 <= $signed(r1p[22:0]);
            s1 <= $signed(r1p[45:23]) + (RED_NOBORROW ? 26'sd0 : $signed({1'b0, r1p[22]}));
        end
    end
endmodule
`default_nettype wire
```
