Merge pull request #3310 from robinsonb5-PRs/master
[yosys.git] / techlibs / common / techmap.v
1 /*
2 * yosys -- Yosys Open SYnthesis Suite
3 *
4 * Copyright (C) 2012 Claire Xenia Wolf <claire@yosyshq.com>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 *
18 * ---
19 *
20 * The internal logic cell technology mapper.
21 *
22 * This Verilog library contains the mapping of internal cells (e.g. $not with
23 * variable bit width) to the internal logic cells (such as the single bit $_NOT_
24 * gate). Usually this logic network is then mapped to the actual technology
25 * using e.g. the "abc" pass.
26 *
27 * Note that this library does not map $mem cells. They must be mapped to logic
28 * and $dff cells using the "memory_map" pass first. (Or map it to custom cells,
29 * which is of course highly recommended for larger memories.)
30 *
31 */
32
33 `define MIN(_a, _b) ((_a) < (_b) ? (_a) : (_b))
34 `define MAX(_a, _b) ((_a) > (_b) ? (_a) : (_b))
35
36
37 // --------------------------------------------------------
38 // Use simplemap for trivial cell types
39 // --------------------------------------------------------
40
41 (* techmap_simplemap *)
42 (* techmap_celltype = "$not $and $or $xor $xnor" *)
43 module _90_simplemap_bool_ops;
44 endmodule
45
46 (* techmap_simplemap *)
47 (* techmap_celltype = "$reduce_and $reduce_or $reduce_xor $reduce_xnor $reduce_bool" *)
48 module _90_simplemap_reduce_ops;
49 endmodule
50
51 (* techmap_simplemap *)
52 (* techmap_celltype = "$logic_not $logic_and $logic_or" *)
53 module _90_simplemap_logic_ops;
54 endmodule
55
56 (* techmap_simplemap *)
57 (* techmap_celltype = "$eq $eqx $ne $nex" *)
58 module _90_simplemap_compare_ops;
59 endmodule
60
61 (* techmap_simplemap *)
62 (* techmap_celltype = "$pos $slice $concat $mux $tribuf $bmux" *)
63 module _90_simplemap_various;
64 endmodule
65
66 (* techmap_simplemap *)
67 (* techmap_celltype = "$sr $ff $dff $dffe $adff $adffe $aldff $aldffe $sdff $sdffe $sdffce $dffsr $dffsre $dlatch $adlatch $dlatchsr" *)
68 module _90_simplemap_registers;
69 endmodule
70
71
72 // --------------------------------------------------------
73 // Shift operators
74 // --------------------------------------------------------
75
76 (* techmap_celltype = "$shr $shl $sshl $sshr" *)
77 module _90_shift_ops_shr_shl_sshl_sshr (A, B, Y);
78 parameter A_SIGNED = 0;
79 parameter B_SIGNED = 0;
80 parameter A_WIDTH = 1;
81 parameter B_WIDTH = 1;
82 parameter Y_WIDTH = 1;
83
84 parameter _TECHMAP_CELLTYPE_ = "";
85 localparam shift_left = _TECHMAP_CELLTYPE_ == "$shl" || _TECHMAP_CELLTYPE_ == "$sshl";
86 localparam sign_extend = A_SIGNED && _TECHMAP_CELLTYPE_ == "$sshr";
87
88 (* force_downto *)
89 input [A_WIDTH-1:0] A;
90 (* force_downto *)
91 input [B_WIDTH-1:0] B;
92 (* force_downto *)
93 output [Y_WIDTH-1:0] Y;
94
95 localparam WIDTH = `MAX(A_WIDTH, Y_WIDTH);
96 localparam BB_WIDTH = `MIN($clog2(shift_left ? Y_WIDTH : A_SIGNED ? WIDTH : A_WIDTH) + 1, B_WIDTH);
97
98 wire [1023:0] _TECHMAP_DO_00_ = "proc;;";
99 wire [1023:0] _TECHMAP_DO_01_ = "RECURSION; CONSTMAP; opt_muxtree; opt_expr -mux_undef -mux_bool -fine;;;";
100
101 integer i;
102 (* force_downto *)
103 reg [WIDTH-1:0] buffer;
104 reg overflow;
105
106 always @* begin
107 overflow = B_WIDTH > BB_WIDTH ? |B[B_WIDTH-1:BB_WIDTH] : 1'b0;
108 buffer = overflow ? {WIDTH{sign_extend ? A[A_WIDTH-1] : 1'b0}} : {{WIDTH-A_WIDTH{A_SIGNED ? A[A_WIDTH-1] : 1'b0}}, A};
109
110 for (i = 0; i < BB_WIDTH; i = i+1)
111 if (B[i]) begin
112 if (shift_left)
113 buffer = {buffer, (2**i)'b0};
114 else if (2**i < WIDTH)
115 buffer = {{2**i{sign_extend ? buffer[WIDTH-1] : 1'b0}}, buffer[WIDTH-1 : 2**i]};
116 else
117 buffer = {WIDTH{sign_extend ? buffer[WIDTH-1] : 1'b0}};
118 end
119 end
120
121 assign Y = buffer;
122 endmodule
123
124 (* techmap_celltype = "$shift $shiftx" *)
125 module _90_shift_shiftx (A, B, Y);
126 parameter A_SIGNED = 0;
127 parameter B_SIGNED = 0;
128 parameter A_WIDTH = 1;
129 parameter B_WIDTH = 1;
130 parameter Y_WIDTH = 1;
131
132 (* force_downto *)
133 input [A_WIDTH-1:0] A;
134 (* force_downto *)
135 input [B_WIDTH-1:0] B;
136 (* force_downto *)
137 output [Y_WIDTH-1:0] Y;
138
139 parameter _TECHMAP_CELLTYPE_ = "";
140 parameter [B_WIDTH-1:0] _TECHMAP_CONSTMSK_B_ = 0;
141 parameter [B_WIDTH-1:0] _TECHMAP_CONSTVAL_B_ = 0;
142
143 localparam extbit = _TECHMAP_CELLTYPE_ == "$shift" ? 1'b0 : 1'bx;
144 wire a_padding = _TECHMAP_CELLTYPE_ == "$shiftx" ? extbit : (A_SIGNED ? A[A_WIDTH-1] : 1'b0);
145
146 localparam BB_WIDTH = `MIN($clog2(`MAX(A_WIDTH, Y_WIDTH)) + (B_SIGNED ? 2 : 1), B_WIDTH);
147 localparam WIDTH = `MAX(A_WIDTH, Y_WIDTH) + (B_SIGNED ? 2**(BB_WIDTH-1) : 0);
148
149 wire [1023:0] _TECHMAP_DO_00_ = "proc;;";
150 wire [1023:0] _TECHMAP_DO_01_ = "CONSTMAP; opt_muxtree; opt_expr -mux_undef -mux_bool -fine;;;";
151
152 integer i;
153 (* force_downto *)
154 reg [WIDTH-1:0] buffer;
155 reg overflow;
156
157 always @* begin
158 overflow = 0;
159 buffer = {WIDTH{extbit}};
160 buffer[Y_WIDTH-1:0] = {Y_WIDTH{a_padding}};
161 buffer[A_WIDTH-1:0] = A;
162
163 if (B_WIDTH > BB_WIDTH) begin
164 if (B_SIGNED) begin
165 for (i = BB_WIDTH; i < B_WIDTH; i = i+1)
166 if (B[i] != B[BB_WIDTH-1])
167 overflow = 1;
168 end else
169 overflow = |B[B_WIDTH-1:BB_WIDTH];
170 if (overflow)
171 buffer = {WIDTH{extbit}};
172 end
173
174 if (B_SIGNED && B[BB_WIDTH-1])
175 buffer = {buffer, {2**(BB_WIDTH-1){extbit}}};
176
177 for (i = 0; i < (B_SIGNED ? BB_WIDTH-1 : BB_WIDTH); i = i+1)
178 if (B[i]) begin
179 if (2**i < WIDTH)
180 buffer = {{2**i{extbit}}, buffer[WIDTH-1 : 2**i]};
181 else
182 buffer = {WIDTH{extbit}};
183 end
184 end
185 assign Y = buffer;
186 endmodule
187
188
189 // --------------------------------------------------------
190 // Arithmetic operators
191 // --------------------------------------------------------
192
193 (* techmap_celltype = "$fa" *)
194 module _90_fa (A, B, C, X, Y);
195 parameter WIDTH = 1;
196
197 (* force_downto *)
198 input [WIDTH-1:0] A, B, C;
199 (* force_downto *)
200 output [WIDTH-1:0] X, Y;
201
202 (* force_downto *)
203 wire [WIDTH-1:0] t1, t2, t3;
204
205 assign t1 = A ^ B, t2 = A & B, t3 = C & t1;
206 assign Y = t1 ^ C, X = t2 | t3;
207 endmodule
208
209 (* techmap_celltype = "$lcu" *)
210 module _90_lcu (P, G, CI, CO);
211 parameter WIDTH = 2;
212
213 (* force_downto *)
214 input [WIDTH-1:0] P, G;
215 input CI;
216
217 (* force_downto *)
218 output [WIDTH-1:0] CO;
219
220 integer i, j;
221 (* force_downto *)
222 reg [WIDTH-1:0] p, g;
223
224 wire [1023:0] _TECHMAP_DO_ = "proc; opt -fast";
225
226 always @* begin
227 p = P;
228 g = G;
229
230 // in almost all cases CI will be constant zero
231 g[0] = g[0] | (p[0] & CI);
232
233 // [[CITE]] Brent Kung Adder
234 // R. P. Brent and H. T. Kung, "A Regular Layout for Parallel Adders",
235 // IEEE Transaction on Computers, Vol. C-31, No. 3, p. 260-264, March, 1982
236
237 // Main tree
238 for (i = 1; i <= $clog2(WIDTH); i = i+1) begin
239 for (j = 2**i - 1; j < WIDTH; j = j + 2**i) begin
240 g[j] = g[j] | p[j] & g[j - 2**(i-1)];
241 p[j] = p[j] & p[j - 2**(i-1)];
242 end
243 end
244
245 // Inverse tree
246 for (i = $clog2(WIDTH); i > 0; i = i-1) begin
247 for (j = 2**i + 2**(i-1) - 1; j < WIDTH; j = j + 2**i) begin
248 g[j] = g[j] | p[j] & g[j - 2**(i-1)];
249 p[j] = p[j] & p[j - 2**(i-1)];
250 end
251 end
252 end
253
254 assign CO = g;
255 endmodule
256
257 (* techmap_celltype = "$alu" *)
258 module _90_alu (A, B, CI, BI, X, Y, CO);
259 parameter A_SIGNED = 0;
260 parameter B_SIGNED = 0;
261 parameter A_WIDTH = 1;
262 parameter B_WIDTH = 1;
263 parameter Y_WIDTH = 1;
264
265 (* force_downto *)
266 input [A_WIDTH-1:0] A;
267 (* force_downto *)
268 input [B_WIDTH-1:0] B;
269 (* force_downto *)
270 output [Y_WIDTH-1:0] X, Y;
271
272 input CI, BI;
273 (* force_downto *)
274 output [Y_WIDTH-1:0] CO;
275
276 (* force_downto *)
277 wire [Y_WIDTH-1:0] AA = A_buf;
278 (* force_downto *)
279 wire [Y_WIDTH-1:0] BB = BI ? ~B_buf : B_buf;
280
281 (* force_downto *)
282 wire [Y_WIDTH-1:0] A_buf, B_buf;
283 \$pos #(.A_SIGNED(A_SIGNED), .A_WIDTH(A_WIDTH), .Y_WIDTH(Y_WIDTH)) A_conv (.A(A), .Y(A_buf));
284 \$pos #(.A_SIGNED(B_SIGNED), .A_WIDTH(B_WIDTH), .Y_WIDTH(Y_WIDTH)) B_conv (.A(B), .Y(B_buf));
285
286 \$lcu #(.WIDTH(Y_WIDTH)) lcu (.P(X), .G(AA & BB), .CI(CI), .CO(CO));
287
288 assign X = AA ^ BB;
289 assign Y = X ^ {CO, CI};
290 endmodule
291
292 (* techmap_maccmap *)
293 (* techmap_celltype = "$macc" *)
294 module _90_macc;
295 endmodule
296
297 (* techmap_wrap = "alumacc" *)
298 (* techmap_celltype = "$lt $le $ge $gt $add $sub $neg $mul" *)
299 module _90_alumacc;
300 endmodule
301
302
303 // --------------------------------------------------------
304 // Divide and Modulo
305 // --------------------------------------------------------
306
307 module \$__div_mod_u (A, B, Y, R);
308 parameter WIDTH = 1;
309
310 (* force_downto *)
311 input [WIDTH-1:0] A, B;
312 (* force_downto *)
313 output [WIDTH-1:0] Y, R;
314
315 (* force_downto *)
316 wire [WIDTH*WIDTH-1:0] chaindata;
317 assign R = chaindata[WIDTH*WIDTH-1:WIDTH*(WIDTH-1)];
318
319 genvar i;
320 generate begin
321 for (i = 0; i < WIDTH; i=i+1) begin:stage
322 (* force_downto *)
323 wire [WIDTH-1:0] stage_in;
324
325 if (i == 0) begin:cp
326 assign stage_in = A;
327 end else begin:cp
328 assign stage_in = chaindata[i*WIDTH-1:(i-1)*WIDTH];
329 end
330
331 assign Y[WIDTH-(i+1)] = stage_in >= {B, {WIDTH-(i+1){1'b0}}};
332 assign chaindata[(i+1)*WIDTH-1:i*WIDTH] = Y[WIDTH-(i+1)] ? stage_in - {B, {WIDTH-(i+1){1'b0}}} : stage_in;
333 end
334 end endgenerate
335 endmodule
336
337 // truncating signed division/modulo
338 module \$__div_mod_trunc (A, B, Y, R);
339 parameter A_SIGNED = 0;
340 parameter B_SIGNED = 0;
341 parameter A_WIDTH = 1;
342 parameter B_WIDTH = 1;
343 parameter Y_WIDTH = 1;
344
345 localparam WIDTH =
346 A_WIDTH >= B_WIDTH && A_WIDTH >= Y_WIDTH ? A_WIDTH :
347 B_WIDTH >= A_WIDTH && B_WIDTH >= Y_WIDTH ? B_WIDTH : Y_WIDTH;
348
349 (* force_downto *)
350 input [A_WIDTH-1:0] A;
351 (* force_downto *)
352 input [B_WIDTH-1:0] B;
353 (* force_downto *)
354 output [Y_WIDTH-1:0] Y, R;
355
356 (* force_downto *)
357 wire [WIDTH-1:0] A_buf, B_buf;
358 \$pos #(.A_SIGNED(A_SIGNED), .A_WIDTH(A_WIDTH), .Y_WIDTH(WIDTH)) A_conv (.A(A), .Y(A_buf));
359 \$pos #(.A_SIGNED(B_SIGNED), .A_WIDTH(B_WIDTH), .Y_WIDTH(WIDTH)) B_conv (.A(B), .Y(B_buf));
360
361 (* force_downto *)
362 wire [WIDTH-1:0] A_buf_u, B_buf_u, Y_u, R_u;
363 assign A_buf_u = A_SIGNED && A_buf[WIDTH-1] ? -A_buf : A_buf;
364 assign B_buf_u = B_SIGNED && B_buf[WIDTH-1] ? -B_buf : B_buf;
365
366 \$__div_mod_u #(
367 .WIDTH(WIDTH)
368 ) div_mod_u (
369 .A(A_buf_u),
370 .B(B_buf_u),
371 .Y(Y_u),
372 .R(R_u)
373 );
374
375 assign Y = A_SIGNED && B_SIGNED && (A_buf[WIDTH-1] != B_buf[WIDTH-1]) ? -Y_u : Y_u;
376 assign R = A_SIGNED && B_SIGNED && A_buf[WIDTH-1] ? -R_u : R_u;
377 endmodule
378
379 (* techmap_celltype = "$div" *)
380 module _90_div (A, B, Y);
381 parameter A_SIGNED = 0;
382 parameter B_SIGNED = 0;
383 parameter A_WIDTH = 1;
384 parameter B_WIDTH = 1;
385 parameter Y_WIDTH = 1;
386
387 (* force_downto *)
388 input [A_WIDTH-1:0] A;
389 (* force_downto *)
390 input [B_WIDTH-1:0] B;
391 (* force_downto *)
392 output [Y_WIDTH-1:0] Y;
393
394 \$__div_mod_trunc #(
395 .A_SIGNED(A_SIGNED),
396 .B_SIGNED(B_SIGNED),
397 .A_WIDTH(A_WIDTH),
398 .B_WIDTH(B_WIDTH),
399 .Y_WIDTH(Y_WIDTH)
400 ) div_mod (
401 .A(A),
402 .B(B),
403 .Y(Y)
404 );
405 endmodule
406
407 (* techmap_celltype = "$mod" *)
408 module _90_mod (A, B, Y);
409 parameter A_SIGNED = 0;
410 parameter B_SIGNED = 0;
411 parameter A_WIDTH = 1;
412 parameter B_WIDTH = 1;
413 parameter Y_WIDTH = 1;
414
415 (* force_downto *)
416 input [A_WIDTH-1:0] A;
417 (* force_downto *)
418 input [B_WIDTH-1:0] B;
419 (* force_downto *)
420 output [Y_WIDTH-1:0] Y;
421
422 \$__div_mod_trunc #(
423 .A_SIGNED(A_SIGNED),
424 .B_SIGNED(B_SIGNED),
425 .A_WIDTH(A_WIDTH),
426 .B_WIDTH(B_WIDTH),
427 .Y_WIDTH(Y_WIDTH)
428 ) div_mod (
429 .A(A),
430 .B(B),
431 .R(Y)
432 );
433 endmodule
434
435 // flooring signed division/modulo
436 module \$__div_mod_floor (A, B, Y, R);
437 parameter A_SIGNED = 0;
438 parameter B_SIGNED = 0;
439 parameter A_WIDTH = 1;
440 parameter B_WIDTH = 1;
441 parameter Y_WIDTH = 1;
442
443 localparam WIDTH =
444 A_WIDTH >= B_WIDTH && A_WIDTH >= Y_WIDTH ? A_WIDTH :
445 B_WIDTH >= A_WIDTH && B_WIDTH >= Y_WIDTH ? B_WIDTH : Y_WIDTH;
446
447 input [A_WIDTH-1:0] A;
448 input [B_WIDTH-1:0] B;
449 output [Y_WIDTH-1:0] Y, R;
450
451 wire [WIDTH-1:0] A_buf, B_buf;
452 \$pos #(.A_SIGNED(A_SIGNED), .A_WIDTH(A_WIDTH), .Y_WIDTH(WIDTH)) A_conv (.A(A), .Y(A_buf));
453 \$pos #(.A_SIGNED(B_SIGNED), .A_WIDTH(B_WIDTH), .Y_WIDTH(WIDTH)) B_conv (.A(B), .Y(B_buf));
454
455 wire [WIDTH-1:0] A_buf_u, B_buf_u, Y_u, R_u, R_s;
456 assign A_buf_u = A_SIGNED && A_buf[WIDTH-1] ? -A_buf : A_buf;
457 assign B_buf_u = B_SIGNED && B_buf[WIDTH-1] ? -B_buf : B_buf;
458
459 \$__div_mod_u #(
460 .WIDTH(WIDTH)
461 ) div_mod_u (
462 .A(A_buf_u),
463 .B(B_buf_u),
464 .Y(Y_u),
465 .R(R_u)
466 );
467
468 // For negative results, if there was a remainder, subtract one to turn
469 // the round towards 0 into a round towards -inf
470 assign Y = A_SIGNED && B_SIGNED && (A_buf[WIDTH-1] != B_buf[WIDTH-1]) ? (R_u == 0 ? -Y_u : -Y_u-1) : Y_u;
471
472 // truncating modulo
473 assign R_s = A_SIGNED && B_SIGNED && A_buf[WIDTH-1] ? -R_u : R_u;
474 // Flooring modulo differs from truncating modulo only if it is nonzero and
475 // A and B have different signs - then `floor - trunc = B`
476 assign R = (R_s != 0) && A_SIGNED && B_SIGNED && (A_buf[WIDTH-1] != B_buf[WIDTH-1]) ? $signed(B_buf) + $signed(R_s) : R_s;
477 endmodule
478
479 (* techmap_celltype = "$divfloor" *)
480 module _90_divfloor (A, B, Y);
481 parameter A_SIGNED = 0;
482 parameter B_SIGNED = 0;
483 parameter A_WIDTH = 1;
484 parameter B_WIDTH = 1;
485 parameter Y_WIDTH = 1;
486
487 (* force_downto *)
488 input [A_WIDTH-1:0] A;
489 (* force_downto *)
490 input [B_WIDTH-1:0] B;
491 (* force_downto *)
492 output [Y_WIDTH-1:0] Y;
493
494 \$__div_mod_floor #(
495 .A_SIGNED(A_SIGNED),
496 .B_SIGNED(B_SIGNED),
497 .A_WIDTH(A_WIDTH),
498 .B_WIDTH(B_WIDTH),
499 .Y_WIDTH(Y_WIDTH)
500 ) div_mod (
501 .A(A),
502 .B(B),
503 .Y(Y)
504 );
505 endmodule
506
507 (* techmap_celltype = "$modfloor" *)
508 module _90_modfloor (A, B, Y);
509 parameter A_SIGNED = 0;
510 parameter B_SIGNED = 0;
511 parameter A_WIDTH = 1;
512 parameter B_WIDTH = 1;
513 parameter Y_WIDTH = 1;
514
515 (* force_downto *)
516 input [A_WIDTH-1:0] A;
517 (* force_downto *)
518 input [B_WIDTH-1:0] B;
519 (* force_downto *)
520 output [Y_WIDTH-1:0] Y;
521
522 \$__div_mod_floor #(
523 .A_SIGNED(A_SIGNED),
524 .B_SIGNED(B_SIGNED),
525 .A_WIDTH(A_WIDTH),
526 .B_WIDTH(B_WIDTH),
527 .Y_WIDTH(Y_WIDTH)
528 ) div_mod (
529 .A(A),
530 .B(B),
531 .R(Y)
532 );
533 endmodule
534
535
536 // --------------------------------------------------------
537 // Power
538 // --------------------------------------------------------
539
540 (* techmap_celltype = "$pow" *)
541 module _90_pow (A, B, Y);
542 parameter A_SIGNED = 0;
543 parameter B_SIGNED = 0;
544 parameter A_WIDTH = 1;
545 parameter B_WIDTH = 1;
546 parameter Y_WIDTH = 1;
547
548 (* force_downto *)
549 input [A_WIDTH-1:0] A;
550 (* force_downto *)
551 input [B_WIDTH-1:0] B;
552 (* force_downto *)
553 output [Y_WIDTH-1:0] Y;
554
555 wire _TECHMAP_FAIL_ = 1;
556 endmodule
557
558
559 // --------------------------------------------------------
560 // Parallel Multiplexers
561 // --------------------------------------------------------
562
563 (* techmap_celltype = "$pmux" *)
564 module _90_pmux (A, B, S, Y);
565 parameter WIDTH = 1;
566 parameter S_WIDTH = 1;
567
568 (* force_downto *)
569 input [WIDTH-1:0] A;
570 (* force_downto *)
571 input [WIDTH*S_WIDTH-1:0] B;
572 (* force_downto *)
573 input [S_WIDTH-1:0] S;
574 (* force_downto *)
575 output [WIDTH-1:0] Y;
576
577 (* force_downto *)
578 wire [WIDTH-1:0] Y_B;
579
580 genvar i, j;
581 generate
582 (* force_downto *)
583 wire [WIDTH*S_WIDTH-1:0] B_AND_S;
584 for (i = 0; i < S_WIDTH; i = i + 1) begin:B_AND
585 assign B_AND_S[WIDTH*(i+1)-1:WIDTH*i] = B[WIDTH*(i+1)-1:WIDTH*i] & {WIDTH{S[i]}};
586 end:B_AND
587 for (i = 0; i < WIDTH; i = i + 1) begin:B_OR
588 (* force_downto *)
589 wire [S_WIDTH-1:0] B_AND_BITS;
590 for (j = 0; j < S_WIDTH; j = j + 1) begin:B_AND_BITS_COLLECT
591 assign B_AND_BITS[j] = B_AND_S[WIDTH*j+i];
592 end:B_AND_BITS_COLLECT
593 assign Y_B[i] = |B_AND_BITS;
594 end:B_OR
595 endgenerate
596
597 assign Y = |S ? Y_B : A;
598 endmodule
599
600 // --------------------------------------------------------
601 // Demultiplexers
602 // --------------------------------------------------------
603
604 (* techmap_celltype = "$demux" *)
605 module _90_demux (A, S, Y);
606 parameter WIDTH = 1;
607 parameter S_WIDTH = 1;
608
609 (* force_downto *)
610 input [WIDTH-1:0] A;
611 (* force_downto *)
612 input [S_WIDTH-1:0] S;
613 (* force_downto *)
614 output [(WIDTH << S_WIDTH)-1:0] Y;
615
616 generate
617 if (S_WIDTH == 0) begin
618 assign Y = A;
619 end else if (S_WIDTH == 1) begin
620 assign Y[0+:WIDTH] = S ? 0 : A;
621 assign Y[WIDTH+:WIDTH] = S ? A : 0;
622 end else begin
623 localparam SPLIT = S_WIDTH / 2;
624 wire [(1 << (S_WIDTH-SPLIT))-1:0] YH;
625 wire [(1 << SPLIT)-1:0] YL;
626 $demux #(.WIDTH(1), .S_WIDTH(SPLIT)) lo (.A(1'b1), .S(S[SPLIT-1:0]), .Y(YL));
627 $demux #(.WIDTH(1), .S_WIDTH(S_WIDTH-SPLIT)) hi (.A(1'b1), .S(S[S_WIDTH-1:SPLIT]), .Y(YH));
628 genvar i;
629 for (i = 0; i < (1 << S_WIDTH); i = i + 1) begin
630 localparam [S_WIDTH-1:0] IDX = i;
631 assign Y[i*WIDTH+:WIDTH] = (YL[IDX[SPLIT-1:0]] & YH[IDX[S_WIDTH-1:SPLIT]]) ? A : 0;
632 end
633 end
634 endgenerate
635 endmodule
636
637
638 // --------------------------------------------------------
639 // LUTs
640 // --------------------------------------------------------
641
642 `ifndef NOLUT
643 (* techmap_simplemap *)
644 (* techmap_celltype = "$lut $sop" *)
645 module _90_lut;
646 endmodule
647 `endif
648