-- See the file "LICENSE" for the full license governing this code. --

library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
use work.lt16x32_global.all;
use work.wishbone.all;
use work.config.all;

entity wb_segment is
	generic(
		memaddr		:	generic_addr_type; --:= CFG_BADR_SEG;
		addrmask	:	generic_mask_type --:= CFG_MADR_SEG;
	);
	port(
		clk		: in  std_logic;
		rst		: in  std_logic;
		wslvi	:	in	wb_slv_in_type;
		wslvo	:	out	wb_slv_out_type;

        anodes : out std_logic_vector(7 downto 0);
        cathodes : out std_logic_vector(7 downto 0)
	);
end wb_segment;

architecture Behavioral of wb_segment is

    signal ack : std_logic;

	signal hex_register : std_logic_vector(63 downto 0);
    signal data_out : std_logic_vector(63 downto 0);

    signal hex : std_logic_vector(4 downto 0);

    signal timer_overflow : std_logic;
    signal overflow_counter : integer range 0 to 7;

    component hex2physical
    port(
        hex : in std_logic_vector(4 downto 0);
        cathodes : out std_logic_vector(7 downto 0)
    );
    end component;

    component simple_timer
    generic(
        timer_start : std_logic_vector (31 downto 0)
    );
    port(
        clk : in std_logic;
        rst : in std_logic;
        timer_overflow : out std_logic
    );
    end component;

begin

    converter : hex2physical
    port map(
        hex => hex,
        cathodes => cathodes
    );

    timer: simple_timer
    generic map (timer_start => x"00000008")
    port map(
        clk => clk,
        rst => rst,
        timer_overflow => timer_overflow
    );
    
	process(clk)
	begin
		if clk'event and clk='1' then
			if rst = '1' then
				ack	<= '0';
				data_out <= (others=>'0');
				hex_register <= (others=>'0');
			else
                data_out	<= (others=>'0');

				if wslvi.stb = '1' and wslvi.cyc = '1' then
				    if wslvi.we='0' then
                        data_out <= hex_register;
                    else
                        -- Write enable
                        if wslvi.adr(2) = '0' then
                            hex_register(31 downto 0) <= dec_wb_dat(wslvi.sel,wslvi.dat);
                        else -- wslvi.adr(2) = '1'
                            hex_register(63 downto 32) <= dec_wb_dat(wslvi.sel,wslvi.dat);
                        end if;
                    end if;
				
					if ack = '0' then
						ack	<= '1';
					else
						ack	<= '0';
					end if;
				else
					ack <= '0';
				end if;
			end if;
		end if;
	end process;

    process(clk)
    begin
        if clk'event and clk='1' then
            if rst = '1' then
                hex <= hex_register(4 downto 0);
                anodes <= (others => '0');
                overflow_counter <= 0;
            else
                if timer_overflow = '1' then
                    if overflow_counter = 7 then
                        overflow_counter <= 0;
                    else
                        overflow_counter <= overflow_counter + 1;
                    end if;

                    anodes <= (others => '0');
                    anodes(overflow_counter) <= '1';
                    
                    hex <= hex_register(overflow_counter * 8 + 4 downto overflow_counter * 8);
                end if;
            end if;
        end if;
    end process;

	wslvo.dat <=
        data_out(31 downto 0) when wslvi.adr(2) = '0' else
        data_out(63 downto 32) when wslvi.adr(2) = '1';

	wslvo.ack	<= ack;
	wslvo.wbcfg	<= wb_membar(memaddr, addrmask);

end Behavioral;
