----------------------------------------------------------------------------------
-- Company: 
-- Engineer: 
-- 
-- Create Date: 28.11.2021 13:47:13
-- Design Name: 
-- Module Name: count_4BCDdigs - count_4BCDdigs
-- Project Name: 
-- Target Devices: 
-- Tool Versions: 
-- Description: 
-- 
-- Dependencies: 
-- 
-- Revision:
-- Revision 0.01 - File Created
-- Additional Comments:
-- 
----------------------------------------------------------------------------------
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;

entity count_4BCDdigs is
    Port ( clock : in STD_LOGIC;
           reset : in STD_LOGIC;
           addressCPU : in STD_LOGIC_VECTOR (31 downto 0);
           ce : in STD_LOGIC;
           rw : in std_logic;
           data_outCPU : in STD_LOGIC_VECTOR (31 downto 0);
           hundreds : out STD_LOGIC_VECTOR (3 downto 0);
           tens : out STD_LOGIC_VECTOR (3 downto 0);
           units_s : out STD_LOGIC_VECTOR (3 downto 0);
           tenths : out STD_LOGIC_VECTOR (3 downto 0)
           );
end count_4BCDdigs;

architecture count_4BCDdigs of count_4BCDdigs is
signal  ce_tenths, ce_units, ce_tens, ce_hundreds : std_logic; 
signal  ces : std_logic_vector (3 downto 0);
begin

    ce_tenths <='1' when (ce='1' and rw='0' and addressCPU=x"10008000") else '0';
    ce_units <='1' when (ce='1' and rw='0' and addressCPU=x"10008001") else '0';
    ce_tens <='1' when (ce='1' and rw='0' and addressCPU=x"10008002") else '0';
    ce_hundreds <='1' when (ce='1' and rw='0' and addressCPU=x"10008003") else '0';

    ces <= ce_hundreds & ce_tens & ce_units & ce_tenths;

    process (clock, reset)
    begin
        if reset='1' then
            tenths <= "0000"; units_s <= "0000"; tens <= "0000"; hundreds <= "0000";
        elsif clock'event and clock='1' then
            case ces is
                 when "0001" =>
                       tenths <= data_outCPU(3 downto 0);
                 when "0010" =>
                       units_s <= data_outCPU(3 downto 0);
                 when "0100" =>
                       tens <= data_outCPU(3 downto 0);
                 when "1000" =>
                       hundreds <= data_outCPU(3 downto 0);
                 when others => null;
            end case;
        end if;
    end process;
    
end count_4BCDdigs;
