收藏 分销(赏)

vhdl计算器模块设计.doc

上传人:w****g 文档编号:2628712 上传时间:2024-06-03 格式:DOC 页数:21 大小:144.69KB 下载积分:10 金币
下载 相关 举报
vhdl计算器模块设计.doc_第1页
第1页 / 共21页
vhdl计算器模块设计.doc_第2页
第2页 / 共21页


点击查看更多>>
资源描述
计算器模块设计 姓名:王文杰 班级:1421202 学号: 201420120201 专业:测控技术与仪器 计算器模块设计 摘要 计算器一般由运算器、控制器、存储器、键盘、显示器、电源和一些可选外围设备组成。低档计算器的运算器、控制器由数字逻辑电路实现简单的串行运算,其随机存储器只有一、二个单元,供累加存储用。使用简单计算器可进行加(+)、减(-)、乘(*)、除(/)、开方(sqrt)、百分数(%)、倒数(1/x)等简单算术计算。本次设计只完成加(+)、减(-)、乘(*)、除(/)运算。 实验设计 一 设计内容与设计要求 1设计内容:设计一个简易计算器 2设计要求:实现最大输入两位十进制数字的四则运算(加减乘除) 能够实现多次连算(无优先级,从左到右计算结果) 最大长度以数码管最大个数为限,溢出报警 有正负数区分; 除法不能除尽时小数保留2位有效数字 能够区分运算优先级(先乘除,后加减) 二 系统具体设计、 1总体框架 数据(包括正负) 除 乘 若出现加减符号或第一次计算,结果反馈的都是输入的数据 结果反馈输出 选择乘或除的结果输出 乘法运算 包括一个乘法器 乘法运算 包括一个乘法器 结果输出 或数据选择输出 进行加减法运算 根据加减法选择数据正负,a-b即a+(-b) 注:各主要模块都加reset复位 进制转换输出 2各个模块设计 ①数据输入模块(VHDL附录9) 受限于输入按键有限,输入的两位二进制数有计数器实现,每按下clk加1,到99后恢复零 元件例化: VHDL编程: library ieee; use ieee.std_logic_1164.all; use ieee.std_logic_arith.all; use ieee.std_logic_unsigned.all; entity leijia is port( reset:in std_logic; clk: in std_logic; dataout: out std_logic_vector(7 downto 0) ); end entity leijia; architecture behave of leijia is signal temp:std_logic_vector(7 downto 0); begin dataout<=temp; process(clk,reset) begin if(reset='0')then if(clk'event and clk='1')then if(temp="01100100")then temp<="00000000"; else temp<=temp+1; end if; end if; else temp<="00000000"; end if; end process; end behave; ②加减选择模块 减法是加法的逆运算,A-B等同于A+(-B),即减法运算只需在进行加法运算前把数据的正负取反,所以当减法的下降沿时输出的正负为输入的取反,加法的下降沿时输出的正负为输入的,实现方法与乘除选择模块一致。 元件例化: VHDL编程: library ieee; use ieee.std_logic_1164.all; use ieee.std_logic_arith.all; use ieee.std_logic_unsigned.all; entity tt2 is port( reset:in std_logic; pn:in std_logic; clk1: in std_logic; clk2: in std_logic; pnout:out std_logic; dout:out std_logic ); end entity tt2; architecture behave of tt2 is signal temp:std_logic; begin dout<=temp; process(clk1,clk2) begin if(reset='0')then if(clk1='1')then temp<='0'; elsif(clk2'event and clk2='0')then temp<='1'; end if; if(temp='1')then pnout<=not(pn); else pnout<=pn; end if; else temp<='0'; end if; end process; end behave; ③加减运算模块 由于上一级已考虑加减情况,此模块只需实现加法运算。A+B有四种情况: 1、 A>0,B>0 输出数据A+B,正负号为正 2、A>0,B<0 |A|>|B| 输出数据|A|-|B| 正负号为正 |A|<|B| 输出数据|B|-|A| 正负号为负 3、A<0,B>0 |A|>|B| 输出数据|A|-|B| 正负号为负 |A|<|B| 输出数据|B|-|A| 正负号为正 4、 A<0,B<0 输出数据A+B,正负号为负 其中clk为加or减,上升沿时读新数据并计算。 元件例化: VHDL编程: library ieee; use ieee.std_logic_1164.all; use ieee.std_logic_arith.all; use ieee.std_logic_unsigned.all; entity add is port(clk :in std_logic; reset:in std_logic; data:in std_logic_vector(27 downto 0); data_pn:in std_logic; dout :out std_logic_vector(27 downto 0); dout_pn:out std_logic ); end entity add; architecture behave of add is signal temp: std_logic_vector(27 downto 0):="0000000000000000000000000000"; signal temp_pn:std_logic:='0'; begin dout<=temp; dout_pn<=temp_pn; process(clk,reset) begin if(reset='1')then temp<="0000000000000000000000000000";temp_pn<='0'; elsif(clk'event and clk='1')then if(temp_pn='0')then if(data_pn='0')then temp<=temp+data;temp_pn<='0'; else if(temp>data or temp=data)then temp<=temp-data;temp_pn<='0'; else temp<=data-temp;temp_pn<='1'; end if; end if; else if(data_pn='1')then temp<=temp+data;temp_pn<='1'; else if(temp<data or temp=data)then temp<=data-temp;temp_pn<='0'; else temp<=temp-data;temp_pn<='1'; end if; end if; end if; end if; end process; end behave; ④乘法模块 当乘法符号的上升沿的时候读入反馈的结果(包括数据及正负号)并输出到data1、data1_pn; 当乘法符号的下降沿的时候读入数据(包括数据及正负号)并输出到data2、data2_pn。并进行计算,结果的数据由乘法器计算得到;结果的正负号即为两个相乘数据的正负号取异或。 数据为保留两位小数,输出的当成被乘数的数乘上100。 元件例化: VHDL编程: library ieee; use ieee.std_logic_1164.all; use ieee.std_logic_arith.all; use ieee.std_logic_unsigned.all; entity mul is port( clk : in std_logic; reset: in std_logic; data : in std_logic_vector(7 downto 0); data_pn : in std_logic; res : in std_logic_vector(27 downto 0); res_pn: in std_logic; data1: out std_logic_vector(27 downto 0); data2: out std_logic_vector(7 downto 0); data1_pn:out std_logic; data2_pn:out std_logic ); end entity mul; architecture behave of mul is signal temp1:std_logic; signal temp2:std_logic; begin process(reset,clk) begin if(reset='0')then if(clk'event and clk='1')then data1<=res; data1_pn<=res_pn; end if; if(clk'event and clk='0')then data2<=data; data2_pn<=data_pn; end if; else data1<="0000000000000000000000000000";data2<="00000000"; end if; end process; end behave; ⑤除法模块 当除法符号的上升沿的时候读入反馈的结果(包括数据及正负号)并输出到data1、data1_pn; 当除法符号的下降沿的时候读入数据(包括数据及正负号)并输出到data2、data2_pn。并进行计算,结果的数据由除法器计算得到;结果的正负号即为两个相除数据的正负号取异或。 数据为保留两位小数,输出的当成被除数的数乘上100。 元件例化: VHDL编程: library ieee; use ieee.std_logic_1164.all; use ieee.std_logic_arith.all; use ieee.std_logic_unsigned.all; entity div is port( clk : in std_logic; reset: in std_logic; data : in std_logic_vector(7 downto 0); data_pn : in std_logic; res : in std_logic_vector(27 downto 0); res_pn: in std_logic; data1: out std_logic_vector(27 downto 0); data2: out std_logic_vector(7 downto 0); data1_pn:out std_logic; data2_pn:out std_logic ); end entity div; architecture behave of div is signal temp1:std_logic; signal temp2:std_logic; begin process(reset,clk) begin if(reset='0')then if(clk'event and clk='1')then data1<=res; data1_pn<=res_pn; end if; if(clk'event and clk='0')then data2<=data; data2_pn<=data_pn; end if; else data1<="0000000000000000000000000000";data2<="00000000"; end if; end process; end behave; ⑥乘或除数据选择输出模块 乘或除的数据及正负在各自符号的下降沿时输出,在输出时确定输出的是乘的结果还是除的结果。即预期在乘符号的下降沿时输出乘的结果;在除符号的下降沿时输出除的结果。 又由于两个时钟信号不能控制同一个变量,所以把clk1当成是清零信号,clk2当成时钟信号,当clk2的下降沿的时候输出数据2(即除的结果),clk2为1时,输出数据1(即除的结果)。又为了防止clk2为1与其后面要用到的上升沿冲突,故添加dout。 元件例化: VHDL编程: library ieee; use ieee.std_logic_1164.all; use ieee.std_logic_arith.all; use ieee.std_logic_unsigned.all; entity tt is port( reset:in std_logic; pn1:in std_logic; pn2:in std_logic; res1:in std_logic_vector(27 downto 0 ); res2:in std_logic_vector(27 downto 0 ); clk1: in std_logic; clk2: in std_logic; resout: out std_logic_vector(27 downto 0 ); pnout:out std_logic; dout:out std_logic ); end entity tt; architecture behave of tt is signal temp:std_logic; begin dout<=temp; process(clk1,clk2) begin if(reset='0')then if(clk1='1')then temp<='0'; elsif(clk2'event and clk2='0')then temp<='1'; end if; if(temp='1')then resout<=res2;pnout<=pn2; else resout<=res1;pnout<=pn1; end if; else temp<='0'; end if; end process; end behave; ⑦结果反馈及输出模块 当第一次进行运算或出现过加减再进行乘除的第一次运算时读取输入的数据值,否则读取前一级的结果值。 其中clr为恢复信号(加or 减),当其为1时,恢复到第一次运算的模式。clk本为乘或除,又为了避开与上一级的清零错位,故其变为(乘or(除and not dout)) 元件例化: VHDL编程: library ieee; use ieee.std_logic_1164.all; use ieee.std_logic_arith.all; use ieee.std_logic_unsigned.all; entity res_cs is port( reset:in std_logic; data: in std_logic_vector(7 downto 0); data_pn: in std_logic; clr: in std_logic; clk: in std_logic; res: in std_logic_vector(27 downto 0); res_pn:in std_logic; result: out std_logic_vector(27 downto 0); result_pn:out std_logic ); end entity res_cs; architecture behave of res_cs is signal cn:std_logic_vector(3 downto 0); begin process(clk,clr) begin if(reset='0')then if(clr='0')then if(cn="0000")then result<="0000000000000"&(data*"1100100");result_pn<=data_pn; else result<=res;result_pn<=res_pn; end if; if(clk'event and clk='1')then cn<=cn+1; end if; else cn<="0000"; end if; else cn<="0000"; end if; end process; end behave; ⑧数据输出选择模块(VHDL附录8) 当按下等号时输出计算结果,否则输出输入的数据。 当输出为负时:第一个数码管显示F;当输出为负时:第一个数码管显示E;当数据超出范围溢出是第一个数码管显示C。 元件例化: VHDL编程: library ieee; use ieee.std_logic_1164.all; use ieee.std_logic_arith.all; use ieee.std_logic_unsigned.all; entity add is port(clk :in std_logic; reset:in std_logic; data:in std_logic_vector(27 downto 0); data_pn:in std_logic; dout :out std_logic_vector(27 downto 0); dout_pn:out std_logic ); end entity add; architecture behave of add is signal temp: std_logic_vector(27 downto 0):="0000000000000000000000000000"; signal temp_pn:std_logic:='0'; begin dout<=temp; dout_pn<=temp_pn; process(clk,reset) begin if(reset='1')then temp<="0000000000000000000000000000";temp_pn<='0'; elsif(clk'event and clk='1')then if(temp_pn='0')then if(data_pn='0')then temp<=temp+data;temp_pn<='0'; else if(temp>data or temp=data)then temp<=temp-data;temp_pn<='0'; else temp<=data-temp;temp_pn<='1'; end if; end if; else if(data_pn='1')then temp<=temp+data;temp_pn<='1'; else if(temp<data or temp=data)then temp<=data-temp;temp_pn<='0'; else temp<=temp-data;temp_pn<='1'; end if; end if; end if; end if; end process; end behave; ⑨进制转换模块 28位二进制数(根据7个数码管为限,最大输出为十进制的9999999,后两位为小数位)以十进制的方式显示到数码管上,采用各位求余分别显示的方法。 其中除数分别为十进制的1000000、100000、10000、1000、100、10,需要输出的是每次相除后的商及最后一个余数。 元件例化: 其实现电路: 系统仿真 仿真的式子为12+34×56-78÷90+9 结果数据为192414,代表1924.14 正负为E,代表正 理论结果为1924.13,仿真结果符合。 仿真的式子为(-12)+34×(-56)-78÷90+9 结果数据为190786,代表1907.86 正负为F, 代表负 理论结果为-1907.87,仿真结果符合。 仿真的式子为90*90*90 结果数据为0,代表0 正负为C,代表溢出 理论结果为27000.00,为溢出,仿真结果符合。 目 录 第一章 总 论 1 1.1项目概况 1 1.2研究依据及范围 2 1.3结论 3 1.4建议 4 第二章 项目建设的背景和必要性 5 2.1项目建设的背景 6 2.2项目建设的必要性 7 第三章 项目服务需求分析 9 第四章 项目选址与建设条件 11 4.1选址原则 11 4.2项目选址 11 4.3建设条件 12 第五章 建设方案与设计 12 5.1建设规模与内容 12 5.2总体规划设计 13 5.3结构方案 18 5.4主要配套设备 19 5.5给水工程 20 5.6排水工程 22 5.7电气设计 23 5.8节能设计 26 第六章 项目实施进度和招投标管理 29 6.1 项目实施进度 29 6.2招投标管理 31 第七章 环境影响分析 31 7.1项目主要污染源分析 32 7.2 环境保护措施及治理效果 35 第八章 消防、安全与卫生防护 37 8.1 消防 37 8.2  劳动安全 38 8.3  卫生防护 39 第九章 组织机构、运作方式与项目实施进度 39 9.1  项目建设组织机构 39 9.2项目运营组织机构 41 9.3劳动定员 42 第十章 投资估算和资金筹措 42 10.1投资估算 43 10.2 项目所需流动资金 49 10.3资金筹措 49 第十一章  经济和社会效益评价 50 11.1 经济效益评价 50 11.2 社会效益评价 50 第十二章 结 论 50 12.1 主要结论 50 12.2 建 议 51 附录:1、****发改局《关于*******迁建工程项目建议书的批复》 2、****村镇建设管理所《*******迁建项目用地红线》
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 包罗万象 > 大杂烩

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服