实验三. 四位有符号数除法
一. 实验目的
使用硬件描述语言(VHDL)对除法器进行描述,并进行模拟和验证,熟悉fpga开发基本
流程,加深对二进制数运算方法的理解。
二.实验内容
设计四位定点有符号整数除法器(op=ai÷bi),软件仿真通过后下载到FPGA板子进行验证:
1、 使用clock为输入时钟信号,其频率为50MHz
2、 使用拨码开关sw7~sw4为被除数ai,其中sw7为MSB(高位),sw4为LSB(低位) 3、 使用拨码开关sw3~sw0为除数bi,其中sw3为MSB,sw0为LSB
4、 使用按钮btn<0>作为输入确定信号,在每次改变输入时按下按钮得到输出结果 5、 以LED7~4为所得商op,LED3为MSB,灯亮代表该位为1. 6、 以LED3~0为所得余数,LED7为MSB
7、 若除数为0,则led7闪烁(闪烁频率自定义,以肉眼能分辨为准),led6~0熄灭
三.设计思路
对于有符号位数,在被除数与除数均为正数的情况下,使用的是如下的方法:被除数为A:A3,A2,A1,A0,除数为B:B3,B2,B1,B0。其中A3,B3为符号位,算法思想如下:
1、将二个操作数A,B都扩展成7位即:( A3和B3是符号位,不参与具体运算) A:0 0 0 A2 A1 A0 B:0 0 0 B2 B1 B0
2、将B左移三位得Q: B2 B1 B0 0 0 0 比较A与Q,若A>=Q,商上1,A=A-Q; 否则,商上0,A=A;
B右移一位;
如此循环四次,便得到所要的结果
但是被除数与除数中有一个是负数的情况下,需要分类讨论,具体如下:
(1)被除数为负,除数为正。这种情况需将被除数取其补码,然后按上述方法操作,在得到相应的余数以及商的结果后,需将余数以及商都转为其补码形式再输出。
(2)被除数为正,除数为负。这种情况下需将除数取其补码,然后按上述方法操作,在得到相应的余数以及商的结果后,需将商转为其补码形式输出,余数按正常结果输出。
(3)被除数除数均为负。这种情况需将其两均转化为补码形式,然后按照上述方法操作,在得到相应的余数以及商的结果后,需将余数转为补码输出,商按正常结果输出。
四.模块划分
整个主程序划分为四个模块,分别为防按键抖动模块(bounce_eliminating)、除法模块(division)、LED灯控制模块(lout)以及分频模块(clk_divider)。
其中分频(clk_divider)模块将50MHz的频率分为200Hz用于控制刷新拨码开关的状态,2Hz用于控制当除数为零时LED灯的闪烁,分为2Hz是为了人眼的区分。
五.FPGA资源使用情况 Device Utilization Summary Logic Utilization Total Number Slice Registers Number used as Flip Flops Number used as Latches Number of 4 input LUTs Number of occupied Slices Number of Slices containing only related logic Number of Slices containing unrelated logic Total Number of 4 input LUTs Number used as logic Number used as a route-thru Number of bonded IOBs IOB Flip Flops [-]Used Available Utilization Note(s) 58 49 9 127 93 93 0 168 127 41 18 7 1,920 1,920 960 93 93 1,920 83 3% 6% 9% 100% 0% 8% 21% Number of BUFGMUXs Average Fanout of Non-Clock Nets 1 3.05 24 4% 整个的资源利用率较高,对于寄存器的使用情况,这组实验占用了58个,其中使用为
触发器的为49个,使用为锁存器的为9个。主要是由于在与本次实验操作中,有些信号的赋值是以另外信号的跳变为条件的(即同步赋值),这些信号经过编译就会生成寄存器。整体说来,本次实验产生的寄存器的数量不是很多,情况很理想。
六.实验心得
这个实验是我第一次用FPGA板子,也是第一次使用xilinx编程,起先自己什么都不懂,
在看了助教老师给出的xilinx指导ppt以及自己的摸索后,才将上课所学的知识运用到了本次实验中。刚开始的实验,由于考虑的情况太少,导致了本次实验的除法只能在两个正数之间,在同学的提醒下我才意识到这个问题。后来在division函数中增加了另外三种情况的判断,运用了大二所学习的数字电路知识,最后 实现了四种情况下的除法操作。总的来说,这次实验收获特别大,这为后面的拓展实验部分打好了良好的基础。最后还得谢谢助教老师们的不辞辛劳,你们为这次实验付出了太多,从你们身上我们学到了许多知识,谢谢你们!
七.附录源代码
主程序main:
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
-- Uncomment the following library declaration if using -- arithmetic functions with Signed or Unsigned values --use IEEE.NUMERIC_STD.ALL;
-- Uncomment the following library declaration if instantiating -- any Xilinx primitives in this code. --library UNISIM;
--use UNISIM.VComponents.all;
entity main is
Port ( clk : in STD_LOGIC; btn0 : in STD_LOGIC;
dividend : in STD_LOGIC_VECTOR (3 downto 0); divisor : in STD_LOGIC_VECTOR (3 downto 0); led : out STD_LOGIC_VECTOR (7 downto 0)); end main;
architecture Behavioral of main is component bounce_eliminating is Port ( btn0 : in STD_LOGIC; clk : in STD_LOGIC;
sout : out STD_LOGIC); end component; component clk_divider is Port ( clk : in STD_LOGIC; clk1 : buffer STD_LOGIC; clk2 : buffer STD_LOGIC); end component; component division is Port ( btn0 : in STD_LOGIC;
a : in STD_LOGIC_VECTOR (3 downto 0); b : in STD_LOGIC_VECTOR (3 downto 0); state : out STD_LOGIC;
led : out STD_LOGIC_VECTOR (7 downto 0)); end component; component lout is Port ( ledin : in STD_LOGIC_VECTOR (7 downto 0); state : in STD_LOGIC; clk : in STD_LOGIC;
ledout : buffer STD_LOGIC_VECTOR (7 downto 0)); end component; signal clk1, clk2, state, sout:std_logic; signal leds:std_logic_vector(7 downto 0);
begin U1:bounce_eliminating port map(btn0,clk1,sout); U2:clk_divider port map(clk,clk1,clk2); U3:division port map(sout,dividend,divisor,state,leds); U4:lout port map(leds,state,clk2,led);
end Behavioral;
防按键抖动模块bounce_eliminating:
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
-- Uncomment the following library declaration if using -- arithmetic functions with Signed or Unsigned values --use IEEE.NUMERIC_STD.ALL;
-- Uncomment the following library declaration if instantiating -- any Xilinx primitives in this code. --library UNISIM;
--use UNISIM.VComponents.all;
entity bounce_eliminating is
Port ( btn0 : in STD_LOGIC; clk : in STD_LOGIC; sout : out STD_LOGIC); end bounce_eliminating;
architecture Behavioral of bounce_eliminating is --防抖动,设置三个量判断 signal d1, d2, d3 : std_logic; begin process(clk) begin
if (clk'event and clk = '1') then d1 <= btn0; d2 <= d1; d3 <= d2; end if; end process;
sout <= d3 and d2 and (not d1);
end Behavioral;
分频模块clk_divider:
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
-- Uncomment the following library declaration if using -- arithmetic functions with Signed or Unsigned values --use IEEE.NUMERIC_STD.ALL;
-- Uncomment the following library declaration if instantiating -- any Xilinx primitives in this code. --library UNISIM;
--use UNISIM.VComponents.all;
entity clk_divider is
Port ( clk : in STD_LOGIC; clk1 : buffer STD_LOGIC;
百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库交大电子系VHDL四位有符号数除法实验报告在线全文阅读。
相关推荐: