~ubuntu-branches/debian/sid/mame/sid

« back to all changes in this revision

Viewing changes to mess/src/emu/sound/spu.c

  • Committer: Package Import Robot
  • Author(s): Jordi Mallach, Jordi Mallach, Emmanuel Kasper
  • Date: 2011-12-19 22:56:27 UTC
  • mfrom: (0.1.2)
  • Revision ID: package-import@ubuntu.com-20111219225627-ub5oga1oys4ogqzm
Tags: 0.144-1
[ Jordi Mallach ]
* Fix syntax errors in DEP5 copyright file (lintian).
* Use a versioned copyright Format specification field.
* Update Vcs-* URLs.
* Move transitional packages to the new metapackages section, and make
  them priority extra.
* Remove references to GNU/Linux and MESS sources from copyright.
* Add build variables for s390x.
* Use .xz tarballs as it cuts 4MB for the upstream sources.
* Add nplayers.ini as a patch. Update copyright file to add CC-BY-SA-3.0.

[ Emmanuel Kasper ]
* New upstream release. Closes: #651538.
* Add Free Desktop compliant png icons of various sizes taken from
  the hydroxygen iconset
* Mess is now built from a new source package, to avoid possible source
  incompatibilities between mame and the mess overlay.
* Mame-tools are not built from the mame source package anymore, but
  from the mess source package

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 
3
 
    Sony PlayStation SPU (CXD2922BQ/CXD2925Q) emulator
4
 
    by pSXAuthor
5
 
    MAME adaptation by R. Belmont
6
 
 
7
 
*/
8
 
 
9
 
#include "emu.h"
10
 
#include "spu.h"
11
 
#include "spureverb.h"
12
 
#include "includes/psx.h"
13
 
 
14
 
//
15
 
//
16
 
//
17
 
 
18
 
//#define warn_if_sweep_used
19
 
#define assert_if_sweep_used
20
 
//#define debug_spu_registers
21
 
//#define debug_spu
22
 
//#define log_loop_cache
23
 
//#define show_xa_debug
24
 
 
25
 
//#ifndef _FINAL
26
 
//  #define show_cache_update
27
 
//#endif
28
 
 
29
 
#ifdef show_xa_debug
30
 
        #define debug_xa printf
31
 
#else
32
 
        #define debug_xa if (0)
33
 
#endif
34
 
 
35
 
// device type definition
36
 
const device_type SPU = &device_creator<spu_device>;
37
 
 
38
 
//
39
 
//
40
 
//
41
 
INLINE unsigned int min(unsigned int a, unsigned int b)
42
 
{
43
 
        return (a > b) ? b : a;
44
 
}
45
 
 
46
 
INLINE unsigned int max(unsigned int a, unsigned int b)
47
 
{
48
 
        return (a > b) ? a : b;
49
 
}
50
 
 
51
 
INLINE double mindb(double a, double b)
52
 
{
53
 
        return (a > b) ? b : a;
54
 
}
55
 
 
56
 
INLINE double maxdb(double a, double b)
57
 
{
58
 
        return (a > b) ? a : b;
59
 
}
60
 
 
61
 
enum spu_registers
62
 
{
63
 
        spureg_voice=0,
64
 
        spureg_voice_last=0x17f,
65
 
        spureg_mvol_l=0x180,
66
 
        spureg_mvol_r=0x182,
67
 
        spureg_rvol_l=0x184,
68
 
        spureg_rvol_r=0x186,
69
 
        spureg_keyon=0x188,
70
 
        spureg_keyoff=0x18c,
71
 
        spureg_fm=0x190,
72
 
        spureg_noise=0x194,
73
 
        spureg_reverb=0x198,
74
 
        spureg_chon=0x19c,
75
 
        spureg_reverb_addr=0x1a2,
76
 
        spureg_irq_addr=0x1a4,
77
 
        spureg_trans_addr=0x1a6,
78
 
        spureg_data=0x1a8,
79
 
        spureg_ctrl=0x1aa,
80
 
        spureg_status=0x1ac,
81
 
        spureg_cdvol_l=0x1b0,
82
 
        spureg_cdvol_r=0x1b2,
83
 
        spureg_exvol_l=0x1b4,
84
 
        spureg_exvol_r=0x1b6,
85
 
        spureg_reverb_config=0x1c0,
86
 
        spureg_last=0x1ff
87
 
};
88
 
 
89
 
enum spu_ctrl
90
 
{
91
 
        spuctrl_irq_enable=0x40,
92
 
        spuctrl_noise_shift=8,
93
 
        spuctrl_noise_mask=0x3f<<spuctrl_noise_shift
94
 
};
95
 
 
96
 
enum
97
 
{
98
 
        adpcmflag_end=1,
99
 
        adpcmflag_loop=2,
100
 
        adpcmflag_loop_start=4
101
 
};
102
 
 
103
 
struct adpcm_packet
104
 
{
105
 
        unsigned char info,
106
 
                                                                flags,
107
 
                                                                data[14];
108
 
};
109
 
 
110
 
enum adsl_flags
111
 
{
112
 
        adsl_am=0x8000,
113
 
        adsl_ar_shift=8,
114
 
        adsl_ar_mask=0x7f<<adsl_ar_shift,
115
 
        adsl_dr_shift=4,
116
 
        adsl_dr_mask=0xf<<adsl_dr_shift,
117
 
        adsl_sl_mask=0xf
118
 
};
119
 
 
120
 
enum srrr_flags
121
 
{
122
 
        srrr_sm=0x8000,
123
 
        srrr_sd=0x4000,
124
 
        srrr_sr_shift=6,
125
 
        srrr_sr_mask=0x7f<<srrr_sr_shift,
126
 
        srrr_rm=0x20,
127
 
        srrr_rr_mask=0x1f
128
 
};
129
 
 
130
 
static const unsigned int sound_buffer_size=65536*4,
131
 
                                                                                                        xa_sector_size=(18*28*8)<<1,
132
 
                                                                                                        xa_buffer_sectors=16,
133
 
                                                                                                        cdda_sector_size=2352,
134
 
                                                                                                        cdda_buffer_sectors=16,
135
 
                                                                                                        num_loop_cache_packets=4,
136
 
                                                                                                        num_loop_cache_samples=num_loop_cache_packets*28,
137
 
                                                                                                        spu_ram_size=512*1024,
138
 
                                                                                                        infinity=0xffffffff,
139
 
 
140
 
                                                                                                        output_buffer_size=65536/8,
141
 
 
142
 
                                                                                                        sample_loop_cache_pool_size=64,
143
 
                                                                                                        sample_loop_cache_extend_size=64,
144
 
                                                                                                        sample_cache_pool_size=64,
145
 
                                                                                                        sample_cache_extend_size=64,
146
 
 
147
 
                                                                                                        stream_marker_pool_size=64,
148
 
                                                                                                        stream_marker_extend_size=64;
149
 
 
150
 
//
151
 
//
152
 
//
153
 
 
154
 
static const int filter_coef[5][2]=
155
 
{
156
 
        { 0,0 },
157
 
        { 60,0 },
158
 
        { 115,-52 },
159
 
        { 98,-55 },
160
 
        { 122,-60 },
161
 
};
162
 
 
163
 
//
164
 
//
165
 
//
166
 
 
167
 
#ifdef debug_spu_registers
168
 
        #define _voice_registers(_voice)        \
169
 
          "voice"#_voice".voll",                \
170
 
                "voice"#_voice".volr",          \
171
 
                "voice"#_voice".pitch",         \
172
 
                "voice"#_voice".addr",          \
173
 
                "voice"#_voice".adsl",          \
174
 
                "voice"#_voice".srrr",          \
175
 
                "voice"#_voice".curvol",        \
176
 
                "voice"#_voice".repaddr"
177
 
 
178
 
        #define _voice_mask_register(_name) \
179
 
                _name##"0-15",                    \
180
 
                _name##"16-23"
181
 
 
182
 
        static const char *spu_register_names[256]=
183
 
        {
184
 
                _voice_registers(0),
185
 
                _voice_registers(1),
186
 
                _voice_registers(2),
187
 
                _voice_registers(3),
188
 
                _voice_registers(4),
189
 
                _voice_registers(5),
190
 
                _voice_registers(6),
191
 
                _voice_registers(7),
192
 
                _voice_registers(8),
193
 
                _voice_registers(9),
194
 
                _voice_registers(10),
195
 
                _voice_registers(11),
196
 
                _voice_registers(12),
197
 
                _voice_registers(13),
198
 
                _voice_registers(14),
199
 
                _voice_registers(15),
200
 
                _voice_registers(16),
201
 
                _voice_registers(17),
202
 
                _voice_registers(18),
203
 
                _voice_registers(19),
204
 
                _voice_registers(20),
205
 
                _voice_registers(21),
206
 
                _voice_registers(22),
207
 
                _voice_registers(23),
208
 
                "mvoll",
209
 
                "mvolr",
210
 
                "rvoll",
211
 
                "rvolr",
212
 
                "keyon0-15", "keyon16-23",
213
 
                "keyoff0-15", "keyoff16-23",
214
 
                "fm0-15", "fm16-23",
215
 
                "noise0-15", "noise16-23",
216
 
                "reverb0-15", "reverb16-23",
217
 
                "chon0-15", "chon16-23",
218
 
                "unknown",
219
 
                "reverbaddr",
220
 
                "irqaddr",
221
 
                "transaddr",
222
 
                "data",
223
 
                "ctrl",
224
 
                "statusl",
225
 
                "statush",
226
 
                "cdvoll",
227
 
                "cdvolr",
228
 
                "exvoll",
229
 
                "exvolr"
230
 
        };
231
 
 
232
 
        const char *get_register_name(const unsigned int addr)
233
 
        {
234
 
                return spu_register_names[(addr&0x1ff)>>1];
235
 
        }
236
 
#endif
237
 
 
238
 
//**************************************************************************
239
 
//  GLOBAL VARIABLES
240
 
//**************************************************************************
241
 
 
242
 
reverb_params *spu_reverb_cfg=NULL;
243
 
 
244
 
float spu_device::freq_multiplier=1.0f;
245
 
 
246
 
//**************************************************************************
247
 
//  DEVICE CONFIGURATION
248
 
//**************************************************************************
249
 
 
250
 
class adpcm_decoder
251
 
{
252
 
        int l0,l1;
253
 
 
254
 
public:
255
 
        adpcm_decoder()
256
 
        {
257
 
                reset();
258
 
        }
259
 
 
260
 
        adpcm_decoder(const adpcm_decoder &other)
261
 
        {
262
 
                operator =(other);
263
 
        }
264
 
 
265
 
        adpcm_decoder &operator =(const adpcm_decoder &other)
266
 
        {
267
 
                l0=other.l0;
268
 
                l1=other.l1;
269
 
                return *this;
270
 
        }
271
 
 
272
 
        void reset()
273
 
        {
274
 
                l0=l1=0;
275
 
        }
276
 
 
277
 
        signed short *decode_packet(adpcm_packet *ap, signed short *dp);
278
 
};
279
 
 
280
 
//
281
 
//
282
 
//
283
 
 
284
 
struct spu_device::sample_cache
285
 
{
286
 
public:
287
 
        unsigned int start,
288
 
                                 end,
289
 
                                 invalid_start,
290
 
                                 invalid_end,
291
 
                                 loopaddr,
292
 
                                 last_update_end;
293
 
        signed short *data,*loop,*dend;
294
 
        adpcm_decoder decoder, update_decoder;
295
 
        mutable int ref_count;
296
 
        bool valid,
297
 
                         is_loop;
298
 
        sample_loop_cache *loop_cache;
299
 
 
300
 
        static unsigned int cache_size;
301
 
 
302
 
        sample_cache()
303
 
                :       invalid_start(0xffffffff),
304
 
                        invalid_end(0),
305
 
                        last_update_end(0xffffffff),
306
 
                        data(NULL),
307
 
                        ref_count(0),
308
 
                        valid(false),
309
 
                        is_loop(false),
310
 
                        loop_cache(NULL)
311
 
        {
312
 
        }
313
 
 
314
 
        ~sample_cache();
315
 
 
316
 
        void add_ref() const { ref_count++; }
317
 
        void remove_ref() const
318
 
        {
319
 
                ref_count--;
320
 
                if (ref_count==0)
321
 
                {
322
 
                        cache_size-=(dend-data)<<1;
323
 
                        global_free(this);
324
 
                }
325
 
        }
326
 
 
327
 
        signed short *get_sample_pointer(const unsigned int addr);
328
 
        bool get_sample_pointer(const unsigned int addr, cache_pointer *cp);
329
 
        bool get_loop_pointer(cache_pointer *cp);
330
 
        unsigned int get_sample_address(const signed short *ptr) const;
331
 
        sample_loop_cache *find_loop_cache(const unsigned int lpend, const unsigned int lpstart);
332
 
        void add_loop_cache(sample_loop_cache *lc);
333
 
 
334
 
        bool is_valid_pointer(signed short *ptr) const;
335
 
 
336
 
        bool try_update(spu_device *spu);
337
 
};
338
 
 
339
 
unsigned int spu_device::sample_cache::cache_size;
340
 
 
341
 
//
342
 
//
343
 
//
344
 
 
345
 
struct spu_device::sample_loop_cache
346
 
{
347
 
public:
348
 
        unsigned int loopend,
349
 
                                                         loopstart,
350
 
                                                         len;
351
 
        signed short data[num_loop_cache_samples];
352
 
        sample_loop_cache *next;
353
 
 
354
 
        sample_loop_cache()
355
 
                :       next(NULL)
356
 
        {
357
 
                sample_cache::cache_size+=num_loop_cache_samples<<1;
358
 
        }
359
 
 
360
 
        ~sample_loop_cache()
361
 
        {
362
 
                sample_cache::cache_size-=num_loop_cache_samples<<1;
363
 
 
364
 
                #ifdef log_loop_cache
365
 
                        log(log_spu,"spu: destroy loop cache %08x\n",this);
366
 
                #endif
367
 
        }
368
 
};
369
 
 
370
 
//
371
 
//
372
 
//
373
 
 
374
 
struct spu_device::cache_pointer
375
 
{
376
 
        signed short *ptr;
377
 
        sample_cache *cache;
378
 
 
379
 
        cache_pointer()
380
 
                : ptr(NULL),
381
 
                        cache(NULL)
382
 
        {
383
 
        }
384
 
 
385
 
        cache_pointer(const cache_pointer &other)
386
 
                : ptr(NULL),
387
 
                        cache(NULL)
388
 
        {
389
 
                operator =(other);
390
 
        }
391
 
 
392
 
        cache_pointer(signed short *_ptr, sample_cache *_cache)
393
 
                : ptr(_ptr),
394
 
                        cache(_cache)
395
 
        {
396
 
                if (cache) cache->add_ref();
397
 
        }
398
 
 
399
 
        ~cache_pointer()
400
 
        {
401
 
                reset();
402
 
        }
403
 
 
404
 
        void reset();
405
 
        cache_pointer &operator =(const cache_pointer &other);
406
 
        bool update(spu_device *spu);
407
 
 
408
 
        unsigned int get_address() const
409
 
        {
410
 
                if (cache)
411
 
                {
412
 
                        return cache->get_sample_address(ptr);
413
 
                } else
414
 
                {
415
 
                        return -1;
416
 
                }
417
 
        }
418
 
 
419
 
        operator bool() const { return cache!=NULL; }
420
 
 
421
 
        bool is_valid() const { return ((cache) && (cache->is_valid_pointer(ptr))); }
422
 
};
423
 
 
424
 
