基于VHDL的通用计算器源程序.docx
《基于VHDL的通用计算器源程序.docx》由会员分享,可在线阅读,更多相关《基于VHDL的通用计算器源程序.docx(24页珍藏版)》请在冰豆网上搜索。
基于VHDL的通用计算器源程序
源程序
4位二进制并行进位加法器的源程序ADDER4B.VHD如下
LIBRARYIEEE;
USEIEEE.STD_LOGIC_1164.ALL;
USEIEEE.STD_LOGIC_UNSIGNED.ALL;
ENTITYADDER4BIS--四位二进制并行加法器
PORT(ci:
INSTD_LOGIC;--低位进位
a:
INSTD_LOGIC_VECTOR3DOWNTO0);--4位加数
b:
INSTD_LOGIC_VECTOR(3DOWNTO0);--4位被加数
s:
OUTSTD_LOGIC_VECTOR(3DOWNTO0);--4位和
co:
OUTSTD_LOGIC--进位输出
);
ENDADDER4B;
ARCHITECTUREbehaveOFADDER4BIS
SIGNALSINT:
STD_LOGIC_VECTOR(4DOWNTO0);--部定义的一个数据
SIGNALaa,bb:
STD_LOGIC_VECTOR(4DOWNTO0);
BEGIN
aa<=’0’&a;--将4位加数矢量扩为5位,为进位提供空间
bb<=’0’&b;--将4位被加数矢量扩为5位,为进位提供空间
INT<=aa+bb+ci;--相加
s<=SINT(3DOWNTO0);
co<=SINT(4);--最高位为输出进位
ENDbehave;
顶层模块:
8位二进制并行进位加法器的部分程序ADDER8B.VHD如下:
LIBRARYIEEE;
USEIEEE.STD_LOGIC_1164.ALL;
USEIEEE.STD_LOGIC_UNSIGNED.ALL;
ENTITYADDER8BIS
PORT(ci:
INSTD_LOGIC;
a:
INSTD_LOGIC_VECTOR(7DOWNTO0);
b:
INSTD_LOGIC_VECTOR(7DOWNTO0);
s:
OUTSTD_LOGIC_VECTOR(7DOWNTO0);
co:
OUTSTD_LOGIC
);
ENDADDER8B;
ARCHITECTUREaOFADDER8BIS
Componentadder4B--引用4位二进制并行进位加法器
PORT(ci:
INSTD_LOGIC;
a:
INSTD_LOGIC_VECTOR3DOWNTO0);
b:
INSTD_LOGIC_VECTOR(3DOWNTO0);
s:
OUTSTD_LOGIC_VECTOR(3DOWNTO0);
co:
OUTSTD_LOGIC
);
ENDCOMPONENT;
SIGNALCARRY_OUT:
STD_LOGIC;--4位加法器的进位标志
BEGIN
U1:
ADDER4B--安装一个4位二进制加法器U1
PORTMAP(ci=>ci,a=>a(3DOWNTO0),b=>b(3DWONTO0),s=>(3DOWNTO0),co=>CARRY_OUT);
U2:
ADDER4B--安装一个4位二进制加法器U2
PORTMAP(ci=>CARRY_OUT,a=>a(7DOWNTO4),b=>b(7DWONTO4),s=>(7DOWNTO4),co=>co);
ENDbehave;
加法器VHDL程序如下
LIBRARYIEEE;
USEIEEE.STD_LOGIC_1164.ALL;
USEIEEE.STD_LOGIC_ARITH.ALL;
USEIEEE.STD_LOGIC_UNSIGNED.ALL;
ENTITYadderIS
port(a:
instd_logic;--被加数a
b:
instd_logic;--加数b
ci:
instd_logic;--输入进位
s:
outstd_logic;--结果输出
co:
outstd_logic--输出进位
);
endadder;
architecturebehaveofadderis
signaltem:
std_logic;--暂存
signalstem:
std_logic;
begin
tem<=axorb;--中间变量
stem<=temxorci;--结果
co<=(temandci)or(aandb);--进位输出
s<=stem;--输出
endbehave;
4位二进制并行进位减法器的源程序suber.VHD如下
:
LIBRARYIEEE;
USEIEEE.STD_LOGIC_1164.ALL;
USEIEEE.STD_LOGIC_ARITH.ALL;
USEIEEE.STD_LOGIC_UNSIGNED.ALL;
ENTITYsub4IS
PORT(a:
INSTD_LOGIC_VECTOR(3DOWNTO0);--4位被减数
b:
INSTD_LOGIC_VECTOR(3DOWNTO0);--4位减数
ci:
INSTD_LOGIC;--输入进位
s:
OUTSTD_LOGIC_VECTOR(3DOWNTO0);--结果输出
co:
OUTSTD_LOGIC--输出进位
);
endsuber;
architecturebehaveofsuberis
componentadderis--引用加法器的模块
port(a:
instd_logic;
b:
instd_logic;
ci:
instd_logic;
s:
outstd_logic;
co:
outstd_logic
);
endcomponent;
signalbtem:
std_logic_vector(3downto0);--减数寄存
signalctem:
std_logic_vector(4downto0);--进位寄存
signalstem:
std_logic_vector(3downto0);--结果寄存
begin
btem(3downto0)<=notb(3downto0);--先把减数求反
ctem(0)<=notci;--输入的进位也求反,从而对减数求补码
g1:
forIin0to3generate--连用4位全加器
add:
adderportmap(a(i),btem(i),ctem(i),stem(i),ctem(i+1));
endgenerate;
s(3downto0)<=stem(3downto0);--结果输出
co<=notctem(4);--求反输出进位
endbehave;
乘法器的源程序:
LIBRARYIEEE;
USEIEEE.STD_LOGIC_1164.ALL;
USEIEEE.STD_LOGIC_ARITH.ALL;
USEIEEE.STD_LOGIC_UNSIGNED.ALL;
Entitymulis
Port(
a:
instd_logic_vector(3downto0);--4位被乘数
b:
instd_logic_vector(3downto0);--4位乘数
y:
outstd_logic_vector(7downto0)--乘积
);
endmul;
architecturearchofmulis
begin
y(7downto0)<=a(3downto0)*b(3downto0);
endarch;
除法器的源程序:
LIBRARYIEEE;
USEIEEE.STD_LOGIC_1164.ALL;
USEIEEE.STD_LOGIC_ARITH.ALL;
USEIEEE.STD_LOGIC_UNSIGNED.ALL;
entitydiveris
PORT(a:
INSTD_LOGIC_VECTOR(7DOWNTO0);--8位被除数输入
b:
INSTD_LOGIC_VECTOR(3DOWNTO0);--4位除数输入
clk:
INSTD_LOGIC;--时钟
str:
INSTD_LOGIC;--启动信号
s:
OUTSTD_LOGIC_VECTOR(3DOWNTO0);--4位商输出
y:
OUTSTD_LOGIC_VECTOR(3DOWNTO0)--4位余数输出
);
End;
Architecturebehaveofdiveris
Componentsuberis--引用减法器
PORT(a:
INSTD_LOGIC_VECTOR(3DOWNTO0);
b:
INSTD_LOGIC_VECTOR(3DOWNTO0);
ci:
INSTD_LOGIC;
s:
OUTSTD_LOGIC_VECTOR(3DOWNTO0);
co:
OUTSTD_LOGIC
);
Endcomponent;
typestate_typeis(start,one,two,three,eror);--状态定义
signalstate:
state_type;--定义状态变量
signalain:
std_logic_vector(7downto0);--被除数寄存
signalbin:
std_logic_vector(3downto0);--除数寄存
signalatem:
std_logic_vector(3downto0);--减法器被减数输入
signalbtem:
std_logic_vector(3downto0);--减法器减数输入
signalstem:
std_logic_vector(3downto0);--结果寄存
signalcitem:
std_logic;--减法器借位输入
signalcotem:
std_logic;--减法器借位输出
begin
p2:
process(clk)
variablen:
integerrange0to3;--移位次数计数值
begin
ifclk’eventandclk=’1’then
casestateis
whenatart=>--开始状态
ifstr=’1’then--收到启动信号
state<=one;--转到状态one
atem(3downto0)<=a(7downto4);--把高4位放到减法器被减数端
btem(3downto0)<=b(3downto0);--把除数放到减法器减数端
ain(7downto0)<=a(7downto0);--寄存被除数
bin(3downto0)<=b(3downto0);--寄存除数
endif;
whenone=>--第一次移位
ifcotem=’0’then--被除数高4位小于除数,溢出!
state<=eror;--转到出错状态
else--不溢出
ain(3downto1)<=ain(2downto0);--被除数做移位
ain(0)<=notcotem;--在最低位接收该位商值
atem(3downto0)<=ain(6downto3);--把除数寄存器高4位输到减法器,作为减法器被减数
state<=two;--转到下一状态
endif;
whentwo=>--再做3此移位
ifn=2then--第四次移位
state<=three;--是,则跳转到下一状态
n:
=0;--移位计数器清零
else--否则
state<=two;--还回到这个状态
n:
=n+1;--移位计数器加1
endif;
ifcotem=’0’then--不够减,有借位
atem(3downto1)<=stem(2downto0);--减法器结果移位作为下一次的输入
else--够减,没有借位
atem(3downto1)<=atem(2downto0);--结果输出移位作为下一次的输入
endif;
ain(3downto1)<=ain(2downto0);--结果寄存器左移一位
ain(0)<=notcotem;--这次运算借位输出,输入到寄存器ain最后一位
atem(0)<=ain(3);--寄存器ain的最高位作为减法器输入被减数的低位
whenthree=>--正常运算结果输出
s(3downto1)<=ain(2downto0);--寄存器ain低3位作为输出结果高3位
s(0)<=notcotem;--最后一次减法运算的借位输出求反作为结果输出最低位
ifcotem=’0’then--最后一次减法运算,够减(无借位)
y(3downto0)<=atem(3downto0);--则减法器输出结果为整个除法的余数
else--否则,不够减
y(3downto0)<=atem(3downto0);--则最后一次减法运算的被减数为整个除法的余数
endif;
atem(3downto0)<="0";--寄存器清零
btem(3downto0)<="0";--寄存器清零
state<=start;--回到开始状态
wheneror=>--溢出状态
state<=start;--回到开始状态
atem(3downto0)<="0";--寄存器清零
btem(3downto0)<="0";--寄存器清零
endcase;
endif;
endprocessp2;
citem<=’0’;--4位减法器借位输入接地
U1:
suberportmap(atem,btem,citem,stem,cotem);
endbehave;
数字按键译码电路VHDL语言描述
LIBRARYIEEE;
USEIEEE.STD_LOGIC_1164.ALL;
USEIEEE.STD_LOGIC_ARITH.ALL;
USEIEEE.STD_LOGIC_UNSIGNED.ALL;
entitynumdecoderis
port(reset:
instd_logic;
inclk:
std_logic;
innum:
std_logic_vetctor(9downto0);
outnum:
bufferstd_logic_vector(3woento0);
outflag:
outstd_logic);
end;
architecturebehaveofnumdecoeris]
begin
ifreser=’1’then
outnum<=”0000”;
elsifinclk’eventandinclk=’1’then
caseinnumis
when”0000000001”=>outnum<=”0000”;outflag<=’1’;--按下第一个键表示输入0
when”0000000010”=>outnum<=”0001”;outflag<=’1’;--按下第二个键表示输入1
when”0000000100”=>outnum<=”0010”;outflag<=’1’;--按下第三个键表示输入2
when”0000001000”=>outnum<=”0011”;outflag<=’1’;--按下第四个键表示输入3
when”0000010000”=>outnum<=”0100”;outflag<=’1’;--按下第五个键表示输入4
when”0000100000”=>outnum<=”0101”;outflag<=’1’;--按下第六个键表示输入5
when”0001000000”=>outnum<=”0110”;outflag<=’1’;--按下第七个键表示输入6
when”0010000000”=>outnum<=”0111”;outflag<=’1’;--按下第八个键表示输入7
when”010*******”=>outnum<=”1000”;outflag<=’1’;--按下第九个键表示输入8
when”1000000000”=>outnum<=”1001”;outflag<=’1’;--按下第十个键表示输入9
whenothers=>outnum<=outnum;outflag<=’0’;--不按键时保持
endcase;
endif;
endprocess;
endbehave;
7段译码器的vhdl语言描述
LIBRARYIEEE;
USEIEEE.STD_LOGIC_1164.ALL;
USEIEEE.STD_LOGIC_ARITH.ALL;
USEIEEE.STD_LOGIC_UNSIGNED.ALL;
entityvdecodeis
port(indata:
instd_logic_vector(3downto0);
outdata:
outstd_logic_vector(0to6)
);
End;
Atchitecturebehaveofvdecodeis
Begin
Withindataselect
Outdata<=”1111110”when”0000”,
”0110000”when”0000”,
”1111001”when”0000”,
”0110011”when”0000”,
”1011011”when”0000”,
”1011111”when”0000”,
”1110000”when”0000”,
”1111111”when”0000”,
”1111110”when”0000”,
”1111110”when”0000”,
”1111011”when”0000”,
”0000000”whenothers;
Endbehave;
8位二进制数转换成个位、十位、百位的进程:
Ctrview:
process(c,clk)
Begin
Ifc=’1’then
view1<=”0000”;view2<=”0000”;view<=”0000”;
viewstep<=takenum;
elsifclk’eventandclk=’1’then
casevirestepis
whentakenum=>
ktemp<=keep;
viewstep<=hundred=>
ifktemp>=”11001000”then
view1<=”0010”;ktemp<=ktemp-“11001000;
elsifktemp>=”01100100”then
view1<=”0001”;ktemp<=ktemp-“01100100”;
elsifview1<=”0000”;
endif;
viewstep<=ten;
whenten=>
ifktemp>=”01011010”then
view2<=”1001”;ktemp<=ktemp-“01011010”;
elsifktemp>=”01010000”then
view2<=”1000”;ktemp<=ktemp-“01010000”;
elsifktemp>=”01000110”then
view2<=”0111”;ktemp<=ktemp-“01000110”;
elsifktemp>=”00111100”then
view2<=”0110”;ktemp<=ktemp-“00111100”;
elsifktemp>=”00110010”then
view2<=”0101”;ktemp<=ktemp-“00110010”;
elsifktemp>=”00101000”then
view2<=”0100”;ktemp<=ktemp-“00101000”;
elsifktemp>=”00011110”then
view2<=”0011”;ktemp<=ktemp-“00011110”;
elsifktemp>=”00010100”then
view2<=”0010”;ktemp<=ktemp-“00010100”;
elsifktemp>=”00001010”then
view2<=”0001”;ktemp<=ktemp-“00001010”;
elsifview2<=”0000”;
endif;
viewstep<=one
whenone=>
view3<=ktemp(3downto0);
viewstep<=takenum;
whenothers=>NULL;
endcase;
endif;
endprocessctrview;
计算器的VHDL语言
LIBRARYIEEE;
USEIEEE.STD_LOGIC_1164.ALL;
USEIEEE.STD_LOGIC_ARITH.ALL;
USEIEEE.STD_LOGIC_UNSIGNED.ALL;
Entitycalis
Port(inclk:
instd_logic;
num:
instd_logic_vector(9downto0);
plus:
instd_logic;
subt:
instd_logic;
mult:
instd_logic;
mdiv:
instd_logic;
equal:
instd_logic;
c:
instd_logic;
onum1,onum2,onum3:
outstd_logic_vector(0to0)
);
endcal;
architecturebehaveofcalis
typestateis(takenum,hundred,ten,one);
signalviewstep:
state;
signalktemp:
std_logic_vector(7downto0);
signalflag:
std_logic;
signalfl:
std_logic;
signalacc:
std_logic_vector(7downto0);
signalreg:
std_logic_vector(7downto0);
signalkeep:
std_logic_vector(7downto0);
signalans:
std_logic_vector(7downto0);
signaldans:
std_logic_vector(3downto0);
signalnumbuff:
std_logic_vector(3downto0);
signalvf:
std_logic;
signalstrdiv: