7,verilog仿真语法模版_函数与任务

System tasks & functions

1.文件输入输出(file IO)

   1)读内存文件(read memory file)

            (1)二进制数据

reg [<memory_width>] <reg_name> [<memory_depth>];

initial

$readmemb ("<file_name>", <reg_name>, <start_address>, <end_address>);

           (2)十六进制数据

reg [<memory_width>] <reg_name> [<memory_depth>];

initial

$readmemh ("<file_name>", <reg_name>, <start_address>, <end_address>);

          (3)例子

//$readmemb/h 是一个系统函数,用于从指定文件中读取2或16进制数据并将其放入数组中。其中 //<file_name> 是包含2或16进制数据的文件名和位置,<reg_name> 是存储内存数据的二维寄存器数组,
//最后两个可选的逗号分隔数字指定数据的起始地址和终止地址。数据文件只能包含2或16进制数据、空白
//和注释。此函数必须在initial块内执行。过去,这些函数只能用于仿真目的,但现在的综合工具可以使
//用这种方法初始化 RAM 和 ROM 阵列。

//下面为从文件读取二进制数据的示例:

reg  [31:0] rom_data[1023:0];

initial

  $readmemb("../data/mem_file.dat", rom_data, 0, 7);

// The initialization file may only contain white spaces, address

// labels (denoted by @<address>), comments and the actual binary

// or hexadecimal data.

// The following is a small example of a binary memory file data:

// This is a comment

1111000011110000     // This specifies these 16-bits to the first address

1010_0101_1010_0101  // This is for the second address with underscores

                     // to make this more readable

<more entries like above to fill up the array>

// Optionally, we can change addresses

@025 // Now at address 025

11111111_00000000

// Addresses can also be specified in-line

@035 00000000_11111111

// It is highly suggested to fill all memory contents with a known value

// when initializing memories.

2)读写文件(read/write to a file)

(1)Change the file position

 <integer> = $fseek(<file_desc>, <offset_value>, <operation_number>);

(2)Close a file

   $fclose(<file_desc>);

(3)Display text to a file

   $fdisplay(<file_desc>, "<string>", variables);

(4)Find the file position

   <reg> = $ftell(<file_desc>);

(5)Flush buffer

   $fflush(<file_desc>);

(6)Open a file

 integer <file_desc>;

   <file_desc> = $fopen("<file_name>", "<file_mode>");

(7)Read a character from a file

reg [7:0] <8-bit_reg>;

   <8-bit_reg> = $fgetc(<file_desc>);

(8)Read a formatted line from a file

   integer <integer>;

   <integer> = $fscanf(<file_desc>, "<format>", <destination_regs>);