//
425
 
//
426
 
//
427
 
 
428
 
struct spu_device::voiceinfo
429
 
{
430
 
        cache_pointer play,loop;
431
 
        sample_loop_cache *loop_cache;
432
 
        unsigned int dptr,
433
 
                                                         lcptr;
434
 
 
435
 
        int env_state;
436
 
        float env_ar,
437
 
                                env_dr,
438
 
                                env_sr,
439
 
                                env_rr,
440
 
                                env_sl,
441
 
                                env_level,
442
 
                                env_delta,
443
 
 
444
 
                                //>>
445
 
                                sweep_vol[2],
446
 
                                sweep_rate[2];
447
 
        int vol[2];
448
 
                                //<<
449
 
 
450
 
        unsigned int pitch,
451
 
                                                         samplestoend,
452
 
                                                         samplestoirq,
453
 
                                                         envsamples;
454
 
        bool hitirq,
455
 
                         inloopcache,
456
 
                         forceloop,
457
 
                         _pad;
458
 
        INT64 keyontime;
459
 
};
460
 
 
461
 
//
462
 
//
463
 
//
464
 
 
465
 
class stream_buffer
466
 
{
467
 
        struct stream_marker
468
 
        {
469
 
        public:
470
 
                unsigned int sector,
471
 
                                                                 offset;
472
 
                stream_marker *next,
473
 
                                                                        *prev;
474
 
        };
475
 
 
476
 
        unsigned char *buffer;
477
 
        unsigned int head,
478
 
                                                         tail,
479
 
                                                         in,
480
 
                                                         sector_size,
481
 
                                                         num_sectors,
482
 
                                                         buffer_size;
483
 
        stream_marker *marker_head,
484
 
                                                                *marker_tail;
485
 
 
486
 
public:
487
 
        stream_buffer(const unsigned int _sector_size,
488
 
                                                                const unsigned int _num_sectors)
489
 
                :       head(0),
490
 
                        tail(0),
491
 
                        in(0),
492
 
                        sector_size(_sector_size),
493
 
                        num_sectors(_num_sectors),
494
 
                        marker_head(NULL),
495
 
                        marker_tail(NULL)
496
 
        {
497
 
                buffer_size=sector_size*num_sectors;
498
 
                buffer=new unsigned char [buffer_size];
499
 
                memset(buffer,0,buffer_size);
500
 
        }
501
 
 
502
 
        ~stream_buffer()
503
 
        {
504
 
                flush_all();
505
 
                global_free(buffer);
506
 
        }
507
 
 
508
 
        unsigned char *add_sector(const unsigned int sector)
509
 
        {
510
 
                stream_marker *xam=new stream_marker;
511
 
                xam->sector=sector;
512
 
                xam->offset=head;
513
 
                xam->next=NULL;
514
 
                xam->prev=marker_tail;
515
 
                if (marker_tail)
516
 
                {
517
 
                        marker_tail->next=xam;
518
 
                } else
519
 
                {
520
 
                        marker_head=xam;
521
 
                }
522
 
                marker_tail=xam;
523
 
 
524
 
                unsigned char *ret=buffer+head;
525
 
                head=(head+sector_size)%buffer_size;
526
 
                in+=sector_size;
527
 
                return ret;
528
 
        }
529
 
 
530
 
        void flush(const unsigned int sector)
531
 
        {
532
 
                // Remove markers from the end of the buffer if they are after
533
 
                // the specified sector
534
 
 
535
 
                while ((marker_tail) && (marker_tail->sector>=sector))
536
 
                {
537
 
//          debug_xa("flushing: %d\n", marker_tail->sector);
538
 
 
539
 
                        stream_marker *xam=marker_tail;
540
 
                        head=xam->offset;
541
 
                        marker_tail=xam->prev;
542
 
                        if (marker_tail) marker_tail->next=NULL;
543
 
                        global_free(xam);
544
 
                }
545
 
 
546
 
                // Set marker head to NULL if the list is now empty
547
 
 
548
 
                if (! marker_tail) marker_head=NULL;
549
 
 
550
 
                // Adjust buffer size counter
551
 
 
552
 
                int sz=(head-tail);
553
 
                if (sz<0) sz+=buffer_size;
554
 
                assert(sz<=(int)in);
555
 
                in=sz;
556
 
        }
557
 
 
558
 
        void flush_all()
559
 
        {
560
 
                // NOTE: ??what happens to the markers??
561
 
 
562
 
                while (marker_head)
563
 
                {
564
 
                        stream_marker *m=marker_head;
565
 
                        marker_head=marker_head->next;
566
 
                        global_free(m);
567
 
                }
568
 
 
569
 
                marker_head=marker_tail=NULL;
570
 
                head=tail=in=0;
571
 
        }
572
 
 
573
 
        void delete_markers(const unsigned int oldtail)
574
 
        {
575
 
                while (marker_head)
576
 
                {
577
 
                        int olddist=marker_head->offset-oldtail,
578
 
                                        dist=marker_head->offset-tail;
579
 
                        if (olddist<0) olddist+=buffer_size;
580
 
                        if (dist<0) dist+=buffer_size;
581
 
                        bool passed=(((olddist==0) && (dist!=0)) || (dist>olddist));
582
 
                        if (! passed) break;
583
 
 
584
 
//          debug_xa("passed: %d\n",marker_head->sector);
585
 
 
586
 
                        stream_marker *xam=marker_head;
587
 
                        marker_head=xam->next;
588
 
                        global_free(xam);
589
 
                        if (marker_head) marker_head->prev=NULL;
590
 
                }
591
 
 
592
 
                if (! marker_head) marker_head=marker_tail=NULL;
593
 
        }
594
 
 
595
 
        unsigned int get_bytes_in() const { return in; }
596
 
        unsigned int get_bytes_free() const { return buffer_size-in; }
597
 
 
598
 
        unsigned char *get_tail_ptr() const { return buffer+tail; }
599
 
        unsigned char *get_tail_ptr(const unsigned int offset) const
600
 
        {
601
 
                return buffer+((tail+offset)%buffer_size);
602
 
        }
603
 
        unsigned int get_tail_offset() const { return tail; }
604
 
        void increment_tail(const unsigned int offset)
605
 
        {
606
 
                tail=(tail+offset)%buffer_size;
607
 
                in-=offset;
608
 
        }
609
 
};
610
 
 
611
 
//
612
 
//
613
 
//
614
 
 
615
 
static inline int clamp(const int v)
616
 
{
617
 
        if (v<-32768) return -32768;
618
 
        if (v>32767) return 32767;
619
 
        return v;
620
 
}
621
 
 
622
 
//
623
 
//
624
 
//
625
 
 
626
 
spu_device::sample_cache::~sample_cache()
627
 
{
628
 
        global_free(data);
629
 
        while (loop_cache)
630
 
        {
631
 
                sample_loop_cache *lc=loop_cache;
632
 
                loop_cache=lc->next;
633
 
                global_free(lc);
634
 
        }
635
 
}
636
 
 
637
 
//
638
 
//
639
 
//
640
 
 
641
 
signed short *spu_device::sample_cache::get_sample_pointer(const unsigned int addr)
642
 
{
643
 
        if ((addr>=start) && (addr<end))
644
 
        {
645
 
                return data+(((addr-start)>>4)*28);
646
 
        } else
647
 
        {
648
 
                return NULL;
649
 
        }
650
 
}
651
 
 
652
 
//
653
 
//
654
 
//
655
 
 
656
 
bool spu_device::sample_cache::get_sample_pointer(const unsigned int addr, cache_pointer *cp)
657
 
{
658
 
        cp->reset();
659
 
        if ((cp->ptr=get_sample_pointer(addr)))
660
 
        {
661
 
                cp->cache=this;
662
 
                add_ref();
663
 
                return true;
664
 
        }
665
 
        return false;
666
 
}
667
 
 
668
 
//
669
 
//
670
 
//
671
 
 
672
 
bool spu_device::sample_cache::get_loop_pointer(cache_pointer *cp)
673
 
{
674
 
        cp->reset();
675
 
        if ((cp->ptr=loop))
676
 
        {
677
 
                cp->cache=this;
678
 
                add_ref();
679
 
                return true;
680
 
        }
681
 
        return false;
682
 
}
683
 
 
684
 
//
685
 
//
686
 
//
687
 
 
688
 
unsigned int spu_device::sample_cache::get_sample_address(const signed short *ptr) const
689
 
{
690
 
        if ((ptr>=data) && (ptr<=dend))
691
 
        {
692
 
                return start+(((ptr-data)/28)<<4);
693
 
        } else
694
 
        {
695
 
                return -1;
696
 
        }
697
 
}
698
 
 
699
 
//
700
 
//
701
 
//
702
 
 
703
 
spu_device::sample_loop_cache *spu_device::sample_cache::find_loop_cache(const unsigned int lpend, const unsigned int lpstart)
704
 
{
705
 
        sample_loop_cache *lc;
706
 
        for (lc=loop_cache; lc; lc=lc->next)
707
 
                if ((lc->loopend==lpend) && (lc->loopstart==lpstart)) break;
708
 
        return lc;
709
 
}
710
 
 
711
 
//
712
 
//
713
 
//
714
 
 
715
 
void spu_device::sample_cache::add_loop_cache(sample_loop_cache *lc)
716
 
{
717
 
        lc->next=loop_cache;
718
 
        loop_cache=lc;
719
 
}
720
 
 
721
 
//
722
 
//
723
 
//
724
 
 
725
 
bool spu_device::sample_cache::is_valid_pointer(signed short *ptr) const
726
 
{
727
 
        if ((ptr>=data) && (data<=dend)) return true;
728
 
        for (sample_loop_cache *slc=loop_cache; slc; slc=slc->next)
729
 
                if ((ptr>=slc->data) && (ptr<(slc->data+num_loop_cache_samples)))
730
 
                        return true;
731
 
        return false;
732
 
}
733
 
 
734
 
//
735
 
//
736
 
//
737
 
 
738
 
bool spu_device::sample_cache::try_update(spu_device *spu)
739
 
{
740
 
        if ((invalid_start>=start) && (invalid_end<=end))
741
 
        {
742
 
                adpcm_packet *ap=(adpcm_packet *)(spu->spu_ram+start);
743
 
                unsigned int a;
744
 
                unsigned int loop=0;
745
 
 
746
 
                for (a=start; a<=end; a+=16, ap++)
747
 
                {
748
 
                        if ((ap->flags&adpcmflag_loop_start) && (ap->flags&adpcmflag_loop)) loop=a;
749
 
                        if (ap->flags&adpcmflag_end) break;
750
 
                }
751
 
 
752
 
                if ((a==(end-16)) && (loop==loopaddr))
753
 
                {
754
 
                        #ifdef show_cache_update
755
 
                                printf("updating %p: ",this);
756
 
                        #endif
757
 
 
758
 
                        if (invalid_start==start)
759
 
                        {
760
 
                                #ifdef show_cache_update
761
 
                                        printf("using end values");
762
 
                                #endif
763
 
 
764
 
                                update_decoder=decoder;
765
 
                        } else
766
 
                        if (invalid_start!=last_update_end)
767
 
                        {
768
 
                                #ifdef show_cache_update
769
 
                                        printf("resetting decoder (istrt=%08x lupd=%08x)",invalid_start,last_update_end);
770
 
                                #endif
771
 
 
772
 
                                update_decoder.reset();
773
 
                        }
774
 
                        #ifdef show_cache_update
775
 
                                printf("\n");
776
 
                        #endif
777
 
 
778
 
                        signed short *dp=data+(((invalid_start-start)>>4)*28);
779
 
                        adpcm_packet *ap=(adpcm_packet *)(spu->spu_ram+invalid_start);
780
 
                        for (a=invalid_start; a<invalid_end; a+=16, ap++)
781
 
                                dp=update_decoder.decode_packet(ap,dp);
782
 
 
783
 
                        if (invalid_end==end)
784
 
                        {
785
 
                                #ifdef show_cache_update
786
 
                                        printf("updating end values\n");
787
 
                                #endif
788
 
                                decoder=update_decoder;
789
 
                        }
790
 
                        last_update_end=invalid_end;
791
 
 
792
 
                        for (sample_loop_cache *lc=loop_cache; lc; lc=lc->next)
793
 
                        {
794
 
                                if (invalid_start==lc->loopstart)
795
 
                                {
796
 
                                        adpcm_decoder tmp=decoder;
797
 
                                        signed short *dp=lc->data,
798
 
                                                                                         *dpend=dp+lc->len;
799
 
                                        unsigned int adr=lc->loopstart;
800
 
                                        for (unsigned int i=0; ((i<num_loop_cache_packets) && (dp<dpend)); i++, adr+=16)
801
 
                                                dp=tmp.decode_packet((adpcm_packet *)(spu->spu_ram+adr),dp);
802
 
                                }
803
 
                        }
804
 
 
805
 
                        invalid_end=0;
806
 
                        invalid_start=0xffffffff;
807
 
                        valid=true;
808
 
 
809
 
                        for (unsigned int a=start; a<end; a+=16, ap++)
810
 
                        {
811
 
                                spu->cache[a>>4]=this;
812
 
                        }
813
 
 
814
 
                        add_ref();
815
 
 
816
 
                        return true;
817
 
                }
818
 
        }
819
 
 
820
 
        return false;
821
 
}
822
 
 
823
 
//
824
 
//
825
 
//
826
 
 
827
 
void spu_device::cache_pointer::reset()
828
 
{
829
 
        if (cache)
830
 
        {
831
 
                ptr=NULL;
832
 
                cache->remove_ref();
833
 
                cache=NULL;
834
 
        }
835
 
}
836
 
 
837
 
//
838
 
//
839
 
//
840
 
 
841
 
spu_device::cache_pointer &spu_device::cache_pointer::operator =(const cache_pointer &other)
842
 
{
843
 
        if (cache) cache->remove_ref();
844
 
        ptr=other.ptr;
845
 
        cache=other.cache;
846
 
        if (cache) cache->add_ref();
847
 
        return *this;
848
 
}
849
 
 
850
 
//
851
 
//
852
 
//
853
 
 
854
 
bool spu_device::cache_pointer::update(spu_device *spu)
855
 
