A simple vhdl fir description.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

264 lines
11KB

  1. LIBRARY ieee;
  2. USE ieee.std_logic_1164.ALL;
  3. USE ieee.numeric_std.ALL;
  4. USE ieee.std_logic_unsigned.ALL;
  5. USE ieee.std_logic_signed.ALL;
  6. PACKAGE GENERAL_INCLUDES IS
  7. FUNCTION log2_sup_integer (number : natural) RETURN natural;
  8. FUNCTION log2_inf_integer (number : natural) RETURN natural;
  9. -- -- CONSTANTS -- --
  10. -- ADC
  11. CONSTANT cst_w_in : natural := 6; -- ADC in bitwidth
  12. CONSTANT cst_w_out : natural := 19;
  13. CONSTANT cst_nb_samples_adc_in : natural := 10; -- ADC in nb samples
  14. -- FILTER
  15. --coefficients
  16. CONSTANT cst_w_coeff : natural := 8; -- coeffs bitwidth
  17. CONSTANT cst_nb_coeffs_filter_in : natural := 20*10;
  18. CONSTANT cst_log2_sup_nb_coeffs_subfilter_in: natural:= 5;
  19. -- FIR
  20. -- POLYPHASE FILTER
  21. CONSTANT cst_nb_subfilters : natural := 10;
  22. -- -- CALCULATIONS -- --
  23. -- SHIFT REG
  24. CONSTANT cst_nb_coeffs_subfilter_in : natural := cst_nb_coeffs_filter_in/cst_nb_subfilters;
  25. CONSTANT cst_nb_samples_shiftreg_temp_in : natural := cst_nb_coeffs_subfilter_in + cst_nb_samples_adc_in;
  26. -- mult
  27. CONSTANT cst_w_mult_out : natural := cst_w_coeff+cst_w_in;
  28. -- adder
  29. CONSTANT cst_log2_adder_stages : natural := cst_log2_sup_nb_coeffs_subfilter_in;
  30. -- fir
  31. CONSTANT cst_w_fir_adder_out : natural := cst_w_mult_out+cst_log2_adder_stages;
  32. -- TYPES
  33. -- ADC
  34. SUBTYPE smpl_adc_data_in IS std_logic_vector(cst_w_in-1 DOWNTO 0);
  35. SUBTYPE smpl_fir_data_out IS std_logic_vector(cst_w_out-1 DOWNTO 0);
  36. -- SHIFT REG
  37. TYPE vect_adc_data_out IS ARRAY (0 TO cst_nb_samples_adc_in-1) OF smpl_adc_data_in;
  38. TYPE vect_fir_data_in IS ARRAY(0 TO cst_nb_coeffs_subfilter_in-1) OF smpl_adc_data_in;
  39. TYPE vect_reg_data IS ARRAY(0 TO cst_nb_samples_shiftreg_temp_in-1) OF smpl_adc_data_in;
  40. TYPE matrix_reg_data_out IS ARRAY(0 TO cst_nb_coeffs_subfilter_in-1) OF vect_fir_data_in;
  41. -- FILTER
  42. SUBTYPE smpl_coeff IS std_logic_vector(cst_w_coeff-1 DOWNTO 0);
  43. -- mult
  44. SUBTYPE smpl_mult_data_out IS std_logic_vector(cst_w_mult_out-1 DOWNTO 0);
  45. SUBTYPE smpl_mult_data_out_signed IS signed(cst_w_mult_out-1 DOWNTO 0);
  46. SUBTYPE smpl_coeffs_signed IS signed(cst_w_coeff-1 DOWNTO 0);
  47. SUBTYPE smpl_mult_data_in_signed IS signed(cst_w_in-1 DOWNTO 0);
  48. TYPE vect_data_mult_in_signed IS ARRAY(0 TO cst_nb_coeffs_subfilter_in-1) OF smpl_mult_data_in_signed;
  49. TYPE vect_fir_coeffs_in IS ARRAY(0 TO cst_nb_coeffs_subfilter_in-1) OF smpl_coeff ;
  50. TYPE vect_mult_coeffs_signed IS ARRAY(0 TO cst_nb_coeffs_subfilter_in-1) OF smpl_coeffs_signed;
  51. TYPE vect_mult_data_out IS ARRAY(0 TO cst_nb_coeffs_subfilter_in-1) OF smpl_mult_data_out;
  52. TYPE vect_mult_data_out_signed IS ARRAY(0 TO cst_nb_coeffs_subfilter_in-1) OF smpl_mult_data_out_signed;
  53. -- adder
  54. TYPE vect_adder_generic IS ARRAY(0 TO 2**(cst_log2_adder_stages)-1) OF std_logic_vector(cst_w_fir_adder_out-1 DOWNTO 0);
  55. TYPE vect_adder_generic_signed IS ARRAY(0 TO 2**(cst_log2_adder_stages)-1) OF signed(cst_w_fir_adder_out-1 DOWNTO 0);
  56. TYPE matrix_adder_generic IS ARRAY(0 TO cst_log2_adder_stages) OF vect_adder_generic;
  57. TYPE matrix_adder_generic_signed IS ARRAY(0 TO cst_log2_adder_stages) OF vect_adder_generic_signed;
  58. -- fir
  59. SUBTYPE smpl_fir_adder_data_out IS std_logic_vector(cst_w_fir_adder_out-1 DOWNTO 0);
  60. TYPE vect_fir_adder_data_out IS ARRAY (0 TO cst_nb_samples_adc_in-1) OF smpl_fir_adder_data_out;
  61. TYPE vect_fir_data_out IS array(0 TO cst_nb_samples_adc_in-1) OF smpl_fir_data_out;
  62. -- POLYPHASE FILTER
  63. -- TYPE matrix_coeffs_polyphase_filter IS array(0 to cst_nb_subfilters) OF vect_mult_coeffs;
  64. -- ---- CONSTANTS ----
  65. --
  66. -- -- data
  67. -- CONSTANT w_x : natural := 6; -- input samples bitwidth
  68. -- CONSTANT w_y : natural := 6; -- output data bitwidth
  69. --
  70. --
  71. -- -- filter
  72. -- CONSTANT filer_nb_coeffs : natural := 1000; -- total nb coefficients
  73. -- CONSTANT filter_nb_channels : natural := 20; -- nb subfilters
  74. -- CONSTANT w_coeffs : natural := 8; --
  75. --
  76. -- -- signal processing properties
  77. -- CONSTANT downsampling_factor : natural := 8;
  78. --
  79. --
  80. --
  81. -- ---- CALCULATIONS ----
  82. --
  83. --
  84. -- VARIABLE subfilter_coeffs_nb_temp : natural := filer_nb_coeffs/filter_nb_channels;
  85. -- if(2**(log2_sup_integer(subfilter_coeffs_nb_temp)) != subfilter_coeffs_nb_temp) then -- if not power of 2
  86. -- CONSTANT subfilter_coeffs_nb : natural := 2**(log2_sup_integer(subfilter_coeffs_nb_temp);
  87. -- else
  88. -- CONSTANT subfilter_coeffs_nb : natural := subfilter_coeffs_nb_temp;
  89. -- end if;
  90. --
  91. -- CONSTANT log_subfilter_coeffs_nb : natural := log2_sup_integer(subfilter_coeffs_nb);
  92. --
  93. -- -- FIR
  94. -- --multplier
  95. -- CONSTANT data_mult_in_w : natural := w_x;
  96. -- CONSTANT data_mult_out_w : natural := w_x+w_coeffs+1;
  97. -- --adder
  98. -- CONSTANT data_add_in_w : natural := data_mult_out_w;
  99. -- CONSTANT data_P_G_stages : natural := natural(conv_std_logic_vector(data_add_in_w)'LENGTH) -- ca marche?
  100. -- CONSTANT data_add_out_w : natural := data_add_in_w+log_subfilter_coeffs_nb;
  101. --
  102. --
  103. -- ---- TYPES ----
  104. --
  105. -- -- filter coeffs
  106. -- TYPE coeff IS std_logic_vector(w_coeffs-1 downto 0);
  107. -- TYPE coeff_signed IS signed(w_coeffs-1 downto 0);
  108. -- TYPE subfilter_coefficients IS array(0 to subfilter_coeffs_nb-1) OF coeff;
  109. -- TYPE polyphase_filter_matrix IS array(0 to filter_nb_channels-1) OF subfilter_coefficients;
  110. --
  111. -- -- polyphase fill
  112. -- polyphase_filter_matrix(others =>(others => 0); -- init;
  113. --
  114. -- for subfilter_nth_coeff in (0 to subfilter_coeffs_nb-1) loop -- fill subfilters
  115. -- for lines in (0 to filter_nb_channels-1) loop
  116. -- polyphase_filter_matrix(lines => (subfilter_nth_coeff => filter(lines*filter_nb_channels+subfilter_nth_coeff));
  117. -- end loop;
  118. -- end loop;
  119. --
  120. -- -- FIR
  121. -- TYPE fir_data_in IS std_logic_vector(w_x-1 downto 0);
  122. -- TYPE fir_data_out IS std_logic_vector(w_x+log_subfilter_coeffs_nb-1 downto 0);
  123. -- --reg
  124. -- TYPE data_reg_in IS data_in;
  125. -- --multiplier
  126. -- TYPE data_mult_in IS std_logic_vector(data_mult_in_w-1 downto 0);
  127. -- TYPE data_mult_in_signed IS signed(data_mult_in_w-1 downto 0);
  128. -- TYPE data_mult_out IS std_logic_vector(data_mult_out_w-1 downto 0);
  129. -- TYPE data_mult_out_signed IS signed(data_mult_out_w-1 downto 0);
  130. -- --adder
  131. -- TYPE data_add_in IS data_mult_out;
  132. -- TYPE data_add_out IS STD_LOGIC_VECTOR(data_add_out_w-1 downto 0);
  133. -- --tree
  134. -- TYPE mult_add_stage_array IS array(0 to subfilter_coeffs_nb-1) OF data_add_out;
  135. -- TYPE mult_add_tree_matrix IS array(0 to log_subfilter_coeffs_nb) OF mult_add_stage_array;
  136. -- --DFT
  137. -- TYPE input_DFT_data IS data_add_out;
  138. -- TYPE output_DFT_data IS
  139. --
  140. -- --TYPE step_nb_channels IS array (0 to filter_nb_channels-1) of signed (w_x-1 downto 0);
  141. -- --TYPE filter_coeffs IS array (0 to filer_nb_coeffs-1) of signed (w_x-1 downto 0);
  142. -- --TYPE type_data_fir IS signed(w_x-1 downto 0);
  143. -- --TYPE type_coeff_fir IS signed
  144. END;
  145. PACKAGE BODY GENERAL_INCLUDES IS
  146. --functions
  147. FUNCTION log2_sup_integer (number : natural) RETURN natural IS
  148. VARIABLE result : natural;
  149. BEGIN
  150. IF(number <= 1) THEN
  151. result := 0;
  152. ELSIF(number = 2) THEN
  153. result := 1;
  154. ELSIF(number > 2 AND number <= 4) THEN
  155. result := 2;
  156. ELSIF(number > 4 AND number <= 8) THEN
  157. result := 3;
  158. ELSIF(number > 8 AND number <= 16) THEN
  159. result := 4;
  160. ELSIF(number > 16 AND number <= 32) THEN
  161. result := 5;
  162. ELSIF(number > 32 AND number <= 64) THEN
  163. result := 6;
  164. ELSIF(number > 64 AND number <= 128) THEN
  165. result := 7;
  166. ELSIF(number > 128 AND number <= 256) THEN
  167. result := 8;
  168. ELSIF(number > 256 AND number <= 512) THEN
  169. result := 9;
  170. ELSIF(number > 512 AND number <= 1024) THEN
  171. result := 10;
  172. ELSIF(number > 1024 AND number <= 2048) THEN
  173. result := 11;
  174. ELSIF(number > 2048 AND number <= 4096) THEN
  175. result := 12;
  176. ELSIF(number > 4096 AND number <= 8192) THEN
  177. result := 13;
  178. ELSIF(number > 8192 AND number <= 16384) THEN
  179. result := 14;
  180. ELSIF(number > 16384 AND number <= 32768) THEN
  181. result := 15;
  182. END IF;
  183. RETURN result;
  184. END FUNCTION;
  185. FUNCTION log2_inf_integer (number : natural) RETURN natural IS
  186. VARIABLE result : natural;
  187. BEGIN
  188. IF(number < 2) THEN
  189. result := 0;
  190. ELSIF(number >= 2 AND number < 4) THEN
  191. result := 1;
  192. ELSIF(number >= 4 AND number < 8) THEN
  193. result := 2;
  194. ELSIF(number >= 8 AND number < 16) THEN
  195. result := 3;
  196. ELSIF(number >= 16 AND number < 32) THEN
  197. result := 4;
  198. ELSIF(number >= 32 AND number < 64) THEN
  199. result := 5;
  200. ELSIF(number >= 64 AND number < 128) THEN
  201. result := 6;
  202. ELSIF(number >= 128 AND number < 256) THEN
  203. result := 7;
  204. ELSIF(number >= 256 AND number < 512) THEN
  205. result := 8;
  206. ELSIF(number >= 512 AND number < 1024) THEN
  207. result := 9;
  208. ELSIF(number >= 1024 AND number < 2048) THEN
  209. result := 10;
  210. ELSIF(number >= 2048 AND number < 4096) THEN
  211. Result := 11;
  212. ELSIF(Number >= 4096 AND number < 8192) THEN
  213. result := 12;
  214. ELSIF(number >= 8192 AND number < 16384) THEN
  215. result := 13;
  216. ELSIF(number >= 16384 AND number < 32768) THEN
  217. result := 14;
  218. END IF;
  219. RETURN result;
  220. END FUNCTION;
  221. END PACKAGE BODY;