.verilog系统任务读写文件$fopen和$fdisplay的使用

来源:互联网 发布:java 获取对象类型 编辑:程序博客网 时间:2024/05/18 18:16
1.$fopen打开文件
  用法1.$fopen("<文件名>");
  用法2.<文件句柄>=$fopen("<文件名>");
注意:用$fopen打开文件会将原来的文件清空,若要读数据就用$readmemb,$readmemh就可以了,这个语句不会清空原来文件中的数据。
用$fopen的情况是为了取得句柄,即文件地址,也就是写文件时用$fdisplay(desc,"display1");时才用。
用法1自然无须多解释,对于用法2,句柄就是任务$fopen返回的多通道描述符,默认为32位,最低位(第0位)默认被设置1,默认开放标准输出通道,即transcript窗口。
  module disp;
  integer handle1,handle2,handle3;
initial
 begin
   handle1=$fopen("file1.dat");
   handle2=$fopen("file2.dat");
   handle3=$fopen("file3.dat");
   $display("%h %h %h",handle1,handle2,handle3);
 end
endmodule
输出
handle1=32‘h0000_0002
handle2=32'h0000_0004
handle3=32'h0000_0008
即对每一次使用$fopen函数后都打开了一个新的通道,并且返回了一个设置为1的位相对应。默认应该是0001,以上每调用分别设置为0010 ,0100,1000(只考虑最低四位)。
这个句柄对我们非常有用,因为在写文件时会用到。
2.写文件我们用到系统任务$fdisplay,$fwrite.
两者用法相似,前者写完就会自动换行,后者不会换行。
用法:$fdisplay(<文件描述符(句柄,用于确定是写哪一个文件)>,p1,p2(写入内容));
描述符是很有意思的(默认为32位),如上我们知道,文件句柄最多只能有一个1,但是描述符可以是有多个1,哪一个位上有1,就同时在这些位对应的输出文件上写文件。因此可以同时写多个文件。
只考虑后四位情况:
 1,0001时,只进行在transcript对话框中的输出,
2,0101时,即对file2文件写又在transcript框输出、
3,1111时,对全部文件写,同时在transcript框输出。
由于每个句柄只有一个位置上是1,因此我们想在哪些文件中同时输出我们就可以用以下语句来写
desc=handleI | handleK | handleM | handleN | 1(一般与1或,最低位置1再transcript框输出。)
一个例子如下:
module disp;
  integer handle1,handle2,handle3;
  integer desc1,desc2,desc3;
initial
 begin
   handle1=$fopen("file1.dat");
   handle2=$fopen("file2.dat");
   handle3=$fopen("file3.dat");
   $display("%h %h %h",handle1,handle2,handle3);
   desc1=handle1|1;
   $fwrite(desc1,"display 1");
    $fmoniter(desc1,"display 1");
     $fstrobe(desc1,"display 1");
   
   desc2=handle2|handle1|1;
   $fdisplay(desc2,"display 2");
   desc3=handle3|1;
   $fdisplay(32'd15,"display 3");
   end
 endmodule
 
原创粉丝点击