library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
entity contador is
generic (
n_bits: integer := 3);
port (
clk : in std_logic;
rst : in std_logic;
Q : inout std_logic_vector(n_bits downto 0));
end contador;
architecture synth of contador is
begin
process (clk) begin
if(rising_edge(clk)) then
if(rst='1') then
Q <= "0000";
else
Q <= Q + 1;
end if;
end if;
end process;
end synth;library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
entity counter is
generic (
N_BITS: integer := 3
);
port (
clk : in std_logic;
rst : in std_logic;
Q : inout std_logic_vector((N_BITS-1) downto 0)
);
end counter;
architecture synth of counter is
begin
process (clk) begin
if(rising_edge(clk)) then
if(rst='1') then
Q <= (others => '0');
else
Q <= std_logic_vector(unsigned(Q) + 1);
end if;
end if;
end process;
end synth;library ieee;
use ieee.std_logic_1164.all;
entity counter_tb is
end entity counter_tb;
architecture test of counter_tb is
constant COUNTER_BITS : integer := 3;
-- instance of the counter
component counter
generic (
N_BITS: integer := COUNTER_BITS
);
port (
clk : in std_logic;
rst : in std_logic;
Q : inout std_logic_vector((N_BITS-1) downto 0)
);
end component;
signal clk : std_logic := '0';
signal rst : std_logic := '0';
signal Q : std_logic_vector((COUNTER_BITS-1) downto 0) := (others => '0');
begin
-- signal => component port
counter_uut: counter port map(clk => clk, rst => rst, Q => Q);
-- generate the clock
clock: process begin
clk <= '1';
wait for 1 ns;
clk <= '0';
wait for 1 ns;
end process clock;
process begin
rst <= '1';
wait for 1 ns;
rst <= '0';
wait for 20 ns;
rst <= '1';
wait for 1 ns;
rst <= '0';
assert false report "Reached end of test";
wait;
end process;
end test;
--! @file
--! @brief n bits counter
--! the counts wraps to 0 when all the bits
--! on the vector are equal to logic 1.
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
entity counter is
generic (
N_BITS: integer := 3
);
port (
clk : in std_logic;
rst : in std_logic;
Q : out std_logic_vector((N_BITS-1) downto 0)
);
end counter;
architecture synth of counter is
--! internal counter
signal Q_in : unsigned((N_BITS-1) downto 0);
begin
assert (N_BITS > 0)
report "N_BITS should be bigger than 0"
severity failure;
-- Set the count to 0 when the rst input is 1,
-- otherwise increment the count by one.
process (clk) begin
if(rising_edge(clk)) then
if(rst='1') then
Q_in <= (others => '0');
else
Q_in <= Q_in + 1;
end if;
end if;
end process;
-- Assign the current count to the Q output
process (clk) begin
if(rising_edge(clk)) then
Q <= std_logic_vector(unsigned(Q_in));