-- This is the 4 bits counter --
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
entity s211311224bc is
port(
clk : in std_logic; -- clock port
clr : in std_logic; -- clear port
load : in std_logic;
ud : in std_logic; -- up / down control port
d : in std_logic_vector (3 downto 0);
q : out std_logic_vector(3 downto 0) -- output port of counting vale
);
end s211311224bc;
architecture a of s211311224bc is
signal sq : std_logic_vector(3 downto 0); -- counting signal object
signal sclr, intclr : std_logic; -- clear signal object
signal sclk : std_logic; -- internal clock
begin
process (intclr, clk, load, ud) -- BCD up/down counter
begin
if intclr = '0' then -- internal reset signal
if ud = '1' then sq <= "0000"; -- initial value for up counter
elsif ud = '0' then sq <= "1001"; -- initial value for down counter
end if;
elsif clk'event and clk = '0' then
if load = '0' then sq <= d;
else
if ud = '1' then sq <= sq + 1; -- up counting
elsif ud = '0' then sq <= sq - 1; -- down counting
end if;
end if;
end if;
end process;
sclr <= '0' when ud = '1' and sq = "1010" else -- to judge sreset status for up / down counting status
'0' when ud = '0' and sq = "1111" else
'1';
intclr <= sclr and clr; -- to get internal reset signal
q <= sq; -- to get counting value
end a;
{"html5":"htmlmixed","css":"css","javascript":"javascript","php":"php","python":"python","ruby":"ruby","lua":"text\/x-lua","bash":"text\/x-sh","go":"go","c":"text\/x-csrc","cpp":"text\/x-c++src","diff":"diff","latex":"stex","sql":"sql","xml":"xml","apl":"apl","asterisk":"asterisk","c_loadrunner":"text\/x-csrc","c_mac":"text\/x-csrc","coffeescript":"text\/x-coffeescript","csharp":"text\/x-csharp","d":"d","ecmascript":"javascript","erlang":"erlang","groovy":"text\/x-groovy","haskell":"text\/x-haskell","haxe":"text\/x-haxe","html4strict":"htmlmixed","java":"text\/x-java","java5":"text\/x-java","jquery":"javascript","mirc":"mirc","mysql":"sql","ocaml":"text\/x-ocaml","pascal":"text\/x-pascal","perl":"perl","perl6":"perl","plsql":"sql","properties":"text\/x-properties","q":"text\/x-q","scala":"scala","scheme":"text\/x-scheme","tcl":"text\/x-tcl","vb":"text\/x-vb","verilog":"text\/x-verilog","yaml":"text\/x-yaml","z80":"text\/x-z80"}