mirror of
https://github.com/verilator/verilator.git
synced 2025-01-10 08:37:35 +00:00
1e938d0e90
Fix preprocessor preservation of newlines across macro substitutions. Fix preprocessor stringification of nested macros. Fix preprocessor whitespace on define arguments
412 lines
11 KiB
Verilog
412 lines
11 KiB
Verilog
// DESCRIPTION: Verilator: Verilog Test module
|
|
// This file ONLY is placed into the Public Domain, for any use,
|
|
// without warranty, 2000-2010 by Wilson Snyder.
|
|
|
|
//===========================================================================
|
|
// Includes
|
|
`include "t_preproc_inc2.vh"
|
|
|
|
//===========================================================================
|
|
// Comments
|
|
|
|
/* verilator pass_thru comment */
|
|
|
|
// verilator pass_thru_comment2
|
|
|
|
//===========================================================================
|
|
// Defines
|
|
|
|
`define DEF_A3
|
|
`define DEF_A1
|
|
// DEF_A0 set by command line
|
|
wire [3:0] q = {
|
|
`ifdef DEF_A3 1'b1 `else 1'b0 `endif ,
|
|
`ifdef DEF_A2 1'b1 `else 1'b0 `endif ,
|
|
`ifdef DEF_A1 1'b1 `else 1'b0 `endif ,
|
|
`ifdef DEF_A0 1'b1 `else 1'b0 `endif
|
|
};
|
|
|
|
text.
|
|
|
|
`define FOOBAR foo /*this */ bar /* this too */
|
|
`define FOOBAR2 foobar2 // but not
|
|
`FOOBAR
|
|
`FOOBAR2
|
|
|
|
`define MULTILINE first part \
|
|
second part \
|
|
third part
|
|
|
|
`define MOREMULTILINE {\
|
|
a,\
|
|
b,\
|
|
c}
|
|
|
|
/*******COMMENT*****/
|
|
`MULTILINE
|
|
`MOREMULTILINE
|
|
Line_Preproc_Check `__LINE__
|
|
|
|
//===========================================================================
|
|
|
|
`define syn_negedge_reset_l or negedge reset_l
|
|
|
|
`define DEEP deep
|
|
`define DEEPER `DEEP `DEEP
|
|
`DEEPER
|
|
|
|
`define nosubst NOT_SUBSTITUTED
|
|
`define WITHTICK "`nosubst"
|
|
"Inside: `nosubst"
|
|
`WITHTICK
|
|
|
|
`define withparam(a, b) a b LLZZ a b
|
|
`withparam(x,y)
|
|
`withparam(`withparam(p,q),`withparam ( r , s ))
|
|
|
|
`withparam(firstline
|
|
,
|
|
comma","line)
|
|
|
|
`define withquote(a, bar) a bar LLZZ "a" bar
|
|
`withquote( x , y)
|
|
|
|
`define noparam (a,b)
|
|
`noparam(a,b)
|
|
|
|
`define msg(x,y) `"x: `\`"y`\`"`"
|
|
$display(`msg(left side, right side))
|
|
|
|
`define foo(f) f``_suffix
|
|
`foo(bar) more
|
|
|
|
`define zap(which) \
|
|
$c("Zap(\"",which,"\");");
|
|
`zap(bug1);
|
|
`zap("bug2");
|
|
|
|
/* Define inside comment: `DEEPER and `WITHTICK */
|
|
// More commentary: `zap(bug1); `zap("bug2");
|
|
|
|
//======================================================================
|
|
// display passthru
|
|
|
|
`define ls left_side
|
|
`define rs right_side
|
|
`define noarg na
|
|
`define thru(x) x
|
|
`define thruthru `ls `rs // Doesn't expand
|
|
`define msg(x,y) `"x: `\`"y`\`"`"
|
|
initial begin
|
|
//$display(`msg( \`, \`)); // Illegal
|
|
$display(`msg(pre `thru(thrupre `thru(thrumid) thrupost) post,right side));
|
|
$display(`msg(left side,right side));
|
|
$display(`msg( left side , right side ));
|
|
$display(`msg( `ls , `rs ));
|
|
$display(`msg( `noarg , `rs ));
|
|
$display(`msg( prep ( midp1 `ls midp2 ( outp ) ) , `rs ));
|
|
$display(`msg(`noarg,`noarg`noarg));
|
|
$display(`msg( `thruthru , `thruthru )); // Results vary between simulators
|
|
$display(`msg(`thru(),)); // Empty
|
|
$display(`msg(`thru(left side),`thru(right side)));
|
|
$display(`msg( `thru( left side ) , `thru( right side ) ));
|
|
$display(`"standalone`");
|
|
|
|
// Unspecified when the stringification has multiple lines
|
|
`define twoline first \
|
|
second
|
|
$display(`msg(twoline, `twoline));
|
|
//$display(`msg(left side, \ right side \ )); // Not sure \{space} is legal.
|
|
$write("*-* All Finished *-*\n");
|
|
$finish;
|
|
end
|
|
endmodule
|
|
|
|
//======================================================================
|
|
// rt.cpan.org bug34429
|
|
|
|
`define ADD_UP(a,c) \
|
|
wire tmp_``a = a; \
|
|
wire tmp_``c = tmp_``a + 1; \
|
|
assign c = tmp_``c ;
|
|
|
|
module add1 ( input wire d1, output wire o1);
|
|
`ADD_UP(d1,o1) // expansion is OK
|
|
endmodule
|
|
module add2 ( input wire d2, output wire o2);
|
|
`ADD_UP( d2 , o2 ) // expansion is bad
|
|
endmodule
|
|
|
|
`define check(mod, width, flopname, gate, path) \
|
|
generate for (i=0; i<(width); i=i+1) begin \
|
|
psl cover { path.d[i] & ~path.q[i] & !path.cond & (gate)} report `"fondNoRise: mod.flopname`"; \
|
|
psl cover { ~path.d[i] & path.q[i] & !path.cond & (gate)} report `"fondNoFall: mod.flopname`"; \
|
|
end endgenerate
|
|
|
|
// parameterized macro with arguments that are macros
|
|
`define MK m5k.f
|
|
`define MF `MK .ctl
|
|
`define CK_fr (`MF.alive & `MF.alive_m1)
|
|
|
|
`check(m5kc_fcl, 3, _ctl_mvldx_m1, `CK_fr, `MF._ctl_mvldx_m1) // ignorecmt
|
|
|
|
//======================================================================
|
|
// Quotes are legal in protected blocks. Grr.
|
|
module prot();
|
|
`protected
|
|
I!#r#e6<_Q{{E2+]I3<[3s)1@D|'E''i!O?]jD>Jo_![Cl)
|
|
#nj1]p,3^1~,="E@QZB\T)eU\pC#C|7=\$J$##A[@-@{Qk]
|
|
`endprotected
|
|
endmodule
|
|
//"
|
|
|
|
//======================================================================
|
|
// macro call with define that has comma
|
|
`define REG_H 6
|
|
`define REG_L 7
|
|
`define _H regs[`REG_H]
|
|
`define _L regs[`REG_L]
|
|
`define _HL {`_H, `_L}
|
|
`define EX_WRITE(ad, da) begin addr <= (ad); wdata <= (da); wr <= 1; end
|
|
`define EX_READ(ad) begin addr <= (ad); rd <= 1; end
|
|
|
|
`EX_READ((`_HL + 1)) and `EX_WRITE((`_HL), rdata)
|
|
`EX_READ(`_HL + 1)
|
|
`EX_WRITE(`_HL, rdata) more
|
|
|
|
//======================================================================
|
|
// include of parameterized file
|
|
`define INCNAME "t_preproc_inc4.vh"
|
|
`include `INCNAME
|
|
`ifndef T_PREPROC_INC4
|
|
`error "No Inc4"
|
|
`endif
|
|
`undef T_PREPROC_INC4
|
|
|
|
`ifdef NOT_DEFINED_INC
|
|
`include NOT_DEFINED_INC
|
|
`endif
|
|
|
|
//======================================================================
|
|
// macro call with , in {}
|
|
|
|
`define xxerror(logfile, msg) $blah(logfile,msg)
|
|
`xxerror("ab,cd","e,f");
|
|
`xxerror(this.logfile, vec);
|
|
`xxerror(this.logfile, vec[1,2,3]);
|
|
`xxerror(this.logfile, {blah.name(), " is not foo"});
|
|
|
|
//======================================================================
|
|
// pragma/default net type
|
|
|
|
`pragma foo = 1
|
|
`default_nettype none
|
|
`default_nettype uwire
|
|
|
|
//======================================================================
|
|
// Ifdef
|
|
|
|
`define EMPTY_TRUE
|
|
`ifndef EMPTY_TRUE
|
|
`error "Empty is still true"
|
|
`endif
|
|
Line_Preproc_Check `__LINE__
|
|
|
|
//======================================================================
|
|
// bug84
|
|
|
|
`define ARGPAR(a, // Hello, comments MIGHT not be legal
|
|
/*more,,)cmts*/ b // But newlines ARE legal... who speced THAT?
|
|
) (a,b)
|
|
`ARGPAR(p,q)
|
|
`ARGPAR( //Here
|
|
x,
|
|
y //Too
|
|
)
|
|
Line_Preproc_Check `__LINE__
|
|
|
|
//======================================================================
|
|
// defines split arguments
|
|
|
|
`define BEGIN begin
|
|
`define END end
|
|
`define BEGINEND `BEGIN`END
|
|
`define quoteit(x) `"x`"
|
|
`BEGIN`END // 2001 spec doesn't require two tokens, so "beginend" ok
|
|
`BEGINEND // 2001 spec doesn't require two tokens, so "beginend" ok
|
|
`quoteit(`BEGIN`END) // No space "beginend"
|
|
|
|
//======================================================================
|
|
// bug106
|
|
`define \esc`def got_escaped
|
|
`ifdef \esc`def
|
|
`\esc`def
|
|
`endif
|
|
Not a \`define
|
|
|
|
//======================================================================
|
|
// misparsed comma in submacro
|
|
`define sb bee
|
|
`define appease_emacs_paren_matcher (
|
|
`define sa(l) x,y)
|
|
`define sfoo(q,r) q--r
|
|
`sfoo(`sa(el),`sb) submacro has comma paren
|
|
|
|
//======================================================================
|
|
// bug191
|
|
`define bug191(bits) $display("bits %d %d", $bits(foo), `bits);
|
|
`bug191(10)
|
|
|
|
//======================================================================
|
|
// 1800-2009
|
|
`define UDALL
|
|
`ifndef PREDEF_COMMAND_LINE `error "Test setup error, PREDEF_COMMAND_LINE pre-missing" `endif
|
|
`undefineall
|
|
`ifdef UDALL `error "undefineall failed" `endif
|
|
`ifndef PREDEF_COMMAND_LINE `error "Deleted too much, no PREDEF_COMMAND_LINE" `endif
|
|
|
|
//======================================================================
|
|
// bug202
|
|
`define FC_INV3(out, in) \
|
|
`ifdef DC \
|
|
cell \inv_``out <$typeof(out)> (.a(<in>), .o(<out>)); \
|
|
/* multi-line comment \
|
|
multi-line comment */ \
|
|
`else \
|
|
`ifdef MACRO_ATTRIBUTE \
|
|
(* macro_attribute = `"INV (out``,in``)`" *) \
|
|
`endif \
|
|
assign out = ~in ; \
|
|
`endif
|
|
|
|
`FC_INV3(a3,b3)
|
|
|
|
`define /* multi \
|
|
line1*/ \
|
|
bug202( i /*multi \
|
|
line2*/ \
|
|
) \
|
|
/* multi \
|
|
line 3*/ \
|
|
def i \
|
|
|
|
`bug202(foo)
|
|
|
|
//======================================================================
|
|
|
|
`define CMT1 // verilator NOT IN DEFINE
|
|
`define CMT2 /* verilator PART OF DEFINE */
|
|
`define CMT3 /* verilator NOT PART
|
|
OF DEFINE */
|
|
`define CMT4 /* verilator PART \
|
|
OF DEFINE */
|
|
`define CMT5 // CMT NOT \
|
|
also in // BUT TEXT IS \
|
|
also3 // CMT NOT
|
|
|
|
1 `CMT1 (nodef)
|
|
2 `CMT2 (hasdef)
|
|
3 `CMT3 (nodef)
|
|
4 `CMT4 (nodef)
|
|
5 `CMT5 (nodef)
|
|
`define NL HAS a NEW \
|
|
LINE
|
|
`NL
|
|
|
|
//======================================================================
|
|
|
|
`define msg_fatal(log, msg) \
|
|
do \
|
|
/* synopsys translate_off */ \
|
|
`ifdef NEVER \
|
|
`error "WTF" \
|
|
`else \
|
|
if (start(`__FILE__, `__LINE__)) begin \
|
|
`endif \
|
|
message(msg); \
|
|
end \
|
|
/* synopsys translate_on */ \
|
|
while(0)
|
|
|
|
`define msg_scen_(cl) cl``_scen
|
|
`define MSG_MACRO_TO_STRING(x) `"x`"
|
|
|
|
EXP: clxx_scen
|
|
`msg_scen_(clxx)
|
|
EXP: clxx_scen
|
|
`MSG_MACRO_TO_STRING(`msg_scen_(clxx))
|
|
`define mf(clx) `msg_fatal(this.log, {"Blah-", `MSG_MACRO_TO_STRING(`msg_scen_(clx)), " end"});
|
|
EXP: do if (start("verilog/inc1.v", 25)) begin message({"Blah-", "clx_scen", " end"}); end while(0);
|
|
`mf(clx)
|
|
|
|
//======================================================================
|
|
|
|
`define makedefine(name) \
|
|
`define def_``name This is name \
|
|
`define def_``name``_2 This is name``_2 \
|
|
|
|
`makedefine(fooed)
|
|
`ifndef def_fooed `error "No def_fooed" `endif
|
|
//`ifndef def_fooed_2 `error "No def_fooed_2" `endif
|
|
EXP: This is fooed
|
|
`def_fooed
|
|
EXP: This is fooed_2
|
|
`def_fooed_2
|
|
|
|
//======================================================================
|
|
`define NOPARAM() np
|
|
`NOPARAM()
|
|
`NOPARAM( )
|
|
//======================================================================
|
|
// It's unclear if the spec allows this; is text_macro_idenitfier before or after substitution?
|
|
`define NODS_DEFINED
|
|
`define NODS_INDIRECT(x) x
|
|
`ifndef `NODS_INDIRECT(NODS_DEFINED)
|
|
`error "Indirect failed"
|
|
`endif
|
|
`ifdef `NODS_INDIRECT(NODS_UNDEFINED)
|
|
`error "Indirect2 failed"
|
|
`endif
|
|
//======================================================================
|
|
// Metaprogramming
|
|
`define REPEAT_0(d)
|
|
`define REPEAT_1(d) d
|
|
`define REPEAT_2(d) `REPEAT_1(d)d
|
|
`define REPEAT_3(d) `REPEAT_2(d)d
|
|
`define REPEAT_4(d) `REPEAT_3(d)d
|
|
|
|
`define CONCAT(a, b) a``b
|
|
`define REPEATC(n, d) `CONCAT(`REPEAT_, n)(d)
|
|
`define REPEATT(n, d) `REPEAT_``n(d)
|
|
|
|
`REPEATC(3, hello3 )
|
|
`REPEATT(4, hello4 )
|
|
//======================================================================
|
|
// Include from stringification
|
|
`undef T_PREPROC_INC4
|
|
`define NODS_CONC_VH(m) `"m.vh`"
|
|
`include `NODS_CONC_VH(t_preproc_inc4)
|
|
`ifndef T_PREPROC_INC4 `error_here `endif
|
|
//======================================================================
|
|
// Defines doing defines
|
|
// Note the newline on the end - required to form the end of a define
|
|
`define DEFINEIT(d) d \
|
|
|
|
`define _DEFIF_Z_0 1
|
|
`define DEFIF_NZ(d,n) `undef d `ifndef _DEFIF_Z_``n `DEFINEIT(`define d 1) `endif
|
|
`DEFIF_NZ(TEMP,1)
|
|
`ifndef TEMP `error "bad" `endif
|
|
`DEFIF_NZ(TEMP,0)
|
|
`ifdef TEMP `error "bad0" `endif
|
|
Line_Preproc_Check `__LINE__
|
|
//======================================================================
|
|
// Quoted multiline - track line numbers, and insure \\n gets propagated
|
|
`define MULQUOTE "FOO \
|
|
BAR "
|
|
`define MULQUOTE2(mq) `MULQUOTE mq `MULQUOTE
|
|
Line_Preproc_Check `__LINE__
|
|
`MULQUOTE2("arg_line1 \
|
|
arg_line2")
|
|
Line_Preproc_Check `__LINE__
|
|
//======================================================================
|