2 * Copyright (c) 2001 Stephen Williams (steve@icarus.com)
4 * This source code is free software; you can redistribute it
5 * and/or modify it in source code form under the terms of the GNU
6 * General Public License as published by the Free Software
7 * Foundation; either version 2 of the License, or (at your option)
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
20 #ident "$Id: d-generic.c,v 1.14 2003/11/12 03:20:14 steve Exp $"
24 # include "fpga_priv.h"
28 * This is the device emitter for the most generic FPGA. It doesn't
29 * know anything special about device types, so can't handle complex
33 static void xnf_draw_pin(ivl_nexus_t nex
, const char*nam
, char dir
)
35 const char*use_name
= nam
;
36 const char*nex_name
= xnf_mangle_nexus_name(nex
);
39 if (use_name
[0] == '~') {
44 fprintf(xnf
, " PIN, %s, %c, %s", use_name
, dir
, nex_name
);
47 fprintf(xnf
, ",,INV");
52 static void show_root_ports_xnf(ivl_scope_t root
)
54 unsigned cnt
= ivl_scope_sigs(root
);
57 for (idx
= 0 ; idx
< cnt
; idx
+= 1) {
58 ivl_signal_t sig
= ivl_scope_sig(root
, idx
);
61 if (ivl_signal_port(sig
) == IVL_SIP_NONE
)
64 use_name
= ivl_signal_basename(sig
);
65 if (ivl_signal_pins(sig
) == 1) {
66 ivl_nexus_t nex
= ivl_signal_pin(sig
, 0);
67 fprintf(xnf
, "SIG, %s, PIN=%s\n",
68 xnf_mangle_nexus_name(nex
), use_name
);
73 for (pin
= 0 ; pin
< ivl_signal_pins(sig
); pin
+= 1) {
74 ivl_nexus_t nex
= ivl_signal_pin(sig
, pin
);
75 fprintf(xnf
, "SIG, %s, PIN=%s%u\n",
76 xnf_mangle_nexus_name(nex
), use_name
,
83 static void show_design_consts_xnf(ivl_design_t des
)
87 for (idx
= 0 ; idx
< ivl_design_consts(des
) ; idx
+= 1) {
89 ivl_net_const_t net
= ivl_design_const(des
, idx
);
90 const char*val
= ivl_const_bits(net
);
92 for (pin
= 0 ; pin
< ivl_const_pins(net
) ; pin
+= 1) {
93 ivl_nexus_t nex
= ivl_const_pin(net
, pin
);
94 fprintf(xnf
, "PWR,%c,%s\n", val
[pin
],
95 xnf_mangle_nexus_name(nex
));
100 static void generic_show_header(ivl_design_t des
)
102 ivl_scope_t root
= ivl_design_root(des
);
104 fprintf(xnf
, "LCANET,6\n");
105 fprintf(xnf
, "PROG,iverilog,$Name: $,\"Icarus Verilog/fpga.tgt\"\n");
107 if (part
&& (part
[0]!=0)) {
108 fprintf(xnf
, "PART,%s\n", part
);
111 show_root_ports_xnf(root
);
114 static void generic_show_footer(ivl_design_t des
)
116 show_design_consts_xnf(des
);
117 fprintf(xnf
, "EOF\n");
121 static void generic_show_logic(ivl_net_logic_t net
)
127 xnf_mangle_logic_name(net
, name
, sizeof name
);
129 switch (ivl_logic_type(net
)) {
132 fprintf(xnf
, "SYM, %s, AND, LIBVER=2.0.0\n", name
);
133 nex
= ivl_logic_pin(net
, 0);
134 xnf_draw_pin(nex
, "O", 'O');
135 for (idx
= 1 ; idx
< ivl_logic_pins(net
) ; idx
+= 1) {
137 nex
= ivl_logic_pin(net
, idx
);
138 sprintf(ipin
, "I%u", idx
-1);
139 xnf_draw_pin(nex
, ipin
, 'I');
141 fprintf(xnf
, "END\n");
145 assert(ivl_logic_pins(net
) == 2);
146 fprintf(xnf
, "SYM, %s, BUF, LIBVER=2.0.0\n", name
);
147 nex
= ivl_logic_pin(net
, 0);
148 xnf_draw_pin(nex
, "O", 'O');
149 nex
= ivl_logic_pin(net
, 1);
150 xnf_draw_pin(nex
, "I", 'I');
151 fprintf(xnf
, "END\n");
155 fprintf(xnf
, "SYM, %s, NAND, LIBVER=2.0.0\n", name
);
156 nex
= ivl_logic_pin(net
, 0);
157 xnf_draw_pin(nex
, "O", 'O');
158 for (idx
= 1 ; idx
< ivl_logic_pins(net
) ; idx
+= 1) {
160 nex
= ivl_logic_pin(net
, idx
);
161 sprintf(ipin
, "I%u", idx
-1);
162 xnf_draw_pin(nex
, ipin
, 'I');
164 fprintf(xnf
, "END\n");
168 fprintf(xnf
, "SYM, %s, NOR, LIBVER=2.0.0\n", name
);
169 nex
= ivl_logic_pin(net
, 0);
170 xnf_draw_pin(nex
, "O", 'O');
171 for (idx
= 1 ; idx
< ivl_logic_pins(net
) ; idx
+= 1) {
173 nex
= ivl_logic_pin(net
, idx
);
174 sprintf(ipin
, "I%u", idx
-1);
175 xnf_draw_pin(nex
, ipin
, 'I');
177 fprintf(xnf
, "END\n");
181 assert(ivl_logic_pins(net
) == 2);
182 fprintf(xnf
, "SYM, %s, INV, LIBVER=2.0.0\n", name
);
183 nex
= ivl_logic_pin(net
, 0);
184 xnf_draw_pin(nex
, "O", 'O');
185 nex
= ivl_logic_pin(net
, 1);
186 xnf_draw_pin(nex
, "I", 'I');
187 fprintf(xnf
, "END\n");
191 fprintf(xnf
, "SYM, %s, OR, LIBVER=2.0.0\n", name
);
192 nex
= ivl_logic_pin(net
, 0);
193 xnf_draw_pin(nex
, "O", 'O');
194 for (idx
= 1 ; idx
< ivl_logic_pins(net
) ; idx
+= 1) {
196 nex
= ivl_logic_pin(net
, idx
);
197 sprintf(ipin
, "I%u", idx
-1);
198 xnf_draw_pin(nex
, ipin
, 'I');
200 fprintf(xnf
, "END\n");
204 fprintf(xnf
, "SYM, %s, XOR, LIBVER=2.0.0\n", name
);
205 nex
= ivl_logic_pin(net
, 0);
206 xnf_draw_pin(nex
, "O", 'O');
207 for (idx
= 1 ; idx
< ivl_logic_pins(net
) ; idx
+= 1) {
209 nex
= ivl_logic_pin(net
, idx
);
210 sprintf(ipin
, "I%u", idx
-1);
211 xnf_draw_pin(nex
, ipin
, 'I');
213 fprintf(xnf
, "END\n");
217 fprintf(xnf
, "SYM, %s, XNOR, LIBVER=2.0.0\n", name
);
218 nex
= ivl_logic_pin(net
, 0);
219 xnf_draw_pin(nex
, "O", 'O');
220 for (idx
= 1 ; idx
< ivl_logic_pins(net
) ; idx
+= 1) {
222 nex
= ivl_logic_pin(net
, idx
);
223 sprintf(ipin
, "I%u", idx
-1);
224 xnf_draw_pin(nex
, ipin
, 'I');
226 fprintf(xnf
, "END\n");
230 fprintf(xnf
, "SYM, %s, TBUF, LIBVER=2.0.0\n", name
);
231 nex
= ivl_logic_pin(net
, 0);
232 xnf_draw_pin(nex
, "O", 'O');
233 nex
= ivl_logic_pin(net
, 1);
234 xnf_draw_pin(nex
, "I", 'I');
235 nex
= ivl_logic_pin(net
, 2);
236 xnf_draw_pin(nex
, "~T", 'I');
237 fprintf(xnf
, "END\n");
241 fprintf(xnf
, "SYM, %s, TBUF, LIBVER=2.0.0\n", name
);
242 nex
= ivl_logic_pin(net
, 0);
243 xnf_draw_pin(nex
, "O", 'O');
244 nex
= ivl_logic_pin(net
, 1);
245 xnf_draw_pin(nex
, "I", 'I');
246 nex
= ivl_logic_pin(net
, 2);
247 xnf_draw_pin(nex
, "T", 'I');
248 fprintf(xnf
, "END\n");
252 fprintf(stderr
, "fpga.tgt: unknown logic type %u\n",
253 ivl_logic_type(net
));
259 static void generic_show_dff(ivl_lpm_t net
)
264 xnf_mangle_lpm_name(net
, name
, sizeof name
);
266 fprintf(xnf
, "SYM, %s, DFF, LIBVER=2.0.0\n", name
);
268 nex
= ivl_lpm_q(net
, 0);
269 xnf_draw_pin(nex
, "Q", 'O');
271 nex
= ivl_lpm_data(net
, 0);
272 xnf_draw_pin(nex
, "D", 'I');
274 nex
= ivl_lpm_clk(net
);
275 xnf_draw_pin(nex
, "C", 'I');
277 if ((nex
= ivl_lpm_enable(net
)))
278 xnf_draw_pin(nex
, "CE", 'I');
280 fprintf(xnf
, "END\n");
284 * The generic == comparator uses EQN records to generate 2-bit
285 * comparators, that are then connected together by a wide AND gate.
287 static void generic_show_cmp_eq(ivl_lpm_t net
)
292 /* Make this many dual pair comparators, and */
293 unsigned deqn
= ivl_lpm_width(net
) / 2;
294 /* Make this many single pair comparators. */
295 unsigned seqn
= ivl_lpm_width(net
) % 2;
297 xnf_mangle_lpm_name(net
, name
, sizeof name
);
299 for (idx
= 0 ; idx
< deqn
; idx
+= 1) {
300 fprintf(xnf
, "SYM, %s/CD%u, EQN, "
301 "EQN=(~((I0 @ I1) + (I2 @ I3)))\n",
304 fprintf(xnf
, " PIN, O, O, %s/CDO%u\n", name
, idx
);
306 nex
= ivl_lpm_data(net
, 2*idx
);
307 xnf_draw_pin(nex
, "I0", 'I');
308 nex
= ivl_lpm_datab(net
, 2*idx
);
309 xnf_draw_pin(nex
, "I1", 'I');
311 nex
= ivl_lpm_data(net
, 2*idx
+1);
312 xnf_draw_pin(nex
, "I2", 'I');
313 nex
= ivl_lpm_datab(net
, 2*idx
+1);
314 xnf_draw_pin(nex
, "I3", 'I');
316 fprintf(xnf
, "END\n");
320 fprintf(xnf
, "SYM, %s/CT, XNOR, LIBVER=2.0.0\n", name
);
322 fprintf(xnf
, " PIN, O, O, %s/CTO\n", name
);
324 nex
= ivl_lpm_data(net
, 2*deqn
);
325 xnf_draw_pin(nex
, "I0", 'I');
327 nex
= ivl_lpm_datab(net
, 2*deqn
);
328 xnf_draw_pin(nex
, "I1", 'I');
330 fprintf(xnf
, "END\n");
333 if (ivl_lpm_type(net
) == IVL_LPM_CMP_EQ
)
334 fprintf(xnf
, "SYM, %s/OUT, AND, LIBVER=2.0.0\n", name
);
336 fprintf(xnf
, "SYM, %s/OUT, NAND, LIBVER=2.0.0\n", name
);
338 nex
= ivl_lpm_q(net
, 0);
339 xnf_draw_pin(nex
, "O", 'O');
341 for (idx
= 0 ; idx
< deqn
; idx
+= 1)
342 fprintf(xnf
, " PIN, I%u, I, %s/CDO%u\n", idx
, name
, idx
);
344 for (idx
= 0 ; idx
< seqn
; idx
+= 1)
345 fprintf(xnf
, " PIN, I%u, I, %s/CTO\n", deqn
+idx
, name
);
347 fprintf(xnf
, "END\n");
351 * This function draws N-bit wide binary mux devices. These are so
352 * very popular because they are the result of such expressions as:
356 * This code only supports the case where sel is a single bit. It
357 * works by drawing for each bit of the width an EQN device that takes
358 * as inputs I0 and I1 the alternative inputs, and I2 the select. The
359 * select bit is common with all the generated mux devices.
361 static void generic_show_mux(ivl_lpm_t net
)
364 ivl_nexus_t nex
, sel
;
367 xnf_mangle_lpm_name(net
, name
, sizeof name
);
369 /* Access the single select bit. This is common to the whole
371 assert(ivl_lpm_selects(net
) == 1);
372 sel
= ivl_lpm_select(net
, 0);
374 for (idx
= 0 ; idx
< ivl_lpm_width(net
) ; idx
+= 1) {
375 fprintf(xnf
, "SYM, %s/M%u, EQN, "
376 "EQN=((I0 * ~I2) + (I1 * I2))\n",
379 nex
= ivl_lpm_q(net
, idx
);
380 xnf_draw_pin(nex
, "O", 'O');
382 nex
= ivl_lpm_data2(net
, 0, idx
);
383 xnf_draw_pin(nex
, "I0", 'I');
385 nex
= ivl_lpm_data2(net
, 1, idx
);
386 xnf_draw_pin(nex
, "I1", 'I');
388 xnf_draw_pin(sel
, "I2", 'I');
390 fprintf(xnf
, "END\n");
395 * This code cheats and just generates ADD4 devices enough to support
396 * the add. Make no effort to optimize, because we have no idea what
397 * kind of device we have.
399 static void generic_show_add(ivl_lpm_t net
)
403 unsigned idx
, nadd4
, tail
;
405 xnf_mangle_lpm_name(net
, name
, sizeof name
);
407 /* Make this many ADD4 devices. */
408 nadd4
= ivl_lpm_width(net
) / 4;
409 tail
= ivl_lpm_width(net
) % 4;
411 for (idx
= 0 ; idx
< nadd4
; idx
+= 1) {
412 fprintf(xnf
, "SYM, %s/A%u, ADD4\n", name
, idx
);
415 fprintf(xnf
, " PIN, CI, I, %s/CO%u\n", name
, idx
-1);
417 nex
= ivl_lpm_q(net
, idx
*4+0);
418 xnf_draw_pin(nex
, "S0", 'O');
420 nex
= ivl_lpm_q(net
, idx
*4+1);
421 xnf_draw_pin(nex
, "S1", 'O');
423 nex
= ivl_lpm_q(net
, idx
*4+2);
424 xnf_draw_pin(nex
, "S2", 'O');
426 nex
= ivl_lpm_q(net
, idx
*4+3);
427 xnf_draw_pin(nex
, "S3", 'O');
429 nex
= ivl_lpm_data(net
, idx
*4+0);
430 xnf_draw_pin(nex
, "A0", 'I');
432 nex
= ivl_lpm_data(net
, idx
*4+1);
433 xnf_draw_pin(nex
, "A1", 'I');
435 nex
= ivl_lpm_data(net
, idx
*4+2);
436 xnf_draw_pin(nex
, "A2", 'I');
438 nex
= ivl_lpm_data(net
, idx
*4+3);
439 xnf_draw_pin(nex
, "A3", 'I');
441 nex
= ivl_lpm_datab(net
, idx
*4+0);
442 xnf_draw_pin(nex
, "B0", 'I');
444 nex
= ivl_lpm_datab(net
, idx
*4+1);
445 xnf_draw_pin(nex
, "B1", 'I');
447 nex
= ivl_lpm_datab(net
, idx
*4+2);
448 xnf_draw_pin(nex
, "B2", 'I');
450 nex
= ivl_lpm_datab(net
, idx
*4+3);
451 xnf_draw_pin(nex
, "B3", 'I');
453 if ((idx
*4+4) < ivl_lpm_width(net
))
454 fprintf(xnf
, " PIN, CO, O, %s/CO%u\n", name
, idx
);
456 fprintf(xnf
, "END\n");
460 fprintf(xnf
, "SYM, %s/A%u, ADD4\n", name
, nadd4
);
462 fprintf(xnf
, " PIN, CI, I, %s/CO%u\n", name
, nadd4
-1);
466 nex
= ivl_lpm_data(net
, nadd4
*4+2);
467 xnf_draw_pin(nex
, "A2", 'I');
469 nex
= ivl_lpm_datab(net
, nadd4
*4+2);
470 xnf_draw_pin(nex
, "B2", 'I');
472 nex
= ivl_lpm_q(net
, nadd4
*4+2);
473 xnf_draw_pin(nex
, "S2", 'O');
475 nex
= ivl_lpm_data(net
, nadd4
*4+1);
476 xnf_draw_pin(nex
, "A1", 'I');
478 nex
= ivl_lpm_datab(net
, nadd4
*4+1);
479 xnf_draw_pin(nex
, "B1", 'I');
481 nex
= ivl_lpm_q(net
, nadd4
*4+1);
482 xnf_draw_pin(nex
, "S1", 'O');
484 nex
= ivl_lpm_data(net
, nadd4
*4+0);
485 xnf_draw_pin(nex
, "A0", 'I');
487 nex
= ivl_lpm_datab(net
, nadd4
*4+0);
488 xnf_draw_pin(nex
, "B0", 'I');
490 nex
= ivl_lpm_q(net
, nadd4
*4+0);
491 xnf_draw_pin(nex
, "S0", 'O');
494 fprintf(xnf
, "END\n");
498 const struct device_s d_generic
= {
502 0, /* show_pad not implemented */
507 0, /* ge not implemented */
508 0, /* gt not implemented */
511 0, /* subtract not implemented */
518 * $Log: d-generic.c,v $
519 * Revision 1.14 2003/11/12 03:20:14 steve
520 * devices need show_cmp_gt
522 * Revision 1.13 2003/06/24 03:55:00 steve
523 * Add ivl_synthesis_cell support for virtex2.
525 * Revision 1.12 2002/10/28 02:05:56 steve
526 * Add Virtex code generators for left shift,
527 * subtraction, and GE comparators.
529 * Revision 1.11 2002/08/12 01:35:02 steve
530 * conditional ident string using autoconfig.
532 * Revision 1.10 2002/08/11 23:47:04 steve
533 * Add missing Log and Ident strings.
535 * Revision 1.9 2001/09/16 01:48:16 steve
536 * Suppor the PAD attribute on signals.
538 * Revision 1.8 2001/09/02 21:33:07 steve
539 * Rearrange the XNF code generator to be generic-xnf
540 * so that non-XNF code generation is also possible.
542 * Start into the virtex EDIF output driver.
544 * Revision 1.7 2001/09/01 04:30:44 steve
547 * Revision 1.6 2001/09/01 02:28:42 steve
548 * Generate code for MUX devices.
550 * Revision 1.5 2001/09/01 02:01:30 steve
551 * identity compare, and PWR records for constants.
553 * Revision 1.4 2001/08/31 23:02:13 steve
554 * Relax pin count restriction on logic gates.
556 * Revision 1.3 2001/08/31 04:17:56 steve
557 * Many more logic gate types.
559 * Revision 1.2 2001/08/31 02:59:06 steve
560 * Add root port SIG records.
562 * Revision 1.1 2001/08/28 04:14:20 steve
563 * Add the fpga target.