divider.vhdl 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. library ieee;
  2. use ieee.std_logic_1164.all;
  3. use ieee.numeric_std.all;
  4. library work;
  5. use work.common.all;
  6. use work.decode_types.all;
  7. entity divider is
  8. port (
  9. clk : in std_logic;
  10. rst : in std_logic;
  11. d_in : in Execute1ToDividerType;
  12. d_out : out DividerToExecute1Type
  13. );
  14. end entity divider;
  15. architecture behaviour of divider is
  16. signal dend : std_ulogic_vector(128 downto 0);
  17. signal div : unsigned(63 downto 0);
  18. signal quot : std_ulogic_vector(63 downto 0);
  19. signal result : std_ulogic_vector(63 downto 0);
  20. signal sresult : std_ulogic_vector(64 downto 0);
  21. signal oresult : std_ulogic_vector(63 downto 0);
  22. signal running : std_ulogic;
  23. signal count : unsigned(6 downto 0);
  24. signal neg_result : std_ulogic;
  25. signal is_modulus : std_ulogic;
  26. signal is_32bit : std_ulogic;
  27. signal extended : std_ulogic;
  28. signal is_signed : std_ulogic;
  29. signal overflow : std_ulogic;
  30. signal ovf32 : std_ulogic;
  31. signal did_ovf : std_ulogic;
  32. begin
  33. divider_0: process(clk)
  34. begin
  35. if rising_edge(clk) then
  36. if rst = '1' then
  37. dend <= (others => '0');
  38. div <= (others => '0');
  39. quot <= (others => '0');
  40. running <= '0';
  41. count <= "0000000";
  42. elsif d_in.valid = '1' then
  43. if d_in.is_extended = '1' then
  44. dend <= '0' & d_in.dividend & x"0000000000000000";
  45. else
  46. dend <= '0' & x"0000000000000000" & d_in.dividend;
  47. end if;
  48. div <= unsigned(d_in.divisor);
  49. quot <= (others => '0');
  50. neg_result <= d_in.neg_result;
  51. is_modulus <= d_in.is_modulus;
  52. extended <= d_in.is_extended;
  53. is_32bit <= d_in.is_32bit;
  54. is_signed <= d_in.is_signed;
  55. count <= "1111111";
  56. running <= '1';
  57. overflow <= '0';
  58. ovf32 <= '0';
  59. elsif running = '1' then
  60. if count = "0111111" then
  61. running <= '0';
  62. end if;
  63. overflow <= quot(63);
  64. if dend(128) = '1' or unsigned(dend(127 downto 64)) >= div then
  65. ovf32 <= ovf32 or quot(31);
  66. dend <= std_ulogic_vector(unsigned(dend(127 downto 64)) - div) &
  67. dend(63 downto 0) & '0';
  68. quot <= quot(62 downto 0) & '1';
  69. count <= count + 1;
  70. elsif dend(128 downto 57) = x"000000000000000000" and count(6 downto 3) /= "0111" then
  71. -- consume 8 bits of zeroes in one cycle
  72. ovf32 <= or (ovf32 & quot(31 downto 24));
  73. dend <= dend(120 downto 0) & x"00";
  74. quot <= quot(55 downto 0) & x"00";
  75. count <= count + 8;
  76. else
  77. ovf32 <= ovf32 or quot(31);
  78. dend <= dend(127 downto 0) & '0';
  79. quot <= quot(62 downto 0) & '0';
  80. count <= count + 1;
  81. end if;
  82. else
  83. count <= "0000000";
  84. end if;
  85. end if;
  86. end process;
  87. divider_1: process(all)
  88. begin
  89. if is_modulus = '1' then
  90. result <= dend(128 downto 65);
  91. else
  92. result <= quot;
  93. end if;
  94. if neg_result = '1' then
  95. sresult <= std_ulogic_vector(- signed('0' & result));
  96. else
  97. sresult <= '0' & result;
  98. end if;
  99. did_ovf <= '0';
  100. if is_32bit = '0' then
  101. did_ovf <= overflow or (is_signed and (sresult(64) xor sresult(63)));
  102. elsif is_signed = '1' then
  103. if ovf32 = '1' or sresult(32) /= sresult(31) then
  104. did_ovf <= '1';
  105. end if;
  106. else
  107. did_ovf <= ovf32;
  108. end if;
  109. if did_ovf = '1' then
  110. oresult <= (others => '0');
  111. elsif (is_32bit = '1') and (is_modulus = '0') then
  112. -- 32-bit divisions set the top 32 bits of the result to 0
  113. oresult <= x"00000000" & sresult(31 downto 0);
  114. else
  115. oresult <= sresult(63 downto 0);
  116. end if;
  117. end process;
  118. divider_out: process(clk)
  119. begin
  120. if rising_edge(clk) then
  121. d_out.valid <= '0';
  122. d_out.write_reg_data <= oresult;
  123. d_out.overflow <= did_ovf;
  124. if count = "1000000" then
  125. d_out.valid <= '1';
  126. end if;
  127. end if;
  128. end process;
  129. end architecture behaviour;