Query regd file handling in System verilog

DearTeam,

My generator code in my SV env goes like this …

`include "transaction.sv"
`define EOF 32'hFFFF_FFFF
`define NULL 0

class generator;
  
  //declaring transaction class 
  rand transaction trans,tr;
  
  //txn_count, to specify number of items to generate
  // This section has declaration for declaring the other block
  int  txn_count;
  bit[1:0] Opcodes;
integer file,r;
bit [31:0] price_rand;
bit [31:0] OrderId_rand;

 
  //mailbox, to generate and send the packet to driver
  mailbox gen2driv;
  
  //event
  event ended;
  
  //constructor
  function new(mailbox gen2driv,event ended);
    //getting the mailbox handle from env, in order to share the transaction packet between the generator and driver, the same mailbox is shared between both.
    this.gen2driv = gen2driv;
    this.ended    = ended;
    trans = new();
  endfunction
  
  //main task, generates(create and randomizes) the txn_count number of transaction packets and puts into mailbox
  task main();
 
for (int i=0;i<txn_count;i++)

   begin : file_block

    Opcodes=trans.cmdcode;
    OrderId_rand=trans.ORDER_BOOK.OrderNmbr;
    price_rand=trans.newprice ;
    

    file = $fopenr("rand_Inputs_generator.txt");
    if (file == `NULL)
        disable file_block;

    while (!$feof(file))
        begin
        r = $fscanf(file, " %d %d %d \n", Opcodes, price_rand, OrderId_rand);
        end // while not EOF

    r = $fcloser(file);
                      
      end
	 tr = trans.do_copy();
	
     gen2driv.put(tr);
    end
    -> ended; 
  endtask
  endclass

my transaction class is 2086 bit and i am trying to configure a few fields(i.e Opcodes, price_rand, OrderId_rand ) of it from a text file(rand_Inputs_generator.txt) . The input text file is in the below format

///////////rand_Inputs_generator.txt////////////
0 1000 1
2 999 2
1 998 3
0 997 4
1 996 5
0 995 6
2 994 7
0 993 8
1 992 9
0 991 10
1 10 7
0 1000 1
2 999 2
1 998 3
0 997 4
1 996 5
0 995 6
2 994 7
0 993 8
1 992 9
0 991 10
1 10 7
0 997 4
1 996 5
/////////////

Need your help in understanding whats wrong with this code as i dont see any inputs applied to those 3 fields from this code…

Thanks in advance
Arjun

In reply to ArjunNag:

In your code, txn_count as default is 0, the loop won’t be executed. Shall you have txn_count to be randomized with “rand” keyword or will you assign a value to it larger than 0 already?

If txn_count is larger than 0, then the following code will work to have Opcodes, price_rand, OrderId_rand get value from the text file. Here, $fopen() and $fclose() used.

But in your code, after you have gotten value from the text file, I don’t see you use them as part of the transaction. I think codes need to be refined to fit for purpose more.


    for (int i=0;i<txn_count;i++) begin : file_block
      file = $fopen("rand_Inputs_generator.txt","r");
      while (!$feof(file)) begin
        r = $fscanf(file, " %d %d %d", Opcodes, price_rand, OrderId_rand);
        $display(" %d %d %d", Opcodes, price_rand, OrderId_rand);
      end // while not EOF
      $fclose(file);
    end

In reply to Lina.Lin:

Hi lina thanks for the reply…

I am doing the below thing in my code to use the values from the file after it is read and traversed.i am setting the txn_count from my test case.i stil. Dont see any values being applied from the file to these fields. Kindly advise.

for (int i=0;i<txn_count;i++) begin : file_block
file = $fopen(“rand_Inputs_generator.txt”,“r”);
while (!$feof(file)) begin
r = $fscanf(file, " %d %d %d", Opcodes, price_rand, OrderId_rand);
trans.cmdcode=opcodes;
trans.ORDER_BOOK.OrderNmbr=OrderId_rand;
trans.newprice=price_rand ;
$display(" %d %d %d", Opcodes, price_rand, OrderId_rand);
end // while not EOF
$fclose(file);
end

In reply to ArjunNag:

Can’t see the code you want to post.

What kind of debug message have you implemented to help on debug and locate the issues? In my previous code, the $display() message has demonstrated the file contents are fed into the three variables successfully. Please show what debug you have done and where you stuck now.