问题
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 operatory = 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