hex文件读取任务和应用实例(systemverilog)
时间:10-02
整理:3721RD
点击:
用于初始化存储器,省的再用工具进行hex->bin转换,自己改写的,出了什么问题还请各位指教
`define rom_size 16'hffff
module irom64kx8(
input[15:0] irom_addr,
inputirom_rd_n,
inputirom_cs_n,
output reg [7:0] irom_data_out
);
logic [7:0] rom_data[`rom_size];
logic [7:0] temp;
int i=0;
int h_mt;
initial begin
read_hex_file("../soft/main.hex",rom_data);
end
always @(*) begin
irom_data_out<=((~irom_rd_n) && (~irom_cs_n))? rom_data[irom_addr]:8'bZ;
end
task read_hex_file(input string file_name , output [7:0] rom_data[`rom_size]);
logic [7:0]column;
logic [15:0] address;
logic [7:0] len;
logic [7:0] dat;
logic [7:0] check_sum;
int i,fd,code;
//initial all data with zero
foreach(rom_data)
rom_data=0;
fd=$fopen(file_name,"r");
if(fd==0) begin
$display("hex file not found,please check file path and file name!");
$stop;// stop when no such file
end
while(~$feof(fd)) begin
//find ":" at the beginning of a record
while(1) begin
if($feof(fd))begin
$fclose(fd);
return;
end
column=$fgetc(fd);
if(column==":") break;// every line begin with ":" int the IntelHex form
end
code=$fscanf(fd,"%2x",len);
if(len==0) return;// stop when no data
check_sum=len;
code=$fscanf(fd,"%4x",address);
check_sum=check_sum+address;
check_sum=check_sum+(address>>8);// unsigned check_sum
code=$fscanf(fd,"%2x",dat);// data type
check_sum=check_sum+dat;
for(i=0;i<len;i=i+1) begin
code=$fscanf(fd,"%2x",dat);
check_sum=check_sum+dat;
rom_data[address]=dat;// rom_data read data from file
//$display("rom_data[%6d]=%2h",address,dat);
address=address+1;
end
code=$fscanf(fd,"%2x",dat);// check data
check_sum=check_sum+dat;
if(check_sum!=0) begin
$display("hex file error:checkcheck_sum is not zero!");
$stop;
end
end
endtask:read_hex_file
endmodule
`define rom_size 16'hffff
module irom64kx8(
input[15:0] irom_addr,
inputirom_rd_n,
inputirom_cs_n,
output reg [7:0] irom_data_out
);
logic [7:0] rom_data[`rom_size];
logic [7:0] temp;
int i=0;
int h_mt;
initial begin
read_hex_file("../soft/main.hex",rom_data);
end
always @(*) begin
irom_data_out<=((~irom_rd_n) && (~irom_cs_n))? rom_data[irom_addr]:8'bZ;
end
task read_hex_file(input string file_name , output [7:0] rom_data[`rom_size]);
logic [7:0]column;
logic [15:0] address;
logic [7:0] len;
logic [7:0] dat;
logic [7:0] check_sum;
int i,fd,code;
//initial all data with zero
foreach(rom_data)
rom_data=0;
fd=$fopen(file_name,"r");
if(fd==0) begin
$display("hex file not found,please check file path and file name!");
$stop;// stop when no such file
end
while(~$feof(fd)) begin
//find ":" at the beginning of a record
while(1) begin
if($feof(fd))begin
$fclose(fd);
return;
end
column=$fgetc(fd);
if(column==":") break;// every line begin with ":" int the IntelHex form
end
code=$fscanf(fd,"%2x",len);
if(len==0) return;// stop when no data
check_sum=len;
code=$fscanf(fd,"%4x",address);
check_sum=check_sum+address;
check_sum=check_sum+(address>>8);// unsigned check_sum
code=$fscanf(fd,"%2x",dat);// data type
check_sum=check_sum+dat;
for(i=0;i<len;i=i+1) begin
code=$fscanf(fd,"%2x",dat);
check_sum=check_sum+dat;
rom_data[address]=dat;// rom_data read data from file
//$display("rom_data[%6d]=%2h",address,dat);
address=address+1;
end
code=$fscanf(fd,"%2x",dat);// check data
check_sum=check_sum+dat;
if(check_sum!=0) begin
$display("hex file error:checkcheck_sum is not zero!");
$stop;
end
end
endtask:read_hex_file
endmodule
花童装出售
支持。support!
where buy flower girl dresses online?
i'm a bride, will marry next 2 month, i have bought my wedding dress, now i like to know where we buy my flowr girl dresses and prom dresses?
we saw jorma wedding dresses factory, anybody know jorma?
who can help me?
bridal gowns
学习了!
学习了!
环锤式破碎机厂 环锤式破碎机价格
有必要那么复杂吗?直接调用$readmemh不行吗