FPGA16位ALU的VHDL源程序,有详细注释。
思考题: 编写十六位运算器的VHDL程序——MYALU
实验任务:用CPT16 的扩展实验板上的开关K3 、K2、K1、K0做为输入、八段数码
管LED0..LED3做为输出,用VHDL语言编写程序,下载到EP1C6中,实现十六位模型机的MYALU 功能见下表。
实验说明: 在MYALU.VHD程序设计中,定义
A 为16位累加器,其值由K3 、K2 两组开关输入;
W 为16位工作寄存器,其值由K1、 K0 两组开关共输入;
S2、S1、S0 为运算功能控制位,接在K4开关的第2、1、0个开关上,根
据S2、S1、S0 的不同,MYALU 实现不同功能
D为运算结果输出,显示在四位八段管LED3..LED0上;
CIn 为进位输入,由K4 开关组的第3 个开关输入;
COut 为进位输出,用发光二极管L0 显示其状态。
逻辑功能:
library ieee; ------库程序包调用 use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
use ieee.std_logic_unsigned.all;
FPGA16位ALU的VHDL源程序,有详细注释。
entity asanxia is ------实体:电路外观配置
port
(inA : IN UNSIGNED(15 DOWNTO 0); -- K3, K2 inA
inW : IN UNSIGNED(15 DOWNTO 0); -- K1, K0 inW
outLED : OUT STD_LOGIC_VECTOR(15 DOWNTO 0); -- LED3~LED0 outLED inC : IN STD_LOGIC_VECTOR(2 DOWNTO 0); --Control Input inC
Cin:IN STD_LOGIC; --C Input k4.3 Cin
Cout: OUT STD_LOGIC -- C Output L0 Cout
);
end asanxia;
architecture arch of asanxia is ------结构体:电路功能描述
--中间变量
signal c:std_logic_vector(15 downto 0); --暂存进位信号
signal q:UNSIGNED(15 downto 0); --暂存各位数值
begin
process(inC,inA,inW,Cin)
begin
case inC is
when "000"=> ------加法:A+B ;A+B+1
q(0)<=inA(0)xor inW(0)xor Cin;
c(0)<=(inA(0) and inW(0))or (inW(0)and Cin)or (inA(0)and Cin); --inA(0),inW(0),Cin有两个以上为1即产生进位。 gen1:for i in 1 to 15 loop
q(i)<=inA(i)xor inW(i)xor c(i-1);
--本位和进位异或,产生不用and,因q(i)只能存储一位信息
c(i)<=( inA(i) and inW(i))or (inW(i)and c(i-1))or (inA(i)and c(i-1));
-- inA(0),inW(0),Cin有两个以上为1即产生进位。
end loop;
outLED<=q(15)&q(14)&q(13)&q(12)&q(11)&q(10)&q(9)&q(8)&q(7)&q(6)&q(5)&q(4)&q(3)&q(2)&q(1)&q(0); --给输出赋值,将各位连缀起来
Cout<=c(15);
when "001"=> ------减法:A-B ;A-B-1
q(0)<=inA(0)xor inW(0)xor Cin;
c(0)<=(not inA(0) and inW(0))or (inW(0)and Cin)or (not inA(0)and Cin);
gen2:for i in 1 to 15 loop
q(i)<=inA(i)xor inW(i)xor c(i-1);
c(i)<=(not inA(i) and inW(i))or (inW(i)and c(i-1))or (not inA(i)and c(i-1));
end loop;
FPGA16位ALU的VHDL源程序,有详细注释。
outLED<=q(15)&q(14)&q(13)&q(12)&q(11)&q(10)&q(9)&q(8)&q(7)&q(6)&q(5)&q(4)&q(3)&q(2)&q(1)&q(0); --给输出赋值
Cout<=c(15);
when "010"=>
if Cin='0'then
outLED<=inA+1; --执行a+1
end if;
Cout<='0';
when "011"=>
if Cin='0'then
outLED<=inA-1; --执行a-1
end if;
Cout<='0';
when "100"=> --逻辑与:AB
gen3:for i in 0 to 15 loop
q(i)<=inA(i)and inW(i);
outLED<=q(15)&q(14)&q(13)&q(12)&q(11)&q(10)&q(9)&q(8)&q(7)&q(6)&q(5)&q(4)&q(
3)&q(2)&q(1)&q(0); --给输出赋值
end loop;
when "101"=> --逻辑或:A+B
gen4:for i in 0 to 15 loop
q(i)<=inA(i)or inW(i);
outLED<=q(15)&q(14)&q(13)&q(12)&q(11)&q(10)&q(9)&q(8)&q(7)&q(6)&q(5)&q(4)&q(
3)&q(2)&q(1)&q(0); --给输出赋值
end loop;
when "110"=> --异或:A⊕B
gen5:for i in 0 to 15 loop
q(i)<=inA(i)xor inW(i);
outLED<=q(15)&q(14)&q(13)&q(12)&q(11)&q(10)&q(9)&q(8)&q(7)&q(6)&q(5)&q(4)&q(
3)&q(2)&q(1)&q(0); --给输出赋值
end loop;
when others=> --逻辑非:
outLED<=not inA(15)¬ inA(14)& not inA(13)& not inA(12)¬ inA(11)& not inA(10)& not inA(9)& not inA(8)¬ inA(7)& not inA(6)& not inA(5)& not inA(4)¬ inA(3)& not inA(2)& not inA(1)& not inA(0);
Cout<='0';
end case;
end process;
end arch;
百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说高考高中16位ALUVHDL实现源程序在线全文阅读。
相关推荐: