-->

VHDL - 是否改变(VHDL - IF alternative)

2019-10-21 09:41发布

我想,我已经if语句下面写,如果的替代。

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;

我希望它更改为以下。

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

但是,编译器给了我下面的错误。

“#错误:COMP96_0015:分:(111,49): ';' 预期。”

我怎样才能解决这个问题? 谢谢。

Answer 1:

问:什么是VAL1和val2的类型?

这里有一些改进:

  • 如果val1和val2的仅具有2个比特: val1 < val2
  • 使用切片,而不是单比特concats: val1(1 downto 0) < val2(1 downto 0)
  • 你可以使用y <= a when (condition) else b; 声明
    这相当于C中的三元操作的VHDL y = cond ? val1 : val2; y = cond ? val1 : val2;
  • 你可以定义的if-then-else的功能让我们把它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; 

    用法:

     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 
  • 你可以定义一个比较功能,让我们称之为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 

    用法:

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


文章来源: VHDL - IF alternative