Facebook
From 1, 10 Months ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 118
  1. -- This is the 4 bits counter --
  2. library ieee;
  3. use ieee.std_logic_1164.all;
  4. use ieee.std_logic_unsigned.all;
  5.  
  6. entity s211311224bc is
  7. port(
  8.        clk : in std_logic;                      -- clock port
  9.        clr : in std_logic;                      -- clear port
  10.        load : in std_logic;
  11.         ud : in std_logic;                      -- up / down control port
  12.          d : in std_logic_vector (3 downto 0);
  13.          q : out std_logic_vector(3 downto 0)  -- output port of counting vale
  14.    
  15. );
  16. end s211311224bc;
  17.  
  18. architecture a of s211311224bc is
  19.  signal sq : std_logic_vector(3 downto 0);   -- counting signal object
  20.     signal sclr, intclr : std_logic;            -- clear signal object
  21.     signal sclk : std_logic;                    -- internal clock            
  22. begin
  23.      
  24.      process (intclr, clk, load, ud)               -- BCD up/down counter
  25.      begin
  26.           if intclr = '0' then                  -- internal reset signal
  27.              if ud = '1' then sq <= "0000";     -- initial value for up counter
  28.              elsif ud = '0' then sq <= "1001";  -- initial value for down counter
  29.              end if;
  30.              
  31.           elsif clk'event and clk = '0' then
  32.              if load = '0' then sq <= d;
  33.              else
  34.     if ud = '1' then sq <= sq + 1;    -- up counting
  35.     elsif ud = '0' then sq <= sq - 1; -- down counting
  36.     end if;        
  37.    end if;
  38.   end if;
  39.      end process;
  40.                  
  41.      sclr <= '0' when ud = '1' and sq = "1010" else    -- to judge sreset status for up / down counting status
  42.              '0' when ud = '0' and sq = "1111" else
  43.              '1';
  44.              
  45.      intclr <= sclr and clr;                        -- to get internal reset signal
  46.                      
  47.      q <= sq;                                   -- to get counting value  
  48. end a;