Use a generic to determine (de)mux size in VHDL?

后端 未结 3 896
走了就别回头了
走了就别回头了 2021-01-20 03:39

I want to use a generic \'p\' to define how many outputs a demux will have. Input and all outputs are 1 bit. The outputs, control, and input can be something simple like:<

3条回答
  •  佛祖请我去吃肉
    2021-01-20 03:54

    You need to pass both the number of outputs and the size of the control array as generics, unless you are always using powers of two.

    Outside of your (de)mux module (ie: when you instantiate), you can use code to calculate the number of bits for the control bus. I have a function in a common package I use to initialize various configuration constants and generics that get passed to code similar to your (de)mux application:

    -- Calculate the number of bits required to represent a given value
    function NumBits(val : integer) return integer is
        variable result : integer;
    begin
        if val=0 then
            result := 0;
        else
            result  := natural(ceil(log2(real(val))));
        end if;
        return result;
    end;
    

    ...which allows you to do things like:

    constant NumOut : integer := 17;
    signal CtrlBus  : std_logic_vector(NumBits(NumOut)-1 downto 0);
    
    my_mux : demux
    generic map (
        NumOut  => NumOut,
        NumCtrl => NumBits(NumOut) )
    port map (
        control => CtrlBus,
    ...
    ...
    

提交回复
热议问题