首页->【超声成像实验室】

2 1

function [ res ] = ui_hex_wr_son(hexfilenamepath,d,words)
%UI_HEX_WR 此处显示有关此函数的摘要
%   此处显示详细说明
%   MATLAB 下用于生成Questasim Verilog HDL的ram仿真的hex文件
%   hex file generation
%   input 4B-32bits
%   function is ok
%   输入参数
    % hexfilenamepath :hex输出文件路径,
    % d :输入数据

    %words:输入数据的宽度

% 输出数据
    %res       : 操作状态

i=0;
fileID=fopen(hexfilenamepath,'wb');
%filepthID=fopen(datpth,'rb');

    if fileID == -1       
       res=-1;
       error('the file path is error ');
       
    end  
    [row,col]=size(d);
    indata=d;
    for cnti=1:row
        for cntj=1:col
            hexgenerate(fileID,i,indata(cnti,cntj),0,words);
            i=i+1;
        end
    end
    
endofhex(fileID);
%end of file
fclose(fileID);
%fclose(filepthID);

res=1;

end

function [chksum]=chkdatasum(data,wordcnts)
    if wordcnts == 2
        data=uint16(data);
        d1= uint16(bitshift(data,-8));
        d3= uint16(mod(data,uint16(hex2dec('100'))));
        d2=0;
        d4=0;
    else       
        d1= uint16(bitshift(data,-24));               %最高位的8
        tmp24=uint32(mod((data),hex2dec('1000000')));   %get 3WB eg FFFFFF
        d2= uint16(bitshift(tmp24,-16));
        tmp16= uint16(mod(tmp24,uint32(hex2dec('10000'))));
        d3= uint16(bitshift(tmp16,-8));
        d4= uint16(mod(tmp16,uint16(hex2dec('100'))));
    end
    
    chksum=uint16(mod((d1+d3+d2+d4),256));
    
end

function hexgenerate(fpathID,addr,data,type,datacnts)
    %datacnts=2;
    chksum0=uint16(datacnts);
    fprintf(fpathID,':%02X',datacnts);    %1 data cnts
    fprintf(fpathID,'%04X',addr);         %addr'
    fprintf(fpathID,'%02X',type);
    
    if datacnts == 2                 
        fprintf(fpathID,'%04X',data);         
    else
        fprintf(fpathID,'%08X',data);     
    end
    d0=chkdatasum(data,datacnts);
    d1=chkdatasum(addr,2);
    d2=mod(d0+d1+chksum0,256);  
    chksum= mod(d2,256);
    
    chksum=mod(chksum,256);
    fprintf(fpathID,'%02X',chksum);
    fprintf(fpathID,'\n');
    
end

function endofhex(fpathID)

   fprintf(fpathID,':%02X',hex2dec('0')); % length of data in this  row
   fprintf(fpathID,'%04X',hex2dec('0'));  % load offset
   fprintf(fpathID,'%02X',hex2dec('1'));  % endof file
   fprintf(fpathID,'%X',hex2dec('ff'));   % chksum
   
end


楼主可见

  1. shwnyoo 1#

    QuestaSim仿真用*.hex文件数据宽度为32-bit_matlab代码。