Debian Codegraph @

src/engine/platform/sound/atomicssg/ssg.c

raw
1 /*
2 * Copyright (C) 2023-2024 nukeykt
3 * Copyright (C) 2024 tildearrow
4 *
5 * This file is part of AtomicSSG.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * SSG (AY-3-8910/YM2149) emulator.
18 * based on YM2608-LLE by nukeykt.
19 *
20 */
21 #include <string.h>
22 #include "ssg.h"
23
24 void SSG_Clock(ssg_t* chip, int clk)
25 {
26 int i;
27
28 chip->input.clk = clk;
29
30 chip->clk1 = !chip->input.clk;
31 chip->clk2 = chip->input.clk;
32
33 chip->ic = !chip->input.ic;
34
35 {
36 int read = !chip->ic && !chip->input.rd && !chip->input.cs;
37 chip->read0 = !chip->ic && !chip->input.rd && !chip->input.cs;
38 int write = !chip->input.wr && !chip->input.cs;
39 int writeaddr = chip->ic || (!chip->input.wr && !chip->input.cs);
40 int writedata = !chip->ic && !chip->input.wr && !chip->input.cs;
41 int read1 = !chip->ic && !chip->input.rd && !chip->input.cs;
42 chip->ssg_write0 = writeaddr;
43 chip->ssg_write1 = writedata || chip->ic;
44 chip->ssg_read1 = read1;
45
46 chip->o_data_d = !read;
47
48 if (write)
49 chip->data_l = (chip->input.data & 255);
50
51 if (chip->ic)
52 chip->data_bus2 |= 0x2f;
53 else
54 if (!read && !chip->ic)
55 chip->data_bus2 = chip->data_l ^ 0x1ff;
56
57 if (chip->ic_latch_fm[1])
58 {
59 chip->data_bus1 &= ~255;
60 }
61 else if (!read && !chip->ic)
62 {
63 chip->data_bus1 = chip->data_l;
64 }
65 }
66
67 {
68 chip->ssg_clk1 = chip->clk1;
69 chip->ssg_clk2 = chip->clk2;
70
71 if (chip->ic)
72 chip->ssg_ssg_addr = 0;
73 else if (chip->ssg_write0)
74 chip->ssg_ssg_addr = 1;
75
76 if (chip->ic)
77 chip->ssg_address = 0;
78 else if (chip->ssg_write0)
79 chip->ssg_address = chip->input.a0 & 0x1f;
80
81 int ssg_access = chip->ssg_ssg_addr && (chip->ssg_write1 || chip->ssg_read1);
82
83 if (chip->ssg_egtrig_rst)
84 chip->ssg_egtrig = 0;
85
86 if (chip->ic)
87 {
88 chip->ssg_freq_a[0] = chip->data_bus1 & 255;
89 chip->ssg_freq_a[1] = chip->data_bus1 & 15;
90 chip->ssg_freq_b[0] = chip->data_bus1 & 255;
91 chip->ssg_freq_b[1] = chip->data_bus1 & 15;
92 chip->ssg_freq_c[0] = chip->data_bus1 & 255;
93 chip->ssg_freq_c[1] = chip->data_bus1 & 15;
94 chip->ssg_noise = chip->data_bus1 & 31;
95 chip->ssg_mode = chip->data_bus1 & 255;
96 chip->ssg_level_a = chip->data_bus1 & 31;
97 chip->ssg_level_b = chip->data_bus1 & 31;
98 chip->ssg_level_c = chip->data_bus1 & 31;
99 chip->ssg_env[0] = chip->data_bus1 & 255;
100 chip->ssg_env[1] = chip->data_bus1 & 255;
101 chip->ssg_envmode = chip->data_bus1 & 15;
102 chip->o_gpio_a = chip->data_bus1 & 255;
103 chip->o_gpio_b = chip->data_bus1 & 255;
104
105 chip->ssg_egtrig = 1;
106 }
107 else if (chip->ssg_ssg_addr && chip->ssg_write1)
108 {
109 switch (chip->ssg_address)
110 {
111 case 0x0:
112 chip->ssg_freq_a[0] = chip->data_bus1 & 255;
113 break;
114 case 0x1:
115 chip->ssg_freq_a[1] = chip->data_bus1 & 15;
116 break;
117 case 0x2:
118 chip->ssg_freq_b[0] = chip->data_bus1 & 255;
119 break;
120 case 0x3:
121 chip->ssg_freq_b[1] = chip->data_bus1 & 15;
122 break;
123 case 0x4:
124 chip->ssg_freq_c[0] = chip->data_bus1 & 255;
125 break;
126 case 0x5:
127 chip->ssg_freq_c[1] = chip->data_bus1 & 15;
128 break;
129 case 0x6:
130 chip->ssg_noise = chip->data_bus1 & 31;
131 break;
132 case 0x7:
133 chip->ssg_mode = chip->data_bus1 & 255;
134 break;
135 case 0x8:
136 chip->ssg_level_a = chip->data_bus1 & 31;
137 break;
138 case 0x9:
139 chip->ssg_level_b = chip->data_bus1 & 31;
140 break;
141 case 0xa:
142 chip->ssg_level_c = chip->data_bus1 & 31;
143 break;
144 case 0xb:
145 chip->ssg_env[0] = chip->data_bus1 & 255;
146 break;
147 case 0xc:
148 chip->ssg_env[1] = chip->data_bus1 & 255;
149 break;
150 case 0xd:
151 chip->ssg_envmode = chip->data_bus1 & 15;
152 chip->ssg_egtrig = 1;
153 break;
154 case 0xe:
155 chip->o_gpio_a = chip->data_bus1 & 255;
156 break;
157 case 0xf:
158 chip->o_gpio_b = chip->data_bus1 & 255;
159 break;
160 }
161 }
162 if (chip->ssg_ssg_addr && chip->ssg_read1)
163 {
164 switch (chip->ssg_address)
165 {
166 case 0x0:
167 chip->data_bus1 &= ~255;
168 chip->data_bus1 |= chip->ssg_freq_a[0] & 255;
169 break;
170 case 0x1:
171 chip->data_bus1 &= ~15;
172 chip->data_bus1 |= chip->ssg_freq_a[1] & 15;
173 break;
174 case 0x2:
175 chip->data_bus1 &= ~255;
176 chip->data_bus1 |= chip->ssg_freq_b[0] & 255;
177 break;
178 case 0x3:
179 chip->data_bus1 &= ~15;
180 chip->data_bus1 |= chip->ssg_freq_b[1] & 15;
181 break;
182 case 0x4:
183 chip->data_bus1 &= ~255;
184 chip->data_bus1 |= chip->ssg_freq_c[0] & 255;
185 break;
186 case 0x5:
187 chip->data_bus1 &= ~15;
188 chip->data_bus1 |= chip->ssg_freq_c[1] & 15;
189 break;
190 case 0x6:
191 chip->data_bus1 &= ~31;
192 chip->data_bus1 |= chip->ssg_noise & 31;
193 break;
194 case 0x7:
195 chip->data_bus1 &= ~255;
196 chip->data_bus1 |= chip->ssg_mode & 255;
197 break;
198 case 0x8:
199 chip->data_bus1 &= ~31;
200 chip->data_bus1 |= chip->ssg_level_a & 31;
201 break;
202 case 0x9:
203 chip->data_bus1 &= ~31;
204 chip->data_bus1 |= chip->ssg_level_b & 31;
205 break;
206 case 0xa:
207 chip->data_bus1 &= ~31;
208 chip->data_bus1 |= chip->ssg_level_c & 31;
209 break;
210 case 0xb:
211 chip->data_bus1 &= ~255;
212 chip->data_bus1 |= chip->ssg_env[0] & 255;
213 break;
214 case 0xc:
215 chip->data_bus1 &= ~255;
216 chip->data_bus1 |= chip->ssg_env[1] & 255;
217 break;
218 case 0xd:
219 chip->data_bus1 &= ~15;
220 chip->data_bus1 |= chip->ssg_envmode & 15;
221 break;
222 case 0xe:
223 chip->data_bus1 &= ~15;
224 chip->data_bus1 |= chip->input.gpio_a & 15;
225 break;
226 case 0xf:
227 chip->data_bus1 &= ~255;
228 chip->data_bus1 |= chip->input.gpio_b & 255;
229 break;
230 }
231 }
232
233 if (chip->ssg_clk1)
234 {
235 chip->ssg_envadd = (chip->ssg_eg_of[1] & 4) != 0 && (chip->ssg_sel[1] & 8) != 0;
236 chip->ssg_envcnt[1] = chip->ssg_hold[0] ? 31 : chip->ssg_envcnt[0];
237 chip->ssg_dir[1] = chip->ssg_dir[0];
238 chip->ssg_hold[1] = chip->ssg_hold[0];
239 chip->ssg_t2[1] = chip->ssg_t2[0];
240
241 chip->ssg_egtrig_s = chip->ssg_egtrig;
242
243 chip->ssg_eg_sel_l = (chip->ssg_sel[1] & 8) != 0;
244
245 chip->ssg_sel[0] = chip->ssg_sel[1] << 1;
246 chip->ssg_sel[0] |= (chip->ssg_sel[1] & 7) == 0 && !chip->ic;
247
248 chip->ssg_sel_freq = 0;
249 if (chip->ssg_sel[1] & 1)
250 chip->ssg_sel_freq |= (chip->ssg_freq_c[1] << 8) | chip->ssg_freq_c[0];
251 if (chip->ssg_sel[1] & 2)
252 chip->ssg_sel_freq |= (chip->ssg_freq_b[1] << 8) | chip->ssg_freq_b[0];
253 if (chip->ssg_sel[1] & 4)
254 chip->ssg_sel_freq |= (chip->ssg_freq_a[1] << 8) | chip->ssg_freq_a[0];
255 if (chip->ssg_sel[1] & 8)
256 chip->ssg_sel_freq |= (chip->ssg_env[1] << 8) | chip->ssg_env[0];
257
258 int cnt = chip->ssg_freq_cnt[7] + 1;
259 int of = (cnt & 0x1000) != 0;
260 chip->ssg_freq_cnt[0] = cnt & 0xfff;
261 chip->ssg_freq_cnt[4] = chip->ssg_freq_cnt[3];
262 chip->ssg_freq_cnt[6] = chip->ssg_freq_cnt[5];
263
264 chip->ssg_cnt_of[1] = chip->ssg_cnt_of[0];
265
266 if (chip->ssg_sel[1] & 8)
267 chip->ssg_sign_toggle = chip->ssg_cnt_of[0] & 7;
268 else
269 chip->ssg_sign_toggle = 0;
270
271 chip->ssg_freq_cnt2[1] = chip->ssg_freq_cnt2[0];
272 chip->ssg_freq_cnt2[3] = chip->ssg_freq_cnt2[2];
273 chip->ssg_freq_cnt2[5] = chip->ssg_freq_cnt2[4];
274 chip->ssg_freq_cnt2[7] = chip->ssg_freq_cnt2[6];
275
276 chip->ssg_sign[1] = chip->ssg_sign[0];
277
278 int cnt_of = !chip->ssg_cnt_reload && (chip->ssg_sel_freq_l - chip->ssg_cnt_of_l < chip->ssg_freq_cnt2[0]);
279
280 chip->ssg_cnt2_add = (chip->ssg_sel[1] & 8) != 0 && of;
281
282 chip->ssg_sel_eg_l[0] = (chip->ssg_sel[1] & 8) != 0;
283
284 chip->ssg_eg_of[0] = (chip->ssg_eg_of[1] << 1) | cnt_of;
285
286 int fr_rst = chip->ssg_ch_of || ((cnt_of || chip->ssg_cnt_reload) && chip->ssg_sel_eg_l[1]);
287
288 chip->ssg_freq_cnt[2] = fr_rst ? 0 : chip->ssg_freq_cnt[1];
289
290 chip->ssg_fr_rst_l = fr_rst;
291
292 chip->ssg_noise_add = (chip->ssg_sel[1] & 1) != 0;
293
294 chip->ssg_noise_cnt[1] = chip->ssg_noise_cnt[0];
295
296 int noise_of = chip->ssg_noise <= (chip->ssg_noise_cnt[0] >> 1);
297
298 chip->ssg_noise_of = noise_of && chip->ssg_noise_of_low;
299
300 chip->ssg_test = chip->input.test;
301
302 chip->ssg_noise_step = chip->ssg_noise_of || !chip->ssg_test;
303 }
304 if (chip->ssg_clk2)
305 {
306 int sum = chip->ssg_envcnt[1] + chip->ssg_envadd;
307 int of = (sum & 32) != 0;
308 chip->ssg_envcnt[0] = chip->ssg_egtrig_s ? 0 : sum & 31;
309
310 int dir = of && (chip->ssg_envmode & 2) != 0 && !chip->ssg_hold[1];
311 chip->ssg_dir[0] = chip->ssg_egtrig_s ? 0 : chip->ssg_dir[1] ^ dir;
312
313 chip->ssg_hold[0] = chip->ssg_egtrig_s ? 0 : chip->ssg_hold[1] || (of && (chip->ssg_envmode & 1) != 0);
314
315 chip->ssg_t2[0] = chip->ssg_egtrig_s ? 0 : chip->ssg_t2[1] || of;
316
317 chip->ssg_egtrig_rst = chip->ssg_egtrig_s && chip->ssg_eg_sel_l;
318
319
320 chip->ssg_sel[1] = chip->ssg_sel[0];
321
322 chip->ssg_freq_cnt[1] = chip->ssg_freq_cnt[0];
323 chip->ssg_freq_cnt[3] = chip->ssg_freq_cnt[2];
324 chip->ssg_freq_cnt[5] = chip->ssg_freq_cnt[4];
325 chip->ssg_freq_cnt[7] = chip->ssg_freq_cnt[6];
326
327 int cnt_of = (chip->ssg_sel_freq & 0xfff) <= chip->ssg_freq_cnt[0];
328
329 chip->ssg_cnt_of_l = cnt_of;
330 chip->ssg_sel_freq_l = (chip->ssg_sel_freq >> 12) & 0xf;
331
332 chip->ssg_cnt_of[0] = (chip->ssg_cnt_of[1] << 1) | cnt_of;
333
334 if (chip->ic)
335 chip->ssg_sign[0] = 0;
336 else
337 chip->ssg_sign[0] = chip->ssg_sign[1] ^ chip->ssg_sign_toggle;
338
339 int cnt = chip->ssg_freq_cnt2[7] + chip->ssg_cnt2_add;
340 chip->ssg_freq_cnt2[0] = cnt & 0xf;
341 chip->ssg_freq_cnt2[2] = chip->ssg_fr_rst_l ? 0 : chip->ssg_freq_cnt2[1];
342 chip->ssg_freq_cnt2[4] = chip->ssg_freq_cnt2[3];
343 chip->ssg_freq_cnt2[6] = chip->ssg_freq_cnt2[5];
344
345 chip->ssg_sel_eg_l[1] = chip->ssg_sel_eg_l[0];
346
347 chip->ssg_ch_of = (!chip->ssg_sel_eg_l[0] && cnt_of) || chip->ic;
348
349 chip->ssg_cnt_reload = chip->ssg_sel_eg_l[0] && chip->ssg_egtrig_s;
350
351 chip->ssg_eg_of[1] = chip->ssg_eg_of[0];
352
353
354 chip->ssg_noise_cnt[0] = chip->ssg_noise_step ? 0 : ((chip->ssg_noise_cnt[1] + chip->ssg_noise_add) & 63);
355
356 chip->ssg_noise_of_low = (chip->ssg_noise_cnt[1] & 1) != 0 && chip->ssg_noise_add;
357
358 }
359
360 if (!chip->ssg_noise_step)
361 {
362 int bit = ((chip->ssg_noise_lfsr[1] >> 16) ^ (chip->ssg_noise_lfsr[1] >> 13)) & 1;
363
364 if ((chip->ssg_noise_lfsr[1] & 0x1ffff) == 0)
365 bit |= 1;
366
367 chip->ssg_noise_lfsr[0] = (chip->ssg_noise_lfsr[1] << 1) | bit;
368 }
369 else
370 {
371 chip->ssg_noise_lfsr[1] = chip->ic ? 0 : chip->ssg_noise_lfsr[0];
372 }
373
374 if (chip->ssg_clk2)
375 chip->ssg_noise_bit = (chip->ssg_noise_lfsr[1] >> 16) & 1;
376
377 int envlevel = chip->ssg_hold[0] ? 31 : chip->ssg_envcnt[0];
378 envlevel = (chip->ssg_dir[0] ^ ((chip->ssg_envmode >> 2) & 1)) == 0 ? (envlevel ^ 31) : envlevel;
379 envlevel = (chip->ssg_t2[0] && (chip->ssg_envmode & 8) == 0) ? 0 : envlevel;
380
381 int vol_a = (chip->ssg_level_a & 0x10) != 0 ? envlevel : (((chip->ssg_level_a & 15) << 1) | 1);
382 int vol_b = (chip->ssg_level_b & 0x10) != 0 ? envlevel : (((chip->ssg_level_b & 15) << 1) | 1);
383 int vol_c = (chip->ssg_level_c & 0x10) != 0 ? envlevel : (((chip->ssg_level_c & 15) << 1) | 1);
384
385 int sign_a = ((chip->ssg_mode & 1) == 0 && (chip->ssg_sign[0] & 1) != 0) || ((chip->ssg_mode & 8) == 0 && chip->ssg_noise_bit);
386 int sign_b = ((chip->ssg_mode & 2) == 0 && (chip->ssg_sign[0] & 2) != 0) || ((chip->ssg_mode & 16) == 0 && chip->ssg_noise_bit);
387 int sign_c = ((chip->ssg_mode & 4) == 0 && (chip->ssg_sign[0] & 4) != 0) || ((chip->ssg_mode & 32) == 0 && chip->ssg_noise_bit);
388
389 static const float volume_lut_ay[32] = {
390 0.0000,
391 0.0000,
392 0.0106,
393 0.0106,
394 0.0150,
395 0.0150,
396 0.0222,
397 0.0222,
398 0.0320,
399 0.0320,
400 0.0466,
401 0.0466,
402 0.0665,
403 0.0665,
404 0.1039,
405 0.1039,
406 0.1237,
407 0.1237,
408 0.1986,
409 0.1986,
410 0.2803,
411 0.2803,
412 0.3548,
413 0.3548,
414 0.4702,
415 0.4702,
416 0.6030,
417 0.6030,
418 0.7530,
419 0.7530,
420 0.9250,
421 0.9250
422 };
423
424 static const float volume_lut_ssg[32] = {
425 0.0000, 0.0000, 0.0049, 0.0075, 0.0105, 0.0131, 0.0156, 0.0183,
426 0.0228, 0.0276, 0.0321, 0.0367, 0.0448, 0.0535, 0.0626, 0.0713,
427 0.0884, 0.1057, 0.1225, 0.1392, 0.1691, 0.2013, 0.2348, 0.2670,
428 0.3307, 0.3951, 0.4573, 0.5196, 0.6316, 0.7528, 0.8787, 1.0000
429 };
430
431 if (chip->type & 1) {
432 chip->o_analog[0] = volume_lut_ay[sign_a ? 0 : vol_a] * 11806;
433 chip->o_analog[1] = volume_lut_ay[sign_b ? 0 : vol_b] * 11806;
434 chip->o_analog[2] = volume_lut_ay[sign_c ? 0 : vol_c] * 11806;
435 } else {
436 chip->o_analog[0] = volume_lut_ssg[sign_a ? 0 : vol_a] * 10922;
437 chip->o_analog[1] = volume_lut_ssg[sign_b ? 0 : vol_b] * 10922;
438 chip->o_analog[2] = volume_lut_ssg[sign_c ? 0 : vol_c] * 10922;
439 }
440 }
441
442 {
443 chip->read_bus = 0; // FIXME
444 if (chip->ssg_read1)
445 chip->read_bus = chip->data_bus1 & 255;
446
447 chip->o_data = chip->read_bus;
448 }
449
450 if (clk)
451 chip->input.cs=1;
452 }
453
454 void SSG_Reset(ssg_t* chip) {
455 memset(chip,0,sizeof(ssg_t));
456
457 chip->input.test=1;
458 chip->input.ic=1;
459 SSG_Clock(chip,0);
460 SSG_Clock(chip,1);
461
462 chip->input.ic=0;
463 SSG_Clock(chip,0);
464 SSG_Clock(chip,1);
465
466 chip->input.ic=1;
467 SSG_Clock(chip,0);
468 SSG_Clock(chip,1);
469
470 chip->input.cs=1;
471 }
472
473 void SSG_Write(ssg_t* chip, unsigned char addr, unsigned char val) {
474 chip->input.cs=0;
475 chip->input.rd=1;
476 chip->input.wr=0;
477
478 chip->input.a0=addr;
479 chip->input.data=val;
480 }
481
482 void SSG_SetType(ssg_t* chip, int type) {
483 chip->type=type;
484 }