-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdsp48e1_ternary.v
More file actions
313 lines (285 loc) · 10.6 KB
/
dsp48e1_ternary.v
File metadata and controls
313 lines (285 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
// ═══════════════════════════════════════════════════════════════════════════════
// TRINITY DSP48E1 WRAPPER — Ternary Acceleration Module
// ═══════════════════════════════════════════════════════════════════════════════
//
// Xilinx 7-series DSP48E1: 25-bit × 18-bit multiplier with 48-bit accumulator
// Optimized for ternary computing: {-1, 0, +1} × {-1, 0, +1} operations
//
// φ² + 1/φ² = 3
// ═══════════════════════════════════════════════════════════════════════════════
// DSP48E1 Primitive (Xilinx 7-series)
// 25-bit A input × 18-bit B input → 48-bit P output
// With pre-adder (A+B), pattern detector, and accumulator
(* DSP48E1 = "YES" *)
module DSP48E1_ternary (
input wire CLK,
input wire RST,
// Data inputs
input wire [29:0] A, // 30-bit A input (using 25 LSB for multiply)
input wire [17:0] B, // 18-bit B input (full width)
input wire [47:0] C, // 48-bit C input (for pre-add or accumulator)
input wire [47:0] D, // 48-bit D input (for pattern detector)
// Control inputs
input wire [4:0] OPMODE, // Operation mode
input wire [3:0] ALUMODE, // ALU control mode
input wire [1:0] INMODE, // Input pipeline mode
// Controls
input wire CARRYIN, // Carry in
input wire CEA1, CEA2, // Clock enables for A
input wire CEB1, CEB2, // Clock enables for B
input wire CEC, // Clock enable for C
input wire CED, // Clock enable for D
input wire CEP, // Clock enable for P
input wire RSTA, RSTB, RSTC, RSTD, RSTP, // Resets
// Outputs
output wire [47:0] P, // 48-bit product output
output wire [8:0] CARRYOUT, // Carry out
output wire [3:0] CARRYCAS, // Cascade carry
output wire [11:0] MULTSIGN, // Multiplier sign extension
output wire PATTERNBDETECT, // Pattern detect output
output wire UNDERFLOW, OVERFLOW // Overflow indicators
);
// DSP48E1 primitive instantiation
DSP48E1 #(
.ALUMODE_DETECT("ALUMODE"),
.AUTORESET_PATDET("NO_RESET"),
.A_INPUT("DIRECT"),
.B_INPUT("DIRECT"),
.CARRYIN_SEL("OPMODE5"),
.CARRYINREG(1),
.CARRYOUTREG(0),
.CLK_INVERTED(1'b0),
.DREG(1),
.DSP48E1_ONLY(1'b0),
.IS_ALUMODE_INVERTED(4'b0000),
.IS_CARRYIN_INVERTED(1'b0),
.IS_CEA1_INVERTED(1'b0),
.IS_CEA2_INVERTED(1'b0),
.IS_CEB1_INVERTED(1'b0),
.IS_CEB2_INVERTED(1'b0),
.IS_CEC_INVERTED(1'b0),
.IS_CED_INVERTED(1'b0),
.IS_CEP_INVERTED(1'b0),
.IS_INMODE_INVERTED(2'b00),
.IS_OPMODE_INVERTED(5'b00000),
.IS_P_INVERTED(1'b0),
.IS_RSTA_INVERTED(1'b0),
.IS_RSTB_INVERTED(1'b0),
.IS_RSTC_INVERTED(1'b0),
.IS_RSTD_INVERTED(1'b0),
.IS_RSTP_INVERTED(1'b0),
.MASK(48'h3FFFFFFFFFFFF),
.MREG(1),
.OPMODESEL(1'b0),
.PATTERN_DETECT(48'h000000000000),
.PREG(1),
.SEL_MASK("MASK"),
.SEL_PATTERN("PATTERN"),
.USE_DPORT("TRUE"),
.USE_MULT("MULTIPLY"),
.USE_PATTERN_DETECT("NO_PATDET"),
.USE_SIMD("ONE48")
) dsp (
.A({14'b0, A[24:0]}), // 30-bit to 25-bit (use LSB)
.B(B),
.C(C),
.D(D),
.ALUMODE(ALUMODE),
.CARRYIN(CARRYIN),
.CARRYOUT(CARRYOUT),
.CLK(CLK),
.INMODE(INMODE),
.OPMODE(OPMODE),
.P(P),
.PATTERNBDETECT(PATTERNBDETECT),
.UNDERFLOW(UNDERFLOW),
.OVERFLOW(OVERFLOW),
.CEA1(CEA1),
.CEA2(CEA2),
.CEB1(CEB1),
.CEB2(CEB2),
.CEC(CEC),
.CED(CED),
.CEP(CEP),
.RSTA(RSTA),
.RSTB(RSTB),
.RSTC(RSTC),
.RSTD(RSTD),
.RSTP(RSTP),
.CARRYCASCIN(),
.CARRYCASCOUT(),
.MULTSIGNIN(),
.MULTSIGNOUT()
);
endmodule
// ═══════════════════════════════════════════════════════════════════════════════
// TERNARY DSP MULTIPLIER
// ═══════════════════════════════════════════════════════════════════════════════
// Convert trit {-1, 0, +1} to 2-bit encoding for DSP
// Encoding: 00 = -1, 01 = 0, 10 = +1, 11 = reserved
function [17:0] trit_to_dsp;
input [1:0] trit;
begin
case (trit)
2'b00: trit_to_dsp = 18'h3FFFF; // -1 → all 1s (two's complement)
2'b01: trit_to_dsp = 18'h00000; // 0
2'b10: trit_to_dsp = 18'h00001; // +1
default: trit_to_dsp = 18'h00000;
endcase
end
endfunction
// DSP-accelerated ternary multiplication
// Uses 25x18 DSP multiply for {-1, 0, +1} operations
module ternary_dsp_mult (
input wire CLK,
input wire RST,
input wire valid_in,
input wire [1:0] trit_a, // Input trit A
input wire [1:0] trit_b, // Input trit B
output reg [1:0] trit_out, // Output trit
output reg valid_out
);
// Convert trits to DSP encoding
wire [17:0] dsp_a = trit_to_dsp(trit_a);
wire [17:0] dsp_b = trit_to_dsp(trit_b);
// DSP multiply result (48-bit)
wire [47:0] dsp_p;
// OPMODE for simple multiply: Z*Z + 0 (OPMODE[4:0] = 00000)
wire [4:0] opmode = 5'b00000;
// Instantiate DSP48E1
DSP48E1_ternary dsp (
.CLK(CLK),
.RST(RST),
.A(30'b0),
.B(dsp_b),
.C(48'b0),
.D(48'b0),
.OPMODE(opmode),
.ALUMODE(4'b0000),
.INMODE(2'b00),
.CARRYIN(1'b0),
.CEA1(1'b0),
.CEA2(1'b0),
.CEB1(valid_in),
.CEB2(1'b0),
.CEC(1'b0),
.CED(1'b0),
.CEP(1'b1),
.RSTA(RST),
.RSTB(RST),
.RSTC(1'b0),
.RSTD(1'b0),
.RSTP(RST),
.P(dsp_p)
);
// Convert DSP result back to trit
// Check sign bit of result
always @(posedge CLK) begin
if (RST) begin
trit_out <= 2'b01; // 0
valid_out <= 1'b0;
end else begin
valid_out <= valid_in;
if (dsp_p[47]) begin // Negative result → -1
trit_out <= 2'b00;
end else if (dsp_p != 0) begin // Positive → +1
trit_out <= 2'b10;
end else begin
trit_out <= 2'b01; // Zero
end
end
end
endmodule
// ═══════════════════════════════════════════════════════════════════════════════
// TERNARY VECTOR-MATRIX MULTIPLY (DSP ACCELERATED)
// ═══════════════════════════════════════════════════════════════════════════════
// Compute: result = weights × input
// weights: N×M trits {-1, 0, +1}
// input: M int8 values
// Uses DSP48E1 for parallel multiply-accumulate
module ternary_vecmat_dsp #(
parameter N = 16, // Vector size
parameter M = 16 // Input size
)(
input wire CLK,
input wire RST,
input wire valid_in,
input wire [(N*M)-1:0] trit_weights, // N×M trits (packed 2-bit)
input wire [M*8-1:0] int8_input, // M int8 values
output reg [31:0] accumulator [N], // N accumulated results
output reg valid_out
);
// Unpack weights (2-bit per trit)
wire [1:0] weights [N-1:0][M-1:0];
genvar i, j;
generate for (i = 0; i < N; i = i + 1) begin : gen_unpack_weights
generate for (j = 0; j < M; j = j + 1) begin : gen_unpack_weight
assign weights[i][j] = trit_weights[(i*M + j)*2 +: 2];
end
end
// Unpack int8 input
wire signed [7:0] input_vals [M-1:0];
generate for (j = 0; j < M; j = j + 1) begin : gen_unpack_input
assign input_vals[j] = int8_input[j*8 +: 8];
end
// Pipeline stage 1: Sign-extend int8 to 18-bit for DSP B input
reg signed [17:0] b_ext [M-1:0];
reg [1:0] a_trit [N-1:0][M-1:0];
reg valid_s1;
always @(posedge CLK) begin
if (RST) begin
valid_s1 <= 1'b0;
end else begin
valid_s1 <= valid_in;
for (j = 0; j < M; j = j + 1) begin
b_ext[j] <= {{9{input_vals[j][7]}}, input_vals[j]}; // Sign extend
end
for (i = 0; i < N; i = i + 1) begin
for (j = 0; j < M; j = j + 1) begin
a_trit[i][j] <= weights[i][j];
end
end
end
end
// Pipeline stage 2: DSP multiply-accumulate (using adder tree)
reg signed [47:0] mac_partial [N-1:0];
reg valid_s2;
// Generate DSP instances for each output element
generate for (i = 0; i < N; i = i + 1) begin : gen_dsp_row
reg signed [47:0] sum;
integer k;
always @(*) begin
sum = 0;
for (k = 0; k < M; k = k + 1) begin
case (a_trit[i][k])
2'b00: sum = sum - {{40{b_ext[k][17]}}, b_ext[k]}; // -1 × x
2'b01: ; // 0 × x = 0
2'b10: sum = sum + {{40{b_ext[k][17]}}, b_ext[k]}; // +1 × x
endcase
end
end
always @(posedge CLK) begin
if (RST) begin
mac_partial[i] <= 0;
end else begin
mac_partial[i] <= sum;
end
end
end
// Pipeline stage 3: Accumulate
always @(posedge CLK) begin
if (RST) begin
valid_s2 <= 1'b0;
valid_out <= 1'b0;
for (i = 0; i < N; i = i + 1) begin
accumulator[i] <= 0;
end
end else begin
valid_s2 <= valid_s1;
valid_out <= valid_s2;
for (i = 0; i < N; i = i + 1) begin
accumulator[i] <= accumulator[i] + mac_partial[i][31:0];
end
end
end
endmodule