annotate modules/bluespec/Pygar/core/AudioCoreSystem.cpp @ 13:6d461680c6d9 pygar svn.14

[svn r14] more stuff
author punk
date Tue, 27 Apr 2010 09:03:28 -0400
parents
children 90197e3375e2
rev   line source
punk@13 1 #include <stdio.h>
punk@13 2 #include <pthread.h>
punk@13 3 #include <semaphore.h>
punk@13 4
punk@13 5 #include "asim/provides/connected_application.h"
punk@13 6 #include "asim/provides/stats_device.h"
punk@13 7 //#include "asim/provides/SndfileWavUtil.h"
punk@13 8
punk@13 9 #include "asim/rrr/client_stub_AUDIOCORERRR.h"
punk@13 10
punk@13 11 using namespace std;
punk@13 12
punk@13 13 pthread_mutex_t CONNECTED_APPLICATION_CLASS::lock;
punk@13 14 pthread_cond_t CONNECTED_APPLICATION_CLASS::cond;
punk@13 15 sem_t CONNECTED_APPLICATION_CLASS::throttle;
punk@13 16
punk@13 17 // constructor
punk@13 18 CONNECTED_APPLICATION_CLASS::CONNECTED_APPLICATION_CLASS(VIRTUAL_PLATFORM vp) :
punk@13 19 clientStub(new AUDIOCORERRR_CLIENT_STUB_CLASS(this))
punk@13 20 {
punk@13 21 }
punk@13 22
punk@13 23 // destructor
punk@13 24 CONNECTED_APPLICATION_CLASS::~CONNECTED_APPLICATION_CLASS()
punk@13 25 {
punk@13 26 }
punk@13 27
punk@13 28 // init
punk@13 29 void
punk@13 30 CONNECTED_APPLICATION_CLASS::Init()
punk@13 31 {
punk@13 32
punk@13 33 pthread_mutex_init(&lock, NULL);
punk@13 34 pthread_cond_init(&cond, NULL);
punk@13 35 sem_init(&throttle, 0, 64);
punk@13 36
punk@13 37 // enable stats
punk@13 38 STATS_DEVICE_SERVER_CLASS::GetInstance()->SetupStats();
punk@13 39 }
punk@13 40
punk@13 41 void
punk@13 42 CONNECTED_APPLICATION_CLASS::UpdateSemaphore()
punk@13 43 {
punk@13 44 sem_post(&throttle);
punk@13 45 }
punk@13 46
punk@13 47 void
punk@13 48 CONNECTED_APPLICATION_CLASS::EndSimulation()
punk@13 49 {
punk@13 50 printf("EndSimulation Called\n");
punk@13 51 fflush(stdout);
punk@13 52 pthread_mutex_lock(&lock);
punk@13 53 // Do something about the race occuring here
punk@13 54 pthread_cond_signal(&cond);
punk@13 55 pthread_mutex_unlock(&lock);
punk@13 56 printf("EndSimulation done\n");
punk@13 57 fflush(stdout);
punk@13 58 }
punk@13 59
punk@13 60 // main
punk@13 61 void
punk@13 62 CONNECTED_APPLICATION_CLASS::Main()
punk@13 63 {
punk@13 64 FILE *inputFile;
punk@13 65 UINT16 sample;
punk@13 66
punk@13 67 // Convert input wav to pcm
punk@13 68 generate_pcm("input.wav","input.pcm");
punk@13 69
punk@13 70 //Send data to the machine here.
punk@13 71 inputFile = fopen("input.pcm","r");
punk@13 72 assert(inputFile);
punk@13 73
punk@13 74
punk@13 75 int count = 0;
punk@13 76
punk@13 77 printf("main: about to enter loop %d\n", count);
punk@13 78
punk@13 79 while(fread(&sample, 2, 1, inputFile)) {
punk@13 80 if(count%1000 == 0)
punk@13 81 printf("main: %d\n", count);
punk@13 82 count++;
punk@13 83 sem_wait(&throttle);
punk@13 84 clientStub->SendUnprocessedStream(Data,(UINT32)sample);
punk@13 85 }
punk@13 86
punk@13 87 printf("main: out of loop\n");
punk@13 88
punk@13 89 // Need to put lock here to prevent potential race condition
punk@13 90 pthread_mutex_lock(&lock);
punk@13 91 clientStub->SendUnprocessedStream(EndOfFile,0);
punk@13 92
punk@13 93 printf("main: wait for end of file\n");
punk@13 94
punk@13 95 pthread_cond_wait(&cond, &lock);
punk@13 96 pthread_mutex_unlock(&lock);
punk@13 97
punk@13 98 printf("main: lastt data out\n");
punk@13 99
punk@13 100 // Convert input wav to pcm
punk@13 101 generate_wav("out_hw.pcm","input.wav","out_hw.wav");
punk@13 102
punk@13 103 printf("generate wav done\n");
punk@13 104
punk@13 105 fflush(stdout);
punk@13 106 exit(0);
punk@13 107 }
punk@13 108
punk@13 109 /* THIS IS THE CODE HANDLING FROM THE REGULAR SOFT-CORE
punk@13 110 TO BE INCORPORATED
punk@13 111 // main
punk@13 112 void
punk@13 113 CONNECTED_APPLICATION_CLASS::Main()
punk@13 114 {
punk@13 115 int sleepCount = 0;
punk@13 116 int result = 0;
punk@13 117
punk@13 118 fflush(stdout);
punk@13 119
punk@13 120 while ((result = clientStub->ReadCPUToHost(0)) != 1) {
punk@13 121 sleep(1);
punk@13 122 //printf("System controller sleeps with result: %d\n", result);
punk@13 123 sleepCount++;
punk@13 124 if(sleepCount == 100) {
punk@13 125 printf("Failed to get response from hardware, bailing\n\n");
punk@13 126 printf("This means that either your hardware is hanging\n");
punk@13 127 printf("or that the software hasn't given it enough time\n");
punk@13 128 printf("to complete. If you think it needs more time, then\n");
punk@13 129 printf("edit CONNECTED_APPLICATION_CLASS::Main() in ProcessorSystem.cpp\n");
punk@13 130 printf("(connected_application)\n");
punk@13 131 exit(0);
punk@13 132 }
punk@13 133 }
punk@13 134
punk@13 135 if(result == 1) {
punk@13 136 printf("\n***PASSED***\n");
punk@13 137 }
punk@13 138
punk@13 139 // Dump the stats file
punk@13 140
punk@13 141 STATS_DEVICE_SERVER_CLASS::GetInstance()->DumpStats();
punk@13 142 STATS_DEVICE_SERVER_CLASS::GetInstance()->EmitFile();
punk@13 143
punk@13 144 fflush(stdout);
punk@13 145 exit(0);
punk@13 146 }*/