VHDL标准包集合文件
- 格式:pdf
- 大小:704.05 KB
- 文档页数:27
VHDL基本知识VHDL程序设计基本结构1、库、程序包库(LIBRARY)——存放预先设计好的程序包和数据的集合体。
程序包(PACKAGE)——将已定义的数据类型、元件调用说明及子程序收集在一起,供VHDL设计实体共享和调用,若干个包则形成库。
(1)VHDL库常用VHDL库有IEEE标准库、STD库和WORK库。
IEEE 标准库包括STD_LOGIC_1164程序包和STD_LOGIC_ARITH程序包。
其中,STD_LOGIC_ARITH程序包是SYNOPSYS公司加入IEEE标准库的程序包,它包括STD_LOGIC_SIGNED(有符号数)程序包、STD_LOGIC_UNSIGNED(无符号数)程序包和STD_LOGIC_SMALL_INT(小整型数)程序包。
其中STD_LOGIC_1164是最重要和最常用的程序包,大部分数字系统设计都是以此程序包设定的标准为基础。
STD库包含STANDARD和TEXTIO程序包,它们是文件输入/输出程序包,在VHDL的编译和综合过程中,系统都能自动调用这两个程序包中的任何内容。
用户在进行电路设计时,可以不必如IEEE库那样打开该库以及它的程序包。
WORK库是用户设计的现行工作库,用于存放用户自己设计的工程项目。
在PC机或工作站利用VHDL进行项目设计,不允许在根目录下进行,必须在根目录下为设计建立一个工程目录(即文件夹),VHDL综合器将此目录默认为WORK库。
但“WORK”不是设计项目的目录名,而是一个逻辑名。
VHDL标准规定WORK库总是可见的,因此在程序设计时不需要明确指定。
(2)VHDL程序包在设计实体中定义的数据类型、子程序或数据对象对于其他设计实体是不可再利用的。
为了使已定义的数据类型、子程序、元件能被其他设计实体调用或共享,可以把它们汇集在程序包中。
VHDL程序包必须经过定义后才能使用,程序包的结构中包含Type Declaration(类型定义)、Subtype Declaration(子类型定义)、Constant Declaration(常量定义)、Signal Declaration(信号定义)、ComponentDeclaration(元件定义)、Subprogram Declaration和(子程序定义)等内容,程序包定义的格式为PACKAGE 程序包名 ISType Declaration(类型定义)Subtype Declaration(子类型定义)Constant Declaration(常量定义)Signal Declaration(信号定义)Component Declaration(元件定义)Subprogram Declaration(子程序定义)END 程序包名;定义程序包的一般语句结构如下:PACKAGE 程序包名 IS -- 程序包首程序包首说明部分END 程序包名;PACKAGE BODY 程序包名 IS -- 程序包体程序包体说明部分以及包体内END 程序包名;例如:定义my_pkg程序包的结构中,包含2输入端与非门nd2元件说明、1位锁存器Latch1元件说明和求最大值函数max的函数首说明以及它的函数体说明。
VHDL 库的使用当使用库时,需要说明使用的库名称,同时需要说明库中包集合的名称及范围;每个实体都应独立进行库的说明;库的说明应该在实体之前;经过说明后,实体和结构体就可以自动调用库中的资源.首先说明总库,语句格式 library 库名,例如:library ieee;其次调用总库中的子库,用 use调取,格式:USE 库名范围(或项目名);如use ieee.std_logic_1164.all。
VHDL中库的主要种类:IEEE库、STD库、ASIC库、 work库、用户定义库和IEEE库:含有IEEE的标准包集合“STD_LOGIC_1164”以及一些大公司提供的包集合;使用前必须说明;例: library ieee; use ieee.std_logic_1164.all; 使用标准逻辑量的定义和运算; use ieee.std_logic_unsigned.all; 无符号数算术运算的定义; use ieee.std_logic_arith.all; 使用符号数算术运算的定义。
STD库:含有“STANDARD”包集合和“TEXTIO”包集合,使用前者时无需说明;ASIC库:由各公司提供,存放与逻辑门一一对应的实体,用于ASIC设计的门级仿真,使用时需加以说明;例 library altera; use altera.maxplus2.all;library lpm; use lpm.lpm_components.all;WORK库: WORK库为现行作业库,位于当前使用时设计文件的指定保存目录; WORK使用时通常无须说明;但在结构设计中进行元件的宏调用时需要说明;例:use work.all;用户定义库:由用户自定义生成,使用时需说明(指定库所在的路径);package 包集合:用于罗列VHDL语言中使用的类型定义、信号定义、常数定义、元件定义、函数定义和过程定义等(类似于C语言中的include语句),方便不同模块的设计中公共定义的共享。
library ieee;use ieee.std_logic_1164.all;entity code isport(clk50m:in std_logic; --50MHz 的输入时钟clk:in std_logic; --时钟选择后得到的时钟,即不同难度对应的时钟start:in std_logic; --开始信号,高电平有效k1,k2,k3,k4,k5,k6,k7,k8:in std_logic; --8 个输入按键clear:in std_logic; --最高分清零信号,clear=1 时清除最高分纪录en:in STD_LOGIC; --暂停信号,en=0 时暂停key:out std_logic_vector(2 downto 0)); --按键编码后得到的编码end code;--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------library ieee;use ieee.std_logic_1164.all;entity code isport(clk50m:in std_logic; --50MHz 的输入时钟clk:in std_logic; --时钟选择后得到的时钟,即不同难度对应的时钟start:in std_logic; --开始信号,高电平有效k1,k2,k3,k4,k5,k6,k7,k8:in std_logic; --8 个输入按键key:out std_logic_vector(2 downto 0)); --按键编码后得到的编码end code;architecture Behavioral of code issignal flag:std_logic; --标志位beginprocess(clk50m,clk,start)beginif start='0' then --初始时令flag=0flag<='0';elsif clk50m='1' then--用50MHz 的时钟进行采样编码,且在一个难度时钟内只采样编码一次if flag='0' thenif k8='1' thenkey<="111";flag<='1'; --flag=1 时表明已经采样编码完毕elsif k7='1' thenkey<="110";flag<='1';elsif k6='1' thenkey<="101";flag<='1';elsif k5='1' thenkey<="100";flag<='1';elsif k4='1' thenkey<="011";flag<='1';elsif k3='1' thenkey<="010";flag<='1';elsif k2='1' thenkey<="001";flag<='1';elsif k1='1' thenkey<="000";flag<='1';end if;end if;elsif rising_edge(clk) then --在难度时钟的上升沿将flag 置0,表明可进行采样编码if flag='1' thenflag<='0';end if;end if;end process;end Behavioral;entity divider isPort ( clk50m : in STD_LOGIC; --50MHz 的输入时钟start:in STD_LOGIC; --开始信号,高电平有效A : out STD_LOGIC; --难度A 的周期为2.5 秒B : out STD_LOGIC; --难度B 的周期为2.0 秒C : out STD_LOGIC; --难度C 的周期为1.5 秒D : out STD_LOGIC; --难度D 的周期为1.0 秒clk500:out STD_LOGIC; --clk500=500Hzclk4:out std_logic; --clk=4Hzclk6m:out std_logic); --clk6m=6.25MHzend divider;architecture Behavioral of divider issignal ma,mb,mc,md,m4,m500,m6m:std_logic:='0';signal cnt1:integer range 1 to 50000;signal cnt2:integer range 1 to 6250000;signal cnt3:integer range 1 to 2;signal cnt4:integer range 1 to 5;signal cnt5:integer range 1 to 3;beginA<=ma;B<=mb;C<=mc;D<=md;clk500<=m500;clk6m<=m6m;clk4<=m4;process(clk50m)variable n:integer range 1 to 5:=1;beginif rising_edge(clk50m) then --50MHz8 分频后得到6.25MHz 的信号clk6m n:=n+1;if n=5 thenm6m<=not m6m;n:=1;end if;if cnt1=50000 then --50MHz100000 分频后得到500Hz 的信号clk500m500<=not m500;cnt1<=1;elsecnt1<=cnt1+1;end if;if cnt2=6250000 then ----50MHz12500000 分频后得到4Hz 的信号clk4 m4<=not m4;cnt2<=1;elsecnt2<=cnt2+1;end if;end if;end process;process(start,m4,md)beginif start='0' then --start=0 时初始化ma<='0';mb<='0';mc<='0';md<='0';cnt3<=1;cnt5<=1;elseif rising_edge(m4) then --4Hz 时钟4 分频后得到1Hzif cnt3=2 thencnt3<=1;md<=not md;elsecnt3<=cnt3+1;end if;if cnt4=5 then --4Hz 时钟10 分频后得到0.4Hz 时ma<=not ma;cnt4<=1;elsecnt4<=cnt4+1;end if;if cnt5=3 then --4Hz 时钟6 分频后得到0.67Hz 时mc<=not mc;cnt5<=1;elsecnt5<=cnt5+1;end if;end if;if rising_edge(md) then --1Hz 时钟2 分频后得到0.5Hz 的时mb<=not mb;end if;end if;end process;end Behavioral;entity clk_sel isport(clk50m:in std_logic; --50MHz 的输入时钟start:in std_logic; --开始信号,高电平有效A,B,C,D:in std_logic; --A,B,C,D 分别对应难度A,B,C,D 的时钟en_A,en_B,en_C,en_D:in std_logic;--en_A,en_B,en_C,en_D 分别为A,B,C,D 的使能信号leda,ledb,ledc,ledd:out std_logic;--不同的难度用不同的LED 来指示clkout:out std_logic); --不同的难度输出对应的难度时钟end clk_sel;architecture Behavioral of clk_sel issignal m:std_logic_vector(3 downto 0);signal clk:std_logic:='0';beginm<=en_A&en_B&en_C&en_D;clkout<=clk;process(clk50m)beginif start='0' thenclk<='0';elsif rising_edge(clk50m) thenif m=8 then --当en_A='1'时,时钟对应为难度A 的时钟,且指示难度A 的LED clk<=A;leda<='1';ledb<='0';ledc<='0';ledd<='0';elsif m=4 then --当en_B='1'时,时钟对应为难度B 的时钟,且指示难度B 的LED 亮clk<=B;leda<='0';ledb<='1';ledc<='0';ledd<='0';elsif m=2 then --当en_C='1'时,时钟对应为难度C 的时钟,且指示难度C 的LED 亮clk<=C;leda<='0';ledb<='0';ledc<='1';ledd<='0';elsif m=1 then --当en_D='1'时,时钟对应为难度D 的时钟,且指示难度D 的LED 亮clk<=D;leda<='0';ledb<='0';ledc<='0';ledd<='1';elseclk<='0';end if;end if;end process;end Behavioral;entity random isPort ( clk : in STD_LOGIC; --难度时钟start:in STD_LOGIC; --开始信号,高电平有效en:in STD_LOGIC; --暂停信号,en=0 时暂停led : out STD_LOGIC_VECTOR (2 downto 0)); --产生的三位随机数end random;architecture Behavioral of random issignal m:std_logic_vector(7 downto 0):="10000000";beginled<=m(2 downto 0);process(clk,en,start)beginif start='0' thenm<="11000011";elsif en='0' thenm<=m;elsif rising_edge(clk) thenm(7 downto 1)<=m(6 downto 0);m(0)<=m(7) xor m(3) xor m(2) xor m(1);end if;end process;end Behavioral;entity compare isport( start:in std_logic; --开始信号,高电平有效clear:in std_logic; --最高分清零信号,clear=1 时清除最高分纪录en:in std_logic; --暂停信号,en=0 时暂停clk : in STD_LOGIC; --clk50m 为50MHz 的输入时钟,clk 为难度时钟key : in STD_LOGIC_VECTOR (2 downto 0); --key 为编码后得到的编码值led : in STD_LOGIC_VECTOR (2 downto 0);--led 为产生的三位随机数win,lose:out STD_LOGIC; --win=1 表明游戏胜利,lose=1 表明游戏失败en_A : out STD_LOGIC; --难度A 的使能信号en_B : out STD_LOGIC; --难度B 的使能信号en_C : out STD_LOGIC; --难度C 的使能信号en_D : out STD_LOGIC; --难度D 的使能信号highscore_out:out STD_LOGIC_VECTOR(11 downto 0); --最高分score_out : out STD_LOGIC_VECTOR (11 downto 0); --当前分数life_out : out STD_LOGIC_VECTOR (7 downto 0)); --当前生命数end compare;architecture Behavioral of compare issignal mscore,mhighscore:integer range 0 to 999;signal mlife:integer range 0 to 99;signal result,mwin,mlose:std_logic;signal cnt1:integer range 0 to 4;signal cnt2:integer range 0 to 1;type state is(na,nb,nc,nd);signal nandu:state;beginresult<=mwin or mlose; --当result=1 时游戏结束,LCD 上会显示输或赢win<=mwin;lose<=mlose;--得5 分加一条命,失误一次扣一分,失误达到2 次扣一条命,当分数为0 时,失误一次不减分,但扣一条命process(clk,start,en,clear)beginif mscore>mhighscore thenmhighscore<=mscore;elsemhighscore<=mhighscore;end if;if start='0' thenmscore<=0; --初始分数为0mlife<=9; --初始命数为9cnt1<=0;mhighscore<=mhighscore;cnt2<=0;mwin<='0';mlose<='0';nandu<=na;en_A<='1';en_B<='0';en_C<='0';en_D<='0';elsif clear='1' thenmhighscore<=0;elsif en='0' thenmscore<=mscore;mhighscore<=mhighscore;mlife<=mlife;elsif rising_edge(clk) thenif result='0' thenif led=key thenif mlife=99 thenmwin<='1';elsif mlife<99 thenif mscore=999 thenmwin<='1';elsif mscore<999 thenmscore<=mscore+1;if cnt1=4 thenmlife<=mlife+1;cnt1<=0;elsecnt1<=cnt1+1;end if;end if;elsif mlife=0 thenmlose<='1';end if;elseif mscore=0 and mlife=0 thenmscore<=0;mlife<=0;mlose<='1';elsif mlife=0 thenmlife<=0;mscore<=mscore;mlose<='1';elsif mscore=0 and mlife>0 then mscore<=0;mlife<=mlife-1;elsemscore<=mscore-1;if cnt2=1 thenmlife<=mlife-1;cnt2<=0;elsecnt2<=cnt2+1;end if;end if;end if;end if;case nandu iswhen na=> --当分数小于5 分时,难度为Aen_A<='1';en_B<='0';en_C<='0';en_D<='0';if mscore>=5 and mscore<10 thennandu<=nb;elsenandu<=na;end if;when nb=> --当分数在5-10 之间时,难度为B en_A<='0';en_B<='1';en_C<='0';en_D<='0';if mscore>=10 and mscore<15 thennandu<=nc;elsenandu<=nb;end if;when nc=> --当分数在10-15 之间时,难度为C en_A<='0';en_B<='0';en_C<='1';en_D<='0';if mscore>=15 and mscore<20 thennandu<=nd;elsenandu<=nc;end if;when nd=> --当分数在15 以上时,难度为Den_A<='0';en_B<='0';en_C<='0';en_D<='1';nandu<=nd;when others=>en_A<='1';en_B<='0';en_C<='0';en_D<='0';nandu<=na;end case;end if;end process;process(clk)beginif falling_edge(clk) thenif mscore>=900 thenscore_out(11 downto 8)<=x"9";if (mscore-900)>=90 thenscore_out(7 downto 4)<=x"9";score_out(3 downto 0)<=conv_std_logic_vector((mscore-990),4);elsif (mscore-900)>=80 thenscore_out(7 downto 4)<=x"8";score_out(3 downto 0)<=conv_std_logic_vector((mscore-980),4); elsif (mscore-900)>=70 thenscore_out(7 downto 4)<=x"7";score_out(3 downto 0)<=conv_std_logic_vector((mscore-970),4); elsif (mscore-900)>=60 thenscore_out(7 downto 4)<=x"6";score_out(3 downto 0)<=conv_std_logic_vector((mscore-960),4); elsif (mscore-900)>=50 thenscore_out(7 downto 4)<=x"5";score_out(3 downto 0)<=conv_std_logic_vector((mscore-950),4); elsif (mscore-900)>=40 thenscore_out(7 downto 4)<=x"4";score_out(3 downto 0)<=conv_std_logic_vector((mscore-940),4); elsif (mscore-900)>=30 thenscore_out(7 downto 4)<=x"3";score_out(3 downto 0)<=conv_std_logic_vector((mscore-930),4); elsif (mscore-900)>=20 thenscore_out(7 downto 4)<=x"2";score_out(3 downto 0)<=conv_std_logic_vector((mscore-920),4); elsif (mscore-900)>=10 thenscore_out(7 downto 4)<=x"1";score_out(3 downto 0)<=conv_std_logic_vector((mscore-910),4); elsescore_out(7 downto 4)<=x"0";score_out(3 downto 0)<=conv_std_logic_vector((mscore-900),4); end if;……if mhighscore>=900 thenhighscore_out(11 downto 8)<=x"9";if (mhighscore-900)>=90 thenhighscore_out(7 downto 4)<=x"9";highscore_out(3 downto0)<=conv_std_logic_vector((mhighscore-990),4);elsif (mhighscore-900)>=80 thenhighscore_out(7 downto 4)<=x"8";highscore_out(3 downto0)<=conv_std_logic_vector((mhighscore-980),4);elsif (mhighscore-900)>=70 thenhighscore_out(7 downto 4)<=x"7";highscore_out(3 downto0)<=conv_std_logic_vector((mhighscore-970),4);elsif (mhighscore-900)>=60 thenhighscore_out(7 downto 4)<=x"6";highscore_out(3 downto0)<=conv_std_logic_vector((mhighscore-960),4);elsif (mhighscore-900)>=50 thenhighscore_out(7 downto 4)<=x"5";highscore_out(3 downto0)<=conv_std_logic_vector((mhighscore-950),4);elsif (mhighscore-900)>=40 thenhighscore_out(7 downto 4)<=x"4";highscore_out(3 downto0)<=conv_std_logic_vector((mhighscore-940),4);elsif (mhighscore-900)>=30 thenhighscore_out(7 downto 4)<=x"3";highscore_out(3 downto0)<=conv_std_logic_vector((mhighscore-930),4);elsif (mhighscore-900)>=20 thenhighscore_out(7 downto 4)<=x"2";highscore_out(3 downto0)<=conv_std_logic_vector((mhighscore-920),4);elsif (mhighscore-900)>=10 thenhighscore_out(7 downto 4)<=x"1";highscore_out(3 downto0)<=conv_std_logic_vector((mhighscore-910),4);elsehighscore_out(7 downto 4)<=x"0";highscore_out(3 downto0)<=conv_std_logic_vector((mhighscore-900),4);end if;……if falling_edge(clk) thenif (mlife)>=90 thenlife_out(7 downto 4)<=x"9";life_out(3 downto 0)<=conv_std_logic_vector((mlife-90),4); elsif (mlife)>=80 thenlife_out(7 downto 4)<=x"8";life_out(3 downto 0)<=conv_std_logic_vector((mlife-80),4); elsif (mlife)>=70 thenlife_out(7 downto 4)<=x"7";life_out(3 downto 0)<=conv_std_logic_vector((mlife-70),4); elsif (mlife)>=60 thenlife_out(7 downto 4)<=x"6";life_out(3 downto 0)<=conv_std_logic_vector((mlife-60),4); elsif (mlife)>=50 thenlife_out(7 downto 4)<=x"5";life_out(3 downto 0)<=conv_std_logic_vector((mlife-50),4); elsif (mlife)>=40 thenlife_out(7 downto 4)<=x"4";life_out(3 downto 0)<=conv_std_logic_vector((mlife-40),4); elsif (mlife)>=30 thenlife_out(7 downto 4)<=x"3";life_out(3 downto 0)<=conv_std_logic_vector((mlife-30),4); elsif (mlife)>=20 thenlife_out(7 downto 4)<=x"2";life_out(3 downto 0)<=conv_std_logic_vector((mlife-20),4); elsif (mlife)>=10 thenlife_out(7 downto 4)<=x"1";life_out(3 downto 0)<=conv_std_logic_vector((mlife-10),4); elselife_out(7 downto 4)<=x"0";life_out(3 downto 0)<=conv_std_logic_vector((mlife),4); end if;……end process;end Behavioral;entity lcd isport( clk500:in std_logic; --输入的500Hz 的时钟start : in STD_LOGIC; --开始信号,高电平有效win,lose:in std_logic; --win=1 表明游戏胜利,lose=1 表明游戏失败highscore:in STD_LOGIC_VECTOR(11 downto 0); --最高分score: in STD_LOGIC_VECTOR (11 downto 0); --当前分数life: in STD_LOGIC_VECTOR (7 downto 0); --当前生命LCD_RS : out STD_LOGIC;LCD_RW : out STD_LOGIC;LCD_EN : out STD_LOGIC;data : out STD_LOGIC_VECTOR (3 downto 0)); --数据信号输出end lcd;architecture Behavioral of lcd is--译码函数完成字符译码function putc(data:character) return std_logic_vector isvariable result:std_logic_vector(7 downto 0);begincase data iswhen 'E'=> result:=x"45";when 'I'=> result:=x"49";when 'L'=> result:=x"4C";when 'N'=> result:=x"4E";when 'O'=> result:=x"4F";when 'S'=> result:=x"53";when 'U'=> result:=x"55";when 'W'=> result:=x"57";when 'Y'=> result:=x"59";when 'c'=> result:=x"63";when 'e'=> result:=x"65";when 'f'=> result:=x"66";when 'g'=> result:=x"67";when 'h'=> result:=x"68";when 'i'=> result:=x"69";when 'l'=> result:=x"6C";when 'o'=> result:=x"6F";when 'r'=> result:=x"72";when 's'=> result:=x"73";when '!'=> result:=x"21";when others => result:=x"20";end case;return result;end putc;--译码函数完成0~9 的ASCII 译码function putn(num:in std_logic_vector(3 downto 0)) return std_logic_vector is variable fig:std_logic_vector(7 downto 0);begincase num iswhen "0000"=> fig:="00110000";when "0001"=> fig:="00110001";when "0010"=> fig:="00110010";when "0011"=> fig:="00110011";when "0100"=> fig:="00110100";when "0101"=> fig:="00110101";when "0110"=> fig:="00110110";when "0111"=> fig:="00110111";when "1000"=> fig:="00111000";when "1001"=> fig:="00111001";when others=> fig:="00100000";end case;return fig;end putn;type istate is(write_instr, --写命令字write_dataup, --写LCD 高四位write_datadown, --写LCD 低四位set_addrup, --设定位置高四位set_addrdown, --设定位置低四位ret_homeup, --光标归位指令高四位ret_homedown); --光标归位指令低四位signal state:istate;signal cnt:integer range 0 to 29:=0;beginLCD_RW<='0';LCD_EN<=clk500;process(clk500,start) --LCD 显示控制beginif start='0' thenstate<=write_instr;LCD_RS<='0';cnt<=0;elsif rising_edge(clk500) thencase state iswhen write_instr=> --写命令字到LCD 控制器,初始化液晶,并设置显示格式LCD_RS<='0';case cnt is --写命令字0x33when 0=> data<="0011";when 1=> data<="0011";--写命令字0x32when 2=> data<="0011";when 3=> data<="0010";--0x28:功能设置,4 位接口模式,2 行显示when 4=> data<="0010";when 5=> data<="1000";---0x06:模式设定,输入后光标右移when 6=> data<="0000";when 7=> data<="0110";--0x0c:显示设定,显示功能开--不显示光标,光标不闪烁when 8=> data<="0000";when 9=> data<="1100";--0x01:清屏when 10=> data<="0000";when 11=> data<="0001";when others=> data<="0000";end case;if cnt>=11 thencnt<=0;state<=write_dataup;elsecnt<=cnt+1;state<=write_instr;end if;when write_dataup=>LCD_RS<='1';--当win=‘1’时第一行显示YOU WIN!--第二行显示high xxxif win='1' thencase cnt iswhen 0=> data<=putc('Y')(7 downto 4);when 1=> data<=putc('O')(7 downto 4);when 2=> data<=putc('U')(7 downto 4);when 3=> data<=putc(' ')(7 downto 4);when 4=> data<=putc('W')(7 downto 4);when 5=> data<=putc('I')(7 downto 4);when 6=> data<=putc('N')(7 downto 4);when 7=> data<=putc('!')(7 downto 4);when 8=> data<=putc(' ')(7 downto 4);when 9=> data<=putc(' ')(7 downto 4);when 10=> data<=putc(' ')(7 downto 4);when 11=> data<=putc(' ')(7 downto 4);when 12=> data<=putc(' ')(7 downto 4);when 13=> data<=putc(' ')(7 downto 4);when 14=> data<=putc(' ')(7 downto 4);--转到第二行显示when 15=> data<=putc('h')(7 downto 4);when 16=> data<=putc('i')(7 downto 4);when 17=> data<=putc('g')(7 downto 4);when 18=> data<=putc('h')(7 downto 4);when 19=> data<=putc(' ')(7 downto 4);when 20=> data<=putc(' ')(7 downto 4);when 22=> data<=putn(highscore(11 downto 8))(7 downto 4); when 23=> data<=putn(highscore(7 downto 4))(7 downto 4); when 24=> data<=putn(highscore(3 downto 0))(7 downto 4); when 25=> data<=putc(' ')(7 downto 4);when 26=> data<=putc(' ')(7 downto 4);when 27=> data<=putc(' ')(7 downto 4);when 28=> data<=putc(' ')(7 downto 4);when 29=> data<=putc(' ')(7 downto 4);when others=> data<="0000";end case;state<=write_datadown;--当lose=‘1’时第一行显示YOU LOSE!--第二行显示high xxxelsif lose='1' thencase cnt iswhen 0=> data<=putc('Y')(7 downto 4);when 1=> data<=putc('O')(7 downto 4);when 2=> data<=putc('U')(7 downto 4);when 3=> data<=putc(' ')(7 downto 4);when 4=> data<=putc('L')(7 downto 4);when 5=> data<=putc('O')(7 downto 4);when 6=> data<=putc('S')(7 downto 4);when 7=> data<=putc('E')(7 downto 4);when 8=> data<=putc('!')(7 downto 4);when 9=> data<=putc(' ')(7 downto 4);when 10=> data<=putc(' ')(7 downto 4);when 11=> data<=putc(' ')(7 downto 4);when 12=> data<=putc(' ')(7 downto 4);when 13=> data<=putc(' ')(7 downto 4);when 14=> data<=putc(' ')(7 downto 4);--转到第二行显示when 15=> data<=putc('h')(7 downto 4);when 16=> data<=putc('i')(7 downto 4);when 17=> data<=putc('g')(7 downto 4);when 18=> data<=putc('h')(7 downto 4);when 19=> data<=putc(' ')(7 downto 4);when 20=> data<=putc(' ')(7 downto 4);when 21=> data<=putc(' ')(7 downto 4);when 22=> data<=putn(highscore(11 downto 8))(7 downto 4); when 23=> data<=putn(highscore(7 downto 4))(7 downto 4); when 24=> data<=putn(highscore(3 downto 0))(7 downto 4); when 25=> data<=putc(' ')(7 downto 4);when 26=> data<=putc(' ')(7 downto 4);when 28=> data<=putc(' ')(7 downto 4);when 29=> data<=putc(' ')(7 downto 4);when others=> data<="0000";end case;state<=write_datadown;--其他情况下第一行显示life score high--第二行显示xx xxx xxx 分别代表命数、分数和历史最高分elsecase cnt iswhen 0=> data<=putc('l')(7 downto 4);when 1=> data<=putc('i')(7 downto 4);when 2=> data<=putc('f')(7 downto 4);when 3=> data<=putc('e')(7 downto 4);when 4=> data<=putc(' ')(7 downto 4);when 5=> data<=putc('s')(7 downto 4);when 6=> data<=putc('c')(7 downto 4);when 7=> data<=putc('o')(7 downto 4);when 8=> data<=putc('r')(7 downto 4);when 9=> data<=putc('e')(7 downto 4);when 10=> data<=putc(' ')(7 downto 4);when 11=> data<=putc('h')(7 downto 4);when 12=> data<=putc('i')(7 downto 4);when 13=> data<=putc('g')(7 downto 4);when 14=> data<=putc('h')(7 downto 4);--转到第二行显示when 15=> data<=putc(' ')(7 downto 4);when 16=> data<=putc(' ')(7 downto 4);when 17=> data<=putn(life(7 downto 4))(7 downto 4);when 18=> data<=putn(life(3 downto 0))(7 downto 4);when 19=> data<=putc(' ')(7 downto 4);when 20=> data<=putc(' ')(7 downto 4);when 21=> data<=putc(' ')(7 downto 4);when 22=> data<=putn(score(11 downto 8))(7 downto 4); when 23=> data<=putn(score(7 downto 4))(7 downto 4); when 24=> data<=putn(score(3 downto 0))(7 downto 4); when 25=> data<=putc(' ')(7 downto 4);when 26=> data<=putc(' ')(7 downto 4);when 27=> data<=putn(highscore(11 downto 8))(7 downto 4); when 28=> data<=putn(highscore(7 downto 4))(7 downto 4); when 29=> data<=putn(highscore(3 downto 0))(7 downto 4); when others=>data<="0000";end case;state<=write_datadown;end if;when write_datadown=>--当win='1'时第一行显示YOU WIN!--第二行显示high xxxif win='1' thencase cnt iswhen 0=> data<=putc('Y')(3 downto 0);when 1=> data<=putc('O')(3 downto 0);when 2=> data<=putc('U')(3 downto 0);when 3=> data<=putc(' ')(3 downto 0);when 4=> data<=putc('W')(3 downto 0);when 5=> data<=putc('I')(3 downto 0);when 6=> data<=putc('N')(3 downto 0);when 7=> data<=putc('!')(3 downto 0);when 8=> data<=putc(' ')(3 downto 0);when 9=> data<=putc(' ')(3 downto 0);when 10=> data<=putc(' ')(3 downto 0);when 11=> data<=putc(' ')(3 downto 0);when 12=> data<=putc(' ')(3 downto 0);when 13=> data<=putc(' ')(3 downto 0);when 14=> data<=putc(' ')(3 downto 0);--转到第二行显示when 15=> data<=putc('h')(3 downto 0);when 16=> data<=putc('i')(3 downto 0);when 17=> data<=putc('g')(3 downto 0);when 18=> data<=putc('h')(3 downto 0);when 19=> data<=putc(' ')(3 downto 0);when 20=> data<=putc(' ')(3 downto 0);when 21=> data<=putc(' ')(3 downto 0);when 22=> data<=putn(highscore(11 downto 8))(3 downto 0); when 23=> data<=putn(highscore(7 downto 4))(3 downto 0); when 24=> data<=putn(highscore(3 downto 0))(3 downto 0); when 25=> data<=putc(' ')(3 downto 0);when 26=> data<=putc(' ')(3 downto 0);when 27=> data<=putc(' ')(3 downto 0);when 28=> data<=putc(' ')(3 downto 0);when 29=> data<=putc(' ')(3 downto 0);when others=> data<="0000";end case;if cnt=14 thencnt<=15;state<=set_addrup;elsif cnt=29 thencnt<=0;elsecnt<=cnt+1;state<=write_dataup;end if;--当lose=‘1’时第一行显示YOU LOSE!--第二行显示high xxxelsif lose='1' thencase cnt iswhen 0=> data<=putc('Y')(3 downto 0);when 1=> data<=putc('O')(3 downto 0);when 2=> data<=putc('U')(3 downto 0);when 3=> data<=putc(' ')(3 downto 0);when 4=> data<=putc('L')(3 downto 0);when 5=> data<=putc('O')(3 downto 0);when 6=> data<=putc('S')(3 downto 0);when 7=> data<=putc('E')(3 downto 0);when 8=> data<=putc('!')(3 downto 0);when 9=> data<=putc(' ')(3 downto 0);when 10=> data<=putc(' ')(3 downto 0);when 11=> data<=putc(' ')(3 downto 0);when 12=> data<=putc(' ')(3 downto 0);when 13=> data<=putc(' ')(3 downto 0);when 14=> data<=putc(' ')(3 downto 0);--转到第二行显示when 15=> data<=putc('h')(3 downto 0);when 16=> data<=putc('i')(3 downto 0);when 17=> data<=putc('g')(3 downto 0);when 18=> data<=putc('h')(3 downto 0);when 19=> data<=putc(' ')(3 downto 0);when 20=> data<=putc(' ')(3 downto 0);when 21=> data<=putc(' ')(3 downto 0);when 22=> data<=putn(highscore(11 downto 8))(3 downto 0); when 23=> data<=putn(highscore(7 downto 4))(3 downto 0); when 24=> data<=putn(highscore(3 downto 0))(3 downto 0); when 25=> data<=putc(' ')(3 downto 0);when 26=> data<=putc(' ')(3 downto 0);when 27=> data<=putc(' ')(3 downto 0);when 28=> data<=putc(' ')(3 downto 0);when 29=> data<=putc(' ')(3 downto 0);when others=> data<="0000";end case;if cnt=14 thencnt<=15;elsif cnt=29 thencnt<=0;state<=ret_homeup;elsecnt<=cnt+1;state<=write_dataup;end if;--其他情况下第一行显示life score high--第二行显示xx xxx xxx 分别代表命数、分数和历史最高分elsecase cnt iswhen 0=> data<=putc('l')(3 downto 0);when 1=> data<=putc('i')(3 downto 0);when 2=> data<=putc('f')(3 downto 0);when 3=> data<=putc('e')(3 downto 0);when 4=> data<=putc(' ')(3 downto 0);when 5=> data<=putc('s')(3 downto 0);when 6=> data<=putc('c')(3 downto 0);when 7=> data<=putc('o')(3 downto 0);when 8=> data<=putc('r')(3 downto 0);when 9=> data<=putc('e')(3 downto 0);when 10=> data<=putc(' ')(3 downto 0);when 11=> data<=putc('h')(3 downto 0);when 12=> data<=putc('i')(3 downto 0);when 13=> data<=putc('g')(3 downto 0);when 14=> data<=putc('h')(3 downto 0);--转到第二行显示when 15=> data<=putc(' ')(3 downto 0);when 16=> data<=putc(' ')(3 downto 0);when 17=> data<=putn(life(7 downto 4))(3 downto 0);when 18=> data<=putn(life(3 downto 0))(3 downto 0);when 19=> data<=putc(' ')(3 downto 0);when 20=> data<=putc(' ')(3 downto 0);when 21=> data<=putc(' ')(3 downto 0);when 22=> data<=putn(score(11 downto 8))(3 downto 0); when 23=> data<=putn(score(7 downto 4))(3 downto 0); when 24=> data<=putn(score(3 downto 0))(3 downto 0); when 25=> data<=putc(' ')(3 downto 0);when 26=> data<=putc(' ')(3 downto 0);when 27=> data<=putn(highscore(11 downto 8))(3 downto 0); when 28=> data<=putn(highscore(7 downto 4))(3 downto 0); when 29=> data<=putn(highscore(3 downto 0))(3 downto 0); when others=>data<="0000";。
VHDL程序的基本结构一个完整的VHDL语言程序通常包含库(Library):如ieee, std, work等程序包(Package)实体(Entity):I/O 端口结构体或构造体(Architecture):具体描述电路所要实现的功能配置(Configuration)实体:用于描述所设计系统的外部接口特性;即该设计实体对外的输入、输出端口数量和端口特性。
结构体:用于描述实体所代表的系统内部的结构和行为;它描述设计实体的结构、行为、元件及内部连接关系。
库:存放已经编译的实体、结构体、程序包集合和配置。
程序包:存放各设计模块都能共享的数据类型、常数和子程序库;配置:实体对应多个结构体时,从与某个实体对应的多个结构体中选定一个作为具体实现。
保存的文件名与实体名字应该一致PORT(端口)说明PORT ( 端口名: 端口模式数据类型;端口名: 端口模式数据类型;…………) ;端口模式out (输出):只能被赋值,用于不能反馈的输出;in (输入):只能读,用于时钟输入、控制输入单向数据输入;inout(输入输出) :既可读又可被赋值,被读的值是端口输入值而不是被赋值,作为双向端口。
buffer(缓冲):类似于输出,但可以读,读的值是被赋值,用做内部反馈用,不能作为双向端口使用结构体的一般表达如下:ARCHITECTURE 结构体名称OF 实体名称IS[说明语句]可以不要BEGIN(功能描述语句)END ARCHITECTURE 结构体名称;功能描述语句:处于begin 与end 之间,描述结构体的行为与连接关系,它是构成结构体的主体。
描述实体的逻辑行为、以各种不同的描述风格表达的功能描述语句,或针对层次设计中,以元件例化语句为特征的外部元件(设计实体)端口间的连接。
库:是一些常用代码的集合。
库说明总是放在设计单元的最前面将常用代码存放到库中有利于设计的复用和代码共享,也可使代码结构更清晰;(1)IEEE 库IEEE 库定义了四个常用的程序包:std_logic_1164 (std_logic (8值)& std_ulogic (9值)逻辑值的逻辑运算) std_logic_arith (signed 、unsigned 数据类型的(算术、比较运算函数)std_logic_signed (std_logic_vector 类型数据的进行有符号算术、比较运算操作函数) std_logic_unsigned (std_logic_vector 类型数据进行无符号算术、比较运算操作函数)(2)STD 库(默认库)VHDL 的标准资源库,包括数据类型和输入/输出文本等内容。