#ifndef _ZVGPORT_H_
#define _ZVGPORT_H_
/*****************************************************************************
* Header file for ZVGPORT.C.
*
* Author:  Zonn Moore
* Created: 11/06/02
*
* History:
*
* (c) Copyright 2002, Zektor, LLC.  All Rights Reserved.
*****************************************************************************/

#define	ZVG_MAX_BFRSZ		256	// maximum number of bytes the ZVG can return

#define	PERIPH_TIMEOUT		100	// the standard says 35ms, we've loosened it too 100ms
#define	PERIPH_WAIT	  		1000 	// this is non-error timeout used to wait for Peripheral responses

// Offset from base address of parallel port registers

#define	ECP_data					0x000
#define	ECP_ecpAFifo			0x000
#define	ECP_dsr					0x001	
#define	ECP_dcr					0x002
#define	ECP_cFifo				0x400
#define	ECP_ecpDFifo			0x400
#define	ECP_tFifo				0x400
#define	ECP_cnfgA				0x400
#define	ECP_cnfgB				0x401
#define	ECP_ecr					0x402

// ECR bitmaps

#define	ECR_SPP_mode			0x00		// SPP mode
#define	ECR_EPP_mode			0x20		// Bi-Di mode
#define	ECR_FSPP_mode			0x40		// Fast SPP mode
#define	ECR_ECP_mode			0x60		// ECP mode
#define	ECR_Cnfg_mode			0xE0		// Confige mode, makes 'cnfgA' and 'cnfgB' available

#define	ECR_nErrIntrEn			0x10
#define	ECR_dmEn					0x08
#define	ECR_serviceIntr		0x04
#define	ECR_full					0x02
#define	ECR_empty				0x01

// DSR Bitmaps

#define	DSR_InvMask				(DSR_Busy)

//    SPP Mode

#define	DSR_Busy					0x80
#define	DSR_nAck					0x40
#define	DSR_PError				0x20
#define	DSR_Select				0x10
#define	DSR_nFault				0x08

//    NIBBLE Mode

#define	DSR_PtrBusy				0x80
#define	DSR_PtrClk				0x40
#define	DSR_AckDataReq			0x20
#define	DSR_XFlag				0x10
#define	DSR_nDataAvail			0x08

//    ECP Mode

#define	DSR_PerphAck			0x80
#define	DSR_PeriphClk			0x40
#define	DSR_nAckReverse		0x20
#define	DSR_nPeriphRequest	0x08

// DCR bitmaps

#define	DCR_InvMask				(DCR_nSelectIn | DCR_nAutoFeed | DCR_nStrobe)

#define	DCR_Direction			0x20
#define	DCR_ackIntEn			0x10

//    SPP Mode

#define	DCR_nSelectIn			0x08
#define	DCR_nInit				0x04
#define	DCR_nAutoFeed			0x02
#define	DCR_nStrobe				0x01

//    NIBBLE Mode

#define	DCR_1284_Active		0x08
#define	DCR_HostBusy			0x02
#define	DCR_HostClk				0x01

//    ECP Mode

#define	DCR_nReverseRequest	0x04
#define	DCR_HostAck				0x02

// Negotiation modes

#define	EMODE_NIBBLE			0x00
#define	EMODE_REQID_NIBBLE	0x04
#define	EMODE_ECP				0x10
#define	EMODE_REQID_ECP		0x14

// Value returned in case of a timeout

#define	ZVG_TIMEOUT				((uint)-1)

// Define some error codes

enum portErrCode
{	errOk = 0,					// no error (must be set to 0)

	errNotEcp,					// ECP port not found at given address
	errDmaIrq,					// Could not determine DMA or IRQ settings
	errEcpWord,					// Not an 8 bit ECP
	errEcpFailed,				// Could not negotiate for ECP mode
	errEcpNoData,				// no data available from the peripheral
	errEcpBadData,				// bad data (or data count) read from ZVG
	errEcpTimeout,				// something took too long and was out of spec
	errEcpBusy,					// peripheral is too busy for us
	errEcpComm,					// communication error
	errEcpBadMode,				// trying to do something in the wrong mode
	errEcpToSpp,				// Protocol changed from ECP to SPP mode
	errZvgRomCS,				// checksum error in ROM packet
	errZvgRomNE,				// flash data did not match ROM packet
	errZvgRomTI,				// flash timeout during write
};

// This structure reflects the structure inside the ZVG firmware. Note that DJGPP does not
// pack structures by default, but the data inside the ZVG is packed by default.

#define	ZVG_MON_SIZE		11		// size of packed 'ZvgMon_s' structure used by ZVG firmware

typedef struct ZVGMON_S
{	uchar		point_i;			// point intensity
	uchar		zShift;			// shift value
	uchar		oShoot;			// overshoot value
	uchar		jumpFactor;		// multiplier used to calc clock cycles for jump
	uchar		settle;			// minimum value of Jump count used for settling time
	uchar		min_i;			// minimum color value allowed
	uchar		max_i;			// maximum color value allowed
	uchar		scale;			// Size of screen
	uchar		flags;			// Monitor status flags
	ushort	cksum;			// Fletcher's check digits of monitor data
} ZvgMon_s;

extern void zvgError( uint err);
extern void zvgInit( void);
extern uint zvgDetectECP( uint portAdr, uint *ecpDMA, uint *ecpIRQ);
extern uint zvgPutc( uchar cc);
extern uint zvgPutMem( uchar *ss, uint len);
extern uint zvgGetMem( uchar *ss, uint bfrLen, uint *aReadLen);
extern uint zvgGetDeviceID( uchar *ss, uint idLen, uint *aReadLen);
extern uint zvgSetEcpMode( void);
extern uint zvgEcpPutc( uchar cc);
extern uint zvgEcpPutMem( uchar **mem, uint memSize);
extern void zvgSetSppMode( void);
extern uint	zvgIsDataAvail( uint time);
extern uint zvgReadMonitorInfo( ZvgMon_s *mon);
extern uint zvgReadSpeedInfo( char *speeds);

#endif
