Problem with encoder

Discussion in 'VHDL' started by xcellx, Oct 2, 2008.

  1. xcellx

    xcellx

    Joined:
    Oct 2, 2008
    Messages:
    3
    I'm trying to describe an encoder in behavioral style. This is my vhdl code:

    Code:
    entity encoder is
    	generic(N: integer:=3);
    	port(i1:  in bit_vector(2**N-1 downto 0);
    		 o1: out bit_vector(N-1 downto 0));
    end encoder;
    
    architecture behavioral of encoder is
    begin
    	process(i1)
    		variable k: integer;
    	begin
    		k:=0;
    		while k<2**N and i1(k)='0' loop
    			k:=k+1;
    		end loop;
    		if k=2**N then
    			o1 <= (others => '0') after 5 ns;
    		else
    			for i in 0 downto N-1 loop
    				if (k mod 2)=1 then
    					o1(i) <= '1' after 5 ns;
    				else
    					o1(i) <= '0' after 5 ns;
    				end if;
    				k:=k/2;
    			end loop;
    		end if;
    	end process;
    end behavioral;

    This is my testbench:


    Code:
    entity encoder_tb is
    		generic(
    		N : INTEGER := 3 );
    end encoder_tb;
    
    architecture TB_ARCHITECTURE of encoder_tb is
    	component encoder
    		generic(
    		N : INTEGER := 3 );
    	port(
    		i1 : in BIT_VECTOR(2**N-1 downto 0);
    		o1 : out BIT_VECTOR(N-1 downto 0) );
    	end component;
    
    
    	signal i1 : BIT_VECTOR(2**N-1 downto 0);
    	signal o1 : BIT_VECTOR(N-1 downto 0);
    
    
    begin
    
    	UUT : encoder
    		generic map (
    			N => N
    		)
    
    		port map (
    			i1 => i1,
    			o1 => o1
    		);
    
    	i1 <= "00000000",
    		  "10000000" after 100 ns,
    		  "01000000" after 200 ns,
    		  "00100000" after 300 ns,
    		  "00010000" after 400 ns,
    		  "00001000" after 500 ns,
    		  "00000100" after 600 ns,
    		  "00000010" after 700 ns,
    		  "00000001" after 800 ns,
    		  "10100100" after 900 ns,
    		  "00001000" after 1000 ns,
    		  "00100111" after 1100 ns,
    		  "11111111" after 1200 ns;
    
    end TB_ARCHITECTURE;
    
    configuration TESTBENCH_FOR_encoder of encoder_tb is
    	for TB_ARCHITECTURE
    		for UUT : encoder
    			use entity work.encoder(behavioral);
    		end for;
    	end for;
    end TESTBENCH_FOR_encoder;

    When I simulate the output is always "000". Debugging my code I saw that when the condition in the "if" is false, the "for" in the "else" doesn't execute, but I don't understand the reason!
    Why doesn't it work? :(

    :banghead: :banghead: :banghead:
    Last edited: Oct 2, 2008
    xcellx, Oct 2, 2008
    #1
    1. Advertising

  2. xcellx

    xcellx

    Joined:
    Oct 2, 2008
    Messages:
    3
    Nobody can help me?
    xcellx, Oct 7, 2008
    #2
    1. Advertising

  3. xcellx

    jeppe

    Joined:
    Mar 10, 2008
    Messages:
    348
    Location:
    Denmark
    I believe the DOWNTO should be TO

    Code:
    entity encoder is
    	generic(N: integer:=3);
    	port(i1:  in bit_vector(2**N-1 downto 0);
    		 o1: out bit_vector(N-1 downto 0));
    end encoder;
    
    architecture behavioral of encoder is
    begin
    	process(i1)
    		variable k: integer;
    	begin
    		k:=0;
    		while k<2**N and i1(k)='0' loop
    			k:=k+1;
    		end loop;
    		if k=2**N then
    			o1 <= (others => '0') after 5 ns;
    		else
    			for i in 0 [COLOR="Red"]TO[/COLOR] N-1 loop
    				if (k mod 2)=1 then
    					o1(i) <= '1' after 5 ns;
    				else
    					o1(i) <= '0' after 5 ns;
    				end if;
    				k:=k/2;
    			end loop;
    		end if;
    	end process;
    end behavioral;
    jeppe, Oct 7, 2008
    #3
  4. xcellx

    xcellx

    Joined:
    Oct 2, 2008
    Messages:
    3
    Aaaaaaaaaaaaaaahhhhhhhhhhhhhhhhhh..........
    :oops: :oops: :oops: :oops: :oops: :oops: :oops: :oops: :oops: :oops:

    :banghead: :banghead: :banghead: :banghead: :banghead: :banghead:


    Thank you very much............ you are right! Sorry for the stupid mistake...... :damnmate:

    Now I'm happy because i don't think yet my simulator is stupid but only I. :mrgreen:
    Last edited: Oct 7, 2008
    xcellx, Oct 7, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ronny Hengst

    manchester encoder

    Ronny Hengst, Aug 7, 2003, in forum: VHDL
    Replies:
    4
    Views:
    6,753
    Jonathan Bromley
    Aug 8, 2003
  2. Gietek

    N_bit decoder/encoder

    Gietek, Dec 3, 2003, in forum: VHDL
    Replies:
    4
    Views:
    630
    Charles M. Elias
    Dec 4, 2003
  3. Fred Bartoli

    Pipelined binary encoder

    Fred Bartoli, Nov 7, 2004, in forum: VHDL
    Replies:
    1
    Views:
    711
    Jonathan Bromley
    Nov 10, 2004
  4. jpereira

    Bit stuffing in a Crc encoder

    jpereira, May 25, 2005, in forum: VHDL
    Replies:
    4
    Views:
    1,407
    Mike Treseler
    May 31, 2005
  5. samith

    Encoder counter problem

    samith, Aug 20, 2009, in forum: VHDL
    Replies:
    1
    Views:
    1,054
    jeppe
    Aug 22, 2009
Loading...

Share This Page