
//		0.0.1	1995. 1.14	main
//		0.0.2			19	main
//		0.0.3		  2.16	BreakFast  <- main
//		0.0.4			17	main
//		0.0.5			18	main
//		0.0.6			19	main

#include <  conio.h >
#include <    egb.h >
#include < fmcfrb.h >
#include <  stdio.h >
#include < stdlib.h >
#include <   time.h >

void	main( void ) ;
short	BreakFast(	short loop,
					short l     ) ;

char	work[ EgbWorkSize ] ;
char	para[ 16 ] ;

void main( void )
{
	short	n ;
	short	m ;
	short	l ;
	char	message_1[ 22 ] = "Punchi Your Keybord!!" ;
	char	message_2[ 13 ] = "Try Again..." ;
	short	a ;

	a = _inp( 0x05ec ) ;
	_outb( 0x05ec, 0 ) ;

	EGB_init( work, EgbWorkSize ) ;
	EGB_resolution( work, 0, 17 ) ;
	EGB_displayPage( work, 0, 1 ) ;
	 WORD( para + 0x0 ) =   0 ;
	 WORD( para + 0x2 ) =   0 ;
	 WORD( para + 0x4 ) = 511 ;
	 WORD( para + 0x6 ) = 479 ;
	EGB_viewport( work, para ) ;
	EGB_displayStart( work, 3, 0, 0 ) ;
	EGB_displayStart( work, 1, 0, 0 ) ;
	EGB_displayStart( work, 2, 1, 1 ) ;
	EGB_clearScreen( work ) ;
	EGB_pen( work, 0x01 ) ;
	EGB_penSize( work, 1 ) ;
	EGB_writePage( work, 0 ) ;

	EGB_writeMode( work, 0 ) ;
	EGB_paintMode( work, 0x0002 ) ;
	EGB_colorIGRB( work, 2, 0x00000000 ) ;

	EGB_textDirection( work, 0 ) ;
	EGB_textDisplayDirection( work, 0 ) ;
	EGB_textSpace( work, 2 ) ;
	EGB_fontStyle( work, 0x01 ) ;

	srand( (unsigned int)time( NULL ) ) ;

	EGB_textZoom( work, 0, 16, 16 ) ; 
	EGB_colorIGRB( work, 0, 0x0000ff00 ) ;
	 WORD( para + 0x0 ) = 10 ;
	 WORD( para + 0x2 ) = 40 ;
	 WORD( para + 0x4 ) = 21 ;
	for ( l = 0 ; l < 21 ; l++ )
	{
	 BYTE( para + 0x6 + l ) = message_1[ l ] ;
	}
	EGB_asciiString( work, 1, para ) ;

	do
	{
		KYB_clrbuf() ;
		while( !_kbhit() ) ;
		n = BreakFast( 256, 8 ) ;
		EGB_clearScreen( work ) ;

		if ( n < 0 )
		{
			EGB_textZoom( work, 0, 16, 16 ) ; 
			EGB_colorIGRB( work, 0, 0x000000ff ) ;
			 WORD( para + 0x0 ) = 10 ;
			 WORD( para + 0x2 ) = 40 ;
			 WORD( para + 0x4 ) = 12 ;
			for ( l = 0 ; l < 12 ; l++ )
			{
			 BYTE( para + 0x6 + l ) = message_2[ l ] ;
			}
			EGB_asciiString( work, 1, para ) ;
		}
	}	while ( n < 0 ) ;

	n = 256 - n ;
	for ( m = 0 ; m < n ; m++ )
	{
		EGB_colorIGRB( work, 0, 0x00000000
				  + 0x00010101 * (rand() % 256)
				  + 0x00000000 * (rand() % 256)
				  + 0x00000000 * (rand() % 256) ) ;	//
		 WORD( para + 0x0 ) = 2 ;
		 WORD( para + 0x2 ) = 240 + rand() %  32 ;
		 WORD( para + 0x4 ) = 208 + rand() %  32 ;
		 WORD( para + 0x6 ) =       rand() % 512 ;
		 WORD( para + 0x8 ) =       rand() % 480 ;
		EGB_unConnect( work, para );
	}

	EGB_textZoom( work, 0, 64, 64 ) ; 
	EGB_colorIGRB( work, 0, 0x007f7fff ) ;
	 WORD( para + 0x0 ) = 320 ;
	 WORD( para + 0x2 ) = 480 ;
	 WORD( para + 0x4 ) = 3 ;
	 WORD( para + 0x6 ) = (short)(n /   100) + 0x30 ;
		n = n -   100 * (para[ 0x6 ] - 0x30) ;
	 WORD( para + 0x7 ) = (short)(n /    10) + 0x30 ;
		n = n -    10 * (para[ 0x7 ] - 0x30) ;
	 WORD( para + 0x8 ) = (short)(n /     1) + 0x30 ;
		n = n -     1 * (para[ 0x8 ] - 0x30) ;
	EGB_asciiString( work, 1, para ) ;

	while( !_kbhit() ) ;

	_outb( 0x05ec, a ) ;
}

short BreakFast(	short loop,
					short l     )
{
	char	matrix[  256 ][ 16 ] ;
	short	n ;
	short	m ;
	short	count ;
	short	key[ 8 ] ;
	short	total ;

	for ( n = 0 ; n < l ; n++ )
	{
		key[ n ] = 0 ;
	}

	for ( m = 0 ; m < loop ; m++ )
	{
		KYB_matrix( &matrix[ m ][ 0 ] ) ;
	}

	m = 0 ;
	count = 0 ;
	do
	{
		for ( n = 0, count = 0 ; n < 0x80 ; n++ )
		{
			if (matrix[ m ][ (short)(n / 8) ] & (1 << (n % 8)) )
			{
				count ++ ;
			}
		}
		key[ count - 1 ]++ ;

		m++ ;
	}	while ( (m < loop) && (count < l) ) ;


	total = 0 ;
	for ( n = 0 ; n < l - 1 ; n++ )
	{
		if ( key[ n ] == 0 )
		{
			total = -1 ;
		}
		total += key[ n ] ;
	}

	return ( total ) ;
}

