Skip to content

Instantly share code, notes, and snippets.

@Enoch2090
Created March 20, 2020 10:45
Show Gist options
  • Save Enoch2090/a20577ad753147e26d5da34f75e71879 to your computer and use it in GitHub Desktop.
Save Enoch2090/a20577ad753147e26d5da34f75e71879 to your computer and use it in GitHub Desktop.
LUT generator
function success=LUT_generator(num,mode)
% --------参数设置--------
indent = ' ';
format long g
digits(16)
factor = 1.94106625850464;
% --------查找表设置--------
switch num
% 原始的查找表
case 0
LUT_RE = [0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4];
LUT_IM = [0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4];
case 2
LUT_RE = [0,1,-1,0,-1,0,-2,-1,1,2,0,1,0,1,-1,0];
LUT_IM = [0,-1,1,0,-1,-2,0,-1,1,0,2,1,0,-1,1,0];
case 4
LUT_RE = [0,-1,1,0,-1,-2,0,-1,1,0,2,1,0,-1,1,0];
LUT_IM = [0,-1,1,0,-1,-2,0,-1,1,0,2,1,0,-1,1,0];
case 6
LUT_RE = [0 -1 1 0 -1 -2 0 -1 1 0 2 1 0 -1 1 0];
LUT_IM = [0 -1 1 0 1 0 2 1 -1 -2 0 -1 0 -1 1 0];
case 1
LUT_RE = [0 -0.707106781187 -1 -1.707106781187 -0.707106781187 -1.414213562374 -1.707106781187 -2.414213562374 -0.707106781187 -1.414213562374 -1.707106781187 -2.414213562374 -1.414213562374 -2.121320343561 -2.414213562374 -3.121320343561 0.707106781187 0 -0.292893218813 -1 0 -0.707106781187 -1 -1.707106781187 0 -0.707106781187 -1 -1.707106781187 -0.707106781187 -1.414213562374 -1.707106781187 -2.414213562374 1 0.292893218813 0 -0.707106781187 0.292893218813 -0.414213562374 -0.707106781187 -1.414213562374 0.292893218813 -0.414213562374 -0.707106781187 -1.414213562374 -0.414213562374 -1.121320343561 -1.414213562374 -2.121320343561 1.707106781187 1 0.707106781187 0 1 0.292893218813 0 -0.707106781187 1 0.292893218813 0 -0.707106781187 0.292893218813 -0.414213562374 -0.707106781187 -1.414213562374];
LUT_IM = [0 -0.707106781187 0.707106781187 0 1 0.292893218813 1.707106781187 1 0.707106781187 0 1.414213562374 0.707106781187 1.707106781187 1 2.414213562374 1.707106781187 1 0.292893218813 1.707106781187 1 2 1.292893218813 2.707106781187 2 1.707106781187 1 2.414213562374 1.707106781187 2.707106781187 2 3.414213562374 2.707106781187 0.707106781187 0 1.414213562374 0.707106781187 1.707106781187 1 2.414213562374 1.707106781187 1.414213562374 0.707106781187 2.121320343561 1.414213562374 2.414213562374 1.707106781187 3.121320343561 2.414213562374 1.707106781187 1 2.414213562374 1.707106781187 2.707106781187 2 3.414213562374 2.707106781187 2.414213562374 1.707106781187 3.121320343561 2.414213562374 3.414213562374 2.707106781187 4.121320343561 3.414213562374];
case 3
LUT_RE = [0 -0.707106781187 1 0.292893218813 -0.707106781187 -1.414213562374 0.292893218813 -0.414213562374 0.707106781187 0 1.707106781187 1 0 -0.707106781187 1 0.292893218813 -0.707106781187 -1.414213562374 0.292893218813 -0.414213562374 -1.414213562374 -2.121320343561 -0.414213562374 -1.121320343561 0 -0.707106781187 1 0.292893218813 -0.707106781187 -1.414213562374 0.292893218813 -0.414213562374 1 0.292893218813 2 1.292893218813 0.292893218813 -0.414213562374 1.292893218813 0.585786437626 1.707106781187 1 2.707106781187 2 1 0.292893218813 2 1.292893218813 0.292893218813 -0.414213562374 1.292893218813 0.585786437626 -0.414213562374 -1.121320343561 0.585786437626 -0.121320343561 1 0.292893218813 2 1.292893218813 0.292893218813 -0.414213562374 1.292893218813 0.585786437626];
LUT_IM = [0 0.707106781187 -0.707106781187 0 1 1.707106781187 0.292893218813 1 0.707106781187 1.414213562374 0 0.707106781187 1.707106781187 2.414213562374 1 1.707106781187 -1 -0.292893218813 -1.707106781187 -1 0 0.707106781187 -0.707106781187 0 -0.292893218813 0.414213562374 -1 -0.292893218813 0.707106781187 1.414213562374 0 0.707106781187 0.707106781187 1.414213562374 0 0.707106781187 1.707106781187 2.414213562374 1 1.707106781187 1.414213562374 2.121320343561 0.707106781187 1.414213562374 2.414213562374 3.121320343561 1.707106781187 2.414213562374 -0.292893218813 0.414213562374 -1 -0.292893218813 0.707106781187 1.414213562374 0 0.707106781187 0.414213562374 1.121320343561 -0.292893218813 0.414213562374 1.414213562374 2.121320343561 0.707106781187 1.414213562374];
case 5
LUT_RE = [0 0.707106781187 -1 -0.292893218813 0.707106781187 1.414213562374 -0.292893218813 0.414213562374 0.707106781187 1.414213562374 -0.292893218813 0.414213562374 1.414213562374 2.121320343561 0.414213562374 1.121320343561 -0.707106781187 0 -1.707106781187 -1 0 0.707106781187 -1 -0.292893218813 0 0.707106781187 -1 -0.292893218813 0.707106781187 1.414213562374 -0.292893218813 0.414213562374 1 1.707106781187 0 0.707106781187 1.707106781187 2.414213562374 0.707106781187 1.414213562374 1.707106781187 2.414213562374 0.707106781187 1.414213562374 2.414213562374 3.121320343561 1.414213562374 2.121320343561 0.292893218813 1 -0.707106781187 0 1 1.707106781187 0 0.707106781187 1 1.707106781187 0 0.707106781187 1.707106781187 2.414213562374 0.707106781187 1.414213562374];
LUT_IM = [0 0.707106781187 -0.707106781187 0 1 1.707106781187 0.292893218813 1 -0.707106781187 0 -1.414213562374 -0.707106781187 0.292893218813 1 -0.414213562374 0.292893218813 1 1.707106781187 0.292893218813 1 2 2.707106781187 1.292893218813 2 0.292893218813 1 -0.414213562374 0.292893218813 1.292893218813 2 0.585786437626 1.292893218813 -0.707106781187 0 -1.414213562374 -0.707106781187 0.292893218813 1 -0.414213562374 0.292893218813 -1.414213562374 -0.707106781187 -2.121320343561 -1.414213562374 -0.414213562374 0.292893218813 -1.121320343561 -0.414213562374 0.292893218813 1 -0.414213562374 0.292893218813 1.292893218813 2 0.585786437626 1.292893218813 -0.414213562374 0.292893218813 -1.121320343561 -0.414213562374 0.585786437626 1.292893218813 -0.121320343561 0.585786437626];
case 7
LUT_RE = [0 0.707106781187 1 1.707106781187 0.707106781187 1.414213562374 1.707106781187 2.414213562374 -0.707106781187 0 0.292893218813 1 0 0.707106781187 1 1.707106781187 0.707106781187 1.414213562374 1.707106781187 2.414213562374 1.414213562374 2.121320343561 2.414213562374 3.121320343561 0 0.707106781187 1 1.707106781187 0.707106781187 1.414213562374 1.707106781187 2.414213562374 1 1.707106781187 2 2.707106781187 1.707106781187 2.414213562374 2.707106781187 3.414213562374 0.292893218813 1 1.292893218813 2 1 1.707106781187 2 2.707106781187 1.707106781187 2.414213562374 2.707106781187 3.414213562374 2.414213562374 3.121320343561 3.414213562374 4.121320343561 1 1.707106781187 2 2.707106781187 1.707106781187 2.414213562374 2.707106781187 3.414213562374];
LUT_IM = [0 -0.707106781187 0.707106781187 0 1 0.292893218813 1.707106781187 1 -0.707106781187 -1.414213562374 0 -0.707106781187 0.292893218813 -0.414213562374 1 0.292893218813 -1 -1.707106781187 -0.292893218813 -1 0 -0.707106781187 0.707106781187 0 -1.707106781187 -2.414213562374 -1 -1.707106781187 -0.707106781187 -1.414213562374 0 -0.707106781187 -0.707106781187 -1.414213562374 0 -0.707106781187 0.292893218813 -0.414213562374 1 0.292893218813 -1.414213562374 -2.121320343561 -0.707106781187 -1.414213562374 -0.414213562374 -1.121320343561 0.292893218813 -0.414213562374 -1.707106781187 -2.414213562374 -1 -1.707106781187 -0.707106781187 -1.414213562374 0 -0.707106781187 -2.414213562374 -3.121320343561 -1.707106781187 -2.414213562374 -1.414213562374 -2.121320343561 -0.707106781187 -1.414213562374];
otherwise
fprintf("LUT Num Error in %d%s", num, mode);
end % 选表
if mode == 'RE'
LUT = LUT_RE*factor;
else
LUT = LUT_IM*factor;
end
fdt_LUT = numerictype(1,16,12);
success = 0;
len = int32(log2(length(LUT)));
fdt = numerictype(0,len,0);
code_str = "";
code_str = code_str+'`timescale 1ns / 1ps'+newline+'module LUT_'+string(len)+mode+'('+newline+indent+'input ['+string(len-1)+':0] IN,'+newline+indent+'output reg [15:0] OUT'+newline+indent+');'+newline+newline+indent+'always @(IN) begin'+newline+indent+indent+'case(IN)'+newline;
for i = 1:1:length(LUT) % 拼合代码
code_str = code_str+indent+indent+indent+len+'''b'+fi(i,fdt).bin+': OUT=16''b'+fi(LUT(i),fdt_LUT).bin+';'+' //'+string(vpa(LUT(i)))+newline;
end
code_str = code_str+indent+indent+indent+'default: OUT=16''b0000000000000000;'+newline;
code_str = code_str+indent+indent+'endcase'+newline+indent+'end'+newline+'endmodule';
file = 'LUT_'+string(num)+'_'+mode+'.v';
fp=fopen(file,'a');
fprintf(fp,'%s',code_str);
fclose(fp);
% disp(code_str);
% clipboard('copy',code_str);
success = 1;
end
@Enoch2090
Copy link
Author

Use MATLAB to generate a Verilog LUT.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment