1

作为我们进行遗传算法的学校项目的一部分,我正在用 VHDL 编写一种称为“交叉核心”的东西。这个核心应该接受两个 64 位输入“父母”,两个输出“孩子”应该包含来自两个输入的部分。

此交叉的起点基于输入 random_number 的值,其中 6 位值确定从何处开始交叉的位号。

例如,如果 random_number 的值为 7(以 10 为底),并且输入只有一个为 0,而另一个只有 1,那么输出应该是这样的:

000.....00011111111 和 111.....11100000000

(交叉从第 7 位开始)

这是 VHDL 代码:

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

entity crossover_core_split is
    generic (
        N : integer := 64;
        R : integer := 6
    );
    port (
        random_number : in  STD_LOGIC_VECTOR(R-1 downto 0);
        parent1       : in  STD_LOGIC_VECTOR(N-1 downto 0);
        parent2       : in  STD_LOGIC_VECTOR(N-1 downto 0);
        child1        : out STD_LOGIC_VECTOR(N-1 downto 0);
        child2        : out STD_LOGIC_VECTOR(N-1 downto 0)
    );
end crossover_core_split;

architecture Behavioral of crossover_core_split is
        signal split : INTEGER := 0; 
begin

    split <= TO_INTEGER(UNSIGNED(random_number));

    child1 <= parent1(N-1 downto split+1) & parent2(split downto 0);
    child2 <= parent2(N-1 downto split+1) & parent1(split downto 0);

end Behavioral;

代码在 Xilinx ISE Project Navigator 12.4 中编写和编译。我已经在 ModelSim 中对此进行了测试,并验证了它是否有效。但是,闩锁存在问题,我收到以下警告:

WARNING:Xst:737 - Found 1-bit latch for signal <child1<62>>. Latches may be generated from incomplete case or if statements. We do not recommend the use of latches in FPGA/CPLD designs, as they may lead to timing problems.
WARNING:Xst:737 - Found 1-bit latch for signal <child1<61>>. Latches may be generated from incomplete case or if statements. We do not recommend the use of latches in FPGA/CPLD designs, as they may lead to timing problems.  
ETC ETC ETC...

WARNING:Xst:1336 -  (*) More than 100% of Device resources are used

总共生成了 128 个锁存器,但显然不推荐使用它们。关于如何避免闩锁或至少减少闩锁的任何建议?

4

1 回答 1

3

该代码不太适合合成:子向量的长度不应变化,这可能是锁存器的原因。对我来说,最好的解决方案是从随机值创建一个掩码:您可以通过多种方式做到这一点(它通常是二进制到温度的转换)。例如(这不是最佳的):

process(random_number)
begin
  for k in 0 to 63 loop
    if k <= to_integer(unsigned(random_number)) then
      mask(k) <= '1';
    else
      mask(k) <= '0';
    end if;
  end loop;
end process;

那么一旦你有了掩码值,你就可以简单地写:

child1 <= (mask and parent1) or ((not mask) and parent2);
child2 <= (mask and parent2) or ((not mask) and parent1);
于 2013-11-06T16:59:22.687 回答