编写翻转第 n 位的代码
Write code that flip the nth bit
正如标题所说,我需要编写一个 vhdl 代码,将一个 32 位向量和一个 6 位向量作为输入。我需要输出另一个 32 位向量,它等于输入的 32 位向量,但它的第 n 位被翻转了。 n = 6 位向量的编号。这是我的代码,但不正确。
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.std_logic_arith.all;
use ieee.std_logic_unsigned.all;
use ieee.numeric_std.all;
entity flipMSB is
Port ( Anotf : in STD_LOGIC_VECTOR (31 downto 0);
count : in STD_LOGIC_VECTOR (5 downto 0);
Af : out STD_LOGIC_VECTOR (31 downto 0));
end flipMSB;
architecture bhv of flipMSB is
signal sig: STD_LOGIC_VECTOR(31 downto 0);
signal n : integer;
begin
n<=CONV_INTEGER(count);
sig<=Anotf;
sig(n)<=not sig(n);
Af<=sig;
end bhv;
首先,一个 6 位数字上升到 64,你的 count
信号只需要 5 位!
第二个:
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.std_logic_arith.all;
use ieee.std_logic_unsigned.all;
use ieee.numeric_std.all;
std_logic_arith
和 numeric_std
类型冲突。由于 std_logic_arith
和 std_logic_unsigned
不是 VHDL 标准(和 IEEE,尽管库名称)的一部分,我建议您只使用 numeric_std
。如果使用VHDL-2008,可以使用numeric_std_unsigned
。您需要将 n <= conv_integer(count)
替换为 n <= to_integer(unsigned(count))
最后,
sig<=Anotf;
sig(n)<=not sig(n);
位n
会有两个输出驱动器,这是不好的。如果您将该逻辑放入一个流程中,那会很好,因为对 sig(n)
的第一次分配将被覆盖(而不是驱动两次):
process(Anotf, count)
variable n : natural;
begin
Af <= Anotf;
n := to_integer(unsigned(count));
Af(n) <= not Anotf(n);
end process;
这样想,如果两个进程驱动相同的信号,这将导致两个驱动程序(并且发生冲突!)。进程外的语句隐含在它自己的进程中。此外,在一个过程中,只有最后一个分配信号的语句才会起作用。
正如标题所说,我需要编写一个 vhdl 代码,将一个 32 位向量和一个 6 位向量作为输入。我需要输出另一个 32 位向量,它等于输入的 32 位向量,但它的第 n 位被翻转了。 n = 6 位向量的编号。这是我的代码,但不正确。
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.std_logic_arith.all;
use ieee.std_logic_unsigned.all;
use ieee.numeric_std.all;
entity flipMSB is
Port ( Anotf : in STD_LOGIC_VECTOR (31 downto 0);
count : in STD_LOGIC_VECTOR (5 downto 0);
Af : out STD_LOGIC_VECTOR (31 downto 0));
end flipMSB;
architecture bhv of flipMSB is
signal sig: STD_LOGIC_VECTOR(31 downto 0);
signal n : integer;
begin
n<=CONV_INTEGER(count);
sig<=Anotf;
sig(n)<=not sig(n);
Af<=sig;
end bhv;
首先,一个 6 位数字上升到 64,你的 count
信号只需要 5 位!
第二个:
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.std_logic_arith.all;
use ieee.std_logic_unsigned.all;
use ieee.numeric_std.all;
std_logic_arith
和 numeric_std
类型冲突。由于 std_logic_arith
和 std_logic_unsigned
不是 VHDL 标准(和 IEEE,尽管库名称)的一部分,我建议您只使用 numeric_std
。如果使用VHDL-2008,可以使用numeric_std_unsigned
。您需要将 n <= conv_integer(count)
替换为 n <= to_integer(unsigned(count))
最后,
sig<=Anotf;
sig(n)<=not sig(n);
位n
会有两个输出驱动器,这是不好的。如果您将该逻辑放入一个流程中,那会很好,因为对 sig(n)
的第一次分配将被覆盖(而不是驱动两次):
process(Anotf, count)
variable n : natural;
begin
Af <= Anotf;
n := to_integer(unsigned(count));
Af(n) <= not Anotf(n);
end process;
这样想,如果两个进程驱动相同的信号,这将导致两个驱动程序(并且发生冲突!)。进程外的语句隐含在它自己的进程中。此外,在一个过程中,只有最后一个分配信号的语句才会起作用。