/*	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.


	this class implements some functionality of the 88192 dual UART
*/


#ifndef SIO_H
#define SIO_H

#include <QObject>
#include "Device.h"
#include "unix/pthreads.h"
#include "MainWindow.h"

#define VOL	volatile


struct SioIoBuffer
{
	VOL double	tpc;			// time per character [s]

	uint8		xbu[256];		// external buffer
	VOL uint	xrp;
	VOL uint	xwp;

	uint8		ibu[16];		// internal 16-byte queue
	VOL uint	irp;
	VOL uint	iwp;

	VOL bool	hsk;
	VOL	bool	enabled;

				SioIoBuffer()	:tpc(10.0/9600),xrp(0),xwp(0),irp(0),iwp(0),hsk(on),enabled(no){}
	uint		xavail()		{ return xwp-xrp; }
	uint		xfree()			{ return 256-xavail(); }
	uint		iavail()		{ return iwp-irp; }
	uint		ifree()			{ return 16-iavail(); }
	bool		ifull()			{ return iavail()==16; }
	bool		iempty()		{ return iavail()==0; }

	uint8		ird()			{ return ibu[(irp++)&15]; }
	void		iwr(uint8 c)	{ ibu[(iwp++)&15] = c; }

	uint8		xrd()			{ return xbu[(xrp++)&255]; }
	void		xwr(uint8 c)	{ xbu[(xwp++)&255] = c; }

	char		s[17];
	cptr		ibustr()		{ uint a=irp&15, e=iwp&15; if(a>e) { memcpy(s,ibu+a,16-a); memcpy(s+16-a,ibu,e); } else { memcpy(s,ibu+a,e-a); }
								  e = (e-a)%15; for(a=0;a<e;a++) if((uint8)s[a]<(uint8)' ') { s[a]='_'; } s[e]=0; return s; }
};


class Sio : public Device
{
	Q_OBJECT
	friend class SioInspector;

	PLock		mutex;
	void		lock(){mutex.lock();}
	void		unlock(){mutex.unlock();}

	uint32		xtal;

public:
	SioIoBuffer	rxa,txa,rxb,txb;

private:
	pthread_t	tid_rxa,tid_txa,tid_rxb,tid_txb;

	union{uint8 mra[3];struct{uint8 mra0,mra1,mra2;};};
	union{uint8 mrb[3];struct{uint8 mrb0,mrb1,mrb2;};};

	uint8		csra,csrb;		// clock select registers
	uint8		acr;			// aux. command register
	VOL uint8	isr;			// interrupt status register
	VOL uint8	imr;			// interrupt mask register
	uint8		ctpu,ctpl;		// c/t preload value
	uint8		gpr;			// general purpose register
	uint8		ipr;			// input port bits
	uint8		opcr;			// output pin control register
	uint8		opr;			// output port bits
	uint8		sra,srb;		// status registers
	uint		mra_ptr,mrb_ptr;// pointer in mra[] and mrb[]

	VOL pthread_t	ct_tid;
	VOL double		ct_period;
	VOL double		ct_when;

	double		calc_tpc			(uint8 acr, uint8 mr0, uint8 csr, bool tx);

public:
	explicit	Sio					(QObject* parent, uint my_bit, cstr eeprom_filepath);

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

	void		set_bits_in_isr		(uint8 mask);
	void		reset_bits_in_isr	(uint8 mask);

	void		fu_ct();			// fu for c/t thread
	void		fu_txa();			// fu for tx thread
	void		fu_rxa();			// fu for rx thread
	void		fu_txb();			// fu for tx thread
	void		fu_rxb();			// fu for rx thread


signals:
public slots:

};


#endif // SIO_H



























