/*	Copyright  (c)	Günter Woigk 2013 - 2013
					mailto:kio@little-bat.de

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

	Permission to use, copy, modify, distribute, and sell this software and
	its documentation for any purpose is hereby granted without fee, provided
	that the above copyright notice appear in all copies and that both that
	copyright notice and this permission notice appear in supporting
	documentation, and that the name of the copyright holder not be used
	in advertising or publicity pertaining to distribution of the software
	without specific, written prior permission.  The copyright holder makes no
	representations about the suitability of this software for any purpose.
	It is provided "as is" without express or implied warranty.

	THE COPYRIGHT HOLDER DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
	INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
	EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY SPECIAL, INDIRECT OR
	CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
	DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
	TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
	PERFORMANCE OF THIS SOFTWARE.
*/


#ifndef DEVICE_H
#define DEVICE_H


#include <QObject>
#include "kio/kio.h"


enum I2cState
{
	i2c_idle, i2c_busy, i2c_a0, i2c_a1, i2c_a2, i2c_rd, i2c_wr
};


class Device : public QObject
{
	Q_OBJECT

protected:
	class Cpu*	cpu;			// Parent

private:
	uint		i2c_dc;				// current bus lines state
	uint		i2c_address;
	uint		i2c_bits;
	uint8		i2c_byte;
	I2cState	i2c_state;			// eeprom transmit state: idle a0 a1 a2 rd wr
	uint8		i2c_buffer[64];		// write buffer
	uint		i2c_buffer_index;
	uint		i2c_busyuntil;


	uint16		select_mask;	// pos. Logik
	uint16		select_ff;		// pos. Logik
	uint16		irpt_ei_ff;		// pos. Logik
	uint16		irpt_pending;	// pos. Logik

	uint		eeprom_size;
	uint		eeprom_mask;
	uint8*		eeprom;

protected:

	void		set_irpt	(bool);
//	void		log			(cstr, ...);

public:

	explicit	Device		(QObject* parent, uint16 my_bit, cstr eeprom_filepath);
				~Device		();

virtual	uint16	rd_data		(uint16 addr)				= 0;
virtual	void	wr_data		(uint16 addr, uint16 data)	= 0;

virtual	void	wr_select	(uint16 pmask);
virtual	void	wr_irpt		(uint16 pmask);
virtual	uint16	rd_irpt		();

	uint16		rd_i2c		(uint32 mc2);
	void		load_eeprom	(cstr filename);

signals:
public slots:

};


#endif // DEVICE_H















