Fix for assertion error when expanding macro.
[iverilog.git] / ieee1364-notes.txt
blob6df9df41616db79b51ef2c448a187768298dca88
2 NOTE: THE CONTENTS OF THIS FILE ARE BEING MOVED TO THE DOCUMENTATION
3 WIKI AT http://iverilog.wikia.com. PLEASE ADD NEW ENTRIES THERE.
5                 Icarus Verilog vs. IEEE1364
6                 Copyright 2000 Stephen Williams
8 The IEEE1364 standard is the bible that defines the correctness of the
9 Icarus Verilog implementation and behavior of the compiled
10 program. The IEEE1364.1 is also referenced for matters of
11 synthesis. So the ultimate definition of right and wrong comes from
12 those documents.
14 That does not mean that a Verilog implementation is fully
15 constrained. The standard document allows for implementation specific
16 behavior that, when properly accounted for, does not effect the
17 intended semantics of the specified language. It is therefore possible
18 and common to write programs that produce different results when run
19 by different Verilog implementations.
22 STANDARDIZATION ISSUES
24 These are some issues where the IEEE1364 left unclear, unspecified or
25 simply wrong. I'll try to be precise as I can, and reference the
26 standard as needed. I've made implementation decisions for Icarus
27 Verilog, and I will make clear what those decisions are and how they
28 affect the language.
30 * OBJECTS CAN BE DECLARED ANYWHERE IN THE MODULE
32 Consider this module:
34     module sample1;
35         initial foo = 1;
36         reg foo;
37         wire tmp = bar;
38         initial #1 $display("foo = %b, bar = %b", foo, tmp);
39     endmodule
41 Notice that the ``reg foo;'' declaration is placed after the first
42 initial statement. It turns out that this is a perfectly legal module
43 according to the -1995 and -2000 versions of the standard. The
44 statement ``reg foo;'' is a module_item_declaration which is in turn a
45 module_item. The BNF in the appendix of IEEE1364-1995 treats all
46 module_item statements equally, so no order is imposed.
48 Furthermore, there is no text (that I can find) elsewhere in the
49 standard that imposes any ordering restriction. The sorts of
50 restrictions I would look for are "module_item_declarations must
51 appear before all other module_items" or "variables must be declared
52 textually before they are referenced." Such statements simply do not
53 exist. (Personally, I think it is fine that they don't.)
55 The closest is the rules for implicit declarations of variables that
56 are otherwise undeclared. In the above example, ``bar'' is implicitly
57 declared and is therefore a wire. However, although ``initial foo = 1;''
58 is written before foo is declared, foo *is* declared within the
59 module, and declared legally by the BNF of the standard.
61 Here is another example:
63     module sample2;
64         initial x.foo = 1;
65         test x;
66         initial #1 $display("foo = %b", x.foo);
67     endmodule
69     module test;
70         reg foo;
71     endmodule;
73 From this example one can clearly see that foo is once again declared
74 after its use in behavioral code. One also sees a forward reference of
75 an entire module. Once again, the standard places no restriction on
76 the order of module declarations in a source file, so this program is,
77 according to the standard, perfectly well formed.
79 Icarus Verilog interprets both of these examples according to "The
80 Standard As I Understand It." However, commercial tools in general
81 break down with these programs. In particular, the first example
82 may generate different errors depending on the tool. The most common
83 error is to claim that ``foo'' is declared twice, once (implicitly) as
84 a wire and once as a reg.
86 So the question now becomes, "Is the standard broken, or are the tools
87 limited?" Coverage of the standard seems to vary widely from tool to
88 tool so it is not clear that the standard really is at fault. It is
89 clear, however, that somebody goofed somewhere.
91 My personal opinion is that there is no logical need to require that
92 all module_item_declarations precede any other module items. I
93 personally would oppose such a restriction. It may make sense to
94 require that declarations of variables within a module be preceded by
95 their use, although even that is not necessary for the implementation
96 of efficient compilers.
98 However, the existence hierarchical naming syntax as demonstrated in
99 sample2 can have implications that affect any declaration order
100 rules. When reaching into a module with a hierarchical name, the
101 module being referenced is already completely declared (or not
102 declared at all, as in sample2) so module_item order is completely
103 irrelevant. But a "declare before use" rule would infect module
104 ordering, by requiring that modules that are used be first defined.
107 * TASK AND FUNCTION PARAMETERS CANNOT HAVE EXPLICIT TYPES
109 Consider a function negate that wants to take a signed integer value
110 and return its negative:
112         function integer negate;
113             input [15:0] val;
114             negate = -val;
115         endfunction
117 This is not quite right, because the input is implicitly a reg type,
118 which is unsigned. The result, then, will always be a negative value,
119 even if a negative val is passed in.
121 It is possible to fix up this specific example to work properly with
122 the bit pattern of a 16bit number, but that is not the point. What's
123 needed is clarification on whether an input can be declared in the
124 port declaration as well as in the contained block declaration.
126 As I understand the situation, this should be allowed:
128         function integer negate;
129             input [15:0] val;
130             reg signed [15:0] val;
131             negate = -val;
132         endfunction
134 In the -1995 standard, the variable is already implicitly a reg if
135 declared within a function or task. However, in the -2000 standard
136 there is now (as in this example) a reason why one might want to
137 actually declare the type explicitly.
139 I think that a port *cannot* be declared as an integer or time type
140 (though the result can) because the range of the port declaration must
141 match the range of the integer/time declaration, but the range of
142 integers is unspecified. This, by the way, also applies to module
143 ports.
145 With the above in mind, I have decided to *allow* function and task
146 ports to be declared with types, as long as the types are variable
147 types, such as reg or integer. Without this, there would be no
148 portable way to pass integers into functions/tasks. The standard does
149 not say it is allowed, but it doesn't *disallow* it, and other
150 commercial tools seem to work similarly.
153 * ROUNDING OF TIME
155 When the `timescale directive is present, the compiler is supposed to
156 round fractional times (after scaling) to the nearest integer. The
157 confusing bit here is that it is apparently conventional that if the
158 `timescale directive is *not* present, times are rounded towards zero
159 always.
162 * VALUE OF X IN PRIMITIVE OUTPUTS
164 The IEEE1364-1995 standard clearly states in Table 8-1 that the x
165 symbols is allowed in input columns, but is not allowed in
166 outputs. Furthermore, none of the examples have an x in the output of
167 a primitive. Table 8-1 in the IEEE1364-2000 also says the same thing.
169 However, the BNF clearly states that 0, 1, x and X are valid
170 output_symbol characters. The standard is self contradictory. So I
171 take it that x is allowed, as that is what Verilog-XL does.
174 * REPEAT LOOPS vs. REPEAT EVENT CONTROL
176 There seems to be ambiguity in how code like this should be parsed:
178         repeat (5) @(posedge clk) <statement>;
180 There are two valid interpretations of this code, from the
181 IEEE1364-1995 standard. One looks like this:
183     procedural_timing_control_statement ::=
184           delay_or_event_control  statement_or_null
186     delay_or_event_control ::=
187           event_control
188           | repeat ( expression ) event_control
190 If this interpretation is used, then the statement <statement> should
191 be executed after the 5th posedge of clk. However, there is also this
192 interpretation:
194     loop_statement ::=
195          repeat ( expression ) statement
197 If *this* interpretation is used, then <statement> should be executed
198 5 times on the posedge of clk. The way the -1995 standard is written,
199 these are both equally valid interpretations of the example, yet they
200 produce very different results. The standard offers no guidance on how
201 to resolve this conflict, and the IEEE1364-2000 DRAFT does not improve
202 the situation.
204 Practice suggests that a repeat followed by an event control should be
205 interpreted as a loop head, and this is what Icarus Verilog does, as
206 well as all the other major Verilog tools, but the standard does not
207 say this.
209 * UNSIZED NUMERIC CONSTANTS ARE NOT LIMITED TO 32 BITS
211 The Verilog standard allows Verilog implementations to limit the size
212 of unsized constants to a bit width of at least 32. That means that a
213 constant 17179869183 (36'h3_ffff_ffff) may overflow some compilers. In
214 fact, it is common to limit these values to 32bits. However, a
215 compiler may just as easily choose another width limit, for example
216 64bits. That value is equally good.
218 However, it is not *required* that an implementation truncate at 32
219 bits, and in fact Icarus Verilog does not truncate at all. It will
220 make the unsized constant as big as it needs to be to hold the value
221 accurately. This is especially useful in situations like this;
223             reg [width-1:0] foo = 17179869183;
225 The programmer wants the constant to take on the width of the reg,
226 which in this example is parameterized. Since constant sizes cannot be
227 parameterized, the programmer ideally gives an unsized constant, which
228 the compiler then expands/contracts to match the l-value.
230 Also, by choosing to not ever truncate, Icarus Verilog can handle code
231 written for a 64bit compiler as easily as for a 32bit compiler. In
232 particular, any constants that the user does not expect to be
233 arbitrarily truncated by his compiler will also not be truncated by
234 Icarus Verilog, no matter what that other compiler chooses as a
235 truncation point.
238 * UNSIZED EXPRESSIONS AS PARAMETERS TO CONCATENATION {}
240 The Verilog standard clearly states in 4.1.14:
242         "Unsized constant numbers shall not be allowed in
243         concatenations. This is because the size of each
244         operand in the concatenation is needed to calculate
245         the complete size of the concatenation."
247 So for example the expression {1'b0, 16} is clearly illegal. It
248 also stands to reason that {1'b0, 15+1} is illegal, for exactly the
249 same justification. What is the size of the expression (15+1)?
250 Furthermore, it is reasonable to expect that (16) and (15+1) are
251 exactly the same so far as the compiler is concerned.
253 Unfortunately, Cadence seems to feel otherwise. In particular, it has
254 been reported that although {1'b0, 16} causes an error, {1'b0, 15+1}
255 is accepted. Further testing shows that any expression other than a
256 simple unsized constant is accepted there, even if all the operands of
257 all the operators that make up the expression are unsized integers.
259 This is a semantic problem. Icarus Verilog doesn't limit the size of
260 integer constants. This is valid as stated in 2.5.1 Note 3:
262         "The number of bits that make up an unsized number
263         (which is a simple decimal number or a number without
264         the size specification) shall be *at*least* 32."
265         [emphasis added]
267 Icarus Verilog will hold any integer constant, so the size will be as
268 large as it needs to be, whether that is 64bits, 128bits, or
269 more. With this in mind, what is the value of these expressions?
271         {'h1_00_00_00_00}
272         {'h1 << 32}
273         {'h0_00_00_00_01 << 32}
274         {'h5_00_00_00_00 + 1}
276 These examples show that the standard is justified in requiring that
277 the operands of concatenation have size. The dispute is what it takes
278 to cause an expression to have a size, and what that size is.
279 Verilog-XL claims that (16) does not have a size, but (15+1) does. The
280 size of the expression (15+1) is the size of the adder that is
281 created, but how wide is the adder when adding unsized constants?
283 One might note that the quote from section 4.1.14 says "Unsized
284 *constant*numbers* shall not be allowed." It does not say "Unsized
285 expressions...", so arguably accepting (15+1) or even (16+0) as an
286 operand to a concatenation is not a violation of the letter of the
287 law. However, the very next sentence of the quote expresses the
288 intent, and accepting (15+1) as having a more defined size then (16)
289 seems to be a violation of that intent.
291 Whatever a compiler decides the size is, the user has no way to
292 predict it, and the compiler should not have the right to treat (15+1)
293 any differently then (16). Therefore, Icarus Verilog takes the
294 position that such expressions are *unsized* and are not allowed as
295 operands to concatenations. Icarus Verilog will in general assume that
296 operations on unsized numbers produce unsized results. There are
297 exceptions when the operator itself does define a size, such as the
298 comparison operators or the reduction operators. Icarus Verilog will
299 generate appropriate error messages.
302 * MODULE INSTANCE WITH WRONG SIZE PORT LIST
304 A module declaration like this declares a module that takes three ports:
306         module three (a, b, c);
307           input a, b, c;
308           reg x;
309         endmodule
311 This is fine and obvious. It is also clear from the standard that
312 these are legal instantiations of this module:
314         three u1 (x,y,z);
315         three u2 ( ,y, );
316         three u3 ( , , );
317         three u4 (.b(y));
319 In some of the above examples, there are unconnected ports. In the
320 case of u4, the pass by name connects only port b, and leaves a and c
321 unconnected. u2 and u4 are the same thing, in fact, but using
322 positional or by-name syntax. The next example is a little less
323 obvious:
325         three u4 ();
327 The trick here is that strictly speaking, the parser cannot tell
328 whether this is a list of no pass by name ports (that is, all
329 unconnected) or an empty positional list. If this were an empty
330 positional list, then the wrong number of ports is given, but if it is
331 an empty by-name list, it is an obviously valid instantiation. So it
332 is fine to accept this case as valid.
334 These are more doubtful:
336         three u5(x,y);
337         three u6(,);
339 These are definitely positional port lists, and they are definitely
340 the wrong length. In this case, the standard is not explicit about
341 what to do about positional port lists in module instantiations,
342 except that the first is connected to the first, second to second,
343 etc. It does not say that the list must be the right length, but every
344 example of unconnected ports used by-name syntax, and every example of
345 ordered list has the right size list.
347 Icarus Verilog takes the (very weak) hint that ordered lists should be
348 the right length, and will therefore flag instances u5 and u6 as
349 errors. The IEEE1364 standard should be more specific one way or the
350 other.
352 * UNKNOWN VALUES IN L-VALUE BIT SELECTS
354 Consider this example:
356         reg [7:0] vec;
357         wire [4:0] idx = <expr>;
358         [...]
359         vec[idx] = 1;
361 So long as the value of idx is a valid bit select address, the
362 behavior of this assignment is obvious. However, there is no explicit
363 word in the standard as to what happens if the value is out of
364 range. The standard clearly states the value of an expression when the
365 bit-select or part select is out of range (the value is x) but does
366 not address the behavior when the expression is an l-value.
368 Icarus Verilog will take the position that bit select expressions in
369 the l-value will select oblivion if it is out of range. That is, if
370 idx has a value that is not a valid bit select of vec, then the
371 assignment will have no effect.
374 * SCHEDULING VALUES IN LOGIC
376 The interaction between blocking assignments in procedural code and
377 logic gates in gate-level code and expressions is poorly defined in
378 Verilog. Consider this example:
380    reg a;
381    reg b;
382    wire q = a & b;
384    initial begin
385       a = 1;
386       b = 0;
387       #1 b = 1;
388       if (q !== 0) begin
389          $display("FAILED -- q changed too soon? %b", q);
390          $finish;
391       end
392    end
394 This is a confusing situation. It is clear from the Verilog standard
395 that an assignment to a variable using a blocking assign causes the
396 l-value to receive the value before the assignment completes. This
397 means that a subsequent read of the assigned variable *must* read back
398 what was blocking-assigned.
400 However, in the example above, the "wire q = a & b" expresses some
401 gate logic between a/b and q. The standard does not say whether a read
402 out of logic should read the value computed from previous assigns to
403 the input from the same thread. Specifically, when "a" and "b" are
404 assigned by blocking assignments, will a read of "q" get the computed
405 value or the existing value?
407 In fact, existing commercial tools do it both ways. Some tools print
408 the FAILED message in the above example, and some do not. Icarus
409 Verilog does not print the FAILED message in the above example,
410 because the gate value change is *scheduled* when inputs are assigned,
411 but not propagated until the thread gives up the processor.
413 Icarus Verilog chooses this behavior in order to filter out zero-width
414 pulses as early as possible. The implication of this is that a read of
415 the output of combinational logic will most likely *not* reflect the
416 changes in inputs until the thread that changed the inputs yields
417 execution.
420 * BIT AND PART SELECTS OF PARAMETERS
422 Bit and part selects are supposed to only be supported on vector nets
423 and variables (wires, regs, etc.) However, it is common for Verilog
424 compilers to also support bit and part select on parameters. Icarus
425 Verilog also chooses to support bit and part selects on parameter
426 names, but we need to define what that means.
428 A bit or a part select on a parameter expression returns an unsigned
429 value with a defined size. The parameter value is considered be a
430 constant vector of bits foo[X:0]. That is, zero based. The bit and
431 part selects operate from that assumption.
433 Verilog 2001 adds syntax to allow the user to explicitly declare the
434 parameter range (i.e. parameter [5:0] foo = 9;) so Icarus Verilog will
435 (or should) use the explicitly declared vector dimensions to interpret
436 bit and part selects.
439 * EDGES OF VECTORS
441 Consider this example:
443    reg [ 5:0] clock;
444    always @(posedge clock) [do stuff]
446 The IEEE1364 standard clearly states that the @(posedge clock) looks
447 only at the bit clock[0] (the least significant bit) to search for
448 edges. It has been pointed out by some that Verilog XL instead
449 implements it as "@(posedge |clock)": it looks for a rise in the
450 reduction or of the vector. Cadence Design Systems technical support
451 has been rumored to claim that the IEEE1364 specification is wrong,
452 but NC-Verilog behaves according to the specification, and thus
453 different from XL.
455 Icarus Verilog, therefore, takes the position that the specification
456 is clear and correct, and it behaves as does NC-Verilog in this
457 matter.
460 * REAL VARIABLES IN $dumpoff DEAD-ZONES
462 The IEEE1364 standard clearly states that in VCD files, the $dumpoff
463 section checkpoints all the dumped variables as X values. For reg and
464 wire bits/vectors, this obviously means 'bx values. Icarus Verilog
465 does this, for example:
467     $dumpoff
468     x!
469     x"
470     $end
472 Real variables can also be included in VCD dumps, but it is not at
473 all obvious what is supposed to be dumped into the $dumpoff-$end
474 section of the VCD file. Verilog-XL dumps "r0 !" to set the real
475 variables to the dead-zone value of 0.0, whereas other tools, such as
476 ModelTech, ignore real variables in this section.
478 For example (from XL):
480     $dumpoff
481     r0 !
482     r0 "
483     $end
485 Icarus Verilog dumps NaN values for real variables in the
486 $dumpoff-$end section of the VCD file. The NaN value is the IEEE754
487 equivalent of an unknown value, and so better reflects the unknown
488 (during the dead zone) status of the variable, like this:
490     $dumpoff
491     rNaN !
492     rNaN "
493     $end
495 It turns out that NaN is conventionally accepted by scanf functions,
496 and viewers that support real variables support NaN values. So while
497 the IEEE1364 doesn't require this behavior, and given the variety that
498 already seems to exist amongst VCD viewers in the wild, this behavior
499 seems to be acceptable according to the standard, is a better mirror
500 of 4-value behavior in the dead zone, and appears more user friendly
501 when viewed by reasonable viewers.
504 $Id: ieee1364-notes.txt,v 1.19 2007/04/18 02:36:13 steve Exp $
505 $Log: ieee1364-notes.txt,v $
506 Revision 1.19  2007/04/18 02:36:13  steve
507  Put to iverilog wiki for further notes.
509 Revision 1.18  2007/03/22 16:08:16  steve
510  Spelling fixes from Larry
512 Revision 1.17  2003/07/15 03:49:22  steve
513  Spelling fixes.
515 Revision 1.16  2003/04/14 03:40:21  steve
516  Make some effort to preserve bits while
517  operating on constant values.
519 Revision 1.15  2003/02/16 23:39:08  steve
520  NaN in dead zones of VCD dumps.
522 Revision 1.14  2003/02/06 17:51:36  steve
523  Edge of vectors notes.
525 Revision 1.13  2002/08/20 04:11:53  steve
526  Support parameters with defined ranges.
528 Revision 1.12  2002/06/11 03:34:33  steve
529  Spelling patch (Larry Doolittle)
531 Revision 1.11  2002/04/27 02:38:04  steve
532  Support selecting bits from parameters.
534 Revision 1.10  2002/03/31 01:54:13  steve
535  Notes about scheduling
537 Revision 1.9  2002/01/26 02:08:07  steve
538  Handle x in l-value of set/x
540 Revision 1.8  2001/08/01 05:17:31  steve
541  Accept empty port lists to module instantiation.
543 Revision 1.7  2001/02/17 05:27:31  steve
544  I allow function ports to have types.
546 Revision 1.6  2001/02/12 16:48:04  steve
547  Rant about bit widths.
549 Revision 1.5  2001/01/02 17:28:08  steve
550  Resolve repeat ambiguity in favor of loop.
552 Revision 1.4  2001/01/01 19:12:35  steve
553  repeat loops ambiguity.
555 Revision 1.3  2000/12/15 00:21:46  steve
556  rounding of time and x in primitives.
558 Revision 1.2  2000/11/19 22:03:04  steve
559  Integer parameter comments.
561 Revision 1.1  2000/07/23 18:06:31  steve
562  Document ieee1364 issues.