【问题标题】:vhdl multipliersvhdl 乘数
【发布时间】:2013-03-06 15:20:09
【问题描述】:
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 multiplication xilinx hdl


    【解决方案1】:

    您在 VHDL 中调用函数就像在 C 中一样 - 要么初始化常量、信号或变量,要么作为进程中的顺序语句。但这并不重要。

    但是你不调用组件!这就像在 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;
    

    【讨论】:

      【解决方案2】:

      您可以定义替代组合电路的 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;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-11-21
        • 1970-01-01
        • 2016-06-19
        • 1970-01-01
        • 2013-02-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多