forked from buricco/marduk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
emu2149.c
440 lines (381 loc) · 9.05 KB
/
emu2149.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/*
* Copyright (C) 2001-2022 Mitsutaka Okazaki.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following condition: The
* above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
/*
* This source refers to the following documents. The author would like to thank all the authors who have
* contributed to the writing of them.
* - psg.vhd -- 2000 written by Kazuhiro Tsujikawa.
* - s_fme7.c -- 1999,2000 written by Mamiya (NEZplug).
* - ay8910.c -- 1998-2001 Author unknown (MAME).
* - MSX-Datapack -- 1991 ASCII Corp.
* - AY-3-8910 data sheet
*/
#include <stdlib.h>
#include <string.h>
#include "emu2149.h"
static uint32_t voltbl[2][32] = {
/* YM2149 - 32 steps */
{0x00, 0x01, 0x01, 0x02, 0x02, 0x03, 0x03, 0x04, 0x05, 0x06, 0x07, 0x09,
0x0B, 0x0D, 0x0F, 0x12,
0x16, 0x1A, 0x1F, 0x25, 0x2D, 0x35, 0x3F, 0x4C, 0x5A, 0x6A, 0x7F, 0x97,
0xB4, 0xD6, 0xFF, 0xFF},
/* AY-3-8910 - 16 steps */
{0x00, 0x00, 0x03, 0x03, 0x04, 0x04, 0x06, 0x06, 0x09, 0x09, 0x0D, 0x0D,
0x12, 0x12, 0x1D, 0x1D,
0x22, 0x22, 0x37, 0x37, 0x4D, 0x4D, 0x62, 0x62, 0x82, 0x82, 0xA6, 0xA6,
0xD0, 0xD0, 0xFF, 0xFF}
};
/* reg mask of 0x07 = 0x3f, wtf why not 0xff? - zDebel*/
static const uint8_t regmsk[16] = {
0xff, 0x0f, 0xff, 0x0f, 0xff, 0x0f, 0x1f, 0xff,
0x1f, 0x1f, 0x1f, 0xff, 0xff, 0x0f, 0xff, 0xff
};
#define GETA_BITS 24
static void
internal_refresh (PSG * psg)
{
uint32_t f_master = psg->clk;
if (psg->clk_div)
f_master /= 2;
if (psg->quality)
{
psg->base_incr = 1 << GETA_BITS;
psg->realstep = f_master;
psg->psgstep = psg->rate * 8;
psg->psgtime = 0;
psg->freq_limit = (uint32_t)(f_master / 16 / (psg->rate / 2));
}
else
{
psg->base_incr = (uint32_t)((double)f_master * (1 << GETA_BITS) / 8 / psg->rate);
psg->freq_limit = 0;
}
}
void
PSG_setClock(PSG *psg, uint32_t clock)
{
if (psg->clk != clock) {
psg->clk = clock;
internal_refresh(psg);
}
}
void
PSG_setClockDivider(PSG *psg, uint8_t enable)
{
if (psg->clk_div != enable) {
psg->clk_div = enable;
internal_refresh (psg);
}
}
void
PSG_setRate (PSG * psg, uint32_t rate)
{
uint32_t r = rate ? rate : 44100;
if (psg->rate != r) {
psg->rate = r;
internal_refresh(psg);
}
}
void
PSG_setQuality (PSG * psg, uint8_t q)
{
if (psg->quality != q) {
psg->quality = q;
internal_refresh(psg);
}
}
PSG *
PSG_new (uint32_t clock, uint32_t rate)
{
PSG *psg;
psg = (PSG *) calloc (1, sizeof (PSG));
if (psg == NULL)
return NULL;
PSG_setVolumeMode(psg, 0);
psg->clk = clock;
psg->clk_div = 0;
psg->rate = rate ? rate : 44100;
psg->quality = 0;
internal_refresh(psg);
PSG_setMask(psg, 0x00);
return psg;
}
void
PSG_setVolumeMode (PSG * psg, int type)
{
switch (type)
{
case 1:
psg->voltbl = voltbl[0]; /* YM2149 */
break;
case 2:
psg->voltbl = voltbl[1]; /* AY-3-8910 */
break;
default:
psg->voltbl = voltbl[0]; /* fallback: YM2149 */
break;
}
}
uint32_t
PSG_setMask (PSG *psg, uint32_t mask)
{
uint32_t ret = 0;
if(psg)
{
ret = psg->mask;
psg->mask = mask;
}
return ret;
}
uint32_t
PSG_toggleMask (PSG *psg, uint32_t mask)
{
uint32_t ret = 0;
if(psg)
{
ret = psg->mask;
psg->mask ^= mask;
}
return ret;
}
void
PSG_reset (PSG * psg)
{
int i;
psg->base_count = 0;
for (i = 0; i < 3; i++)
{
psg->count[i] = 0;
psg->freq[i] = 0;
psg->edge[i] = 0;
psg->volume[i] = 0;
psg->ch_out[i] = 0;
}
psg->mask = 0;
for (i = 0; i < 16; i++)
psg->reg[i] = 0;
psg->adr = 0;
psg->noise_seed = 0xffff;
psg->noise_scaler = 0;
psg->noise_count = 0;
psg->noise_freq = 0;
psg->env_ptr = 0;
psg->env_freq = 0;
psg->env_count = 0;
psg->env_pause = 1;
psg->out = 0;
}
void
PSG_delete (PSG * psg)
{
free (psg);
}
uint8_t
PSG_readIO (PSG * psg)
{
return (uint8_t) (psg->reg[psg->adr]);
}
uint8_t
PSG_readReg (PSG * psg, uint32_t reg)
{
return (uint8_t) (psg->reg[reg & 0x1f]);
}
void
PSG_writeIO (PSG * psg, uint32_t adr, uint32_t val)
{
if (adr & 1)
PSG_writeReg (psg, psg->adr, val);
else
psg->adr = val & 0x1f;
}
static inline void
update_output (PSG * psg)
{
int i, noise;
uint8_t incr;
psg->base_count += psg->base_incr;
incr = (psg->base_count >> GETA_BITS);
psg->base_count &= (1 << GETA_BITS) - 1;
/* Envelope */
psg->env_count += incr;
if (psg->env_count >= psg->env_freq)
{
if (!psg->env_pause)
{
if(psg->env_face)
psg->env_ptr = (psg->env_ptr + 1) & 0x3f ;
else
psg->env_ptr = (psg->env_ptr + 0x3f) & 0x3f;
}
if (psg->env_ptr & 0x20) /* if carry or borrow */
{
if (psg->env_continue)
{
if (psg->env_alternate^psg->env_hold) psg->env_face ^= 1;
if (psg->env_hold) psg->env_pause = 1;
psg->env_ptr = psg->env_face ? 0 : 0x1f;
}
else
{
psg->env_pause = 1;
psg->env_ptr = 0;
}
}
if (psg->env_freq >= incr)
psg->env_count -= psg->env_freq;
else
psg->env_count = 0;
}
/* Noise */
psg->noise_count += incr;
if (psg->noise_count >= psg->noise_freq)
{
psg->noise_scaler ^= 1;
if (psg->noise_scaler)
{
if (psg->noise_seed & 1)
psg->noise_seed ^= 0x24000;
psg->noise_seed >>= 1;
}
if (psg->noise_freq >= incr)
psg->noise_count -= psg->noise_freq;
else
psg->noise_count = 0;
}
noise = psg->noise_seed & 1;
/* Tone */
for (i = 0; i < 3; i++)
{
psg->count[i] += incr;
if (psg->count[i] >= psg->freq[i])
{
psg->edge[i] = !psg->edge[i];
if (psg->freq[i] >= incr)
psg->count[i] -= psg->freq[i];
else
psg->count[i] = 0;
}
if (0 < psg->freq_limit && psg->freq[i] <= psg->freq_limit)
{
/* Mute the channel if the pitch is higher than the Nyquist frequency at the current sample rate,
* to prevent aliased or broken tones from being generated. Of course, this logic doesn't exist
* on the actual chip, but practically all tones higher than the Nyquist frequency are usually
* removed by a low-pass circuit somewhere, so we here halt the output. */
continue;
}
if (psg->mask & PSG_MASK_CH(i))
{
psg->ch_out[i] = 0;
continue;
}
if ((psg->tmask[i]||psg->edge[i]) && (psg->nmask[i]||noise))
{
if (!(psg->volume[i] & 32))
psg->ch_out[i] = (psg->voltbl[psg->volume[i] & 31] << 4);
else
psg->ch_out[i] = (psg->voltbl[psg->env_ptr] << 4);
}
else
{
psg->ch_out[i] = 0;
}
}
}
static inline int16_t
mix_output(PSG *psg)
{
return (int16_t)(psg->ch_out[0] + psg->ch_out[1] + psg->ch_out[2]);
}
int16_t
PSG_calc (PSG * psg)
{
if (!psg->quality)
{
update_output(psg);
psg->out = mix_output(psg);
}
else
{
/* Simple rate converter (See README for detail). */
while (psg->realstep > psg->psgtime)
{
psg->psgtime += psg->psgstep;
update_output(psg);
psg->out += mix_output(psg);
psg->out >>= 1;
}
psg->psgtime -= psg->realstep;
}
return psg->out;
}
void
PSG_writeReg (PSG * psg, uint32_t reg, uint32_t val)
{
int c;
if (reg > 15) return;
val &= regmsk[reg];
psg->reg[reg] = (uint8_t) val;
switch (reg)
{
case 0:
case 2:
case 4:
case 1:
case 3:
case 5:
c = reg >> 1;
psg->freq[c] = ((psg->reg[c * 2 + 1] & 15) << 8) + psg->reg[c * 2];
break;
case 6:
psg->noise_freq = val & 31;
break;
case 7:
psg->tmask[0] = (val & 1);
psg->tmask[1] = (val & 2);
psg->tmask[2] = (val & 4);
psg->nmask[0] = (val & 8);
psg->nmask[1] = (val & 16);
psg->nmask[2] = (val & 32);
break;
case 8:
case 9:
case 10:
psg->volume[reg - 8] = val << 1;
break;
case 11:
case 12:
psg->env_freq = (psg->reg[12] << 8) + psg->reg[11];
break;
case 13:
psg->env_continue = (val >> 3) & 1;
psg->env_attack = (val >> 2) & 1;
psg->env_alternate = (val >> 1) & 1;
psg->env_hold = val & 1;
psg->env_face = psg->env_attack;
psg->env_pause = 0;
psg->env_ptr = psg->env_face ? 0 : 0x1f;
break;
case 14:
case 15:
default:
break;
}
return;
}