-- -----------------------------------------------------------------------------
-- Copyright (c) 2013 Benjamin Krill <benjamin@krll.de>
--
-- Permission is hereby granted, free of charge, to any person obtaining a copy
-- of this software and associated documentation files (the "Software"), to deal
-- in the Software without restriction, including without limitation the rights
-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-- copies of the Software, and to permit persons to whom the Software is
-- furnished to do so, subject to the following conditions:
--
-- The above copyright notice and this permission notice shall be included in
-- all copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-- THE SOFTWARE.
-- -----------------------------------------------------------------------------
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
use std.textio.all;
use ieee.std_logic_textio.all;

entity i2c_rom is
	port (
		clk    : in    std_logic;
		rst_n  : in    std_logic;
		sda_i  : in    std_logic;
		sda_o  : out   std_logic;
		scl_i  : in    std_logic
	);
end i2c_rom;

architecture i2c_rom of i2c_rom is
	--type rom_t is array (0 to 255) of std_logic_vector (7 downto 0);
	type rom_t is array (0 to 127) of std_logic_vector (7 downto 0);
	impure function init_rom_from_file(filename : in string) return rom_t is
	file romfile   : text is in filename;
	variable rline : line;
	variable rom   : rom_t;
	begin
		for i in rom_t'range loop
			readline(romfile, rline);
			hread(rline, rom(i));
		end loop;
		return rom;
	end function;
	--signal rom : rom_t := init_rom_from_file("hdmi_in_syncmaster940t.dat");
	signal rom : rom_t := init_rom_from_file("hdmi_in_syncmaster191t.dat");

	signal rd_data         : std_logic_vector(7 downto 0);
	signal wr_data         : std_logic_vector(7 downto 0);
	signal address         : std_logic_vector(7 downto 0);
	signal wr_pulse        : std_logic;
	signal rd_pulse        : std_logic;
	signal rd_wr           : std_logic;
begin
	rd_data <= rom(to_integer(unsigned(address)));

	i2c_blk_0: entity work.i2c_slave
	generic map ( ADDRESS => "0101000" ) -- 6A write, 6B read
	port map (
		clk             => clk,
		rst_n           => rst_n,
		sda_i           => sda_i,
		sda_o           => sda_o,
		scl_i           => scl_i,
		rd_dat_i        => rd_data,
		addr_o          => address,
		wr_dat_o        => wr_data,
		wr_pulse_o      => wr_pulse,
		rd_pulse_o      => rd_pulse,
		rd_wr_o         => rd_wr
	);

end i2c_rom;
