2006-08-26 11:35:28 +00:00
// DESCRIPTION: Verilator: Verilog Test module
//
2020-03-21 15:24:24 +00:00
// This file ONLY is placed under the Creative Commons Public Domain, for
// any use, without warranty, 2003 by Wilson Snyder.
// SPDX-License-Identifier: CC0-1.0
2006-08-26 11:35:28 +00:00
`include " verilated.v "
2018-05-08 00:42:28 +00:00
`define STRINGIFY(x) `"x`"
2019-07-06 00:09:56 +00:00
`define ratio_error(a,b) (((a)>(b) ? ((a)-(b)) : ((b)-(a))) / (a))
`define checkr(gotv,expv) do if (`ratio_error((gotv),(expv))>0.0001) begin $write("%%Error: %s:%0d: got=%g exp=%g\n", `__FILE__,`__LINE__, (gotv), (expv)); $stop; end while(0);
2020-04-05 15:22:05 +00:00
`define checkh(gotv,expv) do if ((gotv) !== (expv)) begin $write("%%Error: %s:%0d: got='h%x exp='h%x\n", `__FILE__,`__LINE__, (gotv), (expv)); $stop; end while(0);
`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);
2018-05-08 00:42:28 +00:00
2006-08-26 11:35:28 +00:00
module t ;
2019-03-07 23:29:44 +00:00
integer file ;
2006-08-26 11:35:28 +00:00
2008-06-28 00:04:20 +00:00
integer chars ;
reg [ 1 * 8 : 1 ] letterl ;
reg [ 8 * 8 : 1 ] letterq ;
reg [ 16 * 8 : 1 ] letterw ;
2008-07-01 18:15:10 +00:00
reg [ 16 * 8 : 1 ] letterz ;
2011-11-20 06:01:02 +00:00
real r ;
2014-12-25 02:50:38 +00:00
string s ;
2020-03-06 02:49:25 +00:00
integer i ;
2008-07-01 18:15:10 +00:00
2013-09-07 20:43:43 +00:00
reg [ 7 : 0 ] v_a , v_b , v_c , v_d ;
reg [ 31 : 0 ] v_worda ;
reg [ 31 : 0 ] v_wordb ;
2019-09-04 01:28:15 +00:00
integer v_length , v_off ;
2008-07-01 18:15:10 +00:00
`ifdef TEST_VERBOSE
`define verbose 1'b1
`else
`define verbose 1'b0
`endif
2008-06-28 00:04:20 +00:00
2006-08-26 11:35:28 +00:00
initial begin
// Display formatting
`ifdef verilator
if ( file ! = 0 ) $stop ;
$fwrite ( file , " Never printed, file closed \n " ) ;
2008-06-26 12:52:02 +00:00
if ( ! $feof ( file ) ) $stop ;
2006-08-26 11:35:28 +00:00
`endif
2008-07-16 18:06:08 +00:00
`ifdef AUTOFLUSH
// The "w" is required so we get a FD not a MFD
2018-05-08 00:42:28 +00:00
file = $fopen ( { `STRINGIFY ( `TEST_OBJ_DIR ) , " /t_sys_file_autoflush.log " } , " w " ) ;
2008-07-16 18:06:08 +00:00
`else
// The "w" is required so we get a FD not a MFD
2018-05-08 00:42:28 +00:00
file = $fopen ( { `STRINGIFY ( `TEST_OBJ_DIR ) , " /t_sys_file_basic_test.log " } , " w " ) ;
2008-07-16 18:06:08 +00:00
`endif
2008-06-26 12:52:02 +00:00
if ( $feof ( file ) ) $stop ;
2006-08-26 11:35:28 +00:00
$fdisplay ( file , " [%0t] hello v=%x " , $time , 32 'h12345667 ) ;
$fwrite ( file , " [%0t] %s \n " , $time , " Hello2 " ) ;
2020-03-06 02:49:25 +00:00
i = 12 ;
$fwrite ( file , " d: " ) ; $fwrite ( file , i ) ; $fwrite ( file , " " ) ; $fdisplay ( file , i ) ;
$fwriteh ( file , " h: " ) ; $fwriteh ( file , i ) ; $fwriteh ( file , " " ) ; $fdisplayh ( file , i ) ;
$fwriteo ( file , " o: " ) ; $fwriteo ( file , i ) ; $fwriteo ( file , " " ) ; $fdisplayo ( file , i ) ;
$fwriteb ( file , " b: " ) ; $fwriteb ( file , i ) ; $fwriteb ( file , " " ) ; $fdisplayb ( file , i ) ;
2008-06-27 12:45:05 +00:00
$fflush ( file ) ;
2020-04-05 14:11:28 +00:00
$fflush ( ) ;
$fflush ;
2006-08-26 11:35:28 +00:00
$fclose ( file ) ;
`ifdef verilator
2008-04-24 14:32:39 +00:00
if ( file ! = 0 ) $stop ( 1 ) ; // Also test arguments to stop
2006-08-26 11:35:28 +00:00
$fwrite ( file , " Never printed, file closed \n " ) ;
`endif
2006-12-19 14:09:57 +00:00
begin
2008-06-28 00:04:20 +00:00
// Check for opening errors
2008-11-25 02:38:45 +00:00
// The "r" is required so we get a FD not a MFD
2018-05-08 00:42:28 +00:00
file = $fopen ( " DOES_NOT_EXIST " , " r " ) ;
2006-12-19 14:09:57 +00:00
if ( | file ) $stop ; // Should not exist, IE must return 0
2020-04-05 15:22:05 +00:00
// Check error function
s = " " ;
i = $ferror ( file , s ) ;
`checkh ( i , 2 ) ;
`checks ( s , " No such file or directory " ) ;
2006-12-19 14:09:57 +00:00
end
2006-12-21 15:28:32 +00:00
begin
// Check quadword access; a little strange, but it's legal to open "."
file = $fopen ( " . " , " r " ) ;
$fclose ( file ) ;
end
2015-07-21 22:36:27 +00:00
begin
// Check read functions w/string
s = " t/t_sys_file_basic_input.dat " ;
file = $fopen ( s , " r " ) ;
if ( $feof ( file ) ) $stop ;
$fclose ( file ) ;
end
2008-06-28 00:04:20 +00:00
begin
// Check read functions
2008-07-14 21:15:26 +00:00
file = $fopen ( " t/t_sys_file_basic_input.dat " , " r " ) ;
2008-06-28 00:04:20 +00:00
if ( $feof ( file ) ) $stop ;
// $fgetc
if ( $fgetc ( file ) ! = " h " ) $stop ;
if ( $fgetc ( file ) ! = " i " ) $stop ;
if ( $fgetc ( file ) ! = " \n " ) $stop ;
2008-07-01 18:15:10 +00:00
2019-11-16 17:55:10 +00:00
// $ungetc
if ( $ungetc ( " x " , file ) ! = 0 ) $stop ;
if ( $fgetc ( file ) ! = " x " ) $stop ;
2008-06-28 00:04:20 +00:00
// $fgets
chars = $fgets ( letterl , file ) ;
2008-07-01 18:15:10 +00:00
if ( `verbose ) $write ( " c=%0d l=%s \n " , chars , letterl ) ;
2008-06-28 00:04:20 +00:00
if ( chars ! = 1 ) $stop ;
if ( letterl ! = " l " ) $stop ;
chars = $fgets ( letterq , file ) ;
2008-07-01 18:15:10 +00:00
if ( `verbose ) $write ( " c=%0d q=%x=%s " , chars , letterq , letterq ) ; // Output includes newline
2008-06-28 00:04:20 +00:00
if ( chars ! = 5 ) $stop ;
if ( letterq ! = " \0 \0 \0 quad \n " ) $stop ;
letterw = " 5432109876543210 " ;
chars = $fgets ( letterw , file ) ;
2008-07-01 18:15:10 +00:00
if ( `verbose ) $write ( " c=%0d w=%s " , chars , letterw ) ; // Output includes newline
2008-06-28 00:04:20 +00:00
if ( chars ! = 10 ) $stop ;
if ( letterw ! = " \0 \0 \0 \0 \0 \0 widestuff \n " ) $stop ;
2008-07-01 18:15:10 +00:00
// $sscanf
if ( $sscanf ( " x " , " " ) ! = 0 ) $stop ;
if ( $sscanf ( " z " , " z " ) ! = 0 ) $stop ;
chars = $sscanf ( " blabcdefghijklmnop " ,
" %s " , letterq ) ;
if ( `verbose ) $write ( " c=%0d sa=%s \n " , chars , letterq ) ;
if ( chars ! = 1 ) $stop ;
if ( letterq ! = " ijklmnop " ) $stop ;
2020-05-12 02:13:59 +00:00
chars = $sscanf ( " xa=1f ign=22 xb=12898971238912389712783490823_abcdef689_02348923 " ,
" xa=%x ign=%*d xb=%x " , letterq , letterw ) ;
2008-07-01 18:15:10 +00:00
if ( `verbose ) $write ( " c=%0d xa=%x xb=%x \n " , chars , letterq , letterw ) ;
if ( chars ! = 2 ) $stop ;
if ( letterq ! = 64 'h1f ) $stop ;
2019-07-06 00:09:56 +00:00
if ( letterw ! = 128 'h389712783490823 _abcdef689_02348923 ) $stop ;
2008-07-01 18:15:10 +00:00
chars = $sscanf ( " ba=10 bb=110100101010010101012 note_the_two " ,
" ba=%b bb=%b%s " , letterq , letterw , letterz ) ;
if ( `verbose ) $write ( " c=%0d xa=%x xb=%x z=%0s \n " , chars , letterq , letterw , letterz ) ;
if ( chars ! = 3 ) $stop ;
if ( letterq ! = 64 'h2 ) $stop ;
if ( letterw ! = 128 'hd2a55 ) $stop ;
if ( letterz ! = { " \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 " , " 2 " } ) $stop ;
2020-05-12 02:13:59 +00:00
chars = $sscanf ( " oa=23 oi=11 ob=125634123615234123681236 " ,
" oa=%o oi=%*o ob=%o " , letterq , letterw ) ;
2008-07-01 18:15:10 +00:00
if ( `verbose ) $write ( " c=%0d oa=%x ob=%x \n " , chars , letterq , letterw ) ;
if ( chars ! = 2 ) $stop ;
if ( letterq ! = 64 'h13 ) $stop ;
if ( letterw ! = 128 'h55ce14f1a9c29e ) $stop ;
2011-11-20 06:01:02 +00:00
chars = $sscanf ( " r=0.1 d=-236123 " ,
" r=%g d=%d " , r , letterq ) ;
2008-07-01 18:15:10 +00:00
if ( `verbose ) $write ( " c=%0d d=%d \n " , chars , letterq ) ;
2011-11-20 06:01:02 +00:00
if ( chars ! = 2 ) $stop ;
2019-07-06 00:09:56 +00:00
`checkr ( r , 0.1 ) ;
2008-07-01 18:15:10 +00:00
if ( letterq ! = 64 'hfffffffffffc65a5 ) $stop ;
2019-07-06 00:09:56 +00:00
// Cover quad and %e/%f
chars = $sscanf ( " r=0.2 " ,
" r=%e " , r ) ;
if ( `verbose ) $write ( " c=%0d r=%e \n " , chars , r ) ;
`checkr ( r , 0.2 ) ;
chars = $sscanf ( " r=0.3 " ,
" r=%f " , r ) ;
if ( `verbose ) $write ( " c=%0d r=%f \n " , chars , r ) ;
`checkr ( r , 0.3 ) ;
2014-12-25 02:50:38 +00:00
s = " r=0.2 d=-236124 " ;
chars = $sscanf ( s , " r=%g d=%d " , r , letterq ) ;
if ( `verbose ) $write ( " c=%0d d=%d \n " , chars , letterq ) ;
if ( chars ! = 2 ) $stop ;
2019-07-06 00:09:56 +00:00
`checkr ( r , 0.2 ) ;
2014-12-25 02:50:38 +00:00
if ( letterq ! = 64 'hfffffffffffc65a4 ) $stop ;
2008-07-01 18:15:10 +00:00
// $fscanf
if ( $fscanf ( file , " " ) ! = 0 ) $stop ;
if ( ! sync ( " * " ) ) $stop ;
chars = $fscanf ( file , " xa=%x xb=%x " , letterq , letterw ) ;
if ( `verbose ) $write ( " c=%0d xa=%0x xb=%0x \n " , chars , letterq , letterw ) ;
if ( chars ! = 2 ) $stop ;
if ( letterq ! = 64 'h1f ) $stop ;
if ( letterw ! = 128 'h23790468902348923 ) $stop ;
if ( ! sync ( " \n " ) ) $stop ;
if ( ! sync ( " * " ) ) $stop ;
chars = $fscanf ( file , " ba=%b bb=%b %s " , letterq , letterw , letterz ) ;
if ( `verbose ) $write ( " c=%0d ba=%0x bb=%0x z=%0s \n " , chars , letterq , letterw , letterz ) ;
if ( chars ! = 3 ) $stop ;
if ( letterq ! = 64 'h2 ) $stop ;
if ( letterw ! = 128 'hd2a55 ) $stop ;
if ( letterz ! = " \0 \0 \0 \0 note_the_two " ) $stop ;
if ( ! sync ( " \n " ) ) $stop ;
if ( ! sync ( " * " ) ) $stop ;
chars = $fscanf ( file , " oa=%o ob=%o " , letterq , letterw ) ;
if ( `verbose ) $write ( " c=%0d oa=%0x ob=%0x \n " , chars , letterq , letterw ) ;
if ( chars ! = 2 ) $stop ;
if ( letterq ! = 64 'h13 ) $stop ;
if ( letterw ! = 128 'h1573 ) $stop ;
if ( ! sync ( " \n " ) ) $stop ;
if ( ! sync ( " * " ) ) $stop ;
chars = $fscanf ( file , " d=%d " , letterq ) ;
if ( `verbose ) $write ( " c=%0d d=%0x \n " , chars , letterq ) ;
if ( chars ! = 1 ) $stop ;
if ( letterq ! = 64 'hfffffffffffc65a5 ) $stop ;
if ( ! sync ( " \n " ) ) $stop ;
if ( ! sync ( " * " ) ) $stop ;
chars = $fscanf ( file , " %c%s " , letterl , letterw ) ;
if ( `verbose ) $write ( " c=%0d q=%c s=%s \n " , chars , letterl , letterw ) ;
if ( chars ! = 2 ) $stop ;
if ( letterl ! = " f " ) $stop ;
if ( letterw ! = " \0 \0 \0 \0 \0 redfishblah " ) $stop ;
chars = $fscanf ( file , " %c " , letterl ) ;
if ( `verbose ) $write ( " c=%0d l=%x \n " , chars , letterl ) ;
if ( chars ! = 1 ) $stop ;
if ( letterl ! = " \n " ) $stop ;
2013-09-07 20:43:43 +00:00
// msg1229
v_a = $fgetc ( file ) ;
v_b = $fgetc ( file ) ;
v_c = $fgetc ( file ) ;
v_d = $fgetc ( file ) ;
v_worda = { v_d , v_c , v_b , v_a } ;
if ( v_worda ! = " 4321 " ) $stop ;
v_wordb [ 7 : 0 ] = $fgetc ( file ) ;
v_wordb [ 15 : 8 ] = $fgetc ( file ) ;
v_wordb [ 23 : 16 ] = $fgetc ( file ) ;
v_wordb [ 31 : 24 ] = $fgetc ( file ) ;
if ( v_wordb ! = " 9876 " ) $stop ;
if ( $fgetc ( file ) ! = " \n " ) $stop ;
2019-09-04 01:28:15 +00:00
v_length = $ftell ( file ) ;
$frewind ( file ) ;
v_off = $ftell ( file ) ;
if ( v_off ! = 0 ) $stop ;
$fseek ( file , 10 , 0 ) ;
v_off = $ftell ( file ) ;
if ( v_off ! = 10 ) $stop ;
$fseek ( file , 1 , 1 ) ;
v_off = $ftell ( file ) ;
if ( v_off ! = 11 ) $stop ;
$fseek ( file , - 1 , 1 ) ;
v_off = $ftell ( file ) ;
if ( v_off ! = 10 ) $stop ;
$fseek ( file , v_length , 0 ) ;
v_off = $ftell ( file ) ;
if ( v_off ! = v_length ) $stop ;
if ( $fseek ( file , 0 , 2 ) ! = 0 ) $stop ;
v_off = $ftell ( file ) ;
if ( v_off < v_length ) $stop ;
2019-11-16 17:21:35 +00:00
if ( $rewind ( file ) ! = 0 ) $stop ;
v_off = $ftell ( file ) ;
if ( v_off ! = 0 ) $stop ;
2019-09-04 01:28:15 +00:00
2008-06-28 00:04:20 +00:00
$fclose ( file ) ;
end
2006-08-26 11:35:28 +00:00
$write ( " *-* All Finished *-* \n " ) ;
2008-04-24 14:32:39 +00:00
$finish ( 0 ) ; // Test arguments to finish
2006-08-26 11:35:28 +00:00
end
2008-07-01 18:15:10 +00:00
function sync ;
input [ 7 : 0 ] cexp ;
reg [ 7 : 0 ] cgot ;
begin
cgot = $fgetc ( file ) ;
if ( `verbose ) $write ( " sync=%x='%c' \n " , cgot , cgot ) ;
sync = ( cgot = = cexp ) ;
end
endfunction
2006-08-26 11:35:28 +00:00
endmodule