{
856
 
        if ((cache) && (! cache->valid))
857
 
        {
858
 
/*      log(log_spu,"cache_pointer::update: block %08x-%08x invalidated %08x-%08x\n",
859
 
                                cache->start,
860
 
                                cache->end,
861
 
                                cache->invalid_start,
862
 
                                cache->invalid_end);*/
863
 
 
864
 
                if (! cache->try_update(spu))
865
 
                {
866
 
                        // Cache is invalid, calculate play address offset from start of
867
 
                        // old cache block
868
 
 
869
 
                        unsigned int off=ptr-cache->data,
870
 
                                                                         addr=cache->start;
871
 
 
872
 
                        // Release cache block and get updated one
873
 
 
874
 
                        spu->translate_sample_addr(addr,this);
875
 
 
876
 
                        // Calculate play address in new cache block
877
 
 
878
 
                        ptr=cache->data+off;
879
 
 
880
 
                        if (ptr>=cache->dend)
881
 
                        {
882
 
                                // Play address is out of bounds in new cache block, release it and get a
883
 
                                // new one starting at the current play address
884
 
 
885
 
                                spu->translate_sample_addr(addr+((off/28)<<4),this);
886
 
                        }
887
 
                }
888
 
        }
889
 
 
890
 
        // Return false if we do not have a cache block or the play address is invalid
891
 
 
892
 
        if ((cache) && ((ptr>=cache->data) && (ptr<cache->dend)))
893
 
        {
894
 
                return true;
895
 
        } else
896
 
        {
897
 
                reset();
898
 
                return false;
899
 
        }
900
 
}
901
 
 
902
 
//
903
 
//
904
 
//
905
 
 
906
 
signed short *adpcm_decoder::decode_packet(adpcm_packet *ap, signed short *dp)
907
 
{
908
 
        int shift=ap->info&0xf,
909
 
                        filter=ap->info>>4,
910
 
                        f0=filter_coef[filter][0],
911
 
                        f1=filter_coef[filter][1];
912
 
 
913
 
        for (int i=0; i<14; i++)
914
 
        {
915
 
                unsigned char b=ap->data[i];
916
 
                short bl=(b&0xf)<<12,
917
 
                                        bh=(b>>4)<<12;
918
 
 
919
 
                bl=(bl>>shift)+(((l0*f0)+(l1*f1)+32)>>6);
920
 
                *dp++=bl;
921
 
                l1=l0;
922
 
                l0=bl;
923
 
 
924
 
                bh=(bh>>shift)+(((l0*f0)+(l1*f1)+32)>>6);
925
 
                *dp++=bh;
926
 
                l1=l0;
927
 
                l0=bh;
928
 
        }
929
 
 
930
 
        return dp;
931
 
}
932
 
 
933
 
//
934
 
//
935
 
//
936
 
 
937
 
static int shift_register15(int &shift)
938
 
{
939
 
   int bit0, bit1, bit14;
940
 
 
941
 
   bit0 = shift & 1;
942
 
   bit1 = (shift & 2) >> 1;
943
 
   bit14 = (bit0 ^ bit1) ^ 1;
944
 
   shift >>= 1;
945
 
   shift |= (bit14 << 14);
946
 
   return bit0;
947
 
}
948
 
 
949
 
//
950
 
//
951
 
//
952
 
 
953
 
//-------------------------------------------------
954
 
//  spu_device - constructor
955
 
//-------------------------------------------------
956
 
 
957
 
spu_device::spu_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
958
 
        : device_t(mconfig, SPU, "SPU", tag, owner, clock),
959
 
          device_sound_interface(mconfig, *this),
960
 
          m_irq_cb(NULL),
961
 
      dirty_flags(-1),
962
 
        status_enabled(false),
963
 
        xa_voll(0x8000),
964
 
        xa_volr(0x8000),
965
 
          changed_xa_vol(0)
966
 
 
967
 
{
968
 
}
969
 
 
970
 
//-------------------------------------------------
971
 
//  static_set_irqf - configuration helper to set
972
 
//  the IRQ callback
973
 
//-------------------------------------------------
974
 
 
975
 
void spu_device::static_set_irqf(device_t &device, void (*irqf)(device_t *device, UINT32 state))
976
 
{
977
 
        spu_device &spu = downcast<spu_device &>(device);
978
 
        spu.m_irq_cb = irqf;
979
 
}
980
 
 
981
 
void spu_device::device_start()
982
 
{
983
 
        voice=new voiceinfo [24];
984
 
        spu_ram=new unsigned char [spu_ram_size];
985
 
 
986
 
        xa_buffer=new stream_buffer(xa_sector_size,xa_buffer_sectors);
987
 
        cdda_buffer=new stream_buffer(cdda_sector_size,cdda_buffer_sectors);
988
 
 
989
 
        init_stream();
990
 
 
991
 
        cache=new sample_cache *[spu_ram_size>>4];
992
 
        memset(cache,0,(spu_ram_size>>4)*sizeof(sample_cache *));
993
 
 
994
 
        // register save state stuff
995
 
        // per-voice variables
996
 
        for (int v = 0; v < 24; v++)
997
 
        {
998
 
                save_item(NAME(spureg.voice[v].vol_l), v);
999
 
                save_item(NAME(spureg.voice[v].vol_r), v);
1000
 
                save_item(NAME(spureg.voice[v].pitch), v);
1001
 
                save_item(NAME(spureg.voice[v].addr), v);
1002
 
                save_item(NAME(spureg.voice[v].adsl), v);
1003
 
                save_item(NAME(spureg.voice[v].srrr), v);
1004
 
                save_item(NAME(spureg.voice[v].curvol), v);
1005
 
                save_item(NAME(spureg.voice[v].repaddr), v);
1006
 
        }
1007
 
 
1008
 
        // SPU globals
1009
 
        save_item(NAME(spureg.mvol_l));
1010
 
        save_item(NAME(spureg.mvol_r));
1011
 
        save_item(NAME(spureg.rvol_l));
1012
 
        save_item(NAME(spureg.rvol_r));
1013
 
        save_item(NAME(spureg.keyon));
1014
 
        save_item(NAME(spureg.keyoff));
1015
 
        save_item(NAME(spureg.fm));
1016
 
        save_item(NAME(spureg.noise));
1017
 
        save_item(NAME(spureg.reverb));
1018
 
        save_item(NAME(spureg.chon));
1019
 
        save_item(NAME(spureg._unknown));
1020
 
        save_item(NAME(spureg.reverb_addr));
1021
 
        save_item(NAME(spureg.irq_addr));
1022
 
        save_item(NAME(spureg.trans_addr));
1023
 
        save_item(NAME(spureg.data));
1024
 
        save_item(NAME(spureg.ctrl));
1025
 
        save_item(NAME(spureg.status));
1026
 
        save_item(NAME(spureg.cdvol_l));
1027
 
        save_item(NAME(spureg.cdvol_r));
1028
 
        save_item(NAME(spureg.exvol_l));
1029
 
        save_item(NAME(spureg.exvol_r));
1030
 
        save_pointer(NAME(spu_ram), spu_ram_size);
1031
 
}
1032
 
 
1033
 
void spu_device::device_reset()
1034
 
{
1035
 
        cur_reverb_preset = NULL;
1036
 
        cur_frame_sample = 0;
1037
 
 
1038
 
        sample_cache::cache_size = 0;
1039
 
 
1040
 
        status_enabled = false;
1041
 
        xa_voll = xa_volr = 0x8000;
1042
 
        dirty_flags = -1;
1043
 
        changed_xa_vol = 0;
1044
 
 
1045
 
        xa_cnt=0;
1046
 
        xa_freq=0;
1047
 
        xa_channels=2;
1048
 
        xa_spf=0;
1049
 
        xa_out_ptr=0;
1050
 
        xa_playing=false;
1051
 
        memset(xa_last,0,sizeof(xa_last));
1052
 
 
1053
 
        cdda_cnt=0;
1054
 
        cdda_playing=false;
1055
 
 
1056
 
        memset(spu_ram,0,spu_ram_size);
1057
 
        memset(reg,0,0x200);
1058
 
        memset(voice,0,sizeof(voiceinfo)*24);
1059
 
 
1060
 
        spureg.status|=(1<<7)|(1<<10);
1061
 
 
1062
 
        memset(cache,0,(spu_ram_size>>4)*sizeof(sample_cache *));
1063
 
 
1064
 
        for (unsigned int i=0; i<4; i++)
1065
 
                output_buf[i]=new unsigned char [output_buffer_size];
1066
 
        output_head=output_tail=output_size=0;
1067
 
 
1068
 
        noise_t=0;
1069
 
        noise_seed=12345;
1070
 
        noise_cur=shift_register15(noise_seed)?0x7fff:0x8000;
1071
 
}
1072
 
 
1073
 
void spu_device::device_post_load()
1074
 
{
1075
 
        // invalidate the SPURAM cache
1076
 
        invalidate_cache(0, spu_ram_size);
1077
 
        flush_output_buffer();
1078
 
 
1079
 
        // mark everything dirty
1080
 
        dirty_flags = -1;
1081
 
 
1082
 
        // kill and reallocate reverb to avoid artifacts
1083
 
        global_free(rev);
1084
 
        rev = new reverb(44100);
1085
 
 
1086
 
        // and do some update processing
1087
 
        update_reverb();
1088
 
        update_key();
1089
 
        update_voice_state();
1090
 
        update_irq_event();
1091
 
}
1092
 
 
1093
 
//
1094
 
//
1095
 
//
1096
 
void spu_device::device_stop()
1097
 
{
1098
 
        for (unsigned int i=0; i<4; i++)
1099
 
                global_free(output_buf[i]);
1100
 
 
1101
 
        kill_stream();
1102
 
 
1103
 
        global_free(spu_ram);
1104
 
        invalidate_cache(0,spu_ram_size);
1105
 
        global_free(cache);
1106
 
        global_free(xa_buffer);
1107
 
        global_free(cdda_buffer);
1108
 
        global_free(voice);
1109
 
}
1110
 
//
1111
 
//
1112
 
//
1113
 
 
1114
 
void spu_device::init_stream()
1115
 
{
1116
 
        const unsigned int hz=44100;
1117
 
 
1118
 
        m_stream = machine().sound().stream_alloc(*this, 0, 2, hz);
1119
 
 
1120
 
        rev=new reverb(hz);
1121
 
 
1122
 
        cdda_freq=(unsigned int)((44100.0f/(float)hz)*4096.0f);
1123
 
        freq_multiplier=(float)spu_base_frequency_hz/(float)hz;
1124
 
}
1125
 
 
1126
 
//
1127
 
//
1128
 
//
1129
 
 
1130
 
void spu_device::kill_stream()
1131
 
{
1132
 
        global_free(rev);
1133
 
        rev=NULL;
1134
 
}
1135
 
 
1136
 
//
1137
 
//
1138
 
//
1139
 
 
1140
 
void spu_device::reinit_sound()
1141
 
{
1142
 
        kill_stream();
1143
 
        init_stream();
1144
 
        flush_output_buffer();
1145
 
        dirty_flags|=dirtyflag_voice_mask;
1146
 
}
1147
 
 
1148
 
//
1149
 
//
1150
 
//
1151
 
 
1152
 
void spu_device::kill_sound()
1153
 
{
1154
 
        kill_stream();
1155
 
}
1156
 
 
1157
 
//
1158
 
//
1159
 
//
1160
 
 
1161
 
unsigned short spu_device::read_word(const unsigned int addr)
1162
 
{
1163
 
        unsigned short ret=0, *rp=(unsigned short *)(reg+(addr&0x1ff));
1164
 
 
1165
 
        assert((addr&1)==0);
1166
 
 
1167
 
        m_stream->update();
1168
 
 
1169
 
        ret=*rp;
1170
 
 
1171
 
        #ifdef debug_spu_registers
1172
 
                printf("spu: read word %08x = %04x [%s]\n",
1173
 
                                                                                                        addr,
1174
 
                                                                                                        ret,
1175
 
                                                                                                        get_register_name(addr));
1176
 
        #endif
1177
 
 
1178
 
        return ret;
1179
 
}
1180
 
 
1181
 
//
1182
 
//
1183
 
//
1184
 
 
1185
 
unsigned char spu_device::read_byte(const unsigned int addr)
1186
 
{
1187
 
        unsigned char ret=0,
1188
 
                                                                *rp=reg+(addr&0x1ff);
1189
 
 
1190
 
        ret=*rp;
1191
 
 
1192
 
        #ifdef debug_spu_registers
1193
 
                printf("spu: read byte %08x\n",addr);
1194
 
        #endif
1195
 
 
1196
 
        return ret;
1197
 
}
1198
 
 
1199
 
//
1200
 
//
1201
 
//
1202
 
 
1203
 
void spu_device::write_word(const unsigned int addr, const unsigned short data)
1204
 
{
1205
 
        #ifdef debug_spu_registers
1206
 
                printf("spu: write %08x = %04x [%s]\n",
1207
 
                                                                                                        addr,
1208
 
                                                                                                        data,
1209
 
                                                                                                        get_register_name(addr));
1210
 
        #endif
1211
 
 
1212
 
        assert((addr&1)==0);
1213
 
 
1214
 
        m_stream->update();
1215
 
 
1216
 
        const unsigned int a=addr&0x1ff;
1217
 
        switch (a)
1218
 
        {
1219
 
                case spureg_trans_addr:
1220
 
                        spureg.trans_addr=data;
1221
 
                        taddr=data<<3;
1222
 
                        break;
1223
 
 
1224
 
                case spureg_data:
1225
 
                        dirty_flags|=dirtyflag_ram;
1226
 
                        write_data(data);
1227
 
                        break;
1228
 
 
1229
 
                default:
1230
 
                {
1231
 
                        unsigned short *rp=(unsigned short *)(reg+(addr&0x1ff));
1232
 
 
1233
 
                        if ((a==spureg_irq_addr) ||
1234
 
                                        ((a==spureg_ctrl) && ((rp[0]^data)&spuctrl_irq_enable)))
1235
 
                                dirty_flags|=dirtyflag_irq;
1236
 
 
1237
 
                        *rp=data;
1238
 
                        break;
1239
 
                }
1240
 
        }
1241
 
 
1242
 
        if ((a>spureg_reverb_config) && (a<=spureg_last))
1243
 
                dirty_flags|=dirtyflag_reverb;
1244
 
 
1245
 
        if (a<=spureg_voice_last)
1246
 
        {
1247
 
                unsigned int v=(a>>4),r=(a&0xf);
1248
 
                if (r==0xe)
1249
 
                {
1250
 
                        voice[v].forceloop=true;
1251
 
                }
1252
 
 
1253
 
                dirty_flags|=(1<<v);
1254
 
        }
1255
 
 
1256
 
        update_key();
1257
 
        update_vol(a);
1258
 
        update_voice_state();
1259
 
        update_irq_event();
1260
 
}
1261
 
 
1262
 
//
1263
 
//
1264
 
//
1265
 
 
1266
 
void spu_device::write_byte(const unsigned int addr, const unsigned char data)
1267
 
{
1268
 
        #ifdef debug_spu_registers
1269
 
                printf("spu: write %08x = %02x\n",addr,data);
1270
 
        #endif
1271
 
 
1272
 
        const unsigned int a=addr&0x1ff;
1273
 
        reg[a]=data;
1274
 
        if ((a>spureg_reverb_config) && (a<=spureg_last))
1275
 
                dirty_flags|=dirtyflag_reverb;
1276
 
        update_key();
1277
 
}
1278
 
 
1279
 
