Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
11 years ago

hi! Understanding a coding (counter), help

entity counter is

generic (

n : natural := 4;

);

port (

clock : in std_logic;

reset_n : in std_logic;

q : out std_logic_vector(n-1 downto 0)

);

end entity;

architecture rtl of counter is

signal value : std_logic_vector(n-1 downto 0);

begin

process(clock, reset_n)

begin

if (reset_n = ’0’) then

value <= (others => ’0’);

elsif ((clock’event) and (clock = ’1’)) then

value <= value + 1;

end if;

end process;

q <= value;

end rtl;

16 Replies

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    It sounds like a homework assignment. It looks like code to me - why not try writing a testbench and simulating it?

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Apparently you want to create a hierarchical design by instantiating the component counter in a top entity. But you don't write a separate entity, instead a second architecture inside the entity counter. This doesn't work.

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Hi, On the 2nd part your are *completely* wrong : you can't use the same entity in the description of THIS entity, It is equivalent to saying "A car is made with a car",

    I recommand port mapping by names, not by position, some thing like this

    
    my_counter : entity counter
       generic map ( n => 8, k => 4)
       port map ( 
          clock => KEY(1),
          reset_n => KEY(0),
          Q => Q_int,
          SW => ???,
          KEY => ???,
          LEDR => ???
       );
    LEDR <= Q_int;
    

    I think that you only have to modify the "rtl" architecture, that what you have done.
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    HI!! help please...Why it gives me this error? I want to put a function of k n

    library ieee;

    use ieee.std_logic_1164.all;

    use ieee.std_logic_arith.all;

    use ieee.std_logic_signed.all;

    use ieee.numeric_std.all;

    entity contador is

    generic (

    k : natural:= 8;

    n : natural:= LOG2(k+1)--> error (10482): vhdl error at contador.vhd(12): object "log2" is used but not declared

    );

    port

    (

    clock: in std_logic;

    reset_n: in std_logic;

    Q : out std_logic_vector (n-1 downto 0)

    );

    end contador;

    architecture rtl of contador is

    signal clock,reset_n: std_logic;

    signal Q_int: std_logic_vector(n-1 downto 0);

    begin

    PROCESS(clock, reset_n)

    begin

    if (reset_n = '0') then

    Q_int<= (others => '0');

    elsif ((clock'event) and (clock = '1')) then

    if (Q_int<k) then

    Q_int <= Q_int + 1;

    else

    Q_int<= (others => '0');

    end if;

    end if;

    end process;

    Q <= Q_int;

    end rtl;
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    You have no log2 function included - you will need to write one and add it to a package. You will also struggle because you cannot use other generics inside the generic region

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    moreover the generic 'k' is not used here. So you can delete it.

    (and give to 'n' the number of bits.)