annotate psg.c @ 1551:ce1f93be0104

Small cleanup to audio interface between emulation code and renderer backend
author Michael Pavone <pavone@retrodev.com>
date Wed, 28 Mar 2018 23:36:08 -0700
parents 9c65819afec3
children 6ce36c3f250b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
467
140af5509ce7 Added copyright notice to source files and added GPL license text in COPYING
Mike Pavone <pavone@retrodev.com>
parents: 410
diff changeset
1 /*
140af5509ce7 Added copyright notice to source files and added GPL license text in COPYING
Mike Pavone <pavone@retrodev.com>
parents: 410
diff changeset
2 Copyright 2013 Michael Pavone
483
3e1573fa22cf Implement turbo/slow motion feature that overclocks or underclocks the entire system at the push of a button
Mike Pavone <pavone@retrodev.com>
parents: 467
diff changeset
3 This file is part of BlastEm.
467
140af5509ce7 Added copyright notice to source files and added GPL license text in COPYING
Mike Pavone <pavone@retrodev.com>
parents: 410
diff changeset
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.
140af5509ce7 Added copyright notice to source files and added GPL license text in COPYING
Mike Pavone <pavone@retrodev.com>
parents: 410
diff changeset
5 */
354
15dd6418fe67 Initial PSG support. Mostly works, noise channel is borked though.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
6 #include "psg.h"
15dd6418fe67 Initial PSG support. Mostly works, noise channel is borked though.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
7 #include "render.h"
505
b7b7a1cab44a The local clone on my laptop got messed up and some changes had not been pushed. This commit represents the status of the working copy from that clone. It unfortunately contains some changes that I did not intend to commit yet, but this seems like the best option at the moment.
Michael Pavone <pavone@retrodev.com>
parents: 483
diff changeset
8 #include "blastem.h"
354
15dd6418fe67 Initial PSG support. Mostly works, noise channel is borked though.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
9 #include <string.h>
15dd6418fe67 Initial PSG support. Mostly works, noise channel is borked though.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
10 #include <stdlib.h>
838
9a5dc22297f2 Somewhat better handling of high frequency PSG tones. Needs work to fully handle case where frequency > half our output sample rate
Michael Pavone <pavone@retrodev.com>
parents: 522
diff changeset
11 #include <stdio.h>
964
e6dc30231b83 Fix PSG linear resampling and implement a low pass filter
Michael Pavone <pavone@retrodev.com>
parents: 963
diff changeset
12 #include <math.h>
1002
8d032a368dd5 Made low pass filter frequency configurable
Michael Pavone <pavone@retrodev.com>
parents: 964
diff changeset
13 void psg_init(psg_context * context, uint32_t sample_rate, uint32_t master_clock, uint32_t clock_div, uint32_t samples_frame, uint32_t lowpass_cutoff)
354
15dd6418fe67 Initial PSG support. Mostly works, noise channel is borked though.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
14 {
15dd6418fe67 Initial PSG support. Mostly works, noise channel is borked though.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
15 memset(context, 0, sizeof(*context));
1551
ce1f93be0104 Small cleanup to audio interface between emulation code and renderer backend
Michael Pavone <pavone@retrodev.com>
parents: 1451
diff changeset
16 context->audio = render_audio_source(1);
ce1f93be0104 Small cleanup to audio interface between emulation code and renderer backend
Michael Pavone <pavone@retrodev.com>
parents: 1451
diff changeset
17 context->audio_buffer = render_audio_source_buffer(context->audio);
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.
Mike Pavone <pavone@retrodev.com>
parents: 364
diff changeset
18 context->clock_inc = clock_div;
483
3e1573fa22cf Implement turbo/slow motion feature that overclocks or underclocks the entire system at the push of a button
Mike Pavone <pavone@retrodev.com>
parents: 467
diff changeset
19 context->sample_rate = sample_rate;
354
15dd6418fe67 Initial PSG support. Mostly works, noise channel is borked though.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
20 context->samples_frame = samples_frame;
1002
8d032a368dd5 Made low pass filter frequency configurable
Michael Pavone <pavone@retrodev.com>
parents: 964
diff changeset
21 double rc = (1.0 / (double)lowpass_cutoff) / (2.0 * M_PI);
964
e6dc30231b83 Fix PSG linear resampling and implement a low pass filter
Michael Pavone <pavone@retrodev.com>
parents: 963
diff changeset
22 double dt = 1.0 / ((double)master_clock / (double)clock_div);
e6dc30231b83 Fix PSG linear resampling and implement a low pass filter
Michael Pavone <pavone@retrodev.com>
parents: 963
diff changeset
23 double alpha = dt / (dt + rc);
e6dc30231b83 Fix PSG linear resampling and implement a low pass filter
Michael Pavone <pavone@retrodev.com>
parents: 963
diff changeset
24 context->lowpass_alpha = (int32_t)(((double)0x10000) * alpha);
483
3e1573fa22cf Implement turbo/slow motion feature that overclocks or underclocks the entire system at the push of a button
Mike Pavone <pavone@retrodev.com>
parents: 467
diff changeset
25 psg_adjust_master_clock(context, master_clock);
354
15dd6418fe67 Initial PSG support. Mostly works, noise channel is borked though.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
26 for (int i = 0; i < 4; i++) {
15dd6418fe67 Initial PSG support. Mostly works, noise channel is borked though.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
27 context->volume[i] = 0xF;
15dd6418fe67 Initial PSG support. Mostly works, noise channel is borked though.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
28 }
15dd6418fe67 Initial PSG support. Mostly works, noise channel is borked though.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
29 }
15dd6418fe67 Initial PSG support. Mostly works, noise channel is borked though.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
30
884
252dfd29831d Selecting a second game from the menu now works
Michael Pavone <pavone@retrodev.com>
parents: 843
diff changeset
31 void psg_free(psg_context *context)
252dfd29831d Selecting a second game from the menu now works
Michael Pavone <pavone@retrodev.com>
parents: 843
diff changeset
32 {
1551
ce1f93be0104 Small cleanup to audio interface between emulation code and renderer backend
Michael Pavone <pavone@retrodev.com>
parents: 1451
diff changeset
33 render_free_source(context->audio);
884
252dfd29831d Selecting a second game from the menu now works
Michael Pavone <pavone@retrodev.com>
parents: 843
diff changeset
34 free(context);
252dfd29831d Selecting a second game from the menu now works
Michael Pavone <pavone@retrodev.com>
parents: 843
diff changeset
35 }
252dfd29831d Selecting a second game from the menu now works
Michael Pavone <pavone@retrodev.com>
parents: 843
diff changeset
36
963
bd549b25c362 Not so successful attempt at improved PSG resampling
Michael Pavone <pavone@retrodev.com>
parents: 884
diff changeset
37 #define BUFFER_INC_RES 0x40000000UL
483
3e1573fa22cf Implement turbo/slow motion feature that overclocks or underclocks the entire system at the push of a button
Mike Pavone <pavone@retrodev.com>
parents: 467
diff changeset
38
3e1573fa22cf Implement turbo/slow motion feature that overclocks or underclocks the entire system at the push of a button
Mike Pavone <pavone@retrodev.com>
parents: 467
diff changeset
39 void psg_adjust_master_clock(psg_context * context, uint32_t master_clock)
3e1573fa22cf Implement turbo/slow motion feature that overclocks or underclocks the entire system at the push of a button
Mike Pavone <pavone@retrodev.com>
parents: 467
diff changeset
40 {
3e1573fa22cf Implement turbo/slow motion feature that overclocks or underclocks the entire system at the push of a button
Mike Pavone <pavone@retrodev.com>
parents: 467
diff changeset
41 uint64_t old_inc = context->buffer_inc;
3e1573fa22cf Implement turbo/slow motion feature that overclocks or underclocks the entire system at the push of a button
Mike Pavone <pavone@retrodev.com>
parents: 467
diff changeset
42 context->buffer_inc = ((BUFFER_INC_RES * (uint64_t)context->sample_rate) / (uint64_t)master_clock) * (uint64_t)context->clock_inc;
3e1573fa22cf Implement turbo/slow motion feature that overclocks or underclocks the entire system at the push of a button
Mike Pavone <pavone@retrodev.com>
parents: 467
diff changeset
43 }
3e1573fa22cf Implement turbo/slow motion feature that overclocks or underclocks the entire system at the push of a button
Mike Pavone <pavone@retrodev.com>
parents: 467
diff changeset
44
354
15dd6418fe67 Initial PSG support. Mostly works, noise channel is borked though.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
45 void psg_write(psg_context * context, uint8_t value)
15dd6418fe67 Initial PSG support. Mostly works, noise channel is borked though.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
46 {
15dd6418fe67 Initial PSG support. Mostly works, noise channel is borked though.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
47 if (value & 0x80) {
15dd6418fe67 Initial PSG support. Mostly works, noise channel is borked though.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
48 context->latch = value & 0x70;
15dd6418fe67 Initial PSG support. Mostly works, noise channel is borked though.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
49 uint8_t channel = value >> 5 & 0x3;
15dd6418fe67 Initial PSG support. Mostly works, noise channel is borked though.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
50 if (value & 0x10) {
15dd6418fe67 Initial PSG support. Mostly works, noise channel is borked though.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
51 context->volume[channel] = value & 0xF;
15dd6418fe67 Initial PSG support. Mostly works, noise channel is borked though.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
52 } else {
15dd6418fe67 Initial PSG support. Mostly works, noise channel is borked though.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
53 if (channel == 3) {
15dd6418fe67 Initial PSG support. Mostly works, noise channel is borked though.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
54 switch(value & 0x3)
15dd6418fe67 Initial PSG support. Mostly works, noise channel is borked though.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
55 {
15dd6418fe67 Initial PSG support. Mostly works, noise channel is borked though.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
56 case 0:
15dd6418fe67 Initial PSG support. Mostly works, noise channel is borked though.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
57 case 1:
15dd6418fe67 Initial PSG support. Mostly works, noise channel is borked though.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
58 case 2:
15dd6418fe67 Initial PSG support. Mostly works, noise channel is borked though.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
59 context->counter_load[3] = 0x10 << (value & 0x3);
15dd6418fe67 Initial PSG support. Mostly works, noise channel is borked though.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
60 context->noise_use_tone = 0;
15dd6418fe67 Initial PSG support. Mostly works, noise channel is borked though.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
61 break;
15dd6418fe67 Initial PSG support. Mostly works, noise channel is borked though.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
62 default:
15dd6418fe67 Initial PSG support. Mostly works, noise channel is borked though.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
63 context->counter_load[3] = context->counter_load[2];
15dd6418fe67 Initial PSG support. Mostly works, noise channel is borked though.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
64 context->noise_use_tone = 1;
15dd6418fe67 Initial PSG support. Mostly works, noise channel is borked though.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
65 }
15dd6418fe67 Initial PSG support. Mostly works, noise channel is borked though.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
66 context->noise_type = value & 0x4;
15dd6418fe67 Initial PSG support. Mostly works, noise channel is borked though.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
67 context->lsfr = 0x8000;
15dd6418fe67 Initial PSG support. Mostly works, noise channel is borked though.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
68 } else {
15dd6418fe67 Initial PSG support. Mostly works, noise channel is borked though.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
69 context->counter_load[channel] = (context->counter_load[channel] & 0x3F0) | (value & 0xF);
15dd6418fe67 Initial PSG support. Mostly works, noise channel is borked though.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
70 if (channel == 2 && context->noise_use_tone) {
15dd6418fe67 Initial PSG support. Mostly works, noise channel is borked though.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
71 context->counter_load[3] = context->counter_load[2];
15dd6418fe67 Initial PSG support. Mostly works, noise channel is borked though.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
72 }
15dd6418fe67 Initial PSG support. Mostly works, noise channel is borked though.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
73 }
15dd6418fe67 Initial PSG support. Mostly works, noise channel is borked though.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
74 }
15dd6418fe67 Initial PSG support. Mostly works, noise channel is borked though.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
75 } else {
15dd6418fe67 Initial PSG support. Mostly works, noise channel is borked though.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
76 if (!(context->latch & 0x10)) {
15dd6418fe67 Initial PSG support. Mostly works, noise channel is borked though.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
77 uint8_t channel = context->latch >> 5 & 0x3;
15dd6418fe67 Initial PSG support. Mostly works, noise channel is borked though.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
78 if (channel != 3) {
15dd6418fe67 Initial PSG support. Mostly works, noise channel is borked though.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
79 context->counter_load[channel] = (value << 4 & 0x3F0) | (context->counter_load[channel] & 0xF);
15dd6418fe67 Initial PSG support. Mostly works, noise channel is borked though.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
80 if (channel == 2 && context->noise_use_tone) {
15dd6418fe67 Initial PSG support. Mostly works, noise channel is borked though.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
81 context->counter_load[3] = context->counter_load[2];
15dd6418fe67 Initial PSG support. Mostly works, noise channel is borked though.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
82 }
15dd6418fe67 Initial PSG support. Mostly works, noise channel is borked though.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
83 }
15dd6418fe67 Initial PSG support. Mostly works, noise channel is borked though.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
84 }
15dd6418fe67 Initial PSG support. Mostly works, noise channel is borked though.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
85 }
15dd6418fe67 Initial PSG support. Mostly works, noise channel is borked though.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
86 }
15dd6418fe67 Initial PSG support. Mostly works, noise channel is borked though.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
87
522
6a14c5a95648 Adjust PSG and YM-2612 volume to be closer to the real console
Michael Pavone <pavone@retrodev.com>
parents: 505
diff changeset
88 #define PSG_VOL_DIV 14
354
15dd6418fe67 Initial PSG support. Mostly works, noise channel is borked though.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
89
15dd6418fe67 Initial PSG support. Mostly works, noise channel is borked though.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
90 //table shamelessly swiped from PSG doc from smspower.org
1102
c15896605bf2 Clean up symbol visiblity and delete a ltitle bit of dead code
Michael Pavone <pavone@retrodev.com>
parents: 1002
diff changeset
91 static int16_t volume_table[16] = {
354
15dd6418fe67 Initial PSG support. Mostly works, noise channel is borked though.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
92 32767/PSG_VOL_DIV, 26028/PSG_VOL_DIV, 20675/PSG_VOL_DIV, 16422/PSG_VOL_DIV, 13045/PSG_VOL_DIV, 10362/PSG_VOL_DIV,
483
3e1573fa22cf Implement turbo/slow motion feature that overclocks or underclocks the entire system at the push of a button
Mike Pavone <pavone@retrodev.com>
parents: 467
diff changeset
93 8231/PSG_VOL_DIV, 6568/PSG_VOL_DIV, 5193/PSG_VOL_DIV, 4125/PSG_VOL_DIV, 3277/PSG_VOL_DIV, 2603/PSG_VOL_DIV,
354
15dd6418fe67 Initial PSG support. Mostly works, noise channel is borked though.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
94 2067/PSG_VOL_DIV, 1642/PSG_VOL_DIV, 1304/PSG_VOL_DIV, 0
15dd6418fe67 Initial PSG support. Mostly works, noise channel is borked though.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
95 };
15dd6418fe67 Initial PSG support. Mostly works, noise channel is borked though.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
96
15dd6418fe67 Initial PSG support. Mostly works, noise channel is borked though.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
97 void psg_run(psg_context * context, uint32_t cycles)
15dd6418fe67 Initial PSG support. Mostly works, noise channel is borked though.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
98 {
15dd6418fe67 Initial PSG support. Mostly works, noise channel is borked though.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
99 while (context->cycles < cycles) {
15dd6418fe67 Initial PSG support. Mostly works, noise channel is borked though.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
100 for (int i = 0; i < 4; i++) {
15dd6418fe67 Initial PSG support. Mostly works, noise channel is borked though.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
101 if (context->counters[i]) {
15dd6418fe67 Initial PSG support. Mostly works, noise channel is borked though.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
102 context->counters[i] -= 1;
15dd6418fe67 Initial PSG support. Mostly works, noise channel is borked though.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
103 }
15dd6418fe67 Initial PSG support. Mostly works, noise channel is borked though.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
104 if (!context->counters[i]) {
15dd6418fe67 Initial PSG support. Mostly works, noise channel is borked though.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
105 context->counters[i] = context->counter_load[i];
15dd6418fe67 Initial PSG support. Mostly works, noise channel is borked though.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
106 context->output_state[i] = !context->output_state[i];
15dd6418fe67 Initial PSG support. Mostly works, noise channel is borked though.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
107 if (i == 3 && context->output_state[i]) {
15dd6418fe67 Initial PSG support. Mostly works, noise channel is borked though.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
108 context->noise_out = context->lsfr & 1;
15dd6418fe67 Initial PSG support. Mostly works, noise channel is borked though.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
109 context->lsfr = (context->lsfr >> 1) | (context->lsfr << 15);
15dd6418fe67 Initial PSG support. Mostly works, noise channel is borked though.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
110 if (context->noise_type) {
15dd6418fe67 Initial PSG support. Mostly works, noise channel is borked though.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
111 //white noise
15dd6418fe67 Initial PSG support. Mostly works, noise channel is borked though.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
112 if (context->lsfr & 0x40) {
15dd6418fe67 Initial PSG support. Mostly works, noise channel is borked though.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
113 context->lsfr ^= 0x8000;
15dd6418fe67 Initial PSG support. Mostly works, noise channel is borked though.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
114 }
15dd6418fe67 Initial PSG support. Mostly works, noise channel is borked though.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
115 }
15dd6418fe67 Initial PSG support. Mostly works, noise channel is borked though.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
116 }
15dd6418fe67 Initial PSG support. Mostly works, noise channel is borked though.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
117 }
15dd6418fe67 Initial PSG support. Mostly works, noise channel is borked though.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
118 }
838
9a5dc22297f2 Somewhat better handling of high frequency PSG tones. Needs work to fully handle case where frequency > half our output sample rate
Michael Pavone <pavone@retrodev.com>
parents: 522
diff changeset
119
963
bd549b25c362 Not so successful attempt at improved PSG resampling
Michael Pavone <pavone@retrodev.com>
parents: 884
diff changeset
120 context->last_sample = context->accum;
bd549b25c362 Not so successful attempt at improved PSG resampling
Michael Pavone <pavone@retrodev.com>
parents: 884
diff changeset
121 context->accum = 0;
bd549b25c362 Not so successful attempt at improved PSG resampling
Michael Pavone <pavone@retrodev.com>
parents: 884
diff changeset
122
838
9a5dc22297f2 Somewhat better handling of high frequency PSG tones. Needs work to fully handle case where frequency > half our output sample rate
Michael Pavone <pavone@retrodev.com>
parents: 522
diff changeset
123 for (int i = 0; i < 3; i++) {
9a5dc22297f2 Somewhat better handling of high frequency PSG tones. Needs work to fully handle case where frequency > half our output sample rate
Michael Pavone <pavone@retrodev.com>
parents: 522
diff changeset
124 if (context->output_state[i]) {
9a5dc22297f2 Somewhat better handling of high frequency PSG tones. Needs work to fully handle case where frequency > half our output sample rate
Michael Pavone <pavone@retrodev.com>
parents: 522
diff changeset
125 context->accum += volume_table[context->volume[i]];
9a5dc22297f2 Somewhat better handling of high frequency PSG tones. Needs work to fully handle case where frequency > half our output sample rate
Michael Pavone <pavone@retrodev.com>
parents: 522
diff changeset
126 }
9a5dc22297f2 Somewhat better handling of high frequency PSG tones. Needs work to fully handle case where frequency > half our output sample rate
Michael Pavone <pavone@retrodev.com>
parents: 522
diff changeset
127 }
9a5dc22297f2 Somewhat better handling of high frequency PSG tones. Needs work to fully handle case where frequency > half our output sample rate
Michael Pavone <pavone@retrodev.com>
parents: 522
diff changeset
128 if (context->noise_out) {
9a5dc22297f2 Somewhat better handling of high frequency PSG tones. Needs work to fully handle case where frequency > half our output sample rate
Michael Pavone <pavone@retrodev.com>
parents: 522
diff changeset
129 context->accum += volume_table[context->volume[3]];
9a5dc22297f2 Somewhat better handling of high frequency PSG tones. Needs work to fully handle case where frequency > half our output sample rate
Michael Pavone <pavone@retrodev.com>
parents: 522
diff changeset
130 }
964
e6dc30231b83 Fix PSG linear resampling and implement a low pass filter
Michael Pavone <pavone@retrodev.com>
parents: 963
diff changeset
131 int32_t tmp = context->accum * context->lowpass_alpha + context->last_sample * (0x10000 - context->lowpass_alpha);
e6dc30231b83 Fix PSG linear resampling and implement a low pass filter
Michael Pavone <pavone@retrodev.com>
parents: 963
diff changeset
132 context->accum = tmp >> 16;
838
9a5dc22297f2 Somewhat better handling of high frequency PSG tones. Needs work to fully handle case where frequency > half our output sample rate
Michael Pavone <pavone@retrodev.com>
parents: 522
diff changeset
133
354
15dd6418fe67 Initial PSG support. Mostly works, noise channel is borked though.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
134 context->buffer_fraction += context->buffer_inc;
1356
4d16c09210fd Fix resampling code to deal with case in which output frequency is greater than the input frequency. Probably could stand to be improved, but at least it doesn't cause the emulator to deadlock
Michael Pavone <pavone@retrodev.com>
parents: 1102
diff changeset
135 while (context->buffer_fraction >= BUFFER_INC_RES) {
483
3e1573fa22cf Implement turbo/slow motion feature that overclocks or underclocks the entire system at the push of a button
Mike Pavone <pavone@retrodev.com>
parents: 467
diff changeset
136 context->buffer_fraction -= BUFFER_INC_RES;
964
e6dc30231b83 Fix PSG linear resampling and implement a low pass filter
Michael Pavone <pavone@retrodev.com>
parents: 963
diff changeset
137 int32_t tmp = context->last_sample * ((context->buffer_fraction << 16) / context->buffer_inc);
e6dc30231b83 Fix PSG linear resampling and implement a low pass filter
Michael Pavone <pavone@retrodev.com>
parents: 963
diff changeset
138 tmp += context->accum * (0x10000 - ((context->buffer_fraction << 16) / context->buffer_inc));
963
bd549b25c362 Not so successful attempt at improved PSG resampling
Michael Pavone <pavone@retrodev.com>
parents: 884
diff changeset
139 context->audio_buffer[context->buffer_pos++] = tmp >> 16;
964
e6dc30231b83 Fix PSG linear resampling and implement a low pass filter
Michael Pavone <pavone@retrodev.com>
parents: 963
diff changeset
140
354
15dd6418fe67 Initial PSG support. Mostly works, noise channel is borked though.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
141 if (context->buffer_pos == context->samples_frame) {
505
b7b7a1cab44a The local clone on my laptop got messed up and some changes had not been pushed. This commit represents the status of the working copy from that clone. It unfortunately contains some changes that I did not intend to commit yet, but this seems like the best option at the moment.
Michael Pavone <pavone@retrodev.com>
parents: 483
diff changeset
142 if (!headless) {
1551
ce1f93be0104 Small cleanup to audio interface between emulation code and renderer backend
Michael Pavone <pavone@retrodev.com>
parents: 1451
diff changeset
143 context->audio_buffer = render_audio_ready(context->audio);
ce1f93be0104 Small cleanup to audio interface between emulation code and renderer backend
Michael Pavone <pavone@retrodev.com>
parents: 1451
diff changeset
144 context->buffer_pos = 0;
505
b7b7a1cab44a The local clone on my laptop got messed up and some changes had not been pushed. This commit represents the status of the working copy from that clone. It unfortunately contains some changes that I did not intend to commit yet, but this seems like the best option at the moment.
Michael Pavone <pavone@retrodev.com>
parents: 483
diff changeset
145 }
354
15dd6418fe67 Initial PSG support. Mostly works, noise channel is borked though.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
146 }
15dd6418fe67 Initial PSG support. Mostly works, noise channel is borked though.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
147 }
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.
Mike Pavone <pavone@retrodev.com>
parents: 364
diff changeset
148 context->cycles += context->clock_inc;
354
15dd6418fe67 Initial PSG support. Mostly works, noise channel is borked though.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
149 }
15dd6418fe67 Initial PSG support. Mostly works, noise channel is borked though.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
150 }
15dd6418fe67 Initial PSG support. Mostly works, noise channel is borked though.
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
151
1427
4e5797b3935a WIP - New savestate format
Michael Pavone <pavone@retrodev.com>
parents: 1356
diff changeset
152 void psg_serialize(psg_context *context, serialize_buffer *buf)
4e5797b3935a WIP - New savestate format
Michael Pavone <pavone@retrodev.com>
parents: 1356
diff changeset
153 {
4e5797b3935a WIP - New savestate format
Michael Pavone <pavone@retrodev.com>
parents: 1356
diff changeset
154 save_int16(buf, context->lsfr);
4e5797b3935a WIP - New savestate format
Michael Pavone <pavone@retrodev.com>
parents: 1356
diff changeset
155 save_buffer16(buf, context->counter_load, 4);
4e5797b3935a WIP - New savestate format
Michael Pavone <pavone@retrodev.com>
parents: 1356
diff changeset
156 save_buffer16(buf, context->counters, 4);
4e5797b3935a WIP - New savestate format
Michael Pavone <pavone@retrodev.com>
parents: 1356
diff changeset
157 save_buffer8(buf, context->volume, 4);
4e5797b3935a WIP - New savestate format
Michael Pavone <pavone@retrodev.com>
parents: 1356
diff changeset
158 uint8_t output_state = context->output_state[0] << 3 | context->output_state[1] << 2
4e5797b3935a WIP - New savestate format
Michael Pavone <pavone@retrodev.com>
parents: 1356
diff changeset
159 | context->output_state[2] << 1 | context->output_state[3]
4e5797b3935a WIP - New savestate format
Michael Pavone <pavone@retrodev.com>
parents: 1356
diff changeset
160 | context->noise_use_tone << 4;
4e5797b3935a WIP - New savestate format
Michael Pavone <pavone@retrodev.com>
parents: 1356
diff changeset
161 save_int8(buf, output_state);
4e5797b3935a WIP - New savestate format
Michael Pavone <pavone@retrodev.com>
parents: 1356
diff changeset
162 save_int8(buf, context->noise_type);
4e5797b3935a WIP - New savestate format
Michael Pavone <pavone@retrodev.com>
parents: 1356
diff changeset
163 save_int8(buf, context->latch);
4e5797b3935a WIP - New savestate format
Michael Pavone <pavone@retrodev.com>
parents: 1356
diff changeset
164 save_int32(buf, context->cycles);
4e5797b3935a WIP - New savestate format
Michael Pavone <pavone@retrodev.com>
parents: 1356
diff changeset
165 }
4e5797b3935a WIP - New savestate format
Michael Pavone <pavone@retrodev.com>
parents: 1356
diff changeset
166
4e5797b3935a WIP - New savestate format
Michael Pavone <pavone@retrodev.com>
parents: 1356
diff changeset
167 void psg_deserialize(deserialize_buffer *buf, void *vcontext)
4e5797b3935a WIP - New savestate format
Michael Pavone <pavone@retrodev.com>
parents: 1356
diff changeset
168 {
4e5797b3935a WIP - New savestate format
Michael Pavone <pavone@retrodev.com>
parents: 1356
diff changeset
169 psg_context *context = vcontext;
4e5797b3935a WIP - New savestate format
Michael Pavone <pavone@retrodev.com>
parents: 1356
diff changeset
170 context->lsfr = load_int16(buf);
4e5797b3935a WIP - New savestate format
Michael Pavone <pavone@retrodev.com>
parents: 1356
diff changeset
171 load_buffer16(buf, context->counter_load, 4);
4e5797b3935a WIP - New savestate format
Michael Pavone <pavone@retrodev.com>
parents: 1356
diff changeset
172 load_buffer16(buf, context->counters, 4);
4e5797b3935a WIP - New savestate format
Michael Pavone <pavone@retrodev.com>
parents: 1356
diff changeset
173 load_buffer8(buf, context->volume, 4);
4e5797b3935a WIP - New savestate format
Michael Pavone <pavone@retrodev.com>
parents: 1356
diff changeset
174 uint8_t output_state = load_int8(buf);
1451
9c65819afec3 Fix operator precedence in psg serialize/deserialize
Michael Pavone <pavone@retrodev.com>
parents: 1427
diff changeset
175 context->output_state[0] = output_state >> 3 & 1;
9c65819afec3 Fix operator precedence in psg serialize/deserialize
Michael Pavone <pavone@retrodev.com>
parents: 1427
diff changeset
176 context->output_state[1] = output_state >> 2 & 1;
9c65819afec3 Fix operator precedence in psg serialize/deserialize
Michael Pavone <pavone@retrodev.com>
parents: 1427
diff changeset
177 context->output_state[2] = output_state >> 1 & 1;
1427
4e5797b3935a WIP - New savestate format
Michael Pavone <pavone@retrodev.com>
parents: 1356
diff changeset
178 context->output_state[3] = output_state & 1;
1451
9c65819afec3 Fix operator precedence in psg serialize/deserialize
Michael Pavone <pavone@retrodev.com>
parents: 1427
diff changeset
179 context->noise_use_tone = output_state >> 4 & 1;
1427
4e5797b3935a WIP - New savestate format
Michael Pavone <pavone@retrodev.com>
parents: 1356
diff changeset
180 context->noise_type = load_int8(buf);
4e5797b3935a WIP - New savestate format
Michael Pavone <pavone@retrodev.com>
parents: 1356
diff changeset
181 context->latch = load_int8(buf);
4e5797b3935a WIP - New savestate format
Michael Pavone <pavone@retrodev.com>
parents: 1356
diff changeset
182 context->cycles = load_int32(buf);
4e5797b3935a WIP - New savestate format
Michael Pavone <pavone@retrodev.com>
parents: 1356
diff changeset
183 }