/****************************************************************************/
/* Copyright 2002 MBARI							*/
/****************************************************************************/
/* Summary  : Main Routine for OASIS Mooring Controller                     */
/* Filename : oasis.c                                                       */
/* Author   : Robert Herlien (rah)					    */
/* Project  : OASIS Mooring Replacement (OASIS3)			*/
/* Revision : 0.1							*/
/* Created  : 10/14/2002						*/
/*									    */
/* MBARI provides this documentation and code "as is", with no warranty,    */
/* express or implied, of its quality or consistency. It is provided without*/
/* support and without obligation on the part of the Monterey Bay Aquarium  */
/* Research Institute to assist in its use, correction, modification, or    */
/* enhancement. This information should not be published or distributed to  */
/* third parties without specific written permission from MBARI.            */
/*									    */
/****************************************************************************/
/* Modification History:						    */
/* 10oct2002 rah - created from OASIS oasis.c				*/
/****************************************************************************/

#include	<mbariTypes.h>		/* MBARI type definitions	    */
#include	<mbariConst.h>		/* MBARI constants		    */
#include	<cfxbios.h>		// Persistor BIOS and I/O Definitions
#include	<cfxpico.h>		// Persistor PicoDOS Definitions
#include	<stdio.h>
#include	<fcntl.h>
#include	<unistd.h>
#include	<ctype.h>

static char	*filename = "TEST.DAT";
static char	buffer[32768];

#define SECTOR		1024
#define CPU_CLOCK	7360L
	

/************************************************************************/
/* Function    : main							*/
/* Purpose     : Main entry point					*/
/* Inputs      : None							*/
/* Outputs     : None							*/
/* Comments    : Never returns						*/
/************************************************************************/
int main( int argc, char **argv)
{
#pragma unused (argc, argv)
  Nat32		i, iotime;
  Nat16		nsects = 1, drive = DSD_FIRST_DEV;
  Nat16		n, cursize, remain;
  Nat16		j, k;
  uchar		ch;
  RTCTimer	rt;

  BIOSInit();
  TMGSetSpeed(CPU_CLOCK);
  // Identify the progam and build
  printf("\nProgram: %s: %s %s \n", __FILE__, __DATE__, __TIME__);
  // Identify the device and its firmware
  printf("Persistor CF%d SN:%ld   BIOS:%d.%d   PicoDOS:%d.%d\n", CFX,
	 BIOSGVT.CFxSerNum, BIOSGVT.BIOSVersion, BIOSGVT.BIOSRelease, 
	 BIOSGVT.PICOVersion, BIOSGVT.PICORelease);
	
  printf("OASIS file test program\n\n");

  while(TRUE)
  {
    printf("\n");
    printf("Type 'R' to read DOS logical sectors\n");
    printf("Type 'W' to write DOS logical sectors\n");
    printf("Type 'S' to change number of sectors\n");
    printf("Type 'V' to change drive number\n");
    printf("Type 'D' to read and display one DOS logical sector\n");
    printf("Type 'I' to get disk info\n");
    printf("Type 'X' to exit\n");

    switch(cgetc())
    {
      case 'X':
      case 'x':
      case 3:
	printf("Goodbye\n");
	return(0);

      case 'S':
      case 's':
	printf("Current number of sectors is %d.  New number of sectors:  ",
	       nsects);
	if ((scanf("%d", &n) < 1) || (n < 1) || (n > 2048))
	{
	  printf("\nBad input value\n");
	  break;
	}
	nsects = n;
	printf("\nNew number of sectors is %d\n", nsects);
	break;

      case 'V':
      case 'v':
	printf("Current drive is %c.  New drive to use:  ",
	       drive + 'A');
	n = scanf("%c", &ch);
	ch = toupper(ch);
	if ((n < 1) || (ch < 'C') || (ch > 'Z'))
	{
	  printf("\nBad input value\n");
	  break;
	}
	drive = ch - 'A';
	printf("\nNew drive is %c\n", ch);
	break;

      case 'R':
      case 'r':
	RTCElapsedTimerSetup(&rt);
	for (remain = nsects; remain > 0; )
	{
	  cursize = (remain > (sizeof(buffer)/1024)) ?
		     (sizeof(buffer)/1024) : remain;
	  DSDReadSectors(drive, (ulong)SECTOR + nsects - remain,
			  buffer, cursize);
	  remain -= cursize;
	}
	iotime = RTCElapsedTime(&rt);
	
	printf("Read time %ld us\n\n", iotime);
	break;

      case 'W':
      case 'w':
	for (i = 0; i < sizeof(buffer); i++)
	  buffer[i] = (char)i;

	RTCElapsedTimerSetup(&rt);
	for (remain = nsects; remain > 0; )
	{
	  cursize = (remain > (sizeof(buffer)/1024)) ?
		     (sizeof(buffer)/1024) : remain;
	  DSDWriteSectors(drive, (ulong)SECTOR + nsects - remain,
			  buffer, cursize);
	  remain -= cursize;
	}
	iotime = RTCElapsedTime(&rt);
	
	printf("Write time %ld us\n\n", iotime);
	break;

      case 'D':
      case 'd':
	printf("Logical sector to read:  ");
	
	if (scanf("%ld", &i) < 1)
	{
	  printf("\nBad input value\n");
	  break;
	}
	DSDReadSectors(drive, (ulong)i, buffer, 1);
	printf("\n");

	for (j = 0; j < 32; j++)
	{
	  for (k = 0; k < 16; k++)
	 {
	    ch = (uchar)buffer[16*j + k];
	    printf("%02x ", ch);
	 }
	  printf("  ");

	  for (k = 0; k < 16; k++)
	  {
	    ch = (uchar)buffer[16*j + k];
	    putchar(isprint(ch) ? ch : '.');
	  }
	  printf("\n");
	}
	break;

      case 'I':
      case 'i':
	RTCElapsedTimerSetup(&rt);
	i = DIRFreeSpace("C:");
	iotime = RTCElapsedTime(&rt);
	printf("Free space = %ld, time = %ld us\n", i, iotime);
	break;
    }
  }

  return(0);

}	//____ main() ____//

