Advertisement
Guest User

Untitled

a guest
Jan 20th, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.35 KB | None | 0 0
  1. library IEEE;
  2. use IEEE.std_logic_1164.all;
  3. use IEEE.std_logic_unsigned.all;
  4. use IEEE.std_logic_arith.all;
  5.  
  6. entity Collatz is
  7. port (
  8. sysClk : in std_logic := '0';
  9. resetIn : in std_logic := '0';
  10. onStartIn : in std_logic := '0';
  11. dataIn : in std_logic_vector(7 downto 0) := (others => '0'); -- max 255
  12. tempOut : out std_logic_vector(15 downto 0) := (others => '0'); -- max 13120 (when dataIn is 255)
  13. counterOut : out std_logic_vector(7 downto 0) := (others => '0'); -- max 127 (when dataIn is 231, 235)
  14. stateOut : out std_logic_vector(1 downto 0) := (others => '0');
  15. onFinishOut: out std_logic := '0'
  16. );
  17. end Collatz;
  18.  
  19. architecture RTL of Collatz is
  20. signal temp : std_logic_vector(15 downto 0) := (others => '0');
  21. signal counter: std_logic_vector(7 downto 0) := (others => '0');
  22. signal state : std_logic_vector(1 downto 0) := (others => '0');
  23. signal isDone : std_logic := '0';
  24.  
  25. -- states
  26. constant IDLING : std_logic_vector := "00";
  27. constant ONSTART : std_logic_vector := "01";
  28. constant RUNNING : std_logic_vector := "11";
  29. constant ONFINISH: std_logic_vector := "10";
  30.  
  31. type vectorArray is array (0 to 255) of std_logic_vector(7 downto 0);
  32. signal collatzArray : vectorArray := (others => (others => '0'));
  33.  
  34. begin
  35. process begin
  36. wait until rising_edge(sysClk);
  37.  
  38. if resetIn = '1' then
  39. state <= IDLING;
  40. else
  41. case state(0) is
  42. when '0' =>
  43. if onStartIn = '1' then
  44. state(0) <= '1'; -- IDLING -> ONSTART
  45. end if;
  46. when '1' =>
  47. if (state(1) = '1' and isDone = '1') then
  48. state(0) <= '0'; -- RUNNING -> ONFINISH
  49. end if;
  50. when others => -- never happens
  51. state(0) <= '0';
  52. end case;
  53. state(1) <= state(0); -- ONSTART -> RUNNING, ONFINISH -> IDLING
  54. end if;
  55.  
  56. if state = ONSTART then
  57. temp <= "00000000" & dataIn;
  58. counter <= "00000000";
  59. elsif (state = RUNNING and isDone = '0') then
  60. if temp < "00000000" & "11111111" and not (collatzArray(conv_integer(temp)) = "00000000") then
  61. temp <= "0000000000000001";
  62. counter <= counter + conv_integer(collatzArray(conv_integer(temp)));
  63. elsif temp = "0000000000000010" then
  64. temp <= '0' & temp(15 downto 1);
  65. counter <= counter + 1;
  66. elsif temp(0) = '0' then
  67. if temp(1) = '0' then -- temp % 4 = 0: 4n -> n
  68. temp <= "00" & temp(15 downto 2);
  69. counter <= counter + 2;
  70. else -- temp % 4 = 2: 4n + 2 -> 3n + 2
  71. temp <= temp - ("00" & temp(15 downto 2));
  72. counter <= counter + 3;
  73. end if;
  74. else
  75. if temp(1) = '0' then -- temp % 4 = 1: 4n + 1 -> 3n + 1
  76. temp <= temp - ("00" & temp(15 downto 2));
  77. counter <= counter + 3;
  78. else -- temp % 4 = 3: 4n + 3 -> 9n + 8
  79. temp <= temp + temp + ("00" & temp(15 downto 2)) + 2;
  80. counter <= counter + 4;
  81. end if;
  82. end if;
  83. end if;
  84.  
  85. if state = ONFINISH then
  86. collatzArray(conv_integer(dataIn)) <= counter;
  87. end if;
  88. end process;
  89.  
  90. onFinishOut <= '1' when state = ONFINISH else '0';
  91. counterOut <= counter;
  92. tempOut <= temp;
  93. stateOut <= state;
  94.  
  95. isDone <= '1' when (temp = "0000000000000000" or temp = "0000000000000001") else '0';
  96.  
  97. end RTL;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement