annotate cdd_fader.c @ 2208:3809a0bd680e

Fix a couple of Z80 debugger issues
author Michael Pavone <pavone@retrodev.com>
date Sat, 27 Aug 2022 22:02:09 -0700
parents 4fbe1e7c4a73
children 5a21bc0ec583
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2080
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1 #include "cdd_fader.h"
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
2 #include <stdio.h>
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
3
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
4 void cdd_fader_init(cdd_fader *fader)
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
5 {
2081
cfd53c94fffb Initial stab at RF5C164 emulation
Michael Pavone <pavone@retrodev.com>
parents: 2080
diff changeset
6 fader->audio = render_audio_source("CDDA", 16934400, 384, 2);
2080
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
7 fader->cur_attenuation = 0x4000;
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
8 fader->dst_attenuation = 0x4000;
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
9 fader->attenuation_step = 0;
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
10 }
2164
4fbe1e7c4a73 Don't leak all Sega CD resources when freeing a Genesis instance
Michael Pavone <pavone@retrodev.com>
parents: 2081
diff changeset
11
4fbe1e7c4a73 Don't leak all Sega CD resources when freeing a Genesis instance
Michael Pavone <pavone@retrodev.com>
parents: 2081
diff changeset
12 void cdd_fader_deinit(cdd_fader *fader)
4fbe1e7c4a73 Don't leak all Sega CD resources when freeing a Genesis instance
Michael Pavone <pavone@retrodev.com>
parents: 2081
diff changeset
13 {
4fbe1e7c4a73 Don't leak all Sega CD resources when freeing a Genesis instance
Michael Pavone <pavone@retrodev.com>
parents: 2081
diff changeset
14 render_free_source(fader->audio);
4fbe1e7c4a73 Don't leak all Sega CD resources when freeing a Genesis instance
Michael Pavone <pavone@retrodev.com>
parents: 2081
diff changeset
15 }
4fbe1e7c4a73 Don't leak all Sega CD resources when freeing a Genesis instance
Michael Pavone <pavone@retrodev.com>
parents: 2081
diff changeset
16
2080
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
17 void cdd_fader_attenuation_write(cdd_fader *fader, uint16_t attenuation)
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
18 {
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
19 fader->dst_attenuation = attenuation & 0xFFF0;
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
20 fader->flags = attenuation & 0xE;
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
21 if (fader->dst_attenuation > fader->cur_attenuation) {
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
22 fader->attenuation_step = (fader->dst_attenuation - fader->cur_attenuation) >> 4;
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
23 } else if (fader->dst_attenuation < fader->cur_attenuation) {
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
24 fader->attenuation_step = (fader->cur_attenuation - fader->dst_attenuation) >> 4;
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
25 } else {
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
26 fader->attenuation_step = 0;
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
27 }
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
28 }
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
29
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
30 void cdd_fader_data(cdd_fader *fader, uint8_t byte)
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
31 {
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
32 fader->bytes[fader->byte_counter++] = byte;
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
33 if (fader->byte_counter == sizeof(fader->bytes)) {
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
34 fader->byte_counter = 0;
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
35 int32_t left = (fader->bytes[1] << 8) | fader->bytes[0];
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
36 int32_t right = (fader->bytes[3] << 8) | fader->bytes[2];
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
37 if (left & 0x8000) {
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
38 left |= 0xFFFF0000;
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
39 }
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
40 if (right & 0x8000) {
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
41 right |= 0xFFFF0000;
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
42 }
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
43 if (!fader->cur_attenuation) {
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
44 left = right = 0;
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
45 } else if (fader->cur_attenuation >= 4) {
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
46 left *= fader->cur_attenuation & 0x7FF0;
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
47 right *= fader->cur_attenuation & 0x7FF0;
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
48 left >>= 14;
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
49 right >>= 14;
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
50 } else {
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
51 //TODO: FIXME
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
52 left = right = 0;
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
53 }
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
54 render_put_stereo_sample(fader->audio, left, right);
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
55 if (fader->attenuation_step) {
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
56 if (fader->dst_attenuation > fader->cur_attenuation) {
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
57 fader->cur_attenuation += fader->attenuation_step;
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
58 if (fader->cur_attenuation >= fader->dst_attenuation) {
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
59 fader->cur_attenuation = fader->dst_attenuation;
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
60 fader->attenuation_step = 0;
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
61 }
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
62 } else {
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
63 fader->cur_attenuation -= fader->attenuation_step;
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
64 if (fader->cur_attenuation <= fader->dst_attenuation) {
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
65 fader->cur_attenuation = fader->dst_attenuation;
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
66 fader->attenuation_step = 0;
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
67 }
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
68 }
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
69 }
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
70 }
bafb757e1cd2 Implement CD audio
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
71 }