library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;

entity DspDr is
  port(clk:       in std_logic;
       bcdint:    in std_logic_vector(11 downto 0);
       dp3,dp2,dp1,dp0: in std_logic;
       digit:     out std_logic_vector(3 downto 0);
		 seg:       out std_logic_vector(7 downto 0));
end DspDr;

architecture Behavioral of DspDr is 
  signal cd:   std_logic_vector(1 downto 0);
  signal curr: std_logic_vector(3 downto 0);
  signal dp:   std_logic;
  SIGNAL mhertz_count: std_logic;
  SIGNAL khertz_count: std_logic_vector(9 downto 0);
  signal mhertz_en,khertz_en: std_logic;
begin

-- generates a 900 kHz signal from a 1.8 Mhz signal - mhertz_en
 process (clk) begin -- description follows timing module for XC3S
  if clk'event and clk = '1' then
   mhertz_count <= NOT mhertz_count;
   if mhertz_count = '1' then
     mhertz_en <= '1' ;
   else
     mhertz_en <= '0' ;
   end if ;
  end if ;
 end process ;

-- generates a 1 kHz signal from a 1Mhz signal - khertz_en
 process (clk) begin
  if clk'event and clk = '1' then
   if mhertz_en = '1' then
     khertz_count <= khertz_count + 1 ;
     if khertz_count = "1111101000" then
       khertz_en <= '1' ;
       khertz_count <= (others => '0') ;
     else
       khertz_en <= '0' ;
     end if ;
   else
     khertz_en <= '0' ;
   end if ;
  end if ;
 end process ;

process (clk) begin
  if clk'event and clk = '1' then
   if khertz_en = '1' then
     cd <= cd + 1;
   end if;
   case cd(1 downto 0) is -- curr je soucasne zobrazena cifra, digit je jeji pozice na displeji
--     when "00" =>   curr <= bcdint(3 downto 0);   digit <= "1110"; dp <= dp0;
     when "00" =>   curr <="1011";   digit <= "1110"; dp <= dp0;
     when "01" =>   curr <= bcdint(3 downto 0);   digit <= "1101"; dp <= dp1;
     when "10" =>   curr <= bcdint(7 downto 4);  digit <= "1011"; dp <= dp2;
	  when others => curr <= bcdint(11 downto 8); digit <= "0111"; dp <= dp3;
   end case ;
     case curr is 
      when "0000" => seg <= "0000001" & dp; -- 0
      when "0001" => seg <= "1001111" & dp; -- 1
      when "0010" => seg <= "0010010" & dp; -- 2
      when "0011" => seg <= "0000110" & dp; -- 3
      when "0100" => seg <= "1001100" & dp; -- 4
      when "0101" => seg <= "0100100" & dp; -- 5
      when "0110" => seg <= "0100000" & dp; -- 6
      when "0111" => seg <= "0001111" & dp; -- 7
      when "1000" => seg <= "0000000" & dp; -- 8
      when "1001" => seg <= "0000100" & dp; -- 9
--      when "1010" => seg <= "0001000" & dp; -- A
--      when "1011" => seg <= "1100000" & dp; -- b
--      when "1100" => seg <= "0110001" & dp; -- C
--      when "1101" => seg <= "1000010" & dp; -- d
--      when "1110" => seg <= "0110000" & dp; -- E
--      when others => seg <= "0111000" & dp; -- F
		when others => seg <= "0011100" & dp; -- °
     end case;
   end if;
  end process;
 end Behavioral;