(9)Read a line from a file

   integer <integer>;

   reg [8*<#_of_chars>:0] <string_reg>;

   <integer> = $fgets(<string_reg>, <file_desc>);

(10)Strobe text to a file

   $fstrobe(<file_desc>, "<string>", variables);

(11)Test error

   <640-bit_reg> = $ferror(<file_desc>);

(12)Write monitored text to a file

   $fmonitor(<file_desc>, "<string>", variables);

(13)Write text to a file

$fwrite(<file_desc>, "<string>", variables);

(14)例子

//打开命令:
//<file_mode>可以是以下模式之一:
//“r" ...... 打开 ASCII 文件供读取
//“rb" ..... 打开二进制文件以供读取
//“w" ...... 打开用于写入的 ASCII 文件(如果存在则删除)
//“wb" ..... 打开二进制文件用于写入(存在时删除)
//“a" ...... 打开 ASCII 文件以便写入(附加到文件末尾)
//“ab" ..... 打开二进制文件以便写入(追加到文件末尾)
//“r+" ..... 打开 ASCII 文件进行读写

// 写入命令:
//Variables类型
//%b .... 二进制值
//%h .... 十六进制值
//%d .... 十进制值
//%t .... 时间
//%s .... 字符串
//%c .... ASCII码
//%f .... 实值
//%e .... 指数值
//%o .... 八进制值
//%m .... 模块层级名称
//%v .... 强度
// Example of writing monitored signals:

// -------------------------------------

   // Define file handle integer

   integer outfile;

   initial begin

      // Open file output.dat for writing

      outfile = $fopen("output.dat", "w");

      // Check if file was properly opened and if not, produce error and exit

      if (outfile == 0) begin

         $display("Error: File, output.dat could not be opened.\nExiting Simulation.");

         $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

// Example of reading a file using $fscanf:

// ----------------------------------------

   real number;

   // Define integers for file handling

   integer number_file;

   integer i=1;

   initial begin

      // Open file numbers.txt for reading

      number_file = $fopen("numbers.txt", "r");

      // Produce error and exit if file could not be opened

      if (number_file == 0) begin

         $display("Error: Failed to open file, numbers.txt\nExiting Simulation.");

         $finish;

      end

      // Loop while data is being read from file

      //    (i will be -1 when end of file or 0 for blank line)

      while (i>0) begin

         $display("i = %d", i);

         i=$fscanf(number_file, "%f", number);

         $display("Number read from file is %f", number);

         @(posedge CLK);

      end

      // Close out file when finished reading

      $fclose(number_file);

      #100;

      $display("Simulation ended normally");

      $stop;

   end

2.随机数生成(random number generation)

<reg> = $random(<seed>);

3.屏幕输出显示(screen output)

1)Display text

  $display("<string_and/or_variables>", <functions_or_signals>);

2)Monitor signals

   $monitor("<string_and/or_variables>", <functions or signals>);

3)Strobe text

   $strobe ("<string_and/or_variables>", <functions_or_signals>);

4)Write text

   $write ("<string_and/or_variables>", <functions_or_signals>);

5)例子

// Example of $display:

   initial begin

      #100000;

      $display("Simulation Ended Normally at Time: %t", $realtime");

      $stop;

   end

// Example of $monitor:

   initial

 $monitor("time %t: out1=%d(decimal), out2=%h(hex), out3=%b(binary),

        state=%s(string)", $realtime, out1, out2, out3, state);

// Example of $write:

   always @(posedge check)

      $write(".");

// 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);

// Example of using a $monitor to display the state of a state-machine to the screen:

   reg[8*22:0] ascii_state;

   initial

     $monitor("Current State is: %s", ascii_state);

   always @(UUT.top.state_reg)

      case (UUT.top.state_reg)

         2'b00  : ascii_state = "Reset";

         2'b01  : ascii_state = "Send";

         2'b10  : ascii_state = "Poll";

         2'b11  : ascii_state = "Receive";

         default: ascii_state = "ERROR: Undefined State";

      endcase

4.有符号/无符号数据转换(signed/unsigned)

   $signed(<signal>);

   $unsigned(<signal>);

5.仿真时间(simulation time)

1)Format

initial
      $timeformat (-6, 6, " us", 10);
//
initial
      $timeformat (-3, 0, " ms", 8);
//
initial
      $timeformat (-9, 3, " ns", 13);
//
initial
      $timeformat (-12, 1, " ps", 13);
//
initial
      $timeformat (0, 0, " sec", 6);

2)如下例子:

initial
      $timeformat (-9, 3, " ns", 13);

//上述代码指定了以纳秒为单位显示的输出,精确到皮秒,在时间后附加 “ns ”字符串,并允许显示 13 个
//数字来显示该值。下面的代码将在 “Time=”字符串后以上述纳秒格式显示系统时间,并在 DATA_OUT 值
//发生变化时显示 DATA_OUT 的值。每次 DATA_OUT 的值发生变化时,都会显示   DATA_OUT 的值。

initial
     $monitor("Time=%t : DATA_OUT=%b", $realtime, DATA_OUT);

6.停止仿真(stop simulation)

1)暂停仿真

$stop;

2)结束仿真

$finish;

点赞加关注博主(IDFPGA小飞)的博文,咱们一起学习、一起进步吧~

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值