// ###########################################################################
// #
// # OSCAKeyboard - Implementation of OSCA Keyboard Emulation
// # Copyright (C) 2009-2011 Tony Friery [DialTone]
// #
// # ALL RIGHTS RESERVED
// #
// ###########################################################################

#pragma hdrstop

#include "OSCAKeyboard.h"
#include "FormDiag.h"

//---------------------------------------------------------------------------

///////////////////////////////////////////////////////////////////////
//
//  FUNCTION:  TOSCAKeyboard::GotByte
//
//  PURPOSE:	Notification from the Base PS2 Handler that a byte
//					has been clocked in from the host processor to the PS2
//					device, so this now needs to be acted upon
//
//  INPUTS:		byte					Byte data which was clocked in
//
//  OUTPUT:		void
//
///////////////////////////////////////////////////////////////////////
void __fastcall TOSCAKeyboard::GotByte(byte Value)
{
	// This means that the host has sent a byte of
	// data to the keyboard device
	switch(Value)
	{
		case 0xff:	// Reset
			if ((DiagForm) && (DiagForm->IsActive(DIAG_KBMOUSE)))
			{
				DiagForm->AddMessage(DIAG_KBMOUSE, L"Keyboard Command: RESET");
			}

			this->fReadPointer = 0;
			this->fWritePointer = 0;
			this->fIRQState = false;

			// Return a normal (A-OK) BAT result
			this->PushByte(0xff);
			this->PushByte(0xfa);
			this->PushByte(0xaa);
			break;
		default:
			if ((DiagForm) && (DiagForm->IsActive(DIAG_KBMOUSE)))
			{
				DiagForm->AddMessage(DIAG_KBMOUSE, UnicodeString::Format(
					L"Keyboard Command (Unknown): %2.2x", OPENARRAY(TVarRec, (Value)))
				);
			}
			break;
	}
}

///////////////////////////////////////////////////////////////////////
//
//  FUNCTION:  TOSCAKeyboard::Reset
//
//  PURPOSE:	Perform a Power-On Reset of the Keyboard Device
//
//  INPUTS:		void
//
//  OUTPUT:		void
//
///////////////////////////////////////////////////////////////////////
void __fastcall TOSCAKeyboard::Reset()
{
	if ((DiagForm) && (DiagForm->IsActive(DIAG_KBMOUSE)))
	{
		DiagForm->AddMessage(DIAG_KBMOUSE, L"KB Reset");
	}

	// This is fairly naughty - no real need to be a
	// virtual function, but it allows complete control over
	// the reset process if required whilst still allowing
	// the more conventional non-virtual way of doing things!
	TOSCAPS2Device::Reset();
}

///////////////////////////////////////////////////////////////////////
//
//  FUNCTION:  TOSCAKeyboard::TOSCAKeyboard [CONSTRUCTOR]
//
//  PURPOSE:	Constructor for the Keyboard Handler Class
//
//  INPUTS:		none
//
//  OUTPUT:		none
//
///////////////////////////////////////////////////////////////////////
__fastcall TOSCAKeyboard::TOSCAKeyboard()
{
}

#pragma package(smart_init)
