/*
 *
 *  ALL03
 *
 *  26.07.2002
 *
 *  by Fabio Sturman
 *
 *  fabio.sturman@tiscali.it
 *
 *  all03ft.c
 *
 *  file i/o and test
 *  utilities
 *
 */

/* Copyright (c) 2002 Fabio Sturman (fabio.sturman@tiscali.it)
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
 */

#include <stdio.h>
#include "../include/keys.h"
#include "../include/sleep.h"

#ifdef DJGPP
#include <go32.h>
#include <sys/exceptn.h>
#endif

#ifdef WATCOM
#include "ctrlcb.h"
#endif

/* TEST PROCESSOR SPEED */

void test(void)
{
  long t0, t1, dt;

  if(!get_one_ms()) return;
  clear_screen();
  printf("*** TIMING TEST 30000 ms ***\n\n");
  
  printf("> START CHRONO and press <ENTER>!\n");
  wait_enter();

  printf("* TESTING...\n");

  t0=get_timer_ticks();
  printf("* Start:   %fs\n", t0 * (1/18.2));
  delay_ms(30000);
  t1=get_timer_ticks();
  printf("* End:     %fs\n", t1 * (1/18.2));
  dt=t1-t0;
  printf("* Elapsed: %fs\n", dt * (1/18.2));
  printf("* Error:   %.1f%%\n", ((dt * (1/18.2))-30.0)*100/30.0);
  printf("> STOP CHRONO!\n");

  printf("OK\n");

}


/* GENERIC FILE I/O */

int get_file_name(char *input_string, char *file_name)
{
  int i;

  i=inputstr(0, 3, input_string, file_name, 255, 40);
  printf("\n");
  return(i);
}


void read_file_to_buf(
  char *input_string,
  char *file_name,
  unsigned char *buf,
  unsigned long buf_len,
  unsigned char fill)
{
  FILE *f;
  int n, i;

  i=get_file_name(input_string,file_name);
  if(i==K_ESC) return;
  
  for(n=0;n<buf_len;n++) *(buf+n)=fill;

  printf("Reading from disk...\n");
  if((f=fopen(file_name,"rb"))==NULL)
  {
    printf("Error opening file (%s)!\n",file_name);
    return;
  }
  n=fread(buf,1,buf_len,f);
  printf("Readed %d bytes of data\n",n);
  fclose(f);
}


void write_buf_to_file(
  char *input_string,
  char *file_name,
  unsigned char *buf,
  unsigned long buf_len)
{
  FILE *f;
  int n, i;
      
  i=get_file_name(input_string, file_name);
  if(i==K_ESC) return;

  printf("Writing to disk...\n");
  if((f=fopen(file_name,"wb"))==NULL)
  {
    printf("Error opening file (%s)!\n",file_name);
    return;
  }
  n=fwrite(buf,1,buf_len,f);
  printf("Writed %d bytes of data\n",n);
  fclose(f);
}

void flush_out(void)
{
#ifdef WATCOM
    flushall();
#endif
}

void enable_ctrl_c_break(void)
{
#ifdef WATCOM
        enable_traps();
#endif
#ifdef DJGPP
      _go32_want_ctrl_break(1);
      __djgpp_set_ctrl_c(0);
#endif
}

void disable_ctrl_c_break(void)
{
#ifdef WATCOM
  disable_traps();
#endif
#ifdef DJGPP
  _go32_want_ctrl_break(1);
  __djgpp_set_ctrl_c(0);
#endif
}

/*
 * test if timing ok
 *
 */
int test_if_timer(void)
{
  if(!get_one_ms())
  {
    printf("\n\007* ALL03 Error - Timing not OK!\n");
    return(1);
  }
  return(0);
}

