/*
**	$VER: ramspeed  Release (15.12.93)
**
**	Ram Test , tests bulk memeory copies using cpu with display dma acting
**			   as a test of slow down
**
**  Programmed by : Raul A. Sobon.
**
**	(C) Copyright 1993 \.../ TM, Inc.
**		All Rights Reserved
*/


#include <exec/types.h>
#include <exec/memory.h>
#include <exec/execbase.h>

#include <dos/dos.h>
#include <dos/dosextens.h>
#include <dos/stdio.h>				// dos stdio

#include <graphics/gfxbase.h>
#include <exec/memory.h>
#include <intuition/classes.h>
#include <intuition/gadgetclass.h>
#include <intuition/intuitionbase.h>

#include <proto/exec.h>				// use amiga library stuff
#include <proto/dos.h>
#include <proto/graphics.h>
#include <proto/intuition.h>

#include <stdio.h>					// and the thing we all use!
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <dos.h>


extern	void bulkcopy( APTR source, APTR dest, LONG size);




APTR			MainVisualInfo;
struct Screen	*MainScreen;
#include	"ramspeed_rev.h"
static	UBYTE	verstr[]={ VERSTAG };





/*
 * --- Open lots of libraries that I need.
 */
long OpenLibraries( void ){

	if ( !(GfxBase = (struct GfxBase *) OpenLibrary((UBYTE *) "graphics.library" , 33l ))) {
		WriteStr("\graphics.library\n");
		return FALSE;
		}

	if ( !(DOSBase = (struct DosLibrary *) OpenLibrary((UBYTE *) "dos.library", 33l ))) {
		WriteStr("\tdos.library\n");
		return FALSE;
		}

	if ( !(IntuitionBase = (struct IntuitionBase *) OpenLibrary((UBYTE *) "intuition.library", 33l ))) {
		WriteStr("\tintuition.library\n");
		return FALSE;
		}

	return TRUE;
}




/*
 * --- Close the libraries which are opened by me.
 */
void CloseLibraries( void ){
	if (IntuitionBase)	CloseLibrary( (struct Library *) IntuitionBase );
	if (DOSBase)		CloseLibrary( (struct Library *) DOSBase );
	if (GfxBase)		CloseLibrary( (struct Library *) GfxBase );
}





void ShowArgs( void ){
	puts("RamSpeed (c) Raul A. Sobon,  Dec-1993");
	puts("options are :");
	puts("	CHIP2FAST ... copy chip ram to fast ram");
	puts("	FAST2FAST ... copy fast ram to fast ram");
	puts("	CHIP2CHIP ... copy chip ram to chip ram");
	puts("	FAST2CHIP ... copy fast ram to chip ram");
}



#define	MOVESIZE	10000000
#define	BLOCKSIZE	0xf000


	APTR	source;
	APTR	dest;




ULONG	GetSysTime( void ){
	unsigned int t1[2]={0,0};

	timer( t1 );
//	printf("t1=%ld t2=%ld\n",t1[0],t1[1]);

	return ( (1000*t1[0])+(t1[1]/1000) );

}

void TestRam( void ){
	ULONG	time1,time2,time3,blksize,bpms,nsb,lp;

	printf("Moving %ld BYTES ...\n", MOVESIZE );
	blksize = (BLOCKSIZE>>2)-1;
	time1=GetSysTime();
	Forbid();
	for(lp=0;lp<(MOVESIZE/BLOCKSIZE);lp++){
		bulkcopy( source, dest, blksize );
	}
	Permit();

	time2 = GetSysTime();
	time3 = time2-time1;
	if( time3 == 0 ) {printf("error\n"); return; };
	bpms = MOVESIZE/time3;
	nsb = 1000000/bpms;

	puts("done.");

	printf("It took %ld ms to move %ld bytes of data\n",time3,MOVESIZE);
	printf("Which is %ld bytes/sec\n", bpms*1000 );
	printf("Which is %ld ns/byte\n", nsb );
	printf("Which is %ld color clock cycles/byte\n", (nsb/280)+1 );
	printf("Which is %ld 12Mhz 68020 cycles/byte\n", (nsb/83)+1 );
	printf("Which is %ld 25Mhz 68040 cycles/byte\n", (nsb/42)+1 );
}


// ----------------------------------------------------------------------------------------------
int main( int argc,char *argv[] ) {
	BOOL				 running  = TRUE;
	APTR	fastram,chipram;

	OpenLibraries();

	fastram = AllocVec( BLOCKSIZE, MEMF_PUBLIC );
	chipram = AllocVec( BLOCKSIZE, MEMF_CHIP );


	if( argc < 2 )
		ShowArgs();
	else
	if( !strncmp( "CHIP2FAST",argv[1],9) || !strncmp( "chip2fast",argv[1],9) ) {
		source = chipram; dest = fastram;  TestRam();	}
	else
	if( !strncmp( "FAST2FAST",argv[1],9) || !strncmp( "fast2fast",argv[1],9) ) {
		source = fastram; dest = fastram;  TestRam();	}
	else
	if( !strncmp( "CHIP2CHIP",argv[1],9) || !strncmp( "chip2chip",argv[1],9) ) {
		source = chipram; dest = chipram;  TestRam();	}
	else
	if( !strncmp( "FAST2CHIP",argv[1],9) || !strncmp( "fast2chip",argv[1],9) ) {
		source = fastram; dest = chipram;  TestRam();	}
	else
		ShowArgs();


	FreeVec( fastram );
	FreeVec( chipram );

	CloseLibraries();

	Exit( TRUE );
}

