Debian Codegraph @

src/engine/platform/sound/oki/msm5232.cpp

raw
1 // license:GPL-2.0+
2 // copyright-holders:Jarek Burczynski, Hiromitsu Shioya
3 // additional modifications for Furnace by tildearrow
4 #include "msm5232.h"
5 #include <stdlib.h>
6 #include <string.h>
7
8 #define CLOCK_RATE_DIVIDER 16
9
10 /*
11 OKI MSM5232RS
12 8 channel tone generator
13 */
14
15 msm5232_device::msm5232_device(uint32_t clock)
16 : m_noise_cnt(0), m_noise_step(0), m_noise_rng(0), m_noise_clocks(0), m_UpdateStep(0), m_control1(0), m_control2(0), m_gate(0), m_chip_clock(0), m_rate(0), m_clock(clock)
17 , m_gate_handler_cb(NULL)
18 {
19 }
20
21 //-------------------------------------------------
22 // device_start - device-specific startup
23 //-------------------------------------------------
24
25 void msm5232_device::device_start()
26 {
27 int rate = m_clock/CLOCK_RATE_DIVIDER;
28
29 init(m_clock, rate);
30 }
31
32 //-------------------------------------------------
33 // device_reset - device-specific reset
34 //-------------------------------------------------
35
36 void msm5232_device::device_reset()
37 {
38 for (int i=0; i<8; i++)
39 {
40 write(i, 0x80);
41 write(i, 0x00);
42 }
43 m_noise_cnt = 0;
44 m_noise_rng = 1;
45 m_noise_clocks = 0;
46
47 m_control1 = 0;
48 m_EN_out16[0] = 0;
49 m_EN_out8[0] = 0;
50 m_EN_out4[0] = 0;
51 m_EN_out2[0] = 0;
52
53 m_control2 = 0;
54 m_EN_out16[1] = 0;
55 m_EN_out8[1] = 0;
56 m_EN_out4[1] = 0;
57 m_EN_out2[1] = 0;
58
59 gate_update();
60 }
61
62 //-------------------------------------------------
63 // device_stop - device-specific stop
64 //-------------------------------------------------
65
66 void msm5232_device::device_stop()
67 {
68 }
69
70 void msm5232_device::set_capacitors(double cap1, double cap2, double cap3, double cap4, double cap5, double cap6, double cap7, double cap8)
71 {
72 m_external_capacity[0] = cap1;
73 m_external_capacity[1] = cap2;
74 m_external_capacity[2] = cap3;
75 m_external_capacity[3] = cap4;
76 m_external_capacity[4] = cap5;
77 m_external_capacity[5] = cap6;
78 m_external_capacity[6] = cap7;
79 m_external_capacity[7] = cap8;
80 }
81
82 void msm5232_device::set_vol_input(double v1, double v2, double v3, double v4, double v5, double v6, double v7, double v8)
83 {
84 m_external_input[0] = v1;
85 m_external_input[1] = v2;
86 m_external_input[2] = v3;
87 m_external_input[3] = v4;
88 m_external_input[4] = v5;
89 m_external_input[5] = v6;
90 m_external_input[6] = v7;
91 m_external_input[7] = v8;
92 }
93
94 /* Default chip clock is 2119040 Hz */
95 /* At this clock chip generates exactly 440.0 Hz signal on 8' output when pitch data=0x21 */
96
97
98 /* ROM table to convert from pitch data into data for programmable counter and binary counter */
99 /* Chip has 88x12bits ROM (addressing (in hex) from 0x00 to 0x57) */
100 #define ROM(counter,bindiv) (counter|(bindiv<<9))
101
102 static const uint16_t MSM5232_ROM[88]={
103 /* higher values are Programmable Counter data (9 bits) */
104 /* lesser values are Binary Counter shift data (3 bits) */
105
106 /* 0 */ ROM (506, 7),
107
108 /* 1 */ ROM (478, 7),/* 2 */ ROM (451, 7),/* 3 */ ROM (426, 7),/* 4 */ ROM (402, 7),
109 /* 5 */ ROM (379, 7),/* 6 */ ROM (358, 7),/* 7 */ ROM (338, 7),/* 8 */ ROM (319, 7),
110 /* 9 */ ROM (301, 7),/* A */ ROM (284, 7),/* B */ ROM (268, 7),/* C */ ROM (253, 7),
111
112 /* D */ ROM (478, 6),/* E */ ROM (451, 6),/* F */ ROM (426, 6),/*10 */ ROM (402, 6),
113 /*11 */ ROM (379, 6),/*12 */ ROM (358, 6),/*13 */ ROM (338, 6),/*14 */ ROM (319, 6),
114 /*15 */ ROM (301, 6),/*16 */ ROM (284, 6),/*17 */ ROM (268, 6),/*18 */ ROM (253, 6),
115
116 /*19 */ ROM (478, 5),/*1A */ ROM (451, 5),/*1B */ ROM (426, 5),/*1C */ ROM (402, 5),
117 /*1D */ ROM (379, 5),/*1E */ ROM (358, 5),/*1F */ ROM (338, 5),/*20 */ ROM (319, 5),
118 /*21 */ ROM (301, 5),/*22 */ ROM (284, 5),/*23 */ ROM (268, 5),/*24 */ ROM (253, 5),
119
120 /*25 */ ROM (478, 4),/*26 */ ROM (451, 4),/*27 */ ROM (426, 4),/*28 */ ROM (402, 4),
121 /*29 */ ROM (379, 4),/*2A */ ROM (358, 4),/*2B */ ROM (338, 4),/*2C */ ROM (319, 4),
122 /*2D */ ROM (301, 4),/*2E */ ROM (284, 4),/*2F */ ROM (268, 4),/*30 */ ROM (253, 4),
123
124 /*31 */ ROM (478, 3),/*32 */ ROM (451, 3),/*33 */ ROM (426, 3),/*34 */ ROM (402, 3),
125 /*35 */ ROM (379, 3),/*36 */ ROM (358, 3),/*37 */ ROM (338, 3),/*38 */ ROM (319, 3),
126 /*39 */ ROM (301, 3),/*3A */ ROM (284, 3),/*3B */ ROM (268, 3),/*3C */ ROM (253, 3),
127
128 /*3D */ ROM (478, 2),/*3E */ ROM (451, 2),/*3F */ ROM (426, 2),/*40 */ ROM (402, 2),
129 /*41 */ ROM (379, 2),/*42 */ ROM (358, 2),/*43 */ ROM (338, 2),/*44 */ ROM (319, 2),
130 /*45 */ ROM (301, 2),/*46 */ ROM (284, 2),/*47 */ ROM (268, 2),/*48 */ ROM (253, 2),
131
132 /*49 */ ROM (478, 1),/*4A */ ROM (451, 1),/*4B */ ROM (426, 1),/*4C */ ROM (402, 1),
133 /*4D */ ROM (379, 1),/*4E */ ROM (358, 1),/*4F */ ROM (338, 1),/*50 */ ROM (319, 1),
134 /*51 */ ROM (301, 1),/*52 */ ROM (284, 1),/*53 */ ROM (268, 1),/*54 */ ROM (253, 1),
135
136 /*55 */ ROM (253, 1),/*56 */ ROM (253, 1),
137
138 /*57 */ ROM (13, 7)
139 };
140 #undef ROM
141
142
143 #define STEP_SH (16) /* step calculations accuracy */
144
145 /*
146 * resistance values are guesswork, default capacity is mentioned in the datasheets
147 *
148 * charges external capacitor (default is 0.39uF) via R51
149 * in approx. 5*1400 * 0.39e-6
150 *
151 * external capacitor is discharged through R52
152 * in approx. 5*28750 * 0.39e-6
153 */
154
155
156 #define R51 1400 /* charge resistance */
157 #define R52 28750 /* discharge resistance */
158
159 #if 0
160 /*
161 C24 = external capacity
162
163 osd_printf_debug("Time constant T=R*C =%f sec.\n",R51*C24);
164 osd_printf_debug("Cap fully charged after 5T=%f sec (sample=%f). Level=%f\n",(R51*C24)*5,(R51*C24)*5*sample_rate , VMAX*0.99326 );
165 osd_printf_debug("Cap charged after 5T=%f sec (sample=%f). Level=%20.16f\n",(R51*C24)*5,(R51*C24)*5*sample_rate ,
166 VMAX*(1.0-pow(2.718,-0.0748/(R51*C24))) );
167 */
168 #endif
169
170
171
172
173 void msm5232_device::init_tables()
174 {
175 int i;
176 double scale;
177
178 /* sample rate = chip clock !!! But : */
179 /* highest possible frequency is chipclock/13/16 (pitch data=0x57) */
180 /* at 2MHz : 2000000/13/16 = 9615 Hz */
181
182 i = ((double)(1<<STEP_SH) * (double)m_rate) / (double)m_chip_clock;
183 m_UpdateStep = i;
184 /* printf("clock=%d Hz rate=%d Hz, UpdateStep=%d\n",
185 m_chip_clock, m_rate, m_UpdateStep); */
186
187 scale = ((double)m_chip_clock) / (double)m_rate;
188 m_noise_step = ((1<<STEP_SH)/128.0) * scale; /* step of the rng reg in 16.16 format */
189 /* logerror("noise step=%8x\n", m_noise_step); */
190
191 #if 0
192 {
193 /* rate tables (in milliseconds) */
194 static const int ATBL[8] = { 2,4,8,16, 32,64, 32,64};
195 static const int DTBL[16]= { 40,80,160,320, 640,1280, 640,1280,
196 333,500,1000,2000, 4000,8000, 4000,8000};
197 for (i=0; i<8; i++)
198 {
199 double clockscale = (double)m_chip_clock / 2119040.0;
200 double time = (ATBL[i] / 1000.0) / clockscale; /* attack time in seconds */
201 m_ar_tbl[i] = 0.50 * ( (1.0/time) / (double)m_rate );
202 /* printf("ATBL[%d] = %20.16f time = %f s\n",i, m_ar_tbl[i], time); */
203 }
204
205 for (i=0; i<16; i++)
206 {
207 double clockscale = (double)m_chip_clock / 2119040.0;
208 double time = (DTBL[i] / 1000.0) / clockscale; /* decay time in seconds */
209 m_dr_tbl[i] = 0.50 * ( (1.0/time) / (double)m_rate );
210 /* printf("DTBL[%d] = %20.16f time = %f s\n",i, m_dr_tbl[i], time); */
211 }
212 }
213 #endif
214
215
216 for (i=0; i<8; i++)
217 {
218 double clockscale = (double)m_chip_clock / 2119040.0;
219 m_ar_tbl[i] = ((1<<i) / clockscale) * (double)R51;
220 }
221
222 for (i=0; i<8; i++)
223 {
224 double clockscale = (double)m_chip_clock / 2119040.0;
225 m_dr_tbl[i] = ( (1<<i) / clockscale) * (double)R52;
226 m_dr_tbl[i+8] = (6.25*(1<<i) / clockscale) * (double)R52;
227 }
228 }
229
230
231 void msm5232_device::init_voice(int i)
232 {
233 m_voi[i].ar_rate= m_ar_tbl[0] * m_external_capacity[i];
234 m_voi[i].dr_rate= m_dr_tbl[0] * m_external_capacity[i];
235 m_voi[i].rr_rate= m_dr_tbl[0] * m_external_capacity[i]; /* this is constant value */
236 m_voi[i].eg_sect= -1;
237 m_voi[i].eg = 0.0;
238 m_voi[i].eg_arm = 0;
239 m_voi[i].eg_ext = 0;
240 m_voi[i].pitch = -1.0;
241 m_voi[i].mute = false;
242 }
243
244 void msm5232_device::mute(int voice, bool mute)
245 {
246 m_voi[voice].mute = mute;
247 }
248
249 void msm5232_device::gate_update()
250 {
251 int new_state = (m_control2 & 0x20) ? m_voi[7].GF : 0;
252
253 if (m_gate != new_state && m_gate_handler_cb!=NULL)
254 {
255 m_gate = new_state;
256 m_gate_handler_cb(new_state);
257 }
258 }
259
260 int msm5232_device::get_rate() {
261 return m_rate;
262 }
263
264 void msm5232_device::init(int clock, int rate)
265 {
266 int j;
267
268 m_chip_clock = clock;
269 m_rate = rate ? rate : 44100; /* avoid division by 0 */
270
271 init_tables();
272
273 for (j=0; j<8; j++)
274 {
275 memset(&m_voi[j],0,sizeof(VOICE));
276 init_voice(j);
277 }
278 }
279
280
281 void msm5232_device::write(unsigned int offset, uint8_t data)
282 {
283 if (offset > 0x0d)
284 return;
285
286 if (offset < 0x08) /* pitch */
287 {
288 int ch = offset&7;
289
290 m_voi[ch].GF = ((data&0x80)>>7);
291 if (ch == 7)
292 gate_update();
293
294 if(data&0x80)
295 {
296 if(data >= 0xd8)
297 {
298 /*if ((data&0x7f) != 0x5f) logerror("MSM5232: WRONG PITCH CODE = %2x\n",data&0x7f);*/
299 m_voi[ch].mode = 1; /* noise mode */
300 m_voi[ch].eg_sect = 0; /* Key On */
301 }
302 else
303 {
304 if ( m_voi[ch].pitch != (data&0x7f) )
305 {
306 int n;
307 uint16_t pg;
308
309 m_voi[ch].pitch = data&0x7f;
310
311 pg = MSM5232_ROM[ data&0x7f ];
312
313 m_voi[ch].TG_count_period = (pg & 0x1ff) * m_UpdateStep / 2;
314
315 n = (pg>>9) & 7; /* n = bit number for 16' output */
316 m_voi[ch].TG_out16 = 1<<n;
317 /* for 8' it is bit n-1 (bit 0 if n-1<0) */
318 /* for 4' it is bit n-2 (bit 0 if n-2<0) */
319 /* for 2' it is bit n-3 (bit 0 if n-3<0) */
320 n = (n>0)? n-1: 0;
321 m_voi[ch].TG_out8 = 1<<n;
322
323 n = (n>0)? n-1: 0;
324 m_voi[ch].TG_out4 = 1<<n;
325
326 n = (n>0)? n-1: 0;
327 m_voi[ch].TG_out2 = 1<<n;
328 }
329 m_voi[ch].mode = 0; /* tone mode */
330 m_voi[ch].eg_sect = 0; /* Key On */
331 }
332 }
333 else
334 {
335 if ( !m_voi[ch].eg_arm ) /* arm = 0 */
336 m_voi[ch].eg_sect = 2; /* Key Off -> go to release */
337 else /* arm = 1 */
338 m_voi[ch].eg_sect = 1; /* Key Off -> go to decay */
339 }
340 }
341 else
342 {
343 int i;
344 switch(offset)
345 {
346 case 0x08: /* group1 attack */
347 for (i=0; i<4; i++)
348 m_voi[i].ar_rate = m_ar_tbl[data&0x7] * m_external_capacity[i];
349 break;
350
351 case 0x09: /* group2 attack */
352 for (i=0; i<4; i++)
353 m_voi[i+4].ar_rate = m_ar_tbl[data&0x7] * m_external_capacity[i+4];
354 break;
355
356 case 0x0a: /* group1 decay */
357 for (i=0; i<4; i++) {
358 m_voi[i].dr_rate = m_dr_tbl[data&0xf] * m_external_capacity[i];
359 }
360 break;
361
362 case 0x0b: /* group2 decay */
363 for (i=0; i<4; i++)
364 m_voi[i+4].dr_rate = m_dr_tbl[data&0xf] * m_external_capacity[i+4];
365 break;
366
367 case 0x0c: /* group1 control */
368
369 /*if (m_control1 != data)
370 logerror("msm5232: control1 ctrl=%x OE=%x\n", data&0xf0, data&0x0f);*/
371
372 /*if (data & 0x10)
373 popmessage("msm5232: control1 ctrl=%2x\n", data);*/
374
375 m_control1 = data;
376
377 for (i=0; i<4; i++)
378 {
379 if ( (data&0x10) && (m_voi[i].eg_sect == 1) )
380 m_voi[i].eg_sect = 0;
381 m_voi[i].eg_arm = data&0x10;
382 m_voi[i].eg_ext = !(data&0x20);
383 }
384
385 m_EN_out16[0] = (data&1) ? ~0:0;
386 m_EN_out8[0] = (data&2) ? ~0:0;
387 m_EN_out4[0] = (data&4) ? ~0:0;
388 m_EN_out2[0] = (data&8) ? ~0:0;
389
390 break;
391
392 case 0x0d: /* group2 control */
393
394 /*if (m_control2 != data)
395 logerror("msm5232: control2 ctrl=%x OE=%x\n", data&0xf0, data&0x0f);*/
396
397 /*if (data & 0x10)
398 popmessage("msm5232: control2 ctrl=%2x\n", data);*/
399
400 m_control2 = data;
401 gate_update();
402
403 for (i=0; i<4; i++)
404 {
405 if ( (data&0x10) && (m_voi[i+4].eg_sect == 1) )
406 m_voi[i+4].eg_sect = 0;
407 m_voi[i+4].eg_arm = data&0x10;
408 m_voi[i+4].eg_ext = !(data&0x20);
409 }
410
411 m_EN_out16[1] = (data&1) ? ~0:0;
412 m_EN_out8[1] = (data&2) ? ~0:0;
413 m_EN_out4[1] = (data&4) ? ~0:0;
414 m_EN_out2[1] = (data&8) ? ~0:0;
415
416 break;
417 }
418 }
419 }
420
421
422
423 #define VMIN 0
424 #define VMAX 32768
425
426
427 void msm5232_device::EG_voices_advance()
428 {
429 VOICE *voi = &m_voi[0];
430 int samplerate = m_rate;
431 int i;
432
433 i = 8;
434 do
435 {
436 if (voi->eg_ext) {
437 voi->egvol=m_external_input[8-i]*2048.0;
438 } else {
439 switch(voi->eg_sect)
440 {
441 case 0: /* attack */
442
443 /* capacitor charge */
444 if (voi->eg < VMAX)
445 {
446 voi->counter -= (int)((VMAX - voi->eg) / voi->ar_rate);
447 if ( voi->counter <= 0 )
448 {
449 int n = -voi->counter / samplerate + 1;
450 voi->counter += n * samplerate;
451 if ( (voi->eg += n) > VMAX )
452 voi->eg = VMAX;
453 }
454 }
455
456 /* when ARM=0, EG switches to decay as soon as cap is charged to VT (EG inversion voltage; about 80% of MAX) */
457 if (!voi->eg_arm)
458 {
459 if(voi->eg >= VMAX * 80/100 )
460 {
461 voi->eg_sect = 1;
462 }
463 }
464 else
465 /* ARM=1 */
466 {
467 /* when ARM=1, EG stays at maximum until key off */
468 }
469
470 voi->egvol = voi->eg / 16; /*32768/16 = 2048 max*/
471
472 break;
473
474 case 1: /* decay */
475
476 /* capacitor discharge */
477 if (voi->eg > VMIN)
478 {
479 voi->counter -= (int)((voi->eg - VMIN) / voi->dr_rate);
480 if ( voi->counter <= 0 )
481 {
482 int n = -voi->counter / samplerate + 1;
483 voi->counter += n * samplerate;
484 if ( (voi->eg -= n) < VMIN )
485 voi->eg = VMIN;
486 }
487 }
488 else /* voi->eg <= VMIN */
489 {
490 voi->eg_sect =-1;
491 }
492
493 voi->egvol = voi->eg / 16; /*32768/16 = 2048 max*/
494
495 break;
496
497 case 2: /* release */
498
499 /* capacitor discharge */
500 if (voi->eg > VMIN)
501 {
502 voi->counter -= (int)((voi->eg - VMIN) / voi->rr_rate);
503 if ( voi->counter <= 0 )
504 {
505 int n = -voi->counter / samplerate + 1;
506 voi->counter += n * samplerate;
507 if ( (voi->eg -= n) < VMIN )
508 voi->eg = VMIN;
509 }
510 }
511 else /* voi->eg <= VMIN */
512 {
513 voi->eg_sect =-1;
514 }
515
516 voi->egvol = voi->eg / 16; /*32768/16 = 2048 max*/
517
518 break;
519
520 default:
521 break;
522 }
523 }
524
525 voi++;
526 i--;
527 } while (i>0);
528
529 }
530
531 static int o2,o4,o8,o16,solo8,solo16;
532
533 void msm5232_device::TG_group_advance(int groupidx)
534 {
535 VOICE *voi = &m_voi[groupidx*4];
536 int i;
537
538 o2 = o4 = o8 = o16 = solo8 = solo16 = 0;
539
540 i=4;
541 do
542 {
543 int out2, out4, out8, out16;
544
545 out2 = out4 = out8 = out16 = 0;
546
547 if (voi->mode==0) /* generate square tone */
548 {
549 int left = 1<<STEP_SH;
550 do
551 {
552 int nextevent = left;
553
554 if (voi->TG_cnt&voi->TG_out16) out16+=voi->TG_count;
555 if (voi->TG_cnt&voi->TG_out8) out8 +=voi->TG_count;
556 if (voi->TG_cnt&voi->TG_out4) out4 +=voi->TG_count;
557 if (voi->TG_cnt&voi->TG_out2) out2 +=voi->TG_count;
558
559 voi->TG_count -= nextevent;
560
561 while (voi->TG_count <= 0)
562 {
563 voi->TG_count += voi->TG_count_period;
564 voi->TG_cnt++;
565 if (voi->TG_cnt&voi->TG_out16) out16+=voi->TG_count_period;
566 if (voi->TG_cnt&voi->TG_out8 ) out8 +=voi->TG_count_period;
567 if (voi->TG_cnt&voi->TG_out4 ) out4 +=voi->TG_count_period;
568 if (voi->TG_cnt&voi->TG_out2 ) out2 +=voi->TG_count_period;
569
570 if (voi->TG_count > 0)
571 break;
572
573 voi->TG_count += voi->TG_count_period;
574 voi->TG_cnt++;
575 if (voi->TG_cnt&voi->TG_out16) out16+=voi->TG_count_period;
576 if (voi->TG_cnt&voi->TG_out8 ) out8 +=voi->TG_count_period;
577 if (voi->TG_cnt&voi->TG_out4 ) out4 +=voi->TG_count_period;
578 if (voi->TG_cnt&voi->TG_out2 ) out2 +=voi->TG_count_period;
579 }
580 if (voi->TG_cnt&voi->TG_out16) out16-=voi->TG_count;
581 if (voi->TG_cnt&voi->TG_out8 ) out8 -=voi->TG_count;
582 if (voi->TG_cnt&voi->TG_out4 ) out4 -=voi->TG_count;
583 if (voi->TG_cnt&voi->TG_out2 ) out2 -=voi->TG_count;
584
585 left -=nextevent;
586
587 }while (left>0);
588 }
589 else /* generate noise */
590 {
591 if (m_noise_clocks&8) out16+=(1<<STEP_SH);
592 if (m_noise_clocks&4) out8 +=(1<<STEP_SH);
593 if (m_noise_clocks&2) out4 +=(1<<STEP_SH);
594 if (m_noise_clocks&1) out2 +=(1<<STEP_SH);
595 }
596
597 /* calculate signed output */
598 if (!voi->mute) {
599 o16 += vo16[groupidx*4+(4-i)] = ( (out16-(1<<(STEP_SH-1))) * voi->egvol) >> STEP_SH;
600 o8 += vo8 [groupidx*4+(4-i)] = ( (out8 -(1<<(STEP_SH-1))) * voi->egvol) >> STEP_SH;
601 o4 += vo4 [groupidx*4+(4-i)] = ( (out4 -(1<<(STEP_SH-1))) * voi->egvol) >> STEP_SH;
602 o2 += vo2 [groupidx*4+(4-i)] = ( (out2 -(1<<(STEP_SH-1))) * voi->egvol) >> STEP_SH;
603
604 if (i == 1 && groupidx == 1)
605 {
606 solo16 += ( (out16-(1<<(STEP_SH-1))) << 11) >> STEP_SH;
607 solo8 += ( (out8 -(1<<(STEP_SH-1))) << 11) >> STEP_SH;
608 }
609 }
610
611 voi++;
612 i--;
613 }while (i>0);
614
615 /* cut off disabled output lines */
616 o16 &= m_EN_out16[groupidx];
617 o8 &= m_EN_out8 [groupidx];
618 o4 &= m_EN_out4 [groupidx];
619 o2 &= m_EN_out2 [groupidx];
620 }
621
622
623 /* macro saves feet data to mono file */
624 #ifdef SAVE_SEPARATE_CHANNELS
625 #define SAVE_SINGLE_CHANNEL(j,val) \
626 { signed int pom= val; \
627 if (pom > 32767) pom = 32767; else if (pom < -32768) pom = -32768; \
628 fputc((unsigned short)pom&0xff,sample[j]); \
629 fputc(((unsigned short)pom>>8)&0xff,sample[j]); }
630 #else
631 #define SAVE_SINGLE_CHANNEL(j,val)
632 #endif
633
634 /* first macro saves all 8 feet outputs to mixed (mono) file */
635 /* second macro saves one group into left and the other in right channel */
636 #if 1 /*MONO*/
637 #ifdef SAVE_SAMPLE
638 #define SAVE_ALL_CHANNELS \
639 { signed int pom = buf1[i] + buf2[i]; \
640 fputc((unsigned short)pom&0xff,sample[8]); \
641 fputc(((unsigned short)pom>>8)&0xff,sample[8]); \
642 }
643 #else
644 #define SAVE_ALL_CHANNELS
645 #endif
646 #else /*STEREO*/
647 #ifdef SAVE_SAMPLE
648 #define SAVE_ALL_CHANNELS \
649 { signed int pom = buf1[i]; \
650 fputc((unsigned short)pom&0xff,sample[8]); \
651 fputc(((unsigned short)pom>>8)&0xff,sample[8]); \
652 pom = buf2[i]; \
653 fputc((unsigned short)pom&0xff,sample[8]); \
654 fputc(((unsigned short)pom>>8)&0xff,sample[8]); \
655 }
656 #else
657 #define SAVE_ALL_CHANNELS
658 #endif
659 #endif
660
661
662 /* MAME Interface */
663 void msm5232_device::device_post_load()
664 {
665 init_tables();
666 }
667
668 void msm5232_device::set_clock(int clock)
669 {
670 if (m_chip_clock != clock)
671 {
672 m_chip_clock = clock;
673 m_rate = clock/CLOCK_RATE_DIVIDER;
674 init_tables();
675 }
676 }
677
678
679 //-------------------------------------------------
680 // sound_stream_update - handle a stream update
681 //-------------------------------------------------
682
683 void msm5232_device::sound_stream_update(short* outputs)
684 {
685 auto &buf1 = outputs[0];
686 auto &buf2 = outputs[1];
687 auto &buf3 = outputs[2];
688 auto &buf4 = outputs[3];
689 auto &buf5 = outputs[4];
690 auto &buf6 = outputs[5];
691 auto &buf7 = outputs[6];
692 auto &buf8 = outputs[7];
693 auto &bufsolo1 = outputs[8];
694 auto &bufsolo2 = outputs[9];
695 auto &bufnoise = outputs[10];
696
697 /* calculate all voices' envelopes */
698 EG_voices_advance();
699
700 TG_group_advance(0); /* calculate tones group 1 */
701 buf1=o2;
702 buf2=o4;
703 buf3=o8;
704 buf4=o16;
705
706 TG_group_advance(1); /* calculate tones group 2 */
707 buf5=o2;
708 buf6=o4;
709 buf7=o8;
710 buf8=o16;
711
712 bufsolo1=solo8;
713 bufsolo2=solo16;
714
715 /* update noise generator */
716 {
717 int cnt = (m_noise_cnt+=m_noise_step) >> STEP_SH;
718 m_noise_cnt &= ((1<<STEP_SH)-1);
719 while (cnt > 0)
720 {
721 int tmp = m_noise_rng & (1<<16); /* store current level */
722
723 if (m_noise_rng&1)
724 m_noise_rng ^= 0x24000;
725 m_noise_rng>>=1;
726
727 if ( (m_noise_rng & (1<<16)) != tmp ) /* level change detect */
728 m_noise_clocks++;
729
730 cnt--;
731 }
732 }
733
734 bufnoise=(m_noise_rng & (1<<16)) ? 32767 : 0;
735 }