comparison render_sdl.c @ 364:62177cc39049

Incredibly broken YM2612 support plus a fix to Z80 bus request
author Mike Pavone <pavone@retrodev.com>
date Wed, 29 May 2013 00:57:19 -0700
parents 946ae3749260
children c26e48a93fa3
comparison
equal deleted inserted replaced
363:c708dea45f8b 364:62177cc39049
14 uint8_t levels[] = {0, 27, 49, 71, 87, 103, 119, 130, 146, 157, 174, 190, 206, 228, 255}; 14 uint8_t levels[] = {0, 27, 49, 71, 87, 103, 119, 130, 146, 157, 174, 190, 206, 228, 255};
15 15
16 uint32_t min_delay; 16 uint32_t min_delay;
17 uint32_t frame_delay = 1000/60; 17 uint32_t frame_delay = 1000/60;
18 18
19 int16_t * current_audio = NULL; 19 int16_t * current_psg = NULL;
20 int16_t * next_audio = NULL; 20 int16_t * current_ym = NULL;
21 21
22 uint32_t buffer_samples, sample_rate; 22 uint32_t buffer_samples, sample_rate;
23 uint32_t missing_count; 23 uint32_t missing_count;
24 24
25 SDL_mutex * audio_mutex; 25 SDL_mutex * audio_mutex;
26 SDL_cond * audio_ready; 26 SDL_cond * audio_ready;
27 SDL_cond * audio_cond; 27 SDL_cond * psg_cond;
28 SDL_cond * ym_cond;
28 uint8_t quitting = 0; 29 uint8_t quitting = 0;
29 30
30 void audio_callback(void * userdata, uint8_t *byte_stream, int len) 31 void audio_callback(void * userdata, uint8_t *byte_stream, int len)
31 { 32 {
32 //puts("audio_callback"); 33 //puts("audio_callback");
33 int16_t * stream = (int16_t *)byte_stream; 34 int16_t * stream = (int16_t *)byte_stream;
34 int samples = len/(sizeof(int16_t)*2); 35 int samples = len/(sizeof(int16_t)*2);
35 int16_t * source_buf; 36 int16_t * psg_buf, * ym_buf;
36 uint8_t local_quit; 37 uint8_t local_quit;
37 SDL_LockMutex(audio_mutex); 38 SDL_LockMutex(audio_mutex);
38 while (!current_audio && !quitting) { 39 psg_buf = NULL;
39 SDL_CondWait(audio_ready, audio_mutex); 40 ym_buf = NULL;
40 } 41 do {
42 if (!psg_buf) {
43 psg_buf = current_psg;
44 current_psg = NULL;
45 SDL_CondSignal(psg_cond);
46 }
47 if (!ym_buf) {
48 ym_buf = current_ym;
49 current_ym = NULL;
50 SDL_CondSignal(ym_cond);
51 }
52 if (!quitting && (!psg_buf || !ym_buf)) {
53 SDL_CondWait(audio_ready, audio_mutex);
54 }
55 } while(!quitting && (!psg_buf || !ym_buf));
56
41 local_quit = quitting; 57 local_quit = quitting;
42 source_buf = current_audio;
43 current_audio = NULL;
44 SDL_CondSignal(audio_cond);
45 SDL_UnlockMutex(audio_mutex); 58 SDL_UnlockMutex(audio_mutex);
46 if (!local_quit) { 59 if (!local_quit) {
47 for (int i = 0; i < samples; i++) { 60 for (int i = 0; i < samples; i++) {
48 *(stream++) = source_buf[i]; 61 *(stream++) = psg_buf[i] + *(ym_buf++);
49 *(stream++) = source_buf[i]; 62 *(stream++) = psg_buf[i] + *(ym_buf++);
50 } 63 }
51 } 64 }
52 } 65 }
53 66
54 void render_close_audio() 67 void render_close_audio()
111 printf("minimum delay: %d\n", min_delay); 124 printf("minimum delay: %d\n", min_delay);
112 125
113 frame_delay = 1000/fps; 126 frame_delay = 1000/fps;
114 127
115 audio_mutex = SDL_CreateMutex(); 128 audio_mutex = SDL_CreateMutex();
116 audio_cond = SDL_CreateCond(); 129 psg_cond = SDL_CreateCond();
130 ym_cond = SDL_CreateCond();
117 audio_ready = SDL_CreateCond(); 131 audio_ready = SDL_CreateCond();
118 132
119 SDL_AudioSpec desired, actual; 133 SDL_AudioSpec desired, actual;
120 desired.freq = 48000; 134 desired.freq = 48000;
121 desired.format = AUDIO_S16SYS; 135 desired.format = AUDIO_S16SYS;
458 frame_counter = 0; 472 frame_counter = 0;
459 }*/ 473 }*/
460 return ret; 474 return ret;
461 } 475 }
462 476
463 void render_wait_audio(psg_context * context) 477 void render_wait_psg(psg_context * context)
464 { 478 {
465 SDL_LockMutex(audio_mutex); 479 SDL_LockMutex(audio_mutex);
466 while (current_audio != NULL) { 480 while (current_psg != NULL) {
467 SDL_CondWait(audio_cond, audio_mutex); 481 SDL_CondWait(psg_cond, audio_mutex);
468 } 482 }
469 current_audio = context->audio_buffer; 483 current_psg = context->audio_buffer;
470 SDL_CondSignal(audio_ready); 484 SDL_CondSignal(audio_ready);
471 485
472 context->audio_buffer = context->back_buffer; 486 context->audio_buffer = context->back_buffer;
473 context->back_buffer = current_audio; 487 context->back_buffer = current_psg;
474 SDL_UnlockMutex(audio_mutex); 488 SDL_UnlockMutex(audio_mutex);
475 context->buffer_pos = 0; 489 context->buffer_pos = 0;
476 } 490 }
477 491
492 void render_wait_ym(ym2612_context * context)
493 {
494 SDL_LockMutex(audio_mutex);
495 while (current_ym != NULL) {
496 SDL_CondWait(ym_cond, audio_mutex);
497 }
498 current_ym = context->audio_buffer;
499 SDL_CondSignal(audio_ready);
500
501 context->audio_buffer = context->back_buffer;
502 context->back_buffer = current_ym;
503 SDL_UnlockMutex(audio_mutex);
504 context->buffer_pos = 0;
505 }
506
478 void render_fps(uint32_t fps) 507 void render_fps(uint32_t fps)
479 { 508 {
480 frame_delay = 1000/fps; 509 frame_delay = 1000/fps;
481 } 510 }
482 511