//
1280
 
//
1281
 
//
1282
 
 
1283
 
void spu_device::update_vol(const unsigned int addr)
1284
 
{
1285
 
        if (addr<0x180)
1286
 
        {
1287
 
                unsigned int ch=(addr&0xf)>>1;
1288
 
                if (ch<2)
1289
 
                {
1290
 
                        unsigned int v=addr>>4;
1291
 
                        unsigned short newval=*(unsigned short *)(reg+addr);
1292
 
 
1293
 
                        if (newval&0x8000)
1294
 
                        {
1295
 
                                #if 0
1296
 
                                printf("cur=%04x on=%d",voice[v].vol[ch],(spureg.chon>>ch)&1);
1297
 
                                switch ((newval>>13)&3)
1298
 
                                {
1299
 
                                        case 0: printf("linear inc: phase=%d val=%02x\n",(newval>>12)&1,newval&0x7f); break;
1300
 
                                        case 1: printf("linear dec: phase=%d val=%02x\n",(newval>>12)&1,newval&0x7f); break;
1301
 
                                        case 2: printf("exp inc: phase=%d val=%02x\n",(newval>>12)&1,newval&0x7f); break;
1302
 
                                        case 3: printf("exp dec: phase=%d val=%02x\n",(newval>>12)&1,newval&0x7f); break;
1303
 
                                }
1304
 
                                #endif
1305
 
                        }
1306
 
                        else
1307
 
                        {
1308
 
                                voice[v].vol[ch]=((int)newval<<17)>>17;
1309
 
                        }
1310
 
                }
1311
 
        }
1312
 
}
1313
 
 
1314
 
//
1315
 
//
1316
 
//
1317
 
 
1318
 
void spu_device::write_data(const unsigned short data)
1319
 
{
1320
 
        #ifdef debug_spu_registers
1321
 
                printf("spu: write data %04x @ %04x\n",data,taddr);
1322
 
        #endif
1323
 
 
1324
 
        assert(taddr<spu_ram_size);
1325
 
        if (cache[taddr>>4]) flush_cache(cache[taddr>>4],taddr,taddr+2);
1326
 
        *((unsigned short *)(spu_ram+taddr))=data;
1327
 
        taddr+=2;
1328
 
}
1329
 
 
1330
 
//
1331
 
//
1332
 
//
1333
 
 
1334
 
void spu_device::update_key()
1335
 
{
1336
 
        dirty_flags|=((spureg.keyon|spureg.keyoff)&dirtyflag_voice_mask);
1337
 
 
1338
 
        if (spureg.keyoff)
1339
 
        {
1340
 
                unsigned int d=spureg.keyoff;
1341
 
                for (int i=0; i<24; i++, d>>=1)
1342
 
                        if (d&1) key_off(i);
1343
 
        }
1344
 
 
1345
 
        if (spureg.keyon)
1346
 
        {
1347
 
                unsigned int d=spureg.keyon;
1348
 
                for (int i=0; i<24; i++, d>>=1)
1349
 
                        if (d&1) key_on(i);
1350
 
                spureg.chon|=spureg.keyon;
1351
 
        }
1352
 
 
1353
 
        spureg.keyon=spureg.keyoff=0;
1354
 
}
1355
 
 
1356
 
//
1357
 
//
1358
 
//
1359
 
 
1360
 
void spu_device::flush_cache(sample_cache *sc,
1361
 
                                                                                                        const unsigned int istart,
1362
 
                                                                                                        const unsigned int iend)
1363
 
{
1364
 
        for (unsigned int a=sc->start; a<sc->end; a+=16)
1365
 
                cache[a>>4]=NULL;
1366
 
 
1367
 
/*  log_static(log_spu,"cache_invalidate: %08x->%08x\n",
1368
 
                                         sc->start,
1369
 
                                         sc->end);*/
1370
 
 
1371
 
        sc->invalid_start=min(sc->invalid_start,istart);
1372
 
        sc->invalid_end=max(sc->invalid_end,iend);
1373
 
        sc->valid=false;
1374
 
        sc->remove_ref();
1375
 
}
1376
 
 
1377
 
//
1378
 
//
1379
 
//
1380
 
 
1381
 
void spu_device::invalidate_cache(const unsigned int st, const unsigned int en)
1382
 
{
1383
 
        for (unsigned int a=st; a<en; a+=16)
1384
 
                if (cache[a>>4]) flush_cache(cache[a>>4],st,en);
1385
 
}
1386
 
 
1387
 
//
1388
 
//
1389
 
//
1390
 
 
1391
 
spu_device::sample_cache *spu_device::get_sample_cache(const unsigned int addr)
1392
 
{
1393
 
//  log_static(log_spu,"get_sample_cache: %08x\n",addr);
1394
 
 
1395
 
        assert(addr<spu_ram_size);
1396
 
        sample_cache *sc=cache[addr>>4];
1397
 
        if (sc) return sc;
1398
 
 
1399
 
        unsigned int loop=0;
1400
 
 
1401
 
        sc=new sample_cache;
1402
 
        sc->valid=true;
1403
 
        sc->start=addr;
1404
 
        sc->loop=NULL;
1405
 
 
1406
 
        adpcm_packet *ap=(adpcm_packet *)(spu_ram+sc->start);
1407
 
        unsigned int a;
1408
 
        for (a=addr; a<(512*1024); a+=16, ap++)
1409
 
        {
1410
 
                if (cache[a>>4]) flush_cache(cache[a>>4],a,a+16);
1411
 
                cache[a>>4]=sc;
1412
 
 
1413
 
                if ((ap->flags&adpcmflag_loop_start) && (ap->flags&adpcmflag_loop)) loop=a;
1414
 
                if (ap->flags&adpcmflag_end) break;
1415
 
        }
1416
 
 
1417
 
        if (ap->flags&adpcmflag_loop) sc->is_loop=true;
1418
 
 
1419
 
        sc->end=min(spu_ram_size,a+16);
1420
 
 
1421
 
        unsigned int sz=((sc->end-sc->start)>>4)*28;
1422
 
        sc->data=new signed short [sz];
1423
 
        sample_cache::cache_size+=sz<<1;
1424
 
        sc->loopaddr=loop;
1425
 
        if (loop) sc->loop=sc->data+(((loop-sc->start)>>4)*28);
1426
 
 
1427
 
        signed short *dp=sc->data;
1428
 
        ap=(adpcm_packet *)(spu_ram+sc->start);
1429
 
 
1430
 
        for (a=sc->start; a<sc->end; a+=16, ap++)
1431
 
                dp=sc->decoder.decode_packet(ap,dp);
1432
 
 
1433
 
        sc->dend=dp;
1434
 
        sc->add_ref();
1435
 
 
1436
 
/*  log_static(log_spu,"cache_add: %08x->%08x\n",
1437
 
                                         sc->start,
1438
 
                                         sc->end);*/
1439
 
 
1440
 
        return sc;
1441
 
}
1442
 
 
1443
 
//
1444
 
//
1445
 
//
1446
 
 
1447
 
bool spu_device::translate_sample_addr(const unsigned int addr, cache_pointer *cp)
1448
 
{
1449
 
        assert((addr&0xf)==0);
1450
 
        cp->reset();
1451
 
        if ((cp->cache=get_sample_cache(addr)))
1452
 
        {
1453
 
                cp->ptr=cp->cache->data+(((addr-cp->cache->start)>>4)*28);
1454
 
                cp->cache->add_ref();
1455
 
                return true;
1456
 
        }
1457
 
        return false;
1458
 
}
1459
 
 
1460
 
//
1461
 
// Get distance in input samples to next IRQ for voice
1462
 
//
1463
 
 
1464
 
unsigned int spu_device::get_irq_distance(const voiceinfo *vi)
1465
 
{
1466
 
        if (spureg.ctrl&spuctrl_irq_enable)
1467
 
        {
1468
 
                unsigned int irq_addr=spureg.irq_addr<<3;
1469
 
                signed short *irq_ptr;
1470
 
 
1471
 
                if ((irq_ptr=vi->play.cache->get_sample_pointer(irq_addr)))
1472
 
                {
1473
 
                        // IRQ address is inside this voices current cache block.  Return distance
1474
 
                        // if current play address is lower, or equal (and irq has not already
1475
 
                        // triggered)
1476
 
 
1477
 
                        if ((vi->play.ptr<irq_ptr) ||
1478
 
                                        ((! vi->hitirq) && (vi->play.ptr==irq_ptr)))
1479
 
                        {
1480
 
                                return irq_ptr-vi->play.ptr;
1481
 
                        }
1482
 
                }
1483
 
 
1484
 
                if ((vi->loop) &&
1485
 
                                (irq_ptr=vi->loop.cache->get_sample_pointer(irq_addr)) &&
1486
 
                                (irq_ptr>=vi->loop.ptr))
1487
 
                {
1488
 
                        // IRQ address is inside this voices loop cache, return distance
1489
 
 
1490
 
                        return (vi->play.cache->dend-vi->play.ptr)+
1491
 
                                                 (irq_ptr-vi->loop.ptr);
1492
 
                }
1493
 
        }
1494
 
 
1495
 
        // IRQs not enabled, or IRQ address not reachable by voice, distance is infinity
1496
 
 
1497
 
        return infinity;
1498
 
}
1499
 
 
1500
 
//
1501
 
//
1502
 
//
1503
 
 
1504
 
void spu_device::update_voice_events(voiceinfo *vi)
1505
 
{
1506
 
        if (vi->pitch)
1507
 
        {
1508
 
                // Calculate time until end of sample in output samples
1509
 
 
1510
 
                vi->samplestoend=(unsigned int)((((INT64)(vi->play.cache->dend-vi->play.ptr)<<12)-vi->dptr)+(vi->pitch-1))/vi->pitch;
1511
 
                if (vi->inloopcache)
1512
 
                {
1513
 
                        // Voice is inside loop cache, return time until end of that if lower
1514
 
 
1515
 
                        assert(vi->lcptr<vi->loop_cache->len);
1516
 
                        vi->samplestoend=min(vi->samplestoend,
1517
 
                                                                                                         (unsigned int)((((INT64)(vi->loop_cache->len-vi->lcptr)<<12)-vi->dptr)+(vi->pitch-1))/vi->pitch);
1518
 
                }
1519
 
 
1520
 
                // Calculate time until next IRQ in output samples
1521
 
 
1522
 
                unsigned int irqdist=get_irq_distance(vi);
1523
 
                if (irqdist!=infinity)
1524
 
                {
1525
 
                        // Convert IRQ input sample distance to output samples
1526
 
 
1527
 
                        vi->samplestoirq=(unsigned int)(((((INT64)irqdist)<<12)-vi->dptr)+(vi->pitch-1))/vi->pitch;
1528
 
                } else
1529
 
                {
1530
 
                        vi->samplestoirq=infinity;
1531
 
                }
1532
 
        } else
1533
 
        {
1534
 
                // Voice pitch is 0, distance to sample end and IRQ is infinity
1535
 
 
1536
 
                vi->samplestoend=vi->samplestoirq=infinity;
1537
 
        }
1538
 
}
1539
 
 
1540
 
//
1541
 
//
1542
 
//
1543
 
 
1544
 
bool spu_device::update_voice_state(const unsigned int v)
1545
 
{
1546
 
        voicereg *vr=&spureg.voice[v];
1547
 
        voiceinfo *vi=&voice[v];
1548
 
 
1549
 
        // Update sample cache if necessary
1550
 
 
1551
 
        if (! vi->play.update(this))
1552
 
                return false;
1553
 
        assert(vi->play.ptr<vi->play.cache->dend);
1554
 
 
1555
 
        // Get pitch from voice register and apply frequency multiplier if
1556
 
        // there is one in effect
1557
 
 
1558
 
        vi->pitch=vr->pitch;
1559
 
        vi->pitch=(unsigned int)(vi->pitch*freq_multiplier);
1560
 
 
1561
 
        // Update event times
1562
 
 
1563
 
        update_voice_events(vi);
1564
 
 
1565
 
        return true;
1566
 
}
1567
 
 
1568
 
//
1569
 
//
1570
 
//
1571
 
 
1572
 
spu_device::sample_loop_cache *spu_device::get_loop_cache(sample_cache *cache, const unsigned int lpen, sample_cache *lpcache, const unsigned int lpst)
1573
 
{
1574
 
        // Check for existing loop cache
1575
 
 
1576
 
        sample_loop_cache *ret=lpcache->find_loop_cache(lpen,lpst);
1577
 
        if (! ret)
1578
 
        {
1579
 
                // No loop cache exists for this address pair, create a new one
1580
 
 
1581
 
                sample_loop_cache *lc=new sample_loop_cache;
1582
 
                lc->loopend=lpen;
1583
 
                lc->loopstart=lpst;
1584
 
                lpcache->add_loop_cache(lc);
1585
 
                ret=lc;
1586
 
 
1587
 
                // Decode samples from start address using decoder state at end address
1588
 
 
1589
 
                unsigned int adr=lpst;
1590
 
                adpcm_decoder tmp=cache->decoder;
1591
 
                signed short *dp=lc->data;
1592
 
                for (unsigned int i=0; ((i<num_loop_cache_packets) &&
1593
 
                                                                                                                (adr<lpcache->end)); i++, adr+=16)
1594
 
                        dp=tmp.decode_packet((adpcm_packet *)(spu_ram+adr),dp);
1595
 
 
1596
 
                #ifdef log_loop_cache
1597
 
                        log(log_spu,"spu: add loop cache %08x %08x->%08x (end at %08x)\n",lc,lpen,lpst,adr);
1598
 
                #endif
1599
 
 
1600
 
                lc->len=dp-lc->data;
1601
 
        }
1602
 
 
1603
 
        return ret;
1604
 
}
1605
 
 
1606
 
//
1607
 
//
1608
 
//
1609
 
 
1610
 
void spu_device::update_voice_loop(const unsigned int v)
1611
 
{
1612
 
//  voicereg *vr=&spureg.voice[v];
1613
 
        voiceinfo *vi=&voice[v];
1614
 
        unsigned int ra = 0;
1615
 
 
1616
 
        // Check for looping using the voices repeat address register and get
1617
 
        // a pointer to the loop position if enabled
1618
 
 
1619
 
        vi->loop.reset();
1620
 
 
1621
 
        // If loop address is not forced get the loop pointer from the cache
1622
 
        // block (if present)
1623
 
 
1624
 
        if ((! voice[v].forceloop) &&
1625
 
                        (vi->play.cache->get_loop_pointer(&vi->loop)))
1626
 
        {
1627
 
                ra=vi->play.cache->loopaddr;
1628
 
        }
1629
 
 
1630
 
        // Otherwise use the address set in repaddr (if set)
1631
 
 
1632
 
        if ((! vi->loop) && (vi->play.cache->is_loop))
1633
 
        {
1634
 
                ra=spureg.voice[v].repaddr<<3;
1635
 
                ra=(ra+0xf)&~0xf;
1636
 
                const adpcm_packet *ap=ra?(adpcm_packet *)(spu_ram+ra):NULL;
1637
 
 
1638
 
                if (ap)
1639
 
                {
1640
 
                        if (ap->flags&adpcmflag_loop)
1641
 
                        {
1642
 
                                // Repeat address points to a block with loop flag set
1643
 
 
1644
 
                                if (! vi->play.cache->get_sample_pointer(ra,&vi->loop))
1645
 
                                {
1646
 
                                        // Repeat address is in a different block
1647
 
 
1648
 
                                        translate_sample_addr(ra,&vi->loop);
1649
 
                                }
1650
 
                        }
1651
 
                }
1652
 
        }
1653
 
 
1654
 
        // Update loop cache
1655
 
 
1656
 
        if (vi->loop)
1657
 
        {
1658
 
                vi->loop_cache=get_loop_cache(vi->play.cache,vi->play.cache->end,vi->loop.cache,ra);
1659
 
        }
1660
 
}
1661
 
 
1662
 
