/* HS08IB2.C 
 * Laufzeitmessung von Programmabschnitten
 * Beschleunigung von BIOS-Aufrufen in C
 */
#include <dos.h>
#include <stdio.h>
#include <time.h>
#include <bios.h>
#include <conio.h>          
#include <graph.h>

              
enum COUNTERMODE { CTR_START, CTR_STOP, CTR_STOPREAD, CTR_SUSPEND, CTR_RESUME}; 

double counter(enum COUNTERMODE mode)
{
  static clock_t start;
  clock_t finish;
  static double duration;

  switch (mode)
  {
  default: return duration;
  case CTR_START:
    start = clock();
    duration = 0.0;
    return duration;
  case CTR_STOP:
    finish = clock();
    duration = duration + (double)(finish - start) / CLOCKS_PER_SEC;
    return duration;
  case CTR_STOPREAD:
    finish = clock();
    duration = duration + (double)(finish - start) / CLOCKS_PER_SEC;
    printf( "\n%f Sekunden\n", duration );
    return duration;
  case CTR_SUSPEND:
    finish = clock();
    duration = duration + (double)(finish - start) / CLOCKS_PER_SEC;
    return duration;
  case CTR_RESUME:
    start = clock();
    return duration;
  }
}

void test_ANSI(void)
{      
  int z,s;
  /* Bildschirm löschen */
  printf("\x1b[2J");
  for (z=0; z<25; z++)
    for (s=0; s<80; s++)
      /* Positionieren */
      printf("\x1b[%i;%iH*",z,s  );
}
                
void test_BIOS(void)
{      
  int z,s;
  /* Bildschirm löschen */
  _clearscreen(_GCLEARSCREEN);
  for (z=0; z<25; z++)
    for (s=0; s<80; s++)
    {
      /* Positionieren */
      _settextposition(z,s);  
      /* cprintf("%c",'*'); */
      putch('*');
    }
}
                
void test_DIRECT1(void)
{      
  int z,s;      
  unsigned char _far *p = (unsigned char _far *)_MK_FP(0xb800,0);
  /* Bildschirm löschen */
  _clearscreen(_GCLEARSCREEN);
  for (z=0; z<25; z++)
    for (s=0; s<80; s++)
    {
      *p='*';
      p++;
      p++;
    }
}
                
void test_DIRECT(void)
{      
  int z,s;      
  unsigned char _far *p = (unsigned char _far *)_MK_FP(0xb800,0);
  /* Bildschirm löschen */
  _clearscreen(_GCLEARSCREEN);
  for (z=0; z<25; z++)
    for (s=0; s<80; s++)
    {
      unsigned char _far *p = (unsigned char _far *)_MK_FP(0xb800,(z*80+s)*2);
      *p='*';
    }
}
                
void main(void)
{
  unsigned int i;       

  counter(CTR_START);
  for (i=0; i<100; i++)
  {
    test_DIRECT();
  }
  counter(CTR_STOPREAD);

  getch();
}     


