Studentenversion des ESY6/A Praktikums "signal_processing".
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.

squareRoot_pipe.vhd 5.0KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. ----------------------------------------------------------------------------------------------------
  2. -- Component : squareRoot_pipe
  3. -- Author : pwkolas
  4. ----------------------------------------------------------------------------------------------------
  5. -- File : squareRoot_pipe.vhd
  6. -- Mod. Date : XX.XX.XXXX
  7. -- Version : 1.00
  8. ----------------------------------------------------------------------------------------------------
  9. -- Description : Square root calculator.
  10. -- Based on
  11. -- "A New Non-Restoring Square Root Algorithm and Its VLSI Implementations"
  12. --
  13. ----------------------------------------------------------------------------------------------------
  14. -- Modification History :
  15. --
  16. ----------------------------------------------------------------------------------------------------
  17. -- Comments :
  18. --
  19. ----------------------------------------------------------------------------------------------------
  20. library ieee;
  21. use ieee.std_logic_1164.all;
  22. use ieee.numeric_std.all;
  23. entity squareRoot_pipe is
  24. generic (
  25. G_DATA_W : integer := 32
  26. );
  27. port (
  28. clk : in std_logic;
  29. rst : in std_logic;
  30. iv_data : in std_logic_vector(G_DATA_W-1 downto 0);
  31. ov_res : out std_logic_vector((G_DATA_W/2)-1 downto 0)
  32. );
  33. end entity squareRoot_pipe;
  34. architecture squareRoot_pipe_rtl of squareRoot_pipe is
  35. constant C_ALU_W : integer := ((G_DATA_W/2) + 2);
  36. constant C_PIPE_L : integer := G_DATA_W/2;
  37. constant C_OFFSET : integer := 3; -- width of start vectors going to ALU
  38. type t_arr_pipe_x_data is array (C_PIPE_L-1 downto 0) of unsigned(G_DATA_W-1 downto 0);
  39. signal a_data : t_arr_pipe_x_data; -- (D)
  40. signal a_R : t_arr_pipe_x_data; -- (R)
  41. type t_arr_pipe_x_alu is array (C_PIPE_L-1 downto 0) of unsigned(C_ALU_W-1 downto 0);
  42. type t_arr_pipe_x_res is array (C_PIPE_L-1 downto 0) of unsigned(G_DATA_W/2-1 downto 0);
  43. signal a_Q : t_arr_pipe_x_res; -- (ALU Q out)
  44. signal nextOp : std_logic_vector(C_PIPE_L-1 downto 0);
  45. begin
  46. sqrt_p : process (clk, rst)
  47. variable va_AluInR : t_arr_pipe_x_alu; -- (ALU R in)
  48. variable va_AluInQ : t_arr_pipe_x_alu; -- (ALU Q in)
  49. variable va_AluOut : t_arr_pipe_x_alu; -- (ALU Q out)
  50. begin
  51. if (rst = '1') then
  52. a_data <= (others => (others => '0'));
  53. a_R <= (others => (others => '0'));
  54. a_Q <= (others => (others => '0'));
  55. va_AluInR := (others => (others => '0'));
  56. va_AluInQ := (others => (others => '0'));
  57. va_AluOut := (others => (others => '0'));
  58. nextOp <= (others => '0');
  59. elsif rising_edge(clk) then
  60. -- stage 0 start conditions, ALU inputs
  61. va_AluInR(0) := (others => '0');
  62. va_AluInR(0)(1 downto 0) := unsigned(iv_data(G_DATA_W-1 downto G_DATA_W-1-1));
  63. va_AluInQ(0) := (others => '0');
  64. va_AluInQ(0)(0) := '1';
  65. -- stage 0 calculations
  66. va_AluOut(0) := va_AluInR(0) - va_AluInQ(0);
  67. -- stage 0 result registers, ALU output
  68. a_data(0) <= shift_left(unsigned(iv_data), 2);
  69. a_R(0) <= (others => '0');
  70. a_R(0)(G_DATA_W-1 downto G_DATA_W-1-1) <= va_AluOut(0)(1 downto 0);
  71. a_Q(0) <= (others => '0');
  72. a_Q(0)(0) <= not va_AluOut(0)(2);
  73. nextOp(0) <= not va_AluOut(0)(2);
  74. -- next stages
  75. for i in 1 to C_PIPE_L-1 loop
  76. -- prepare inputs for next stage
  77. va_AluInR(i) := (others => '0');
  78. va_AluInR(i)(C_OFFSET+i-1 downto 2) := a_R(i-1)(G_DATA_W-(i-1)-1 downto G_DATA_W-(2*i));
  79. va_AluInR(i)(2-1 downto 0) := a_data(i-1)(G_DATA_W-1 downto G_DATA_W-1-1);
  80. va_AluInQ(i) := (others => '0');
  81. va_AluInQ(i)(C_OFFSET+(i-1)-1 downto 2) := a_Q(i-1)(i-1 downto 0);
  82. va_AluInQ(i)(1) := not a_Q(i-1)(0);
  83. va_AluInQ(i)(0) := '1';
  84. -- ALU ADD/SUB
  85. if (nextOp(i-1) = '1') then
  86. va_AluOut(i) := va_AluInR(i) - va_AluInQ(i);
  87. else
  88. va_AluOut(i) := va_AluInR(i) + va_AluInQ(i);
  89. end if;
  90. -- result registers
  91. a_data(i) <= shift_left(unsigned(a_data(i-1)), 2);
  92. a_R(i) <= (others => '0');
  93. a_R(i)(G_DATA_W-i-1 downto G_DATA_W-2*(i+1)) <= va_AluOut(i)(i+1 downto 0);
  94. a_Q(i) <= shift_left(unsigned(a_Q(i-1)), 1);
  95. a_Q(i)(0) <= not va_AluOut(i)(i+2);
  96. nextOp(i) <= not va_AluOut(i)(i+2);
  97. end loop;
  98. end if;
  99. end process;
  100. ov_res <= std_logic_vector(a_Q(C_PIPE_L-1));
  101. end architecture squareRoot_pipe_rtl;