Advertisement
Guest User

Untitled

a guest
May 11th, 2025
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VHDL 8.19 KB | None | 0 0
  1. library IEEE;
  2. use IEEE.STD_LOGIC_1164.ALL;
  3. use IEEE.NUMERIC_STD.ALL;
  4.  
  5. entity Accelerator is
  6.     generic (
  7.         D : integer := 10000;
  8.         N : integer := 32;
  9.         M : integer := 200
  10.     );
  11.     Port (
  12.         clk : in STD_LOGIC;
  13.         reset : in STD_LOGIC;
  14.         feature_values : in STD_LOGIC_VECTOR(N*16-1 downto 0);
  15.         start : in STD_LOGIC;
  16.         done : out STD_LOGIC;
  17.         encoded_hv_ready : out STD_LOGIC;
  18.         bundled_result : out STD_LOGIC_VECTOR(D-1 downto 0);
  19.         im_we : in STD_LOGIC;
  20.         im_addr : in STD_LOGIC_VECTOR(15 downto 0);
  21.         im_data_in : in STD_LOGIC_VECTOR(31 downto 0);
  22.         cm_we : in STD_LOGIC;
  23.         cm_addr : in STD_LOGIC_VECTOR(15 downto 0);
  24.         cm_data_in : in STD_LOGIC_VECTOR(31 downto 0);
  25.         im_data_out : out STD_LOGIC_VECTOR(31 downto 0);
  26.         cm_data_out : out STD_LOGIC_VECTOR(31 downto 0)
  27.     );
  28. end Accelerator;
  29.  
  30. architecture Behavioral of Accelerator is
  31.  
  32.     component IdentityMemory
  33.         Port (
  34.             clk : in STD_LOGIC;
  35.             we : in STD_LOGIC;
  36.             addr : in STD_LOGIC_VECTOR(15 downto 0);
  37.             data_in : in STD_LOGIC_VECTOR(31 downto 0);
  38.             data_out : out STD_LOGIC_VECTOR(31 downto 0)
  39.         );
  40.     end component;
  41.  
  42.     component ContinousMemory
  43.         Port (
  44.             clk : in STD_LOGIC;
  45.             we : in STD_LOGIC;
  46.             addr : in STD_LOGIC_VECTOR(15 downto 0);
  47.             data_in : in STD_LOGIC_VECTOR(31 downto 0);
  48.             data_out : out STD_LOGIC_VECTOR(31 downto 0)
  49.         );
  50.     end component;
  51.  
  52.     constant CHUNK_WIDTH : integer := 32;
  53.     constant CHUNKS_PER_VEC : integer := (D + CHUNK_WIDTH - 1) / CHUNK_WIDTH;
  54.  
  55.     type ram_array_type is array(0 to CHUNKS_PER_VEC-1) of std_logic_vector(31 downto 0);
  56.     signal majority_ram : ram_array_type := (others => (others => '0'));
  57.  
  58.     type counter_array_type is array(0 to 31) of unsigned(4 downto 0);
  59.     signal count_array : counter_array_type := (others => (others => '0'));
  60.  
  61.     signal state : integer range 0 to 11 := 0;
  62.  
  63.     signal feature_index : integer range 0 to N-1 := 0;
  64.     signal chunk_counter : integer range 0 to CHUNKS_PER_VEC-1 := 0;
  65.     signal level_index   : integer range 0 to M-1 := 0;
  66.  
  67.     signal position_chunk : std_logic_vector(31 downto 0) := (others => '0');
  68.     signal level_chunk    : std_logic_vector(31 downto 0) := (others => '0');
  69.     signal bound_chunk    : std_logic_vector(31 downto 0) := (others => '0');
  70.  
  71.     signal im_addr_mux, cm_addr_mux : std_logic_vector(15 downto 0);
  72.     signal im_data, cm_data : std_logic_vector(31 downto 0);
  73.     signal im_read_en, cm_read_en : std_logic := '0';
  74.     signal test : std_logic := '0';
  75.  
  76. begin
  77.  
  78.     -- Identity Memory Address
  79.     process(clk)
  80.     begin
  81.         if rising_edge(clk) then
  82.             if im_we = '1' then
  83.                 im_addr_mux <= im_addr;
  84.             elsif im_read_en = '1' then
  85.                 im_addr_mux <= std_logic_vector(to_unsigned(feature_index * (CHUNKS_PER_VEC) + chunk_counter, 16));
  86.             end if;
  87.             -- kein else nötig → keine Latch, Wert bleibt im Flip-Flop gespeichert
  88.         end if;
  89.     end process;
  90.  
  91.  
  92.     -- Continous Memory Address
  93.     process(clk)
  94.     begin
  95.         if rising_edge(clk) then
  96.             if cm_we = '1' then
  97.                 cm_addr_mux <= cm_addr;
  98.             elsif cm_read_en = '1' then
  99.                 cm_addr_mux <= std_logic_vector(to_unsigned(level_index * (CHUNKS_PER_VEC) + chunk_counter, 16));
  100.             end if;
  101.         end if;
  102.     end process;
  103.    
  104.  
  105.     IM: IdentityMemory port map (
  106.         clk => clk, we => im_we, addr => im_addr_mux, data_in => im_data_in, data_out => im_data
  107.     );
  108.  
  109.     CIM: ContinousMemory port map (
  110.         clk => clk, we => cm_we, addr => cm_addr_mux, data_in => cm_data_in, data_out => cm_data
  111.     );
  112.  
  113.     im_data_out <= im_data;
  114.     cm_data_out <= cm_data;
  115.  
  116.    -- Der Header und alle Konstanten bleiben wie in deiner letzten Version
  117. -- nur der Prozess ab "process(clk, reset)" wurde aktualisiert:
  118. process(clk, reset)
  119. begin
  120.     if reset = '1' then
  121.         state <= 0;
  122.         feature_index <= 0;
  123.         chunk_counter <= 0;
  124.         majority_ram <= (others => (others => '0'));
  125.         count_array <= (others => (others => '0'));
  126.         bundled_result <= (others => '0');
  127.         done <= '0';
  128.         encoded_hv_ready <= '0';
  129.         im_read_en <= '0';
  130.         cm_read_en <= '0';
  131.  
  132.     elsif rising_edge(clk) then
  133.         case state is
  134.  
  135.             when 0 => -- IDLE
  136.                 if start = '1' then
  137.                     bundled_result <= (others => '0');
  138.                     feature_index <= 0;
  139.                     chunk_counter <= 0;
  140.                     count_array <= (others => (others => '0'));
  141.                     done <= '0';
  142.                     encoded_hv_ready <= '0';
  143.                     state <= 1;
  144.                 end if;
  145.  
  146.             when 1 => -- Set im_read_en (nachdem feature_index gesetzt wurde)
  147.                 im_read_en <= '1';
  148.                 state <= 2;
  149.             when 2 =>
  150.                 state <= 3;
  151.             when 3 => -- Fetch Position + berechne level_index
  152.                 im_read_en <= '0';
  153.                
  154.                 level_index <= (to_integer(unsigned(feature_values((feature_index+1)*16-1 downto feature_index*16))) * (M - 1) + 27500) / 55000;
  155.                 cm_read_en <= '1';
  156.                 state <= 4;
  157.  
  158.             when 4 => -- Trigger CM Read (mit gültigem level_index)
  159.                 position_chunk <= im_data;
  160.                 state <= 5;
  161.  
  162.             when 5 => -- Fetch Level
  163.                 cm_read_en <= '0';
  164.                 state <= 6;
  165.             when 6 =>
  166.                 level_chunk <= cm_data;
  167.                 state <= 7;
  168.             when 7 => -- Calculate Bound Chunk
  169.                 bound_chunk <= position_chunk xor level_chunk;
  170.                 state <= 8;
  171.  
  172.             when 8 => -- Count '1's in Bound Chunk
  173.                 for i in 0 to 31 loop
  174.                     if bound_chunk(i) = '1' then
  175.                         count_array(i) <= count_array(i) + 1;
  176.                     end if;
  177.                 end loop;
  178.  
  179.                 if feature_index < N-1 then
  180.                     feature_index <= feature_index + 1;
  181.                     state <= 1;
  182.                 else
  183.                     feature_index <= 0;
  184.                     state <= 9;
  185.                 end if;
  186.  
  187.             when 9 => -- Write Majority for current chunk
  188.                 for k in 0 to 31 loop
  189.                     if (chunk_counter * 32 + k) < (D+15) then
  190.                         if count_array(k)(4) = '1' then
  191.                             majority_ram(chunk_counter)(k) <= '1';
  192.                         else
  193.                             majority_ram(chunk_counter)(k) <= '0';
  194.                         end if;
  195.                     end if;
  196.                 end loop;
  197.                 count_array <= (others => (others => '0'));
  198.  
  199.                 if chunk_counter < CHUNKS_PER_VEC-1 then
  200.                     chunk_counter <= chunk_counter + 1;
  201.                     if chunk_counter = 311 then
  202.                         test <= '1';
  203.                     end if;
  204.                     state <= 1;
  205.                 else
  206.                     chunk_counter <= 0;
  207.                     state <= 10;
  208.                 end if;
  209.  
  210.             when 10 => -- Output bundled result
  211.                 for i in 0 to CHUNKS_PER_VEC-1 loop
  212.                     if i < CHUNKS_PER_VEC-1 then
  213.                         -- bundled_result(D-i*32-1 downto D-(i+1)*32)
  214.                         bundled_result(D-i*32-1 downto D-(i+1)*32) <= majority_ram(i);
  215.                     else
  216.                         for j in 0 to 15 loop
  217.                             bundled_result(D-i*32 - j) <= majority_ram(i)(31-j);
  218.                         end loop;
  219.                     end if;
  220.                 end loop;
  221.                 encoded_hv_ready <= '1';
  222.                 done <= '1';
  223.                 majority_ram <= (others => (others => '0'));
  224.                 state <= 11;
  225.             when 11 =>
  226.                 done <= '0';
  227.                 state <= 0;
  228.             when others => -- Wait here until reset
  229.                 state <= 0;
  230.         end case;
  231.     end if;
  232. end process;
  233.  
  234.  
  235. end Behavioral;
  236.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement