ai_sounds.c 8.06 KB
#include <sys/types.h>
#ifdef __sgi__
#include <sys/sbd.h>
#endif
#include <sys/stat.h>
#include <sys/mman.h>
#ifdef __sgi__
#include <sys/sema.h>
#endif
#include <netinet/in.h>

#include <errno.h>
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>



#include <rcp.h>

#include "diag.h"
#include <dbg_comm.h>


extern char Name[];
extern int NameValid;

int
ai_sounds (TEST_REF *test_ref)
{


    int NumFailures = 0;
    int i;
    int MoreData, FirstTime, AudioFull;
    int AudioReadPtr;

    int CurrentLength,AudioBusy;

    int *pwbuf;
    int *prbuf;
    unsigned int numbytes;
    unsigned int rdbuf;

#define SampleSize 4
#define AI_STATUS_AUDIO_BUSY 0x40000000
#define AI_STATUS_DMA_BUSY 0x08000000
/* RCP1 has limit of 32k-1 bytes Length specification */
/* or 8k-1 32 bit samples */
/* Length is specified  in bytes with 64 bit resoultion */
/* RCP2 has increased limit */
/* Need to check version and adjust */

#define MaxBufferLength (200000L)

    unsigned char wbuf[MaxBufferLength*SampleSize];
    unsigned char rbuf[MaxBufferLength*SampleSize];

    int BufferLength;

FILE *aud_fp;

FILE *fopen();
unsigned char aud_file[80];
unsigned char fullpath[256];

/* determine audio sample file name and open it */
/*  note this is local to current working directory */
if (NameValid) {
    strcat (aud_file,&(Name[0]));
    strcat (aud_file,".bin");
    errlog(DEBUG,"Command line selected sample file is %s",aud_file);
    NameValid = 0;
}
else { /* default sample file */
     errlog (DEBUG,"Reading Samples from default file elec_piano.bin");
     strcpy (aud_file,"elec_piano.bin");
}

if (! dgFindFile(fullpath, "/PR/diags/ai/data", aud_file)){
	
	if (! dgFindFile(fullpath, "/usr/diags/data",aud_file)){

		errlog(ERR_SEVERE,"Failed to find file %s",aud_file);
}
}

if((aud_fp = fopen(fullpath,"r")) == NULL){
        errlog(ERR_SEVERE,"Unable to open input file %s",aud_file);
        return (1);
}


    /* Initialize Rdram */
    errlog(DEBUG,"Initializing Rdram");

    /* Init VI to get video clock signal into AI */
    /* if (dgWriteWord(VI_STATUS_REG, 0) ) return (1); */


/* ready to start loading and dma */

    pwbuf = (int *)wbuf;
    prbuf = (int *)rbuf;

    MoreData = 1;
    FirstTime = 1;

    AudioReadPtr = dgRdramFree ;
    errlog(DEBUG,"Audio Buffer will be loaded at address %x",AudioReadPtr);


   /* Main Loop */
   while (MoreData) {
    if (dgReadWord(AI_STATUS_REG, &rdbuf) ) return (1);

    AudioFull =  (rdbuf & AI_STATUS_FIFO_FULL) ;

    if (FirstTime & AudioFull) {
        errlog(ERR_SEVERE,"AI Status is initially full, should be 0 on reset");
    }
    if ( !AudioFull) {

    /* may want to read earlier */
    BufferLength = fread (pwbuf, SampleSize , MaxBufferLength, aud_fp);
        errlog(DEBUG,"Read 0x%x  L/R Audio Samples ",BufferLength);

    /* did we get the whole file read in */
    /* currently disabled, only load up to MaxBufferLength */
    /* so always set MoreData = 0 */
    if (BufferLength == MaxBufferLength)  MoreData = 0;
    if (BufferLength < MaxBufferLength)   MoreData = 0;
    if (BufferLength == 0) MoreData = 0;

    if (BufferLength != 0) {
    /* load/verify the buffer to rdram */
#define MEMLOAD
#ifdef WORDLOAD
    for (i = 0; i < BufferLength ; i ++) {

        if (dgWriteWord ((AudioReadPtr+(i*4)),*(pwbuf+i))) return (1);
        errlog(DEBUG,"address %x  Data %x",(AudioReadPtr+(i*4)),*(pwbuf+i));
    


        if (dgReadWord ((AudioReadPtr+(i*4)),&rdbuf)) return (1);
        if (rdbuf != *(pwbuf+i)) {
            errlog ( ERR_SEVERE,
            "Rdram buffer load failed - actual = %08x, expected = %08x",
            rdbuf, *(pwbuf+i));
        }
    }
#endif
#ifdef MEMLOAD

/* load /verify whole buffer */

    numbytes =  BufferLength * 4 ;
    if (dgWriteMem (dgRdramFree, numbytes, ((char *)pwbuf)) ) return (1);
    if (dgReadMem  (dgRdramFree, numbytes, ((char *)prbuf)) ) return (1);

    for (i = 0; i < BufferLength; i++){
      if (*(pwbuf+i) != *(prbuf+i) ) {
      errlog(ERR_SEVERE,
          "Rdram load failed - index %x actual = %08x, expected = %08x",
          i,*(prbuf+i), *(pwbuf+i));
      }
    }
#endif
    /* kick off the dma */

    if (FirstTime) {
	FirstTime = 0;

        /* Initialize audio here, to stop output until ready */
        /* Program Audio for 44.1k rate
        * To get ~44.1 KHz for audio (based on Rambus clock of 243.37 MHz
        * and video clock of 48.67 MHz), AI dac rate = 1103 (0x44f) and
        * AI bit rate = 15 (0xf)
        */
        errlog(DEBUG,"Initializing Audio Frequency");
        if ( dgWriteWord(AI_DACRATE_REG, 0x44f) ) return (1);
        if ( dgWriteWord(AI_BITRATE_REG, 0xf) ) return (1);
        if ( dgWriteWord(AI_CONTROL_REG, AI_CONTROL_DMA_ON) ) return (1);
    }
        errlog(DEBUG,"Starting DMA: DMA Address %x, DMA Byte Length %x",
            AudioReadPtr,(BufferLength*4));

	errlog(INFO,"Listen for an Audio Sample ....");
	dgWait("To Hear the Audio Sample ...");
	if (1) {
	  long int i, curLen, mySize, curPtr;
	  i = 0;
	  curLen = 4 * BufferLength;
	  mySize = 4 * 4096L;
	  curPtr = AudioReadPtr;
          while (curLen > 0) {
	    if ( dgWriteWord(AI_DRAM_ADDR_REG, curPtr) ) return(1);
            if (curLen > mySize) {
	      if ( dgWriteWord(AI_LEN_REG, mySize) ) return (1);
	    } else {
	      if ( dgWriteWord(AI_LEN_REG, curLen) ) return (1);
	    }
            errlog(DEBUG,"Starting DMA @ Address %08x , len left=%d\n",
			curPtr, curLen);
	    i++;
	    curLen -= mySize;
	    curPtr += mySize;
	    do {
              if (dgReadWord(AI_STATUS_REG, &rdbuf) ) return (1);
              AudioFull =  (rdbuf & AI_STATUS_FIFO_FULL) ;
	    } while(AudioFull);
	  }
	}
	return(0);
	/* end of rhn added */

        AudioReadPtr += BufferLength*4 ;
    } /* BufferLength 0 */

    }  /* !AudioFull */
    else { /*  Full */

    /* spin on Audio Full */

    while (AudioFull){
        if (dgReadWord(AI_STATUS_REG, &rdbuf) ) return (1);
        AudioFull =  (rdbuf & AI_STATUS_FIFO_FULL) ;
        errlog
	(DEBUG,"Spinning on Full = %x Audio Busy = %x DMA Busy = %x DAC CTR = %x",
	(rdbuf & AI_STATUS_FIFO_FULL), (rdbuf & AI_STATUS_AUDIO_BUSY),
        (rdbuf & AI_STATUS_DMA_BUSY), (rdbuf & 0xfff7) );
    }
    }

   } /* MoreData */
/* wait for audio output to finish */

    errlog (DEBUG," Loaded Last of Audio Data to Rdram");

    CurrentLength = 0x7fffffff; /* bogus number */
    if (dgReadWord(AI_STATUS_REG, &rdbuf) ) return (1);
    AudioBusy = rdbuf & AI_STATUS_AUDIO_BUSY;
    while (AudioBusy) {

        if (dgReadWord(AI_LEN_REG, &rdbuf)) return (1);
        if (CurrentLength != rdbuf) {
            CurrentLength = rdbuf;
            errlog (DEBUG,"Current Length is %x",CurrentLength);
            AudioBusy = 1;
        }
        else {
            if (dgReadWord(AI_STATUS_REG, &rdbuf) ) return (1);
            AudioBusy = rdbuf & AI_STATUS_AUDIO_BUSY;
        }
    }

/* should wait a while here for simulation to run the data out the dac */
/* for simulation purposes only */
/*
while (1) AudioStatus();
*/


    fclose (aud_fp);

    return(NumFailures);

}



int
AudioStatus ()
{

int Full, Busy , DmaBusy, DmaRequest, DmaEnable;
int Dfifo2Loaded, DataAvail, WordSelect, AbusWord2;
int DacCnt0, DacCntValue;
unsigned int rdbuf;



      if (dgReadWord(AI_STATUS_REG, &rdbuf) ) return (1);

      Full =  ((rdbuf >> 31) & 1);
      Busy = ((rdbuf >> 30) & 1);
      DmaBusy = ((rdbuf >> 27) & 1);
      DmaRequest = ((rdbuf >> 26) & 1);
      DmaEnable = ((rdbuf >> 25) & 1);
      Dfifo2Loaded = ((rdbuf >> 23) & 1);
      DataAvail = ((rdbuf >> 22) & 1);
      WordSelect = ((rdbuf >> 21) & 1);
      AbusWord2 = ((rdbuf >> 19) & 1);
      DacCnt0 = ((rdbuf >> 16) & 1);
      DacCntValue =  ((rdbuf >> 1) & 0x7fff);

      errlog(INFO,
      "Full: %d Busy: %d Dfifo2Loaded: %d DataAvail: %d",
      Full,Busy,Dfifo2Loaded,DataAvail);

      errlog(INFO,
      "DmaEnable: %d DmaBusy: %d DmaRequest %d",
      DmaEnable,DmaBusy,DmaRequest);

      errlog(INFO,
      "WordSelect: %d AbusWord2: %d DacCntValue: %xx DacCnt0 %d",
      WordSelect, AbusWord2, DacCntValue, DacCnt0);

      if (dgReadWord(AI_LEN_REG, &rdbuf)) return (1);
      errlog(INFO,"Current Length is %x",rdbuf);

    return (0);


}