1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <unistd.h>
5 #include <sys/mman.h>
6 #include <sys/ioctl.h>
7 #include <sys/time.h>
8 #include <fcntl.h>
9 #include <errno.h>
10 
11 #include "940shared.h"
12 #include "../gp2x.h"
13 //#include "emu.h"
14 //#include "menu.h"
15 #include "../asmutils.h"
16 #include "../helix/pub/mp3dec.h"
17 
18 /* we will need some gp2x internals here */
19 extern volatile unsigned short *gp2x_memregs; /* from minimal library rlyeh */
20 extern volatile unsigned long  *gp2x_memregl;
21 
22 static unsigned char *shared_mem = 0;
23 static _940_data_t *shared_data = 0;
24 static _940_ctl_t *shared_ctl = 0;
25 static unsigned char *mp3_mem = 0;
26 
27 #define MP3_SIZE_MAX (0x1000000 - 4*640*480)
28 
29 int crashed_940 = 0;
30 
31 
32 /***********************************************************/
33 
34 #define MAXOUT		(+32767)
35 #define MINOUT		(-32768)
36 
37 /* limitter */
38 #define Limit(val, max,min) { \
39 	if ( val > max )      val = max; \
40 	else if ( val < min ) val = min; \
41 }
42 
43 
wait_busy_940(void)44 void wait_busy_940(void)
45 {
46 	int i;
47 #if 0
48 	printf("940 busy, entering wait loop.. (cnt: %i, wc: %i, ve: ", shared_ctl->loopc, shared_ctl->waitc);
49 	for (i = 0; i < 8; i++)
50 		printf("%i ", shared_ctl->vstarts[i]);
51 	printf(")\n");
52 
53 	for (i = 0; shared_ctl->busy; i++)
54 	{
55 		spend_cycles(1024); /* needs tuning */
56 	}
57 	printf("wait iterations: %i\n", i);
58 #else
59 	for (i = 0; shared_ctl->busy && i < 0x10000; i++)
60 		spend_cycles(8*1024);
61 	if (i < 0x10000) return;
62 
63 	/* 940 crashed */
64 	printf("940 crashed (cnt: %i, ve: ", shared_ctl->loopc);
65 	for (i = 0; i < 8; i++)
66 		printf("%i ", shared_ctl->vstarts[i]);
67 	printf(")\n");
68 	crashed_940 = 1;
69 #endif
70 }
71 
72 
add_job_940(int job0,int job1)73 void add_job_940(int job0, int job1)
74 {
75 	shared_ctl->jobs[0] = job0;
76 	shared_ctl->jobs[1] = job1;
77 	shared_ctl->busy = 1;
78 	gp2x_memregs[0x3B3E>>1] = 0xffff; // cause an IRQ for 940
79 }
80 
81 
read_to_upper(void * dest,void * tmpbuf,int tmpsize,FILE * f)82 static int read_to_upper(void *dest, void *tmpbuf, int tmpsize, FILE *f)
83 {
84 	int nRead, nLen = 0;
85 
86 	while(1)
87 	{
88 		nRead = fread(tmpbuf, 1, tmpsize, f);
89 		if(nRead <= 0)
90 			break;
91 		memcpy((unsigned char *)dest + nLen, tmpbuf, nRead);
92 		nLen += nRead;
93 	}
94 
95 	return nLen;
96 }
97 
simpleWait(int thissec,int lim_time)98 static void simpleWait(int thissec, int lim_time)
99 {
100 	struct timeval tval;
101 
102 	spend_cycles(1024);
103 	gettimeofday(&tval, 0);
104 	if(thissec != tval.tv_sec) tval.tv_usec+=1000000;
105 
106 	while(tval.tv_usec < lim_time)
107 	{
108 		spend_cycles(1024);
109 		gettimeofday(&tval, 0);
110 		if(thissec != tval.tv_sec) tval.tv_usec+=1000000;
111 	}
112 }
113 
114 
115 char **g_argv;
116 
117 /* none of the functions in this file should be called before this one */
YM2612Init_940(int baseclock,int rate)118 void YM2612Init_940(int baseclock, int rate)
119 {
120 	printf("YM2612Init_940()\n");
121 	printf("Mem usage: shared_data: %i, shared_ctl: %i\n", sizeof(*shared_data), sizeof(*shared_ctl));
122 
123 	Reset940(1, 2);
124 	Pause940(1);
125 
126 	gp2x_memregs[0x3B46>>1] = 0xffff; // clear pending DUALCPU interrupts for 940
127 	gp2x_memregs[0x3B42>>1] = 0xffff; // enable DUALCPU interrupts for 940
128 
129 	gp2x_memregl[0x4508>>2] = ~(1<<26); // unmask DUALCPU ints in the undocumented 940's interrupt controller
130 
131 	if (shared_mem == NULL)
132 	{
133 		shared_mem = (unsigned char *) mmap(0, 0x210000, PROT_READ|PROT_WRITE, MAP_SHARED, memdev, 0x2000000);
134 		if(shared_mem == MAP_FAILED)
135 		{
136 			printf("mmap(shared_data) failed with %i\n", errno);
137 			exit(1);
138 		}
139 		shared_data = (_940_data_t *) (shared_mem+0x100000);
140 		/* this area must not get buffered on either side */
141 		shared_ctl =  (_940_ctl_t *)  (shared_mem+0x200000);
142 		mp3_mem = (unsigned char *) mmap(0, MP3_SIZE_MAX, PROT_READ|PROT_WRITE, MAP_SHARED, memdev, 0x3000000);
143 		if (mp3_mem == MAP_FAILED)
144 		{
145 			printf("mmap(mp3_mem) failed with %i\n", errno);
146 			exit(1);
147 		}
148 		crashed_940 = 1;
149 	}
150 
151 	if (crashed_940)
152 	{
153 		unsigned char ucData[1024];
154 		int i;
155 		char binpath[1024];
156 		FILE *fp;
157 
158 		strncpy(binpath, g_argv[0], 1023);
159 		binpath[1023] = 0;
160 		for (i = strlen(binpath); i > 0; i--)
161 			if (binpath[i] == '/') { binpath[i] = 0; break; }
162 		strcat(binpath, "/code940.bin");
163 
164 		fp = fopen(binpath, "rb");
165 		if(!fp)
166 		{
167 			printf("failed to open %s\n", binpath);
168 			exit(1);
169 		}
170 
171 		read_to_upper(shared_mem, ucData, sizeof(ucData), fp);
172 		fclose(fp);
173 		crashed_940 = 0;
174 	}
175 
176 	memset(shared_data, 0, sizeof(*shared_data));
177 	memset(shared_ctl,  0, sizeof(*shared_ctl));
178 
179 	/* now cause 940 to init it's ym2612 stuff */
180 	shared_ctl->baseclock = baseclock;
181 	shared_ctl->rate = rate;
182 	shared_ctl->jobs[0] = JOB940_INITALL;
183 	shared_ctl->jobs[1] = 0;
184 	shared_ctl->busy = 1;
185 
186 	/* start the 940 */
187 	Reset940(0, 2);
188 	Pause940(0);
189 }
190 
191 
192 unsigned char *mp3_data = 0;
193 
local_decode(void)194 void local_decode(void)
195 {
196 	int mp3_offs = shared_ctl->mp3_offs;
197 	unsigned char *readPtr = mp3_data + mp3_offs;
198 	int bytesLeft = shared_ctl->mp3_len - mp3_offs;
199 	int offset; // frame offset from readPtr
200 	int err = 0;
201 
202 	if (bytesLeft <= 0) return; // EOF, nothing to do
203 
204 	offset = MP3FindSyncWord(readPtr, bytesLeft);
205 	if (offset < 0) {
206 		shared_ctl->mp3_offs = shared_ctl->mp3_len;
207 		return; // EOF
208 	}
209 	readPtr += offset;
210 	bytesLeft -= offset;
211 
212 	err = MP3Decode(shared_data->mp3dec, &readPtr, &bytesLeft,
213 			shared_data->mp3_buffer[shared_ctl->mp3_buffsel], 0);
214 	if (err) {
215 		if (err == ERR_MP3_INDATA_UNDERFLOW) {
216 			shared_ctl->mp3_offs = shared_ctl->mp3_len; // EOF
217 			return;
218 		} else if (err <= -6 && err >= -12) {
219 			// ERR_MP3_INVALID_FRAMEHEADER, ERR_MP3_INVALID_*
220 			// just try to skip the offending frame..
221 			readPtr++;
222 		}
223 		shared_ctl->mp3_errors++;
224 		shared_ctl->mp3_lasterr = err;
225 	}
226 	shared_ctl->mp3_offs = readPtr - mp3_data;
227 }
228 
229 
230 void gp2x_sound_sync(void);
231 
232 #define USE_LOCAL 0
233 #define BENCHMARK 0
234 
main(int argc,char * argv[])235 int main(int argc, char *argv[])
236 {
237 	FILE *f;
238 	int size;
239 	struct timeval tval; // timing
240 	int thissec = 0, fps = 0;
241 	int target_frametime, frame_samples, samples_ready, mp3_buffer_offs, play_bufsel;
242 	unsigned char play_buffer[44100/50*2*2];
243 
244 	if (argc != 2) {
245 		printf("usage: %s <mp3file>\n", argv[0]);
246 		return 1;
247 	}
248 
249 	g_argv = argv;
250 
251 	gp2x_init();
252 	YM2612Init_940(123, 44100);
253 
254 	// load a mp3
255 	f = fopen(argv[1], "rb");
256 	if (!f) {
257 		printf("can't open %s\n", argv[1]);
258 		return 1;
259 	}
260 
261 	fseek(f, 0, SEEK_END);
262 	size = (int) ftell(f);
263 	if (size > MP3_SIZE_MAX) {
264 		printf("size %i > %i\n", size, MP3_SIZE_MAX);
265 		size = MP3_SIZE_MAX;
266 	}
267 
268 	fseek(f, 0, SEEK_SET);
269 	if (fread(mp3_mem, 1, size, f) != size) {
270 		printf("read failed, errno=%i\n", errno);
271 		fclose(f);
272 		exit(1);
273 	}
274 	fclose(f);
275 	shared_ctl->mp3_len = size;
276 
277 #if USE_LOCAL
278 	shared_data->mp3dec = MP3InitDecoder();
279 	mp3_data = malloc(size);
280 	printf("init: dec: %p ptr: %p\n", shared_data->mp3dec, mp3_data);
281 	if (!mp3_data) {
282 		printf("low mem\n");
283 		exit(1);
284 	}
285 	memcpy(mp3_data, mp3_mem, size);
286 #else
287 	//printf("YM2612UpdateOne_940()\n");
288 	if (shared_ctl->busy) wait_busy_940();
289 #endif
290 
291 	gp2x_start_sound(44100, 16, 1);
292 
293 	#define DESIRED_FPS 50
294 	target_frametime = 1000000/DESIRED_FPS;
295 	frame_samples = 44100/DESIRED_FPS;
296 	samples_ready = mp3_buffer_offs = 0;
297 	play_bufsel = 1;
298 
299 	for (;; fps++)
300 	{
301 		int lim_time;
302 
303 		gettimeofday(&tval, 0);
304 		if (tval.tv_sec != thissec)
305 		{
306 			printf("fps: %i\n", fps);
307 			thissec = tval.tv_sec;
308 			fps = 0;
309 #if BENCHMARK
310 			shared_ctl->mp3_offs = 0;
311 #endif
312 		}
313 
314 #if 0
315 		// decode
316 #if USE_LOCAL
317 		shared_ctl->mp3_buffsel ^= 1;
318 		local_decode();
319 #else
320 		wait_busy_940();
321 		shared_ctl->mp3_buffsel ^= 1;
322 		add_job_940(JOB940_MP3DECODE, 0);
323 #endif
324 
325 		if (shared_ctl->mp3_lasterr) {
326 			printf("mp3_lasterr #%i: %i size: %i offs: %i\n", shared_ctl->mp3_errors, shared_ctl->mp3_lasterr,
327 				shared_ctl->mp3_len, shared_ctl->mp3_offs);
328 			printf("loopc: %i bytes: %08x\n",
329 				shared_ctl->loopc, *(int *)(mp3_mem+shared_ctl->mp3_offs));
330 			shared_ctl->mp3_lasterr = 0;
331 		}
332 
333 #if !BENCHMARK
334 		// play
335 		gp2x_sound_sync();
336 		gp2x_sound_write(shared_data->mp3_buffer[shared_ctl->mp3_buffsel^1], 1152*2*2);
337 #endif
338 #else
339 		lim_time = (fps+1) * target_frametime;
340 
341 		wait_busy_940();
342 
343 		// decode, play
344 		if (samples_ready >= frame_samples) {
345 			if (1152 - mp3_buffer_offs >= frame_samples) {
346 				memcpy(play_buffer, shared_data->mp3_buffer[play_bufsel] + mp3_buffer_offs*2,
347 					frame_samples*2*2);
348 				mp3_buffer_offs += frame_samples;
349 			} else {
350 				// collect from both buffers..
351 				int left = 1152 - mp3_buffer_offs;
352 				memcpy(play_buffer, shared_data->mp3_buffer[play_bufsel] + mp3_buffer_offs*2,
353 					left*2*2);
354 				play_bufsel ^= 1;
355 				mp3_buffer_offs = frame_samples - left;
356 				memcpy(play_buffer + left*2*2, shared_data->mp3_buffer[play_bufsel],
357 					mp3_buffer_offs*2*2);
358 			}
359 			gp2x_sound_write(play_buffer, frame_samples*2*2);
360 			samples_ready -= frame_samples;
361 		}
362 
363 		// make sure we will have enough samples next frame
364 		if (samples_ready < frame_samples) {
365 //			wait_busy_940();
366 			shared_ctl->mp3_buffsel ^= 1;
367 			add_job_940(JOB940_MP3DECODE, 0);
368 			samples_ready += 1152;
369 		}
370 
371 		gettimeofday(&tval, 0);
372 		if(thissec != tval.tv_sec) tval.tv_usec+=1000000;
373 		if(tval.tv_usec < lim_time)
374 		{
375 			// we are too fast
376 			simpleWait(thissec, lim_time);
377 		}
378 #endif
379 	}
380 
381 	return 0;
382 }
383 
384