//===========================================================================//
// File:	L4Serial.hh                                                         //
// Project: MUNGA           Brick: Platform-specific IO                      //
// Contents: PC serial interface class                                       //
//---------------------------------------------------------------------------//
//   Date    Who  Modification                                               //
// --------  ---  -----------------------------------------------------------//
// 01/04/95  CPB  Initial coding.                                            //
//---------------------------------------------------------------------------//
// Copyright (C) 1994, Virtual World Entertainment, Inc.                     //
// All Rights reserved worldwide                                             //
// This unpublished sourcecode is PROPRIETARY and CONFIDENTIAL               //
//===========================================================================//

#pragma once

#include "../MUNGA/STYLE.H"
//Win32 Serial support: ADB 06/30/07
#include <Windows.h>
#include <iostream>

// WARNING**WARNING**WARNING**WARNING**WARNING**WARNING**WARNING**WARNING
// Do NOT change ANY of the following lines without making
// corresponding changes in PCSerial.asm!  PCSerial.hh exists
// ONLY to inform C++ compilers of the data and structures
// used by PCSerial.asm!
// WARNING**WARNING**WARNING**WARNING**WARNING**WARNING**WARNING**WARNING


//Win32 Serial support: ADB 06/30/07
// These are purposely grouped as two parameters in one equate.
// The standard PC COM ports ALWAYS use these combinations.
//#define	PCS_COM1	0x3F8,0x0C		// portAddress,interruptNumber
//#define	PCS_COM2	0x2F8,0x0B
//#define	PCS_COM3	0x3E8,0X0C
//#define	PCS_COM4	0x2E8,0x0B

// Data rates
#define	PCS_300		0x0180			// 300 Baud
#define	PCS_1200	0x0060			// 1200 Baud
#define	PCS_2400	0x0030			// 2400 Baud
#define	PCS_9600	0x000C			// 9600 Baud
#define	PCS_19P2	0x0006			// 19200 Baud
#define	PCS_38P4	0x0003			// 38400 Baud
#define	PCS_115K	0x0001			// 115,200 Baud (not supported on all PC's)

// Data formats
//Win32 Serial support: ADB 06/23/07
//Added masks as follows
//00PP PTSS where SS = Speed, T = Stop Bits, & PPP = Parity
#define PCS5D		0x0000			// 5 data bits
#define PCS6D		0x0001			// 6 data bits
#define PCS7D		0x0002			// 7 data bits
#define PCS8D		0x0003			// 8 data bits
#define PCSMD		0x0003			// data size mask

#define PCS1S		0x0000			// one stop bit
#define PCS2S		0x0004			// two stop bits
#define PCSMS		0x0004			// stop bit mask

#define PCSNP		0x0000			// no parity
#define PCSOP		0x0008			// odd parity
#define PCSEP		0x0018			// even parity
#define PCSMP		0x0028			// mark parity (untested)
#define PCSSP		0x0038			// space parity (untested)
#define PCSMAP		0x0038			// parity mask

									// some popular combinations-
#define	PCS_N81	(PCS8D|PCS1S|PCSNP)	// no parity, 8 data, 1 stop
#define	PCS_E81	(PCS8D|PCS1S|PCSEP)	// even parity, 8 data, 1 stop
#define	PCS_O81	(PCS8D|PCS1S|PCSOP)	// odd parity, 8 data, 1 stop

// Flow control
//	#define	PCS_NOFLOWCTRL	0x0000	// no flow control

// error bits returned by PC_Serial::Errors()
#define	PCS_ERR_OVERRUN	0x0002
#define	PCS_ERR_PARITY		0x0004
#define	PCS_ERR_FRAMING	0x0008
#define	PCS_ERR_BREAK		0x0010
#define	PCS_ERR_ALLOC		0x0100		// failed at slot allocation
#define	PCS_ERR_INIT		0x0200		// failed DPMI initialization
#define	PCS_ERR_TXOVER		0x0400		// overran transmit buffer
#define	PCS_ERR_RXOVER		0x0500		// overran receive buffer
//Win32 Serial support: ADB 01/15/07
#define PCS_ERR_OTHER		0x8000		//Other Error not listed above

// bits returned by PC_Serial::ModemStatus()

// (1st four bits not defined yet, but non-zero)
#define	PCS_MS_CTS			0x0010
#define	PCS_MS_DSR			0x0020
#define	PCS_MS_RI			0x0040
#define	PCS_MS_DCD			0x0080


class PCSerial;

HANDLE InitSerialPort(WORD rate, WORD format, const char* port, bool overlapped);

class PCSerial
{
public:
	//Win32 Serial support: ADB 01/11/07
	PCSerial(WORD rate, WORD format, const char* port);

	~PCSerial();

	//Win32 Serial support: ADB 01/15/07
	Logical TestInstance();

   Logical CanDraw();

	//Win32 Serial support: ADB 01/15/07
	int ReceiveString(char *destPtr, int maxLen);// returns length

	//Win32 Serial support: ADB 01/15/07
	void FlowControlEnable(Logical onOff);

	//Win32 Serial support: ADB 01/15/07
	void SendString(char *srcPtr, int length);

	//Win32 Serial support: ADB 01/15/07
	int ReceiveCharCount();

	//Win32 Serial support: ADB 01/15/07
	int TransmitCharCount();

	//Win32 Serial support: ADB 01/15/07
	int Errors();// returns error value, clears it

	//Win32 Serial support: ADB 01/15/07
	int IsActive();

	//Win32 Serial support: ADB 01/15/07
	int ModemStatus();

#		if defined(USE_SIGNATURE)
		friend int
			Is_Signature_Bad(const volatile PCSerial *ptr)
				{ return PCSerialIsSignatureBad(ptr); }
#		endif

//	protected:

	//Word portBase;
	HANDLE hComm;
   OVERLAPPED mOverlappedOb;
	//Word errorValue;
	Byte enabled;
	//Byte txFlow;
	//Byte flowOverride;
	//LWord intDataPtr;
	//LWord hackCount;

	// This is for INTERNAL USE only (reflects structure in .ASM file)
	//Win32 Serial support: ADB 01/15/07
	//struct PCSerBuf
	//{
	//	Word head, tail, count;
	//	Byte	data[1024];
	//}
	//txBuf, rxBuf;
};