
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <intuition/intuitionbase.h>
#include <devices/timer.h>
#ifdef VBCC
#include <inline/graphics_protos.h>
#endif
#ifdef GCC
#include <inline/graphics.h>
#endif
/* Add inline/protos/pragmas foryour compiler here,if it isn't VBCC nor GCC */


#include <graphics/gfx.h>
#include <graphics/rastport.h>
#include <exec/memory.h>
#include "posbb.h"
#include "posbb_rev.h"
#include "posbb_logo.h"


void Perform_Test();
FILE *fi = 0;
UBYTE Version []=VERSTAG;
BYTE Copyright []=VERS;
struct IntuitionBase *IntuitionBase=0;
struct GfxBase *GfxBase=0;
struct Window *window=0;
struct timerequest *timereq = NULL;
struct MsgPort *msgport=0;
struct Device *TimerBase=0;
int main( argc, argv )
int argc;
char *argv[];
{
long tests = 0;
int c,precision = POSBB_PRECISIONLOW;
BOOL freeze = FALSE;
   if (argc == 1)
   {
      printf("POSBB Portable OS Based Benchmark   ( Amiga version )\n  Created by Pietro Altomani <altomanipietro@pragmanet.it>\nUsage: %s <options>\nOptions:\n  -all  Perform all tests\n  -gfx Perform only graphics tests\n  -mem Perform only memory tests\n  -disk Perform only disk tests\n  -math Perform only math tests\n  -o <filename> Output file name\n  -freeze Freeze the machine when running\n-p <precision> Set precision. <precision> can be 'low','medium','high','higher' or 'highest'.\nSee docs for more infos.\n",argv[0]);
      return 10;
   }

   for (c=0;c<argc;c++)
   {
#ifndef NOFORBID
      if ((strcmp(argv[c],"-freeze")) == 0)
      {
	freeze = TRUE;
	printf("Freezing...");
      }
#endif
      if ((strcmp(argv[c],"-all") == 0) || (strcmp(argv[c],"-full")  == 0))
      {
	 tests = -1;
      }
      if (strcmp(argv[c],"-gfx") == 0)
      {
	 tests |= (POSBB_TESTWRITEPIXEL | POSBB_TESTDRAWELLIPSE | POSBB_TESTDRAW);
      }
      if (strcmp(argv[c],"-disk") == 0)
      {
	 tests |= (POSBB_TESTREAD | POSBB_TESTWRITE);
      }
      if (strcmp(argv[c],"-math") == 0)
      {
	 tests |= (POSBB_TESTIMATH | POSBB_TESTFPMATH);
      }
      if (strcmp(argv[c],"-mem") == 0)
      {
	 tests |= (POSBB_TESTCOPYMEM);
      }
      if ((strcmp(argv[c],"-o")) == 0)
      {
	 fi = fopen(argv[c+1],"w");
      }
      if ((strcmp(argv[c],"-p")) == 0)
      {
	 if ((strcmp(argv[c+1],"low")) == 0) precision = POSBB_PRECISIONLOW;
	 if ((strcmp(argv[c+1],"medium")) == 0) precision = POSBB_PRECISIONMEDIUM;
	 if ((strcmp(argv[c+1],"high")) == 0) precision = POSBB_PRECISIONHIGH;
	 if ((strcmp(argv[c+1],"higher")) == 0) precision = POSBB_PRECISIONHIGHER;
	 if ((strcmp(argv[c+1],"highest")) == 0) precision = POSBB_PRECISIONHIGHEST;
      }
   }

   if (fi == 0) fi = fopen(POSBB_DEFRESULTFILE,"w");
   Perform_Test(tests,precision);
   fclose( ( FILE * ) fi );
   return 0;
}

