view modules/bluespec/Pygar/core/AudioCoreSystem.cpp @ 41:16ba43f0a7c3 pygar svn.42

[svn r42] got channels working
author rlm
date Tue, 04 May 2010 22:32:52 -0400
parents 5a30f173bbac
children ced2ebd41347
line wrap: on
line source
1 #include <stdio.h>
2 #include <pthread.h>
3 #include <semaphore.h>
5 #include "asim/provides/connected_application.h"
6 #include "asim/provides/stats_device.h"
7 //#include "asim/provides/SndfileWavUtil.h"
9 #include "asim/rrr/client_stub_AUDIOCORERRR.h"
11 using namespace std;
13 pthread_mutex_t CONNECTED_APPLICATION_CLASS::lock;
14 pthread_cond_t CONNECTED_APPLICATION_CLASS::cond;
15 sem_t CONNECTED_APPLICATION_CLASS::throttle;
17 // constructor
18 CONNECTED_APPLICATION_CLASS::CONNECTED_APPLICATION_CLASS(VIRTUAL_PLATFORM vp) :
20 clientStub(new AUDIOCORERRR_CLIENT_STUB_CLASS(this))
21 {
22 }
24 // destructor
25 CONNECTED_APPLICATION_CLASS::~CONNECTED_APPLICATION_CLASS()
26 {
27 }
29 // init
30 void
31 CONNECTED_APPLICATION_CLASS::Init()
32 {
34 pthread_mutex_init(&lock, NULL);
35 pthread_cond_init(&cond, NULL);
36 sem_init(&throttle, 0, 64);
38 // enable stats
39 STATS_DEVICE_SERVER_CLASS::GetInstance()->SetupStats();
40 }
42 void
43 CONNECTED_APPLICATION_CLASS::UpdateSemaphore()
44 {
45 sem_post(&throttle);
46 }
48 void
49 CONNECTED_APPLICATION_CLASS::EndSimulation()
50 {
51 printf("EndSimulation Called\n");
52 fflush(stdout);
53 pthread_mutex_lock(&lock);
54 // Do something about the race occuring here
55 pthread_cond_signal(&cond);
56 pthread_mutex_unlock(&lock);
57 printf("EndSimulation done\n");
58 fflush(stdout);
59 }
61 // main
62 void
63 CONNECTED_APPLICATION_CLASS::Main()
64 {
65 //rlm: two files
66 FILE *inputFile;
67 // FILE *inputFile1;
68 UINT16 sample;
69 //UINT16 sample1;
71 //rlm: not sure if normal ints are ok here; using UINT16 because I know it works
72 UINT16 channel0 = 5;
73 //UINT16 channel1 = 1;
75 //init processor
76 int sleepCount = 0;
77 int result = 0;
79 bool coreFin = false;
81 fflush(stdout);
83 // Convert input wav to pcm
86 //rlm: for now we are going to going to just have 2 files with "common names"
88 generate_pcm("input.wav","input.pcm");
89 //generate_pcm("input1.wav", "input1.pcm");
92 //Send data to the machine here.
93 //rlm: two files
94 inputFile = fopen("input.pcm","r");
95 // inputFile = fopen("input1.pcm", "r");
97 //assert(inputFile1);
98 assert(inputFile);
100 int count = 0;
102 printf("main:PROCESSOR about to enter loop %d\n", count);
104 //rlm: here we read both files. later refactor into a function.
105 // also, this will terminate when the FIRST file reaches its end.
106 while( fread(&sample, 2, 1, inputFile)) //&& (fread(&sample1, 2 , 1, inputFile1)))
107 { /*
108 printf("hi\n");
109 if (!coreFin && (result = clientStub->ReadCPUToHost(0)) != 1)
110 {
111 sleepCount++;
112 if(sleepCount == 200) {
113 printf("Failed to get response from hardware, bailing\n\n");
114 printf("This means that either your hardware is hanging\n");
115 printf("or that the software hasn't given it enough time\n");
116 printf("to complete. If you think it needs more time, then\n");
117 printf("edit CONNECTED_APPLICATION_CLASS::Main() in ProcessorSystem.cpp\n");
118 printf("(connected_application)\n");
119 }
120 }
121 else if (!coreFin && result == 1)
122 {
123 printf("\n***SOFT_CORE PASSED***\n");
124 coreFin = true;
125 }
126 */
128 if(count%1000 == 0)
129 printf("main: %d\n", count);
130 count++;
131 sem_wait(&throttle);
133 printf("sending file\n");
134 //rlm: two files.
135 // clientStub->SendUnprocessedStream( Data,(UINT32)sample);
136 clientStub->SendUnprocessedStream((UINT32)channel0 , Data,(UINT32)sample);
137 //clientStub->SendUnprocessedStream(channel1 , Data,(UINT32)sample1);
139 }
141 printf("main: out of loop\n");
143 // Need to put lock here to prevent potential race condition
144 pthread_mutex_lock(&lock);
146 //rlm: have to send end-files to both voices.
147 // all of these operations wil eventually be moved into functions.
148 //clientStub->SendUnprocessedStream(EndOfFile,0);
149 clientStub->SendUnprocessedStream((UINT32)channel0, EndOfFile,0);
150 // clientStub->SendUnprocessedStream(channel1, EndOfFile,0);
152 printf("main: wait for end of file\n");
154 pthread_cond_wait(&cond, &lock);
155 pthread_mutex_unlock(&lock);
157 printf("main: last data out\n");
159 // Convert input wav to pcm
160 // this part is ok because there is always only one input file.
161 generate_wav("out_hw.pcm","input.wav","out_hw.wav");
163 printf("generate wav done\n");
165 printf("If you missed it, core %s", coreFin ? "PASSED\n" : "FAILED\n");
166 // Dump the stats file
168 STATS_DEVICE_SERVER_CLASS::GetInstance()->DumpStats();
169 STATS_DEVICE_SERVER_CLASS::GetInstance()->EmitFile();
171 fflush(stdout);
172 exit(0);
173 }