基于4位加法器的VHDL 4位乘法器

基于4位加法器的VHDL 4位乘法器,vhdl,fpga,multiplication,Vhdl,Fpga,Multiplication,我对VHDL有点陌生,我尝试通过示例学习。长话短说,我从一些基本的例子开始,比如创建这个全加器。 我还使用了一个4位加法器测试台文件,我发现输出是正确的。现在我正试图用4位加法器实现一个4位乘法器,但我有点卡住了。实际上,这就是我试图实现的乘数 我写的代码是这样的,但是我被端口映射卡住了 --library library IEEE; use IEEE.std_logic_1164.all; use IEEE.std_logic_textio.all; use IEEE.st

我对VHDL有点陌生,我尝试通过示例学习。长话短说,我从一些基本的例子开始,比如创建这个全加器。

我还使用了一个4位加法器测试台文件,我发现输出是正确的。现在我正试图用4位加法器实现一个4位乘法器,但我有点卡住了。实际上,这就是我试图实现的乘数

我写的代码是这样的,但是我被端口映射卡住了

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

--entity
entity multy is
  port (x: in std_logic_vector(3 downto 0);
        y: in std_logic_vector(3 downto 0);
        p : out std_logic_vector(7 downto 0)
        );
end multy ;

-- architecture
architecture rtl of multy is
component Ripple_Adder
  Port ( A : in std_logic_vector (3 downto 0);
     B : in std_logic_vector (3 downto 0);
     Cin : in std_logic;
     S : out std_logic_vector (3 downto 0);
     Cout : out std_logic);
end component ;

signal andgate: std_logic_vector(15 downto 0);
signal sumout: std_logic_vector( 11 downto 0);
signal carry: std_logic_vector(11 downto 0);


begin
    andgate(0) <= x(0) and y(0);
    andgate(1) <= x(1) and y(0); --b0
    andgate(2) <= x(2) and y(0); --b1
    andgate(3) <= x(3) and y(0); --b2
B

    andgate(4) <= x(0) and y(1);
    andgate(5) <= x(1) and y(1);
    andgate(6) <= x(2) and y(1);
    andgate(7) <= x(3) and y(1);

    andgate(8) <= x(0) and y(2);
    andgate(9) <= x(1) and y(2);
    andgate(10) <= x(2) and y(2);
    andgate(11) <= x(3) and y(2);

    andgate(12) <= x(0) and y(3);
    andgate(13) <= x(1) and y(3);
    andgate(14) <= x(2) and y(3);
    andgate(15) <= x(3) and y(3);

--gates


cell_1: Ripple_Adder port map();
cell_2: Ripple_Adder port map();
cell_3: Ripple_Adder port map();


 --Assigning p values
    p(0) <= andgate(0);
    p(1) <= sumout(0);
    p(2) <= sumout(4);
    p(3) <= sumout(8);
    p(4) <= sumout(9);
    p(5) <= sumout(10);
    p(6) <= sumout(11);
    p(7) <= carry(11);

end rtl ;  
--库
图书馆IEEE;
使用IEEE.std_logic_1164.all;
使用IEEE.std_logic_textio.all;
使用IEEE.std_logic_unsigned.all;
--实体
实体multy是
端口(x:std_逻辑_向量中(3到0);
y:标准逻辑向量(3到0);
p:out标准逻辑向量(7到0)
);
结束多蒂;
--建筑
multy的rtl体系结构是
元件纹波加法器
端口(A:标准逻辑向量(3到0);
B:标准逻辑向量(3到0);
Cin:标准逻辑;
S:输出标准逻辑向量(3到0);
Cout:输出标准逻辑);
端部元件;
信号与门:标准逻辑向量(15到0);
信号求和:标准逻辑向量(11到0);
信号进位:标准逻辑向量(11到0);
开始
andgate(0)“我卡在端口映射上”不是一个特定的问题陈述

与maps中正式端口的命名关联成员可以单独关联,也可以整体关联,只要正式端口的所有成员都关联-IEEE Std 1076-2008 6.5.7关联列表:

正式接口对象应为明确声明的接口对象或此类接口对象的成员(见5.1)。在前一种情况下,这样一种形式被称为整体关联。在后一种情况下,应使用命名的关联来关联正式的和实际的;这种形式的子元素称为单独关联的。此外,显式声明的接口对象的每个标量子元素应与同一关联列表中的实际(或其子元素)精确关联一次,并且所有此类关联应在该关联列表中以连续顺序出现。将接口对象的切片或子元素(或其切片)关联的每个关联元素应使用本地静态名称标识形式

请注意,您有太多的进位元素(只需要两个)、不需要andgate(0)、不需要sumout(0)、sumout(4)或sumout(11 down)或sumout(8),multy体系结构中有一个无关字符,缺少上下文子句,并且有未使用的use子句

使用数组中介信号的代码:

library ieee;
use ieee.std_logic_1164.all;
-- use ieee.std_logic_textio.all;   -- NOT USED
-- use ieee.std_logic_unsigned.all; -- NOT USED

