verilog ifdef_verilog define

verilog ifdef_verilog define注意:feof判断文件结束是通过读取函数fread/fscanf等返回错误来识别的,故而判断文件是否结束应该是在读取函数之后进行判断。比如,在while循环读取一个文件时,如果是在读取函数之前进行判断,则如果文件最后一行是空白行,可能会造成内存错误。https://blog.csdn.net/Chauncey_wu/article/details/902897491.打开文件  int…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全家桶1年46,售后保障稳定

注意feof判断文件结束是通过读取函数fread/fscanf等返回错误来识别的,故而判断文件是否结束应该是在读取函数之后进行判断。比如,在while循环读取一个文件时,如果是在读取函数之前进行判断,则如果文件最后一行是空白行,可能会造成内存错误。

https://blog.csdn.net/Chauncey_wu/article/details/90289749

1.打开文件

  integer file_id;

  file_id = fopen(“file_path/file_name”);

2.写入文件:$fmonitor,$fwrite,$fdisplay,$fstrobe

  //$fmonitor只要有变化就一直记录

  $fmonitor(file_id, “%format_char”, parameter);

  $fmonitor(file_id, “%m: %t in1=%d o1=%h”, $time, in1, o1);

//$fwrite需要触发条件才记录

  $fwrite(file_id, “%format_char”, parameter);

//$fdisplay需要触发条件才记录

  $fdisplay(file_id, “%format_char”, parameter);

$fstrobe();

3.读取文件:$fread

  integer file_id;

  file_id = $fread(“file_path/file_name”, “r”);

4.关闭文件:$fclose

  $fclose(fjile_id);

5.由文件设定存储器初值:$readmemh,$readmemb

  $readmemh(“file_name”, memory_name”); //初始化数据为十六进制

  $readmemb(“file_name”, memory_name”); //初始化数据为二进制

6、文件显示:$monitor,$write,$display

 $display,$write用于输出信息

  $display(“rvel = %h hex %d decimal”,rvel,rvel);

  $monitor($time, ,”rxd = %b txd = %b”,rxd ,txd)

6、文件定位

  $fseek,文件定位,可以从任意点对文件进行操作;

  $fscanf,对文件一行进行读写。

7、退出仿真器$finish

8、随机数据产生:$random

 

