Grüße!
Also ich hab auch schon seit Ewigkeiten nach einem generischen
Multiplexer gesucht. Da ich nirgends einen komplett generischen finden
konnte, habe ich mich selbst mal hingesetzt. Das ist für die Nachwelt
dabei herausgekommen (die Bitbreite muss man dann nur im Package
eintragen):
-- =====================================================================
-- Project: General
-- Filename: GENERIC_MUX.vhd
-- Description: Completely variable multiplexer for 32-bit data vectors.
-- Author(s): Christian Anton
-- Created: 24.02.2009
-- Last Change: 24.02.2009
-- =====================================================================
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all; -- for conversion
use work.BASICS_PACK.all; -- for N_x_std_logic_vec_32, ld_ceil
entity GENERIC_MUX is
generic(
COUNT_INPUT : natural := 4 -- count of input vectors
);
port(
INPUTS : in N_x_std_logic_vec_32(COUNT_INPUT-1 downto 0);
SEL : in std_logic_vector(ld_ceil(COUNT_INPUT)-1 downto
0);
OUTPUT : out std_logic_vector(31 downto 0)
);
end entity;
architecture BEHAVIOR of GENERIC_MUX is
begin
-- Choose the output accordingly to SEL
CHOOSE_OUTPUT : process(SEL, INPUTS)
variable output_number : std_logic_vector(ld_ceil(COUNT_INPUT)-1
downto 0);
begin
-- Iterate over all input vectors and output the selected data
for i in INPUTS'range loop
output_number := std_logic_vector( to_unsigned(i,
ld_ceil(COUNT_INPUT)) );
if (SEL = output_number) then
OUTPUT <= INPUTS(i);
exit;
else
OUTPUT <= (others => '0');
end if;
end loop;
end process CHOOSE_OUTPUT;
end architecture;
-- =====================================================================
-- Project: General
-- Filename: GENERIC_MUX_PACK.vhd
-- Description: Package including basic type definition and function for
-- the generic mulitplexer.
-- Author(s): Christian Anton
-- Created: 24.02.2009
-- Last Change: 24.02.2009
-- =====================================================================
library ieee;
use ieee.std_logic_1164.all;
package GENERIC_MUX_PACK is
type N_x_std_logic_vec_32 is array(natural range <>) of
std_logic_vector(31 downto 0);
-- Calculates the rounded up logarithmus duales of x
function ld_ceil(x : natural) return natural;
end package;
package body GENERIC_MUX_PACK is
-- Calculates the rounded up logarithmus duales of x
function ld_ceil(x : natural) return natural is
variable ld : natural := 0;
variable x_int : real := real(x);
begin
while (x_int > 1.0) loop
ld := ld + 1;
x_int := x_int / 2.0;
end loop;
return ld;
end function ld_ceil;
end package body;