Debian Codegraph @

src/engine/platform/sound/supervision.c

raw
1 // THIS IS A MODIFIED VERSION OF POTATOR'S SOUND EMULATION CORE
2 // MODIFIED BY AART1256 IN 2024
3
4 #include "supervision.h"
5
6 #include <string.h>
7
8 #define SV_SAMPLE_RATE ((int)((svision->UNSCALED_CLOCK)/64))
9 #define SV_DEC_TICK ((SV_SAMPLE_RATE)/60)
10
11 void supervision_sound_set_clock(struct svision_t *svision, uint32 clock) {
12 svision->UNSCALED_CLOCK = clock;
13 }
14
15 void supervision_memorymap_registers_write(struct svision_t *svision, uint32 Addr, uint8 Value)
16 {
17 switch (Addr & 0x1fff) {
18 case 0x10: case 0x11: case 0x12: case 0x13:
19 case 0x14: case 0x15: case 0x16: case 0x17:
20 supervision_sound_wave_write(svision, ((Addr & 0x4) >> 2), Addr & 3, Value);
21 break;
22 case 0x18:
23 case 0x19:
24 case 0x1a:
25 case 0x1b:
26 case 0x1c:
27 supervision_sound_dma_write(svision, Addr & 0x07, Value);
28 break;
29 case 0x28:
30 case 0x29:
31 case 0x2a:
32 supervision_sound_noise_write(svision, Addr & 0x07, Value);
33 break;
34 }
35 }
36
37 void supervision_set_mute_mask(struct svision_t *svision, uint8 mask) {
38 svision->ch_mask = mask;
39 }
40
41 void supervision_sound_set_flags(struct svision_t *svision, uint8 flags_set)
42 {
43 svision->flags = flags_set;
44 }
45
46 void supervision_sound_reset(struct svision_t *svision)
47 {
48 memset(svision->m_channel, 0, sizeof(svision->m_channel));
49 memset(&svision->m_noise, 0, sizeof(svision->m_noise) );
50 memset(&svision->m_dma, 0, sizeof(svision->m_dma) );
51
52 memset(svision->ch, 0, sizeof(svision->ch) );
53 svision->decrement_tick = 0;
54 svision->ch_mask = 15;
55 }
56
57 void supervision_sound_stream_update(struct svision_t *svision, uint8 *stream, uint32 len)
58 {
59 size_t i, j;
60 SVISION_CHANNEL *channel;
61 uint8 s = 0;
62 uint8 *left = stream + 0;
63 uint8 *right = stream + 1;
64 uint8 *chout = stream + 2;
65
66 for (i = 0; i < len >> 1; i++, left += 2, right += 2) {
67 *left = *right = 0;
68 for (channel = svision->m_channel, j = 0; j < 2; j++, channel++) {
69 chout[j] = 0;
70 if (svision->ch[j].size != 0) {
71 if (svision->ch[j].on || channel->count != 0) {
72 BOOL on = 0;
73 switch (svision->ch[j].waveform) {
74 case 0: // 12.5%
75 on = svision->ch[j].pos < (28 * svision->ch[j].size) >> 5;
76 break;
77 case 1: // 25%
78 on = svision->ch[j].pos < (24 * svision->ch[j].size) >> 5;
79 break;
80 case 2: // 50%
81 on = svision->ch[j].pos < svision->ch[j].size / 2;
82 break;
83 case 3: // 75%
84 on = svision->ch[j].pos < svision->ch[j].size / 4;
85 // MESS/MAME: <= (9 * svision->ch[j].size) >> 5;
86 break;
87 }
88 s = on ? (svision->ch[j].volume)<<2 : 0;
89 s = ((svision->ch_mask>>(3-j))&1)?s:0;
90 if (svision->flags&1) {
91 if (j == 0)
92 *right += s;
93 else
94 *left += s;
95 } else {
96 *left += s;
97 *right += s;
98 }
99 chout[j] = s;
100 }
101 svision->ch[j].pos++;
102 if (svision->ch[j].pos >= svision->ch[j].size) {
103 svision->ch[j].pos = 0;
104 // Transition from off to on
105 if (channel->on) {
106 memcpy(&svision->ch[j], channel, sizeof(svision->ch[j]));
107 channel->on = 0;
108 }
109 }
110 }
111 }
112
113 if (svision->m_noise.on && (svision->m_noise.play || svision->m_noise.count != 0)) {
114 s = (svision->m_noise.value * svision->m_noise.volume) << 2;
115 s = svision->ch_mask&1?s:0;
116 chout[3] = 0;
117 if (svision->m_noise.left) {
118 *left += s;
119 chout[3] = s;
120 }
121 if (svision->m_noise.right) {
122 *right += s;
123 chout[3] = s;
124 }
125 svision->m_noise.pos += svision->m_noise.step;
126 while (svision->m_noise.pos >= 1.0) { // if/while difference - Pacific Battle
127 // LFSR: x^2 + x + 1
128 uint16 feedback;
129 svision->m_noise.value = svision->m_noise.state & 1;
130 feedback = ((svision->m_noise.state >> 1) ^ svision->m_noise.state) & 0x0001;
131 feedback <<= svision->m_noise.type;
132 svision->m_noise.state = (svision->m_noise.state >> 1) | feedback;
133 svision->m_noise.pos -= 1.0;
134 }
135 }
136
137 chout[2] = 0;
138 if (svision->m_dma.on) {
139 uint8 sample;
140 uint16 addr = svision->m_dma.start + (uint16)svision->m_dma.pos / 2;
141 if (addr >= 0x8000 && addr < 0xc000) {
142 sample = svision->supervision_dma_mem[(addr & 0x3fff) | svision->m_dma.ca14to16];
143 }
144 if (((uint16)svision->m_dma.pos) & 1)
145 s = (sample & 0xf);
146 else
147 s = (sample & 0xf0) >> 4;
148 s <<= 2;
149 s = ((svision->ch_mask>>1)&1)?s:0;
150 chout[2] = 0;
151 if (svision->m_dma.left) {
152 *left += s;
153 chout[2] = s;
154 }
155 if (svision->m_dma.right) {
156 *right += s;
157 chout[2] = s;
158 }
159 svision->m_dma.pos += svision->m_dma.step;
160 if (svision->m_dma.pos >= svision->m_dma.size) {
161 svision->m_dma.on = 0;
162 }
163 }
164
165 if (svision->decrement_tick > SV_DEC_TICK) {
166 svision->decrement_tick = 0;
167 supervision_sound_decrement(svision);
168 }
169 svision->decrement_tick++;
170 }
171 }
172
173 void supervision_sound_decrement(struct svision_t *svision)
174 {
175 if (svision->m_channel[0].count > 0)
176 svision->m_channel[0].count--;
177 if (svision->m_channel[1].count > 0)
178 svision->m_channel[1].count--;
179 if (svision->m_noise.count > 0)
180 svision->m_noise.count--;
181 }
182
183 void supervision_sound_wave_write(struct svision_t *svision, int which, int offset, uint8 data)
184 {
185 SVISION_CHANNEL *channel = &svision->m_channel[which];
186
187 channel->reg[offset] = data;
188 switch (offset) {
189 case 0:
190 case 1: {
191 uint16 size;
192 size = channel->reg[0] | ((channel->reg[1] & 7) << 8);
193 // if size == 0 then channel->size == 0
194 if (size) {
195 channel->size = (uint16)(((real)SV_SAMPLE_RATE) * ((real)((size + 1) << 5)) / ((real)svision->UNSCALED_CLOCK));
196 } else {
197 channel->size = 0;
198 }
199 channel->pos = 0;
200 // Popo Team
201 if (channel->count != 0 || svision->ch[which].size == 0 || channel->size == 0) {
202 svision->ch[which].size = channel->size;
203 if (channel->count == 0)
204 svision->ch[which].pos = 0;
205 }
206 }
207 break;
208 case 2:
209 channel->on = data & 0x40;
210 channel->waveform = (data & 0x30) >> 4;
211 channel->volume = data & 0x0f;
212 if (!channel->on || svision->ch[which].size == 0 || channel->size == 0) {
213 uint16 pos = svision->ch[which].pos;
214 memcpy(&svision->ch[which], channel, sizeof(svision->ch[which]));
215 if (channel->count != 0) // Journey to the West
216 svision->ch[which].pos = pos;
217 }
218 break;
219 case 3:
220 channel->count = data + 1;
221 svision->ch[which].size = channel->size; // Sonny Xpress!
222 break;
223 }
224 }
225
226 void supervision_sound_dma_write(struct svision_t *svision, int offset, uint8 data)
227 {
228 svision->m_dma.reg[offset] = data;
229 switch (offset) {
230 case 0:
231 case 1:
232 svision->m_dma.start = (svision->m_dma.reg[0] | (svision->m_dma.reg[1] << 8));
233 break;
234 case 2:
235 svision->m_dma.size = (data ? data : 0x100) * 32; // Number of 4-bit samples
236 break;
237 case 3:
238 // Test games: Classic Casino, SSSnake
239 svision->m_dma.step = ((real)svision->UNSCALED_CLOCK) / ((real)SV_SAMPLE_RATE * (256 << (data & 3)));
240 // MESS/MAME. Wrong
241 //svision->m_dma.step = svision->UNSCALED_CLOCK / (256.0 * SV_SAMPLE_RATE * (1 + (data & 3)));
242 svision->m_dma.right = data & 4;
243 svision->m_dma.left = data & 8;
244 svision->m_dma.ca14to16 = ((data & 0x70) >> 4) << 14;
245 break;
246 case 4:
247 svision->m_dma.on = data & 0x80;
248 if (svision->m_dma.on) {
249 svision->m_dma.pos = 0.0;
250 }
251 break;
252 }
253 }
254
255 void supervision_sound_noise_write(struct svision_t *svision, int offset, uint8 data)
256 {
257 svision->m_noise.reg[offset] = data;
258 switch (offset) {
259 case 0: {
260 uint32 divisor = 8 << (data >> 4);
261 if (divisor) {
262 svision->m_noise.step = ((real)svision->UNSCALED_CLOCK) / ((real)SV_SAMPLE_RATE * divisor);
263 } else {
264 svision->m_noise.step = 0;
265 }
266
267 svision->m_noise.step = ((real)svision->UNSCALED_CLOCK) / ((real)SV_SAMPLE_RATE * divisor);
268 svision->m_noise.volume = data & 0xf;
269 }
270 break;
271 case 1:
272 svision->m_noise.count = data + 1;
273 break;
274 case 2:
275 svision->m_noise.type = (data & 1) ? 14 : 6;
276 svision->m_noise.play = data & 2;
277 svision->m_noise.right = data & 4;
278 svision->m_noise.left = data & 8;
279 svision->m_noise.on = data & 0x10; /* honey bee start */
280 svision->m_noise.state = 1;
281 break;
282 }
283 svision->m_noise.pos = 0.0;
284 }