2020-05-14 22:03:00 +00:00
// DESCRIPTION: Verilator: Verilog Test module
//
// This file ONLY is placed under the Creative Commons Public Domain, for
// any use, without warranty, 2020 by Wilson Snyder.
// SPDX-License-Identifier: CC0-1.0
2023-02-05 15:18:03 +00:00
`define checkd(gotv,expv) do if ((gotv) !== (expv)) begin $write("%%Error: %s:%0d: got=%0d exp=%0d\n", `__FILE__,`__LINE__, (gotv), (expv)); $stop; end while(0);
2020-05-14 22:03:00 +00:00
module t ;
`define STR(__s) `"__s`"
2023-02-05 15:18:03 +00:00
task automatic fail ( string s ) ;
2020-05-14 22:03:00 +00:00
$display ( { " FAIL! Reason: " , s } ) ;
$stop ;
2023-02-05 15:18:03 +00:00
endtask
2020-05-14 22:03:00 +00:00
2023-02-05 15:18:03 +00:00
task automatic test1 ;
2020-05-14 22:03:00 +00:00
int fd [ 30 ] , fd_fail , fd_success , fd_close , tmp ;
for ( int i = 0 ; i < 30 ; i + + ) begin
// Attempt to allocate 30 MCD descriptors; returned descriptors
// should fall within correct range: [1, 30].
tmp = $fopen ( $sformatf ( " %s/some_file%0d.dat " , `STR ( `TEST_OBJ_DIR ) , i ) ) ;
fd [ i ] = tmp ;
if ( ( fd [ i ] = = 0 ) | | ! $onehot ( fd [ i ] ) )
fail ( $sformatf ( " MCD descriptor out of range %d " , fd [ i ] ) ) ;
end
// Attempt to allocate another MCD descriptor when all should
// be used. We expect this operation to fail and return the
// invalid descriptor (0).
fd_fail = $fopen ( $sformatf ( " %s/another_file.dat " , `STR ( `TEST_OBJ_DIR ) ) ) ;
if ( fd_fail ! = 0 )
fail ( " Able to allocate MCD descriptor when fully utilized. " ) ;
// Return descriptor back to pool
2021-06-06 23:32:48 +00:00
fd_close = fd [ 0 ] ;
2020-05-14 22:03:00 +00:00
$fclose ( fd_close ) ;
// Re-attempt MCD allocation; should pass at this point.
fd_success = $fopen ( $sformatf ( " %s/yet_another_file.dat " , `STR ( `TEST_OBJ_DIR ) ) ) ;
if ( fd_success = = 0 )
fail ( " Expect to have free descriptors at this point. " ) ;
// Returned descriptor should have a value matching that which
// had previously just been returned back to the pool.
if ( fd_success ! = fd [ 0 ] )
fail ( " Descriptor has incorrect value. " ) ;
// Return all descriptors back to the pool.
for ( int i = 1 ; i < 30 ; i + + ) begin
fd_close = fd [ i ] ;
$fclose ( fd_close ) ;
end
2023-02-05 15:18:03 +00:00
endtask
2020-05-14 22:03:00 +00:00
2023-02-05 15:18:03 +00:00
task automatic test2 ;
2020-05-14 22:03:00 +00:00
// Validate basic MCD functionality.
integer fd [ 3 ] , fd_all , tmp ;
for ( int i = 0 ; i < 3 ; i + + ) begin
tmp = $fopen ( $sformatf ( " %s/t_sys_file_basic_mcd_test2_%0d.dat " , `STR ( `TEST_OBJ_DIR ) , i ) ) ;
fd [ i ] = tmp ;
end
fd_all = 0 ;
for ( int i = 0 ; i < 3 ; i + + )
fd_all | = fd [ i ] ;
$fwrite ( fd_all , " Scotland is the greatest country. \n " ) ;
$fwrite ( fd_all , " All other countries are inferior. \n " ) ;
$fwrite ( fd_all , " Woe betide those to stand against the mighty Scottish nation. \n " ) ;
$fclose ( fd_all ) ;
2023-02-05 15:18:03 +00:00
endtask
2020-05-14 22:03:00 +00:00
2023-02-05 15:18:03 +00:00
task automatic test3 ;
int result ;
2020-05-14 22:03:00 +00:00
// Write some things to standard output.
$fwrite ( 32 'h8000 _0001 , " Sean Connery was the best Bond. \n " ) ;
2023-02-05 15:18:03 +00:00
$fwrite ( 32 'h8000 _0001 ) ;
$fstrobe ( 32 'h8000 _0001 ) ;
2020-05-14 22:03:00 +00:00
2023-02-05 15:18:03 +00:00
result = $fseek ( 32 'hffffffff , 0 , 0 ) ;
`checkd ( result , - 1 ) ;
result = $ftell ( 32 'hffffffff ) ;
`checkd ( result , - 1 ) ;
result = $rewind ( 32 'hffffffff ) ;
`checkd ( result , - 1 ) ;
result = $feof ( 0 ) ;
`checkd ( result , 1 ) ;
endtask
task automatic test4 ;
2020-05-14 22:03:00 +00:00
int fd ;
2020-05-14 22:50:49 +00:00
// Wide filename
fd = $fopen ( { `STR ( `TEST_OBJ_DIR ) ,
2021-06-06 23:32:48 +00:00
" /some_very_large_filename_that_no_one_would_ever_use_ " ,
2020-05-14 22:50:49 +00:00
" except_to_purposefully_break_my_beautiful_code.dat " } ) ;
2020-05-14 22:03:00 +00:00
if ( fd = = 0 ) fail ( " Long filename could not be opened. " ) ;
$fclose ( fd ) ;
2023-02-05 15:18:03 +00:00
endtask
2020-05-14 22:03:00 +00:00
2023-02-05 15:18:03 +00:00
task automatic test5 ;
2021-06-06 23:32:48 +00:00
int fd_all ;
fd_all = $fopen ( { `STR ( `TEST_OBJ_DIR ) , " /t_sys_file_basic_mcd_test5.dat " } ) ;
if ( fd_all = = 0 ) fail ( " could not be opened. " ) ;
fd_all | = 1 ;
$fdisplay ( fd_all , " To file and to stdout " ) ;
$fclose ( fd_all ) ;
2023-02-05 15:18:03 +00:00
endtask
2021-06-06 23:32:48 +00:00
2020-05-14 22:03:00 +00:00
initial begin
// Test1: Validate file descriptor region.
test1 ;
// Test2: Validate basic MCD functionality.
test2 ;
// Test3: Validate explicit descriptor ID
test3 ;
// Test4: Validate filename lengths
test4 ;
2021-06-06 23:32:48 +00:00
// Test5: OR with stdout
test5 ;
2020-05-14 22:03:00 +00:00
$write ( " *-* All Finished *-* \n " ) ;
$finish ( 0 ) ; // Test arguments to finish
end // initial begin
`undef STR
endmodule // t