MATLAB生成.coe檔案和.mif檔案程式碼示例

zkf0100007發表於2018-08-14

.coe檔案和.mif檔案分別是XILINX和ALTERA用來初始ROM的檔案
生成.coe檔案的程式碼如下:

clear all;
close all;
clc;

BASE_FREQ = 450000000;
CARRIER = 90000000;
BAND = 50000;

fid_s = fopen('fm_tx_rom.coe', 'wt');
fprintf(fid_s, '%s\n', 'MEMORY_INITIALIZATION_RADIX = 16;');
fprintf(fid_s, '%s\n', 'MEMORY_INITIALIZATION_VECTOR =');

for (i=1:256)
    freq_wish = CARRIER + BAND*(i-1)/256;
    divider = BASE_FREQ / freq_wish;
    m=(divider-4)/(5-divider); 
    fprintf(fid_s, '%d', round(m));
    fprintf(fid_s, '%s', ',');
end
fclose(fid_s);
disp('===================轉換完成=========================');

生成.mif檔案的程式碼如下:

clear all;
close all;
clc;

BASE_FREQ = 450000000;
CARRIER = 90000000;
BAND = 50000;

fid_s = fopen('fm_tx_rom.mif', 'wt');
fprintf(fid_s, '%s\n', 'DEPTH = 256;');
fprintf(fid_s, '%s\n', 'WIDTH =16;');
fprintf(fid_s, '%s\n', 'ADDRESS_RADIX = UNS;');%UNS表示為十進位制
fprintf(fid_s, '%s\n', 'DATA_RADIX = UNS;');
fprintf(fid_s, '%s\n', 'CONTENT BEGIN');

for (i=1:256)
    freq_wish = CARRIER + BAND*(i-1)/256;
    divider = BASE_FREQ / freq_wish;
    m=(divider-4)/(5-divider); 
    fprintf(fid_s, '%03d', i-1);
    fprintf(fid_s, '%s', ' : ');
    fprintf(fid_s, '%d', round(m));
    fprintf(fid_s, '%s\n', ';');
end
fclose(fid_s);
disp('===================轉換完成=========================');

相關文章