view modules/bluespec/Pygar/core/AudioCoreSystem.cpp @ 63:1d5cbb5343d2 pygar svn.64

[svn r64] mods to compile correctly for FPGA
author punk
date Mon, 10 May 2010 22:54:54 -0400
parents b5be746a0d74
children bf08daea854e
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 //This isn't called.
43 void
44 CONNECTED_APPLICATION_CLASS::UpdateSemaphore()
45 {
46 sem_post(&throttle);
47 }
49 void
50 CONNECTED_APPLICATION_CLASS::EndSimulation()
51 {
52 printf("EndSimulation Called\n");
53 fflush(stdout);
54 pthread_mutex_lock(&lock);
55 // Do something about the race occuring here
56 pthread_cond_signal(&cond);
57 pthread_mutex_unlock(&lock);
58 printf("EndSimulation done\n");
59 fflush(stdout);
60 }
62 // main
63 void
64 CONNECTED_APPLICATION_CLASS::Main()
65 {
66 //rlm: two files
67 FILE *inputFile;
68 FILE *inputFile1;
69 UINT16 sample;
70 UINT16 sample1;
72 //rlm: not sure if normal ints are ok here; using UINT16 because I know it works
73 UINT16 channel0 = 0;
74 UINT16 channel1 = 1;
76 //init processor
77 int sleepCount = 0;
78 int result = 0;
80 bool coreFin = false;
82 fflush(stdout);
84 // 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 inputFile1 = fopen("input1.pcm", "r");
97 assert(inputFile1);
98 assert(inputFile);
100 int count = 0;
102 printf("main:samples about to start sending %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 }
113 else if (!coreFin && result == 1)
114 {
115 printf("\n***SOFT_CORE PASSED***\n");
116 coreFin = true;
117 }
118 */
120 if(count%1000 == 0)
121 printf("main: %d\n", count);
122 count++;
124 //rlm: two files.
125 // clientStub->SendUnprocessedStream( Data,(UINT32)sample);
126 clientStub->SendUnprocessedStream((UINT32)channel0 , Data,(UINT32)sample);
127 clientStub->SendUnprocessedStream((UINT32)channel1 , Data,(UINT32)sample1);
128 }
130 printf("main: out of loop\n");
132 // Need to put lock here to prevent potential race condition
133 pthread_mutex_lock(&lock);
135 //rlm: have to send end-files to both voices.
136 // all of these operations wil eventually be moved into functions.
137 //clientStub->SendUnprocessedStream(EndOfFile,0);
138 clientStub->SendUnprocessedStream((UINT32)channel0, EndOfFile,0);
139 clientStub->SendUnprocessedStream((UINT32)channel1, EndOfFile,0);
141 printf("main: wait for end of file\n");
143 pthread_cond_wait(&cond, &lock);
144 pthread_mutex_unlock(&lock);
146 printf("main: last data out\n");
148 // Convert input wav to pcm
149 // this part is ok because there is always only one input file.
150 generate_wav("out_hw.pcm","input.wav","out_hw.wav");
152 printf("generate wav done\n");
154 // printf("If you missed it, core %s", coreFin ? "PASSED\n" : "FAILED\n");
155 // Dump the stats file
157 STATS_DEVICE_SERVER_CLASS::GetInstance()->DumpStats();
158 STATS_DEVICE_SERVER_CLASS::GetInstance()->EmitFile();
160 fflush(stdout);
161 // exit(0);
162 }