2013-03-06 97 views
0
library IEEE; 
use IEEE.STD_LOGIC_1164.ALL; 

entity Lab3_Adder1 is 
    Port (cin : in STD_LOGIC; 
      a : in STD_LOGIC_VECTOR (3 downto 0); 
      b : in STD_LOGIC_VECTOR (3 downto 0); 
      s : out STD_LOGIC_VECTOR (3 downto 0); 
      cout : out STD_LOGIC); 
end Lab3_Adder1; 

architecture Behavioral of Lab3_Adder1 is 

    SIGNAL c : STD_LOGIC_VECTOR (4 DOWNTO 0); 

begin 
    c(0) <= cin; 
    s <= a XOR b XOR c (3 DOWNTO 0); 
    c (4 DOWNTO 1) <= (a AND b) OR (a AND c(3 DOWNTO 0)) OR (b AND c(3 DOWNTO 0)); 
    cout <= c(4); 
end Behavioral; 

你好,这是我第一次使用这个论坛。我在VHDL上做了华莱士树乘法。上面的代码是完整加法器的代码。我想知道我们如何在主代码中调用函数/组件? (如在C编程中)。我会在我的主代码中调用这个完整的加法器。 vhdl乘法器

回答

5

您可以像在C中一样调用VHDL中的函数 - 既可以初始化常量,信号或变量,也可以作为过程中的顺序语句。但是这个并不重要。

但是你不叫组件!这就像在C++中调用一个对象 - 它绝对没有意义!

在VHDL中,您可以实例化组件或(更简单!)实体,并使用信号互连其端口。这(非常粗暴地)更像是以面向对象的语言来声明对象和发送消息。这就是所谓的“结构性VHDL”,经常出现在VHDL设计的顶尖水平,创建和CPU,内存接口,FFT处理器等

互连元件鉴于你的实体

entity Lab3_Adder1 is 
    Port (cin : in STD_LOGIC; 
      a : in STD_LOGIC_VECTOR (3 downto 0); 
      b : in STD_LOGIC_VECTOR (3 downto 0); 
      s : out STD_LOGIC_VECTOR (3 downto 0); 
      cout : out STD_LOGIC); 
end Lab3_Adder1; 

我可以建立例如一个8位加法器,如下所示:

entity Adder_8bit is 
    Port (cin : in STD_LOGIC; 
      a : in STD_LOGIC_VECTOR (7 downto 0); 
      b : in STD_LOGIC_VECTOR (7 downto 0); 
      s : out STD_LOGIC_VECTOR (7 downto 0); 
      cout : out STD_LOGIC); 
end Adder_8bit; 

architecture Structural of Adder_8bit is 

signal carry_int : std_logic; -- between lower and upper halves 

begin 
-- We need to create and connect up two adders 

LSB_adder : entity work.Lab3_Adder1 
    Port Map( 
      cin => cin, 
      a => a(3 downto 0), 
      b => b(3 downto 0), 
      s => s(3 downto 0), 
      cout => carry_int 
    ); 
MSB_adder : entity work.Lab3_Adder1 
    Port Map( 
      cin => carry_int, 
      a => a(7 downto 4), 
      b => b(7 downto 4), 
      s => s(7 downto 4), 
      cout => cout 
    ); 

end Structural; 
0

可以定义VHDL-功能,其取代组合电路并且其可以在主VHDL码类似于C的功能的任何地方被调用。

您需要首先在函数定义所在的位置定义一个包。

======= myAdders.vhdl ==============

library IEEE; 
use IEEE.std_logic_1164.all; 
use IEEE.std_logic_unsigned.all; 

package myAdders is 

function Lab3_Adder1(cin : in STD_LOGIC; 
a : in STD_LOGIC_VECTOR (3 downto 0); 
b : in STD_LOGIC_VECTOR (3 downto 0); 
s : out STD_LOGIC_VECTOR (3 downto 0)) return std_logic; 
end Lab3_Adder1; 

end myAdders; 

package body myAdders is 


function Lab3_Adder1 (cin : in STD_LOGIC; 
a : in STD_LOGIC_VECTOR (3 downto 0); 
b : in STD_LOGIC_VECTOR (3 downto 0); 
s : out STD_LOGIC_VECTOR (3 downto 0)) return std_logic is 
variable c: std_logic_vector(4 downto 0); 
begin 

c(0) := cin; 
s := a XOR b XOR c (3 DOWNTO 0); 
c (4 DOWNTO 1) := (a AND b) OR (a AND c(3 DOWNTO 0)) OR (b AND c(3 DOWNTO 0)); 
return c(4); 
end Lab3_Adder1; 


end myAdders; 

======= topLevel.vhdl ===== =========

library IEEE; 
use IEEE.std_logic_1164.all; 
use IEEE.std_logic_unsigned.all; 
use work.myAddres.all; 


entity TopLevel is 
    Port ( 
      cin : in STD_LOGIC; 
      a : in STD_LOGIC_VECTOR (3 downto 0); 
      b : in STD_LOGIC_VECTOR (3 downto 0); 
      c : out STD_LOGIC_VECTOR (3 downto 0) 
      ); 
end TopLevel; 

architecture Structural of TopLevel is 

signal carry : std_logic; 

begin 

carry <= Lab3_Adder1(cin, a, b, c); 

... and so on ... 

end Structural;