Forum Discussion
Altera_Forum
Honored Contributor
8 years ago --- Quote Start --- I am looking for a way to dynamic instantiate a multiplexer of N channels which has a one-hot encoded select. My current hard-coded version for a 3 channel system looks like this: Both work correctly, but I have not found a synthesize solution to parameterize the mux to make it generic for use across system builds. I have been unsuccessful trying Generate statements or For Loops to handle the One-Hot select case. Thanks, Aaron --- Quote End --- Here is a solution with a for loop:
entity combmuxs is
generic (
WIDTH_D : natural := 4 ;
SIZE : natural := 16
) ;
port (
D : in std_logic_2D( SIZE - 1 downto 0 , WIDTH_D - 1 downto 0) ;
Sel : in std_logic_vector(SIZE - 1 downto 0) ;
Q : out std_logic_vector(WIDTH_D - 1 downto 0)
) ;
end combmuxs ;
architecture a of combmuxs is
function smux3( d : std_logic_2D ; sel : std_logic_vector)
return std_logic_vector
is
variable r : std_logic_vector( d'high(2) downto 0) ;
begin
r:= (others => '0') ; -- default return if none selected
for i in 0 to d'high(1) loop
if (sel(i) = '1') then
return to_std_logic_vector( d , i ) ;
end if ;
end loop ;
return r ;
end function ;
begin
process( D , Sel )
begin
Q <= smux3(D , Sel) ;
end process ;
end a ;
It is written for a std_logic_2d from the lpm_components package but it is easy to replace this by a 1Dx1D array. I have written it as a function so I could test other approaches, like a recursive version. I add the supporting function:
function to_std_logic_vector( source : std_logic_2D ; idx : natural )
return std_logic_vector
is
variable r : std_logic_vector( source'high(2) downto 0) ;
begin
for i in 0 to source'high(2) loop
r(i) := source(idx , i) ;
end loop ;
return r ;
end function ;