Debian Codegraph @

src/engine/platform/sound/d65modified.c

raw
1 /*
2
3 ============================================================================
4
5 NEC D65010G031 sound emulator
6 by cam900
7
8 This file is licensed under zlib license.
9
10 ============================================================================
11
12 zlib License
13
14 (C) 2023-present cam900 and contributors
15
16 This software is provided 'as-is', without any express or implied
17 warranty. In no event will the authors be held liable for any damages
18 arising from the use of this software.
19
20 Permission is granted to anyone to use this software for any purpose,
21 including commercial applications, and to alter it and redistribute it
22 freely, subject to the following restrictions:
23
24 1. The origin of this software must not be misrepresented; you must not
25 claim that you wrote the original software. If you use this software
26 in a product, an acknowledgment in the product documentation would be
27 appreciated but is not required.
28 2. Altered source versions must be plainly marked as such, and must not be
29 misrepresented as being the original software.
30 3. This notice may not be removed or altered from any source distribution.
31
32 ============================================================================
33
34 TODO:
35 - needs hardware test
36
37 ALTERED VERSION!!!
38 ALTERED VERSION!!!
39 ALTERED VERSION!!!
40 ALTERED VERSION!!!
41 ALTERED VERSION!!!
42 ALTERED VERSION!!!
43 ALTERED VERSION!!!
44 ALTERED VERSION!!!
45 ALTERED VERSION!!!
46 ALTERED VERSION!!!
47 ALTERED VERSION!!!
48 ALTERED VERSION!!!
49 ALTERED VERSION!!!
50 ALTERED VERSION!!!
51
52
53 THIS IS **NOT** NOT NOT NOT!!!! THE ORIGINAL SOFTWARE
54 IT ISN'T
55 THE MODIFICATIONS THAT WERE MADE ARE:
56
57 1. FIX VOLUMES - APPARENTLY THE SQUARES HAVE DIFFERENT VOLUMES (thanks forple)
58
59 */
60
61 #include "d65modified.h"
62 #include <stdlib.h>
63
64 static int d65010g031_max(int a, int b) { return (a > b) ? a : b; }
65
66 int d65010g031_square_tick(struct d65010g031_square_t *square, const int cycle)
67 {
68 if (square->period > 0)
69 {
70 const int period = square->period;
71 square->counter += cycle;
72 while (square->counter >= period)
73 {
74 square->counter -= period;
75 square->out ^= 1;
76 }
77 return square->out;
78 }
79 return 0;
80 }
81
82 // this is the bit I altered
83 // THIS IS **NOT** THE ORIGINAL SOFTWARE! I am plainly marking it as such!
84 const int d65Volumes[3]={
85 3840, // -6dB
86 5120, // -3dB
87 8192 // 0dB
88 };
89
90 int d65010g031_sound_tick(struct d65010g031_t *d65010g031, const int cycle)
91 {
92 int out = 0;
93 for (int i = 0; i < 3; i++)
94 {
95 d65010g031->out[i] = 0;
96 }
97 if (d65010g031->ctrl & 2)
98 {
99 if (d65010g031->ctrl & 1) // ring modulation
100 {
101 int sout[3] = {
102 d65010g031_square_tick(&d65010g031->square[0], cycle),
103 d65010g031_square_tick(&d65010g031->square[1], cycle),
104 d65010g031_square_tick(&d65010g031->square[2], cycle),
105 };
106 d65010g031->out[0] = (sout[0] ^ sout[1]) ? d65Volumes[0] : -d65Volumes[0];
107 d65010g031->out[1] = (sout[1] ^ sout[2]) ? d65Volumes[1] : -d65Volumes[1];
108 d65010g031->out[2] = (sout[2] ? d65Volumes[2] : -d65Volumes[2]);
109 }
110 else
111 {
112 for (int i = 0; i < 3; i++)
113 {
114 d65010g031->out[i] = d65010g031_square_tick(&d65010g031->square[i], cycle)?d65Volumes[i]:-d65Volumes[i];
115 }
116 }
117 out = d65010g031->out[0] + d65010g031->out[1] + d65010g031->out[2];
118 }
119 return out;
120 }
121
122 void d65010g031_reset(struct d65010g031_t *d65010g031)
123 {
124 for (int i = 0; i < 3; i++)
125 {
126 d65010g031->square[i].period = 0;
127 d65010g031->square[i].counter = 0;
128 d65010g031->square[i].out = 0;
129 }
130 d65010g031->ctrl = 0;
131 }
132
133 void d65010g031_write(struct d65010g031_t *d65010g031, const unsigned char a, const unsigned char d)
134 {
135 switch (a)
136 {
137 case 3:
138 d65010g031->ctrl = d;
139 break;
140 default:
141 {
142 const unsigned char per = (unsigned char)(~d) & 0x3f;
143 if ((per == 0) && (d65010g031->square[a].period != 0))
144 {
145 d65010g031->square[a].out ^= 1;
146 }
147 d65010g031->square[a].period = per;
148 break;
149 }
150 }
151 }