2009-11-24 03:24:55 +01:00
// DESCRIPTION: Verilator: Verilog Test module
//
2026-01-27 02:24:34 +01:00
// This file ONLY is placed under the Creative Commons Public Domain.
// SPDX-FileCopyrightText: 2008 Wilson Snyder
2020-03-21 16:24:24 +01:00
// SPDX-License-Identifier: CC0-1.0
2009-11-24 03:24:55 +01:00
2026-03-15 03:43:56 +01:00
// verilog_format: off
`define stop $stop
`define checks(gotv,expv) do if ((gotv) != (expv)) begin $write("%%Error: %s:%0d: got='%s' exp='%s'\n", `__FILE__,`__LINE__, (gotv), (expv)); `stop; end while(0);
`ifdef verilator
`define no_optimize(v) $c(v)
`else
`define no_optimize(v) (v)
`endif
// verilog_format: on
module t (
input string empty_no_opt
) ;
2009-11-24 03:24:55 +01:00
2026-03-10 02:38:29 +01:00
// Note $sscanf already tested elsewhere
reg [ 3 : 0 ] n ;
reg [ 63 : 0 ] q ;
reg [ 16 * 8 : 1 ] wide ;
reg [ 8 : 1 ] ochar ;
reg [ 48 * 8 : 1 ] str ;
reg [ 48 * 8 : 1 ] str2 ;
string str3 ;
reg [ 39 : 0 ] instruction_str [ 1 : 0 ] ;
real r ;
2026-03-15 03:43:56 +01:00
function string cvt2string ( input int width , input int v ) ;
string fmt ;
$sformat ( fmt , " %0d'h%%%0dh " , width , ( width - 1 ) / 4 + 1 ) ;
$sformat ( cvt2string , { " FMT= " , fmt , empty_no_opt } , v ) ;
$display ( " Format '%s' -> '%s' " , fmt , cvt2string ) ;
endfunction
int mem [ 2 ] = ' { 1 , 2 } ;
string s ;
2026-03-10 02:38:29 +01:00
initial begin
n = 4 'b1100 ;
q = 64 'h1234 _5678_abcd_0123 ;
wide = " hello-there12345 " ;
$sformat ( str , " n=%b q=%d w=%s " , n , q , wide ) ;
2026-03-15 03:43:56 +01:00
`checks ( str , " n=1100 q= 1311768467750060323 w=hello-there12345 " ) ;
2026-03-10 02:38:29 +01:00
q = { q [ 62 : 0 ] , 1 'b1 } ;
$swrite ( str2 , " n=%b q=%d w=%s " , n , q , wide ) ;
2026-03-15 03:43:56 +01:00
`checks ( str2 , " n=1100 q= 2623536935500120647 w=hello-there12345 " ) ;
2026-03-10 02:38:29 +01:00
str3 = $sformatf ( " n=%b q=%d w=%s " , n , q , wide ) ;
2026-03-15 03:43:56 +01:00
`checks ( str3 , " n=1100 q= 2623536935500120647 w=hello-there12345 " ) ;
2026-03-10 02:38:29 +01:00
$swrite ( str2 , " e=%e " , r ) ;
$swrite ( str2 , " e=%f " , r ) ;
$swrite ( str2 , " e=%g " , r ) ;
str3 = " hello " ;
$swrite ( str2 , { str3 , str3 } ) ;
2026-03-15 03:43:56 +01:00
`checks ( str2 , " hellohello " ) ;
2026-03-10 02:38:29 +01:00
r = 0.01 ;
$swrite ( str2 , " e=%e f=%f g=%g " , r , r , r ) ;
2026-03-15 03:43:56 +01:00
`checks ( str2 , " e=1.000000e-02 f=0.010000 g=0.01 " ) ;
2026-03-10 02:38:29 +01:00
$swrite ( str2 , " mod=%m " ) ;
2026-03-15 03:43:56 +01:00
`checks ( str2 , " mod=top.t " ) ;
2026-03-10 02:38:29 +01:00
$swrite ( str2 , " lib=%l " ) ;
2026-03-15 03:43:56 +01:00
`checks ( str2 , " lib=work.t " ) ;
2026-03-10 02:38:29 +01:00
str3 = $sformatf ( " u=%u " , { " a " , " b " , " c " , " d " } ) ; // Value selected so is printable
2026-03-15 03:43:56 +01:00
`checks ( str3 , " u=dcba " ) ;
2026-03-10 02:38:29 +01:00
str3 = $sformatf ( " v=%v " , 4 'b01 xz ) ; // Value selected so is printable
`ifdef TEST_VERBOSE
$display ( " chkv %s " , str3 ) ;
`endif
str3 = $sformatf ( " z=%z " , { " a " , " b " , " c " , " d " } ) ; // Value selected so is printable
`ifdef TEST_VERBOSE
$display ( " chkz %s " , str3 ) ;
`endif
$sformat ( ochar , " %s " , " c " ) ;
2026-03-15 03:43:56 +01:00
`checks ( ochar , " c " ) ;
2026-03-10 02:38:29 +01:00
$swrite ( str2 , 4 'd12 ) ;
2026-03-15 03:43:56 +01:00
`checks ( str2 , " 12 " ) ;
2026-03-10 02:38:29 +01:00
$swriteb ( str2 , 4 'd12 ) ;
2026-03-15 03:43:56 +01:00
`checks ( str2 , " 1100 " ) ;
2026-03-10 02:38:29 +01:00
$swriteh ( str2 , 4 'd12 ) ;
2026-03-15 03:43:56 +01:00
`checks ( str2 , " c " ) ;
2026-03-10 02:38:29 +01:00
$swriteo ( str2 , 4 'd12 ) ;
2026-03-15 03:43:56 +01:00
`checks ( str2 , " 14 " ) ;
2026-03-10 02:38:29 +01:00
str3 = " foo " ;
$sformat ( str3 , " %s " , str3 ) ; // $sformat twice so verilator does not
$sformat ( str3 , " %s " , str3 ) ; // optimize the call to $sformat(str3, "%s", "foo")
2026-03-15 03:43:56 +01:00
`checks ( str3 , " foo " ) ;
2026-03-10 02:38:29 +01:00
$sformat ( instruction_str [ 0 ] , " %s " , " Hello " ) ;
$sformat ( instruction_str [ 1 ] , " %s " , " World " ) ;
2026-03-15 03:43:56 +01:00
`checks ( instruction_str [ 0 ] , " Hello " ) ;
`checks ( instruction_str [ 1 ] , " World " ) ;
s = cvt2string ( `no_optimize ( 2 ) , `no_optimize ( 2 * 16 ) ) ;
`checks ( s , " FMT=2'h20 " ) ;
s = cvt2string ( `no_optimize ( 14 ) , `no_optimize ( 14 * 16 ) ) ;
`checks ( s , " FMT=14'h00e0 " ) ;
s = " hello " ;
s = $sformatf ( { " %x " , " -const " } , s ) ;
`checks ( s , " 68656c6c6f-const " ) ;
s = " hello " ;
s = $sformatf ( { " %x " , empty_no_opt } , s ) ;
`checks ( s , " 68656c6c6f " ) ; // Simulators vary in output; hex required by V3Randomize
// Also is more consistent with when have $sformat("%x", "string-as-verilog-number")
s = $sformatf ( " %s " , " constified " ) ;
s = $sformatf ( " %s " , s ) ;
s = $sformatf ( " %s " , s ) ;
`checks ( s , " constified " ) ;
$display ( mem ) ; // Implied %p
2026-03-10 02:38:29 +01:00
$write ( " *-* All Finished *-* \n " ) ;
$finish ;
end
2009-11-24 03:24:55 +01:00
endmodule