图3.4 十六位全加器组成原理图
16位全加器总共有16位输入,而每四位为一组输入到4位先行进位加法器中进行加法运算,一般输入是两位二进制数,如图是:Y4-1 X4-1 一直到Y16-13 X16-13 为输入;输出为F,共16位输出,而和输出一样四位为一组一起经过运算后输出,不同的是输入有两个二进制数,而输出只有一个二进制数;C表示进位,C0初始值为1,后面的C4,C8,C12,C16分别是每个四位加法器运算后的进位。
2. 软件方案
用VHDL编写代码验证:
在对真值表进行分析和各个功能设计完成之后,就可以使用VHDL编写程序,运用MAX-PLUSⅡ进行模拟仿真,以验证其正确性。如下就是四位先行进位全加器和16位全加器的VHDL代码。
//导入各种所需要的库 library IEEE;
- 11 -
use IEEE.STD_LOGIC_1164.ALL; use IEEE.STD_LOGIC_ARITH.ALL; use IEEE.STD_LOGIC_UNSIGNED.ALL;
-- Uncomment the following lines to use the declarations that are -- provided for instantiating Xilinx primitive components. --library UNISIM;
--use UNISIM.VComponents.all;
entity test is port(
a,b:in std_logic_vector(3 downto 0); //a,b为输入,为4位标准逻辑矢量类型
cin:in std_logic; //cin为输入,为标准矢量类型 s:out std_logic_vector(3 downto 0); //s为输出,为4位标准逻辑矢量类型
cout:out std_logic); //cout为输出,为标准矢量类型 end test;
architecture Behavioral of test is
signal d,t:std_logic_vector(3 downto 0); //信号量d,t为4位标准逻辑矢量
signal c:std_logic_vector(4 downto 0); //信号量c为5位标准逻辑矢量
- 12 -
begin
as_add: for i in 0 to 3 generate //i从0循环到3,循环4次
d(i)<=a(i) and b(i); //把a,b相与的结果赋给d t(i)<=a(i) or b(i); //把a,b相或的结果赋给t s(i)<=a(i) xor b(i) xor c(i); //把a,b,c进行异或的结果赋给s end generate; //以下为赋值语句 c(0)<=cin;
c(1)<=d(0) or (t(0) and c(0));
c(2)<=d(1) or (t(1) and d(0)) or (t(1) and t(0) and c(0));
c(3)<=d(2) or (t(2) and d(1)) or (t(1) and t(2) and d(0)) or (t(1) and t(2) and t(0) and c(0)) ;
c(4)<=d(3) or (t(3) and d(2)) or (t(3) and t(2) and d(1)) or (t(1) and t(2) and t(3) and d(0)) or (t(3) and t(2) and t(1) and t(0) and c(0)); cout<=c(4);
end Behavioral;
如下是16位全加器的VHDL代码。 library IEEE;
use IEEE.std_logic_1164.all; //导入各种需要的库
entity adder16 is
- 13 -
generic(n : integer := 16);
port (a : in std_logic_vector(16 downto 1); //a为16位标准逻辑矢量类型的输入
b : in std_logic_vector(16 downto 1); //b为16位标准逻辑矢量类型的输入
cin : in std_logic; //c为标准逻辑类型的输入 sum : out std_logic_vector(16 downto 1); //sum为16位标准逻辑矢量类型的输入 cout : out std_logic); end adder16;
-- structural implementation of the 16-bit adder architecture structural of adder16 is
component adder
//以下a,b,cin,sum,cout都为标准逻辑类型,其中a,b,cin为输入,sum,cout为输出
port (a : in std_logic; b : in std_logic; cin : in std_logic; sum : out std_logic; cout : out std_logic); end component;
signal carry : std_logic_vector(0 to 16); begin
- 14 -
carry(0) <= cin; //把信号量cin 的值赋给carry(0) cout <= carry(16); //把信号量carry(16)的值赋给cout
-- instantiate a single-bit adder 16 times
gen: for I in 1 to 16 generate //I从1循环至16,循环16次
add: adder port map(
//以下为赋值语句
a => a(I), b => b(I), cin => carry(I - 1), sum => sum(I), cout => carry(I));
end generate; end structural;
-- behavioral implementation of the 16-bit adder
architecture behavioral of adder16 is //定义结构体 begin
p1: process(a, b, cin) //过程开始
variable vsum : std_logic_vector(16 downto 1); variable carry : std_logic;
begin
- 15 -
carry := cin;
百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库16位全加器(3)在线全文阅读。
相关推荐: