1.文件打开和关闭:首先定义integer指针,然后调用$fopen(file_name,mode)任务,不需要文件时,调用$fopen(file_name)

常用mode包括

“w"打开文件并从文件头开始写,如果不存在就创建文件。

“w+"打开文件并从文件头开始读写,如果不存在就创建文件

"a"打开文件并从文件末尾开始写,如果不存在就创建文件

“a+"打开文件并从文件末尾开始读写,如果不存在就创建文件

2.从文件中读取数据,可以用 $readmemb       $readmemh  从文件中读入数据,该文件格式是一定的.

reg[7:0] data[47:0];

$readmemh("file_name.txt",data );

就是将file_name.txt中的数据读入到data数组中,然后就可以使用这些数据了.

还有一种方式可以把指定的数据放入指定的存储器地址单元内,就是在存放数据的文本文件内,给相应的数据规定其内存地址,形式如下:

@address_in_hexadecimal   data

@2f             20

两个系统任务可以在仿真的任何时刻被执行使用,其使用格式共有以下六种:

1)     $readmemb("",);

2)     $readmemb("",,);

3)     $readmemb("",,,);

4)     $readmemh("",);

5)     $readmemh("",,);

6)     $readmemh("",,,);

在这两个系统任务中,被读取的数据文件的内容只能包含:空白位置(空格,换行,制表格(tab)和form-feeds),注释行(//形式的和形式的都 允许),二进制或十六进制的数字。数字中不能包含位宽说明和格式说明,对于$readmemb系统任务,每个数字必须是二进制数字,对 于$readmemh系统任务,每个数字必须是十六进制数字。数字中不定值x或X,高阻值z或Z,和下划线(_)的使用方法及代表的意义与一般 Verilog HDL程序中的用法及意义是一样的。另外数字必须用空白位置或注释行来分隔开。

3. 以前我一般常用到的系统函数只有几 个:$readmemb,$readmemh,$display,$fmonitor,$fwrite,$fopen,$fclose等。通常需要对文件 作预处理,才能用于Testbench读取。今天又尝试了几个其他的文件输入输出函数,不需要对文件进行预处理,直接使用需要的文件,只对需要的部分进行 读取。

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

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

下面是一些常见的应用:

1、读写文件

`timescale 1 ns/1 ns

module FileIO_tb;

integer fp_r, fp_w, cnt;

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

initial begin

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

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

while(!$feof(fp_r)) begin

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

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

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

end

$fclose(fp_r);

$fclose(fp_w);

end

endmodule

2、

integer file, char;

reg eof;

initial begin

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

eof = 0;

while (eof == 0) begin

char = $fgetc(file);

eof = $feof (file);

$display ("%s", char);

end

end

3、文件处理定位

`define SEEK_SET 0

`define SEEK_CUR 1

`define SEEK_END 2

integer file, offset, position, r;

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

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

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

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

4、

integer r, file, start, count;

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

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

r = $fread(file, r16);

5、

integer file, position;

position = $ftell(file);

6、

integer file, r, a, b;

reg [80*8:1] string;

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

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

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

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

7、

integer file, r;

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

r = $fflush(file);

8、

// This is a pattern file - read_pattern.pat

// time bin dec hex

10: 001 1 1

20.0: 010 20 020

50.02: 111 5 FFF

62.345: 100 4 DEADBEEF

75.789: XXX 2 ZzZzZzZz

`timescale 1ns / 10 ps

`define EOF 32'hFFFF_FFFF

`define NULL 0

`define MAX_LINE_LENGTH 1000

module read_pattern;

integer file, c, r;

reg [3:0] bin;

reg [31:0] dec, hex;

real real_time;

reg [8*`MAX_LINE_LENGTH:0] line;

initial

begin : file_block

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

$display("time bin decimal hex");

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

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

disable file_block; // Just quit

c = $fgetc(file);

while (c != `EOF)

begin

if (c == "/")

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

else

begin

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

r = $ungetc(c, file);

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

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

if ($realtime > real_time)

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

real_time);

else

#(real_time - $realtime)

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

end // if c else

c = $fgetc(file);

end // while not EOF

r = $fcloser(file);

end // initial

// Display changes to the signals

always @(bin or dec or hex)

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

endmodule // read_pattern

9、自动比较输出结果

`define EOF 32'hFFFF_FFFF

`define NULL 0

`define MAX_LINE_LENGTH 1000

module compare;

integer file, r;

reg a, b, expect, clock;

wire out;

reg [`MAX_LINE_LENGTH*8:1];

parameter cycle = 20;

initial

begin : file_block

$display("Time Stim Expect Output");

clock = 0;

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

