Close File
$fclose closes a previous opened file.
$fclose(<file_desc>);
In general, you may wish to limit the amount and occurrences of reading and writing to a file during simulation as it may have a negative impact on overall simulation runtime. File access can be a slow process and if done often can weigh down simulation quite a bit.
Example of writing monitored signals:
reg [8*80-1:0] error_string_reg;
integer error_number;
integer outfile;
initial begin
// Create necessary Variables and Open file
outfile = $fopen("output.dat");
// Check if file was properly opened and if not, produce error and exit
if (outfile == 0) begin
error_number = $ferror(outfile, error_string_reg);
$display("Error: File, output.dat could not be opened due to the following error:\n %d: %s", error_number, error_string_reg);
$finish;
end
// Write monitor data to a file
$fmonitor (outfile, "Time: %t\t Data_out = %h", $realtime, Data_out);
// Wait for 1 ms and end monitoring
#1000000;
// Close file to end monitoring
$fclose(outfile);
end
Screen output
$display will display a string to the standard output (screen/console) of the simulator. Variables may be added to the string to indicate current time (as well as other system functions) and states of signals in the design. After the string is displayed, a carriage return is issued.
$display("<string_and/or_variables>", <functions_or_signals>);
Example of $display:
initial begin
#100000;
$display("Simulation Ended Normally at Time: %t", $realtime");
$stop;
end
$monitor will display a string to the standard output whenever a change in value is detected for one of the variables being displayed. After the string is displayed, a carriage return is issued.
$monitor("<string_and/or_variables>", <functions or signals>);
Example of $monitor:
initial
$monitor("time %t: out1=%d(decimal), out2=%h(hex), out3=%b(binary),
state=%s(string)", $realtime, out1, out2, out3, state);
$write acts very similar to $display in that it can output a specified string to the standard out however it does not return a carriage return after performing this operation.
$write ("<string_and/or_variables>", <functions_or_signals>);
Example of $write:
always @(posedge check)
$write(".");
$strobe is also similar to $display only waits for all simulation events in the queue to be executed before generating the message.
$strobe ("<string_and/or_variables>", <functions_or_signals>);
Example of $strobe:
always @(out1)
if (out1 != correct_out1)
$strobe("Error at time %t: out1 is %h and should be %h",
$realtime, out1, correct_out1);
When using these standard output commands, variables can be specified to the output in a variety of formats. Also, special escape characters can be used to specify special characters or formating.
$display and $strobe are general used within a conditional statement (i.e. if (error) $display) specified from an initial or always construct while the $monitor is generally specified from an initial statement without any other qualification. Display functions are for simulation purposes only and while very useful, should be used sparingly in order to increase the overall speed of simulation. It is very useful to use these constructs to indicate problems in the simulation.
// UP counter
`timescale 1 ns/1ps
module COUNTRE (CLK, RES, CE, Q);
input CLK, RES, CE;
parameter upper = 8;
output [upper-1:0]Q;
parameter up = 2;
parameter DELAY = 0;
reg [upper-1:0] Q;
always @(posedge CLK or posedge RES)
if (RES) Q <= 0;
else if (CE) #DELAY Q <= Q + up;
endmodule
// D:\FPGA_DEMO
`timescale 1ns/1ps
module TIME;
reg CLK;
reg RES;
reg CE;
parameter uppera = 8;
parameter upperb =4;
parameter T_CLK = 5;
wire [uppera-1:0] QA;
wire [upperb-1:0] QB;
defparam UUT.upper = uppera;
defparam UUT.up = 2;
defparam UUT.DELAY = 5;
COUNTRE UUT (.CLK(CLK),.RES(RES),.CE(CE),.Q(QA));
COUNTRE #(4,1,0)UUT2(.CLK(CLK),.RES(RES),.CE(CE),.Q(QB));
integer TX_FILE;
integer TX_ERROR;
integer NEXT_Q; //
always
begin //clock process
CLK = 1'b0;
#T_CLK
CLK = 1'b1;
#T_CLK
#T_CLK
CHECK_Q (NEXT_Q, QA,TX_ERROR); //
CLK = 1'b0;
#T_CLK
CLK = 1'b0;
end
initial
begin
NEXT_Q = 0; ///
TX_ERROR=0;
TX_FILE=$fopen("results.txt");
// --------------------
RES = 1'b0;
CE = 1'b0;
// --------------------
#40 // Time=40 ns
RES = 1'b1;
// --------------------
#40 // Time=80 ns
RES = 1'b0;
// --------------------
#40 // Time=120 ns
CE = 1'b1;
// --------------------
#160 // Time=280 ns
CE = 1'b0;
// --------------------
#125 // Time=405 ns
// --------------------
if (TX_ERROR == 0) begin
$display("No errors or warnings");
$fdisplay(TX_FILE,"No errors or warnings");
end else begin
$display("%d errors found in simulation",TX_ERROR);
$fdisplay(TX_FILE,"%d errors found in simulation",TX_ERROR);
end
$fclose(TX_FILE);
$stop;
end
task CHECK_Q;
input [7:0] NEXT_Q;
input [7:0] Q;//
output TX_ERROR;
#0 begin
if (NEXT_Q !== QA) begin
$display("Error at time=%dns QA=%b, expected=%b",
$time, QA, NEXT_Q);
$fdisplay(TX_FILE,"Error at time=%dns QA=%b, expected=%b",
$time, QA, NEXT_Q);
TX_ERROR = TX_ERROR + 1;
end
end