gbounce
Junior Member level 3
When creating a counter, or just any old if statement, does the either version of the code use less resources? Is comparing against 0 a special case, or does it not matter?
Compare against 0:
Compare against 7:
Compare against 0:
Code:
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
use ieee.std_logic_unsigned.all;
entity counter is
port (
clk : in std_logic;
rst_n : in std_logic
);
end counter;
architecture rtl of counter is
signal cnt : integer range 0 to 7;
begin
-----------------------------------------------------------------------------
-- count process
-----------------------------------------------------------------------------
process (clk, rst_n)
begin
if (rst_n = '0') then
cnt <= 7;
elsif rising_edge(clk) then
-------------------------------------------------------------------------
-- compare against 0
-------------------------------------------------------------------------
if (cnt /= 0) then
cnt <= cnt - 1;
-------------------------------------------------------------------------
-- rollover at 0 to 7
-------------------------------------------------------------------------
else
cnt <= 7;
end if;
end if;
end process;
end rtl;
Compare against 7:
Code:
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
use ieee.std_logic_unsigned.all;
entity counter is
port (
clk : in std_logic;
rst_n : in std_logic
);
end counter;
architecture rtl of counter is
signal cnt : integer range 0 to 7;
begin
-----------------------------------------------------------------------------
-- count process
-----------------------------------------------------------------------------
process (clk, rst_n)
begin
if (rst_n = '0') then
cnt <= 0;
elsif rising_edge(clk) then
-------------------------------------------------------------------------
-- compare against 7
-------------------------------------------------------------------------
if (cnt /= 7) then
cnt <= cnt + 1;
-------------------------------------------------------------------------
-- rollover at 7 to 0
-------------------------------------------------------------------------
else
cnt <= 0;
end if;
end if;
end process;
end rtl;