1 // This file is taken from the openMSX project.
2 // The file has been modified to be built in the blueMSX environment.
3
4 // $Id: OpenMsxYMF278.cpp,v 1.6 2008/03/31 22:07:05 hap-hap Exp $
5
6 #include "OpenMsxYMF278.h"
7
8 #include <stdio.h>
9 #include <cmath>
10 #include <cstring>
11 #include <stdlib.h>
12
13 extern "C" {
14 #include "SaveState.h"
15 }
16
17 const int EG_SH = 16; // 16.16 fixed point (EG timing)
18 const unsigned int EG_TIMER_OVERFLOW = 1 << EG_SH;
19
20 // envelope output entries
21 const int ENV_BITS = 10;
22 const int ENV_LEN = 1 << ENV_BITS;
23 const DoubleT ENV_STEP = 128.0 / ENV_LEN;
24 const int MAX_ATT_INDEX = (1 << (ENV_BITS - 1)) - 1; //511
25 const int MIN_ATT_INDEX = 0;
26
27 // Envelope Generator phases
28 const int EG_ATT = 4;
29 const int EG_DEC = 3;
30 const int EG_SUS = 2;
31 const int EG_REL = 1;
32 const int EG_OFF = 0;
33
34 const int EG_REV = 5; //pseudo reverb
35 const int EG_DMP = 6; //damp
36
37 // Pan values, units are -3dB, i.e. 8.
38 const int pan_left[16] = {
39 0, 8, 16, 24, 32, 40, 48, 256, 256, 0, 0, 0, 0, 0, 0, 0
40 };
41 const int pan_right[16] = {
42 0, 0, 0, 0, 0, 0, 0, 0, 256, 256, 48, 40, 32, 24, 16, 8
43 };
44
45 // Mixing levels, units are -3dB, and add some marging to avoid clipping
46 const int mix_level[8] = {
47 8, 16, 24, 32, 40, 48, 56, 256
48 };
49
50 // decay level table (3dB per step)
51 // 0 - 15: 0, 3, 6, 9,12,15,18,21,24,27,30,33,36,39,42,93 (dB)
52 #define SC(db) (unsigned int)(db * (2.0 / ENV_STEP))
53 const unsigned int dl_tab[16] = {
54 SC( 0), SC( 1), SC( 2), SC(3 ), SC(4 ), SC(5 ), SC(6 ), SC( 7),
55 SC( 8), SC( 9), SC(10), SC(11), SC(12), SC(13), SC(14), SC(31)
56 };
57 #undef SC
58
59 const byte RATE_STEPS = 8;
60 const byte eg_inc[15 * RATE_STEPS] = {
61 //cycle:0 1 2 3 4 5 6 7
62 0, 1, 0, 1, 0, 1, 0, 1, // 0 rates 00..12 0 (increment by 0 or 1)
63 0, 1, 0, 1, 1, 1, 0, 1, // 1 rates 00..12 1
64 0, 1, 1, 1, 0, 1, 1, 1, // 2 rates 00..12 2
65 0, 1, 1, 1, 1, 1, 1, 1, // 3 rates 00..12 3
66
67 1, 1, 1, 1, 1, 1, 1, 1, // 4 rate 13 0 (increment by 1)
68 1, 1, 1, 2, 1, 1, 1, 2, // 5 rate 13 1
69 1, 2, 1, 2, 1, 2, 1, 2, // 6 rate 13 2
70 1, 2, 2, 2, 1, 2, 2, 2, // 7 rate 13 3
71
72 2, 2, 2, 2, 2, 2, 2, 2, // 8 rate 14 0 (increment by 2)
73 2, 2, 2, 4, 2, 2, 2, 4, // 9 rate 14 1
74 2, 4, 2, 4, 2, 4, 2, 4, // 10 rate 14 2
75 2, 4, 4, 4, 2, 4, 4, 4, // 11 rate 14 3
76
77 4, 4, 4, 4, 4, 4, 4, 4, // 12 rates 15 0, 15 1, 15 2, 15 3 for decay
78 8, 8, 8, 8, 8, 8, 8, 8, // 13 rates 15 0, 15 1, 15 2, 15 3 for attack (zero time)
79 0, 0, 0, 0, 0, 0, 0, 0, // 14 infinity rates for attack and decay(s)
80 };
81
82 #define O(a) (a * RATE_STEPS)
83 const byte eg_rate_select[64] = {
84 O( 0),O( 1),O( 2),O( 3),
85 O( 0),O( 1),O( 2),O( 3),
86 O( 0),O( 1),O( 2),O( 3),
87 O( 0),O( 1),O( 2),O( 3),
88 O( 0),O( 1),O( 2),O( 3),
89 O( 0),O( 1),O( 2),O( 3),
90 O( 0),O( 1),O( 2),O( 3),
91 O( 0),O( 1),O( 2),O( 3),
92 O( 0),O( 1),O( 2),O( 3),
93 O( 0),O( 1),O( 2),O( 3),
94 O( 0),O( 1),O( 2),O( 3),
95 O( 0),O( 1),O( 2),O( 3),
96 O( 0),O( 1),O( 2),O( 3),
97 O( 4),O( 5),O( 6),O( 7),
98 O( 8),O( 9),O(10),O(11),
99 O(12),O(12),O(12),O(12),
100 };
101 #undef O
102
103 //rate 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
104 //shift 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 0, 0, 0
105 //mask 4095, 2047, 1023, 511, 255, 127, 63, 31, 15, 7, 3, 1, 0, 0, 0, 0
106 #define O(a) (a)
107 const byte eg_rate_shift[64] = {
108 O(12),O(12),O(12),O(12),
109 O(11),O(11),O(11),O(11),
110 O(10),O(10),O(10),O(10),
111 O( 9),O( 9),O( 9),O( 9),
112 O( 8),O( 8),O( 8),O( 8),
113 O( 7),O( 7),O( 7),O( 7),
114 O( 6),O( 6),O( 6),O( 6),
115 O( 5),O( 5),O( 5),O( 5),
116 O( 4),O( 4),O( 4),O( 4),
117 O( 3),O( 3),O( 3),O( 3),
118 O( 2),O( 2),O( 2),O( 2),
119 O( 1),O( 1),O( 1),O( 1),
120 O( 0),O( 0),O( 0),O( 0),
121 O( 0),O( 0),O( 0),O( 0),
122 O( 0),O( 0),O( 0),O( 0),
123 O( 0),O( 0),O( 0),O( 0),
124 };
125 #undef O
126
127
128 //number of steps to take in quarter of lfo frequency
129 //TODO check if frequency matches real chip
130 #define O(a) ((int)((EG_TIMER_OVERFLOW / a) / 6))
131 const int lfo_period[8] = {
132 O(0.168), O(2.019), O(3.196), O(4.206),
133 O(5.215), O(5.888), O(6.224), O(7.066)
134 };
135 #undef O
136
137
138 #define O(a) ((int)(a * 65536))
139 const int vib_depth[8] = {
140 O(0), O(3.378), O(5.065), O(6.750),
141 O(10.114), O(20.170), O(40.106), O(79.307)
142 };
143 #undef O
144
145
146 #define SC(db) static_cast<int>(db * (2.0 / ENV_STEP))
147 const int am_depth[8] = {
148 SC(0), SC(1.781), SC(2.906), SC(3.656),
149 SC(4.406), SC(5.906), SC(7.406), SC(11.91)
150 };
151 #undef SC
152
153
YMF278Slot()154 YMF278Slot::YMF278Slot()
155 {
156 reset();
157 }
158
reset()159 void YMF278Slot::reset()
160 {
161 wave = FN = OCT = PRVB = LD = TL = pan = lfo = vib = AM = 0;
162 AR = D1R = DL = D2R = RC = RR = 0;
163 step = stepptr = 0;
164 bits = startaddr = loopaddr = endaddr = 0;
165 env_vol = MAX_ATT_INDEX;
166 //env_vol_step = env_vol_lim = 0;
167
168 lfo_active = false;
169 lfo_cnt = lfo_step = 0;
170 lfo_max = lfo_period[0];
171
172 state = EG_OFF;
173 active = false;
174 }
175
compute_rate(int val)176 int YMF278Slot::compute_rate(int val)
177 {
178 if (val == 0) {
179 return 0;
180 } else if (val == 15) {
181 return 63;
182 }
183 int res;
184 if (RC != 15) {
185 int oct = OCT;
186 if (oct & 8) {
187 oct |= -8;
188 }
189 res = (oct + RC) * 2 + (FN & 0x200 ? 1 : 0) + val * 4;
190 } else {
191 res = val * 4;
192 }
193 if (res < 0) {
194 res = 0;
195 } else if (res > 63) {
196 res = 63;
197 }
198 return res;
199 }
200
compute_vib()201 int YMF278Slot::compute_vib()
202 {
203 return (((lfo_step << 8) / lfo_max) * vib_depth[(int)vib]) >> 24;
204 }
205
206
compute_am()207 int YMF278Slot::compute_am()
208 {
209 if (lfo_active && AM) {
210 return (((lfo_step << 8) / lfo_max) * am_depth[(int)AM]) >> 12;
211 } else {
212 return 0;
213 }
214 }
215
set_lfo(int newlfo)216 void YMF278Slot::set_lfo(int newlfo)
217 {
218 lfo_step = (((lfo_step << 8) / lfo_max) * newlfo) >> 8;
219 lfo_cnt = (((lfo_cnt << 8) / lfo_max) * newlfo) >> 8;
220
221 lfo = newlfo;
222 lfo_max = lfo_period[(int)lfo];
223 }
224
225
advance()226 void YMF278::advance()
227 {
228 eg_timer += eg_timer_add;
229
230 if (eg_timer > 4 * EG_TIMER_OVERFLOW) {
231 eg_timer = EG_TIMER_OVERFLOW;
232 }
233
234 while (eg_timer >= EG_TIMER_OVERFLOW) {
235 eg_timer -= EG_TIMER_OVERFLOW;
236 eg_cnt++;
237
238 for (int i = 0; i < 24; i++) {
239 YMF278Slot &op = slots[i];
240
241 if (op.lfo_active) {
242 op.lfo_cnt++;
243 if (op.lfo_cnt < op.lfo_max) {
244 op.lfo_step++;
245 } else if (op.lfo_cnt < (op.lfo_max * 3)) {
246 op.lfo_step--;
247 } else {
248 op.lfo_step++;
249 if (op.lfo_cnt == (op.lfo_max * 4)) {
250 op.lfo_cnt = 0;
251 }
252 }
253 }
254
255 // Envelope Generator
256 switch(op.state) {
257 case EG_ATT: { // attack phase
258 byte rate = op.compute_rate(op.AR);
259 if (rate < 4) {
260 break;
261 }
262 byte shift = eg_rate_shift[rate];
263 if (!(eg_cnt & ((1 << shift) -1))) {
264 byte select = eg_rate_select[rate];
265 op.env_vol += (~op.env_vol * eg_inc[select + ((eg_cnt >> shift) & 7)]) >> 3;
266 if (op.env_vol <= MIN_ATT_INDEX) {
267 op.env_vol = MIN_ATT_INDEX;
268 if (op.DL == 0) {
269 op.state = EG_SUS;
270 }
271 else {
272 op.state = EG_DEC;
273 }
274 }
275 }
276 break;
277 }
278 case EG_DEC: { // decay phase
279 byte rate = op.compute_rate(op.D1R);
280 if (rate < 4) {
281 break;
282 }
283 byte shift = eg_rate_shift[rate];
284 if (!(eg_cnt & ((1 << shift) -1))) {
285 byte select = eg_rate_select[rate];
286 op.env_vol += eg_inc[select + ((eg_cnt >> shift) & 7)];
287
288 if (((unsigned int)op.env_vol > dl_tab[6]) && op.PRVB) {
289 op.state = EG_REV;
290 } else {
291 if (op.env_vol >= op.DL) {
292 op.state = EG_SUS;
293 }
294 }
295 }
296 break;
297 }
298 case EG_SUS: { // sustain phase
299 byte rate = op.compute_rate(op.D2R);
300 if (rate < 4) {
301 break;
302 }
303 byte shift = eg_rate_shift[rate];
304 if (!(eg_cnt & ((1 << shift) -1))) {
305 byte select = eg_rate_select[rate];
306 op.env_vol += eg_inc[select + ((eg_cnt >> shift) & 7)];
307
308 if (((unsigned int)op.env_vol > dl_tab[6]) && op.PRVB) {
309 op.state = EG_REV;
310 } else {
311 if (op.env_vol >= MAX_ATT_INDEX) {
312 op.env_vol = MAX_ATT_INDEX;
313 op.active = false;
314 checkMute();
315 }
316 }
317 }
318 break;
319 }
320 case EG_REL: { // release phase
321 byte rate = op.compute_rate(op.RR);
322 if (rate < 4) {
323 break;
324 }
325 byte shift = eg_rate_shift[rate];
326 if (!(eg_cnt & ((1 << shift) -1))) {
327 byte select = eg_rate_select[rate];
328 op.env_vol += eg_inc[select + ((eg_cnt >> shift) & 7)];
329
330 if (((unsigned int)op.env_vol > dl_tab[6]) && op.PRVB) {
331 op.state = EG_REV;
332 } else {
333 if (op.env_vol >= MAX_ATT_INDEX) {
334 op.env_vol = MAX_ATT_INDEX;
335 op.active = false;
336 checkMute();
337 }
338 }
339 }
340 break;
341 }
342 case EG_REV: { //pseudo reverb
343 //TODO improve env_vol update
344 byte rate = op.compute_rate(5);
345 //if (rate < 4) {
346 // break;
347 //}
348 byte shift = eg_rate_shift[rate];
349 if (!(eg_cnt & ((1 << shift) - 1))) {
350 byte select = eg_rate_select[rate];
351 op.env_vol += eg_inc[select + ((eg_cnt >> shift) & 7)];
352
353 if (op.env_vol >= MAX_ATT_INDEX) {
354 op.env_vol = MAX_ATT_INDEX;
355 op.active = false;
356 checkMute();
357 }
358 }
359 break;
360 }
361 case EG_DMP: { //damping
362 //TODO improve env_vol update, damp is just fastest decay now
363 byte rate = 56;
364 byte shift = eg_rate_shift[rate];
365 if (!(eg_cnt & ((1 << shift) - 1))) {
366 byte select = eg_rate_select[rate];
367 op.env_vol += eg_inc[select + ((eg_cnt >> shift) & 7)];
368
369 if (op.env_vol >= MAX_ATT_INDEX) {
370 op.env_vol = MAX_ATT_INDEX;
371 op.active = false;
372 checkMute();
373 }
374 }
375 break;
376 }
377 case EG_OFF:
378 // nothing
379 break;
380
381 default:
382 break;
383 }
384 }
385 }
386 }
387
getSample(YMF278Slot & op)388 short YMF278::getSample(YMF278Slot &op)
389 {
390 short sample;
391 switch (op.bits) {
392 case 0: {
393 // 8 bit
394 sample = readMem(op.startaddr + op.pos) << 8;
395 break;
396 }
397 case 1: {
398 // 12 bit
399 int addr = op.startaddr + ((op.pos / 2) * 3);
400 if (op.pos & 1) {
401 sample = readMem(addr + 2) << 8 |
402 ((readMem(addr + 1) << 4) & 0xF0);
403 } else {
404 sample = readMem(addr + 0) << 8 |
405 (readMem(addr + 1) & 0xF0);
406 }
407 break;
408 }
409 case 2: {
410 // 16 bit
411 int addr = op.startaddr + (op.pos * 2);
412 sample = (readMem(addr + 0) << 8) |
413 (readMem(addr + 1));
414 break;
415 }
416 default:
417 // TODO unspecified
418 sample = 0;
419 }
420 return sample;
421 }
422
checkMute()423 void YMF278::checkMute()
424 {
425 setInternalMute(!anyActive());
426 }
427
anyActive()428 bool YMF278::anyActive()
429 {
430 for (int i = 0; i < 24; i++) {
431 if (slots[i].active) {
432 return true;
433 }
434 }
435 return false;
436 }
437
updateBuffer(int length)438 int* YMF278::updateBuffer(int length)
439 {
440 if (isInternalMuted()) {
441 return NULL;
442 }
443
444 int vl = mix_level[pcm_l];
445 int vr = mix_level[pcm_r];
446 int *buf = buffer;
447 while (length--) {
448 int left = 0;
449 int right = 0;
450 int cnt = oplOversampling;
451 while (cnt--) {
452 for (int i = 0; i < 24; i++) {
453 YMF278Slot &sl = slots[i];
454 if (!sl.active) {
455 continue;
456 }
457
458 short sample = (sl.sample1 * (0x10000 - sl.stepptr) +
459 sl.sample2 * sl.stepptr) >> 16;
460 int vol = sl.TL + (sl.env_vol >> 2) + sl.compute_am();
461
462 int volLeft = vol + pan_left [(int)sl.pan] + vl;
463 int volRight = vol + pan_right[(int)sl.pan] + vr;
464
465 // TODO prob doesn't happen in real chip
466 if (volLeft < 0) {
467 volLeft = 0;
468 }
469 if (volRight < 0) {
470 volRight = 0;
471 }
472
473 left += (sample * volume[volLeft] ) >> 10;
474 right += (sample * volume[volRight]) >> 10;
475
476 if (sl.lfo_active && sl.vib) {
477 int oct = sl.OCT;
478 if (oct & 8) {
479 oct |= -8;
480 }
481 oct += 5;
482 sl.stepptr += (oct >= 0 ? ((sl.FN | 1024) + sl.compute_vib()) << oct
483 : ((sl.FN | 1024) + sl.compute_vib()) >> -oct) / oplOversampling;
484 } else {
485 sl.stepptr += sl.step / oplOversampling;
486 }
487
488 int count = (sl.stepptr >> 16) & 0x0f;
489 sl.stepptr &= 0xffff;
490 while (count--) {
491 sl.sample1 = sl.sample2;
492 sl.pos++;
493 if (sl.pos >= sl.endaddr) {
494 sl.pos = sl.loopaddr;
495 }
496 sl.sample2 = getSample(sl);
497 }
498 }
499 advance();
500 }
501 *buf++ = left / oplOversampling;
502 *buf++ = right / oplOversampling;
503 }
504 return buffer;
505 }
506
keyOnHelper(YMF278Slot & slot)507 void YMF278::keyOnHelper(YMF278Slot& slot)
508 {
509 slot.active = true;
510 setInternalMute(false);
511
512 int oct = slot.OCT;
513 if (oct & 8) {
514 oct |= -8;
515 }
516 oct += 5;
517 slot.step = oct >= 0 ? (slot.FN | 1024) << oct : (slot.FN | 1024) >> -oct;
518 slot.state = EG_ATT;
519 slot.stepptr = 0;
520 slot.pos = 0;
521 slot.sample1 = getSample(slot);
522 slot.pos = 1;
523 slot.sample2 = getSample(slot);
524 }
525
writeRegOPL4(byte reg,byte data,const EmuTime & time)526 void YMF278::writeRegOPL4(byte reg, byte data, const EmuTime &time)
527 {
528 BUSY_Time = time + 88 * 6 / 9;
529
530 // Handle slot registers specifically
531 if (reg >= 0x08 && reg <= 0xF7) {
532 int snum = (reg - 8) % 24;
533 YMF278Slot& slot = slots[snum];
534 switch ((reg - 8) / 24) {
535 case 0: {
536 LD_Time = time;
537 slot.wave = (slot.wave & 0x100) | data;
538 int base = (slot.wave < 384 || !wavetblhdr) ?
539 (slot.wave * 12) :
540 (wavetblhdr * 0x80000 + ((slot.wave - 384) * 12));
541 byte buf[12];
542 for (int i = 0; i < 12; i++) {
543 buf[i] = readMem(base + i);
544 }
545 slot.bits = (buf[0] & 0xC0) >> 6;
546 slot.set_lfo((buf[7] >> 3) & 7);
547 slot.vib = buf[7] & 7;
548 slot.AR = buf[8] >> 4;
549 slot.D1R = buf[8] & 0xF;
550 slot.DL = dl_tab[buf[9] >> 4];
551 slot.D2R = buf[9] & 0xF;
552 slot.RC = buf[10] >> 4;
553 slot.RR = buf[10] & 0xF;
554 slot.AM = buf[11] & 7;
555 slot.startaddr = buf[2] | (buf[1] << 8) |
556 ((buf[0] & 0x3F) << 16);
557 slot.loopaddr = buf[4] + (buf[3] << 8);
558 slot.endaddr = (((buf[6] + (buf[5] << 8)) ^ 0xFFFF) + 1);
559 if ((regs[reg + 4] & 0x080)) {
560 keyOnHelper(slot);
561 }
562 break;
563 }
564 case 1: {
565 slot.wave = (slot.wave & 0xFF) | ((data & 0x1) << 8);
566 slot.FN = (slot.FN & 0x380) | (data >> 1);
567 int oct = slot.OCT;
568 if (oct & 8) {
569 oct |= -8;
570 }
571 oct += 5;
572 slot.step = oct >= 0 ? (slot.FN | 1024) << oct : (slot.FN | 1024) >> -oct;
573 break;
574 }
575 case 2: {
576 slot.FN = (slot.FN & 0x07F) | ((data & 0x07) << 7);
577 slot.PRVB = ((data & 0x08) >> 3);
578 slot.OCT = ((data & 0xF0) >> 4);
579 int oct = slot.OCT;
580 if (oct & 8) {
581 oct |= -8;
582 }
583 oct += 5;
584 slot.step = oct >= 0 ? (slot.FN | 1024) << oct : (slot.FN | 1024) >> -oct;
585 break;
586 }
587 case 3:
588 slot.TL = data >> 1;
589 slot.LD = data & 0x1;
590
591 // TODO
592 if (slot.LD) {
593 // directly change volume
594 } else {
595 // interpolate volume
596 }
597 break;
598 case 4:
599 if (data & 0x10) {
600 // output to DO1 pin:
601 // this pin is not used in moonsound
602 // we emulate this by muting the sound
603 slot.pan = 8; // both left/right -inf dB
604 } else {
605 slot.pan = data & 0x0F;
606 }
607
608 if (data & 0x020) {
609 // LFO reset
610 slot.lfo_active = false;
611 slot.lfo_cnt = 0;
612 slot.lfo_max = lfo_period[(int)slot.vib];
613 slot.lfo_step = 0;
614 } else {
615 // LFO activate
616 slot.lfo_active = true;
617 }
618
619 switch (data >> 6) {
620 case 0: //tone off, no damp
621 if (slot.active && (slot.state != EG_REV) ) {
622 slot.state = EG_REL;
623 }
624 break;
625 case 2: //tone on, no damp
626 if (!(regs[reg] & 0x080)) {
627 keyOnHelper(slot);
628 }
629 break;
630 case 1: //tone off, damp
631 case 3: //tone on, damp
632 slot.state = EG_DMP;
633 break;
634 }
635 break;
636 case 5:
637 slot.vib = data & 0x7;
638 slot.set_lfo((data >> 3) & 0x7);
639 break;
640 case 6:
641 slot.AR = data >> 4;
642 slot.D1R = data & 0xF;
643 break;
644 case 7:
645 slot.DL = dl_tab[data >> 4];
646 slot.D2R = data & 0xF;
647 break;
648 case 8:
649 slot.RC = data >> 4;
650 slot.RR = data & 0xF;
651 break;
652 case 9:
653 slot.AM = data & 0x7;
654 break;
655 }
656 } else {
657 // All non-slot registers
658 switch (reg) {
659 case 0x00: // TEST
660 case 0x01:
661 break;
662
663 case 0x02:
664 wavetblhdr = (data >> 2) & 0x7;
665 memmode = data & 1;
666 break;
667
668 case 0x03:
669 memadr = (memadr & 0x00FFFF) | (data << 16);
670 break;
671
672 case 0x04:
673 memadr = (memadr & 0xFF00FF) | (data << 8);
674 break;
675
676 case 0x05:
677 memadr = (memadr & 0xFFFF00) | data;
678 break;
679
680 case 0x06: // memory data
681 BUSY_Time += 28 * 6 / 9;
682 writeMem(memadr, data);
683 memadr = (memadr + 1) & 0xFFFFFF;
684 break;
685
686 case 0xF8:
687 // TODO use these
688 fm_l = data & 0x7;
689 fm_r = (data >> 3) & 0x7;
690 break;
691
692 case 0xF9:
693 pcm_l = data & 0x7;
694 pcm_r = (data >> 3) & 0x7;
695 break;
696 }
697 }
698
699 regs[reg] = data;
700 }
701
peekRegOPL4(byte reg,const EmuTime & time)702 byte YMF278::peekRegOPL4(byte reg, const EmuTime &time)
703 {
704 BUSY_Time = time;
705
706 byte result;
707 switch(reg) {
708 case 2: // 3 upper bits are device ID
709 result = (regs[2] & 0x1F) | 0x20;
710 break;
711
712 case 6: // Memory Data Register
713 result = readMem(memadr);
714 break;
715
716 default:
717 result = regs[reg];
718 break;
719 }
720 return result;
721 }
722
readRegOPL4(byte reg,const EmuTime & time)723 byte YMF278::readRegOPL4(byte reg, const EmuTime &time)
724 {
725 BUSY_Time = time;
726
727 byte result;
728 switch(reg) {
729 case 2: // 3 upper bits are device ID
730 result = (regs[2] & 0x1F) | 0x20;
731 break;
732
733 case 6: // Memory Data Register
734 BUSY_Time += 38 * 6 / 9;
735 result = readMem(memadr);
736 memadr = (memadr + 1) & 0xFFFFFF;
737 break;
738
739 default:
740 result = regs[reg];
741 break;
742 }
743 return result;
744 }
745
peekStatus(const EmuTime & time)746 byte YMF278::peekStatus(const EmuTime &time)
747 {
748 byte result = 0;
749 if (time - BUSY_Time < 88 * 6 / 9) {
750 result |= 0x01;
751 }
752 if (time - LD_Time < 10000 * 6 / 9) {
753 result |= 0x02;
754 }
755 return result;
756 }
757
readStatus(const EmuTime & time)758 byte YMF278::readStatus(const EmuTime &time)
759 {
760 byte result = 0;
761 if (time - BUSY_Time < 88 * 6 / 9) {
762 result |= 0x01;
763 }
764 if (time - LD_Time < 10000 * 6 / 9) {
765 result |= 0x02;
766 }
767 return result;
768 }
769
YMF278(short volume,int ramSize,void * romData,int romSize,const EmuTime & time)770 YMF278::YMF278(short volume, int ramSize, void* romData, int romSize,
771 const EmuTime &time)
772 {
773 LD_Time = 0;
774 BUSY_Time = 0;
775 memadr = 0; // avoid UMR
776 endRom = romSize;
777 ramSize *= 1024; // in kb
778
779 this->ramSize = ramSize;
780 rom = (byte*)romData;
781 ram = (byte*)calloc(1, ramSize);
782
783 oplOversampling = 1;
784
785 endRam = endRom + ramSize;
786
787 reset(time);
788 }
789
~YMF278()790 YMF278::~YMF278()
791 {
792 free(ram);
793 free(rom);
794 }
795
reset(const EmuTime & time)796 void YMF278::reset(const EmuTime &time)
797 {
798 eg_timer = 0;
799 eg_cnt = 0;
800
801 int i;
802 for (i = 0; i < 24; i++) {
803 slots[i].reset();
804 }
805 for (i = 255; i >= 0; i--) { // reverse order to avoid UMR
806 writeRegOPL4(i, 0, time);
807 }
808 setInternalMute(true);
809 wavetblhdr = memmode = memadr = 0;
810 fm_l = fm_r = pcm_l = pcm_r = 0;
811 BUSY_Time = time;
812 LD_Time = time;
813 }
814
setSampleRate(int sampleRate,int Oversampling)815 void YMF278::setSampleRate(int sampleRate, int Oversampling)
816 {
817 oplOversampling = Oversampling;
818 eg_timer_add = (unsigned int)((1 << EG_SH) / oplOversampling);
819 }
820
setInternalVolume(short newVolume)821 void YMF278::setInternalVolume(short newVolume)
822 {
823 newVolume /= 32;
824 // Volume table, 1 = -0.375dB, 8 = -3dB, 256 = -96dB
825 int i;
826 for (i = 0; i < 256; i++) {
827 volume[i] = (int)(4.0 * (DoubleT)newVolume * pow(2.0, (-0.375 / 6) * i));
828 }
829 for (i = 256; i < 256 * 4; i++) {
830 volume[i] = 0;
831 }
832 }
833
readMem(unsigned int address)834 byte YMF278::readMem(unsigned int address)
835 {
836 if (address < endRom) {
837 return rom[address];
838 } else if (address < endRam) {
839 return ram[address - endRom];
840 } else {
841 return 255; // TODO check
842 }
843 }
844
writeMem(unsigned int address,byte value)845 void YMF278::writeMem(unsigned int address, byte value)
846 {
847 if (address < endRom) {
848 // can't write to ROM
849 } else if (address < endRam) {
850 ram[address - endRom] = value;
851 } else {
852 // can't write to unmapped memory
853 }
854 }
855
856
857 //REMEMBER TO SAVE RAM
loadState()858 void YMF278::loadState()
859 {
860 SaveState* state = saveStateOpenForRead("ymf278");
861
862 ramSize = saveStateGet(state, "ramSize", 0);
863 eg_cnt = saveStateGet(state, "eg_cnt", 0);
864 eg_timer = saveStateGet(state, "eg_timer", 0);
865 eg_timer_add = saveStateGet(state, "eg_timer_add", 0);
866 eg_timer_overflow = saveStateGet(state, "eg_timer_overflow", 0);
867
868 wavetblhdr = (char)saveStateGet(state, "wavetblhdr", 0);
869 memmode = (char)saveStateGet(state, "memmode", 0);
870 memadr = saveStateGet(state, "memadr", 0);
871
872 fm_l = saveStateGet(state, "fm_l", 0);
873 fm_r = saveStateGet(state, "fm_r", 0);
874 pcm_l = saveStateGet(state, "pcm_l", 0);
875 pcm_r = saveStateGet(state, "pcm_r", 0);
876
877 endRom = saveStateGet(state, "endRom", 0);
878 endRam = saveStateGet(state, "endRam", 0);
879
880 LD_Time = saveStateGet(state, "LD_Time", 0);
881 BUSY_Time = saveStateGet(state, "BUSY_Time", 0);
882
883 saveStateGetBuffer(state, "regs", regs, sizeof(regs));
884 saveStateGetBuffer(state, "ram", ram, ramSize);
885
886 for (int i = 0; i < 24; i++) {
887 char tag[32];
888
889 sprintf(tag, "wave%d", i);
890 slots[i].wave = (short)saveStateGet(state, tag, 0);
891
892 sprintf(tag, "FN%d", i);
893 slots[i].FN = (short)saveStateGet(state, tag, 0);
894
895 sprintf(tag, "OCT%d", i);
896 slots[i].OCT = (char)saveStateGet(state, tag, 0);
897
898 sprintf(tag, "PRVB%d", i);
899 slots[i].PRVB = (char)saveStateGet(state, tag, 0);
900
901 sprintf(tag, "LD%d", i);
902 slots[i].LD = (char)saveStateGet(state, tag, 0);
903
904 sprintf(tag, "TL%d", i);
905 slots[i].TL = (char)saveStateGet(state, tag, 0);
906
907 sprintf(tag, "pan%d", i);
908 slots[i].pan = (char)saveStateGet(state, tag, 0);
909
910 sprintf(tag, "lfo%d", i);
911 slots[i].lfo = (char)saveStateGet(state, tag, 0);
912
913 sprintf(tag, "vib%d", i);
914 slots[i].vib = (char)saveStateGet(state, tag, 0);
915
916 sprintf(tag, "AM%d", i);
917 slots[i].AM = (char)saveStateGet(state, tag, 0);
918
919 sprintf(tag, "AR%d", i);
920 slots[i].AR = (char)saveStateGet(state, tag, 0);
921
922 sprintf(tag, "D1R%d", i);
923 slots[i].D1R = (char)saveStateGet(state, tag, 0);
924
925 sprintf(tag, "DL%d", i);
926 slots[i].DL = saveStateGet(state, tag, 0);
927
928 sprintf(tag, "D2R%d", i);
929 slots[i].D2R = (char)saveStateGet(state, tag, 0);
930
931 sprintf(tag, "RC%d", i);
932 slots[i].RC = (char)saveStateGet(state, tag, 0);
933
934 sprintf(tag, "RR%d", i);
935 slots[i].RR = (char)saveStateGet(state, tag, 0);
936
937 sprintf(tag, "step%d", i);
938 slots[i].step = saveStateGet(state, tag, 0);
939
940 sprintf(tag, "stepptr%d", i);
941 slots[i].stepptr = saveStateGet(state, tag, 0);
942
943 sprintf(tag, "pos%d", i);
944 slots[i].pos = saveStateGet(state, tag, 0);
945
946 sprintf(tag, "sample1%d", i);
947 slots[i].sample1 = (short)saveStateGet(state, tag, 0);
948
949 sprintf(tag, "sample2%d", i);
950 slots[i].sample2 = (short)saveStateGet(state, tag, 0);
951
952 sprintf(tag, "active%d", i);
953 slots[i].active = saveStateGet(state, tag, 0) != 0;
954
955 sprintf(tag, "bits%d", i);
956 slots[i].bits = (char)saveStateGet(state, tag, 0);
957
958 sprintf(tag, "startaddr%d", i);
959 slots[i].startaddr = saveStateGet(state, tag, 0);
960
961 sprintf(tag, "loopaddr%d", i);
962 slots[i].loopaddr = saveStateGet(state, tag, 0);
963
964 sprintf(tag, "endaddr%d", i);
965 slots[i].endaddr = saveStateGet(state, tag, 0);
966
967 sprintf(tag, "state%d", i);
968 slots[i].state = (char)saveStateGet(state, tag, 0);
969
970 sprintf(tag, "env_vol%d", i);
971 slots[i].env_vol = saveStateGet(state, tag, 0);
972
973 sprintf(tag, "env_vol_step%d", i);
974 slots[i].env_vol_step = saveStateGet(state, tag, 0);
975
976 sprintf(tag, "env_vol_lim%d", i);
977 slots[i].env_vol_lim = saveStateGet(state, tag, 0);
978
979 sprintf(tag, "lfo_active%d", i);
980 slots[i].lfo_active = saveStateGet(state, tag, 0) != 0;
981
982 sprintf(tag, "lfo_cnt%d", i);
983 slots[i].lfo_cnt = saveStateGet(state, tag, 0);
984
985 sprintf(tag, "lfo_step%d", i);
986 slots[i].lfo_step = saveStateGet(state, tag, 0);
987
988 sprintf(tag, "lfo_max%d", i);
989 slots[i].lfo_max = saveStateGet(state, tag, 0);
990 }
991
992 saveStateClose(state);
993 }
994
saveState()995 void YMF278::saveState()
996 {
997 SaveState* state = saveStateOpenForWrite("ymf278");
998
999 saveStateSet(state, "ramSize", ramSize);
1000 saveStateSet(state, "eg_cnt", eg_cnt);
1001 saveStateSet(state, "eg_timer", eg_timer);
1002 saveStateSet(state, "eg_timer_add", eg_timer_add);
1003 saveStateSet(state, "eg_timer_overflow", eg_timer_overflow);
1004
1005 saveStateSet(state, "wavetblhdr", wavetblhdr);
1006 saveStateSet(state, "memmode", memmode);
1007 saveStateSet(state, "memadr", memadr);
1008
1009 saveStateSet(state, "fm_l", fm_l);
1010 saveStateSet(state, "fm_r", fm_r);
1011 saveStateSet(state, "pcm_l", pcm_l);
1012 saveStateSet(state, "pcm_r", pcm_r);
1013
1014 saveStateSet(state, "endRom", endRom);
1015 saveStateSet(state, "endRam", endRam);
1016
1017 saveStateSet(state, "LD_Time", LD_Time);
1018 saveStateSet(state, "BUSY_Time", BUSY_Time);
1019
1020 saveStateSetBuffer(state, "regs", regs, sizeof(regs));
1021 saveStateSetBuffer(state, "ram", ram, ramSize);
1022
1023 for (int i = 0; i < 24; i++) {
1024 char tag[32];
1025
1026 sprintf(tag, "wave%d", i);
1027 saveStateSet(state, tag, slots[i].wave);
1028
1029 sprintf(tag, "FN%d", i);
1030 saveStateSet(state, tag, slots[i].FN);
1031
1032 sprintf(tag, "OCT%d", i);
1033 saveStateSet(state, tag, slots[i].OCT);
1034
1035 sprintf(tag, "PRVB%d", i);
1036 saveStateSet(state, tag, slots[i].PRVB);
1037
1038 sprintf(tag, "LD%d", i);
1039 saveStateSet(state, tag, slots[i].LD);
1040
1041 sprintf(tag, "TL%d", i);
1042 saveStateSet(state, tag, slots[i].TL);
1043
1044 sprintf(tag, "pan%d", i);
1045 saveStateSet(state, tag, slots[i].pan);
1046
1047 sprintf(tag, "lfo%d", i);
1048 saveStateSet(state, tag, slots[i].lfo);
1049
1050 sprintf(tag, "vib%d", i);
1051 saveStateSet(state, tag, slots[i].vib);
1052
1053 sprintf(tag, "AM%d", i);
1054 saveStateSet(state, tag, slots[i].AM);
1055
1056 sprintf(tag, "AR%d", i);
1057 saveStateSet(state, tag, slots[i].AR);
1058
1059 sprintf(tag, "D1R%d", i);
1060 saveStateSet(state, tag, slots[i].D1R);
1061
1062 sprintf(tag, "DL%d", i);
1063 saveStateSet(state, tag, slots[i].DL);
1064
1065 sprintf(tag, "D2R%d", i);
1066 saveStateSet(state, tag, slots[i].D2R);
1067
1068 sprintf(tag, "RC%d", i);
1069 saveStateSet(state, tag, slots[i].RC);
1070
1071 sprintf(tag, "RR%d", i);
1072 saveStateSet(state, tag, slots[i].RR);
1073
1074 sprintf(tag, "step%d", i);
1075 saveStateSet(state, tag, slots[i].step);
1076
1077 sprintf(tag, "stepptr%d", i);
1078 saveStateSet(state, tag, slots[i].stepptr);
1079
1080 sprintf(tag, "pos%d", i);
1081 saveStateSet(state, tag, slots[i].pos);
1082
1083 sprintf(tag, "sample1%d", i);
1084 saveStateSet(state, tag, slots[i].sample1);
1085
1086 sprintf(tag, "sample2%d", i);
1087 saveStateSet(state, tag, slots[i].sample2);
1088
1089 sprintf(tag, "active%d", i);
1090 saveStateSet(state, tag, slots[i].active);
1091
1092 sprintf(tag, "bits%d", i);
1093 saveStateSet(state, tag, slots[i].bits);
1094
1095 sprintf(tag, "startaddr%d", i);
1096 saveStateSet(state, tag, slots[i].startaddr);
1097
1098 sprintf(tag, "loopaddr%d", i);
1099 saveStateSet(state, tag, slots[i].loopaddr);
1100
1101 sprintf(tag, "endaddr%d", i);
1102 saveStateSet(state, tag, slots[i].endaddr);
1103
1104 sprintf(tag, "state%d", i);
1105 saveStateSet(state, tag, slots[i].state);
1106
1107 sprintf(tag, "env_vol%d", i);
1108 saveStateSet(state, tag, slots[i].env_vol);
1109
1110 sprintf(tag, "env_vol_step%d", i);
1111 saveStateSet(state, tag, slots[i].env_vol_step);
1112
1113 sprintf(tag, "env_vol_lim%d", i);
1114 saveStateSet(state, tag, slots[i].env_vol_lim);
1115
1116 sprintf(tag, "lfo_active%d", i);
1117 saveStateSet(state, tag, slots[i].lfo_active);
1118
1119 sprintf(tag, "lfo_cnt%d", i);
1120 saveStateSet(state, tag, slots[i].lfo_cnt);
1121
1122 sprintf(tag, "lfo_step%d", i);
1123 saveStateSet(state, tag, slots[i].lfo_step);
1124
1125 sprintf(tag, "lfo_max%d", i);
1126 saveStateSet(state, tag, slots[i].lfo_max);
1127 }
1128
1129 saveStateClose(state);
1130 }
1131