如何在vhdl中将位分成不同的信号?

Jea*_*lho 1 verilog concatenation vhdl bit

我有以下一行我要转换为vhdl的verilog代码:

assign   {cout,sum} = ( add ) ? ( in_a + in_b + cin ) : ( in_a - in_b - cin );
Run Code Online (Sandbox Code Playgroud)

我将如何在vhdl中执行此操作?

zen*_*hoy 6

实际上你也可以这样做,你只需要记住增加输入值的宽度,以便为输出进位"腾出空间".

(cout, sum) <= ('0'&in_a) + ('0'&in_b) + cin when(add='1') else ('0'&in_a) - ('0'&in_b) - cin;
Run Code Online (Sandbox Code Playgroud)

既然这条线非常非常丑陋且难以理解,我建议将整个过程转换为一个过程:

process(in_a, in_b, cin) begin
    if(add='1') then
        (cout, sum) <= ('0'&in_a) + ('0'&in_b) + cin;
    else
        (cout, sum) <= ('0'&in_a) - ('0'&in_b) - cin;
    end if;
end process;
Run Code Online (Sandbox Code Playgroud)

这至少要清晰一点.

编辑:

请注意,这仅适用于VHDL 2008.对于早期版本,您必须创建一个比输入宽的中间信号,将结果分配给该信号,然后提取cout和sum.

process(in_a, in_b, cin)
    -- Assumes in_a and in_b have the same width, otherwise
    -- use the wider of the two.
    variable result : unsigned(in_a'length downto 0);
begin
    if(add='1') then
        result := ('0'&in_a) + ('0'&in_b) + cin;
    else
        result := ('0'&in_a) - ('0'&in_b) - cin;
    end if;
    cout <= result(result'high);
    sum  <= result(result'high-1 downto 0);
end process;
Run Code Online (Sandbox Code Playgroud)