entity multy is 
    port (
        x: in  std_logic_vector (3 downto 0);
        y: in  std_logic_vector (3 downto 0);
        p: out std_logic_vector (7 downto 0)
    );
end entity multy;

architecture rtl of multy is
    component Ripple_Adder
        port ( 
            A:      in  std_logic_vector (3 downto 0);
            B:      in  std_logic_vector (3 downto 0);
            Cin:    in  std_logic;
            S:      out std_logic_vector (3 downto 0);
           Cout:    out std_logic
        );
    end component;
-- AND Product terms:
    signal G0, G1, G2:  std_logic_vector (3 downto 0);
-- B Inputs (B0 has three bits of AND product)
    signal B0, B1, B2:  std_logic_vector (3 downto 0);

begin

    -- y(1) thru y (3) AND products, assigned aggregates:
    G0 <= (x(3) and y(1), x(2) and y(1), x(1) and y(1), x(0) and y(1));
    G1 <= (x(3) and y(2), x(2) and y(2), x(1) and y(2), x(0) and y(2));
    G2 <= (x(3) and y(3), x(2) and y(3), x(1) and y(3), x(0) and y(3));
    -- y(0) AND products (and y0(3) '0'):
    B0 <=  ('0',          x(3) and y(0), x(2) and y(0), x(1) and y(0));

-- named association:
cell_1: 
    Ripple_Adder 
        port map (
            a => G0,
            b => B0,
            cin => '0',
            cout => B1(3), -- named association can be in any order
            S(3) => B1(2), -- individual elements of S, all are associated
            S(2) => B1(1), -- all formal members must be provide contiguously
            S(1) => B1(0),
            S(0) => p(1)
        );
cell_2: 
    Ripple_Adder 
        port map (
            a => G1,
            b => B1,
            cin => '0',
            cout => B2(3),
            S(3) => B2(2),
            S(2) => B2(1),
            S(1) => B2(0),
            S(0) => p(2)
        );
cell_3: 
    Ripple_Adder 
        port map (
            a => G2,
            b => B2,
            cin => '0',
            cout => p(7),
            S => p(6 downto 3)  -- matching elements for formal
        );
    p(0) <= x(0) and y(0); 
end architecture rtl;
ieee库;
使用ieee.std_logic_1164.all;
--使用ieee.std\U逻辑\U文本IO.all;——不用
--使用ieee.std_逻辑_unsigned.all;——不用
实体multy是
港口(
x:标准逻辑向量(3到0);
y:标准逻辑向量(3到0);
p:out标准逻辑向量(7到0)
);
终端实体多;
multy的rtl体系结构是
元件纹波加法器
港口(
A:标准逻辑向量(3到0);
B:标准逻辑向量(3到0);
Cin:标准逻辑;
S:输出标准逻辑向量(3到0);
Cout:out标准逻辑
);
端部元件;
--及产品条款:
信号G0、G1、G2:std_逻辑_向量(3到0);
--B输入(B0有三位和乘积)
信号B0、B1、B2:标准逻辑向量(3到0);
开始
--y(1)至y(3)和产品、指定骨料:
G0 B1(3),--命名关联可以是任意顺序
S(3)=>B1(2),--S的单个元素,所有元素都是关联的
S(2)=>B1(1),--必须连续提供所有正式成员
S(1)=>B1(0),
S(0)=>p(1)
);
单元2:
纹波加法器
港口地图(
a=>G1,
b=>B1,
cin=>“0”,
cout=>B2(3),
S(3)=>B2(2),
S(2)=>B2(1),
S(1)=>B2(0),
S(0)=>p(2)
);
单元3:
纹波加法器
港口地图(
a=>G2,
b=>B2,
cin=>“0”,
cout=>p(7),
S=>p(6到3)——匹配形式化
);

这是我真正想要的。你的解释真的很有帮助,它真的帮助了我理解了很多。主要是我对如何将
y(1)到y(3)和产品
添加到
a
b
输入感到困惑。再次感谢你。
--library
library IEEE;
   use IEEE.std_logic_1164.all;
   use IEEE.std_logic_textio.all;
   use IEEE.std_logic_unsigned.all;

--entity
entity multy is
  port (x: in std_logic_vector(3 downto 0);
        y: in std_logic_vector(3 downto 0);
        p : out std_logic_vector(7 downto 0)
        );
end multy ;

-- architecture
architecture rtl of multy is
component Ripple_Adder
  Port ( A : in std_logic_vector (3 downto 0);
     B : in std_logic_vector (3 downto 0);
     Cin : in std_logic;
     S : out std_logic_vector (3 downto 0);
     Cout : out std_logic);
end component ;

signal andgate: std_logic_vector(15 downto 0);
signal sumout: std_logic_vector( 11 downto 0);
signal carry: std_logic_vector(11 downto 0);


