BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / embedded-system / #10562同步于 2011/5/6
该镜像源已超过 30 天没有更新,可能在源站已被删除。
Embedded_System机器人发帖

vhdl连续读取文件数据的问题

mosquitowen
2011/5/6镜像同步2 回复
现在想在modelsim里面的testbench测试文件中连续读取一个文档里的多行数据充当激励信号,对于textio库里的函数不太会用,readline(file,buf);加上read(buf,data)是每次读一行里的一个数据吧,怎么读其他行的呢。意思就是每个时钟周期读文件里一行数据,存进data里,下个时钟周期读下一行的。我写的结构体里的文件进程如下,大家帮我改改。x是信号,之前已经声明过。 file_process:process file filein: text; --定义text类型的文件句柄; variable fstatus1:FILE_OPEN_STATUS; --定义文件状态指示变量; variable buf:LINE; --定义行变量 variable data:integer; begin file_open(fstatus1 ,filein ,"sig.txt",read_mode);-- 打开文件 readline(filein,buf); read(buf,data); x<=data; file_close(filein); end process;
订阅后,新回复会通过你的通知中心匿名送达。
2 条回复
yuxuejun1123机器人#1 · 2011/5/10
BD
zenith机器人#2 · 2011/5/17
自从用了verilog,vhdl忘的差不多了。。。 能找到以前自己记的几行笔记,贴上来lz自己看看,也许有帮助。。。 文件的读写,testbench中常常用到。 首先,textio只能用bit和bit_vector,如果想使用std_logic和std_logic_vector需要调用 std_logic_textio。 读写文件对象的声明: file file_obj_name : text open read_mode is "目录+文件.后缀"; file file_obj_name : text open write_mode is "目录+文件.后缀"--这里是97版的,87版这里就不写了 textio里面定义了两种数据类型:line和text。text是一种文件类型,相当于type text is file of string,即以文本的方式读写文件,只要文件中的东西是asicc码可编的。line是缓冲。在文件读写的时候先将vhdl中的变量写进一个line型的变量,然后再把这个line型的变量写进一个text类型的文件。 constant n : natural := 4; file vectors : text; file results : text; 。。。 stim_proc: process variable iline, oline : line; variable xin, yin, zout : bit_vector(n-1 downto 0); variable ci_in, co_out : bit; variable ch : character; begin file_open(vectors, "D:\work\test\adder\vectors.txt", read_mode); file_open(results, "D:\work\test\adder\results.txt", write_mode); while not endfile(vectors) loop readline(vectors, iline); read(iline, xin); read(iline, ch); read(iline, yin); read(iline, ch); read(iline, ci_in); a <= to_stdlogicvector(xin); b <= to_stdlogicvector(yin); cin <= to_stdulogic(ci_in); wait for 150ns; zout := to_bitvector(sum); co_out := to_bit(cout); write(oline, zout, right, 5); write(oline, co_out, right, 2); writeline(results, oline); end loop; file_close(vectors); file_close(results); wait; end process; 上面是今天写的一个例子。 读语句: realine(文件变量file_variable,行变量l);--将文件 file_variable中的一行数据读至行变量l中 read(行变量l,数据变量v);--行变量l中保存的数据是从文件变量file_variable中读出来的,将其中的nbit放至数据变量v 中,n为数据变量v的数据位数 在此之前,需要定义变量: variable l:line; 数据变量v也要定义,一般是信号signal。 写语句类似: write(行变量l,数据变量v);或 write(行变量l,数据变量v,起始位置(left or right,左对齐或又对齐),字符数);--数据变量v为now时,是写时间,字符数的控制可以使各个变量之间有一定的空格。 writeline(文件变量,行变量l); 另外,读文件时,需要检查文件是否结束: endfile(文件变量); 还有要说的就是注意上面wait语句的位置,因为wait语句才将进程挂起,执行仿真周期中的变量值改变的部分。