-- accelerometer.vhd

-- This file was auto-generated as a prototype implementation of a module
-- created in component editor.  It ties off all outputs to ground and
-- ignores all inputs.  It needs to be edited to make it do something
-- useful.
-- 
-- This file will not be automatically regenerated.  You should check it in
-- to your version control system if you want to keep it.

library IEEE;
use IEEE.std_logic_1164.all;
--use IEEE.numeric_std.all;
use IEEE.std_logic_arith.all;
use IEEE.std_logic_unsigned.all;

entity accelerometer is
	port (
		sda_padoen_o		: in    std_logic;
		scl_i2c_to_accel        : out   std_logic                     := '0';                     --  conduit_end.export
		sda_i2c_to_accel        : inout std_logic         	      := '1';	          --             .export
		scl_i2c_to_bus          : in   std_logic                      := '0';      	          --  conduit_end.export
		sda_i2c_to_bus          : inout std_logic		      := '1';            	  --             .export
		interrupt2              : in    std_logic                     := '0';             --             .export
		clk                     : in    std_logic                     := '0';             --        clock.clk
		reset_n                 : in    std_logic                     := '0';             --        reset.reset_n
		avalon_slave_address    : in    std_logic_vector(2 downto 0)  := (others => '0'); -- avalon_slave.address
		avalon_slave_readdata   : out   std_logic_vector(31 downto 0);                    --             .readdata
		avalon_slave_writedata  : in    std_logic_vector(31 downto 0) := (others => '0'); --             .writedata
		avalon_slave_write_n    : in    std_logic                     := '0';             --             .write
		avalon_slave_chipselect : in    std_logic                     := '0';             --             .chipselect
		avalon_slave_byteenable : in    std_logic_vector(3 downto 0)  := (others => '0'); --             .byteenable
		avalon_slave_read       : in    std_logic                     := '0'              --             .read
	);
end entity accelerometer;

architecture rtl of accelerometer is
	signal data_out :  std_logic_vector (31 DOWNTO 0);

        signal sda_to_master:  std_logic;
	signal sda_from_master:  std_logic;

begin
	
scl_i2c_to_accel <= scl_i2c_to_bus;

PROCESS (scl_i2c_to_bus,clk)
   BEGIN 

	IF( sda_padoen_o = '0') THEN
		IF( sda_i2c_to_bus = '1') THEN
	 		sda_i2c_to_accel <= '1';
		END IF;
		IF( sda_i2c_to_bus = '0') THEN
			 sda_i2c_to_accel <= '0';
		END IF;
		sda_i2c_to_bus <= 'Z';
	END IF;

	IF( sda_padoen_o = '1') THEN
		IF( sda_i2c_to_accel = '1') THEN
	 		sda_i2c_to_bus <= '1';
		END IF;
		IF( sda_i2c_to_accel = '0') THEN
			 sda_i2c_to_bus <= '0';
		END IF;
		sda_i2c_to_accel <= 'Z';
	END IF;
	 

END PROCESS;

--PROCESS (sda_padoen_o, sda_i2c_to_accel)
   --BEGIN 
      --IF( sda_padoen_o = '1') THEN 
         --sda_i2c_to_accel <= 'Z'; 
    --  ELSE 
   --      sda_i2c_to_accel <= sda_from_master; 
  --    END IF; 
   --   sda_to_master <= sda_i2c_to_accel;
 
--END PROCESS;
	
	--sda_i2c_to_bus <= sda_i2c_to_accel when (sda_padoen_o /= '0')else 'Z';

	--sda_i2c_to_accel <= sda_i2c_to_bus when (sda_padoen_o = '0')else 'Z';	

	--sda_i2c_to_bus <= '1' when((sda_padoen_o = '1') and (sda_i2c_to_accel = '1') and (scl_i2c_to_bus = '1'))else 'Z';

	--sda_i2c_to_accel <= sda_i2c_to_bus when (sda_padoen_o /= '1')else 'Z';
		

process (clk,interrupt2,avalon_slave_write_n)
	begin
		--If motion detected, increment step
		if(rising_edge(interrupt2))then
     			data_out <= data_out + '1';
      		end if;

		--Gate timer went off (every 1s)
		if avalon_slave_read = '1' then
			avalon_slave_readdata <= data_out;
		end if;
		--resets pace count
		if avalon_slave_write_n = '0' then
      			data_out <= std_logic_vector'("00000000000000000000000000000000");
		end if;

 	end process;

end architecture rtl; -- of accelerometer