Jetbrains全家桶1年46,售后保障稳定

  1. 下面是一些常见的应用:

  2. 1、读写文件

  3. `timescale 1 ns/1 ns

  4. module FileIO_tb;

  5. integer fp_r, fp_w, cnt;

  6. reg [7:0] reg1, reg2, reg3;

  7. initial begin

  8. fp_r = $fopen("data_in.txt", "r");

  9. fp_w = $fopen("data_out.txt", "w");

  10.  
  11. while(!$feof(fp_r)) begin

  12. cnt = $fscanf(fp_r, "%d %d %d", reg1, reg2, reg3);

  13. $display("%d %d %d", reg1, reg2, reg3);

  14. $fwrite(fp_w, "%d %d %d\n", reg3, reg2, reg1);

  15. end

  16.  
  17. $fclose(fp_r);

  18. $fclose(fp_w);

  19. end

  20. endmodule

  21. 2、

  22. integer file, char;

  23. reg eof;

  24. initial begin

  25. file = $fopenr("myfile.txt");

  26. eof = 0;

  27. while (eof == 0) begin

  28. char = $fgetc(file);

  29. eof = $feof (file);

  30. $display ("%s", char);

  31. end

  32. end

  33. 3、文件处理定位

  34. `define SEEK_SET 0

  35. `define SEEK_CUR 1

  36. `define SEEK_END 2

  37. integer file, offset, position, r;

  38. r = $fseek(file, 0, `SEEK_SET);

  39. r = $fseek(file, 0, `SEEK_CUR);

  40. r = $fseek(file, 0, `SEEK_END);

  41. r = $fseek(file, position, `SEEK_SET);

  42.   4、

  43. integer r, file, start, count;

  44. reg [15:0] mem[0:10], r16;

  45. r = $fread(file, mem[0], start, count);

  46. r = $fread(file, r16);

  47.   5、

  48. integer file, position;

  49. position = $ftell(file);

  50. 6、

  51. integer file, r, a, b;

  52. reg [80*8:1] string;

  53. file = $fopenw("output.log");

  54. r = $sformat(string, "Formatted %d %x", a, b);

  55. r = $sprintf(string, "Formatted %d %x", a, b);

  56. r = $fprintf(file, "Formatted %d %x", a, b);

  57. 7、

  58. integer file, r;

  59. file = $fopenw("output.log");

  60. r = $fflush(file);

  61. 8、

  62. // This is a pattern file - read_pattern.pat

  63. // time bin dec hex

  64. 10: 001 1 1

  65. 20.0: 010 20 020

  66. 50.02: 111 5 FFF

  67. 62.345: 100 4 DEADBEEF

  68. 75.789: XXX 2 ZzZzZzZz

  69. `timescale 1ns / 10 ps

  70. `define EOF 32'hFFFF_FFFF

  71. `define NULL 0

  72. `define MAX_LINE_LENGTH 1000

  73.  
  74. module read_pattern;

  75. integer file, c, r;

  76. reg [3:0] bin;

  77. reg [31:0] dec, hex;

  78. real real_time;

  79. reg [8*`MAX_LINE_LENGTH:0] line;

  80.  
  81. initial

  82. begin : file_block

  83. $timeformat(-9, 3, "ns", 6);

  84. $display("time bin decimal hex");

  85. file = $fopenr("read_pattern.pat");

  86. if (file == `NULL) // If error opening file

  87. disable file_block; // Just quit

  88.  
  89. c = $fgetc(file);

  90. while (c != `EOF)

  91. begin

  92.  
  93. if (c == "/")

  94. r = $fgets(line, `MAX_LINE_LENGTH, file);

  95. else

  96. begin

  97. // Push the character back to the file then read the next time

  98. r = $ungetc(c, file);

  99. r = $fscanf(file," %f:\n", real_time);

  100.  
  101. // Wait until the absolute time in the file, then read stimulus

  102. if ($realtime > real_time)

  103. $display("Error - absolute time in file is out of order - %t",

  104. real_time);

  105. else

  106. #(real_time - $realtime)

  107. r = $fscanf(file," %b %d %h\n",bin,dec,hex);

  108. end // if c else

  109. c = $fgetc(file);

  110. end // while not EOF

  111.  
  112. r = $fcloser(file);

  113. end // initial

  114.  
  115. // Display changes to the signals

  116. always @(bin or dec or hex)

  117. $display("%t %b %d %h", $realtime, bin, dec, hex);

  118.  
  119. endmodule // read_pattern

  120. 9、自动比较输出结果

  121. `define EOF 32'hFFFF_FFFF

  122. `define NULL 0

  123. `define MAX_LINE_LENGTH 1000

  124. module compare;

  125. integer file, r;

  126. reg a, b, expect, clock;

  127. wire out;

  128. reg [`MAX_LINE_LENGTH*8:1];

  129. parameter cycle = 20;

  130.  
  131. initial

  132. begin : file_block

  133. $display("Time Stim Expect Output");

  134. clock = 0;

  135.  
  136. file = $fopenr("compare.pat");

  137. if (file == `NULL)

  138. disable file_block;

  139.  
  140. r = $fgets(line, MAX_LINE_LENGTH, file); // Skip comments

  141. r = $fgets(line, MAX_LINE_LENGTH, file);

  142.  
  143. while (!$feof(file))

  144. begin

  145. // Wait until rising clock, read stimulus

  146. @(posedge clock)

  147. r = $fscanf(file, " %b %b %b\n", a, b, expect);

  148.  
  149. // Wait just before the end of cycle to do compare

  150. #(cycle - 1)

  151. $display("%d %b %b %b %b", $stime, a, b, expect, out);

  152. $strobe_compare(expect, out);

  153. end // while not EOF

  154.  
  155. r = $fcloser(file);

  156. $stop;

  157. end // initial

  158.  
  159. always #(cycle / 2) clock = !clock; // Clock generator

  160.  
  161. and #4 (out, a, b); // Circuit under test

  162. endmodule // compare

  163. 10、从文件中读数据到mem(这个好像一般人用的最多了)

  164. `define EOF 32'HFFFF_FFFF

  165. `define MEM_SIZE 200_000

  166. module load_mem;

  167. integer file, i;

  168. reg [7:0] mem[0:`MEM_SIZE];

  169. reg [80*8:1] file_name;

  170. initial

  171. begin

  172. file_name = "data.bin";

  173. file = $fopenr(file_name);

  174. i = $fread(file, mem[0]);

  175. $display("Loaded %0d entries \n", i);

  176. i = $fcloser(file);

  177. $stop;

  178. end endmodule // load_mem

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/234533.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 二级Python选择题_计算机二级python题库及答案

    二级Python选择题_计算机二级python题库及答案第一套试题关于数据的存储结构,以下选项描述正确的是A数据所占的存储空间量B数据在计算机中的顺序存储方式C数据的逻辑结构在计算机中的表示D存储在外存中的数据正确答案:C关于线性链表的描述,以下选项中正确的是A存储空间不一定连续,且前件元素一定存储在后件元素的前面B存储空间必须连续,且前件元素一定存储在后件元素的前面C存储空间必须连续,且各元素的存储顺序是任…

    2022年10月12日
    2
  • STM32 的 “位带”操作Bit-banding–学习笔记

    STM32 的 “位带”操作Bit-banding–学习笔记利用2个32MB大小的“虚拟”内存空间实现对2个1MB大小的物理内存空间进行“位”的置位和清除操作。这样就可以有效地对设备寄存器和位于SRAM中的数据变量进行位操作,而不再需要冗长的布尔逻辑运算过程。…

    2022年9月25日
    4
  • 推荐系统(Recommendation system )介绍[通俗易懂]

    推荐系统(Recommendation system )介绍[通俗易懂]前言随着电子商务的发展,网络购物成为一种趋势,当你打开某个购物网站比如淘宝、京东的时候,会看到很多给你推荐的产品,你是否觉得这些推荐的产品都是你似曾相识或者正好需要的呢。这个就是现在电子商务里面的推

    2022年8月4日
    5
  • pytest+allure实战

    pytest+allure实战pytest+allure实战pytest+allure实战基本架构Login.pytest.pyrun_all_case.py测试报告pytest+allure实战写之前,说一下自己的感受,大家之前调试出来的框架什么的一定要做好记录,或者归纳整理好,pytest+allure很久之前就用过了,但是当时出报告以后就扔一边了,当我想整理写一篇关于这个的时候完全找不到在哪,但是脑子里还记的这个框架之前100%用过,就是不知道放哪里了,重新调试也不想调,就一直翻电脑,越翻越燥,大半天也没找见,其实就在我眼皮底

    2022年7月26日
    7
  • AutoEventWireup

    AutoEventWireup
      Google了一番,大家讨论AutoEventWireup问题可不少,Page指令的AutoEventWireup属性被设置为true(或者如果缺少此属性,因为它默认为true),该页框架将自动调用页事件,即Page_Init、Page_Load等14个方法,在这种情况下,不需要任何显式的Handles子句或委托。但这是怎么实现的呢?.net又怎样根据AutoEventWireup属性来动态编译或者预编译页面呢?我在Google上没有找到答案。
     

    2022年5月28日
    35
  • wget404错误_错误403谷歌

    wget404错误_错误403谷歌报错信息Resolvings3.ap-northeast-1.amazonaws.com(s3.ap-northeast-1.amazonaws.com)…52.219.8.176Connectingtos3.ap-northeast-1.amazonaws.com(s3.ap-northeast-1.amazonaws.com)|52.219.8.176|:443…connected.HTTPrequestsent,awaitingresponse…403Forbidden

    2025年5月27日
    1

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号