comparison psg.c @ 380:1c8d74f2ab0b

Make the PSG and YM2612 use the master clock internal with an increment based on clock divider so that they stay perflectly in sync. Run both the PSG and YM2612 whenver one of them needs to be run.
author Mike Pavone <pavone@retrodev.com>
date Mon, 03 Jun 2013 21:43:38 -0700
parents 62177cc39049
children 41c079dbee73
comparison
equal deleted inserted replaced
379:3218e2f8d685 380:1c8d74f2ab0b
1 #include "psg.h" 1 #include "psg.h"
2 #include "render.h" 2 #include "render.h"
3 #include <string.h> 3 #include <string.h>
4 #include <stdlib.h> 4 #include <stdlib.h>
5 5
6 void psg_init(psg_context * context, uint32_t sample_rate, uint32_t clock_rate, uint32_t samples_frame) 6 void psg_init(psg_context * context, uint32_t sample_rate, uint32_t master_clock, uint32_t clock_div, uint32_t samples_frame)
7 { 7 {
8 memset(context, 0, sizeof(*context)); 8 memset(context, 0, sizeof(*context));
9 context->audio_buffer = malloc(sizeof(*context->audio_buffer) * samples_frame); 9 context->audio_buffer = malloc(sizeof(*context->audio_buffer) * samples_frame);
10 context->back_buffer = malloc(sizeof(*context->audio_buffer) * samples_frame); 10 context->back_buffer = malloc(sizeof(*context->audio_buffer) * samples_frame);
11 context->buffer_inc = (double)sample_rate / (double)clock_rate; 11 double clock_rate = (double)master_clock / (double)clock_div;
12 context->buffer_inc = ((double)sample_rate / (double)master_clock) * clock_div;
13 context->clock_inc = clock_div;
12 context->samples_frame = samples_frame; 14 context->samples_frame = samples_frame;
13 for (int i = 0; i < 4; i++) { 15 for (int i = 0; i < 4; i++) {
14 context->volume[i] = 0xF; 16 context->volume[i] = 0xF;
15 } 17 }
16 } 18 }
104 context->audio_buffer[context->buffer_pos++] = acc; 106 context->audio_buffer[context->buffer_pos++] = acc;
105 if (context->buffer_pos == context->samples_frame) { 107 if (context->buffer_pos == context->samples_frame) {
106 render_wait_psg(context); 108 render_wait_psg(context);
107 } 109 }
108 } 110 }
109 context->cycles++; 111 context->cycles += context->clock_inc;
110 } 112 }
111 } 113 }
112 114