【正文】
cess(clr,clk,load,s,dir,dil) begin if clk39。event and clk=39。139。then if clr=39。139。then temp=00000000000000000000000000000000。 elsif(load=39。139。) then temp=data。 elsif s=39。139。then for i in 31 downto 1 loop實(shí)現(xiàn)右移循環(huán) temp(i1)=dout(i)。 end loop。 temp(31)=dir。 else for i in 0 to 30 loop實(shí)現(xiàn)左移循環(huán) temp(i+1)=dout(i)。 end loop。 temp(0)=dil。 end if。 end if。 dout=temp。 end process。 end art。 仿真波形如圖 所示: 圖 :連續(xù)輸入 4個(gè)或者 4個(gè)以上的 0 時(shí)檢測(cè)器輸出為高電平 1, 否則輸出為 0;如果輸入的為 0,隨 clk 的有效狀態(tài)向下個(gè)狀態(tài)轉(zhuǎn)換; 如果輸入的不為 0,返回到初始 狀態(tài) s0,重新開始檢測(cè)。 71 參考程序如下所示: library ieee。 use 。 entity sequence_check is port(din,clk,clr:in std_logic。串性輸入數(shù)據(jù)端,工作時(shí)鐘,復(fù)位信號(hào) dout: out std_logic)。檢測(cè)結(jié)果輸出 end。 architecture art of sequence_check is type statetype is (s0,s1,s2,s3,s4)。 begin process(clk,clr) variable p_state:statetype。 begin if clr=39。139。then dout=39。039。 異步清零 elsif clk39。event and clk=39。139。 then時(shí)鐘上升沿,判斷并處理當(dāng)前輸入的位 case p_state is when s0=if din=39。039。 then p_state:=s1。 else p_state:=s0。dout=39。039。end if。 when s1=if din=39。039。 then p_state:=s2。 else p_state:=s0。dout=39。039。end if。 when s2=if din=39。039。 then p_state:=s3。 else p_state:=s0。dout=39。039。end if。 when s3=if din=39。039。 then p_state:=s4。 else p_state:=s0。dout=39。039。end if。 when s4 =p_state:=s0。dout=39。139。序列檢測(cè)正確 end case。 end if。 end process。 end art。 仿真波形如圖 示: 0/0 圖 S0 1/0 S1 S2 S4 S3 0/0 1/0 1/0 1/0 0/0 0/1 X/1 72 圖 移圖如圖 所示。 參考程序如下所示: library ieee。 use 。 entity m_state is port(clk,clr:in std_logic。工作時(shí)鐘,復(fù)位信號(hào) din: in std_logic_vector(1 downto 0)。 dout: out std_logic)。檢測(cè)結(jié)果輸出 end。 architecture art of m_state is type statetype is (s0,s1,s2,s3,s4,s5,s6)。 begin process(clk,clr) variable p_state:statetype。 begin if clr=39。139。then dout=39。039。 異步清零 elsif clk39。event and clk=39。139。 then時(shí)鐘上升沿,判斷并處理當(dāng)前輸入的位 case p_state is 圖 S0 10/0 S1 S2 S6 S5 11/1 S3 S4 00/1 00/1 00/1 00/1 00/1 00/1 01/1 01/1 01/1 01/1 01/1 01/1 10/0 10/0 10/0 10/0 10/0 10/0 73 when s0=if din=00 then p_state:=s1。dout=39。139。 elsif din=10 then p_state:=s0。dout=39。039。 end if。 when s1=if din=00 then p_state:=s2。dout=39。139。 elsif din=01 then p_state:=s0。dout=39。139。 elsif din=10 then p_state:=s1。dout=39。039。 end if。 when s2=if din=00 then p_state:=s3。dout=39。139。 elsif din=01 then p_state:=s1。dout=39。139。 elsif din=10 then p_state:=s2。dout=39。039。 end if。 when s3=if din=00 then p_state:=s4。dout=39。139。 elsif din=01 then p_state:=s2。dout=39。139。 elsif din=10 then p_state:=s3。dout=39。039。 end if。 when s4=if din=00 then p_state:=s5。dout=39。139。 elsif din=01 then p_state:=s3。dout=39。139。 elsif din=10 then p_state:=s4。dout=39。039。 end if。 when s5=if din=00 then p_state:=s6。dout=39。139。 elsif din=01 then p_state:=s4。dout=39。139。 elsif din=10 then p_state:=s5。dout=39。039。 end if。 when s6=if din=11 then p_state:=s0。dout=39。139。 elsif din=10 then p_state:=s6。dout=39。039。 elsif din=01 then p_state:=s5。dout=39。139。 end if。 end case。 end if。 74 end process。 end art。 仿真波形如圖 所示: 圖 習(xí)題 7 5. 利用 MegaWizard PlugIn Manager 定制 lpmabs, 參考程序如 圖 所示: 圖 仿真波形如圖 所示: 圖 . MegaWizard PlugIn Manager 定制 lpmxor。 參考程序如圖 所示: 75 圖 仿真波形如圖 所示: 圖 “File→New” 命令,打開一個(gè)新的“ Memory initialization file”編輯窗口, 建立存儲(chǔ)器初值設(shè)定文件 ,如圖 ;然后 利用 MegaWizard PlugIn Manager定制lpmrom, 參考程序如圖 所示: 圖 76 圖 仿真波形如圖 所示: 圖 習(xí)題 9 : library ieee。 use 。 entity multiplier88 is port(din0,din1:in integer range 0 to 255。 dout: out integer range 0 to 65535)。 end。 architecture art of multiplier88 is begin dout=din0*din1。 end art。 仿真波形如圖 所示: 77 圖 主要有 12 個(gè)子模塊構(gòu)成, 參考程序如下所示: 1)時(shí)鐘的 hour 子模塊源程序: library ieee。 use 。 use 。 use 。 entity hour is port (clk_minute : in std_logic。 reset:in std_logic。 start:in std_logic。 set_hour:in std_logic_vector(7 downto 0)。 carry : out std_logic。 out1:out std_logic_vector(7 downto 0))。BCD code output,024。 end hour。 architecture behavioral of hour is begin process(clk_minute,reset,set_hour) variable n:std_logic_vector(out139。left downto out139。right)。 variable m:integer range 0 to 16。 begin if reset=39。039。then out1=set_hour。 preset the hour。 carry=39。039。 n:=set_hour。 m:=conv_integer(set_hour(set_hour39。right+3 downto 0))。n,m must be modified at the same time。 elsif rising_edge(clk_minute)then if start=39。039。then n:=n+1。 m:=m+1。 if m=10 then m=10 means n=1001。 n:=n+00000110。 Because of using BCD code,so if n=1001,n=n+6。 m:=0。 end if。 if n=00100100then when n=24 hour。 n:=00000000。 m:=0。 78 carry=39。139。 else carry=39。039。 end if。 out1=n。 end if。 end process。 end behavioral。 2)時(shí)鐘的 minute 子模塊源程序: library ieee。 use 。 use 。 use 。 entity minute is port(clk_second : in std_logic。 reset:in std_logic。 set_minute:in std_logic_vector(7 downto 0)。 carry : out std_logic。 out1:out std_logic_vector(7 downto 0))。BCD code output,060。 end minute。 architecture behavioral of minute is begin process(clk_second,reset,set_minute) variable n:std_logic_vector(out139。left downto