Verilog common system tasks and functions

        When writing simulation test stimulus files in FPGA development, verilog system functions are often used, and it is very convenient to use system functions. This article summarizes commonly used system functions, and provides instructions and usage examples.

1. File operation

1. Open and close files

integer fp;
fp = $fopen("file_path/file_name","wb");
$fclose(fp);

Open the file $fopen function:

        The first parameter: file path, both absolute path and relative path are acceptable.

        The second parameter:

        Return value: 0 means opening the file successfully, non-zero value means opening the file failed

        Close the file $fclose function: the parameter is the integer variable returned by $fopen

2. Write to file

$fdisplay(fp,"%d", dout);
$fwrite(fp,"%d", dout);
$fmonitor(fp, "%h", $time);
$fstrobe(fp, "%h", 16'h78)

        Functions such as $fdisplay write data into the specified file, the first parameter is the file pointer, the second parameter is the format of the written data, and the third parameter is the written data.

        Write data format:

illustrate:

        $fdisplay is executed immediately after calling, and it can automatically wrap,

        The $fwrite call is executed immediately, and the newline needs to be written as "%d\n".

        $fmonitor will only write to the file when the variable changes, just call it once in the initial.

        Write the file after the $fstrobe statement is executed

3. Read the file

        For the read file function, it is necessary to set the memory to store data, and the memory can be set as a two-dimensional array.

$fread(mem, fd, start, count)

        Parameter description: mem is the storage array or register variable, fp is the file pointer, start is the starting address of the file, and count is the read length. If start and count are omitted, all the data will be filled to the variable mem and then stop.

$readmemh("file_path/file_name",mem,start, end);
$readmemb("file_path/file_name",mem,start, end);

$readmemb是以2进制读取,$readmemh是以16进制读取

        Parameter description: file_path/file_name is the file path, mem is the storage array or register variable, start is the starting address, and end is the end point. If start and end are omitted, all the data will be filled into the variable mem.

        $fgetc( fd ) reads the file by character, each execution reads 8bit.

        $fgets(str, fd) reads a file line by line, one line per execution.

localparam FILE_LEN = 1000;
localparam WIDTH = 8;
integer num, i;
reg [WIDTH-1:0] mem [0:FILE_LEN-1];
reg [WIDTH-1:0] mem_r;
reg [31:0] mem_load [3:0] ;
reg [99:0] line_buf [9:0] ;
//方法1
$fread(mem,fp,0,FILE_LEN);//数组型读取
$fread(mem_r,fp);//单个寄存器
//方法2
$readmemb("file_path/file_name", mem);
$readmemb("file_path/file_name", mem,0,FILE_LEN-1);
$readmemh("./DATA_WITHNOTE.HEX", mem_load);
//方法3
mem[0] = $fgetc(fp);
for(i=0;i<FILE_LEN;i=i+1) begin
mem[i] = $fgetc(fp);
end
//方法4
for(i=0;i<9;i=i+1) begin
$fgets(line_buf[i], fd);
end

4. File location

        pos = $ftell( fd ) ; Get the file position function, return the offset from the current position of the file to the beginning of the file, and the offset is in bytes.

        $fseek(fd, offset, type) ; Relocation function, offset is the offset, type is the offset reference address, 0 indicates the starting position of the file, 1 indicates the current position, and 2 indicates the end address of the file.

        $rewind( fd ) ; equivalent to $fseek( fd, 0, 0) , reposition the file pointer to the beginning of the file

        $feof(fd) ; The end-of-file detection function, the return value is 1 when the end of the file is detected, otherwise it is 0.

pos = $ftell(fd);
$fseek(fd, 6, 0) ; //重定位到文件起始后的第6个地址
$fseek(fd, 6, 1) ; //重定位到当前地址后的第6个地址
$rewind(fd);//重新将文件指针的位置指向文件首部

2. Display/print class

        Used to display and print information on the terminal, common functions: $display, $strobe, $monitor

$display("<string>", variables); $strobe("<string>", variables);

        Output format:

        escape character:

illustrate:

        $display prints the result directly, regardless of whether it is a blocking assignment or a non-blocking assignment.

        The $strobe execution waits until the non-blocking assignment completes before printing the result.

        $monitor is used for continuous monitoring of variables, as long as the variable changes, it will be printed and displayed.

3. Others

1. Random number generation function

        $random, used to generate random numbers

//num为范围在 -(b-1):(b-1) 中的随机数, b为十进制整数
num = $random%b;
//产生随机正整数, 在 0:(b-1) 中的随机数
num = {$random}%b

2. Simulation process control function

        $finish means to end the current simulation, and $stop means to suspend the current simulation.

$finish;
$stop;

3. Display the simulation time

        $time returns an integer time value, and $realtime returns a real number time value, both of which are based on the time at the start of the simulation.

$display($time,“ dout=%d”,dout);

Guess you like

Origin blog.csdn.net/jk_101/article/details/129793734