我已经写了一个通用的进位超前加法器的代码。但是我无法将此代码转换为包中的过程。有人可以帮我吗?
当我尝试编译此代码时,它没有将n
注册为通用编号。
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
entity Carry_Look_Ahead is
Generic(n:integer:=4);
Port ( A : in STD_LOGIC_VECTOR (n-1 downto 0);
B : in STD_LOGIC_VECTOR (n-1 downto 0);
Cin : in STD_LOGIC;
S : out STD_LOGIC_VECTOR (n-1 downto 0);
Cout : out STD_LOGIC);
end Carry_Look_Ahead;
architecture Behavioral of Carry_Look_Ahead is
begin
process(A,B,Cin)
variable ci :STD_LOGIC_VECTOR(n downto 0);
variable P: STD_LOGIC_VECTOR(n-1 downto 0);
variable G: STD_LOGIC_VECTOR(n-1 downto 0);
variable si :STD_LOGIC_VECTOR(n-1 downto 0);
begin
ci(0) := Cin;
FOR i IN 0 TO n-1 LOOP
P(i) := A(i) xor B(i);
G(i) := A(i) and B(i);
ci(i+1) := G(i) or (P(i) and G(i));
si(i) := P(i) xor ci(i);
END LOOP;
S<=si;
Cout<=ci(n);
end process;
end Behavioral;
这是我尝试编译此代码时遇到的错误:
Unknown identifier "n".
答案 0 :(得分:1)
子程序(如过程)可以具有通用参数。语法和用法类似于通用实体或通用包。
procedure cla
generic (
constant n : in positive
)
parameter (
signal Cin : in STD_LOGIC;
signal input1 : in STD_LOGIC_VECTOR;
signal input2 : in STD_LOGIC_VECTOR;
signal Sum : out STD_LOGIC_VECTOR;
signal Cout : out STD_LOGIC
) is
variable c : STD_LOGIC_VECTOR(n downto 0);
variable P : STD_LOGIC_VECTOR(n-1 downto 0);
variable G : STD_LOGIC_VECTOR(n-1 downto 0);
variable s : STD_LOGIC_VECTOR(n-1 downto 0);
begin
c(0) := Cin;
for i in 0 to n-1 loop
P(i) := input1(i) xor input2(i);
G(i) := input1(i) and input2(i);
c(i+1) := G(i) or (P(i) and G(i));
s(i) := P(i) xor c(i);
end loop;
Sum <=s;
Cout <=c(n);
end procedure;
答案 1 :(得分:-1)
我的朋友为此找到了解决方案:
您可以将整数用作通用数字。这是完成的PROCEDURE
:
PROCEDURE cla (
SIGNAL n : IN INTEGER; -- generic number
SIGNAL Cin : IN STD_LOGIC;
SIGNAL input1 , input2 : IN STD_LOGIC_VECTOR;
SIGNAL Sum : OUT STD_LOGIC_VECTOR;
SIGNAL Cout : OUT STD_LOGIC
) IS
VARIABLE c :STD_LOGIC_VECTOR(n downto 0);
VARIABLE P: STD_LOGIC_VECTOR(n-1 DOWNTO 0);
VARIABLE G: STD_LOGIC_VECTOR(n-1 DOWNTO 0);
VARIABLE s :STD_LOGIC_VECTOR(n-1 DOWNTO 0);
BEGIN
c(0) := Cin;
FOR i IN 0 TO n-1 LOOP
P(i) := input1(i) XOR input2(i);
G(i) := input1(i) AND input2(i);
c(i+1) := G(i) OR (P(i) AND G(i));
s(i) := P(i) XOR c(i);
END LOOP;
Sum<=s;
Cout<=c(n);
END cla;