FPGA实验报告
姓名: 学号:
专业: 电子信息工程 老师:
项目名称:算术逻辑单元(
ALU) 2014.5
1. 项目详细描述:
1开发平台: ○
Xilinx ISE Design Software Suite 10.1,Digilent’s Adept,一台电脑,Spartan-3E FPGA开发板一张。
2原理: ○
ALU是一种组合电路,执行一系列基本的算术和逻辑运算。它有很多选择线用来确定要执行的操作。选择线在ALU中进行解码,所以m选择线路可以指定多达2不同的操作。图p8-1显示一个典型的n位算术逻辑单元的符号。N位输入数据包含n个B数据输入和A数据输入相结合产生一个n位数据输出G。通过S3,S2,S1,S0来进行各种运算的选择,对应的分别有加,减,乘,除,求模,自增,自减,与,或,异或,非十一种运算,而由选通键控制的其它符号表示清零。对应的真值表如下:
3对应于开发板的I/O接口设计: ○
m
输出d[7..0]-->LED7~LED0; 选通S[3..0]—>按键4-1; 输入a[3..0]—> 拨码开关4-1; 输入b[3..0]--> 拨码开关 8-5.
选通信号 运算 s3s2s1s0 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011~1111
2.系统设计与实现:
1原理图: ○
算术运算 A+B A-B A*B A/B A%B A++ A-- 逻辑运算 AB A||B +B A○A 清零 输出为0
2VHDL语言描述: ○
library ieee;
use ieee.std_logic_1164.all; use ieee.std_logic_arith.all; use ieee.std_logic_unsigned.all;
entity alu4 is ------实体:电路外观配置 port
( a:in std_logic_vector(3 downto 0);
b:in std_logic_vector(3 downto 0);
s:in std_logic_vector(3 downto 0); bcdout:out std_logic_vector(7 downto 0) ); end alu4;
architecture arch of alu4 is ------结构体:电路功能描述 signal c:std_logic_vector(3 downto 0); signal q:std_logic_vector(3 downto 0); begin
process(s,a,b,c0,c1,c2,c3)
variable tmpa,tmpb:std_logic_vector(7 downto 0);--中间变量定义 variable tmpc:std_logic_vector(3 downto 0);
signal c0,c1,c2,c3:std_logic_vector(3 downto 0); signal cin: std_logic;
begin case s is
when \"0000\"=>
------加法:A+B ;
cin<='0';
q(0)<=a(0)xor b(0)xor cin;
c(0)<=(a(0) and b(0))or (b(0)and cin)or (a(0)and cin); gen1:for i in 1 to 3 loop q(i)<=a(i)xor b(i)xor c(i-1);
c(i)<=( a(i) and b(i))or (b(i)and c(i-1))or (a(i)and c(i-1)); end loop;
bcdout<=\"000\"&c(3)&q(3)&q(2)&q(1)&q(0);
when \"0001\"=> ------减法:A-B ;
cin<='0';
q(0)<=a(0)xor b(0)xor cin;
c(0)<=(not a(0) and b(0))or (b(0)and cin)or (not a(0)and cin); gen2:for i in 1 to 3 loop q(i)<=a(i)xor b(i)xor c(i-1);
c(i)<=(not a(i) and b(i))or (b(i)and c(i-1))or (not a(i)and c(i-1)); end loop;
bcdout<=\"000\"&c(3)&q(3)&q(2)&q(1)&q(0); when \"0010\"=> -------乘法:A*B
if (b(0)='0')then c0<=\"0000\"; else c0<=a;
end if;
if (b(1)='0')then c1<=\"0000\"; else c1<=a;
end if;
if (b(2)='0')then c2<=\"0000\"; else c2<=a;
end if;
if (b(3)='0')then c3<=\"0000\"; else c3<=a;
end if;
when \"0011\"=> ----除法,A/B
bcdout<=(\"0000\"&c0)+(\"000\"&c1&'0')+(\"00\"&c2&\"00\")+('0'&c3&\"000\"); tmpa:=\"00000000\";
tmpb:=\"00000000\";
tmpc:=\"0000\"; ---中间变量初始化 if(b>\"0000\")then ---B不为零(除数不为零) if(a>\"0000\")then ----A不为零(被除数不为零) if(a>b)then ----被除数大于除数时
tmpa(3 downto 0):=a; -----以下四条If语句完成求满足以上 tmpb(6 downto 3):=b; ---三个条件时被除数除以除数的商和余数 if(tmpa>=tmpb)then tmpa:=tmpa-tmpb; tmpc(3):='1'; else
tmpc(3):='0'; end if;
tmpb(5 downto 2):=b; tmpb(6):='0'; if(tmpa>=tmpb)then tmpa:=tmpa-tmpb; tmpc(2):='1'; else
tmpc(2):='0'; end if;
tmpb(4 downto 1):=b; tmpb(5):='0'; if(tmpa>=tmpb)then tmpa:=tmpa-tmpb; tmpc(1):='1'; else
tmpc(1):='0'; end if;
tmpb(3 downto 0):=b; tmpb(4):='0'; if(tmpa>=tmpb)then tmpa:=tmpa-tmpb; tmpc(0):='1'; else
tmpc(0):='0';
end if;
bcdout<=\"0000\"&tmpc;
elsif(a=b)then ---------被除数等于除数 bcdout<=\"00000001\"; else
bcdout<=\"00000000\"; ---------被除数小于除数 end if;
else ----被除数为零时 bcdout<=\"00000000\";
end if;
else ---------除数为零时 bcdout<=\"ZZZZZZZZ\"; end if;
when \"0100\"=> ------求模 A%B
tmpa:=\"00000000\"; tmpb:=\"00000000\";
tmpc:=\"0000\"; ---中间变量初始化 if(b>\"0000\")then ---B不为零(除数不为零) if(a>\"0000\")then ----A不为零(被除数不为零) if(a>b)then ----被除数大于除数时
tmpa(3 downto 0):=a; -----以下四条If语句完成求满足以上 tmpb(6 downto 3):=b; ---三个条件时被除数除以除数的商和余数 if(tmpa>=tmpb)then tmpa:=tmpa-tmpb; tmpc(3):='1'; else
tmpc(3):='0'; end if;
tmpb(5 downto 2):=b; tmpb(6):='0'; if(tmpa>=tmpb)then tmpa:=tmpa-tmpb; tmpc(2):='1'; else
tmpc(2):='0'; end if;
tmpb(4 downto 1):=b; tmpb(5):='0'; if(tmpa>=tmpb)then tmpa:=tmpa-tmpb; tmpc(1):='1'; else
tmpc(1):='0'; end if;
tmpb(3 downto 0):=b; tmpb(4):='0'; if(tmpa>=tmpb)then tmpa:=tmpa-tmpb; tmpc(0):='1'; else
tmpc(0):='0';
end if; bcdout<=\"0000\"&tmpa(3 downto 0);
elsif(a=b)then ---------被除数等于除数
bcdout<=\"00000000\"; else
---------被除数小于除数 bcdout<=\"0000\"&a; end if;
else ----被除数为零时
bcdout<=\"00000000\"; end if;
else ---------除数为零时
bcdout<=\"ZZZZZZZZ\"; end if;
when \"0101\"=>
bcdout<=\"0000\"&(a+1); --执行a+1
when \"0110\"=>
bcdout<=\"0000\"&(a-1); --执行a-1
when \"0111\"=> --逻辑与:AB q(3)<=a(3)and b(3);
when \"1001\"=> --异或:A⊕B
q(2)<=a(2)and b(2); q(1)<=a(1)and b(1); q(0)<=a(0)and b(0);
bcdout<=\"0000\"&q(3)&q(2)&q(1)&q(0);
when \"1000\"=> --逻辑或:A+B
q(2)<=a(2)or b(2); q(1)<=a(1)or b(1); q(0)<=a(0)or b(0);
bcdout<=\"0000\"&q(3)&q(2)&q(1)&q(0);
q(3)<=a(3)or b(3);
q(3)<=a(3)xor b(3);
q(2)<=a(2)xor b(2); q(1)<=a(1)xor b(1); q(0)<=a(0)xor b(0);
bcdout<=\"0000\"&q(3)&q(2)&q(1)&q(0);
when \"1010\"=> --逻辑非:
bcdout<=\"0000\"¬ a(3)& not a(2)& not a(1)& not a(0); when others=>
bcdout<=\"00000000\"; end case;
end arch;
end process;
3行为仿真如下: ○
输入信号
仿真结果:
3.问题和技术所遇到的问题的详细描述:
1对原理图生成下载文件时,遇到如下错误: ○
ERROR:Place:1018 - A clock IOB / clock component pair have been found that are not placed at an optimal clock IOB /
clock site pair. The clock component is placed at site sub optimal condition is acceptable for this design, you may use the in CLOCK_DEDICATED_ROUTE constraint the .ucf file to demote this message to a WARNING and allow your design to continue. However, the use of this override is highly discouraged as it may lead to very poor timing results. It is recommended that this error condition be corrected in the design. A list of all the COMP.PINs used in this clock placement rule is listed below. These examples can be used directly in the .ucf file to override this clock rule. 解决办法:通过在网上查找,找到如下解决办法 在ucf文件中加NET \"s<1>\" CLOCK_DEDICATED_ROUTE = FALSE;即可! 2 原理图综合时,遇到如下错误: ○ Process will terminate. For technical support on this issue, please open a WebCase with this project attached at http://www.xilinx.com/support. 解决办法:将重新建立一个工程,然后再综合。 4.结论 经过对ise的熟悉,知道建立工程,编辑VHDL文件,原理图的建立,以及对其综合,生成下载文件,和之后的建立仿真文件进行仿真,每一步都需要实践,改进,最终得到了想要的结果,满足了设计所需要求! 5.改进 其实最先的时候,是想用动态数码扫描来显示所得结果,发现其所需引入时钟及各个控制模块,是一个时序项目。如果能力足够,可考虑用动态扫描数码管来显示计算结果。 6.心得体会 之前认为这样的一个组合逻辑alu的设计比较简单,只是对各个加法器,乘法器等进行选通就行了,但实际操作过程中却发现颇有难度,主要体现在乘法器和除法器的选择和实现上。这其中破费了一些功夫。 因篇幅问题不能全部显示,请点此查看更多更全内容> is placed at site