ALU(算术逻辑运算单元)的设计

更新时间:2023-04-24 06:18:01 阅读量: 实用文档 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

EDA技术与应用 实验报告(四)

实验名称: ALU(算术逻辑运算单元)的设

姓 名: 学 号: 班 级: 时 间:

南京理工大学紫金学院电光系

陈丹 100401202 电信(2)班 2012.12.11

一、 实验目的

1、学习包集和元件例化语句的使用。 2、学习ALU电路的设计。

二、 实验原理

1、ALU原理 ALU的电路原理图如图1 所示,主要由算术运算单元、逻辑单元、选择单元构成。

图1 表

1

ALU功能表如表1 所示。

2、元件、包集

在结构体的层次化设计中,采用结构描述方法就是通过调用库中的元件或者已经设计好

的模块来完成相应的设计。在这种结构体中,功能描述就像网表一样来表示模块和模块之间的互联。如ALU 是由算术单元、逻辑单元、多路复用器互相连接而构成。而以上三个模块是由相应的VHDL 代码产生的,在VHDL 输入方式下,如果要将三个模块连接起来,就要用到元件例化语句。元件例化语句分为元件声明和元件例化。 1、元件声明

在VHDL 代码中要引入设计好的模块,首先要在结构体的说明部分对要引入的模块进行说明。然后使用元件例化语句引入模块。

元件声明语句格式:

component 引入的元件(或模块)名

port(端口说明);

end component;

注意:元件说明语句要放在“architecture”和“begin”之间。

2、元件例化语句

为将引入的元件正确地嵌入到高一层的结构体描述中,就必须将被引用的元件端口信号与结构体相应端口信号正确地连接起来,元件例化语句可以实现该功能。 元件例化语句格式:

标号名:元件名(模块名) port map(端口映射);

标号名是元件例化语句的唯一标识,且结构体中的标识必须是唯一的;端口映射分为:位置映射、

名称映射。

位置映射指 port map 中实际信号的书写顺序与component 中端口说明中的信号书写顺序一致,位置映射对书写顺序要求很严格,不能颠倒;名称映射指port map 中将引用的元件的端口信号名称赋予结构体中要使用元件的各个信号,名称映射的书写顺序要求不严格,顺序可以颠倒。 3 包集

在实体及结构体中定义的对象、数据类型,对另外代码的实体是不能使用的。但是在同一工程的不同VHDL 文件中,有些对象、数据类型、子程序等常常被重复使用。为方便VHDL 代码的编写,简化电路设计,故引入包集。包集也称为程序包。

包集主要由两部分组成:程序包说明和程序包体。其中,程序包体是可选的,一般程序包说明列出所有项的名称,而程序包体给出各项的细节。

程序包说明中包含的内容很多,只要是通用的全局量,都可以在程序包中加以说明。主要内容如下:

★ 对象(常量、变量、信号)的数据类型说明。

★ 对象(常量、变量、信号)子类型的数值范围说明。 ★ 函数与过程说明。 ★ 元件语句说明。

程序包说明的书写格式如下: package 程序包名 is 说明语句;

end 程序包名;

程序包名:设计者自定义便于记忆的标识符。说明语句:包括各种类型的说明语句。 程序包体书写格式如下: package body 程序包名 is

52

顺序语句;

end 程序包名;

注意:程序包定义的内容不是自动可见的,不是自动被使用。若某个实体及结构体设计需要使用程序包,可以使用use 语句制定要使用的程序包。 如:use work.程序包名.all;

三、 实验内容

1、 算术单元 arith_unit

library ieee;

use ieee.std_logic_1164.all; use ieee.std_logic_unsigned.all; entity arith_unit is

port(a,b:in std_logic_vector(7 downto 0); sel:in std_logic_vector(2 downto 0);

cin:in std_logic;

x:out std_logic_vector(7 downto 0));

end;

architecture rhg of arith_unit is begin

with sel select x<=a when"000",

a+1 when"001", a-1 when"010", b when"011", b+1 when"100", b-1 when"101", a+b when"110",

a+b+cin when others; end rhg; 仿真结果:

2、逻辑单元 logic_unit library ieee;

use ieee.std_logic_1164.all;

use ieee.std_logic_unsigned.all;

entity logic_unit is port(a,b:in std_logic_vector(7 downto 0); sel:in std_logic_vector(2 downto 0);

x:out std_logic_vector(7 downto 0));

end;

architecture rhg of logic_unit is begin with sel select

x<=not a when"000", not b when"001",

a and b when"010", a or b when"011", a nand b when"100", a nor b when"101", a xor b when"110", not(a xor b) when others;

end rhg; 仿真结果:

3、 多路复用器 sel

library ieee;

use ieee.std_logic_1164.all; entity sel is port(sel:in std_logic;

arith:in std_logic_vector(7 downto 0);

logic:in std_logic_vector(7 downto 0);

y:out std_logic_vector(7 downto 0)); end;

architecture rhg of sel is begin with sel select

y<=arith when '0',

logic when others;

end; 仿真结果:

4、 包集 package alu

library ieee;

use ieee.std_logic_1164.all;

package alu is component arith_unit is port(a,b:in std_logic_vector(7 downto 0); sel:in std_logic_vector(2 downto 0);

cin:in std_logic;

x:out std_logic_vector(7 downto 0));

end component; component logic_unit is

port(a,b:in std_logic_vector(7 downto 0); sel:in std_logic_vector(2 downto 0); x:out std_logic_vector(7 downto 0));

end component ; component sel is

port(sel:in std_logic;

arith:in std_logic_vector(7 downto 0);

logic:in std_logic_vector(7 downto 0); y:out std_logic_vector(7 downto 0)); end component;

component clk_4 is port(clk:in std_logic;

y:buffer std_logic_vector(3 downto 0)); end component; end alu; 5、 clk_4

library ieee;

use ieee.std_logic_1164.all; use ieee.std_logic_unsigned.all; entity clk_4 is port(clk:in std_logic;

y:buffer std_logic_vector(3 downto 0)); end;

architecture rhg of clk_4 is begin

process(clk) variable s:std_logic_vector(3 downto 0); begin

if clk'event and clk='1' then

if s="1111" then

s:="0000"; else

s:=s+1; end if; end if;

y<=s; end process; end rhg; 仿真结果:

6、 主程序 alu_unit

library ieee;

use ieee.std_logic_1164.all; use work.alu.all;

entity alu_unit is port(a,b:in std_logic_vector(7 downto 0);

clk:in std_logic;

cin:in std_logic;

selout:out std_logic_vector(3 downto 0); y:out std_logic_logic_vector(7 downto 0)); end;

architecture thg of alu_unit is signal x1,x2:std_logic_vector(7 downto 0);

signal x3:std_logic_vector(7 downto 0); begin u1:arith_unit port map(a,b,x3(2 downto 0),cin,x1); u2:logic_unit port map(a,b,x3(2 downto 0),x2); u3:sel port map(x3(3),x1,x2,y); u4:clk_4 port map(clk,x3); selout<=x3; end; 仿真结果:

管脚配置:

四、 小结与体会

通过这次实验我知道了什么是顺序关联法和名称关联法以及他们的区别。

在VHDL代码中要引入设计好的模块,首先要在结构体的说明部分对要引入的模块进行说明。然后使用元件例化语句引入模块。掌握了如何使用多个模块编译代码,掌握了如何使用包集。

注意每次运行检验的时候都要将文件置顶,否则编译无法通过。

本文来源:https://www.bwwdw.com/article/lzxq.html

Top