2012-11-26 55 views
0

我是VHDL的新手。我需要編寫一個模塊來進行數據過濾。我的模塊結構是:VHDL過濾數據

a_rst - async reset 
clk - clock 
s_rst - sync reset 
valid_in - 0 - no data, 1 - where is data 
data_in - [7 downto 0] 

輸出信號:

valid_out - 0 - no data, 1 - where is data 
data_out - [7 downto 0] 

我寫testbeanch這使我的模塊data_in:00,01,,03,0A,02,00,01 ,,0F。

但我模塊返回:00,01,AA,03,0A,02,00,01,AA,0F
insted的組成:00,01,AA,03,0A,02 ,00,01,,0F。

我試着這樣做:

--libraries 
library ieee; 
use ieee.std_logic_1164.all; 
use ieee.numeric_std.all; 
--entity 
entity ex6_v03 is 
port 
(
a_rst  : in std_logic; 
clk  : in std_logic; -- 200 MHz 
s_rst  : in std_logic; 
valid_in : in std_logic; 
data_in : in std_logic_vector (7 downto 0); 
valid_out : out std_logic; 
data_out : out std_logic_vector (7 downto 0) 
); 
end entity ex6_v03; 


architecture behavior of ex6_v03 is 

signal st : integer := 0; 

begin 

process(a_rst, clk) 
begin 
-- asynchronous reset 
if (a_rst = '1') then 
data_out <= x"00"; 
valid_out <= '0'; 

-- synchronous reset   
elsif rising_edge(clk) then -- clk 
    if (s_rst = '1') then 
    valid_out <= '0'; 
    data_out <= x"00";  
    else        
    -- normal activity 
    if(valid_in = '1') then 
     -- main logic 
     if(data_in = x"00") then 
     st <= 1; 
     valid_out <= '1'; 
     data_out <= data_in; 
     elsif(st = 1 and data_in = x"01") then 
     st <= 2; 
     valid_out <= '1'; 
     data_out <= data_in; 
     elsif(st = 2 and data_in = x"02") then 
     st <= 3; 
     valid_out <= '1'; 
     data_out <= x"AA"; 
     elsif(st = 3 and data_in = x"03") then 
     valid_out <= '1'; 
     data_out <= data_in; 
     st <= 0; 
     else 
     st <= 0; 
     valid_out <= '1'; 
     data_out <= data_in; 
     end if; 

     -- end main logic 
    else 
     valid_out <= '0'; 
     data_out <= x"00"; 
    end if; 
    end if; 
end if; 
end process; 

end architecture behavior; 

但我的模塊不等待0x03和即時發送和0xAA。如何解決這個問題?

+0

沒有明確定義您要修復的內容。 –

+0

如何爲此添加緩衝區?當在輸入時遇到00,01,02,03替換爲00,01,AA,03。 –

+0

你的意思是,用AA代替O2,但只有當下一個狀態是03時?或者僅在序列01,02,03?或者在00,01,02,03的順序中?或者只有第一次出現02但沒有其他人,或者......在你提出明智的問題之前,你不可能得到明智的答案。 –

回答

0

您需要添加一個時鐘週期緩衝區,以便在選擇是發送02還是AA之前知道下一個輸入是否爲03。當然,這意味着輸出不會出現,直到輸入後2個週期,而不是隻有一個。請參閱修訂後的代碼:

--libraries 
library ieee; 
use ieee.std_logic_1164.all; 
use ieee.numeric_std.all; 
--entity 
entity ex6_v03 is 
port 
(
a_rst  : in std_logic; 
clk  : in std_logic; -- 200 MHz 
s_rst  : in std_logic; 
valid_in : in std_logic; 
data_in : in std_logic_vector (7 downto 0); 
valid_out : out std_logic; 
data_out : out std_logic_vector (7 downto 0) 
); 
end entity ex6_v03; 


architecture behavior of ex6_v03 is 

signal st : integer := 0; 
signal bvalid : std_logic := '0'; --is buffer valid? 
signal data_buffer : std_logic_vector (7 downto 0); --data from previous cycle 

begin 

process(a_rst, clk) 
begin 
if (a_rst = '1') then -- asynchronous reset 
data_out <= x"00"; 
valid_out <= '0'; 
bvalid <= '0'; 

elsif rising_edge(clk) then -- clk 
    if (s_rst = '1') then --sync reset 
    valid_out <= '0'; 
    bvalid <= '0'; 
    data_out <= x"00";  
    else -- normal activity 

    if(valid_in = '1') then --fill buffer 

     if(data_in = x"00") then 
     st <= 1; 
     data_out <= data_in; 
     elsif(st = 1 and data_in = x"01") then 
     st <= 2; 
     data_out <= data_in; 
     elsif(st = 2 and data_in = x"02") then 
     st <= 3; 
     else 
     st <= 0; 
     end if; 

     data_buffer <= data_in; 
     bvalid <= '1'; 
    else 
     bvalid <= '0'; 
    end if; 

    if(bvalid = '1') then --use buffer to populate output 
     valid_out <= '1' 
     if(st = 3 and data_in = x"03" and valid_in = '1') then --EDIT: make sure the x"03" sitting on the input is actually valid 
      data_out <= x"AA"; --output for the previous cycle (buffer contains x"02") 
     else 
      data_out <= data_buffer; 
     end if 
    else 
     valid_out <= '0'; 
     data_out <= x"00"; 
    end if; 

    end if; 
end if; 
end process; 

end architecture behavior;