begin
    andgate(0) <= x(0) and y(0);
    andgate(1) <= x(1) and y(0); --b0
    andgate(2) <= x(2) and y(0); --b1
    andgate(3) <= x(3) and y(0); --b2
B

    andgate(4) <= x(0) and y(1);
    andgate(5) <= x(1) and y(1);
    andgate(6) <= x(2) and y(1);
    andgate(7) <= x(3) and y(1);

    andgate(8) <= x(0) and y(2);
    andgate(9) <= x(1) and y(2);
    andgate(10) <= x(2) and y(2);
    andgate(11) <= x(3) and y(2);

    andgate(12) <= x(0) and y(3);
    andgate(13) <= x(1) and y(3);
    andgate(14) <= x(2) and y(3);
    andgate(15) <= x(3) and y(3);

--gates


cell_1: Ripple_Adder port map();
cell_2: Ripple_Adder port map();
cell_3: Ripple_Adder port map();


 --Assigning p values
    p(0) <= andgate(0);
    p(1) <= sumout(0);
    p(2) <= sumout(4);
    p(3) <= sumout(8);
    p(4) <= sumout(9);
    p(5) <= sumout(10);
    p(6) <= sumout(11);
    p(7) <= carry(11);

end rtl ;  
library ieee;
use ieee.std_logic_1164.all;
-- use ieee.std_logic_textio.all;   -- NOT USED
-- use ieee.std_logic_unsigned.all; -- NOT USED

entity multy is 
    port (
        x: in  std_logic_vector (3 downto 0);
        y: in  std_logic_vector (3 downto 0);
        p: out std_logic_vector (7 downto 0)
    );
end entity multy;

architecture rtl of multy is
    component Ripple_Adder
        port ( 
            A:      in  std_logic_vector (3 downto 0);
            B:      in  std_logic_vector (3 downto 0);
            Cin:    in  std_logic;
            S:      out std_logic_vector (3 downto 0);
           Cout:    out std_logic
        );
    end component;
-- AND Product terms:
    signal G0, G1, G2:  std_logic_vector (3 downto 0);
-- B Inputs (B0 has three bits of AND product)
    signal B0, B1, B2:  std_logic_vector (3 downto 0);

begin

    -- y(1) thru y (3) AND products, assigned aggregates:
    G0 <= (x(3) and y(1), x(2) and y(1), x(1) and y(1), x(0) and y(1));
    G1 <= (x(3) and y(2), x(2) and y(2), x(1) and y(2), x(0) and y(2));
    G2 <= (x(3) and y(3), x(2) and y(3), x(1) and y(3), x(0) and y(3));
    -- y(0) AND products (and y0(3) '0'):
    B0 <=  ('0',          x(3) and y(0), x(2) and y(0), x(1) and y(0));

-- named association:
cell_1: 
    Ripple_Adder 
        port map (
            a => G0,
            b => B0,
            cin => '0',
            cout => B1(3), -- named association can be in any order
            S(3) => B1(2), -- individual elements of S, all are associated
            S(2) => B1(1), -- all formal members must be provide contiguously
            S(1) => B1(0),
            S(0) => p(1)
        );
cell_2: 
    Ripple_Adder 
        port map (
            a => G1,
            b => B1,
            cin => '0',
            cout => B2(3),
            S(3) => B2(2),
            S(2) => B2(1),
            S(1) => B2(0),
            S(0) => p(2)
        );
cell_3: 
    Ripple_Adder 
        port map (
            a => G2,
            b => B2,
            cin => '0',
            cout => p(7),
            S => p(6 downto 3)  -- matching elements for formal
        );
    p(0) <= x(0) and y(0); 
end architecture rtl;
library ieee;
use ieee.std_logic_1164.all;

entity multy_tb is           -- testbench
end entity;

architecture foo of multy_tb is
    signal x, y:        std_logic_vector (3 downto 0);
    signal yp, rp:      std_logic_vector (7 downto 0);

    use ieee.numeric_std.all;

    function to_string (inp: std_logic_vector) return string is
        variable image_str: string (1 to inp'length);
        alias input_str:  std_logic_vector (1 to inp'length) is inp;
    begin
        for i in input_str'range loop
            image_str(i) := character'VALUE(std_ulogic'IMAGE(input_str(i)));
        end loop;
        return image_str;
    end function;

begin
DUT:
    entity work.multy
        port map (
            x => x,
            y => y,
            p => yp
        );
STIMULI:
    process
    begin
        for i in 0 to 15 loop
            x <= std_logic_vector(to_unsigned(i, x'length));
            for j in 0 to 15 loop
                y <= std_logic_vector(to_unsigned(j, y'length));
                wait for 0 ns; -- assignments take effect
                rp <= std_logic_vector(unsigned (x) * unsigned(y));
                wait for 10 ns;
                if yp /= rp then
                    report "multy error";
                    report HT & "expected " & to_string (rp);
                    report HT & "got      " & to_string (yp);
                end if;
            end loop;
        end loop;
        wait;
    end process;
end architecture;