1 /*
2  * Copyright (C) 2010-2020 Cary R. (cygcary@yahoo.com)
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License along
15  *  with this program; if not, write to the Free Software Foundation, Inc.,
16  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18 
19 # include <inttypes.h>
20 # include <string.h>
21 # include "config.h"
22 # include "vlog95_priv.h"
23 # include "ivl_alloc.h"
24 
25 const char *func_rtn_name = 0;
26 
emit_func_return(ivl_signal_t sig)27 static void emit_func_return(ivl_signal_t sig)
28 {
29         // Handle SV void functions.
30       if (sig == 0)
31             return;
32 
33       if (ivl_signal_dimensions(sig) > 0) {
34 	    fprintf(stderr, "%s:%u: vlog95 error: A function cannot return "
35 	                    "an array.\n", ivl_signal_file(sig),
36 	                    ivl_signal_lineno(sig));
37 	    vlog_errors += 1;
38       } else if (ivl_signal_integer(sig)) {
39 	    fprintf(vlog_out, " integer");
40       } else if (ivl_signal_data_type(sig) == IVL_VT_REAL) {
41 	    fprintf(vlog_out, " real");
42       } else {
43 	    int msb, lsb;
44 	    get_sig_msb_lsb(sig, &msb, &lsb);
45 	    if (msb != 0 || lsb != 0) fprintf(vlog_out, " [%d:%d]", msb, lsb);
46       }
47 }
48 
emit_sig_file_line(ivl_signal_t sig)49 void emit_sig_file_line(ivl_signal_t sig)
50 {
51       if (emit_file_line) {
52 	    fprintf(vlog_out, " /* %s:%u */",
53 	                      ivl_signal_file(sig),
54 	                      ivl_signal_lineno(sig));
55       }
56 }
57 
emit_sig_id(ivl_signal_t sig)58 static void emit_sig_id(ivl_signal_t sig)
59 {
60       emit_id(ivl_signal_basename(sig));
61       fprintf(vlog_out, ";");
62       emit_sig_file_line(sig);
63       fprintf(vlog_out, "\n");
64 }
65 
emit_var_def(ivl_signal_t sig)66 static void emit_var_def(ivl_signal_t sig)
67 {
68       if (ivl_signal_local(sig)) return;
69       fprintf(vlog_out, "%*c", indent, ' ');
70       if (ivl_signal_integer(sig)) {
71 	    fprintf(vlog_out, "integer ");
72 	    emit_sig_id(sig);
73 	    if (ivl_signal_dimensions(sig) > 0) {
74 		  fprintf(stderr, "%s:%u: vlog95 error: Integer arrays (%s) "
75 		                  "are not supported.\n", ivl_signal_file(sig),
76 		                  ivl_signal_lineno(sig),
77 		                  ivl_signal_basename(sig));
78 		  vlog_errors += 1;
79 	    }
80       } else if (ivl_signal_data_type(sig) == IVL_VT_REAL) {
81 	    fprintf(vlog_out, "real ");
82 	    emit_sig_id(sig);
83 	    if (ivl_signal_dimensions(sig) > 0) {
84 		  fprintf(stderr, "%s:%u: vlog95 error: Real arrays (%s) "
85 		                  "are not supported.\n", ivl_signal_file(sig),
86 		                  ivl_signal_lineno(sig),
87 		                  ivl_signal_basename(sig));
88 		  vlog_errors += 1;
89 	    }
90       } else if (ivl_signal_data_type(sig) == IVL_VT_STRING) {
91 	    fprintf(vlog_out, "string ");
92 	    emit_sig_id(sig);
93 	    fprintf(stderr, "%s:%u: vlog95 error: SystemVerilog strings (%s) "
94 	                    "are not supported.\n", ivl_signal_file(sig),
95 	                    ivl_signal_lineno(sig), ivl_signal_basename(sig));
96 	    vlog_errors += 1;
97       } else if (ivl_signal_data_type(sig) == IVL_VT_DARRAY) {
98 	    fprintf(vlog_out, "<dynamic array> ");
99 	    emit_sig_id(sig);
100 	    fprintf(stderr, "%s:%u: vlog95 error: SystemVerilog dynamic "
101 	                    "arrays (%s) are not supported.\n",
102 	                    ivl_signal_file(sig),
103 	                    ivl_signal_lineno(sig), ivl_signal_basename(sig));
104 	    vlog_errors += 1;
105       } else if (ivl_signal_data_type(sig) == IVL_VT_QUEUE) {
106 	    fprintf(vlog_out, "<queue> ");
107 	    emit_sig_id(sig);
108 	    fprintf(stderr, "%s:%u: vlog95 error: SystemVerilog queues "
109 	                    "(%s) are not supported.\n",
110 	                    ivl_signal_file(sig),
111 	                    ivl_signal_lineno(sig), ivl_signal_basename(sig));
112 	    vlog_errors += 1;
113       } else {
114 	    int msb, lsb;
115 	    get_sig_msb_lsb(sig, &msb, &lsb);
116 	    fprintf(vlog_out, "reg ");
117 	    if (ivl_signal_signed(sig)) {
118 		  if (allow_signed) {
119 			fprintf(vlog_out, "signed ");
120 		  } else {
121 			fprintf(stderr, "%s:%u: vlog95 error: Signed registers "
122 			                "(%s) are not supported.\n",
123 			                ivl_signal_file(sig),
124 			                ivl_signal_lineno(sig),
125 			                ivl_signal_basename(sig));
126 			vlog_errors += 1;
127 		  }
128 	    }
129 	    if (msb != 0 || lsb != 0) fprintf(vlog_out, "[%d:%d] ", msb, lsb);
130 	    emit_id(ivl_signal_basename(sig));
131 	    if (ivl_signal_dimensions(sig) > 0) {
132 		  unsigned wd_count = ivl_signal_array_count(sig);
133 		  int first = ivl_signal_array_base(sig);
134 		  int last = first + wd_count - 1;
135 		  if (ivl_signal_array_addr_swapped(sig)) {
136 			fprintf(vlog_out, " [%d:%d]", last, first);
137 		  } else {
138 			fprintf(vlog_out, " [%d:%d]", first, last);
139 		  }
140 	    }
141 	    fprintf(vlog_out, ";");
142 	    emit_sig_file_line(sig);
143 	    fprintf(vlog_out, "\n");
144       }
145 }
146 
147 /*
148  * Keep a list of constants that drive nets and need to be emitted as
149  * a continuous assignment.
150 */
151 static ivl_signal_t *net_consts = 0;
152 static unsigned num_net_consts = 0;
153 
add_net_const_to_list(ivl_signal_t net_const)154 static void add_net_const_to_list(ivl_signal_t net_const)
155 {
156       num_net_consts += 1;
157       net_consts = realloc(net_consts, num_net_consts * sizeof(ivl_signal_t));
158       net_consts[num_net_consts-1] = net_const;
159 }
160 
emit_and_free_net_const_list(ivl_scope_t scope)161 static unsigned emit_and_free_net_const_list(ivl_scope_t scope)
162 {
163       unsigned idx;
164       for (idx = 0; idx < num_net_consts; idx += 1) {
165 	    emit_signal_net_const_as_ca(scope, net_consts[idx]);
166       }
167       free(net_consts);
168       net_consts = 0;
169       idx = num_net_consts != 0;
170       num_net_consts = 0;
171       return idx;
172 }
173 
save_net_constants(ivl_scope_t scope,ivl_signal_t sig)174 static void save_net_constants(ivl_scope_t scope, ivl_signal_t sig)
175 {
176       ivl_nexus_t nex = ivl_signal_nex(sig, 0);
177       unsigned idx, count = ivl_nexus_ptrs(nex);
178       for (idx = 0; idx < count; idx += 1) {
179 	    ivl_nexus_ptr_t nex_ptr = ivl_nexus_ptr(nex, idx);
180 	    ivl_net_const_t net_const = ivl_nexus_ptr_con(nex_ptr);
181 	    if (! net_const) continue;
182 	    if (scope != ivl_const_scope(net_const)) continue;
183 	    add_net_const_to_list(sig);
184       }
185 }
186 
emit_net_def(ivl_scope_t scope,ivl_signal_t sig)187 static void emit_net_def(ivl_scope_t scope, ivl_signal_t sig)
188 {
189       int msb, lsb;
190       get_sig_msb_lsb(sig, &msb, &lsb);
191       if (ivl_signal_local(sig)) return;
192       fprintf(vlog_out, "%*c", indent, ' ');
193       if (ivl_signal_data_type(sig) == IVL_VT_REAL){
194 	    fprintf(vlog_out, "wire ");
195 	    emit_sig_id(sig);
196 	    fprintf(stderr, "%s:%u: vlog95 error: Real nets (%s) are "
197 	                    "not supported.\n", ivl_signal_file(sig),
198 	                    ivl_signal_lineno(sig), ivl_signal_basename(sig));
199 	    vlog_errors += 1;
200       } else if (ivl_signal_dimensions(sig) > 0) {
201 	    fprintf(vlog_out, "wire ");
202 	    if (msb != 0 || lsb != 0) fprintf(vlog_out, "[%d:%d] ", msb, lsb);
203 	    emit_sig_id(sig);
204 	    fprintf(stderr, "%s:%u: vlog95 error: Array nets (%s) are "
205 	                    "not supported.\n", ivl_signal_file(sig),
206 	                    ivl_signal_lineno(sig), ivl_signal_basename(sig));
207 	    vlog_errors += 1;
208       } else {
209 	    switch (ivl_signal_type(sig)) {
210 	      case IVL_SIT_TRI:
211 	      case IVL_SIT_UWIRE:
212 // HERE: Need to add support for supply nets. Probably supply strength
213 //       with a constant 0/1 driver for all the bits.
214 		  fprintf(vlog_out, "wire ");
215 		  break;
216 	      case IVL_SIT_TRI0:
217 		  fprintf(vlog_out, "tri0 ");
218 		  break;
219 	      case IVL_SIT_TRI1:
220 		  fprintf(vlog_out, "tri1 ");
221 		  break;
222 	      case IVL_SIT_TRIAND:
223 		  fprintf(vlog_out, "wand ");
224 		  break;
225 	      case IVL_SIT_TRIOR:
226 		  fprintf(vlog_out, "wor ");
227 		  break;
228 	      default:
229 		  fprintf(vlog_out, "<unknown> ");
230 		  fprintf(stderr, "%s:%u: vlog95 error: Unknown net type "
231 	                    "(%d).\n", ivl_signal_file(sig),
232 	                    ivl_signal_lineno(sig), (int)ivl_signal_type(sig));
233 		  vlog_errors += 1;
234 		  break;
235 	    }
236 	    if (ivl_signal_signed(sig)) {
237 		  if (allow_signed) {
238 			fprintf(vlog_out, "signed ");
239 		  } else {
240 			fprintf(stderr, "%s:%u: vlog95 error: Signed nets (%s) "
241 			                "are not supported.\n",
242 			                ivl_signal_file(sig),
243 			                ivl_signal_lineno(sig),
244 			                ivl_signal_basename(sig));
245 			vlog_errors += 1;
246 		  }
247 	    }
248 	    if (msb != 0 || lsb != 0) fprintf(vlog_out, "[%d:%d] ", msb, lsb);
249 	    emit_sig_id(sig);
250 	      /* A constant driving a net does not create an lpm or logic
251 	       * element in the design so save them from the definition. */
252 	    save_net_constants(scope, sig);
253       }
254 }
255 
emit_mangled_name(ivl_scope_t scope,unsigned root)256 static void emit_mangled_name(ivl_scope_t scope, unsigned root)
257 {
258 	/* If the module has parameters and it's not a root module then it
259 	 * may not be unique so we create a mangled name version instead.
260 	 * The mangled name is of the form:
261 	 *   <module_name>[<full_instance_scope>]. */
262       if (ivl_scope_params(scope) && ! root) {
263 	    char *name;
264 	    size_t len = strlen(ivl_scope_name(scope)) +
265 	                 strlen(ivl_scope_tname(scope)) + 3;
266 	    name = (char *)malloc(len);
267 	    (void) strcpy(name, ivl_scope_tname(scope));
268 	    (void) strcat(name, "[");
269 	    (void) strcat(name, ivl_scope_name(scope));
270 	    (void) strcat(name, "]");
271 	    assert(name[len-1] == 0);
272 	      /* Emit the mangled name as an escaped identifier. */
273 	    fprintf(vlog_out, "\\%s ", name);
274 	    free(name);
275       } else {
276 	    emit_id(ivl_scope_tname(scope));
277       }
278 }
279 
280 /*
281  * This function is called for each process in the design so that we
282  * can extract the processes for the given scope.
283  */
find_process(ivl_process_t proc,ivl_scope_t scope)284 static int find_process(ivl_process_t proc, ivl_scope_t scope)
285 {
286       if (scope == ivl_process_scope(proc)) emit_process(scope, proc);
287       return 0;
288 }
289 
emit_scope_variables(ivl_scope_t scope)290 void emit_scope_variables(ivl_scope_t scope)
291 {
292       unsigned idx, count;
293       assert(! num_net_consts);
294 	/* Output the parameters for this scope. */
295       count = ivl_scope_params(scope);
296       for (idx = 0; idx < count; idx += 1) {
297 	    ivl_parameter_t par = ivl_scope_param(scope, idx);
298 	    ivl_expr_t pex = ivl_parameter_expr(par);
299 	    fprintf(vlog_out, "%*cparameter ", indent, ' ');
300 	    emit_id(ivl_parameter_basename(par));
301 	    fprintf(vlog_out, " = ");
302 	      /* Need to emit the parameters value not its name. */
303 	    emitting_param = par;
304 	    emit_expr(scope, pex, ivl_parameter_width(par), 1, 0, 0);
305 	    emitting_param = 0;
306 	    fprintf(vlog_out, ";");
307 	    if (emit_file_line) {
308 		  fprintf(vlog_out, " /* %s:%u */",
309 		                    ivl_parameter_file(par),
310 		                    ivl_parameter_lineno(par));
311 	    }
312 	    fprintf(vlog_out, "\n");
313       }
314       if (count) fprintf(vlog_out, "\n");
315 
316 	/* Output the signals for this scope. */
317       count = ivl_scope_sigs(scope);
318       for (idx = 0; idx < count; idx += 1) {
319 	    ivl_signal_t sig = ivl_scope_sig(scope, idx);
320 	    if (ivl_signal_type(sig) == IVL_SIT_REG) {
321 		    /* Do not output the implicit function return register. */
322 		  if (ivl_scope_type(scope) == IVL_SCT_FUNCTION &&
323                       strcmp(ivl_signal_basename(sig),
324 		              ivl_scope_tname(scope)) == 0) continue;
325 		  emit_var_def(sig);
326 	    } else {
327 		  emit_net_def(scope, sig);
328 	    }
329       }
330       if (count) fprintf(vlog_out, "\n");
331 
332 	/* Output the named events for this scope. */
333       count = ivl_scope_events(scope);
334       for (idx = 0; idx < count; idx += 1) {
335 	    ivl_event_t event = ivl_scope_event(scope, idx);
336 	      /* If this event has any type of edge sensitivity then it is
337 	       * not a named event. */
338 	    if (ivl_event_nany(event)) continue;
339 	    if (ivl_event_npos(event)) continue;
340 	    if (ivl_event_nneg(event)) continue;
341 	    fprintf(vlog_out, "%*cevent ", indent, ' ');
342 	    emit_id(ivl_event_basename(event));
343 	    fprintf(vlog_out, ";");
344 	    if (emit_file_line) {
345 		  fprintf(vlog_out, " /* %s:%u */",
346 		                    ivl_event_file(event),
347 		                    ivl_event_lineno(event));
348 	    }
349 	    fprintf(vlog_out, "\n");
350       }
351       if (count) fprintf(vlog_out, "\n");
352       if (emit_and_free_net_const_list(scope)) fprintf(vlog_out, "\n");
353 }
354 
emit_scope_file_line(ivl_scope_t scope)355 static void emit_scope_file_line(ivl_scope_t scope)
356 {
357       if (emit_file_line) {
358 	    fprintf(vlog_out, " /* %s:%u */",
359 	                      ivl_scope_file(scope),
360 	                      ivl_scope_lineno(scope));
361       }
362 }
363 
emit_module_ports(ivl_scope_t scope)364 static void emit_module_ports(ivl_scope_t scope)
365 {
366       unsigned idx, count = ivl_scope_ports(scope);
367 
368       if (count == 0) return;
369 
370       fprintf(vlog_out, "(");
371       emit_nexus_as_ca(scope, ivl_scope_mod_port(scope, 0), 0, 0);
372       for (idx = 1; idx < count; idx += 1) {
373 	    fprintf(vlog_out, ", ");
374 	    emit_nexus_as_ca(scope, ivl_scope_mod_port(scope, idx), 0, 0);
375       }
376       fprintf(vlog_out, ")");
377 }
378 
get_port_from_nexus(ivl_scope_t scope,ivl_nexus_t nex,unsigned * word)379 static ivl_signal_t get_port_from_nexus(ivl_scope_t scope, ivl_nexus_t nex,
380 	                                unsigned *word)
381 {
382       assert(nex);
383       unsigned idx, count = ivl_nexus_ptrs(nex);
384       ivl_signal_t sig = 0;
385       *word = 0;
386       for (idx = 0; idx < count; idx += 1) {
387 	    ivl_nexus_ptr_t nex_ptr = ivl_nexus_ptr(nex, idx);
388 	    ivl_signal_t t_sig = ivl_nexus_ptr_sig(nex_ptr);
389 	    if (t_sig) {
390 		  if (ivl_signal_scope(t_sig) != scope) continue;
391 		  assert(! sig);
392 		  sig = t_sig;
393 		  *word = ivl_nexus_ptr_pin(nex_ptr);
394 	    }
395       }
396       return sig;
397 }
398 
emit_sig_type(ivl_signal_t sig)399 static void emit_sig_type(ivl_signal_t sig)
400 {
401       ivl_signal_type_t type = ivl_signal_type(sig);
402       if (ivl_signal_dimensions(sig) != 0) {
403 	    fprintf(stderr, "%s:%u: vlog95 error: Array ports (%s) are not "
404 	                    "supported.\n",
405 	                    ivl_signal_file(sig),
406 	                    ivl_signal_lineno(sig),
407 	                    ivl_signal_basename(sig));
408 	    vlog_errors += 1;
409       }
410 	/* Check to see if we have a variable (reg) or a net. */
411       if (type == IVL_SIT_REG) {
412 	      /* The variable data type will be declared later, so here
413 		 we just want to declare the range and whether or not it
414 		 is signed. */
415 	    if (ivl_signal_integer(sig)) {
416 		  /* nothing to do */
417 	    } else if (ivl_signal_data_type(sig) == IVL_VT_REAL) {
418 		  /* nothing to do */
419 	    } else {
420 		  int msb, lsb;
421 		  get_sig_msb_lsb(sig, &msb, &lsb);
422 		  if (ivl_signal_signed(sig)) {
423 			if (allow_signed) {
424 			      fprintf(vlog_out, " signed");
425 			} else {
426 			      fprintf(stderr, "%s:%u: vlog95 error: Signed "
427 			                      "ports (%s) are not supported.\n",
428 			                      ivl_signal_file(sig),
429 			                      ivl_signal_lineno(sig),
430 			                      ivl_signal_basename(sig));
431 			      vlog_errors += 1;
432 			}
433 		  }
434 		  if (msb != 0 || lsb != 0) {
435 			fprintf(vlog_out, " [%d:%d]", msb, lsb);
436 		  }
437 	    }
438       } else {
439 	    assert((type == IVL_SIT_TRI) ||
440 	           (type == IVL_SIT_TRI0) ||
441 	           (type == IVL_SIT_TRI1) ||
442 	           (type == IVL_SIT_UWIRE));
443 	    if (ivl_signal_data_type(sig) == IVL_VT_REAL) {
444 		  fprintf(stderr, "%s:%u: vlog95 error: Real net ports (%s) "
445 		                  "are not supported.\n",
446 		                  ivl_signal_file(sig),
447 		                  ivl_signal_lineno(sig),
448 		                  ivl_signal_basename(sig));
449 		  vlog_errors += 1;
450 	    } else {
451 		  int msb, lsb;
452 		  get_sig_msb_lsb(sig, &msb, &lsb);
453 		  if (ivl_signal_signed(sig)) {
454 			if (allow_signed) {
455 			      fprintf(vlog_out, " signed");
456 			} else {
457 			      fprintf(stderr, "%s:%u: vlog95 error: Signed net "
458 			                      "ports (%s) are not supported.\n",
459 			                      ivl_signal_file(sig),
460 			                      ivl_signal_lineno(sig),
461 			                      ivl_signal_basename(sig));
462 			      vlog_errors += 1;
463 			}
464 		  }
465 		  if (msb != 0 || lsb != 0) {
466 			fprintf(vlog_out, " [%d:%d]", msb, lsb);
467 		  }
468 	    }
469       }
470 }
471 
emit_port(ivl_signal_t port)472 static void emit_port(ivl_signal_t port)
473 {
474       assert(port);
475       fprintf(vlog_out, "%*c", indent, ' ');
476       switch (ivl_signal_port(port)) {
477 	case IVL_SIP_INPUT:
478 	    fprintf(vlog_out, "input");
479 	    break;
480 	case IVL_SIP_OUTPUT:
481 	    fprintf(vlog_out, "output");
482 	    break;
483 	case IVL_SIP_INOUT:
484 	    fprintf(vlog_out, "inout");
485 	    break;
486 	default:
487 	    fprintf(vlog_out, "<unknown>");
488 	    fprintf(stderr, "%s:%u: vlog95 error: Unknown port direction (%d) "
489 	                    "for signal %s.\n", ivl_signal_file(port),
490 	                    ivl_signal_lineno(port), (int)ivl_signal_port(port),
491 	                    ivl_signal_basename(port));
492 	    vlog_errors += 1;
493 	    break;
494       }
495       emit_sig_type(port);
496       fprintf(vlog_out, " ");
497 	/* Split port (arg[7:4],arg[3:0]) are generated using local signals. */
498       if (ivl_signal_local(port)) {
499 	    fprintf(vlog_out, "ivlog%s", ivl_signal_basename(port));
500       } else {
501 	    emit_id(ivl_signal_basename(port));
502       }
503       fprintf(vlog_out, ";");
504       emit_sig_file_line(port);
505       fprintf(vlog_out, "\n");
506 }
507 
emit_module_port_defs(ivl_scope_t scope)508 static void emit_module_port_defs(ivl_scope_t scope)
509 {
510       unsigned word, idx, count = ivl_scope_ports(scope);
511       for (idx = 0; idx < count; idx += 1) {
512 	    ivl_nexus_t nex = ivl_scope_mod_port(scope, idx);
513 	    ivl_signal_t port = get_port_from_nexus(scope, nex, &word);
514 // HERE: Do we need to use word?
515 	    if (port) emit_port(port);
516 	    else {
517 		  fprintf(vlog_out, "<missing>");
518 		  fprintf(stderr, "%s:%u: vlog95 error: Could not find signal "
519 	                    "definition for port (%u) of module %s.\n",
520 		            ivl_scope_file(scope), ivl_scope_lineno(scope),
521 	                    idx + 1, ivl_scope_basename(scope));
522 		  vlog_errors += 1;
523 	    }
524       }
525       if (count) fprintf(vlog_out, "\n");
526 }
527 
emit_module_call_expr(ivl_scope_t scope,unsigned idx)528 static void emit_module_call_expr(ivl_scope_t scope, unsigned idx)
529 {
530       unsigned word;
531       ivl_nexus_t nex = ivl_scope_mod_port(scope, idx);
532       ivl_signal_t port = get_port_from_nexus(scope, nex, &word);
533 	/* For an input port we need to emit the driving expression. */
534       if (ivl_signal_port(port) == IVL_SIP_INPUT) {
535 	    emit_nexus_port_driver_as_ca(ivl_scope_parent(scope),
536 	                                 ivl_signal_nex(port, word));
537 	/* For an output we need to emit the signal the output is driving. */
538       } else {
539 	    emit_nexus_as_ca(ivl_scope_parent(scope),
540 	                     ivl_signal_nex(port, word), 0, 0);
541       }
542 }
543 
emit_module_call_expressions(ivl_scope_t scope)544 static void emit_module_call_expressions(ivl_scope_t scope)
545 {
546       unsigned idx, count = ivl_scope_ports(scope);
547       if (count == 0) return;
548       emit_module_call_expr(scope, 0);
549       for (idx = 1; idx < count; idx += 1) {
550 	    fprintf(vlog_out, ", ");
551 	    emit_module_call_expr(scope, idx);
552       }
553 }
554 
emit_task_func_port_defs(ivl_scope_t scope)555 static void emit_task_func_port_defs(ivl_scope_t scope)
556 {
557       unsigned idx, count = ivl_scope_ports(scope);
558       unsigned start = ivl_scope_type(scope) == IVL_SCT_FUNCTION;
559       for (idx = start; idx < count; idx += 1) {
560 	    ivl_signal_t port = ivl_scope_port(scope, idx);
561 	    emit_port(port);
562       }
563 	/* If the start and count are both 1 then this is a SystemVerilog
564 	 * function that does not have an argument so add a dummy one. */
565       if ((start == 1) && (count == 1)) {
566 	    fprintf(vlog_out, "%*cinput _vlog95_dummy;", indent, ' ');
567 	    if (emit_file_line) fprintf(vlog_out, " /* no file/line */");
568 	    fprintf(vlog_out, "\n");
569       }
570       if (count) fprintf(vlog_out, "\n");
571 }
572 
573 /*
574  * Recursively look for the named block in the given statement.
575  */
has_named_block(ivl_scope_t scope,ivl_statement_t stmt)576 static int has_named_block(ivl_scope_t scope, ivl_statement_t stmt)
577 {
578       unsigned idx, count;
579       int rtn = 0;
580       if (! stmt) return 0;
581       switch (ivl_statement_type(stmt)) {
582 	  /* Block or fork items can contain a named block. */
583 	case IVL_ST_BLOCK:
584 	case IVL_ST_FORK:
585 	case IVL_ST_FORK_JOIN_ANY:
586 	case IVL_ST_FORK_JOIN_NONE:
587 	    if (ivl_stmt_block_scope(stmt) == scope) return 1;
588 	    count = ivl_stmt_block_count(stmt);
589 	    for (idx = 0; (idx < count) && ! rtn ; idx += 1) {
590 		  rtn |= has_named_block(scope,
591 		                         ivl_stmt_block_stmt(stmt, idx));
592 	    }
593 	    break;
594 	  /* Case items can contain a named block. */
595 	case IVL_ST_CASE:
596 	case IVL_ST_CASER:
597 	case IVL_ST_CASEX:
598 	case IVL_ST_CASEZ:
599 	    count = ivl_stmt_case_count(stmt);
600 	    for (idx = 0; (idx < count) && ! rtn; idx += 1) {
601 		  rtn |= has_named_block(scope,
602 		                         ivl_stmt_case_stmt(stmt, idx));
603 	    }
604 	    break;
605 	  /* Either the true or false clause may have a named block. */
606 	case IVL_ST_CONDIT:
607 	    rtn = has_named_block(scope, ivl_stmt_cond_true(stmt));
608 	    if (! rtn) {
609 		  rtn = has_named_block(scope, ivl_stmt_cond_false(stmt));
610 	    }
611 	    break;
612 	  /* The looping statements may have a named block. */
613 	case IVL_ST_DO_WHILE:
614 	case IVL_ST_FOREVER:
615 	case IVL_ST_REPEAT:
616 	case IVL_ST_WHILE:
617 	  /* The delay and wait statements may have a named block. */
618 	case IVL_ST_DELAY:
619 	case IVL_ST_DELAYX:
620 	case IVL_ST_WAIT:
621 	    rtn = has_named_block(scope, ivl_stmt_sub_stmt(stmt));
622 	    break;
623 	default: /* The rest cannot have a named block. */ ;
624       }
625       return rtn;
626 }
627 
628 /*
629  * Look at all the processes to see if we can find one with the expected
630  * scope. If we don't find one then we can assume the block only has
631  * variable definitions and needs to be emitted here in the scope code.
632  */
no_stmts_in_process(ivl_process_t proc,ivl_scope_t scope)633 static int no_stmts_in_process(ivl_process_t proc, ivl_scope_t scope)
634 {
635       return has_named_block(scope, ivl_process_stmt(proc));
636 }
637 
638 /*
639  * If a named block has no statements then we may need to emit it here if
640  * there are variable definitions in the scope. We translate all this to
641  * an initial and named begin since that is enough to hold the variables.
642  */
emit_named_block_scope(ivl_scope_t scope)643 static void emit_named_block_scope(ivl_scope_t scope)
644 {
645       unsigned idx, count = ivl_scope_events(scope);
646       unsigned named_ev = 0;
647 
648 	/* If there are no parameters, signals or named events then skip
649 	 * this block. */
650       for (idx = 0; idx < count; idx += 1) {
651 	    ivl_event_t event = ivl_scope_event(scope, idx);
652 	      /* If this event has any type of edge sensitivity then it is
653 	       * not a named event. */
654 	    if (ivl_event_nany(event)) continue;
655 	    if (ivl_event_npos(event)) continue;
656 	    if (ivl_event_nneg(event)) continue;
657 	    named_ev = 1;
658 	    break;
659       }
660       if ((ivl_scope_params(scope) == 0) && (ivl_scope_sigs(scope) == 0) &&
661 	  (named_ev == 0)) return;
662 	/* Currently we only need to emit a named block for the variables
663 	 * if the parent scope is a module. This gets much more complicated
664 	 * if this is not true. */
665       if (ivl_scope_type(ivl_scope_parent(scope)) != IVL_SCT_MODULE) return;
666 	/* Scan all the processes looking for one that matches this scope.
667 	 * If a match is found then this named block was already emitted by
668 	 * the process code. */
669       if (ivl_design_process(design, (ivl_process_f)no_stmts_in_process,
670                              scope)) return;
671 	/* A match was not found so emit the named block here to get the
672 	 * variable definitions. */
673       fprintf(vlog_out, "\n%*cinitial begin: ", indent, ' ');
674       emit_id(ivl_scope_tname(scope));
675       emit_scope_file_line(scope);
676       fprintf(vlog_out, "\n");
677       indent += indent_incr;
678       emit_scope_variables(scope);
679       indent -= indent_incr;
680       fprintf(vlog_out, "%*cend  /* ", indent, ' ');
681       emit_id(ivl_scope_tname(scope));
682       fprintf(vlog_out, " */\n");
683 }
684 
685 /*
686  * In SystemVerilog a task, function, or block can have a process to
687  * initialize variables. SystemVerilog requires this to be before the
688  * initial/always blocks are processed, but there's no way to express
689  * this in Verilog-95.
690  */
find_tfb_process(ivl_process_t proc,ivl_scope_t scope)691 static int find_tfb_process(ivl_process_t proc, ivl_scope_t scope)
692 {
693       if (scope == ivl_process_scope(proc)) {
694 	    ivl_scope_t mod_scope = scope;
695 	      /* A task or function or named block can only have initial
696 	       * processes that are used to set local variables. */
697 	    assert(ivl_process_type(proc) == IVL_PR_INITIAL);
698 	      /* Find the module scope for this task/function. */
699 	    while (ivl_scope_type(mod_scope) != IVL_SCT_MODULE) {
700 		  mod_scope = ivl_scope_parent(mod_scope);
701 		  assert(mod_scope);
702 	    }
703 	      /* Emit the process in the module scope since that is where
704 	       * this all started. */
705 	    emit_process(mod_scope, proc);
706       }
707       return 0;
708 }
709 
710 /*
711  * Emit any initial blocks for the tasks/functions/named blocks in a module.
712  */
emit_tfb_process(ivl_scope_t scope,ivl_scope_t parent)713 static int emit_tfb_process(ivl_scope_t scope, ivl_scope_t parent)
714 {
715       ivl_scope_type_t sc_type = ivl_scope_type(scope);
716       (void)parent;  /* Parameter is not used. */
717       if ((sc_type == IVL_SCT_FUNCTION) || (sc_type == IVL_SCT_TASK) ||
718           (sc_type == IVL_SCT_BEGIN) || (sc_type == IVL_SCT_FORK)) {
719 	/* Output the initial/always blocks for this module. */
720 	    ivl_design_process(design, (ivl_process_f)find_tfb_process, scope);
721       }
722       return 0;
723 }
724 
emit_path_delay(ivl_scope_t scope,ivl_delaypath_t dpath)725 static void emit_path_delay(ivl_scope_t scope, ivl_delaypath_t dpath)
726 {
727       unsigned idx, count = 6;
728       uint64_t pdlys [12];
729       pdlys[0] = ivl_path_delay(dpath, IVL_PE_01);
730       pdlys[1] = ivl_path_delay(dpath, IVL_PE_10);
731       pdlys[2] = ivl_path_delay(dpath, IVL_PE_0z);
732       pdlys[3] = ivl_path_delay(dpath, IVL_PE_z1);
733       pdlys[4] = ivl_path_delay(dpath, IVL_PE_1z);
734       pdlys[5] = ivl_path_delay(dpath, IVL_PE_z0);
735       pdlys[6] = ivl_path_delay(dpath, IVL_PE_0x);
736       pdlys[7] = ivl_path_delay(dpath, IVL_PE_x1);
737       pdlys[8] = ivl_path_delay(dpath, IVL_PE_1x);
738       pdlys[9] = ivl_path_delay(dpath, IVL_PE_x0);
739       pdlys[10] = ivl_path_delay(dpath, IVL_PE_xz);
740       pdlys[11] = ivl_path_delay(dpath, IVL_PE_zx);
741 	/* If the first six pdlys match then this may be a 1 delay form. */
742       if ((pdlys[0] == pdlys[1]) &&
743           (pdlys[0] == pdlys[2]) &&
744           (pdlys[0] == pdlys[3]) &&
745           (pdlys[0] == pdlys[4]) &&
746           (pdlys[0] == pdlys[5])) count = 1;
747 	/* Check to see if only a rise and fall value are given for the first
748 	 * six pdlys. */
749       else if ((pdlys[0] == pdlys[2]) &&
750                (pdlys[0] == pdlys[3]) &&
751                (pdlys[1] == pdlys[4]) &&
752                (pdlys[1] == pdlys[5])) count = 2;
753 	/* Check to see if a rise, fall and high-Z value are given for the
754 	 * first six pdlys. */
755       else if ((pdlys[0] == pdlys[3]) &&
756                (pdlys[1] == pdlys[5]) &&
757                (pdlys[2] == pdlys[4])) count = 3;
758 	/* Now check to see if the 'bx related pdlys match the reduced
759 	 * delay form. If not then this is a twelve delay value. */
760       if ((pdlys[6]  != ((pdlys[0] < pdlys[2]) ?  pdlys[0] : pdlys[2])) ||
761           (pdlys[8]  != ((pdlys[1] < pdlys[4]) ?  pdlys[1] : pdlys[4])) ||
762           (pdlys[11] != ((pdlys[3] < pdlys[5]) ?  pdlys[3] : pdlys[5])) ||
763           (pdlys[7]  != ((pdlys[0] > pdlys[3]) ?  pdlys[0] : pdlys[3])) ||
764           (pdlys[9]  != ((pdlys[1] > pdlys[5]) ?  pdlys[1] : pdlys[5])) ||
765           (pdlys[10] != ((pdlys[2] > pdlys[4]) ?  pdlys[2] : pdlys[4]))) {
766 	    count = 12;
767       }
768       emit_scaled_delay(scope, pdlys[0]);
769       for(idx = 1; idx < count; idx += 1) {
770 	    fprintf(vlog_out, ", ");
771 	    emit_scaled_delay(scope, pdlys[idx]);
772       }
773 }
774 
emit_specify_paths(ivl_scope_t scope,ivl_signal_t sig)775 static void emit_specify_paths(ivl_scope_t scope, ivl_signal_t sig)
776 {
777       unsigned idx, count = ivl_signal_npath(sig);
778       for(idx = 0; idx < count; idx += 1) {
779 	    ivl_delaypath_t dpath = ivl_signal_path(sig, idx);
780 	    ivl_nexus_t cond = ivl_path_condit(dpath);
781 	    ivl_nexus_t source = ivl_path_source(dpath);
782 	    unsigned has_edge = 0;
783 	    fprintf(vlog_out, "%*c", indent, ' ');
784 	    if (cond) {
785 		  fprintf(vlog_out, "if (");
786 		  emit_nexus_as_ca(scope, cond, 0, 0);
787 		  fprintf(vlog_out, ") ");
788 	    } else if (ivl_path_is_condit(dpath)) {
789 		  fprintf(vlog_out, "ifnone ");
790 	    }
791 	    fprintf(vlog_out, "(");
792 	    if (ivl_path_source_posedge(dpath)) {
793 		  fprintf(vlog_out, "posedge ");
794 		  has_edge = 1;
795 	    }
796 	    if (ivl_path_source_negedge(dpath)) {
797 		  fprintf(vlog_out, "negedge ");
798 		  has_edge = 1;
799 	    }
800 	    emit_nexus_as_ca(scope, source, 0, 0);
801 	    if (ivl_path_is_parallel(dpath)) {
802 		  fprintf(vlog_out, " =>");
803 	    } else {
804 		  fprintf(vlog_out, " *>");
805 	    }
806 	      /* The compiler does not keep the source expression for an edge
807 	       * sensitive path so add a constant to get the syntax right. */
808 	    if (has_edge) {
809 		  fprintf(vlog_out, "(%s : 1'bx /* Missing */)",
810 		                    ivl_signal_basename(sig));
811 	    } else {
812 		  fprintf(vlog_out, "%s", ivl_signal_basename(sig));
813 	    }
814 	    fprintf(vlog_out, ") = (");
815 	    emit_path_delay(scope, dpath);
816 	    fprintf(vlog_out, ");\n");
817       }
818 }
819 
820 /*
821  * The path delay information from the specify block is attached to the
822  * output ports.
823  */
emit_specify(ivl_scope_t scope)824 static void emit_specify(ivl_scope_t scope)
825 {
826       unsigned word, idx, count = ivl_scope_ports(scope);
827       unsigned need_specify = 0;
828       for (idx = 0; idx < count; idx += 1) {
829 	    ivl_nexus_t nex = ivl_scope_mod_port(scope, idx);
830 	    ivl_signal_t port = get_port_from_nexus(scope, nex, &word);
831 // HERE: Do we need to use word? See emit_module_port_def().
832 	    assert(port);
833 	    if (ivl_signal_npath(port)) {
834 		  if (! need_specify) {
835 			fprintf(vlog_out, "\n%*cspecify\n", indent, ' ');
836 			need_specify = 1;
837 			indent += indent_incr;
838 		  }
839 		  emit_specify_paths(scope, port);
840 	    }
841       }
842       if (need_specify) {
843 	    indent -= indent_incr;
844 	    fprintf(vlog_out, "%*cendspecify\n", indent, ' ');
845       }
846 }
847 
848 /*
849  * Look for a disable in the statement (function body) for this scope.
850  */
has_func_disable(ivl_scope_t scope,ivl_statement_t stmt)851 static unsigned has_func_disable(ivl_scope_t scope, ivl_statement_t stmt)
852 {
853       unsigned idx, count, rtn = 0;
854 	/* If there is a statement then look to see if it is or has a
855 	 * disable for this function scope. */
856       if (! stmt) return 0;
857       assert(ivl_scope_type(scope) == IVL_SCT_FUNCTION);
858       switch (ivl_statement_type(stmt)) {
859 	  /* These are not allowed in a function. */
860 	case IVL_ST_ASSIGN_NB:
861 	case IVL_ST_DELAY:
862 	case IVL_ST_DELAYX:
863 	case IVL_ST_FORK:
864 	case IVL_ST_FORK_JOIN_ANY:
865 	case IVL_ST_FORK_JOIN_NONE:
866 	case IVL_ST_UTASK:
867 	case IVL_ST_WAIT:
868 	    assert(0);
869 	    break;
870 	  /* These are allowed in a function and cannot have a disable. */
871 	case IVL_ST_NOOP:
872 	case IVL_ST_ALLOC:
873 	case IVL_ST_ASSIGN:
874 	case IVL_ST_CASSIGN:
875 	case IVL_ST_DEASSIGN:
876 	case IVL_ST_FORCE:
877 	case IVL_ST_FREE:
878 	case IVL_ST_RELEASE:
879 	case IVL_ST_STASK:
880 	case IVL_ST_TRIGGER:
881 	    break;
882 	  /* Look for a disable in each block statement. */
883 	case IVL_ST_BLOCK:
884 	    count = ivl_stmt_block_count(stmt);
885 	    for (idx = 0; (idx < count) && ! rtn; idx += 1) {
886 		  rtn |= has_func_disable(scope,
887 		                          ivl_stmt_block_stmt(stmt, idx));
888 	    }
889 	    break;
890 	  /* Look for a disable in each case branch. */
891 	case IVL_ST_CASE:
892 	case IVL_ST_CASER:
893 	case IVL_ST_CASEX:
894 	case IVL_ST_CASEZ:
895 	    count = ivl_stmt_case_count(stmt);
896 	    for (idx = 0; (idx < count) && ! rtn; idx += 1) {
897 		  rtn |= has_func_disable(scope,
898 		                          ivl_stmt_case_stmt(stmt, idx));
899 	    }
900 	    break;
901 	  /* Either the true or false clause may have a disable. */
902 	case IVL_ST_CONDIT:
903 	    rtn = has_func_disable(scope, ivl_stmt_cond_true(stmt));
904 	    if (! rtn) {
905 		  rtn = has_func_disable(scope, ivl_stmt_cond_false(stmt));
906 	    }
907 	    break;
908 	  /* These have a single sub-statement so look for a disable there. */
909 	case IVL_ST_DO_WHILE:
910 	case IVL_ST_FOREVER:
911 	case IVL_ST_REPEAT:
912 	case IVL_ST_WHILE:
913 	    rtn = has_func_disable(scope, ivl_stmt_sub_stmt(stmt));
914 	    break;
915 	  /* The function has a disable if the disable scope matches the
916 	   * function scope. */
917 	case IVL_ST_DISABLE:
918 	    rtn = scope == ivl_stmt_call(stmt);
919 	    break;
920 	default:
921 	    fprintf(stderr, "%s:%u: vlog95 error: Unknown statement type (%d) "
922 	                    "in function disable check.\n",
923 	                    ivl_stmt_file(stmt),
924 	                    ivl_stmt_lineno(stmt),
925 	                    (int)ivl_statement_type(stmt));
926 
927 	    vlog_errors += 1;
928 	    break;
929       }
930       return rtn;
931 }
932 
933 /*
934  * This is the block name used when a SystemVerilog return is used in a
935  * function and the body does not already have an enclosing named block.
936  * This is needed since the actual function cannot be disabled.
937  */
get_func_return_name(ivl_scope_t scope)938 static char *get_func_return_name(ivl_scope_t scope)
939 {
940       const char *name_func = ivl_scope_basename(scope);
941       const char *name_head = "_ivl_";
942       const char *name_tail = "_return";
943       char *name_return;
944       name_return = (char *)malloc(strlen(name_head) +
945                                    strlen(name_func) +
946                                    strlen(name_tail) + 1);
947       name_return[0] = 0;
948       (void) strcpy(name_return, name_head);
949       (void) strcat(name_return, name_func);
950       (void) strcat(name_return, name_tail);
951       return name_return;
952 }
953 
954 /*
955  * This search method may be slow for a large structural design with a
956  * large number of gate types. That's not what this converter was built
957  * for so this is probably OK. If this becomes an issue then we need a
958  * better method/data structure.
959  */
960 static const char **scopes_emitted = 0;
961 static unsigned num_scopes_emitted = 0;
962 
scope_has_been_emitted(ivl_scope_t scope)963 static unsigned scope_has_been_emitted(ivl_scope_t scope)
964 {
965       unsigned idx;
966       for (idx = 0; idx < num_scopes_emitted; idx += 1) {
967 	    if (! strcmp(ivl_scope_tname(scope), scopes_emitted[idx])) return 1;
968       }
969       return 0;
970 }
971 
add_scope_to_list(ivl_scope_t scope)972 static void add_scope_to_list(ivl_scope_t scope)
973 {
974       num_scopes_emitted += 1;
975       scopes_emitted = realloc(scopes_emitted, num_scopes_emitted *
976                                                sizeof(char *));
977       scopes_emitted[num_scopes_emitted-1] = ivl_scope_tname(scope);
978 }
979 
free_emitted_scope_list()980 void free_emitted_scope_list()
981 {
982       free(scopes_emitted);
983       scopes_emitted = 0;
984       num_scopes_emitted = 0;
985 }
986 
987 /*
988  * A list of module scopes that need to have their definition emitted when
989  * the current root scope (module) is finished is kept here.
990  */
991 static ivl_scope_t *scopes_to_emit = 0;
992 static unsigned num_scopes_to_emit = 0;
993 static unsigned emitting_scopes = 0;
994 
emit_scope(ivl_scope_t scope,ivl_scope_t parent)995 int emit_scope(ivl_scope_t scope, ivl_scope_t parent)
996 {
997       char *package_name = 0;
998       ivl_scope_type_t sc_type = ivl_scope_type(scope);
999       unsigned is_auto = ivl_scope_is_auto(scope);
1000       unsigned idx;
1001 
1002 	/* Output the scope definition. */
1003       switch (sc_type) {
1004 	case IVL_SCT_MODULE:
1005 	    assert(!is_auto);
1006 	      /* This is an instantiation. */
1007 	    if (parent) {
1008 		  assert(indent != 0);
1009 		    /* If the module has parameters then it may not be unique
1010 		     * so we create a mangled name version instead. */
1011 		  fprintf(vlog_out, "\n%*c", indent, ' ');
1012 		  emit_mangled_name(scope, !parent && !emitting_scopes);
1013 		  fprintf(vlog_out, " ");
1014 		  emit_id(ivl_scope_basename(scope));
1015 		  fprintf(vlog_out, "(");
1016 		  emit_module_call_expressions(scope);
1017 		  fprintf(vlog_out, ");");
1018 		  emit_scope_file_line(scope);
1019 		  fprintf(vlog_out, "\n");
1020 		  num_scopes_to_emit += 1;
1021 		  scopes_to_emit = realloc(scopes_to_emit, num_scopes_to_emit *
1022 		                                           sizeof(ivl_scope_t));
1023 		  scopes_to_emit[num_scopes_to_emit-1] = scope;
1024 		  return 0;
1025 	    }
1026 	    assert(indent == 0);
1027 	      /* Set the time scale for this scope. */
1028 	    fprintf(vlog_out, "\n`timescale %s/%s\n",
1029 	                      get_time_const(ivl_scope_time_units(scope)),
1030 	                      get_time_const(ivl_scope_time_precision(scope)));
1031 	    if (ivl_scope_is_cell(scope)) {
1032 		  fprintf(vlog_out, "`celldefine\n");
1033 	    }
1034 	    fprintf(vlog_out, "/* This module was originally defined in "
1035 	                      "file %s at line %u. */\n",
1036 	                      ivl_scope_def_file(scope),
1037 	                      ivl_scope_def_lineno(scope));
1038 	    fprintf(vlog_out, "module ");
1039 	    emit_mangled_name(scope, !parent && !emitting_scopes);
1040 	    emit_module_ports(scope);
1041 	    break;
1042 	case IVL_SCT_FUNCTION:
1043 	      /* Root scope functions have already been emitted. */
1044 	    if (! parent) return 0;
1045 	    assert(indent != 0);
1046 	    fprintf(vlog_out, "\n%*cfunction", indent, ' ');
1047 	    if (ivl_scope_ports(scope) < 1) {
1048 		  fprintf(stderr, "%s:%u: vlog95 error: Function (%s) has "
1049 		                  "no return value.\n",
1050 		                  ivl_scope_file(scope),
1051 		                  ivl_scope_lineno(scope),
1052 		                  ivl_scope_tname(scope));
1053 		  vlog_errors += 1;
1054 	    }
1055 	      /* The function return information is the zero port. */
1056 	    emit_func_return(ivl_scope_port(scope, 0));
1057 	    fprintf(vlog_out, " ");
1058 	    emit_id(ivl_scope_tname(scope));
1059 	    if (is_auto) {
1060 		  fprintf(stderr, "%s:%u: vlog95 error: Automatic functions "
1061 	                    "(%s) are not supported.\n", ivl_scope_file(scope),
1062 	                    ivl_scope_lineno(scope), ivl_scope_tname(scope));
1063 		  vlog_errors += 1;
1064 	    }
1065 	    break;
1066 	case IVL_SCT_TASK:
1067 	      /* Root scope tasks have already been emitted. */
1068 	    if (! parent) return 0;
1069 	    assert(indent != 0);
1070 	    fprintf(vlog_out, "\n%*ctask ", indent, ' ');
1071 	    emit_id(ivl_scope_tname(scope));
1072 	    if (is_auto) {
1073 		  fprintf(stderr, "%s:%u: vlog95 error: Automatic tasks "
1074 	                    "(%s) are not supported.\n", ivl_scope_file(scope),
1075 	                    ivl_scope_lineno(scope), ivl_scope_tname(scope));
1076 		  vlog_errors += 1;
1077 	    }
1078 	    break;
1079 	case IVL_SCT_BEGIN:
1080 	case IVL_SCT_FORK:
1081 	    assert(indent != 0);
1082 	    emit_named_block_scope(scope);
1083 	    return 0; /* A named begin/fork is handled in line. */
1084 	case IVL_SCT_GENERATE:
1085 	    fprintf(stderr, "%s:%u: vlog95 sorry: generate scopes are not "
1086 	                    "currently translated \"%s\".\n",
1087 	                    ivl_scope_file(scope),
1088 	                    ivl_scope_lineno(scope),
1089 	                    ivl_scope_tname(scope));
1090 	    vlog_errors += 1;
1091 	    return 0;
1092 	case IVL_SCT_PACKAGE:
1093 	    assert(indent == 0);
1094 	    assert(! parent);
1095 	      /* Set the time scale for this scope. */
1096 	    fprintf(vlog_out, "\n`timescale %s/%s\n",
1097 	                      get_time_const(ivl_scope_time_units(scope)),
1098 	                      get_time_const(ivl_scope_time_precision(scope)));
1099 	      /* Emit a package as a module with a special name. */
1100 	    fprintf(vlog_out, "/* This package (module) was originally "
1101 	                      "defined in file %s at line %u. */\n",
1102 	                      ivl_scope_def_file(scope),
1103 	                      ivl_scope_def_lineno(scope));
1104 	    fprintf(vlog_out, "module ");
1105 	    package_name = get_package_name(scope);
1106 	    emit_id(package_name);
1107 	    break;
1108 	case IVL_SCT_CLASS:
1109 	    fprintf(stderr, "%s:%u: vlog95 sorry: class scopes are not "
1110 	                    "currently translated \"%s\".\n",
1111 	                    ivl_scope_file(scope),
1112 	                    ivl_scope_lineno(scope),
1113 	                    ivl_scope_tname(scope));
1114 	    vlog_errors += 1;
1115 	    return 0;
1116 	default:
1117 	    fprintf(stderr, "%s:%u: vlog95 error: Unsupported scope type "
1118 	                    "(%d) named: %s.\n", ivl_scope_file(scope),
1119 	                    ivl_scope_lineno(scope), sc_type,
1120 	                    ivl_scope_tname(scope));
1121 	    vlog_errors += 1;
1122 	    return 0;
1123       }
1124       fprintf(vlog_out, ";");
1125       emit_scope_file_line(scope);
1126       fprintf(vlog_out, "\n");
1127       indent += indent_incr;
1128 
1129 	/* Output the scope ports for this scope. */
1130       if (sc_type == IVL_SCT_MODULE) {
1131 	    emit_module_port_defs(scope);
1132       } else {
1133 	    emit_task_func_port_defs(scope);
1134       }
1135 
1136       emit_scope_variables(scope);
1137 
1138       if (sc_type == IVL_SCT_MODULE) {
1139 	    unsigned count = ivl_scope_lpms(scope);
1140 	      /* Output the LPM devices. */
1141 	    for (idx = 0; idx < count; idx += 1) {
1142 		  emit_lpm(scope, ivl_scope_lpm(scope, idx));
1143 	    }
1144 
1145 	      /* Output any logic devices. */
1146 	    count = ivl_scope_logs(scope);
1147 	    for (idx = 0; idx < count; idx += 1) {
1148 		  emit_logic(scope, ivl_scope_log(scope, idx));
1149 	    }
1150 
1151 	      /* Output any switch (logic) devices. */
1152 	    count = ivl_scope_switches(scope);
1153 	    for (idx = 0; idx < count; idx += 1) {
1154 		  emit_tran(scope, ivl_scope_switch(scope, idx));
1155 	    }
1156 
1157 	      /* Output any initial blocks for tasks or functions or named
1158 	       * blocks defined in this module. Used to initialize local
1159 	       * variables. */
1160 	    ivl_scope_children(scope, (ivl_scope_f*) emit_tfb_process, scope);
1161 
1162 	      /* Output the initial/always blocks for this module. */
1163 	    ivl_design_process(design, (ivl_process_f)find_process, scope);
1164       }
1165 
1166 	/* Output the function body. */
1167       if (sc_type == IVL_SCT_FUNCTION) {
1168 	    ivl_statement_t body = ivl_scope_def(scope);
1169 	    assert(func_rtn_name == 0);
1170 	      /* If the function disables itself then that is really a
1171 	       * SystemVerilog return statement in disguise. A toplevel
1172 	       * named begin is needed to make this work in standard Verilog
1173 	       * so add one if it is needed. */
1174 	    if (ivl_statement_type(body) == IVL_ST_BLOCK) {
1175 		  ivl_scope_t blk_scope = ivl_stmt_block_scope(body);
1176 		  if (blk_scope) {
1177 			func_rtn_name = ivl_scope_basename(blk_scope);
1178 			emit_stmt(scope, body);
1179 			func_rtn_name = 0;
1180 		  } else if (has_func_disable(scope, body)) {
1181 			char *name_return = get_func_return_name(scope);
1182 			unsigned count = ivl_stmt_block_count(body);
1183 			fprintf(vlog_out, "%*cbegin: %s\n", indent, ' ',
1184 			                  name_return);
1185 			indent += indent_incr;
1186 			func_rtn_name = name_return;
1187 			for (idx = 0; idx < count; idx += 1) {
1188 			      emit_stmt(scope, ivl_stmt_block_stmt(body, idx));
1189 			}
1190 			func_rtn_name = 0;
1191 			indent -= indent_incr;
1192 			fprintf(vlog_out, "%*cend /* %s */\n", indent, ' ',
1193 			                  name_return);
1194 			free(name_return);
1195 		  } else emit_stmt(scope, body);
1196 	      /* A non-block statement may need a named block for a return. */
1197 	    } else if (has_func_disable(scope, body)) {
1198 		  char *name_return = get_func_return_name(scope);
1199 		  fprintf(vlog_out, "%*cbegin: %s\n", indent, ' ',
1200 		                     name_return);
1201 		  indent += indent_incr;
1202 		  func_rtn_name = name_return;
1203 		  emit_stmt(scope, body);
1204 		  func_rtn_name = 0;
1205 		  indent -= indent_incr;
1206 		  fprintf(vlog_out, "%*cend /* %s */\n", indent, ' ',
1207 		                    name_return);
1208 		  free(name_return);
1209 	    } else emit_stmt(scope, body);
1210       }
1211 	/* Output the task body. */
1212       if (sc_type == IVL_SCT_TASK) emit_stmt(scope, ivl_scope_def(scope));
1213 
1214 	/* Print any sub-scopes. */
1215       ivl_scope_children(scope, (ivl_scope_f*) emit_scope, scope);
1216 
1217 	/* And finally print a specify block when needed. */
1218       if (sc_type == IVL_SCT_MODULE) emit_specify(scope);
1219 
1220 	/* Output the scope ending. */
1221       assert(indent >= indent_incr);
1222       indent -= indent_incr;
1223       switch (sc_type) {
1224 	case IVL_SCT_MODULE:
1225 	    assert(indent == 0);
1226 	    fprintf(vlog_out, "endmodule  /* ");
1227 	    emit_mangled_name(scope, !parent && !emitting_scopes);
1228 	    fprintf(vlog_out, " */\n");
1229 	    if (ivl_scope_is_cell(scope)) {
1230 		  fprintf(vlog_out, "`endcelldefine\n");
1231 	    }
1232 	      /* If this is a root scope then emit any saved instance scopes.
1233 	       * Save any scope that does not have parameters/a mangled name
1234 	       * to a list so we don't print duplicate module definitions. */
1235 	    if (!emitting_scopes) {
1236 		  emitting_scopes = 1;
1237 		  for (idx =0; idx < num_scopes_to_emit; idx += 1) {
1238 			ivl_scope_t scope_to_emit = scopes_to_emit[idx];
1239 			if (scope_has_been_emitted(scope_to_emit)) continue;
1240 			(void) emit_scope(scope_to_emit, 0);
1241 			  /* If we used a mangled name then the instance is
1242 			   * unique so don't add it to the list. */
1243 			if (ivl_scope_params(scope_to_emit)) continue;
1244 			add_scope_to_list(scope_to_emit);
1245 		  }
1246 		  free(scopes_to_emit);
1247 		  scopes_to_emit = 0;
1248 		  num_scopes_to_emit = 0;
1249 		  emitting_scopes = 0;
1250 	    }
1251 	    break;
1252 	case IVL_SCT_FUNCTION:
1253 	    fprintf(vlog_out, "%*cendfunction  /* %s */\n", indent, ' ',
1254 	                      ivl_scope_tname(scope));
1255 	    break;
1256 	case IVL_SCT_TASK:
1257 	    fprintf(vlog_out, "%*cendtask  /* %s */\n", indent, ' ',
1258 	                      ivl_scope_tname(scope));
1259 	    break;
1260 	case IVL_SCT_PACKAGE:
1261 	    fprintf(vlog_out, "endmodule  /* ");
1262 	    emit_id(package_name);
1263 	    free(package_name);
1264 	    fprintf(vlog_out, " */\n");
1265 	    break;
1266 	default:
1267 	    assert(0);
1268 	    break;
1269       }
1270       return 0;
1271 }
1272