Altera_Forum
Honored Contributor
11 years agoTrying to get State Machine to Stop Glitching
Currently I am developing a sequencer that every 16 clock ticks one of the tx enables are high while the rx enables are kept high all the time. My state machine is working occasionally although I have sudden glitches in it which causes for my code to go off course. I have attached the signal scope of this to the forum post. Does anyone have an idea of what could have caused this?
http://www.alteraforum.com/forum/attachment.php?attachmentid=11082&stc=1 code LIBRARY IEEE; USE IEEE.std_logic_1164.ALL; USE IEEE.std_logic_textio.ALL; USE IEEE.numeric_bit.ALL; USE IEEE.numeric_std.ALL; LIBRARY STD; USE STD.textio; ENTITY TOP IS GENERIC (txNum : INTEGER := 5); PORT ( clkrx : IN std_logic; txen_1 : OUT std_logic; txen_2 : OUT std_logic; txen_3 : OUT std_logic; txen_4 : OUT std_logic; txen_5 : OUT std_logic; rxen_1 : OUT std_logic; rxen_2 : OUT std_logic; rxen_3 : OUT std_logic; rxen_4 : OUT std_logic; rxen_5 : OUT std_logic; clkout : OUT std_logic ); END TOP; ARCHITECTURE behavioral OF TOP IS CONSTANT s0 : STD_LOGIC_vector(2 DOWNTO 0) := "000"; CONSTANT s1 : STD_LOGIC_vector(2 DOWNTO 0) := "001"; CONSTANT s2 : STD_LOGIC_vector(2 DOWNTO 0) := "010"; CONSTANT s3 : STD_LOGIC_vector(2 DOWNTO 0) := "011"; CONSTANT s4 : STD_LOGIC_vector(2 DOWNTO 0) := "100"; CONSTANT s5 : STD_LOGIC_vector(2 DOWNTO 0) := "101"; SIGNAL current_s : STD_LOGIC_vector(2 DOWNTO 0) := s5; --current and next state declaration. SIGNAL next_s : STD_LOGIC_vector(2 DOWNTO 0) := s0; SIGNAL cnt : INTEGER RANGE 0 TO 16 := 0; SIGNAL p : BOOLEAN := false; BEGIN PROCESS (clkrx) BEGIN IF (rising_edge(clkrx)) THEN IF (cnt = 0) THEN current_s <= next_s; cnt <= cnt + 1; p <= true; ELSIF (cnt = 16) THEN current_s <= next_s; cnt <= cnt + 2; ELSE cnt <= cnt + 1; END IF; END IF; END PROCESS; PROCESS (clkrx, current_s, p) BEGIN IF (p) THEN clkout <= clkrx; END IF; END PROCESS; --state machine process. PROCESS (current_s) BEGIN CASE current_s IS WHEN s0 => --when current state is "s0" rxen_1 <= '1'; rxen_2 <= '1'; rxen_3 <= '1'; rxen_4 <= '1'; rxen_5 <= '1'; txen_1 <= '1'; txen_2 <= '0'; txen_3 <= '0'; txen_4 <= '0'; txen_5 <= '0'; IF (txNum = 1) THEN next_s <= s0; ELSE next_s <= s1; END IF; WHEN s1 => --when current state is "s1" rxen_1 <= '1'; rxen_2 <= '1'; rxen_3 <= '1'; txen_1 <= '0'; txen_2 <= '1'; txen_3 <= '0'; txen_4 <= '0'; txen_5 <= '0'; IF (txNum = 2) THEN next_s <= s0; ELSE next_s <= s2; END IF; WHEN s2 => --when current state is "s2" rxen_1 <= '1'; rxen_2 <= '1'; rxen_3 <= '1'; rxen_4 <= '1'; rxen_5 <= '1'; txen_1 <= '0'; txen_2 <= '0'; txen_3 <= '1'; txen_4 <= '0'; txen_5 <= '0'; IF (txNum = 3) THEN next_s <= s0; ELSE next_s <= s3; END IF; WHEN s3 => --when current state is "s3" rxen_1 <= '1'; rxen_2 <= '1'; rxen_3 <= '1'; rxen_4 <= '1'; rxen_5 <= '1'; txen_1 <= '0'; txen_2 <= '0'; txen_3 <= '0'; txen_4 <= '1'; txen_5 <= '0'; IF (txNum = 4) THEN next_s <= s0; ELSE next_s <= s4; END IF; WHEN s4 => rxen_1 <= '1'; rxen_2 <= '1'; rxen_3 <= '1'; rxen_4 <= '1'; rxen_5 <= '1'; txen_1 <= '0'; txen_2 <= '0'; txen_3 <= '0'; txen_4 <= '0'; txen_5 <= '1'; next_s <= s0; WHEN OTHERS => rxen_1 <= '1'; rxen_2 <= '1'; rxen_3 <= '1'; rxen_4 <= '1'; rxen_5 <= '1'; txen_1 <= '1'; txen_2 <= '0'; txen_3 <= '0'; txen_4 <= '0'; txen_5 <= '0'; next_s <= s0; END CASE; END PROCESS; END behavioral;