punk@69: #include punk@69: #include punk@69: #include punk@69: punk@69: #include "asim/provides/connected_application.h" punk@69: #include "asim/provides/stats_device.h" punk@69: //#include "asim/provides/SndfileWavUtil.h" punk@69: punk@69: #include "asim/rrr/client_stub_AUDIOCORERRR.h" punk@69: punk@69: using namespace std; punk@69: punk@69: #define NUM_VOICES 2 punk@69: punk@69: pthread_mutex_t CONNECTED_APPLICATION_CLASS::lock; punk@69: pthread_cond_t CONNECTED_APPLICATION_CLASS::cond; punk@69: sem_t CONNECTED_APPLICATION_CLASS::throttle; punk@69: punk@69: // constructor punk@69: CONNECTED_APPLICATION_CLASS::CONNECTED_APPLICATION_CLASS(VIRTUAL_PLATFORM vp) : punk@69: punk@69: clientStub(new AUDIOCORERRR_CLIENT_STUB_CLASS(this)) punk@69: { punk@69: } punk@69: punk@69: // destructor punk@69: CONNECTED_APPLICATION_CLASS::~CONNECTED_APPLICATION_CLASS() punk@69: { punk@69: } punk@69: punk@69: // init punk@69: void punk@69: CONNECTED_APPLICATION_CLASS::Init() punk@69: { punk@69: punk@69: pthread_mutex_init(&lock, NULL); punk@69: pthread_cond_init(&cond, NULL); punk@69: sem_init(&throttle, 0, 64); punk@69: punk@69: // enable stats punk@69: STATS_DEVICE_SERVER_CLASS::GetInstance()->SetupStats(); punk@69: } punk@69: punk@69: void punk@69: CONNECTED_APPLICATION_CLASS::UpdateSemaphore() punk@69: { punk@69: sem_post(&throttle); punk@69: } punk@69: punk@69: void punk@69: CONNECTED_APPLICATION_CLASS::EndSimulation() punk@69: { punk@69: printf("EndSimulation Called\n"); punk@69: fflush(stdout); punk@69: pthread_mutex_lock(&lock); punk@69: // Do something about the race occuring here punk@69: pthread_cond_signal(&cond); punk@69: pthread_mutex_unlock(&lock); punk@69: printf("EndSimulation done\n"); punk@69: fflush(stdout); punk@69: } punk@69: punk@69: // main punk@69: void punk@69: CONNECTED_APPLICATION_CLASS::Main() punk@69: { punk@69: //rlm: two files punk@69: FILE* inputFiles[NUM_VOICES]; punk@69: // FILE *inputFile; punk@69: // FILE *inputFile1; punk@69: UINT16 sample; punk@69: UINT16 sample1; punk@69: punk@69: //rlm: not sure if normal ints are ok here; using UINT16 because I know it works punk@69: UINT16 channel0 = 0; punk@69: UINT16 channel1 = 1; punk@69: punk@69: //init processor punk@69: int sleepCount = 0; punk@69: int result = 0; punk@69: punk@69: bool coreFin = false; punk@69: punk@69: fflush(stdout); punk@69: punk@69: // Convert input wav to pcm punk@69: punk@69: printf("word\n"); punk@69: punk@69: for (int i = 0; i < NUM_VOICES; i++) punk@69: { punk@69: char* filename; punk@69: sprintf(filename, "input%d", i); punk@69: punk@69: printf("Opening audio file %s", strcat(filename, ".wav")); punk@69: generate_pcm(strcat(filename, ".wav"),strcat(filename, ".pcm")); punk@69: // generate_pcm("input1.wav", "input1.pcm"); punk@69: punk@69: inputFiles[i] = fopen(strcat(filename, ".pcm"), "r"); punk@69: punk@69: assert(inputFiles[i]); punk@69: } punk@69: /* punk@69: //rlm: for now we are going to going to just have 2 files with "common names" punk@69: generate_pcm("input.wav","input.pcm"); punk@69: generate_pcm("input1.wav", "input1.pcm"); punk@69: //Send data to the machine here. punk@69: //rlm: two files punk@69: inputFile = fopen("input.pcm","r"); punk@69: inputFile1 = fopen("input1.pcm", "r"); punk@69: assert(inputFile1); punk@69: assert(inputFile); punk@69: */ punk@69: punk@69: int count = 0; punk@69: punk@69: printf("main:PROCESSOR about to enter loop %d\n", count); punk@69: punk@69: //rlm: here we read both files. later refactor into a function. punk@69: // also, this will terminate when the FIRST file reaches its end. punk@69: while( (fread(&sample, 2, 1, inputFiles[0])) || (fread(&sample1, 2 , 1, inputFiles[1]))) punk@69: { /* punk@69: printf("hi\n"); punk@69: if (!coreFin && (result = clientStub->ReadCPUToHost(0)) != 1) punk@69: { punk@69: sleepCount++; punk@69: } punk@69: else if (!coreFin && result == 1) punk@69: { punk@69: printf("\n***SOFT_CORE PASSED***\n"); punk@69: coreFin = true; punk@69: } punk@69: */ punk@69: punk@69: if(count%1000 == 0) punk@69: printf("main: %d\n", count); punk@69: count++; punk@69: sem_wait(&throttle); punk@69: punk@69: //rlm: two files. punk@69: // clientStub->SendUnprocessedStream( Data,(UINT32)sample); punk@69: clientStub->SendUnprocessedStream((UINT32)channel0 , Data,(UINT32)sample); punk@69: clientStub->SendUnprocessedStream((UINT32)channel1 , Data,(UINT32)sample1); punk@69: } punk@69: punk@69: printf("main: out of loop\n"); punk@69: punk@69: // Need to put lock here to prevent potential race condition punk@69: pthread_mutex_lock(&lock); punk@69: punk@69: //rlm: have to send end-files to both voices. punk@69: // all of these operations wil eventually be moved into functions. punk@69: //clientStub->SendUnprocessedStream(EndOfFile,0); punk@69: clientStub->SendUnprocessedStream((UINT32)channel0, EndOfFile,0); punk@69: clientStub->SendUnprocessedStream((UINT32)channel1, EndOfFile,0); punk@69: punk@69: printf("main: wait for end of file\n"); punk@69: punk@69: pthread_cond_wait(&cond, &lock); punk@69: pthread_mutex_unlock(&lock); punk@69: punk@69: printf("main: last data out\n"); punk@69: punk@69: // Convert input wav to pcm punk@69: // this part is ok because there is always only one input file. punk@69: generate_wav("out_hw.pcm","input.wav","out_hw.wav"); punk@69: punk@69: printf("generate wav done\n"); punk@69: punk@69: // printf("If you missed it, core %s", coreFin ? "PASSED\n" : "FAILED\n"); punk@69: // Dump the stats file punk@69: punk@69: STATS_DEVICE_SERVER_CLASS::GetInstance()->DumpStats(); punk@69: STATS_DEVICE_SERVER_CLASS::GetInstance()->EmitFile(); punk@69: punk@69: fflush(stdout); punk@69: // exit(0); punk@69: }