【发布时间】: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;
【问题讨论】:
-
你在架构声明部分而不是在架构声明部分声明了信号。变量计数是在流程语句的语句序列中声明的,而不是在流程语句的声明部分中声明的。两者都位于前面各自保留字
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,否则表示空范围。将不会执行具有空范围的循环语句中的语句序列。右边界表达式不需要括号,方向应该向下。