test_n64dd.c 4.19 KB

/*************************************************************

  test_mem.h : Nintendo 64 Music Tools Library Sample
  (c) Copyright 1998, Software Creations (Holdings) Ltd.

  Version 3.11

  N64DD demo N64DD related source file. 

**************************************************************/

/* include system header files */
#ifndef F3DEX_GBI
#define F3DEX_GBI
#endif

#include <ultra64.h>
#include <leo.h>

#include "test_n64dd.h"


#define	NUM_LEO_MESGS	8

static	OSMesg	LeoMessages[NUM_LEO_MESGS];
static OSMesgQueue     diskQ;
static OSMesg		diskQBuf;

static OSPiHandle *cartrom_handle;
static OSPiHandle *diskrom_handle;


/*
 * Define for error_handle routine
 */
#define	RETRY	0
#define	FATAL	1


s32 error_handle(s32 result)
{
  LEODiskID	diskID;
  LEOCmd	cmdBlock;
  s32		error;
  
  if (!( (result == LEO_ERROR_MEDIUM_NOT_PRESENT) ||
	(result == LEO_ERROR_MEDIUM_MAY_HAVE_CHANGED )))
    return FATAL;
  
  if (result == LEO_ERROR_MEDIUM_NOT_PRESENT)
  {
#ifdef _DEBUG
    osSyncPrintf("Insert the disk\n");
#endif  
  }
  
  while(1)
  {
    /*
     * Do the following at a time
     *  1. Wait for the users to insert the disk (if the error
     *     was "NO MEDIA").
     *  2. Get the disk ID of the inserted disk.
     */
    for (;;)
    {
      LeoReadDiskID(&cmdBlock, &diskID, &diskQ);
      osRecvMesg(&diskQ, (OSMesg *)&error, OS_MESG_BLOCK);

      if (error == 0)
	break;
      
      if (!( (error == LEO_ERROR_MEDIUM_NOT_PRESENT) ||
	    (error == LEO_ERROR_MEDIUM_MAY_HAVE_CHANGED )))
      {
#ifdef _DEBUG
	osSyncPrintf("error = 0x%x\n", error);
#endif
	return FATAL;
      }      
    } 
    
    /*
     * Check that this disk is the same as the previous one.
     */
    if (bcmp((void *)&leoBootID, (void *)&diskID, sizeof(LEODiskID))== 0)
      return RETRY;
    
    /*
     * Since the inserted disk is the wrong one, stop the spindle
     * motor and wait for the users to change the disk.
     */
#ifdef _DEBUG
    osSyncPrintf("This disk is wrong. Eject it and insert the right disk.\n");
#endif
    do
    {
      LeoSpdlMotor(&cmdBlock, SLEEP, &diskQ);
      osRecvMesg(&diskQ, (OSMesg *)&error, OS_MESG_BLOCK);
      if (!( (error == 0) ||
	    (error == LEO_ERROR_MEDIUM_NOT_PRESENT) ||
	    (error == LEO_ERROR_MEDIUM_MAY_HAVE_CHANGED )))
	return FATAL;
    } while ( error != LEO_ERROR_MEDIUM_MAY_HAVE_CHANGED );

  } /* while(1) */

  /* Never reached, only to remove warning message */
  return 0;
  
} /* error_handle() */

void DiskFatalError(void)
{
  osSyncPrintf("Fatal error\n");
  for(;;);
}

#ifdef __GNUC__
void DiskRead(short int start, short int end, void *dest)
#else
void DiskRead(void *start, void *end, void *dest)
#endif
{
  LEOCmd	cmdBlock;
  s32	error;

  for (;;)
  {
    LeoReadWrite(&cmdBlock, OS_READ, (u32)start, dest, (u32)end - (u32)start, &diskQ);
    osRecvMesg(&diskQ, (OSMesg *)&error, OS_MESG_BLOCK);
    if (error == 0)
      return;
    if (error_handle(error) == FATAL)
      DiskFatalError();
  }
}

#ifdef __GNUC__
int DiskSize(short int start, short int end)
#else
int DiskSize(void *start, void *end)
#endif
{
  s32 bytes;
  s32 error;

  error = LeoLBAToByte((u32)start, (u32)end-(u32)start, &bytes);
  if (error!=LEO_ERROR_GOOD)
    DiskFatalError();
  return (bytes);
}

void DiskInitialise(void)
{
  s32 error;
  LEODiskID	diskID;
  LEOCmd	cmdBlock;

  cartrom_handle = osCartRomInit();
  diskrom_handle = osDriveRomInit();

  osCreateMesgQueue(&diskQ, &diskQBuf, 1);
  error = LeoCreateLeoManager((OSPri)OS_PRIORITY_LEOMGR - 1,
			      (OSPri)OS_PRIORITY_LEOMGR,
			      LeoMessages, NUM_LEO_MESGS);
  
  if(error != LEO_ERROR_GOOD)
    if (error_handle(error) == FATAL)		  
      DiskFatalError();

  /*
   * We must check that the current disk is the
   * same as to booted disk
   */
  for (;;)
  {
    LeoReadDiskID(&cmdBlock, &diskID, &diskQ);
    osRecvMesg(&diskQ, (OSMesg *)&error, OS_MESG_BLOCK);
    
    if (error == 0)
      break;
      
    if (!( (error == LEO_ERROR_MEDIUM_NOT_PRESENT) ||
	   (error == LEO_ERROR_MEDIUM_MAY_HAVE_CHANGED )))
    {	
      DiskFatalError();
    }      
  }
	
  /*
   * Check that this disk is the same as the booted one.
   */
  if (bcmp((void *)&leoBootID, (void *)&diskID, sizeof(LEODiskID)) != 0)
    DiskFatalError();
}


/* end of file */