1

I want to use $fmonitor so that an output value is written to a file every time the value changes.

When I place $fclose inside the initial begin block, there are no errors. However, unsurprisingly, the output file is empty.

When I place $fclose right before the endmodule statement, I get a syntax error. Where should I place it?

`timescale 1ns / 1ps

module fir168_input_ones_tb();
    integer fp;
    reg clk, rst;
    wire tvalid, fir_valid, fir_rdy;
    wire [15:0] inputdata;
    wire signed [39:0] fir_out;
   
    generateOnes indata(
        .clk (clk), 
        .outData (inputdata), 
        .tlast (tvalid));  
         
    fir168 fir168_i
       //(.M_AXIS_DATA_0_tdata(fir_out_trunc),
       (.M_AXIS_DATA_0_tdata(fir_out),
        .M_AXIS_DATA_0_tvalid(fir_valid), 
        .S_AXIS_DATA_0_tdata(inputdata),
        .S_AXIS_DATA_0_tready(fir_rdy),
        .S_AXIS_DATA_0_tvalid(tvalid),
        .aclk_0(clk));
    
    always begin
        clk = ~clk;  #5;
    end
    
    initial begin
       fp = $fopen("simOutput.txt", "w+");
       $fmonitorh(fp, fir_out);
       clk = 0;
       //$display("%d", fir_out);
       //$fclose(fp);    
    end//initial
    $fclose(fp);   
endmodule
toolic
  • 57,801
  • 17
  • 75
  • 117
Rahel Miz
  • 159
  • 1
  • 9

3 Answers3

1

$fclose must be used inside a procedural block, like an initial block. That is why you get the syntax error when you place it right before endmodule.

If you want the file to be written until the end of the simulation, there is no need to call $fclose. The file will automatically be closed at the end of the sim.

Otherwise, you need to pick a specific time or a signal event and then close the file. But, keep in mind that $fmonitor will be active for the entire simulation. That means that if fir_out changes after you close the file, your simulator might generate error or warning messages.

toolic
  • 57,801
  • 17
  • 75
  • 117
1

$fopen/$fmonitor/$fclose are all procedural statements that must be placed inside procedural code.

When you had $fclose in the initial block, you closed it at time 0 before $fmonitor had a chance to produce any output. $fmonitor writes the state of its arguments at then end of current time slot when any of them change value.

You need to let your simulation run for some period of time and then have it execute a $finish. There is no need to explicitly use $fclose unless you have too many files open at one time, or some other process is blocked waiting for the file output.

dave_59
  • 39,096
  • 3
  • 24
  • 63
0

$fmoniotor statement does not do much, except it schedules monitor events at the end of every simulation tick. As a result, the $fclose will close the file in the same time tick where $fmonitor was issued before the first execution of a scheduled monitor event.

initial begin
   $fmonitor(...);
   ...
   $fclose()
end

You can do several things:

Put a certain timing distance between $fmonitor and $fclose, giving a chance for simulation to do something before the file gets closed:

initial begin
   $fmoniotor(..);
   ...
   #1000 // add a simulation delay of some value before closing
   $fclose(...);
end

you can place $fclose in the final block if you want to close it before exit:

initial begin
   $fmonitor(...);
   ...
end
final begin
   ... 
   $fclose(...);
end

Or you can figure out some event scheme which will close your file when a certain event is generated.

initial
    $fmonitor

always @* begin
   if (condition)
      $fclose()
end
Serge
  • 11,616
  • 3
  • 18
  • 28