【问题标题】:Sorting a vector in VHDL在 VHDL 中对向量进行排序
【发布时间】:2021-05-09 11:02:10
【问题描述】:

我试图对向量进行排序,例如,如果输入为 101010,则输出将为 111000。每次我尝试模拟代码时,我的输出总是全为零。

我发布我的代码供您参考。如果我遗漏了什么或者有更好的方法来实现,请告诉我

    library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.NUMERIC_STD.ALL;

entity num_ones_for is
    Port ( A : in  STD_LOGIC_VECTOR (15 downto 0);
           A_S : out  STD_LOGIC_VECTOR (15 downto 0));
end num_ones_for;

architecture Behavioral of num_ones_for is
function sort_binary ( ones :unsigned;a : std_logic_vector) return std_logic_vector is
    variable A1: std_logic_vector ( 15 downto 0) := (others =>'0') ;
    begin
    for i in 15 to(15 - to_integer(ones)) loop
     A1(i) := '1';
    end loop;
    return A1;
    end function sort_binary;
signal ones : unsigned (4 downto 0);
begin

process(A)
variable count : unsigned(4 downto 0) := "00000";
begin

count := "00000";   --initialize count variable.
    for i in 0 to 15 loop   --for all the bits.
        count := count + ("0000" & A(i));   --Add the bit to the count.
    end loop;
    ones <= count;    --assign the count to output.
end process;
A_S <= sort_binary(ones =>ones,a =>A);

end Behavioral;

enter image description here

【问题讨论】:

  • 你在架构声明部分而不是在架构声明部分声明了信号。变量计数是在流程语句的语句序列中声明的,而不是在流程语句的声明部分中声明的。两者都位于前面各自保留字begin 的错误一侧。函数体的 for 循环参数范围有错误。右边界的类型与左边​​界文字 0 不兼容。将 1 转换为整数:for i in 0 to to_integer(ones) loop
  • @user1155120 谢谢,成功了。
  • for i in 15 to(15 - to_integer(ones)) loop 除非整数值为 0,否则表示空范围。将不会执行具有空范围的循环语句中的语句序列。右边界表达式不需要括号,方向应该向下。

标签: vhdl hdl


【解决方案1】:

最后,我解决了大部分错误并稍微改变了代码的逻辑。如果将来有人想编写相同类型的 VHDL 代码,可以参考我的代码。如果有更好的方法来解决这个问题,请告诉我 总是乐于学习。 谢谢

library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.NUMERIC_STD.ALL;

entity num_ones_for is
    Port ( A : in  STD_LOGIC_VECTOR (15 downto 0);
           A_S : out  STD_LOGIC_VECTOR (15 downto 0));
end num_ones_for;

architecture Behavioral of num_ones_for is
function sort_binary ( ones :unsigned;a : std_logic_vector) return std_logic_vector is
    variable A1: std_logic_vector ( 15 downto 0) := (others =>'0') ;
    begin
    for i in 0 to 15 loop
    if i >(15- ones) then
     A1(i) := '1';
     else
     next;
     end if;
    end loop;
    return A1;
    end function sort_binary;
signal ones : unsigned (4 downto 0);
begin

process(A)
variable count : unsigned(4 downto 0);
begin

count := "00000";   --initialize count variable.
    for i in 0 to 15 loop   --for all the bits.
        count := count + ("0000" & A(i));   --Add the bit to the count.
    end loop;
    ones <= count;    --assign the count to output.
end process;
A_S <= sort_binary(ones =>ones,a =>A);

end Behavioral;

enter image description here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-10
    • 1970-01-01
    • 2015-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多