//
1663
 
//
1664
 
//
1665
 
 
1666
 
void spu_device::update_voice_state()
1667
 
{
1668
 
        // If RAM or irq state is dirty make all voices dirty
1669
 
 
1670
 
        if (dirty_flags&(dirtyflag_ram|dirtyflag_irq))
1671
 
        {
1672
 
                dirty_flags|=dirtyflag_voice_mask;
1673
 
                dirty_flags&=~(dirtyflag_ram|dirtyflag_irq);
1674
 
        }
1675
 
 
1676
 
        // Update state for dirty voices
1677
 
 
1678
 
        if (dirty_flags&dirtyflag_voice_mask)
1679
 
        {
1680
 
                unsigned int voicemask=1;
1681
 
                for (unsigned int i=0; i<24; i++, voicemask<<=1)
1682
 
                        if (dirty_flags&voicemask)
1683
 
                        {
1684
 
                                update_voice_state(i);
1685
 
                                dirty_flags&=~voicemask;
1686
 
                        }
1687
 
        }
1688
 
}
1689
 
 
1690
 
//
1691
 
// Process voice state and build output segments
1692
 
//
1693
 
//  Input:  const unsigned int v        Voice number
1694
 
//                  const unsigned int sz       Amount of time to process (in output samples)
1695
 
//          unsigned int *tleft         Returned number of output samples remaining
1696
 
//
1697
 
//  Output: bool                                        true if voice is still playing
1698
 
//
1699
 
 
1700
 
bool spu_device::process_voice(const unsigned int v,
1701
 
                                                                                                                const unsigned int sz,
1702
 
                                                                                                                void *ptr,
1703
 
                                                                                                                void *fmnoise_ptr,
1704
 
                                                                                                                void *outxptr,
1705
 
                                                                                                                unsigned int *tleft)
1706
 
{
1707
 
        bool ret=true;
1708
 
        unsigned int voice_mask=1<<v,
1709
 
                                                         num=sz,
1710
 
                                                         off=0;
1711
 
        bool noise=((spureg.noise&voice_mask)!=0),
1712
 
                         fm=((spureg.fm&voice_mask)!=0);
1713
 
        voiceinfo *vi=&voice[v];
1714
 
 
1715
 
        // Early exit if we don't have a sample cache block
1716
 
 
1717
 
        if (! vi->play)
1718
 
        {
1719
 
                *tleft=sz;
1720
 
                return false;
1721
 
        }
1722
 
 
1723
 
        // Generate samples
1724
 
 
1725
 
        while (num)
1726
 
        {
1727
 
                // Play up to end of sample, envelope event, or IRQ, whichever comes first
1728
 
 
1729
 
                unsigned int ntoplay=fm?1:num,
1730
 
                                                                 nextevent=min(vi->samplestoend,
1731
 
                                                                                                                         min(vi->samplestoirq,vi->envsamples));
1732
 
                ntoplay=min(ntoplay,nextevent);
1733
 
 
1734
 
                if (ntoplay)
1735
 
                {
1736
 
                        signed short *noisep=NULL;
1737
 
 
1738
 
                        if (fm)
1739
 
                        {
1740
 
                                int fmv=((signed short *)fmnoise_ptr)[off<<1];
1741
 
                                vi->pitch=spureg.voice[v].pitch;
1742
 
                                vi->pitch=(unsigned int)(vi->pitch*freq_multiplier);
1743
 
                                vi->pitch=(vi->pitch*(fmv+32768))>>15;
1744
 
                        } else
1745
 
                        if (noise)
1746
 
                        {
1747
 
                                noisep=(signed short *)fmnoise_ptr;
1748
 
                                noisep+=(off<<1);
1749
 
                        }
1750
 
 
1751
 
                        signed short *dp=(signed short *)ptr,
1752
 
                                                                         *outxp=(signed short *)outxptr;
1753
 
                        dp+=off<<1;
1754
 
                        if (outxp) outxp+=off<<1;
1755
 
 
1756
 
                        generate_voice(v, dp, noisep, outxp, ntoplay);
1757
 
 
1758
 
                        num-=ntoplay;
1759
 
                        off+=ntoplay;
1760
 
 
1761
 
                        vi->samplestoend-=ntoplay;
1762
 
                        if (vi->samplestoirq!=infinity) vi->samplestoirq-=ntoplay;
1763
 
                        if (vi->envsamples!=infinity) vi->envsamples-=ntoplay;
1764
 
                        vi->hitirq=false;
1765
 
                }
1766
 
 
1767
 
                // Determine which event(s) we hit
1768
 
 
1769
 
                bool hitend=fm?(vi->play.ptr>=vi->play.cache->dend)
1770
 
                                                                        :(vi->samplestoend==0),
1771
 
                                 hitirq=(vi->samplestoirq==0),
1772
 
                                 hitenv=(vi->envsamples==0);
1773
 
 
1774
 
                // Update loop cache pointer if we are playing a loop cache
1775
 
 
1776
 
                if ((vi->inloopcache) && (vi->lcptr>=vi->loop_cache->len))
1777
 
                {
1778
 
                        vi->inloopcache=false;
1779
 
                        hitend=(vi->play.ptr>=vi->play.cache->dend);
1780
 
 
1781
 
                        #ifdef log_loop_cache
1782
 
                                log(log_spu,"spu: %d leave loop cache %08x, lcptr=%d, hitend=%d\n",
1783
 
                                                                                v,
1784
 
                                                                                vi->loop_cache,
1785
 
                                                                                vi->lcptr,
1786
 
                                                                                hitend);
1787
 
                        #endif
1788
 
                }
1789
 
 
1790
 
                bool stopped=false;
1791
 
 
1792
 
                if (hitend)
1793
 
                {
1794
 
                        // End of sample reached, calculate how far we overshot
1795
 
 
1796
 
                        unsigned int poff=vi->play.ptr-vi->play.cache->dend;
1797
 
 
1798
 
                        // Make sure loop info is up to date and end the current output segment
1799
 
 
1800
 
                        update_voice_loop(v);
1801
 
                        if (vi->loop)
1802
 
                        {
1803
 
                                // We are looping, set play address to loop address and account for
1804
 
                                // overshoot
1805
 
 
1806
 
                                vi->play=vi->loop;
1807
 
                                vi->play.ptr+=poff;
1808
 
                                vi->lcptr=poff;
1809
 
                                vi->inloopcache=(poff<vi->loop_cache->len);
1810
 
 
1811
 
                                #ifdef log_loop_cache
1812
 
                                        if (vi->inloopcache)
1813
 
                                                log(log_spu,"spu: %d enter loop cache %08x, lcptr=%d\n",
1814
 
                                                                                                v,
1815
 
                                                                                                vi->loop_cache,
1816
 
                                                                                                vi->lcptr);
1817
 
                                #endif
1818
 
 
1819
 
                                // Check for IRQ at/just after repeat address
1820
 
 
1821
 
                                if (spureg.ctrl&spuctrl_irq_enable)
1822
 
                                {
1823
 
                                        if (spureg.voice[v].repaddr==spureg.irq_addr)
1824
 
                                                hitirq=true;
1825
 
 
1826
 
                                        signed short *irq_ptr;
1827
 
                                        unsigned int irq_addr=spureg.irq_addr<<3;
1828
 
 
1829
 
                                        if ((irq_ptr=vi->loop.cache->get_sample_pointer(irq_addr)))
1830
 
                                        {
1831
 
                                                if ((irq_ptr>=vi->loop.ptr) &&
1832
 
                                                                (vi->play.ptr>=irq_ptr))
1833
 
                                                        hitirq=true;
1834
 
                                        }
1835
 
                                }
1836
 
                        } else
1837
 
                        {
1838
 
                                // Not looping, stop voice
1839
 
 
1840
 
                                spureg.reverb&=~(1<<v);
1841
 
                                stopped=true;
1842
 
 
1843
 
                                // Check for IRQ at repeat address
1844
 
 
1845
 
                                if (spureg.ctrl&spuctrl_irq_enable)
1846
 
                                {
1847
 
                                        if (spureg.voice[v].repaddr==spureg.irq_addr)
1848
 
                                                hitirq=true;
1849
 
                                }
1850
 
                        }
1851
 
 
1852
 
                        assert((stopped) || (vi->play.ptr<vi->play.cache->dend));
1853
 
                } else
1854
 
                {
1855
 
                        assert(vi->play.ptr<vi->play.cache->dend);
1856
 
                }
1857
 
 
1858
 
                if (hitirq)
1859
 
                {
1860
 
                        // Went past IRQ address, trigger IRQ
1861
 
                        m_irq_cb(this, 1);
1862
 
 
1863
 
                        vi->samplestoirq=infinity;
1864
 
                        vi->hitirq=true;
1865
 
                }
1866
 
 
1867
 
                if (hitenv)
1868
 
                {
1869
 
                        // Envelope event, update the envelope (stop if necessary), and start
1870
 
                        // a new output segment
1871
 
 
1872
 
                        stopped=((stopped) || (! update_envelope(v)));
1873
 
                }
1874
 
 
1875
 
                if (stopped)
1876
 
                {
1877
 
                        // Voice is now stopped
1878
 
 
1879
 
                        ret=false;
1880
 
                        break;
1881
 
                }
1882
 
 
1883
 
                // Update voice event times
1884
 
 
1885
 
                update_voice_events(vi);
1886
 
        }
1887
 
 
1888
 
        // Set current volume register
1889
 
 
1890
 
        spureg.voice[v].curvol=(unsigned short)(vi->env_level*32767.0f);
1891
 
 
1892
 
        // Return how much time is left and whether or not the voice is still playing
1893
 
 
1894
 
        *tleft=num;
1895
 
        return ret;
1896
 
}
1897
 
 
1898
 
//
1899
 
// Generate voice output samples
1900
 
//
1901
 
//  Inputs: const unsigned int v        Voice number
1902
 
//            void *ptr                             Output buffer (if no reverb)
1903
 
//          const unsigned int sz       Number of samples to output
1904
 
//
1905
 
 
1906
 
void spu_device::generate_voice(const unsigned int v,
1907
 
                                                                                                                 void *ptr,
1908
 
                                                                                                                 void *noiseptr,
1909
 
                                                                                                                 void *outxptr,
1910
 
                                                                                                                 const unsigned int sz)
1911
 
