VHDL 中的冒泡排序不会排序

问题描述 投票:0回答:0

如果输入数组是(4,3,2,1),输出数组在(4,2,4,1)和输入之间振荡。 这是我的代码。我在 VIVADO 工作,语言为 VHDL。我想在 Xilinx Artix-7 Basys Dilligent FPGA 上实现它。


library IEEE;
use IEEE.STD_LOGIC_1164.ALL;


entity please_work is
Port ( 
            clk : in std_logic;
            enable : in std_logic ;
            rst : in std_logic ;
            arr_in : in integer_vector (0 to 3);
            arr_out : out integer_vector (0 to 3)
          );
end please_work;

architecture Behavioral of please_work is

signal ph : integer_vector (0 to 3);
signal sorted : std_logic := '0';

begin

process (rst,clk)
variable i,j: integer range 0 to 3;
variable temp: integer;
begin
    if (rst='1') then
        sorted<='0';
        ph<=(0,0,0,0);
        i:=0;
        j:=0;
    elsif (clk'event and clk='1') then
        ph<=arr_in;
        if (enable='1') then
                for i in 0 to 2 loop
                    for j in 0 to 1 loop
                        if (ph(j)>ph(j+1)) then
                            temp := ph(j);
                            ph(j) <= ph(j+1);
                            ph(j+1) <= temp;
                        end if;
                    end loop;
                end loop;
                sorted <= '1';
            end if;
        end if;
    
end process;
arr_out<=ph;


end Behavioral;

模拟:

enter image description here

enter image description here

任何帮助表示赞赏。我是 VHDL 的初学者,所以请原谅我的错误。我别无选择,只能做这个项目;我的教授告诉我的。

我试过带时钟和不带时钟的冒泡排序。我尝试在程序中定义要排序的数组。我试过 while 循环。没有任何效果。 如果我输入一个数组 (4,3,2,1),我期望 (1,2,3,4) 作为我的输出。

vhdl fpga bubble-sort
© www.soinside.com 2019 - 2024. All rights reserved.