Electronic – Compiling a simple 2-input multiplexer with GHDL fails

multiplexersimulationvhdl

I started reading "The Designer's Guide to VHDL" by P. J. Ashenden, but I'm already stuck after exercise 9, here is my code:

entity mul2 is
port ( a, b, sel : in bit;
       z : out bit );
end entity mul2;

architecture behav of mul2 is
begin

multiplex : process is
begin
    if sel then
        z <= a;
    else
        z <= b;
    end if;
end process multiplex;

end architecture behav;

When I invoke GHDL with

ghdl -a mul2.vhdl

I get the following error:

mul2.vhdl:11:19: can't match port "sel" with type boolean
mul2.vhdl:2:21: (location of port "sel")

I'm kind of confused, because in the book there is a example that shows a in bit port being used in a if-statement the same way I do in my code – but that example won't compile as well(example 1.2, if you have the book on hand).

I think this is a problem with GHDL not implementing the standard properly. Has anyone an idea what's wrong here?

Best Answer

The problem is in the if statement.

Unlike C, in VHDL you there is no shortcut that would evaluate any non-zero value to true and the rest to false. VHDL is much stricter here.

Here is the fixed code:

entity mul2 is
port ( a, b, sel : in bit;
       z : out bit );
end entity mul2;

architecture behav of mul2 is
begin

multiplex : process is
begin
    if sel = '1' then  --- see, if you do a proper compare it works
        z <= a;
    else
        z <= b;
    end if;
end process multiplex;

end architecture behav;
Related Topic