// snarfer.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#undef LINUX
#define DOS 1

#include <stdio.h>
#include <conio.h>
#include <dos.h>

#include "pport.h"

#define DIR CR_INIT
#define ADS CR_AUTO
#define RCLK CR_STB


void set_adr( long i ) {

	outb( (char) 0x00, LP1_DR );
	outb( (char) DIR | ADS, LP1_CR ); /* pulse ADS to load in address */
	outb( (char) DIR, LP1_CR );
	outb( (char) (i & 0xFF), LP1_DR ); /* assert data */
	outb( (char) DIR | RCLK, LP1_CR ); /* pulse ADS to load in address */
	outb( (char) DIR, LP1_CR );

	outb( (char) 0x01, LP1_DR );
	outb( (char) DIR | ADS, LP1_CR ); /* pulse ADS to load in address */
	outb( (char) DIR, LP1_CR );
	outb( (char) ((i & 0xFF00) >> 8), LP1_DR ); /* assert data */
	outb( (char) DIR | RCLK, LP1_CR ); /* pulse ADS to load in address */
	outb( (char) DIR, LP1_CR );

	outb( (char) 0x02, LP1_DR );
	outb( (char) DIR | ADS, LP1_CR ); /* pulse ADS to load in address */
	outb( (char) DIR, LP1_CR );
	outb( (char) ((i & 0xFF0000) >> 16), LP1_DR ); /* assert data */
	outb( (char) DIR | RCLK, LP1_CR ); /* pulse ADS to load in address */
	outb( (char) DIR, LP1_CR );

#if 0
	outb( (char) 0x03, LP1_DR );
	outb( (char) DIR | ADS, LP1_CR ); /* pulse ADS to load in address */
	outb( (char) DIR, LP1_CR );
	outb( (char) ((i & 0xFF000000) >> 24), LP1_DR ); /* assert data */
	outb( (char) DIR | RCLK, LP1_CR ); /* pulse ADS to load in address */
	outb( (char) DIR, LP1_CR );
#endif

}

int main(int argc, char* argv[])
{
	unsigned char c;
	long i;
	long j;
	long adr;

	char data;

	FILE *ofile;

	if( NULL == (ofile = fopen( "segaall.bin", "w" )) ) {
		printf( "Couldn't open sega1.bin for writing.\n" );
		return(0);
	}

	data = inb( LP1_BASE + 0x402 );
	printf( "ECR mode: %02X\n", data & 0xFF );
	outb( (data & 0x1F) | 0x20, LP1_BASE + 0x402 );

	outb( (char) DIR, LP1_CR );  /* sane settings */

	for( i = 0; i < 256; i++ ) {
		adr = i;
		set_adr( adr );
		
		outb( (char) 0xFF, LP1_DR );
		outb( (char) CR_BID, LP1_CR );
		c = (unsigned char) inb( LP1_DR );
		outb( (char) DIR, LP1_CR );

		if( (i % 16) == 0 ) {
			printf( "\n%08lX: ", adr );
		}
		printf( "%02X ", ((unsigned int) c ) & 0xFF );

	}
	for( i = 0; i < 4194304; i++ ) {
		adr = i;
		set_adr( adr );

		outb( (char) 0xFF, LP1_DR );
		outb( (char) CR_BID, LP1_CR );
		c = (unsigned char) inb( LP1_DR );
		outb( (char) DIR, LP1_CR );

		fwrite( &c, 1, 1, ofile );
		
	}
	fflush( ofile );
	fclose( ofile );

	return(0);
}

