VHDL - IF alternative

风流意气都作罢 提交于 2019-12-11 12:28:54

问题


I want write an alternative of the if, I have following if statement.

if val1(1)&val1(0) < val2(1)&val2(0) then
            r:="10";
    else
            if  val1(1)&val1(0) = val2(1)&val2(0) then
                r:="00";
            else 
                r:="01";
            end if;
    end if;

And I want it to change to following.

s:=((data1(9)&data1(8)) < (data2(9)&data2(8)))?"01":(((data1(9)&data1(8)) = (data2(9)&data2(8)))?"00":"01");

But the compiler gives me following error.

"# Error: COMP96_0015: min: (111, 49): ';' expected."

How can I resolve this? Thanks.


回答1:


Question: what's the type of val1 and val2?

Here are some improvements:

  • if val1 and val2 have only 2 bits: val1 < val2
  • use slices instead of single bit concats: val1(1 downto 0) < val2(1 downto 0)
  • you could use the y <= a when (condition) else b;statement
    this is the VHDL equivalent to C's ternary operator y = cond ? val1 : val2;
  • you could define a if-then-else functions let's call it ite:

    function ite(cond : boolean; val1 : std_logic_vector; val2 : std_logic_vector)
    return std_logic_vector is
    begin
      if cond then
        return val1;
      else
        return val2;
      end if;
    end function;
    

    Usage:

    s := ite((val1(1 downto 0) < val2(1 downto 0)), "10",    -- less
         ite((val1(1 downto 0) = val2(1 downto 0)), "00",    -- equal
                                                    "01"));  -- greater
    
  • you could define a compare function, let's call it comp:

    function comp(val1 : std_logic_vector; val2 : std_logic_vector)
    return std_logic_vector is
    begin
      if (val1 < val2) then
        return "10";
      elsif (val1 = val2) then
        return "00";
      else
        return "01";
      end if;
    end function
    

    Usage:

    s := comp(val1(1 downto 0), val2(1 downto 0));
    


来源:https://stackoverflow.com/questions/27408170/vhdl-if-alternative

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!