void Perform_Test(tests,precision,freeze)
long tests;
int precision;
int freeze;
{
  int r,tot;
  struct SystemConfig *cfg;
  /* Here it prints the header of the result file.*/
  fprintf(fi,"%s",VSTRING);
  fprintf(fi,"Portable OS Based Benchmark\n");
  fprintf(fi,"Amiga Version\n");
  fprintf(fi,"Created by Pietro Altomani <altomanipietro@pragmanet.it>\n");
  printf("%s",VSTRING);
  printf("Portable OS Based Benchmark\n");
  printf("Amiga Version\n");
  printf("Created by Pietro Altomani <altomanipietro@pragmanet.it>\n");
  ShowConfiguration(fi,TRUE);  /* this prints in the file,and if the second parameter is TRUE on the console, the system configuration   */

  msgport = (struct MsgPort *) CreatePort(NULL,NULL,NULL);
  timereq = (struct timerequest *) CreateExtIO( msgport,sizeof(struct timerequest));
  OpenDevice(TIMERNAME,UNIT_VBLANK,timereq,NULL);
  TimerBase = timereq->tr_node.io_Device;

  if ((tests | ~(POSBB_TESTCOPYMEM)) == -1) {
  printf("CopyMem test in progress...\n");
  r = Test_CopyMem(precision,freeze);
    if (r != 0)
    {
      printf("Result = %d seconds\n",(r * POSBB_PRECISIONHIGHEST / precision));
      fprintf(fi,"CopyMem Test\nTest the speed of RAM access.\nTime:%d seconds\n\n",(r * POSBB_PRECISIONHIGHEST / precision));
    }
    else printf("Test failed. Not enough free memory\n");
   }
  if ((tests | ~(POSBB_TESTPRINTF)) == -1) {
    r = Test_printf(precision,freeze);
    printf("Result = %d seconds\n",(r * POSBB_PRECISIONHIGHEST / precision));
    fprintf(fi,"printf Test\nTests the speed of text output to stdout (console)\nTime:%d seconds\n\n", (r * POSBB_PRECISIONHIGHEST / precision));
  }
  if ((tests | ~(POSBB_TESTIMATH)) == -1) {
    printf("Math test in progress...\n");
    r = Test_IMath(precision,freeze);
    printf("Result = %d seconds\n", (r * POSBB_PRECISIONHIGHEST / precision));
    fprintf( fi,"Math Test\nTest the speed of some math operations (+,-,*,/)\nTime:%d seconds\n\n",(r * POSBB_PRECISIONHIGHEST / precision));
  }
  if ((tests | ~(POSBB_TESTFPMATH)) == -1) {
    printf("Floating Point Math test in progress...\n");
    r = Test_FPMath(precision,freeze);
    printf("Result = %d seconds\n", (r * POSBB_PRECISIONHIGHEST / precision));
   fprintf( fi,"FPMath Test\nTest the speed of some math operations (+,-,*,/)\nTime:%d seconds\n\n",(r * POSBB_PRECISIONHIGHEST / precision));
  }
  if ((tests | ~(POSBB_TESTTMATH)) == -1) {
    printf("TMath test in progress...\n");
    r = Test_TMath(precision,freeze);
    printf("Result = %d seconds\n",(r * POSBB_PRECISIONHIGHEST / precision));
    fprintf( fi,"TMath Test\nTest the speed of trigonometric functions.\nTime:%d seconds\n\n",(r * POSBB_PRECISIONHIGHEST / precision));
  }



/*    qsort test isn't finished
  if ((tests | ~(POSBB_TESTQSORT)) == -1) {
    printf("qsort test in progress...\n");
    r = Test_qsort(precision);
    printf("Result = %d seconds\n",(r*POSBB_PRECISIONHIGHEST/precision));
    fprintf(fi,"Result = %d seconds\n",(r*POSBB_PRECISIONHIGHEST/precision));
  }

*/
#ifdef POSBB_TESTDISK                       /* This can turn on or off the disk speed tests. In fact it isn't very useful.  */
  if ((tests | ~(POSBB_TESTREAD)) == -1) {
    printf("Write test in progress...\n");
    r = Test_Write(POSBB_DEFTESTFILE,precision,freeze);
    printf("Result = %d seconds\n",r);
  }
  if ((tests | ~(POSBB_TESTWRITE)) == -1) {
    printf("Read test in progress...\n");
    r = Test_Read(POSBB_DEFTESTFILE,precision,freeze);
    printf("Result = %d seconds\n",r);
  }
#endif
  /* Here starts gxf tests. */
  IntuitionBase = (struct IntuitionBase *) OpenLibrary("intuition.library",0);
  GfxBase = (struct GfxBase *) OpenLibrary("graphics.library",0);

  window = (struct Window *) OpenWindowTags(NULL,WA_Left,0,WA_Top,0,WA_Width,320,WA_Height,256,WA_Title,"POSBB Test Window",NULL);

  /* Draw the logo in the window */
  /* Working in progress ! */
  #ifdef PRINTLOGOWORKS        /* this turns off logo section if PRINTLOGOWORKS isn't defined.  */
  posbb_logo_bm = (struct BitMap *) AllocMem(sizeof(struct BitMap),MEMF_CHIP);  /* Allocate memory for the bitmap */
  posbb_logo_bm->BytesPerRow = 20;
  posbb_logo_bm->Rows = 257;
  posbb_logo_bm->Flags = BMF_DISPLAYABLE;
  posbb_logo_bm->Depth = 2;
  posbb_logo_bm->Planes[0] = (UBYTE *)posbb_logo_bm[0];
  posbb_logo_bm->Planes[1] = (UBYTE *)posbb_logo_bm[1];
  BltBitMapRastPort(posbb_logo_bm,0,0,window->RPort,0,0,320,256,0x0C0);
  Delay(50);
  #endif
  if ((tests | ~(POSBB_TESTWRITEPIXEL)) == -1) {
  printf("WritePixel test in progress...\n");
  r = Test_WritePixel(window->RPort,precision);
  printf("Result = %d seconds\n",(r * POSBB_PRECISIONHIGHEST / precision));
  fprintf( fi,"WritePixel Test\nTests the speed of pixel drawing.\nTime: %d seconds\n\n",(r * POSBB_PRECISIONHIGHEST / precision));
  }
  if ((tests | ~(POSBB_TESTDRAWELLIPSE)) == -1) {
  printf("DrawEllipse test in progress...\n");
  r = Test_DrawEllipse(window->RPort,precision);
  printf("Result = %d seconds\n",(r * POSBB_PRECISIONHIGHEST / precision));
  fprintf( fi,"DrawEllipse Test\nTests the speed of ellipse drawing.\nTime: %d seconds\n\n",(r * POSBB_PRECISIONHIGHEST / precision));
  }
  if ((tests | ~(POSBB_TESTDRAW)) == -1) {
  printf("Draw test in progress...\n");
  r = Test_Draw(window->RPort,precision);
  printf("Result = %d seconds\n",(r * POSBB_PRECISIONHIGHEST / precision));
  fprintf( fi,"Draw Test\nTests the speed of line drawing.\nTime: %d seconds\n\n",(r * POSBB_PRECISIONHIGHEST / precision));
  }
  if (window) CloseWindow(window);
  if (IntuitionBase) CloseLibrary(IntuitionBase);
  if (GfxBase) CloseLibrary(GfxBase);
  if (timereq) CloseDevice(timereq);
  if (timereq) DeleteExtIO(timereq);
  if (msgport) DeleteMsgPort(msgport);

}

ShowConfiguration(resfile,conflag)     /* file is the (FILE *) to write on. conflag is a flag saying if it has to write to the console,too. conflag = 1 */
FILE *resfile;
{
  fprintf(resfile,"In this version POSBB can't show system config.\n");
  if (conflag) printf("In this version POSBB can't show system configuration.\n");
  return 0;
}

