Advertisement
Guest User

Untitled

a guest
Mar 30th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. library ieee;
  2. use ieee.std_logic_1164.all;
  3. use ieee.numeric_std.all;
  4.  
  5. package tipos is
  6. constant bandas : positive := 4;
  7. type vector32 is array (0 to bandas-1) of signed (31 downto 0);
  8. end package tipos;
  9.  
  10. library IEEE;
  11. use IEEE.std_logic_1164.all;
  12. use IEEE.numeric_std.all;
  13. library work; use work.tipos.all;
  14.  
  15. package propios is
  16. --function declaration.
  17. function module (a : vector32; bands: natural) return unsigned;
  18. end propios; --end of package.
  19.  
  20. package body propios is --start of package body
  21. --definition of function
  22. --based on: https://en.m.wikipedia.org/wiki/Methods_of_computing_square_roots#Binary_numeral_system_.28base_2.29
  23. function module (a : vector32; bands: natural) return unsigned is --To adapt it to a diferent number of bits in the input vector:
  24. --substitute the 71 for the needed number. Number of bits in each element of the vector *2 + power of two that can represent the maximum
  25. --number of bands, or fields. In this case, 32bit numbers, maximum number of bands, 256, so 2^8. 32*2+8=72.
  26. variable sum : unsigned(71 downto 0):= (others => '0');
  27. variable b : unsigned(71 downto 0):=(0=>'0', 70 => '1', others => '0');
  28. variable a_unsig: unsigned(31 downto 0):=(others =>'0');--for this vector use the same length as the input vector, 32bit in my case.
  29. variable result: unsigned (71 downto 0):= (others => '0');
  30. begin
  31.  
  32. for i in 0 to bands-1 loop--Sum of all the elements squared
  33. a_unsig:=unsigned(a(i));
  34. sum:=sum + (a_unsig * a_unsig);
  35. end loop;
  36.  
  37.  
  38. --Square root of sum
  39. while b>sum loop--Do any needed changes here. You only have to change the 71's
  40. b:='0'&'0'& b(71 downto 2);
  41. end loop;
  42.  
  43. while (b/=0) loop
  44. if (sum>=result+b) then
  45. sum:=sum - (result + b);
  46. result:=('0'& result(71 downto 1))+b;
  47. else
  48. result:='0'& result(71 downto 1);
  49. end if;
  50. b:='0' & '0' & b(71 downto 2);
  51. end loop;
  52.  
  53. return result(35 downto 0);--sqrt(2^72)=2^36. Use half of the bits you put in place of 71
  54. end module;
  55.  
  56.  
  57. end propios; --end of the package body
  58.  
  59. library ieee;
  60. use ieee.std_logic_1164.all;
  61. use ieee.numeric_std.all;
  62. library work;
  63. use work.propios.all;
  64. use work.tipos.all;
  65.  
  66. ENTITY test IS
  67.  
  68. END test;
  69. Architecture simple of test is
  70. signal a:vector32;
  71. signal c: unsigned(35 downto 0);
  72. signal b: natural:= 4;
  73. begin
  74. a(0)<="00000000110010011010011100000000";
  75. a(1)<="00000000110010011010011100000000";
  76. a(2)<="00000000110010011010011100000000";
  77. a(3)<="00000000110010011010011100000000";
  78.  
  79.  
  80. process
  81. begin
  82. wait for 200ps;
  83. c<= module (a , b);
  84. wait;
  85. end process;
  86.  
  87.  
  88. end simple;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement