VHDL中灵活/通用解码器的想法

Eri*_*ada 7 fpga vhdl xilinx

我想创建一个地址解码器,它足够灵活,可以在改变选择器的位数和解码的输出信号时使用.

因此,而不是具有静态(固定输入/输出大小)解码器,看起来像这样:

entity Address_Decoder is
Generic
(
    C_INPUT_SIZE: integer := 2
);
Port
(
    input   : in  STD_LOGIC_VECTOR (C_INPUT_SIZE-1 downto 0);
    output  : out STD_LOGIC_VECTOR ((2**C_INPUT_SIZE)-1 downto 0);
    clk : in  STD_LOGIC;
    rst : in  STD_LOGIC
);
end Address_Decoder;

architecture Behavioral of Address_Decoder is

begin        
        process(clk)
            begin
               if rising_edge(clk) then 
                  if (rst = '1') then
                     output <= "0000";
                  else
                     case <input> is
                        when "00" => <output> <= "0001";
                        when "01" => <output> <= "0010";
                        when "10" => <output> <= "0100";
                        when "11" => <output> <= "1000";
                        when others => <output> <= "0000";
                     end case;
                  end if;
               end if;
            end process;

end Behavioral;
Run Code Online (Sandbox Code Playgroud)

有一些更灵活/更通用的东西,看起来像这样:

    entity Address_Decoder is
    Generic
    (
        C_INPUT_SIZE: integer := 2
    );
    Port
    (
        input   : in  STD_LOGIC_VECTOR (C_INPUT_SIZE-1 downto 0);
        output  : out STD_LOGIC_VECTOR ((2**C_INPUT_SIZE)-1 downto 0);
        clk : in  STD_LOGIC;
        rst : in  STD_LOGIC
    );
    end Address_Decoder;

    architecture Behavioral of Address_Decoder is

    begin        

DECODE_PROC:
    process (clk)
    begin

        if(rising_edge(clk)) then
         if ( rst = '1') then
           output <= conv_std_logic_vector(0, output'length);
         else
           case (input) is
             for i in 0 to (2**C_INPUT_SIZE)-1 generate
             begin
                when (i = conv_integer(input)) => output <= conv_std_logic_vector((i*2), output'length);        
             end generate;
            when others => output <= conv_std_logic_vector(0, output'length);
           end case;
         end if;
        end if;
    end process;

    end Behavioral;
Run Code Online (Sandbox Code Playgroud)

我知道这段代码是无效的,并且"when"测试用例必须是常量,并且我不能在case语句之间使用for-generate,但它显示了我所追求的:一个实体聪明到足以满足我的需求.

我一直试图为这个问题找到一个优雅的解决方案而没有太大的成功,所以,我愿意接受任何建议.

提前谢谢,埃里克

Jan*_*uwe 13

显然,您希望输入是应该设置的输出位的索引.

这样写.类似的东西(假设来自numeric_std的类型):

output <= (others => '0'); -- default
output(to_integer(input)) <= '1';
Run Code Online (Sandbox Code Playgroud)

  • 简短而有效(并且工作得很好!).这让我意识到有时候想要在硬件描述中稍微"低一点"(试图将逻辑定义为更接近其硬件实现的样子),可能会使您从明显的解决方案中失明这会将一些设计负担放在软件而不是你身上.谢谢你的解决方案Jan. (2认同)
  • 我很高兴您收到了元消息:-)祝贺这一见解,这在HDL设计界非常罕见! (2认同)