4
\$\begingroup\$

I'm needing to create a 4 bit, 16 input multiplexer. I know I could describe this as a long list of S => when "0000" etc.. but I think that's not very clean, and I'd like to opt for a generic n-bit multiplexer so I can reuse it later for more inputs or whatever.

How would you do this?

\$\endgroup\$
4
\$\begingroup\$

It's like this:

use ieee.numeric_std.all;
...
signal din :std_logic_vector (15 downto 0);  -- must be "downto 0"
signal sel :std_logic_vector (3 downto 0);
...
dout <= din(conv_integer(sel));

Hope that helps!

Addition: If you'd need a multiple bit wide mux then...

type data_bus_array is array(7 downto 0) of std_logic_vector (31 downto 0);
signal din :data_bus_array;
signal dout :std_logic_vector (31 downto 0);
signal sel :std_logic_vector (3 downto 0);
...
dout <= din(conv_integer(sel));

This is where VHDL gets a little bit wordy, since sometimes it's not "compact" to fill in that data bus array just to do a simple mux. But, there you have it...

\$\endgroup\$
  • \$\begingroup\$ Wow. Now that's just too easy. what if you wanted multiple bit inputs though? \$\endgroup\$ – Earlz Apr 3 '11 at 21:56
  • \$\begingroup\$ @Earlz I added multi-bit-wide stuff to my answer, above. \$\endgroup\$ – user3624 Apr 3 '11 at 23:09
  • 1
    \$\begingroup\$ Nice. Would've been a bit better if you had added generics in, but that's pretty easy to change around \$\endgroup\$ – Earlz Apr 4 '11 at 3:22
  • \$\begingroup\$ You have numeric_std - yay! But then you need "to_integer", not "conv_integer". Then, why not make sel an "unsigned" type? \$\endgroup\$ – Martin Thompson Apr 4 '11 at 12:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.