{
1912
 
        voiceinfo *vi=&voice[v];
1913
 
        signed short *fp,*sp;
1914
 
        unsigned int n=sz;
1915
 
 
1916
 
        // Get input pointer
1917
 
 
1918
 
        if (vi->inloopcache)
1919
 
        {
1920
 
                sp=vi->loop_cache->data+vi->lcptr;
1921
 
        } else
1922
 
        {
1923
 
                sp=vi->play.ptr;
1924
 
        }
1925
 
        fp=sp;
1926
 
 
1927
 
        unsigned int dptr=vi->dptr;
1928
 
 
1929
 
        // Get output pointer (and advance output offset)
1930
 
 
1931
 
        signed short *dp=(signed short *)ptr;
1932
 
        signed short *outxp=(signed short *)outxptr;
1933
 
 
1934
 
        // Calculate fixed point envelope levels/deltas premultiplied by channel volume
1935
 
 
1936
 
        int     vol_l=outxptr?0x3fff:vi->vol[0],
1937
 
                        vol_r=outxptr?0x3fff:vi->vol[1],
1938
 
                  env_l=(int)(vi->env_level*2.0f*vol_l),
1939
 
                        env_r=(int)(vi->env_level*2.0f*vol_r),
1940
 
                        envdelta_l=(int)(vi->env_delta*2.0f*vol_l),
1941
 
                        envdelta_r=(int)(vi->env_delta*2.0f*vol_r);
1942
 
 
1943
 
        // Update the segments envelope level
1944
 
 
1945
 
        vi->env_level+=(n*vi->env_delta);
1946
 
 
1947
 
        if (noiseptr)
1948
 
        {
1949
 
                INT64 dptr=((INT64)n*vi->pitch)+vi->dptr;
1950
 
                unsigned int d=(unsigned int)(dptr>>12);
1951
 
                vi->dptr=(unsigned int)(dptr&0xfff);
1952
 
                vi->play.ptr+=d;
1953
 
                if (vi->inloopcache) vi->lcptr+=d;
1954
 
 
1955
 
                sp=(signed short *)noiseptr;
1956
 
 
1957
 
                if (outxp)
1958
 
                {
1959
 
                        while (n--)
1960
 
                        {
1961
 
                                int vl=*sp++,
1962
 
                                                vr=*sp++,
1963
 
                                                l=(vl*env_l)>>15,
1964
 
                                                r=(vr*env_r)>>15;
1965
 
                                env_l+=envdelta_l;
1966
 
                                env_r+=envdelta_r;
1967
 
 
1968
 
                                outxp[0]=l;
1969
 
                                outxp[1]=r;
1970
 
                                outxp+=2;
1971
 
 
1972
 
                                l=(l*vi->vol[0])>>15;
1973
 
                                r=(r*vi->vol[1])>>15;
1974
 
 
1975
 
                                dp[0]=clamp(l+dp[0]);
1976
 
                                dp[1]=clamp(r+dp[1]);
1977
 
                                dp+=2;
1978
 
                        }
1979
 
                } else
1980
 
                {
1981
 
                        while (n--)
1982
 
                        {
1983
 
                                int vl=*sp++,
1984
 
                                                vr=*sp++,
1985
 
                                                l=(vl*env_l)>>15,
1986
 
                                                r=(vr*env_r)>>15;
1987
 
                                env_l+=envdelta_l;
1988
 
                                env_r+=envdelta_r;
1989
 
 
1990
 
                                dp[0]=clamp(l+dp[0]);
1991
 
                                dp[1]=clamp(r+dp[1]);
1992
 
                                dp+=2;
1993
 
                        }
1994
 
                }
1995
 
        } else
1996
 
        {
1997
 
                if (1) //settings.sound_interpolate)
1998
 
                {
1999
 
                        unsigned int num_stitch=0;
2000
 
                        signed short *ep;
2001
 
 
2002
 
                        // Linear interpolation enabled, calculate how many samples we will
2003
 
                        // read from input data
2004
 
 
2005
 
                        INT64 fracend=(((INT64)(n-1))*vi->pitch)+dptr;
2006
 
                        unsigned int end=(unsigned int)(fracend>>12);
2007
 
 
2008
 
                        // Get pointer to last sample of input data
2009
 
 
2010
 
                        if (vi->inloopcache)
2011
 
                        {
2012
 
                                ep=vi->loop_cache->data+vi->loop_cache->len-1;
2013
 
                        } else
2014
 
                        {
2015
 
                                ep=vi->play.cache->dend-1;
2016
 
                        }
2017
 
 
2018
 
                        // If we read the last sample "stitching" will be necessary (inerpolation
2019
 
                        // from last sample of segment to first sample of next segment)
2020
 
 
2021
 
                        if (((sp+end)>=ep) && (vi->pitch))
2022
 
                        {
2023
 
                                num_stitch=min(n,max(0x1fff/vi->pitch,1));
2024
 
                                n-=num_stitch;
2025
 
                        }
2026
 
 
2027
 
                        // Generate samples
2028
 
 
2029
 
                        if (outxp)
2030
 
                        {
2031
 
                                while (n--)
2032
 
                                {
2033
 
                                        int v=sp[0];
2034
 
 
2035
 
                                        v+=((sp[1]-v)*(int)dptr)>>12;
2036
 
 
2037
 
                                        int l=(v*env_l)>>15,
2038
 
                                                        r=(v*env_r)>>15;
2039
 
                                        env_l+=envdelta_l;
2040
 
                                        env_r+=envdelta_r;
2041
 
 
2042
 
                                        outxp[0]=l;
2043
 
                                        outxp[1]=r;
2044
 
                                        outxp+=2;
2045
 
 
2046
 
                                        l=(l*vi->vol[0])>>15;
2047
 
                                        r=(r*vi->vol[1])>>15;
2048
 
 
2049
 
                                        dp[0]=clamp(l+dp[0]);
2050
 
                                        dp[1]=clamp(r+dp[1]);
2051
 
                                        dp+=2;
2052
 
 
2053
 
                                        dptr+=vi->pitch;
2054
 
                                        sp+=(dptr>>12);
2055
 
                                        dptr&=0xfff;
2056
 
                                }
2057
 
                        }
2058
 
                        else
2059
 
                        {
2060
 
                                while (n--)
2061
 
                                {
2062
 
                                        int v=sp[0];
2063
 
 
2064
 
                                        v+=((sp[1]-v)*(int)dptr)>>12;
2065
 
 
2066
 
                                        int l=(v*env_l)>>15,
2067
 
                                                        r=(v*env_r)>>15;
2068
 
                                        env_l+=envdelta_l;
2069
 
                                        env_r+=envdelta_r;
2070
 
 
2071
 
                                        dp[0]=clamp(l+dp[0]);
2072
 
                                        dp[1]=clamp(r+dp[1]);
2073
 
                                        dp+=2;
2074
 
 
2075
 
                                        dptr+=vi->pitch;
2076
 
                                        sp+=(dptr>>12);
2077
 
                                        dptr&=0xfff;
2078
 
                                }
2079
 
                        }
2080
 
 
2081
 
                        if (num_stitch)
2082
 
                        {
2083
 
                                // Stitch samples, get the first sample of the next segment
2084
 
 
2085
 
                                signed short *nsp=NULL;
2086
 
 
2087
 
                                if (vi->inloopcache)
2088
 
                                {
2089
 
                                        nsp=vi->play.ptr+(vi->loop_cache->len-vi->lcptr);
2090
 
                                        if (nsp>=vi->play.cache->dend)
2091
 
                                                nsp=NULL;
2092
 
                                }
2093
 
 
2094
 
                                if (! nsp)
2095
 
                                {
2096
 
                                        update_voice_loop(v);
2097
 
                                        if (vi->loop) nsp=vi->loop_cache->data;
2098
 
                                }
2099
 
 
2100
 
                                int ns=nsp?nsp[0]:0;
2101
 
 
2102
 
                                // Generate stitch samples
2103
 
 
2104
 
                                if (outxp)
2105
 
                                {
2106
 
                                        while (num_stitch--)
2107
 
                                        {
2108
 
                                                int v=sp[0],
2109
 
                                                                nv=(sp>=ep)?ns:sp[1];
2110
 
 
2111
 
                                                v+=((nv-v)*(int)dptr)>>12;
2112
 
 
2113
 
                                                int l=(v*env_l)>>15,
2114
 
                                                                r=(v*env_r)>>15;
2115
 
                                                env_l+=envdelta_l;
2116
 
                                                env_r+=envdelta_r;
2117
 
 
2118
 
                                                outxp[0]=l;
2119
 
                                                outxp[1]=r;
2120
 
                                                outxp+=2;
2121
 
 
2122
 
                                                l=(l*vi->vol[0])>>15;
2123
 
                                                r=(r*vi->vol[1])>>15;
2124
 
 
2125
 
                                                dp[0]=clamp(l+dp[0]);
2126
 
                                                dp[1]=clamp(r+dp[1]);
2127
 
                                                dp+=2;
2128
 
 
2129
 
                                                dptr+=vi->pitch;
2130
 
                                                sp+=(dptr>>12);
2131
 
                                                dptr&=0xfff;
2132
 
                                        }
2133
 
                                } else
2134
 
                                {
2135
 
                                        while (num_stitch--)
2136
 
                                        {
2137
 
                                                int v=sp[0],
2138
 
                                                                nv=(sp>=ep)?ns:sp[1];
2139
 
 
2140
 
                                                v+=((nv-v)*(int)dptr)>>12;
2141
 
 
2142
 
                                                int l=(v*env_l)>>15,
2143
 
                                                                r=(v*env_r)>>15;
2144
 
                                                env_l+=envdelta_l;
2145
 
                                                env_r+=envdelta_r;
2146
 
 
2147
 
                                                dp[0]=clamp(l+dp[0]);
2148
 
                                                dp[1]=clamp(r+dp[1]);
2149
 
                                                dp+=2;
2150
 
 
2151
 
                                                dptr+=vi->pitch;
2152
 
                                                sp+=(dptr>>12);
2153
 
                                                dptr&=0xfff;
2154
 
                                        }
2155
 
                                }
2156
 
                        }
2157
 
                } else
2158
 
                {
2159
 
                        // Generate samples with no interpolation
2160
 
 
2161
 
                        if (outxp)
2162
 
                        {
2163
 
                                while (n--)
2164
 
                                {
2165
 
                                        int l=(sp[0]*env_l)>>15,
2166
 
                                                        r=(sp[0]*env_r)>>15;
2167
 
                                        env_l+=envdelta_l;
2168
 
                                        env_r+=envdelta_r;
2169
 
 
2170
 
                                        outxp[0]=l;
2171
 
                                        outxp[1]=r;
2172
 
                                        outxp+=2;
2173
 
 
2174
 
                                        l=(l*vi->vol[0])>>15;
2175
 
                                        r=(r*vi->vol[1])>>15;
2176
 
 
2177
 
                                        dp[0]=clamp(l+dp[0]);
2178
 
                                        dp[1]=clamp(r+dp[1]);
2179
 
                                        dp+=2;
2180
 
 
2181
 
                                        dptr+=vi->pitch;
2182
 
                                        sp+=(dptr>>12);
2183
 
                                        dptr&=0xfff;
2184
 
                                }
2185
 
                        } else
2186
 
                        {
2187
 
                                while (n--)
2188
 
                                {
2189
 
                                        int l=(sp[0]*env_l)>>15,
2190
 
                                                        r=(sp[0]*env_r)>>15;
2191
 
                                        env_l+=envdelta_l;
2192
 
                                        env_r+=envdelta_r;
2193
 
 
2194
 
                                        dp[0]=clamp(l+dp[0]);
2195
 
                                        dp[1]=clamp(r+dp[1]);
2196
 
                                        dp+=2;
2197
 
 
2198
 
                                        dptr+=vi->pitch;
2199
 
                                        sp+=(dptr>>12);
2200
 
                                        dptr&=0xfff;
2201
 
                                }
2202
 
                        }
2203
 
                }
2204
 
 
2205
 
                // Update segment pointer
2206
 
 
2207
 
                vi->play.ptr+=sp-fp;
2208
 
                vi->dptr=dptr;
2209
 
                if (vi->inloopcache)
2210
 
                        vi->lcptr=sp-vi->loop_cache->data;
2211
 
        }
2212
 
}
2213
 
 
2214
 
//
2215
 
//
2216
 
//
2217
 
 
2218
 
bool spu_device::update_envelope(const int v)
2219
 
{
2220
 
        while (voice[v].envsamples==0)
2221
 
        {
2222
 
                voice[v].env_state++;
2223
 
 
2224
 
                switch (voice[v].env_state)
2225
 
                {
2226
 
                        case 1: // decay
2227
 
                                voice[v].env_level=1.0f;
2228
 
                                voice[v].env_delta=voice[v].env_dr;
2229
 
                                if (voice[v].env_dr!=0.0f)
2230
 
                                {
2231
 
                                        voice[v].envsamples=(unsigned int)((voice[v].env_sl-1.0f)/voice[v].env_dr);
2232
 
                                } else
2233
 
                                {
2234
 
                                        voice[v].envsamples=infinity;
2235
 
                                }
2236
 
                                break;
2237
 
 
2238
 
                        case 2: // sustain
2239
 
                                voice[v].env_level=voice[v].env_sl;
2240
 
                                voice[v].env_delta=voice[v].env_sr;
2241
 
 
2242
 
                                if (voice[v].env_sr>0.0f)
2243
 
                                {
2244
 
                                        voice[v].envsamples=(unsigned int)((1.0f-voice[v].env_level)/voice[v].env_sr);
2245
 
                                } else
2246
 
                                if (voice[v].env_sr<0.0f)
2247
 
                                {
2248
 
                                        voice[v].envsamples=(unsigned int)(voice[v].env_level/-voice[v].env_sr);
2249
 
                                } else
2250
 
                                {
2251
 
                                        voice[v].envsamples=infinity;
2252
 
                                }
2253
 
                                break;
2254
 
 
2255
 
                        case 3: // sustain end
2256
 
                                voice[v].envsamples=infinity;
2257
 
                                voice[v].env_delta=0.0f;
2258
 
                                if (voice[v].env_sr<=0.0f)
2259
 
                                {
2260
 
                                        voice[v].env_level=0.0f;
2261
 
                                        return false;
2262
 
                                } else
2263
 
                                {
2264
 
                                        voice[v].env_level=1.0f;
2265
 
                                }
2266
 
                                break;
2267
 
 
2268
 
                        case 4: // release
2269
 
                                voice[v].env_level=mindb(1.0f,maxdb(0.0f,voice[v].env_level));
2270
 
                                voice[v].env_delta=voice[v].env_rr;
2271
 
                                if (voice[v].env_rr == -0.0f)   // 0.0 release means infinite time
2272
 
                                {
2273
 
                                        voice[v].envsamples=infinity;
2274
 
                                }
2275
 
                                else
2276
 
                                {
2277
 
                                        voice[v].envsamples=(unsigned int)(voice[v].env_level/-voice[v].env_rr);
2278
 
                                }
2279
 
                                break;
2280
 
 
2281
 
                        case 5: // release end
2282
 
                                voice[v].env_level=0.0f;
2283
 
                                voice[v].env_delta=0.0f;
2284
 
                                voice[v].envsamples=infinity;
2285
 
                                return false;
2286
 
                }
2287
 
        }
2288
 
        return true;
2289
 
}
2290
 
 
2291
 
//
2292
 
//
2293
 
//
2294
 
 
2295
 
void spu_device::key_on(const int v)
2296
 
{
2297
 
        voice[v].loop.reset();
2298
 
 
2299
 
//  printf("key_on: %d @ %x (pitch %x)\n", v, spureg.voice[v].addr<<3, spureg.voice[v].pitch);
2300
 
 
2301
 
        translate_sample_addr(spureg.voice[v].addr<<3,&voice[v].play);
2302
 
        assert(voice[v].play.ptr<voice[v].play.cache->dend);
2303
 
 
2304
 
        voice[v].keyontime=0; //get_system_time();
2305
 
 
2306
 
        voice[v].dptr=0;
2307
 
        voice[v].inloopcache=false;
2308
 
        voice[v].lcptr=-1;
2309
 
        voice[v].env_level=0.0f;
2310
 
        voice[v].env_state=0;
2311
 
        voice[v].forceloop=false;
2312
 
 
2313
 
        // Note: ChronoCross has note hang problems if this is 0 immediately
2314
 
        //           after key on
2315
 
        spureg.voice[v].curvol=1;
2316
 
 
2317
 
        for (unsigned int ch=0; ch<2; ch++)
2318
 
        {
2319
 
                {
2320
 
                        voice[v].sweep_vol[ch]=1.0f;
2321
 
                }
2322
 
        }
2323
 
 
2324
 
        #ifdef warn_if_sweep_used
2325
 
                static bool sweepused;
2326
 
                if ((spureg.voice[v].vol_l|spureg.voice[v].vol_r)&0x8000)
2327
 
                {
2328
 
                        if (! sweepused)
2329
 
                        {
2330
 
                                printf("sweep\n");
2331
 
                                sweepused=true;
2332
 
                        }
2333
 
                }
2334
 
        #endif
2335
 
 
2336
 
        #ifdef assert_if_sweep_used
2337
 
                assert(((spureg.voice[v].vol_l|spureg.voice[v].vol_r)&0x8000)==0);
2338
 
        #endif
2339
 
 
2340
 
        if (spureg.voice[v].adsl&adsl_am)
2341
 
        {
2342
 
                voice[v].env_ar=get_pos_exp_rate((spureg.voice[v].adsl&adsl_ar_mask)>>adsl_ar_shift);
2343
 
        } else
2344
 
        {
2345
 
                voice[v].env_ar=get_linear_rate((spureg.voice[v].adsl&adsl_ar_mask)>>adsl_ar_shift);
2346
 
        }
2347
 
 
2348
 
        voice[v].env_dr=-get_decay_rate((spureg.voice[v].adsl&adsl_dr_mask)>>adsl_dr_shift);
2349
 
        voice[v].env_sl=get_sustain_level(spureg.voice[v].adsl&adsl_sl_mask);
2350
 
 
2351
 
        if (spureg.voice[v].srrr&srrr_sm)
2352
 
        {
2353
 
                if (spureg.voice[v].srrr&srrr_sd)
2354
 
                {
2355
 
                        voice[v].env_sr=get_neg_exp_rate((spureg.voice[v].srrr&srrr_sr_mask)>>srrr_sr_shift);
2356
 
                } else
2357
 
                {
2358
 
                        voice[v].env_sr=get_pos_exp_rate((spureg.voice[v].srrr&srrr_sr_mask)>>srrr_sr_shift);
2359
 
                }
2360
 
        } else
2361
 
        {
2362
 
                voice[v].env_sr=get_linear_rate((spureg.voice[v].srrr&srrr_sr_mask)>>srrr_sr_shift);
2363
 
                if (spureg.voice[v].srrr&srrr_sd)
2364
 
                        voice[v].env_sr=-voice[v].env_sr;
2365
 
        }
2366
 
 
2367
 
        if (spureg.voice[v].srrr&srrr_rm)
2368
 
        {
2369
 
                voice[v].env_rr=-get_exp_release_rate(spureg.voice[v].srrr&srrr_rr_mask);
2370
 
        } else
2371
 
        {
2372
 
                voice[v].env_rr=-get_linear_release_rate(spureg.voice[v].srrr&srrr_rr_mask);
2373
 
        }
2374
 
 
2375
 
        voice[v].envsamples=(unsigned int)(1.0f/voice[v].env_ar);
2376
 
        voice[v].env_delta=voice[v].env_ar;
2377
 
}
2378
 
 
2379
 
