從Matlab到FPGA(Matlab生成coe檔案或mem檔案)
當定點模擬完成後,就需要使用FPGA實現。
這時候需要把之前仿好的濾波器引數或者輸入訊號輸出為coes檔案:
%% output coe file
Ff = fimath('CastBeforeSum', 0, 'OverflowMode', 'Saturate', ...
'RoundMode', 'round', 'ProductMode', 'SpecifyPrecision', 'SumMode', 'SpecifyPrecision', ...
'ProductWordLength', 16, 'ProductFractionLength', 15, 'SumWordLength', 16, 'SumFractionLength', 15);
Tf = numerictype('WordLength', 16, 'FractionLength', 15);
wr = 'ram'; % select the output type
outset = fi(hmc.Numerator, Tf, Ff); % the filter need to be outputted, datatype:fi
outset_len = length(outset);
fp = fopen('D:\tmp\fpga_matlab\FIR.coe','w');
if( strcmp(wr, 'ram') )
fprintf( fp, 'MEMORY_INITIALIZATION_RADIX=16;\n' );
fprintf( fp, 'MEMORY_INITIALIZATION_VECTOR=\n' );
elseif( strcmp(wr, 'fir') )
fprintf( fp, 'radix=10;\n' );
fprintf( fp, 'coefdata=\n' );
end
for i = 1:outset_len
out = outset(i);
if( strcmp(wr, 'ram') )
out_r = real(out); %ram
out_i = imag(out);
fprintf(fp, '%s%s\n', out_i.hex, out_r.hex);
elseif( strcmp(wr, 'fir') )
out = outset(i); %fir
fprintf(fp, '%.17f\n', out);
end
end
fclose(fp);
由於濾波器在模擬中一個結構體,所以把裡面的引數變成fi物件,這樣可以很方便輸出十六進位制或者二進位制之類。
這裡程式碼還提供了RAM的coe檔案輸出的選擇。
這樣,使用IP核的時候就可以把這些檔案拖進去。
也可以使用原語的方式實現RAM,這樣可以很方便地移植程式碼,如下:
xpm_memory_sdpram # (
// Common module parameters
.MEMORY_SIZE (4096), //positive integer
.MEMORY_PRIMITIVE ("auto"), //string; "auto", "distributed", "block" or "ultra";
.CLOCKING_MODE ("common_clock"), //string; "common_clock", "independent_clock"
.MEMORY_INIT_FILE (file), //string; "none" or "<filename>.mem"
.MEMORY_INIT_PARAM ("" ), //string;
.USE_MEM_INIT (1), //integer; 0,1
.WAKEUP_TIME ("disable_sleep"), //string; "disable_sleep" or "use_sleep_pin"
.MESSAGE_CONTROL (0), //integer; 0,1
.ECC_MODE ("no_ecc"), //string; "no_ecc", "encode_only", "decode_only" or "both_encode_and_decode"
.AUTO_SLEEP_TIME (0), //Do not Change
// Port A module parameters
.WRITE_DATA_WIDTH_A (16), //positive integer
.BYTE_WRITE_WIDTH_A (16), //integer; 8, 9, or WRITE_DATA_WIDTH_A value
.ADDR_WIDTH_A (8), //positive integer
// Port B module parameters
.READ_DATA_WIDTH_B (16), //positive integer
.ADDR_WIDTH_B (8), //positive integer
.READ_RESET_VALUE_B ("0"), //string
.READ_LATENCY_B (2), //non-negative integer
.WRITE_MODE_B ("no_change") //string; "write_first", "read_first", "no_change"
) xpm_memory_sdpram_inst (
// Common module ports
.sleep (1'b0),
// Port A module ports
.clka (clk),
.ena (1'b1),
.wea (enw),
.addra (waddr),
.dina (wdata),
.injectsbiterra (1'b0),
.injectdbiterra (1'b0),
// Port B module ports
.clkb (clk),
.rstb (1'b0),
.enb (1'b1),
.regceb (1'b1),
.addrb (raddr),
.doutb (rdata),
.sbiterrb (),
.dbiterrb ()
);
這樣就例化了一個讀寫RAM這裡RAM的初始化資料是mem檔案,可以用如下Matlab程式碼輸出:
outset = [0;ER1];
outset = fi(outset, T, F); % the filter need to be outputted, datatype:fi
outset_len = length(outset);
fp = fopen('D:\tmp\fpga_matlab\ER1.mem','w');
for i = 1:outset_len
out = outset(i);
fprintf(fp, '@%04x %04x\n', (i-1), out.int);
end
fclose(fp);
使用Verilog測試時,需要讀取txt檔案的資料輸入模擬的模組,並把輸出的資料儲存到txt再用Matlab讀取出來分析,Matlab側輸出資料到txt的程式碼如下:
%% output fi object
outset = RR;%fi(Tx, T, F); % the filter need to be outputted, datatype:fi
outset_len = length(outset);
% if(outset_len>10000)
% outset_len = 10000;
% end
fp = fopen('D:\tmp\fpga_matlab\Tx.txt','w');
for i = 1:outset_len
out = outset(i);
r_out = real(out);
i_out = imag(out);
fprintf(fp, '%s %s\n', r_out.dec, i_out.dec);
end
fclose(fp);
Matlab側讀取txt的程式碼為:
%% input fi object
fp = fopen('D:\tmp\fpga_matlab\Rx.txt','r');
in = textscan(fp, '%s');
fclose(fp);
ins = in{1,1};
in_l = length(ins);
rx = zeros( in_l/2, 1 );
for i = 1:2:in_l
in_r = str2double(ins{i});
in_i = str2double(ins{i+1});
rx( (i+1)/2 ) = in_r/(2^14) + 1j*in_i/(2^14);
end
對應的verilog程式碼如下:integer fp_r, fp_w, cnt, r_n, w_n;
// read data
initial begin
fp_r = $fopen("D:/tmp/fpga_matlab/Tx1.txt", "r");
r_n = 0;
s_axis_data_tvalid = 0;
s_axis_data_tdata = 0;
wait(rst_n==1);
@(posedge clk)
while(!$feof(fp_r)) begin
#1
cnt = $fscanf(fp_r, "%d %d", data_in_I, data_in_Q);
r_n = r_n + 1;
s_axis_data_tdata = { data_in_Q[15:0], data_in_I[15:0] };
s_axis_data_tvalid = 1;
@(posedge clk);
#1
s_axis_data_tvalid = 0;
#(1*PERIOD);
end
s_axis_data_tvalid = 0;
s_axis_data_tdata = 0;
$fclose(fp_r);
end
// write data
initial begin
fp_w = $fopen("D:/tmp/fpga_matlab/Tx2_fpga.txt","w");//以寫的方式開啟檔案
w_n = 0;
@(posedge m_axis_data_tvalid)
while(m_axis_data_tvalid)
begin
@(posedge clk)
data_out_I = m_axis_data_tdata[15:0];
data_out_Q = m_axis_data_tdata[31:16];
$fwrite(fp_w,"%d %d\n", $signed(data_out_I), $signed(data_out_Q));
w_n = w_n + 1;
end
$fclose(fp_w);
end
相關文章
- MATLAB生成coe檔案Matlab
- matlab寫coe檔案Matlab
- MATLAB生成.coe檔案和.mif檔案程式碼示例Matlab
- 使用matlab生成rom初始化檔案.coeMatlab
- 利用MATLAB產生COE檔案Matlab
- Matlab生成exe檔案Matlab
- Matlab 生成 圖片的coe檔案初始化xilinx romMatlab
- 用MATLAB編寫VIVADO的coe檔案Matlab
- Matlab生成.exe格式檔案Matlab
- 用MATLAB產生VIVADO可用的coe檔案Matlab
- MATLAB生成可執行檔案Matlab
- Matlab生成Xilinx Rom IP CORE的初始化內容coe檔案Matlab
- matlab (.m)檔案生成 windows 可執行(.exe)檔案MatlabWindows
- Xilinx COE檔案生成
- matlab開啟和生成使用.p檔案和exe檔案Matlab
- matlab輸出複數到檔案Matlab
- matlab讀取npy檔案Matlab
- Matlab製作shp檔案Matlab
- Matlab將.mat檔案儲存為.txt檔案Matlab
- matlab生成exe獨立執行檔案已破解(好用)Matlab
- MATLAB快速讀取STL檔案Matlab
- COE檔案是什麼
- matlab輸出資料為excel檔案MatlabExcel
- 使用MATLAB開啟.bdf腦電檔案Matlab
- matlab之對檔案路徑的表示Matlab
- 【MATLAB】讀取和寫入文字檔案Matlab
- matlab 資料檔案MAT的讀與寫Matlab
- 從C檔案到可執行elf檔案
- matlab遍歷資料夾下的所有檔案Matlab
- Matlab批量實現圖片檔案格式轉換Matlab
- Matlab檔案IO--文字與數值的讀Matlab
- Matlab檔案IO--文字與數值的寫Matlab
- Octave matlab中執行.m檔案方式對比Matlab
- ar——建立或修改備存檔案,或是從備存檔案中抽取檔案
- MATLAB實戰系列(四)-匯入txt檔案技巧大全Matlab
- RM-2 使用matlab進行txt檔案讀寫Matlab
- 用RMAN遷移檔案到ASM或從ASM遷出ASM
- coe檔案資料後的逗號