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 _90_simplemap_bool_ops;
46 (* techmap_simplemap *)
47 (* techmap_celltype = "$reduce_and $reduce_or $reduce_xor $reduce_xnor $reduce_bool" *)
48 module _90_simplemap_reduce_ops;
51 (* techmap_simplemap *)
52 (* techmap_celltype = "$logic_not $logic_and $logic_or" *)
53 module _90_simplemap_logic_ops;
56 (* techmap_simplemap *)
57 (* techmap_celltype = "$eq $eqx $ne $nex" *)
58 module _90_simplemap_compare_ops;
61 (* techmap_simplemap *)
62 (* techmap_celltype = "$pos $slice $concat $mux $tribuf" *)
63 module _90_simplemap_various;
66 (* techmap_simplemap *)
67 (* techmap_celltype = "$sr $ff $dff $dffe $adff $dffsr $dlatch" *)
68 module _90_simplemap_registers;
72 // --------------------------------------------------------
74 // --------------------------------------------------------
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;
84 parameter _TECHMAP_CELLTYPE_ = "";
85 localparam shift_left = _TECHMAP_CELLTYPE_ == "$shl" || _TECHMAP_CELLTYPE_ == "$sshl";
86 localparam sign_extend = A_SIGNED && _TECHMAP_CELLTYPE_ == "$sshr";
88 input [A_WIDTH-1:0] A;
89 input [B_WIDTH-1:0] B;
90 output [Y_WIDTH-1:0] Y;
92 localparam WIDTH = `MAX(A_WIDTH, Y_WIDTH);
93 localparam BB_WIDTH = `MIN($clog2(shift_left ? Y_WIDTH : A_SIGNED ? WIDTH : A_WIDTH) + 1, B_WIDTH);
95 wire [1023:0] _TECHMAP_DO_00_ = "proc;;";
96 wire [1023:0] _TECHMAP_DO_01_ = "RECURSION; CONSTMAP; opt_muxtree; opt_expr -mux_undef -mux_bool -fine;;;";
99 reg [WIDTH-1:0] buffer;
103 overflow = B_WIDTH > BB_WIDTH ? |B[B_WIDTH-1:BB_WIDTH] : 1'b0;
104 buffer = overflow ? {WIDTH{sign_extend ? A[A_WIDTH-1] : 1'b0}} : {{WIDTH-A_WIDTH{A_SIGNED ? A[A_WIDTH-1] : 1'b0}}, A};
106 for (i = 0; i < BB_WIDTH; i = i+1)
109 buffer = {buffer, (2**i)'b0};
110 else if (2**i < WIDTH)
111 buffer = {{2**i{sign_extend ? buffer[WIDTH-1] : 1'b0}}, buffer[WIDTH-1 : 2**i]};
113 buffer = {WIDTH{sign_extend ? buffer[WIDTH-1] : 1'b0}};
120 (* techmap_celltype = "$shift $shiftx" *)
121 module _90_shift_shiftx (A, B, Y);
122 parameter A_SIGNED = 0;
123 parameter B_SIGNED = 0;
124 parameter A_WIDTH = 1;
125 parameter B_WIDTH = 1;
126 parameter Y_WIDTH = 1;
128 input [A_WIDTH-1:0] A;
129 input [B_WIDTH-1:0] B;
130 output [Y_WIDTH-1:0] Y;
132 localparam BB_WIDTH = `MIN($clog2(`MAX(A_WIDTH, Y_WIDTH)) + (B_SIGNED ? 2 : 1), B_WIDTH);
133 localparam WIDTH = `MAX(A_WIDTH, Y_WIDTH) + (B_SIGNED ? 2**(BB_WIDTH-1) : 0);
135 parameter _TECHMAP_CELLTYPE_ = "";
136 localparam extbit = _TECHMAP_CELLTYPE_ == "$shift" ? 1'b0 : 1'bx;
138 wire [1023:0] _TECHMAP_DO_00_ = "proc;;";
139 wire [1023:0] _TECHMAP_DO_01_ = "CONSTMAP; opt_muxtree; opt_expr -mux_undef -mux_bool -fine;;;";
142 reg [WIDTH-1:0] buffer;
147 buffer = {WIDTH{extbit}};
148 buffer[`MAX(A_WIDTH, Y_WIDTH)-1:0] = A;
150 if (B_WIDTH > BB_WIDTH) begin
152 for (i = BB_WIDTH; i < B_WIDTH; i = i+1)
153 if (B[i] != B[BB_WIDTH-1])
156 overflow = |B[B_WIDTH-1:BB_WIDTH];
158 buffer = {WIDTH{extbit}};
161 for (i = BB_WIDTH-1; i >= 0; i = i-1)
163 if (B_SIGNED && i == BB_WIDTH-1)
164 buffer = {buffer, {2**i{extbit}}};
165 else if (2**i < WIDTH)
166 buffer = {{2**i{extbit}}, buffer[WIDTH-1 : 2**i]};
168 buffer = {WIDTH{extbit}};
176 // --------------------------------------------------------
177 // Arithmetic operators
178 // --------------------------------------------------------
180 (* techmap_celltype = "$fa" *)
181 module _90_fa (A, B, C, X, Y);
184 input [WIDTH-1:0] A, B, C;
185 output [WIDTH-1:0] X, Y;
187 wire [WIDTH-1:0] t1, t2, t3;
189 assign t1 = A ^ B, t2 = A & B, t3 = C & t1;
190 assign Y = t1 ^ C, X = t2 | t3;
193 (* techmap_celltype = "$lcu" *)
194 module _90_lcu (P, G, CI, CO);
197 input [WIDTH-1:0] P, G;
200 output [WIDTH-1:0] CO;
203 reg [WIDTH-1:0] p, g;
205 wire [1023:0] _TECHMAP_DO_ = "proc; opt -fast";
211 // in almost all cases CI will be constant zero
212 g[0] = g[0] | (p[0] & CI);
214 // [[CITE]] Brent Kung Adder
215 // R. P. Brent and H. T. Kung, "A Regular Layout for Parallel Adders",
216 // IEEE Transaction on Computers, Vol. C-31, No. 3, p. 260-264, March, 1982
219 for (i = 1; i <= $clog2(WIDTH); i = i+1) begin
220 for (j = 2**i - 1; j < WIDTH; j = j + 2**i) begin
221 g[j] = g[j] | p[j] & g[j - 2**(i-1)];
222 p[j] = p[j] & p[j - 2**(i-1)];
227 for (i = $clog2(WIDTH); i > 0; i = i-1) begin
228 for (j = 2**i + 2**(i-1) - 1; j < WIDTH; j = j + 2**i) begin
229 g[j] = g[j] | p[j] & g[j - 2**(i-1)];
230 p[j] = p[j] & p[j - 2**(i-1)];
238 (* techmap_celltype = "$alu" *)
239 module _90_alu (A, B, CI, BI, X, Y, CO);
240 parameter A_SIGNED = 0;
241 parameter B_SIGNED = 0;
242 parameter A_WIDTH = 1;
243 parameter B_WIDTH = 1;
244 parameter Y_WIDTH = 1;
246 input [A_WIDTH-1:0] A;
247 input [B_WIDTH-1:0] B;
248 output [Y_WIDTH-1:0] X, Y;
251 output [Y_WIDTH-1:0] CO;
253 wire [Y_WIDTH-1:0] A_buf, B_buf;
254 \$pos #(.A_SIGNED(A_SIGNED), .A_WIDTH(A_WIDTH), .Y_WIDTH(Y_WIDTH)) A_conv (.A(A), .Y(A_buf));
255 \$pos #(.A_SIGNED(B_SIGNED), .A_WIDTH(B_WIDTH), .Y_WIDTH(Y_WIDTH)) B_conv (.A(B), .Y(B_buf));
257 wire [Y_WIDTH-1:0] AA = A_buf;
258 wire [Y_WIDTH-1:0] BB = BI ? ~B_buf : B_buf;
260 \$lcu #(.WIDTH(Y_WIDTH)) lcu (.P(X), .G(AA & BB), .CI(CI), .CO(CO));
263 assign Y = X ^ {CO, CI};
266 (* techmap_maccmap *)
267 (* techmap_celltype = "$macc" *)
271 (* techmap_wrap = "alumacc" *)
272 (* techmap_celltype = "$lt $le $ge $gt $add $sub $neg $mul" *)
277 // --------------------------------------------------------
279 // --------------------------------------------------------
281 module \$__div_mod_u (A, B, Y, R);
284 input [WIDTH-1:0] A, B;
285 output [WIDTH-1:0] Y, R;
287 wire [WIDTH*WIDTH-1:0] chaindata;
288 assign R = chaindata[WIDTH*WIDTH-1:WIDTH*(WIDTH-1)];
292 for (i = 0; i < WIDTH; i=i+1) begin:stage
293 wire [WIDTH-1:0] stage_in;
298 assign stage_in = chaindata[i*WIDTH-1:(i-1)*WIDTH];
301 assign Y[WIDTH-(i+1)] = stage_in >= {B, {WIDTH-(i+1){1'b0}}};
302 assign chaindata[(i+1)*WIDTH-1:i*WIDTH] = Y[WIDTH-(i+1)] ? stage_in - {B, {WIDTH-(i+1){1'b0}}} : stage_in;
307 module \$__div_mod (A, B, Y, R);
308 parameter A_SIGNED = 0;
309 parameter B_SIGNED = 0;
310 parameter A_WIDTH = 1;
311 parameter B_WIDTH = 1;
312 parameter Y_WIDTH = 1;
315 A_WIDTH >= B_WIDTH && A_WIDTH >= Y_WIDTH ? A_WIDTH :
316 B_WIDTH >= A_WIDTH && B_WIDTH >= Y_WIDTH ? B_WIDTH : Y_WIDTH;
318 input [A_WIDTH-1:0] A;
319 input [B_WIDTH-1:0] B;
320 output [Y_WIDTH-1:0] Y, R;
322 wire [WIDTH-1:0] A_buf, B_buf;
323 \$pos #(.A_SIGNED(A_SIGNED), .A_WIDTH(A_WIDTH), .Y_WIDTH(WIDTH)) A_conv (.A(A), .Y(A_buf));
324 \$pos #(.A_SIGNED(B_SIGNED), .A_WIDTH(B_WIDTH), .Y_WIDTH(WIDTH)) B_conv (.A(B), .Y(B_buf));
326 wire [WIDTH-1:0] A_buf_u, B_buf_u, Y_u, R_u;
327 assign A_buf_u = A_SIGNED && A_buf[WIDTH-1] ? -A_buf : A_buf;
328 assign B_buf_u = B_SIGNED && B_buf[WIDTH-1] ? -B_buf : B_buf;
339 assign Y = A_SIGNED && B_SIGNED && (A_buf[WIDTH-1] != B_buf[WIDTH-1]) ? -Y_u : Y_u;
340 assign R = A_SIGNED && B_SIGNED && A_buf[WIDTH-1] ? -R_u : R_u;
343 (* techmap_celltype = "$div" *)
344 module _90_div (A, B, Y);
345 parameter A_SIGNED = 0;
346 parameter B_SIGNED = 0;
347 parameter A_WIDTH = 1;
348 parameter B_WIDTH = 1;
349 parameter Y_WIDTH = 1;
351 input [A_WIDTH-1:0] A;
352 input [B_WIDTH-1:0] B;
353 output [Y_WIDTH-1:0] Y;
368 (* techmap_celltype = "$mod" *)
369 module _90_mod (A, B, Y);
370 parameter A_SIGNED = 0;
371 parameter B_SIGNED = 0;
372 parameter A_WIDTH = 1;
373 parameter B_WIDTH = 1;
374 parameter Y_WIDTH = 1;
376 input [A_WIDTH-1:0] A;
377 input [B_WIDTH-1:0] B;
378 output [Y_WIDTH-1:0] Y;
394 // --------------------------------------------------------
396 // --------------------------------------------------------
398 (* techmap_celltype = "$pow" *)
399 module _90_pow (A, B, Y);
400 parameter A_SIGNED = 0;
401 parameter B_SIGNED = 0;
402 parameter A_WIDTH = 1;
403 parameter B_WIDTH = 1;
404 parameter Y_WIDTH = 1;
406 input [A_WIDTH-1:0] A;
407 input [B_WIDTH-1:0] B;
408 output [Y_WIDTH-1:0] Y;
410 wire _TECHMAP_FAIL_ = 1;
414 // --------------------------------------------------------
415 // Parallel Multiplexers
416 // --------------------------------------------------------
418 (* techmap_celltype = "$pmux" *)
419 module _90_pmux (A, B, S, Y);
421 parameter S_WIDTH = 1;
424 input [WIDTH*S_WIDTH-1:0] B;
425 input [S_WIDTH-1:0] S;
426 output [WIDTH-1:0] Y;
428 wire [WIDTH-1:0] Y_B;
432 wire [WIDTH*S_WIDTH-1:0] B_AND_S;
433 for (i = 0; i < S_WIDTH; i = i + 1) begin:B_AND
434 assign B_AND_S[WIDTH*(i+1)-1:WIDTH*i] = B[WIDTH*(i+1)-1:WIDTH*i] & {WIDTH{S[i]}};
436 for (i = 0; i < WIDTH; i = i + 1) begin:B_OR
437 wire [S_WIDTH-1:0] B_AND_BITS;
438 for (j = 0; j < S_WIDTH; j = j + 1) begin:B_AND_BITS_COLLECT
439 assign B_AND_BITS[j] = B_AND_S[WIDTH*j+i];
440 end:B_AND_BITS_COLLECT
441 assign Y_B[i] = |B_AND_BITS;
445 assign Y = |S ? Y_B : A;
449 // --------------------------------------------------------
451 // --------------------------------------------------------
454 (* techmap_simplemap *)
455 (* techmap_celltype = "$lut $sop" *)