comparison psg.c @ 487:c08a4efeee7f opengl

Update opengl branch from default. Fix build breakage unrelated to merge
author Mike Pavone <pavone@retrodev.com>
date Sat, 26 Oct 2013 22:38:47 -0700
parents 3e1573fa22cf
children b7b7a1cab44a
comparison
equal deleted inserted replaced
449:7696d824489d 487:c08a4efeee7f
1 /*
2 Copyright 2013 Michael Pavone
3 This file is part of BlastEm.
4 BlastEm is free software distributed under the terms of the GNU General Public License version 3 or greater. See COPYING for full license text.
5 */
1 #include "psg.h" 6 #include "psg.h"
2 #include "render.h" 7 #include "render.h"
3 #include <string.h> 8 #include <string.h>
4 #include <stdlib.h> 9 #include <stdlib.h>
5 10
6 void psg_init(psg_context * context, uint32_t sample_rate, uint32_t master_clock, uint32_t clock_div, uint32_t samples_frame) 11 void psg_init(psg_context * context, uint32_t sample_rate, uint32_t master_clock, uint32_t clock_div, uint32_t samples_frame)
7 { 12 {
8 memset(context, 0, sizeof(*context)); 13 memset(context, 0, sizeof(*context));
9 context->audio_buffer = malloc(sizeof(*context->audio_buffer) * samples_frame); 14 context->audio_buffer = malloc(sizeof(*context->audio_buffer) * samples_frame);
10 context->back_buffer = malloc(sizeof(*context->audio_buffer) * samples_frame); 15 context->back_buffer = malloc(sizeof(*context->audio_buffer) * samples_frame);
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; 16 context->clock_inc = clock_div;
17 context->sample_rate = sample_rate;
14 context->samples_frame = samples_frame; 18 context->samples_frame = samples_frame;
19 psg_adjust_master_clock(context, master_clock);
15 for (int i = 0; i < 4; i++) { 20 for (int i = 0; i < 4; i++) {
16 context->volume[i] = 0xF; 21 context->volume[i] = 0xF;
17 } 22 }
23 }
24
25 #define BUFFER_INC_RES 1000000000UL
26
27 void psg_adjust_master_clock(psg_context * context, uint32_t master_clock)
28 {
29 uint64_t old_inc = context->buffer_inc;
30 context->buffer_inc = ((BUFFER_INC_RES * (uint64_t)context->sample_rate) / (uint64_t)master_clock) * (uint64_t)context->clock_inc;
18 } 31 }
19 32
20 void psg_write(psg_context * context, uint8_t value) 33 void psg_write(psg_context * context, uint8_t value)
21 { 34 {
22 if (value & 0x80) { 35 if (value & 0x80) {
63 #define PSG_VOL_DIV 6 76 #define PSG_VOL_DIV 6
64 77
65 //table shamelessly swiped from PSG doc from smspower.org 78 //table shamelessly swiped from PSG doc from smspower.org
66 int16_t volume_table[16] = { 79 int16_t volume_table[16] = {
67 32767/PSG_VOL_DIV, 26028/PSG_VOL_DIV, 20675/PSG_VOL_DIV, 16422/PSG_VOL_DIV, 13045/PSG_VOL_DIV, 10362/PSG_VOL_DIV, 80 32767/PSG_VOL_DIV, 26028/PSG_VOL_DIV, 20675/PSG_VOL_DIV, 16422/PSG_VOL_DIV, 13045/PSG_VOL_DIV, 10362/PSG_VOL_DIV,
68 8231/PSG_VOL_DIV, 6568/PSG_VOL_DIV, 5193/PSG_VOL_DIV, 4125/PSG_VOL_DIV, 3277/PSG_VOL_DIV, 2603/PSG_VOL_DIV, 81 8231/PSG_VOL_DIV, 6568/PSG_VOL_DIV, 5193/PSG_VOL_DIV, 4125/PSG_VOL_DIV, 3277/PSG_VOL_DIV, 2603/PSG_VOL_DIV,
69 2067/PSG_VOL_DIV, 1642/PSG_VOL_DIV, 1304/PSG_VOL_DIV, 0 82 2067/PSG_VOL_DIV, 1642/PSG_VOL_DIV, 1304/PSG_VOL_DIV, 0
70 }; 83 };
71 84
72 void psg_run(psg_context * context, uint32_t cycles) 85 void psg_run(psg_context * context, uint32_t cycles)
73 { 86 {
90 } 103 }
91 } 104 }
92 } 105 }
93 } 106 }
94 context->buffer_fraction += context->buffer_inc; 107 context->buffer_fraction += context->buffer_inc;
95 if (context->buffer_fraction >= 1.0) { 108 if (context->buffer_fraction >= BUFFER_INC_RES) {
96 context->buffer_fraction -= 1.0; 109 context->buffer_fraction -= BUFFER_INC_RES;
97 int16_t acc = 0; 110 int16_t acc = 0;
98 for (int i = 0; i < 3; i++) { 111 for (int i = 0; i < 3; i++) {
99 if (context->output_state[i]) { 112 if (context->output_state[i]) {
100 acc += volume_table[context->volume[i]]; 113 acc += volume_table[context->volume[i]];
101 } 114 }