//
2380
 
//
2381
 
//
2382
 
 
2383
 
void spu_device::set_xa_format(const float _freq, const int channels)
2384
 
{
2385
 
        // Adjust frequency to compensate for slightly slower/faster frame rate
2386
 
        float freq=44100.0; //(_freq*get_adjusted_frame_rate())/ps1hw.rcnt->get_vertical_refresh();
2387
 
 
2388
 
        xa_freq=(unsigned int)((freq/44100.0)*4096.0f);
2389
 
        xa_channels=channels;
2390
 
        xa_spf=(unsigned int)(freq/60.0)*channels;
2391
 
}
2392
 
 
2393
 
//
2394
 
//
2395
 
//
2396
 
 
2397
 
void spu_device::generate_xa(void *ptr, const unsigned int sz)
2398
 
{
2399
 
        if (xa_buffer->get_bytes_in())
2400
 
        {
2401
 
                // Don't start playing until 8 frames worth of data are in
2402
 
 
2403
 
        if ((! xa_playing) && (xa_buffer->get_bytes_in()<(xa_spf<<3)))
2404
 
                {
2405
 
//          debug_xa("waiting...\n");
2406
 
                return;
2407
 
                }
2408
 
 
2409
 
                xa_playing=true;
2410
 
 
2411
 
                // Init buffer pointers/counters
2412
 
 
2413
 
                int n=sz>>2;
2414
 
                signed short *sp=(signed short *)xa_buffer->get_tail_ptr(),
2415
 
                                                                 *dp=(signed short *)ptr;
2416
 
                unsigned int noff=(1<<xa_channels),
2417
 
                                                                 oldtail=xa_buffer->get_tail_offset();
2418
 
 
2419
 
                assert((xa_channels==1) || (xa_channels==2));
2420
 
 
2421
 
                // Calculate volume
2422
 
 
2423
 
                int voll=spureg.cdvol_l,
2424
 
                                volr=spureg.cdvol_r;
2425
 
                voll=(voll*xa_voll)>>15;
2426
 
                volr=(volr*xa_volr)>>15;
2427
 
 
2428
 
                // Generate requested number of XA samples
2429
 
 
2430
 
                while ((xa_buffer->get_bytes_in()) && (n--))
2431
 
                {
2432
 
                        // Get left/right input samples
2433
 
 
2434
 
                        int vl=sp[0],
2435
 
                                        vr=sp[xa_channels-1];
2436
 
 
2437
 
                        // Linear interpolation
2438
 
 
2439
 
                        if (1) //settings.sound_interpolate)
2440
 
                        {
2441
 
                                signed short *nsp=(signed short *)xa_buffer->get_tail_ptr(noff);
2442
 
                                int     vdl=nsp[0]-vl,
2443
 
                                                vdr=nsp[xa_channels-1]-vr;
2444
 
 
2445
 
                                vl+=(vdl*(int)xa_cnt)>>12;
2446
 
                                vr+=(vdr*(int)xa_cnt)>>12;
2447
 
                        }
2448
 
 
2449
 
                        // Multiply by
2450
 
 
2451
 
                        vl=(vl*voll)>>15;
2452
 
                        vr=(vr*volr)>>15;
2453
 
 
2454
 
                        // Write to SPU XA buffer (for emulation purposes - some games read this
2455
 
                        // back to do analysers, etc...)
2456
 
 
2457
 
                        *(signed short *)(spu_ram+xa_out_ptr)=vl;
2458
 
                        *(signed short *)(spu_ram+xa_out_ptr+0x800)=vr;
2459
 
                        xa_out_ptr=(xa_out_ptr+2)&0x7ff;
2460
 
 
2461
 
                        // Mix samples into output buffer
2462
 
 
2463
 
                        dp[0]=clamp(dp[0]+vl);
2464
 
                        dp[1]=clamp(dp[1]+vr);
2465
 
                        dp+=2;
2466
 
 
2467
 
                        // Advance input counter/pointer
2468
 
 
2469
 
                        xa_cnt+=xa_freq;
2470
 
                        int ss=(xa_cnt>>12);
2471
 
                        xa_cnt&=0xfff;
2472
 
 
2473
 
                        if (ss)
2474
 
                        {
2475
 
                                ss<<=xa_channels;
2476
 
                                ss=min(ss,(int)xa_buffer->get_bytes_in());
2477
 
 
2478
 
                                xa_buffer->increment_tail(ss);
2479
 
                                sp=(signed short *)xa_buffer->get_tail_ptr();
2480
 
                        }
2481
 
                }
2482
 
 
2483
 
                // Delete buffer markers we have passed
2484
 
 
2485
 
                xa_buffer->delete_markers(oldtail);
2486
 
        }
2487
 
 
2488
 
        // If we run out of input set status to stopped and clear the SPU XA buffer
2489
 
 
2490
 
        if (! xa_buffer->get_bytes_in())
2491
 
        {
2492
 
                xa_playing=false;
2493
 
 
2494
 
                memset(spu_ram,0,0x1000);
2495
 
                xa_out_ptr=0;
2496
 
        }
2497
 
}
2498
 
 
2499
 
//
2500
 
//
2501
 
//
2502
 
 
2503
 
void spu_device::generate_cdda(void *ptr, const unsigned int sz)
2504
 
{
2505
 
        if (cdda_buffer->get_bytes_in())
2506
 
        {
2507
 
                unsigned int cdda_spf=(44100*4)/60.0,
2508
 
                                                                 freq=(unsigned int)((cdda_freq*60.0)/60.0);
2509
 
 
2510
 
                if ((! cdda_playing) && (cdda_buffer->get_bytes_in()<(cdda_spf<<3)))
2511
 
                        return;
2512
 
 
2513
 
                cdda_playing=true;
2514
 
 
2515
 
                int n=sz>>2;
2516
 
                signed short *sp=(signed short *)cdda_buffer->get_tail_ptr(),
2517
 
                                                                 *dp=(signed short *)ptr;
2518
 
                unsigned int oldtail=cdda_buffer->get_tail_offset();
2519
 
 
2520
 
                int voll=spureg.cdvol_l,
2521
 
                                volr=spureg.cdvol_r;
2522
 
 
2523
 
                while ((cdda_buffer->get_bytes_in()) && (n--))
2524
 
                {
2525
 
                        dp[0]=clamp(dp[0]+((sp[0]*voll)>>15));
2526
 
                        dp[1]=clamp(dp[1]+((sp[1]*volr)>>15));
2527
 
                        dp+=2;
2528
 
 
2529
 
                        cdda_cnt+=freq;
2530
 
                        int ss=(cdda_cnt>>12);
2531
 
                        cdda_cnt&=0xfff;
2532
 
 
2533
 
                        if (ss)
2534
 
                        {
2535
 
                                ss<<=2;
2536
 
 
2537
 
                                cdda_buffer->increment_tail(ss);
2538
 
                                sp=(signed short *)cdda_buffer->get_tail_ptr();
2539
 
                        }
2540
 
                }
2541
 
 
2542
 
                cdda_buffer->delete_markers(oldtail);
2543
 
 
2544
 
                if (! cdda_buffer->get_bytes_in())
2545
 
                        cdda_playing=false;
2546
 
 
2547
 
                if (n>0) printf("cdda buffer underflow (n=%d cdda_in=%d spf=%d)\n",n,cdda_buffer->get_bytes_in(),cdda_spf);
2548
 
        }
2549
 
}
2550
 
 
2551
 
//
2552
 
//
2553
 
//
2554
 
 
2555
 
void spu_device::key_off(const int v)
2556
 
{
2557
 
//  printf("key_off: %d\n", v);
2558
 
 
2559
 
        if (voice[v].env_state<=3)
2560
 
        {
2561
 
                voice[v].env_state=3;
2562
 
                voice[v].envsamples=0;
2563
 
        }
2564
 
}
2565
 
 
2566
 
//
2567
 
//
2568
 
//
2569
 
 
2570
 
void spu_device::update_reverb()
2571
 
{
2572
 
        if (dirty_flags&dirtyflag_reverb)
2573
 
        {
2574
 
                cur_reverb_preset=find_reverb_preset((unsigned short *)&reg[0x1c0]);
2575
 
 
2576
 
                if (cur_reverb_preset==NULL)
2577
 
                {
2578
 
//          printf("spu: reverb=unknown (reg 1c0 = %x)\n", reg[0x1c0]);
2579
 
                } else
2580
 
                {
2581
 
//          printf("spu: reverb=%s\n",cur_reverb_preset->name);
2582
 
                        spu_reverb_cfg=&cur_reverb_preset->cfg;
2583
 
 
2584
 
                        if ((mame_stricmp("reverb off",cur_reverb_preset->name)) && (spu_reverb_cfg->band_gain<=0.0f))
2585
 
                        {
2586
 
//              printf("spu: no reverb config for %s\n",cur_reverb_preset->name);
2587
 
                        }
2588
 
                }
2589
 
 
2590
 
                dirty_flags&=~dirtyflag_reverb;
2591
 
        }
2592
 
}
2593
 
 
2594
 
//
2595
 
//
2596
 
//
2597
 
 
2598
 
void spu_device::flush_output_buffer()
2599
 
{
2600
 
        output_head=output_tail=output_size=0;
2601
 
}
2602
 
 
2603
 
//
2604
 
//
2605
 
//
2606
 
 
2607
 
void spu_device::generate(void *ptr, const unsigned int sz)
2608
 
{
2609
 
        cur_generate_sample+=sz>>2;
2610
 
        process_until(cur_generate_sample);
2611
 
 
2612
 
        update_reverb();
2613
 
 
2614
 
        unsigned int left=sz;
2615
 
        unsigned char *dp=(unsigned char *)ptr;
2616
 
 
2617
 
        while ((left) && (output_size))
2618
 
        {
2619
 
                unsigned int n=min(min(left,output_buffer_size-output_head),output_size);
2620
 
                memcpy(dp,output_buf[0]+output_head,n);
2621
 
 
2622
 
                rev->process((signed short *)dp,
2623
 
                                                                 (signed short *)(output_buf[1]+output_head),
2624
 
                                                                 spu_reverb_cfg,
2625
 
                                                                 (signed short)spureg.rvol_l,
2626
 
                                                                 (signed short)spureg.rvol_r,
2627
 
                                                                 n);
2628
 
 
2629
 
                output_size-=n;
2630
 
                output_head+=n;
2631
 
                output_head&=(output_buffer_size-1);
2632
 
                dp+=n;
2633
 
                left-=n;
2634
 
        }
2635
 
 
2636
 
        if (left)
2637
 
        {
2638
 
                memset(dp,0,left);
2639
 
        }
2640
 
 
2641
 
        generate_xa(ptr,sz);
2642
 
        generate_cdda(ptr,sz);
2643
 
}
2644
 
 
2645
 
//
2646
 
//
2647
 
//
2648
 
 
2649
 
void spu_device::update_irq_event()
2650
 
{
2651
 
        if (spureg.ctrl&spuctrl_irq_enable)
2652
 
        {
2653
 
                unsigned int samplestoirq=infinity;
2654
 
                for (int i=0; i<24; i++)
2655
 
                        if (voice[i].samplestoirq!=infinity)
2656
 
                        {
2657
 
                                if (voice[i].samplestoirq==0)
2658
 
                                {
2659
 
                                        m_irq_cb(this, 1);
2660
 
 
2661
 
                                        voice[i].samplestoirq=infinity;
2662
 
                                        voice[i].hitirq=true;
2663
 
                                } else
2664
 
                                {
2665
 
                                        samplestoirq=min(samplestoirq,voice[i].samplestoirq);
2666
 
                                }
2667
 
                        }
2668
 
        }
2669
 
}
2670
 
 
2671
 
 
2672
 
//
2673
 
//
2674
 
//
2675
 
 
2676
 
void spu_device::generate_noise(void *ptr, const unsigned int num)
2677
 
{
2678
 
        unsigned int np=(unsigned int)(65536.0f/(0x40-((spureg.ctrl&spuctrl_noise_mask)>>spuctrl_noise_shift)));
2679
 
        np=((np<<1)+np)>>1;
2680
 
 
2681
 
        signed short *dp=(signed short *)ptr;
2682
 
 
2683
 
        for (unsigned int i=0; i<num; i++)
2684
 
        {
2685
 
                signed short v=noise_cur;
2686
 
                *dp++=v;
2687
 
                *dp++=v;
2688
 
                noise_t+=np;
2689
 
 
2690
 
                if (noise_t>0xffff)
2691
 
                {
2692
 
                        noise_t-=0xffff;
2693
 
                        shift_register15(noise_seed);
2694
 
                        noise_cur=noise_seed<<1;
2695
 
                }
2696
 
        }
2697
 
}
2698
 
 
2699
 
//
2700
 
//
2701
 
//
2702
 
 
2703
 
void spu_device::process_until(const unsigned int tsample)
2704
 
