#include	<string.h>
#include	<stdio.h>
#include	<dos.h>
#include	<conio.h>
#include	<process.h>
#include	<stdlib.h>

// base = 0x378 - 1 port,  0x278 - 2 port

#define	BASE		0x0378
#define	DATALATCH   BASE
#define	STATUS		(BASE + 1)
#define	CONTROLS	(BASE + 2)

#define	STROBE		0x01
#define AULF		0x02
#define	_INIT		0x04

#define	ACK			0x40
// #define	BUSY		0x80

int   	far *head_keys  = (int far *)0x0040001aL;
int   	far *tail_keys  = (int far *)0x0040001cL;
unsigned int extrabit = 0; // inverted AULF

int fast_kbhit(void)
{
if( *head_keys != *tail_keys )
	return(1);
else
	return(0);
}

void mass_copy ( unsigned char mass[], unsigned int sizemass )
{
unsigned int addr;

	delay ( 50 );
	outp( CONTROLS, 0x00 | STROBE | extrabit );
	delay ( 50 );
	outp( CONTROLS, _INIT | STROBE | extrabit );
	delay ( 50 );
	for( addr = 0; addr < sizemass; addr++ )
			{
			outp( DATALATCH, mass[addr] );
			outp( CONTROLS, _INIT | extrabit );
			while( ACK & inp( STATUS ) )
				if(fast_kbhit())
					break;
			outp( CONTROLS, _INIT | STROBE | extrabit );
			while( ACK &  ~inp( STATUS ) )
				if(fast_kbhit()) 
					break;
			}
}

main (int argc, char *argv[] )
{
	FILE	*data_file;
	long i;
	int		c, numbytes, recordtype, cflag=32;
	unsigned int addr;
	char	s[256],  ext[10] ;
	char	*cp;
	unsigned char mass[0x8000];
	char mybuff[4000];

 outp( CONTROLS, 0x00 | extrabit ); //setup STR=0 for testing ACK
	printf("<<<<<<<<<<<<<<   ROM emulator loader v1.2   >>>>>>>>>>>>>>\n");
	if ((argc != 2) && (argc != 3))
		{
		printf("        Usage: romul <filename> [flag]\nFlag - (8, 16) if need reduce capasity\n");
		exit(1);
		}
	sscanf( argv[1], "%*[^.] %*c %s", ext );
	strlwr(ext);
	if(argc == 3)
		{
		sscanf(argv[2], "%d", &cflag);
		if((cflag == 8) || (cflag == 16))
			printf("Reduce flag = %d\n", cflag);
		else
			{
			printf("Err: Legal volume of reduce flag 8, 16\n");
			exit(2);
            }
		}

	if( !strcmp( ext, "hex" ))
		if((data_file = fopen ( argv[1], "rt" )) == NULL)
			{
			printf ( " ----    Can't open input file\n" );
			exit ( 3 );
			}

	if( !strcmp( ext, "bin" ))
		if((data_file = fopen ( argv[1], "rb" )) == NULL)
			{
			printf ( "----    Can't open input file\n" );
			exit ( 3 );
			}

	if( strcmp( ext, "hex" ) && strcmp( ext, "bin" ) )
		{
		printf( "\nBad type\n\7" );
		exit ( 3 );
		}
	//For determination
	for(i=0; i<sizeof(mass); i++)
		mass[i] = 0xff;

	if( ACK & inp( STATUS ) )
		{
		printf( "\nEmulator is not ready, please unlock for continue or press any key for abort\n\7" );
		while( ACK & inp( STATUS ) )
			if( fast_kbhit() != NULL )
				{
				fclose ( data_file );
				while(fast_kbhit())
					getch();
				exit( 2 );
				}
		}
// outp( CONTROLS, 0x00 | extrabit );
 setvbuf( data_file, mybuff, _IOFBF, sizeof(mybuff) );
	if( !strcmp( ext, "hex" ) )
		{
		while ( fgets ( s, sizeof(s), data_file ) != NULL )
			{
			sscanf ( s, ":%2x%4x%2x", &numbytes, &addr, &recordtype );
			cp = s + 9;
			if ( recordtype == 0 )
				while ( numbytes-- )
					{
					sscanf ( cp, "%2x", &c );
					cp += 2;
					if(addr < sizeof(mass))
						mass[addr++] = (unsigned char) ( c );
					else
						printf("Out of range\n");
					}
			}
		}
	if(!strcmp( ext, "bin" ))
		fread(mass, 1, sizeof(mass), data_file);
	if(cflag == 8)
		for(i=0; i < sizeof(mass)/4; i++)
			mass[i+0x2000] = mass[i+0x4000] = mass[i+0x6000] = mass[i];
	if(cflag == 16)
		for(i=0; i < sizeof(mass)/2; i++)
			mass[i+0x4000] = mass[i];

	printf("Press any key for abort.\n");
	mass_copy(mass , sizeof(mass));
	fclose( data_file );
	while(fast_kbhit())
		getch();
	return 0;
}


Сайт создан в системе uCoz