if (file == `NULL)

disable file_block;

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

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

while (!$feof(file))

begin

// Wait until rising clock, read stimulus

@(posedge clock)

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

// Wait just before the end of cycle to do compare

#(cycle - 1)

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

$strobe_compare(expect, out);

end // while not EOF

r = $fcloser(file);

$stop;

end // initial

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

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

endmodule // compare

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

`define EOF 32'HFFFF_FFFF

`define MEM_SIZE 200_000

module load_mem;

integer file, i;

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

reg [80*8:1] file_name;

initial

begin

file_name = "data.bin";

file = $fopenr(file_name);

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

$display("Loaded entries \n", i);

i = $fcloser(file);

$stop;

end endmodule // load_mem

verilog从txt中读取_Verilog中的文件操作相关推荐

  1. 如何matlab导入邻接矩阵,“excel如何做矩阵“matlab中读取excle中的邻接矩阵

    怎么在excel中使用矩阵函数 在excel中使用矩阵: 1.矩阵乘法运算择G3:H4,公式:=MMULT(A3:B4,D3:E4) 按Ctrl Shift Enter键,即输组公式. 2.阵的逆矩阵 ...

  2. [转载] pythonpandas读取csv文件最后一行_简单小案例(一):使用Pandas在Python中读取和写入CSV文件...

    参考链接: 使用Pandas在Python中读写CSV文件 前期文章链接: YOLOv3论文 https://arxiv.org/pdf/1804.02767.pdf 正文: 有许多方法可以在Pyth ...

  3. python在文件中写入字典_python初学--文件操作、字典

    文件读写 1.先打开文件 2.读取/写入内容 3.保存文件 文件的open模式有三种 1.w 写模式,它是不能读的 只要用w打开文件,文件中的东西都会被清空 w+, 写读模式,只要沾上w 就会清空原来 ...

  4. axios nodejs 上传图片_vue项目中使用axios上传图片等文件操作

    axios 简介 axios 是一个基于Promise 用于浏览器和 nodejs 的 HTTP 客户端,它本身具有以下特征: 从浏览器中创建 XMLHttpRequest 从 node.js 发出 ...

  5. c++读取txt文件中的数字_在Python中读取包中的数据文件的三种方式

    我们知道,写Python代码的时候,如果一个包(package)里面的一个模块要导入另一个模块,那么我们可以使用相对导入: 假设当前代码结构如下图所示: 其中test_1是一个包,在util.py里面 ...

  6. verilog 给数组集体赋值_Verilog中如何对数组赋值(存储器memory详解)

    http://blog.sina.com.cn/s/blog_9424755f0101rhrh.html 存储器是一个寄存器数组.存储器使用如下方式说明: reg [ msb: 1sb] memory ...

  7. 在Python中读取MATLAB的数据文件

    简 介: 本文测试了几种通过python直接读取MATLAB的**.MAT格式的数据文件,有些方法经过测试发现无法完成.而通过mat4py可以比较方便的读取MATLAB中的数据文件.利用手边已有的用于 ...

  8. python读文件和写文件-python开发--从文件中读取数据和写入文件

    #! /usr/bin/env python -*- coding:utf-8 -*- """ @Author:gcan @Email:1528667112@qq.com ...

  9. C#中读取数据库中Image数据

    作者:未知 请与本人联系 DataReader 的默认行为是在整个数据行可用时立即以行的形式加载传入数据.但是,对于二进制大对象 (BLOB) 则需要进行不同的处理,因为它们可能包含数十亿字节的数据, ...

最新文章

  1. jquery 获取和设置 select下拉框的值(转手册)
  2. setcookie无效
  3. 参数估计_数据分析|统计之一个总体参数估计(附代码)
  4. python入门编程软件免费-Python编程干货免费领取!!!
  5. 【代码笔记】iOS-长条label
  6. sqlserver日志文件过大的处理方法
  7. Statement与PreparedStatement区别
  8. 纯前端表格控件SpreadJS V12.1 隆重登场,专注易用性,提升用户体验
  9. 移动web开发适配rem
  10. python progressbar 倒计时_Python使用progressbar模块实现的显示进度条功能
  11. 美食杂志排行榜_百度知道
  12. springboot+vue整合百度的Ueditor(保姆级教程)
  13. Android 解决Dialog 样式的Activity 半透明背景失效问题
  14. solr全文检索(多字段搜索)
  15. Datawhale组队学习开源内容汇总
  16. 机器学习之实现一元线性回归模型
  17. 布局中颜色搭配怎么看最舒服之白色的最佳10种颜色搭配
  18. php老虎杠子鸡虫条件,老虎、杠子、鸡——在游戏中学习
  19. DIV滚动条自动滚动到最底部的两种方法
  20. 2022-2027年中国电饭煲自动检测线行业市场全景评估及发展战略规划报告

热门文章

  1. iPhone SE 3最高机身内存将增至256GB 较当前版本翻番
  2. 小米获京东自营安卓平板销量冠军 小米平板5 Pro全版本闪降100元
  3. 4999元起!三星在中国正式发布Galaxy S22系列
  4. 微信发虎年新春贺词领福袋:游戏皮肤、QQ音乐VIP、现金红包等
  5. 宣称“禁用新疆产品”,英特尔紧急道歉:出于表述合规合法初衷
  6. 双十一临近 乐视商城宣布全场包邮
  7. 国家邮政局:除夕和初一包裹量超1.3亿件,同比增长223%
  8. 微信又上线新功能,能让你更会聊天?
  9. 线上安全大会还能这么玩 ISC 2020首创“3D立体云展馆”
  10. 诺基亚贝尔回应“落选运营商5G采购”:尊重运营商决定