1.我想使用Verilog写一个读文件的模块,接口规划如下:
module read_file_v #(
parameter SIZE = 8
)(
input clk,
input resetn,
input i_de,
output o_de,
output [SIZE-1:0] o_data,
?? input_file_path
);
如何给出input_file_path的数据类型?如果没有string这种数据类型,可以使用parameter或者define来解决吗?使用VHDL或者systemVerilog可以解决这个问题吗?可以的话能否给出例子?
1.1我用VHDL语言写了一个读文件的module(如下),遇到了几个问题:
1)文件名怎么放到接口上,我知道VHDL有text数据类型,这种数据类型如何放到接口上,怎么操作?
2) 我使用的readline读一行数据(这样如果一行有几个数据就不方便了),有没有一个clk读一个数据函数(类似Verilog的$fscanf)?
library ieee;
use ieee.std_logic_1164.all;
--use ieee.std_logic_arith.all;
--use ieee.std_logic_unsigned.all;
use IEEE.std_logic_textio.all;
library work;
--use work.DFN_PKG.ALL;
--use pkg.all;
entity read_file_y is
generic (
BIT_WIDTH : integer := 8);
port (
clk : in std_logic;
resetn : in std_logic;
i_den : in std_logic;
o_den : out std_logic;
o_data : out std_logic_vector(BIT_WIDTH-1 downto 0)
);
end read_file_y;
architecture BEHAVE of read_file_y is
file file_read_data : text open read_mode is "input/inY.hex";--file path
signal s_good : Boolean;
signal s_read_highbit_test : std_logic_vector(2 downto 0);
begin
process(clk,resetn)
variable line_read_hex_data : line;
variable read_hex_data : std_logic_vector((BIT_WIDTH+3)/4*4-1 downto 0);
variable good : BOOLEAN;
begin
if (resetn='0') then
o_den <= '0';
o_data <= (others =>'0');
s_read_highbit_test <= (others =>'0');
s_good <= true;
good := false;
elsif (clk'event and clk='1') then
if not endfile(file_read_data) and (i_den='1') then
readline(file_read_data,line_read_hex_data);--each line has one data
hread(line_read_hex_data, read_hex_data,good);
o_data <= read_hex_data(BIT_WIDTH-1 downto 0);
if(good=true) then --
o_den <= '1';
else
o_den <= '0';
end if;
--debug
s_good <= good;
--s_read_highbit_test <= read_hex_data(BIT_WIDTH+2 downto BIT_WIDTH);
else
o_den <= '0';
end if;
end if;
end process;
end BEHAVE;
1.2 我使用modelsim仿真,写了一个读文件的module,但是读出来的数据不能在module内部延迟,这是modelsim本身的bug还是我的代码的bug?另外如何把路径放到接口上面?代码如下:(你们有兴趣的话可以尝试一下,我的结果是o_de延迟了3clk,o_data没有延迟这么多) `timescale 1 ns/ 1 ps
module read_file_v #(
parameter SIZE = 8,
parameter DELAY = 3
)(
input clk,
input resetn,
input i_de,
output o_de,
output [SIZE-1:0] o_data
);
integer fp_in;
initial
begin
fp_in = $fopen("input//inY.hex","r");
end
integer fDataFlg;
reg [DELAY-1:0] ri_de ;
reg [DELAY*SIZE-1:0] ri_data;
always@(posedge clk or negedge resetn)
if(!resetn)begin
fDataFlg <= 32'd0;
ri_de[0] <= 32'd0;
ri_data[SIZE-1:0] <= 32'd0;
end
else if(!$feof(fp_in))begin
ri_de[0] <= i_de;
if(i_de)begin
fDataFlg <= $fscanf(fp_in,"%x",ri_data[SIZE-1:0]);
end
end
else begin
ri_de[0] <= i_de;
$fclose(fp_in);
end
//delay match
genvar i;
generate for(i=1;i
always@(posedge clk or negedge resetn)
if(!resetn)begin
ri_de <= 32'd0;
ri_data[(i+1)*SIZE-1:i*SIZE] <= 32'd0;
end
else begin
ri_de <= ri_de[i-1];
ri_data[(i+1)*SIZE-1:i*SIZE] <= ri_data[i*SIZE-1:(i-1)*SIZE];
end
end
endgenerate
assign o_data = ri_data[DELAY*SIZE-1:(DELAY-1)*SIZE];
assign o_de = ri_de[DELAY-1];
endmodule