{
2705
 
        while (tsample>cur_frame_sample)
2706
 
        {
2707
 
                unsigned int process_samples=(unsigned int)(tsample-cur_frame_sample);
2708
 
 
2709
 
                // Drop samples from the head of the queue if its full
2710
 
 
2711
 
                process_samples=min(process_samples,output_buffer_size>>2);
2712
 
                unsigned int nsz=output_size+(process_samples<<2);
2713
 
                if (nsz>output_buffer_size)
2714
 
                {
2715
 
                        nsz-=output_buffer_size;
2716
 
 
2717
 
                        output_head+=nsz;
2718
 
                        output_size-=nsz;
2719
 
                        output_head&=(output_buffer_size-1);
2720
 
                }
2721
 
 
2722
 
                // Decide how many samples to process taking into account buffer
2723
 
                // wrap in output queue.  Get pointers to the queues.
2724
 
 
2725
 
                process_samples=min(process_samples,
2726
 
                                                                                                (output_buffer_size-output_tail)>>2);
2727
 
 
2728
 
                unsigned char *outptr=output_buf[0]+output_tail,
2729
 
                                                                        *reverbptr=output_buf[1]+output_tail,
2730
 
                                                                        *fmptr=output_buf[2]+output_tail,
2731
 
                                                                        *noiseptr=output_buf[3]+output_tail;
2732
 
 
2733
 
                output_tail+=process_samples<<2;
2734
 
                output_tail&=(output_buffer_size-1);
2735
 
                output_size+=process_samples<<2;
2736
 
                assert(output_size<=output_buffer_size);
2737
 
 
2738
 
                // Intialise the output samples to 0 (process_voice always adds samples)
2739
 
 
2740
 
                memset(outptr,0,process_samples<<2);
2741
 
                memset(reverbptr,0,process_samples<<2);
2742
 
 
2743
 
                // If noise is enabled for any channels generate noise samples
2744
 
 
2745
 
                if (spureg.noise&0xffffff)
2746
 
                        generate_noise(noiseptr,process_samples);
2747
 
 
2748
 
                unsigned int mask=1;
2749
 
                for (int i=0; i<24; i++, mask<<=1)
2750
 
                {
2751
 
                        unsigned int tleft=process_samples;
2752
 
                        bool isfmin=((i<23) && (spureg.fm&(1<<(i+1)))),
2753
 
                                         isfm=(spureg.fm&(1<<i))!=0,
2754
 
                                         isnoise=(spureg.noise&(1<<i))!=0,
2755
 
                                         isreverb=(spureg.reverb&(1<<i))!=0;
2756
 
 
2757
 
                        // This channel is an FM input for the next channel - clear the
2758
 
                        // FM input buffer
2759
 
 
2760
 
                        if (isfmin)
2761
 
                                memset(fmptr,0,process_samples<<2);
2762
 
 
2763
 
                        if (spureg.chon&mask)
2764
 
                        {
2765
 
                                // Generate samples
2766
 
 
2767
 
                                if (! process_voice(i,
2768
 
                                                                                                                process_samples,
2769
 
                                                                                                                isreverb?reverbptr:outptr,
2770
 
                                                                                                                isnoise?noiseptr
2771
 
                                                                                                                       :(isfm?fmptr:NULL),
2772
 
                                                                                                                isfmin?fmptr:NULL,
2773
 
                                                                                                                &tleft))
2774
 
                                {
2775
 
                                        spureg.chon&=~mask;
2776
 
                                        //spureg.reverb&=~mask;
2777
 
 
2778
 
                                        voice[i].play.reset();
2779
 
                                        voice[i].loop.reset();
2780
 
                                }
2781
 
                        } else
2782
 
                        {
2783
 
                                spureg.voice[i].curvol=0;
2784
 
                        }
2785
 
                }
2786
 
 
2787
 
                cur_frame_sample+=process_samples;
2788
 
        }
2789
 
}
2790
 
 
2791
 
//
2792
 
//
2793
 
//
2794
 
 
2795
 
void spu_device::update_timing()
2796
 
{
2797
 
        samples_per_frame=44100.0/60.0; //get_adjusted_frame_rate();
2798
 
        samples_per_cycle=samples_per_frame/60*(44100*768); //ps1hw.rcnt->get_vertical_cycles();
2799
 
 
2800
 
}
2801
 
 
2802
 
//
2803
 
//
2804
 
//
2805
 
 
2806
 
void spu_device::sound_stream_update(sound_stream &stream, stream_sample_t **inputs, stream_sample_t **outputs, int samples)
2807
 
{
2808
 
        stream_sample_t *outL, *outR;
2809
 
        INT16 temp[44100], *src;
2810
 
 
2811
 
        outL = outputs[0];
2812
 
        outR = outputs[1];
2813
 
 
2814
 
        generate(temp, samples*4);      // second parameter is bytes, * 2 (size of INT16) * 2 (stereo)
2815
 
 
2816
 
        src = &temp[0];
2817
 
        for (int i = 0; i < samples; i++)
2818
 
        {
2819
 
                *outL++ = *src++;
2820
 
                *outR++ = *src++;
2821
 
        }
2822
 
}
2823
 
 
2824
 
//
2825
 
//
2826
 
//
2827
 
 
2828
 
void spu_device::start_dma(UINT8 *mainram, bool to_spu, UINT32 size)
2829
 
{
2830
 
        UINT32 st=spureg.trans_addr<<3, en=st+size;
2831
 
 
2832
 
        if (en>(512*1024))
2833
 
        {
2834
 
                en=512*1024;
2835
 
                size=en-st;
2836
 
        }
2837
 
 
2838
 
        if (to_spu)
2839
 
        {
2840
 
                invalidate_cache(st,en);
2841
 
 
2842
 
                memcpy(spu_ram+(spureg.trans_addr<<3), mainram, size);
2843
 
 
2844
 
                dirty_flags|=dirtyflag_ram;
2845
 
        }
2846
 
        else
2847
 
        {
2848
 
                memcpy(mainram, spu_ram+(spureg.trans_addr<<3), size);
2849
 
        }
2850
 
}
2851
 
 
2852
 
//
2853
 
//
2854
 
//
2855
 
 
2856
 
void spu_device::decode_xa_mono(const unsigned char *xa,
2857
 
                                                                                                                 unsigned char *ptr)
2858
 
{
2859
 
        signed short *dp=(signed short *)ptr;
2860
 
 
2861
 
        int l0=xa_last[0],
2862
 
                        l1=xa_last[1];
2863
 
 
2864
 
        for (int b=0; b<18; b++)
2865
 
        {
2866
 
                for (int s=0; s<4; s++)
2867
 
                {
2868
 
                        unsigned char flags=xa[4+(s<<1)],
2869
 
                                                                                shift=flags&0xf,
2870
 
                                                                                filter=flags>>4;
2871
 
                        int f0=filter_coef[filter][0],
2872
 
                                        f1=filter_coef[filter][1];
2873
 
                        int i;
2874
 
 
2875
 
                        for (i=0; i<28; i++)
2876
 
                        {
2877
 
                                short d=(xa[16+(i<<2)+s]&0xf)<<12;
2878
 
                                d=clamp((d>>shift)+(((l0*f0)+(l1*f1)+32)>>6));
2879
 
                                *dp++=d;
2880
 
                                l1=l0;
2881
 
                                l0=d;
2882
 
                        }
2883
 
 
2884
 
                        flags=xa[5+(s<<1)];
2885
 
                        shift=flags&0xf;
2886
 
                        filter=flags>>4;
2887
 
                        f0=filter_coef[filter][0];
2888
 
                        f1=filter_coef[filter][1];
2889
 
 
2890
 
                        for (i=0; i<28; i++)
2891
 
                        {
2892
 
                                short d=(xa[16+(i<<2)+s]>>4)<<12;
2893
 
                                d=clamp((d>>shift)+(((l0*f0)+(l1*f1)+32)>>6));
2894
 
                                *dp++=d;
2895
 
                                l1=l0;
2896
 
                                l0=d;
2897
 
                        }
2898
 
                }
2899
 
 
2900
 
                xa+=128;
2901
 
        }
2902
 
 
2903
 
        xa_last[0]=l0;
2904
 
        xa_last[1]=l1;
2905
 
}
2906
 
 
2907
 
//
2908
 
//
2909
 
//
2910
 
 
2911
 
void spu_device::decode_xa_stereo(const unsigned char *xa,
2912
 
                                                                                                                         unsigned char *ptr)
2913
 
{
2914
 
        signed short *dp=(signed short *)ptr;
2915
 
 
2916
 
        int l0=xa_last[0],
2917
 
                        l1=xa_last[1],
2918
 
                        l2=xa_last[2],
2919
 
                        l3=xa_last[3];
2920
 
 
2921
 
        for (int b=0; b<18; b++)
2922
 
        {
2923
 
                for (int s=0; s<4; s++)
2924
 
                {
2925
 
                        unsigned char flags0=xa[4+(s<<1)],
2926
 
                                                                                shift0=flags0&0xf,
2927
 
                                                                                filter0=flags0>>4,
2928
 
                                                                                flags1=xa[5+(s<<1)],
2929
 
                                                                                shift1=flags1&0xf,
2930
 
                                                                                filter1=flags1>>4;
2931
 
 
2932
 
                        int f0=filter_coef[filter0][0],
2933
 
                                        f1=filter_coef[filter0][1],
2934
 
                                        f2=filter_coef[filter1][0],
2935
 
                                        f3=filter_coef[filter1][1];
2936
 
 
2937
 
                        for (int i=0; i<28; i++)
2938
 
                        {
2939
 
                                short d=xa[16+(i<<2)+s],
2940
 
                                                        d0=(d&0xf)<<12,
2941
 
                                                        d1=(d>>4)<<12;
2942
 
                                d0=clamp((int)(d0>>shift0)+(((l0*f0)+(l1*f1)+32)>>6));
2943
 
                                *dp++=d0;
2944
 
                                l1=l0;
2945
 
                                l0=d0;
2946
 
 
2947
 
                                d1=clamp((int)(d1>>shift1)+(((l2*f2)+(l3*f3)+32)>>6));
2948
 
                                *dp++=d1;
2949
 
                                l3=l2;
2950
 
                                l2=d1;
2951
 
                        }
2952
 
                }
2953
 
 
2954
 
                xa+=128;
2955
 
        }
2956
 
 
2957
 
        xa_last[0]=l0;
2958
 
        xa_last[1]=l1;
2959
 
        xa_last[2]=l2;
2960
 
        xa_last[3]=l3;
2961
 
}
2962
 
 
2963
 
//
2964
 
//
2965
 
//
2966
 
 
2967
 
/*
2968
 
enum
2969
 
{
2970
 
    xaencoding_stereo_mask=3,
2971
 
    xaencoding_freq_shift=2,
2972
 
    xaencoding_freq_mask=3<<xaencoding_freq_shift,
2973
 
    xaencoding_bps_shift=4,
2974
 
    xaencoding_bps_mask=3<<xaencoding_bps_shift,
2975
 
    xaencoding_emphasis=(1<<6)
2976
 
};
2977
 
*/
2978
 
 
2979
 
bool spu_device::play_xa(const unsigned int sector, const unsigned char *xa)
2980
 
{
2981
 
        // Don't process the sector if the buffer is full
2982
 
 
2983
 
        if (xa_buffer->get_bytes_free()<xa_sector_size) return false;
2984
 
 
2985
 
//  debug_xa("play_xa: %d\n",sector);
2986
 
 
2987
 
        // Get XA format from sector header
2988
 
 
2989
 
        const unsigned char *hdr=xa+4;
2990
 
        float freq;
2991
 
        int channels;
2992
 
 
2993
 
        switch (hdr[3]&0x3f)    // ignore emphasis and reserved bits
2994
 
        {
2995
 
                case 0:
2996
 
                        channels=1;
2997
 
                        freq=37800.0f;  //18900.0f;
2998
 
                        break;
2999
 
 
3000
 
                case 1:
3001
 
                        channels=2;
3002
 
                        freq=37800.0f;
3003
 
                        break;
3004
 
 
3005
 
                case 4:
3006
 
                        channels=1;
3007
 
                        freq=18900.0f;  ///2.0f;
3008
 
                        break;
3009
 
 
3010
 
                case 5:
3011
 
                        channels=2;
3012
 
                        freq=18900.0f;  //37800.0f/2.0f;
3013
 
                        break;
3014
 
 
3015
 
                default:
3016
 
                        printf("play_xa: unhandled xa mode %08x\n",hdr[3]);
3017
 
                        return true;
3018
 
        }
3019
 
 
3020
 
        set_xa_format(freq,channels);
3021
 
 
3022
 
        // Store XA marker
3023
 
 
3024
 
        unsigned char *ptr=xa_buffer->add_sector(sector);
3025
 
 
3026
 
        // Decode the sector
3027
 
 
3028
 
        if (channels==2)
3029
 
        {
3030
 
                decode_xa_stereo(xa+8,ptr);
3031
 
        } else
3032
 
        {
3033
 
                decode_xa_mono(xa+8,ptr);
3034
 
        }
3035
 
 
3036
 
        // Return that we processed the sector
3037
 
 
3038
 
        return true;
3039
 
}
3040
 
 
3041
 
//
3042
 
// Flush everything after a given sector in the XA buffer
3043
 
//
3044
 
 
3045
 
void spu_device::flush_xa(const unsigned int sector)
3046
 
{
3047
 
//  debug_xa("flush_xa: %d\n",sector);
3048
 
 
3049
 
        if (xa_playing)
3050
 
        {
3051
 
                xa_buffer->flush(sector);
3052
 
        } else
3053
 
        {
3054
 
                // Not playing, flush the entire buffer
3055
 
 
3056
 
                xa_buffer->flush_all();
3057
 
                xa_cnt=0;
3058
 
        }
3059
 
}
3060
 
 
3061
 
//
3062
 
//
3063
 
//
3064
 
 
3065
 
bool spu_device::play_cdda(const unsigned int sector, const unsigned char *cdda)
3066
 
{
3067
 
        if (cdda_buffer->get_bytes_free()<cdda_sector_size) return false;
3068
 
 
3069
 
        signed short *dp=(signed short *)cdda_buffer->add_sector(sector);
3070
 
        memcpy(dp,cdda,cdda_sector_size);
3071
 
 
3072
 
        return true;
3073
 
}
3074
 
 
3075
 
void spu_device::flush_cdda(const unsigned int sector)
3076
 
{
3077
 
//  debug_xa("flush_cdda: %d\n",sector);
3078
 
 
3079
 
        if (cdda_playing)
3080
 
        {
3081
 
                cdda_buffer->flush(sector);
3082
 
        } else
3083
 
        {
3084
 
                cdda_buffer->flush_all();
3085
 
                cdda_cnt=0;
3086
 
        }
3087
 
}
3088
 
 
3089
 
// MAME I/O stuff.  This can get cleaner when machine/psx.c does.
3090
 
 
3091
 
void spu_device::dma_read( UINT32 n_address, INT32 n_size )
3092
 
{
3093
 
        UINT8 *psxram = (UINT8 *)memory_get_shared(machine(), "share1");
3094
 
 
3095
 
        start_dma(psxram + n_address, false, n_size*4);
3096
 
}
3097
 
 
3098
 
void spu_device::dma_write( UINT32 n_address, INT32 n_size )
3099
 
{
3100
 
        UINT8 *psxram = (UINT8 *)memory_get_shared(machine(), "share1");
3101
 
 
3102
 
//  printf("SPU DMA write from %x, size %x\n", n_address, n_size);
3103
 
 
3104
 
        start_dma(psxram + n_address, true, n_size*4);
3105
 
}
3106
 
 
3107
 
READ16_HANDLER( spu_r )
3108
 
{
3109
 
        spu_device *spu = space->machine().device<spu_device>("spu");
3110
 
 
3111
 
        if (spu == NULL )
3112
 
        {
3113
 
                return 0;
3114
 
        }
3115
 
 
3116
 
        return spu->read_word(offset*2);
3117
 
}
3118
 
 
3119
 
WRITE16_HANDLER( spu_w )
3120
 
{
3121
 
        spu_device *spu = space->machine().device<spu_device>("spu");
3122
 
 
3123
 
        if (spu == NULL)
3124
 
        {
3125
 
                return;
3126
 
        }
3127
 
 
3128
 
        spu->write_word(offset*2, data);
3129
 
}