2 * yosys -- Yosys Open SYnthesis Suite
4 * Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
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.
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.
20 * The internal logic cell technology mapper.
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.
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.)
33 `define MIN(_a, _b) ((_a) < (_b) ? (_a) : (_b))
34 `define MAX(_a, _b) ((_a) > (_b) ? (_a) : (_b))
37 // --------------------------------------------------------
38 // Use simplemap for trivial cell types
39 // --------------------------------------------------------
41 (* techmap_simplemap *)
42 (* techmap_celltype = "$not $and $or $xor $xnor" *)
43 module simplemap_bool_ops;
46 (* techmap_simplemap *)
47 (* techmap_celltype = "$reduce_and $reduce_or $reduce_xor $reduce_xnor $reduce_bool" *)
48 module simplemap_reduce_ops;
51 (* techmap_simplemap *)
52 (* techmap_celltype = "$logic_not $logic_and $logic_or" *)
53 module simplemap_logic_ops;
56 (* techmap_simplemap *)
57 (* techmap_celltype = "$pos $slice $concat $mux" *)
58 module simplemap_various;
61 (* techmap_simplemap *)
62 (* techmap_celltype = "$sr $dff $adff $dffsr $dlatch" *)
63 module simplemap_registers;
67 // --------------------------------------------------------
68 // Trivial substitutions
69 // --------------------------------------------------------
72 parameter A_SIGNED = 0;
73 parameter A_WIDTH = 1;
74 parameter Y_WIDTH = 1;
76 input [A_WIDTH-1:0] A;
77 output [Y_WIDTH-1:0] Y;
92 module \$ge (A, B, Y);
93 parameter A_SIGNED = 0;
94 parameter B_SIGNED = 0;
95 parameter A_WIDTH = 1;
96 parameter B_WIDTH = 1;
97 parameter Y_WIDTH = 1;
99 input [A_WIDTH-1:0] A;
100 input [B_WIDTH-1:0] B;
101 output [Y_WIDTH-1:0] Y;
109 ) _TECHMAP_REPLACE_ (
116 module \$gt (A, B, Y);
117 parameter A_SIGNED = 0;
118 parameter B_SIGNED = 0;
119 parameter A_WIDTH = 1;
120 parameter B_WIDTH = 1;
121 parameter Y_WIDTH = 1;
123 input [A_WIDTH-1:0] A;
124 input [B_WIDTH-1:0] B;
125 output [Y_WIDTH-1:0] Y;
133 ) _TECHMAP_REPLACE_ (
141 // --------------------------------------------------------
143 // --------------------------------------------------------
145 (* techmap_celltype = "$shr $shl $sshl $sshr" *)
146 module shift_ops_shr_shl_sshl_sshr (A, B, Y);
147 parameter A_SIGNED = 0;
148 parameter B_SIGNED = 0;
149 parameter A_WIDTH = 1;
150 parameter B_WIDTH = 1;
151 parameter Y_WIDTH = 1;
153 parameter _TECHMAP_CELLTYPE_ = "";
154 localparam shift_left = _TECHMAP_CELLTYPE_ == "$shl" || _TECHMAP_CELLTYPE_ == "$sshl";
155 localparam sign_extend = A_SIGNED && _TECHMAP_CELLTYPE_ == "$sshr";
157 input [A_WIDTH-1:0] A;
158 input [B_WIDTH-1:0] B;
159 output [Y_WIDTH-1:0] Y;
161 localparam WIDTH = `MAX(A_WIDTH, Y_WIDTH);
162 localparam BB_WIDTH = `MIN($clog2(shift_left ? Y_WIDTH : A_SIGNED ? WIDTH : A_WIDTH) + 1, B_WIDTH);
164 wire [1023:0] _TECHMAP_DO_00_ = "proc;;";
165 wire [1023:0] _TECHMAP_DO_01_ = "RECURSION; CONSTMAP; opt_muxtree; opt_const -mux_undef -mux_bool -fine;;;";
168 reg [WIDTH-1:0] buffer;
172 overflow = B_WIDTH > BB_WIDTH ? |B[B_WIDTH-1:BB_WIDTH] : 1'b0;
173 buffer = overflow ? {WIDTH{sign_extend ? A[A_WIDTH-1] : 1'b0}} : {{WIDTH-A_WIDTH{A_SIGNED ? A[A_WIDTH-1] : 1'b0}}, A};
175 for (i = 0; i < BB_WIDTH; i = i+1)
178 buffer = {buffer, (2**i)'b0};
179 else if (2**i < WIDTH)
180 buffer = {{2**i{sign_extend ? buffer[WIDTH-1] : 1'b0}}, buffer[WIDTH-1 : 2**i]};
182 buffer = {WIDTH{sign_extend ? buffer[WIDTH-1] : 1'b0}};
189 (* techmap_celltype = "$shift $shiftx" *)
190 module shift_shiftx (A, B, Y);
191 parameter A_SIGNED = 0;
192 parameter B_SIGNED = 0;
193 parameter A_WIDTH = 1;
194 parameter B_WIDTH = 1;
195 parameter Y_WIDTH = 1;
197 input [A_WIDTH-1:0] A;
198 input [B_WIDTH-1:0] B;
199 output [Y_WIDTH-1:0] Y;
201 localparam BB_WIDTH = `MIN($clog2(`MAX(A_WIDTH, Y_WIDTH)) + (B_SIGNED ? 2 : 1), B_WIDTH);
202 localparam WIDTH = `MAX(A_WIDTH, Y_WIDTH) + (B_SIGNED ? 2**(BB_WIDTH-1) : 0);
204 parameter _TECHMAP_CELLTYPE_ = "";
205 localparam extbit = _TECHMAP_CELLTYPE_ == "$shift" ? 1'b0 : 1'bx;
207 wire [1023:0] _TECHMAP_DO_00_ = "proc;;";
208 wire [1023:0] _TECHMAP_DO_01_ = "CONSTMAP; opt_muxtree; opt_const -mux_undef -mux_bool -fine;;;";
211 reg [WIDTH-1:0] buffer;
216 buffer = {WIDTH{extbit}};
217 buffer[`MAX(A_WIDTH, Y_WIDTH)-1:0] = A;
219 if (B_WIDTH > BB_WIDTH) begin
221 for (i = BB_WIDTH; i < B_WIDTH; i = i+1)
222 if (B[i] != B[BB_WIDTH-1])
225 overflow = |B[B_WIDTH-1:BB_WIDTH];
227 buffer = {WIDTH{extbit}};
230 for (i = BB_WIDTH-1; i >= 0; i = i-1)
232 if (B_SIGNED && i == BB_WIDTH-1)
233 buffer = {buffer, {2**i{extbit}}};
234 else if (2**i < WIDTH)
235 buffer = {{2**i{extbit}}, buffer[WIDTH-1 : 2**i]};
237 buffer = {WIDTH{extbit}};
245 // --------------------------------------------------------
246 // ALU Infrastructure
247 // --------------------------------------------------------
249 module \$fa (A, B, C, X, Y);
252 input [WIDTH-1:0] A, B, C;
253 output [WIDTH-1:0] X, Y;
255 wire [WIDTH-1:0] t1, t2, t3;
257 assign t1 = A ^ B, t2 = A & B, t3 = C & t1;
258 assign Y = t1 ^ C, X = t2 | t3;
261 module \$lcu (P, G, CI, CO);
264 input [WIDTH-1:0] P, G;
267 output [WIDTH-1:0] CO;
270 reg [WIDTH-1:0] p, g;
272 wire [1023:0] _TECHMAP_DO_ = "proc; opt -fast";
278 // in almost all cases CI will be constant zero
279 g[0] = g[0] | (p[0] & CI);
281 // [[CITE]] Brent Kung Adder
282 // R. P. Brent and H. T. Kung, “A Regular Layout for Parallel Adders”,
283 // IEEE Transaction on Computers, Vol. C-31, No. 3, p. 260-264, March, 1982
286 for (i = 1; i <= $clog2(WIDTH); i = i+1) begin
287 for (j = 2**i - 1; j < WIDTH; j = j + 2**i) begin
288 g[j] = g[j] | p[j] & g[j - 2**(i-1)];
289 p[j] = p[j] & p[j - 2**(i-1)];
294 for (i = $clog2(WIDTH); i > 0; i = i-1) begin
295 for (j = 2**i + 2**(i-1) - 1; j < WIDTH; j = j + 2**i) begin
296 g[j] = g[j] | p[j] & g[j - 2**(i-1)];
297 p[j] = p[j] & p[j - 2**(i-1)];
305 module \$alu (A, B, CI, BI, X, Y, CO);
306 parameter A_SIGNED = 0;
307 parameter B_SIGNED = 0;
308 parameter A_WIDTH = 1;
309 parameter B_WIDTH = 1;
310 parameter Y_WIDTH = 1;
312 input [A_WIDTH-1:0] A;
313 input [B_WIDTH-1:0] B;
314 output [Y_WIDTH-1:0] X, Y;
317 output [Y_WIDTH-1:0] CO;
319 wire [Y_WIDTH-1:0] A_buf, B_buf;
320 \$pos #(.A_SIGNED(A_SIGNED), .A_WIDTH(A_WIDTH), .Y_WIDTH(Y_WIDTH)) A_conv (.A(A), .Y(A_buf));
321 \$pos #(.A_SIGNED(B_SIGNED), .A_WIDTH(B_WIDTH), .Y_WIDTH(Y_WIDTH)) B_conv (.A(B), .Y(B_buf));
323 wire [Y_WIDTH-1:0] AA = A_buf;
324 wire [Y_WIDTH-1:0] BB = BI ? ~B_buf : B_buf;
326 \$lcu #(.WIDTH(Y_WIDTH)) lcu (.P(X), .G(AA & BB), .CI(CI), .CO(CO));
329 assign Y = X ^ {CO, CI};
333 // --------------------------------------------------------
334 // ALU Cell Types: Compare, Add, Subtract
335 // --------------------------------------------------------
337 `define ALU_COMMONS(_width, _sub) """
338 parameter A_SIGNED = 0;
339 parameter B_SIGNED = 0;
340 parameter A_WIDTH = 1;
341 parameter B_WIDTH = 1;
342 parameter Y_WIDTH = 1;
344 localparam WIDTH = _width;
346 input [A_WIDTH-1:0] A;
347 input [B_WIDTH-1:0] B;
348 output [Y_WIDTH-1:0] Y;
350 wire [WIDTH-1:0] alu_x, alu_y, alu_co;
351 wire [WIDTH:0] carry = {alu_co, |_sub};
370 assign cf = !carry[WIDTH];
371 assign of = carry[WIDTH] ^ carry[WIDTH-1];
372 assign sf = alu_y[WIDTH-1];
375 module \$lt (A, B, Y);
376 wire [1023:0] _TECHMAP_DO_ = "RECURSION; opt_const -mux_undef -mux_bool -fine;;;";
377 `ALU_COMMONS(`MAX(A_WIDTH, B_WIDTH), 1)
378 assign Y = A_SIGNED && B_SIGNED ? of != sf : cf;
381 module \$le (A, B, Y);
382 wire [1023:0] _TECHMAP_DO_ = "RECURSION; opt_const -mux_undef -mux_bool -fine;;;";
383 `ALU_COMMONS(`MAX(A_WIDTH, B_WIDTH), 1)
384 assign Y = &alu_x || (A_SIGNED && B_SIGNED ? of != sf : cf);
387 module \$add (A, B, Y);
388 wire [1023:0] _TECHMAP_DO_ = "RECURSION; opt_const -mux_undef -mux_bool -fine;;;";
389 `ALU_COMMONS(Y_WIDTH, 0)
393 module \$sub (A, B, Y);
394 wire [1023:0] _TECHMAP_DO_ = "RECURSION; opt_const -mux_undef -mux_bool -fine;;;";
395 `ALU_COMMONS(Y_WIDTH, 1)
400 // --------------------------------------------------------
402 // --------------------------------------------------------
404 (* techmap_maccmap *)
408 module \$mul (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;
415 input [A_WIDTH-1:0] A;
416 input [B_WIDTH-1:0] B;
417 output [Y_WIDTH-1:0] Y;
419 wire [1023:0] _TECHMAP_DO_ = "RECURSION; CONSTMAP; opt -purge";
421 localparam [ 3:0] CONFIG_WIDTH_BITS = 15;
422 localparam [ 0:0] CONFIG_IS_SIGNED = A_SIGNED && B_SIGNED;
423 localparam [ 0:0] CONFIG_DO_SUBTRACT = 0;
424 localparam [14:0] CONFIG_A_WIDTH = A_WIDTH;
425 localparam [14:0] CONFIG_B_WIDTH = B_WIDTH;
428 .CONFIG({CONFIG_B_WIDTH, CONFIG_A_WIDTH, CONFIG_DO_SUBTRACT, CONFIG_IS_SIGNED, CONFIG_WIDTH_BITS}),
429 .CONFIG_WIDTH(15 + 15 + 2 + 4),
430 .A_WIDTH(B_WIDTH + A_WIDTH),
433 ) _TECHMAP_REPLACE_ (
441 // --------------------------------------------------------
443 // --------------------------------------------------------
445 module \$__div_mod_u (A, B, Y, R);
448 input [WIDTH-1:0] A, B;
449 output [WIDTH-1:0] Y, R;
451 wire [WIDTH*WIDTH-1:0] chaindata;
452 assign R = chaindata[WIDTH*WIDTH-1:WIDTH*(WIDTH-1)];
456 for (i = 0; i < WIDTH; i=i+1) begin:stage
457 wire [WIDTH-1:0] stage_in;
462 assign stage_in = chaindata[i*WIDTH-1:(i-1)*WIDTH];
465 assign Y[WIDTH-(i+1)] = stage_in >= {B, {WIDTH-(i+1){1'b0}}};
466 assign chaindata[(i+1)*WIDTH-1:i*WIDTH] = Y[WIDTH-(i+1)] ? stage_in - {B, {WIDTH-(i+1){1'b0}}} : stage_in;
471 module \$__div_mod (A, B, Y, R);
472 parameter A_SIGNED = 0;
473 parameter B_SIGNED = 0;
474 parameter A_WIDTH = 1;
475 parameter B_WIDTH = 1;
476 parameter Y_WIDTH = 1;
479 A_WIDTH >= B_WIDTH && A_WIDTH >= Y_WIDTH ? A_WIDTH :
480 B_WIDTH >= A_WIDTH && B_WIDTH >= Y_WIDTH ? B_WIDTH : Y_WIDTH;
482 input [A_WIDTH-1:0] A;
483 input [B_WIDTH-1:0] B;
484 output [Y_WIDTH-1:0] Y, R;
486 wire [WIDTH-1:0] A_buf, B_buf;
487 \$pos #(.A_SIGNED(A_SIGNED), .A_WIDTH(A_WIDTH), .Y_WIDTH(WIDTH)) A_conv (.A(A), .Y(A_buf));
488 \$pos #(.A_SIGNED(B_SIGNED), .A_WIDTH(B_WIDTH), .Y_WIDTH(WIDTH)) B_conv (.A(B), .Y(B_buf));
490 wire [WIDTH-1:0] A_buf_u, B_buf_u, Y_u, R_u;
491 assign A_buf_u = A_SIGNED && A_buf[WIDTH-1] ? -A_buf : A_buf;
492 assign B_buf_u = B_SIGNED && B_buf[WIDTH-1] ? -B_buf : B_buf;
503 assign Y = A_SIGNED && B_SIGNED && (A_buf[WIDTH-1] != B_buf[WIDTH-1]) ? -Y_u : Y_u;
504 assign R = A_SIGNED && B_SIGNED && A_buf[WIDTH-1] ? -R_u : R_u;
507 module \$div (A, B, Y);
508 parameter A_SIGNED = 0;
509 parameter B_SIGNED = 0;
510 parameter A_WIDTH = 1;
511 parameter B_WIDTH = 1;
512 parameter Y_WIDTH = 1;
514 input [A_WIDTH-1:0] A;
515 input [B_WIDTH-1:0] B;
516 output [Y_WIDTH-1:0] Y;
531 module \$mod (A, B, Y);
532 parameter A_SIGNED = 0;
533 parameter B_SIGNED = 0;
534 parameter A_WIDTH = 1;
535 parameter B_WIDTH = 1;
536 parameter Y_WIDTH = 1;
538 input [A_WIDTH-1:0] A;
539 input [B_WIDTH-1:0] B;
540 output [Y_WIDTH-1:0] Y;
556 // --------------------------------------------------------
558 // --------------------------------------------------------
560 module \$pow (A, B, Y);
561 parameter A_SIGNED = 0;
562 parameter B_SIGNED = 0;
563 parameter A_WIDTH = 1;
564 parameter B_WIDTH = 1;
565 parameter Y_WIDTH = 1;
567 input [A_WIDTH-1:0] A;
568 input [B_WIDTH-1:0] B;
569 output [Y_WIDTH-1:0] Y;
571 wire _TECHMAP_FAIL_ = 1;
575 // --------------------------------------------------------
576 // Equal and Not-Equal
577 // --------------------------------------------------------
579 module \$eq (A, B, Y);
580 parameter A_SIGNED = 0;
581 parameter B_SIGNED = 0;
582 parameter A_WIDTH = 1;
583 parameter B_WIDTH = 1;
584 parameter Y_WIDTH = 1;
586 localparam WIDTH = A_WIDTH > B_WIDTH ? A_WIDTH : B_WIDTH;
588 input [A_WIDTH-1:0] A;
589 input [B_WIDTH-1:0] B;
590 output [Y_WIDTH-1:0] Y;
592 wire carry, carry_sign;
593 wire [WIDTH-1:0] A_buf, B_buf;
594 \$pos #(.A_SIGNED(A_SIGNED), .A_WIDTH(A_WIDTH), .Y_WIDTH(WIDTH)) A_conv (.A(A), .Y(A_buf));
595 \$pos #(.A_SIGNED(B_SIGNED), .A_WIDTH(B_WIDTH), .Y_WIDTH(WIDTH)) B_conv (.A(B), .Y(B_buf));
597 assign Y = ~|(A_buf ^ B_buf);
600 module \$ne (A, B, Y);
601 parameter A_SIGNED = 0;
602 parameter B_SIGNED = 0;
603 parameter A_WIDTH = 1;
604 parameter B_WIDTH = 1;
605 parameter Y_WIDTH = 1;
607 localparam WIDTH = A_WIDTH > B_WIDTH ? A_WIDTH : B_WIDTH;
609 input [A_WIDTH-1:0] A;
610 input [B_WIDTH-1:0] B;
611 output [Y_WIDTH-1:0] Y;
613 wire carry, carry_sign;
614 wire [WIDTH-1:0] A_buf, B_buf;
615 \$pos #(.A_SIGNED(A_SIGNED), .A_WIDTH(A_WIDTH), .Y_WIDTH(WIDTH)) A_conv (.A(A), .Y(A_buf));
616 \$pos #(.A_SIGNED(B_SIGNED), .A_WIDTH(B_WIDTH), .Y_WIDTH(WIDTH)) B_conv (.A(B), .Y(B_buf));
618 assign Y = |(A_buf ^ B_buf);
621 module \$eqx (A, B, Y);
622 parameter A_SIGNED = 0;
623 parameter B_SIGNED = 0;
624 parameter A_WIDTH = 1;
625 parameter B_WIDTH = 1;
626 parameter Y_WIDTH = 1;
628 localparam WIDTH = A_WIDTH > B_WIDTH ? A_WIDTH : B_WIDTH;
630 input [A_WIDTH-1:0] A;
631 input [B_WIDTH-1:0] B;
632 output [Y_WIDTH-1:0] Y;
634 wire carry, carry_sign;
635 wire [WIDTH-1:0] A_buf, B_buf;
636 \$pos #(.A_SIGNED(A_SIGNED), .A_WIDTH(A_WIDTH), .Y_WIDTH(WIDTH)) A_conv (.A(A), .Y(A_buf));
637 \$pos #(.A_SIGNED(B_SIGNED), .A_WIDTH(B_WIDTH), .Y_WIDTH(WIDTH)) B_conv (.A(B), .Y(B_buf));
639 assign Y = ~|(A_buf ^ B_buf);
642 module \$nex (A, B, Y);
643 parameter A_SIGNED = 0;
644 parameter B_SIGNED = 0;
645 parameter A_WIDTH = 1;
646 parameter B_WIDTH = 1;
647 parameter Y_WIDTH = 1;
649 localparam WIDTH = A_WIDTH > B_WIDTH ? A_WIDTH : B_WIDTH;
651 input [A_WIDTH-1:0] A;
652 input [B_WIDTH-1:0] B;
653 output [Y_WIDTH-1:0] Y;
655 wire carry, carry_sign;
656 wire [WIDTH-1:0] A_buf, B_buf;
657 \$pos #(.A_SIGNED(A_SIGNED), .A_WIDTH(A_WIDTH), .Y_WIDTH(WIDTH)) A_conv (.A(A), .Y(A_buf));
658 \$pos #(.A_SIGNED(B_SIGNED), .A_WIDTH(B_WIDTH), .Y_WIDTH(WIDTH)) B_conv (.A(B), .Y(B_buf));
660 assign Y = |(A_buf ^ B_buf);
664 // --------------------------------------------------------
665 // Parallel Multiplexers
666 // --------------------------------------------------------
668 module \$pmux (A, B, S, Y);
670 parameter S_WIDTH = 1;
673 input [WIDTH*S_WIDTH-1:0] B;
674 input [S_WIDTH-1:0] S;
675 output [WIDTH-1:0] Y;
677 wire [WIDTH-1:0] Y_B;
681 wire [WIDTH*S_WIDTH-1:0] B_AND_S;
682 for (i = 0; i < S_WIDTH; i = i + 1) begin:B_AND
683 assign B_AND_S[WIDTH*(i+1)-1:WIDTH*i] = B[WIDTH*(i+1)-1:WIDTH*i] & {WIDTH{S[i]}};
685 for (i = 0; i < WIDTH; i = i + 1) begin:B_OR
686 wire [S_WIDTH-1:0] B_AND_BITS;
687 for (j = 0; j < S_WIDTH; j = j + 1) begin:B_AND_BITS_COLLECT
688 assign B_AND_BITS[j] = B_AND_S[WIDTH*j+i];
689 end:B_AND_BITS_COLLECT
690 assign Y_B[i] = |B_AND_BITS;
694 assign Y = |S ? Y_B : A;
698 // --------------------------------------------------------
700 // --------------------------------------------------------