2014-04-10 77 views
0

我想在BASYS2上實現隨機數遊戲。在這個遊戲中會有五個LED被選中,其中一個會隨機打開一兩秒(這個時間可以改變以增加或減少遊戲的難度)。然後,用戶需要在開機後按下後面的開關按鈕來響應此LED事件。如果他或她能夠成功完成,則會得分並且將顯示在七段顯示屏上。如果他或她失敗了,則不會得分。將有9個這樣的事件之後,遊戲可以重播。隨機LED在VHDL中打開和關閉

Now following is my code (only for the random LED turning on). However, I am unable to fix it. Please somebody help. The FPGA I am using is BASYS2 SPARTAN 3E-100. 

在此先感謝大家。

library IEEE; 
use IEEE.STD_LOGIC_1164.ALL; 
use IEEE.STD_LOGIC_unsigned.ALL; 
use IEEE.STD_LOGIC_arith.ALL; 

entity random_number is 
generic (width : integer := 4); 
port (
clk : in std_logic; 
reset : in std_logic; 
random_num : out std_logic_vector (width-1 downto 0) --output vector    
); 
end random_number; 

architecture Behavioral of random_number is 
signal q: std_logic_vector(23 downto 0); 
signal divided_clock: std_logic; 
begin 
process(clk, reset) 
begin 
if (reset = '1')then 
q <= X"000000"; 
elsif(rising_edge(clk)) then 
q <= q + 1; 
end if; 
end process; 
divided_clock <= q(22); 


process (divided_clock) 
variable rand_temp : std_logic_vector(width-1 downto 0):=("1000"); 
variable temp : std_logic := '0'; 
begin 
if(rising_edge(divided_clock)) then 
temp := rand_temp(width-1) xor rand_temp(width-2); 
rand_temp(width-1 downto 1) := rand_temp(width-2 downto 0); 
rand_temp(0) := temp; 
end if; 
random_num <= rand_temp; 
end process; 
end Behavioral; 

回答

1

我認爲第二個過程,即使應與主CLK和devided時鐘應該是一個能運行。

signal divided_enable: std_logic; 
process(clk, reset) 
begin 
if (reset = '1')then 
    q <= X"000000"; 
elsif(rising_edge(clk)) then 
    q <= q + 1; 
end if; 
if (q(22) = '1') then 
    --short pulse wenn q bit 22 is high 
    divided_enable <= '1'; 
    q <= (others => '0'); 
end if; 
end process; 


process (clk) 
variable rand_temp : std_logic_vector(width-1 downto 0):=("1000"); 
variable temp : std_logic := '0'; 
begin 
if(rising_edge(clk)) then 
    if(divided_enable = '1') then 
    temp := rand_temp(width-1) xor rand_temp(width-2); 
    rand_temp(width-1 downto 1) := rand_temp(width-2 downto 0); 
    rand_temp(0) := temp; 
    end if; 
end if; 
random_num <= rand_temp; 
end process; 

我不知道這是否能解決您的所有問題。請描述編譯器錯誤或行爲錯誤。

+0

這真是太好了。你修改的代碼沒有任何錯誤。我會不斷提出一些問題,因爲我有一個項目要在本學期結束時提交。回頭見。 – user3518317

+0

@ user3518317,如果解決方案適用於您,也許您可​​以將答案標記爲已接受?這是這裏的標準做法,也是表達你的感激之情的好方法:) – rick