3
Sony PlayStation SPU (CXD2922BQ/CXD2925Q) emulator
5
MAME adaptation by R. Belmont
11
#include "spureverb.h"
12
#include "includes/psx.h"
18
//#define warn_if_sweep_used
19
#define assert_if_sweep_used
20
//#define debug_spu_registers
22
//#define log_loop_cache
23
//#define show_xa_debug
26
// #define show_cache_update
30
#define debug_xa printf
32
#define debug_xa if (0)
35
// device type definition
36
const device_type SPU = &device_creator<spu_device>;
41
INLINE unsigned int min(unsigned int a, unsigned int b)
43
return (a > b) ? b : a;
46
INLINE unsigned int max(unsigned int a, unsigned int b)
48
return (a > b) ? a : b;
51
INLINE double mindb(double a, double b)
53
return (a > b) ? b : a;
56
INLINE double maxdb(double a, double b)
58
return (a > b) ? a : b;
64
spureg_voice_last=0x17f,
75
spureg_reverb_addr=0x1a2,
76
spureg_irq_addr=0x1a4,
77
spureg_trans_addr=0x1a6,
85
spureg_reverb_config=0x1c0,
91
spuctrl_irq_enable=0x40,
92
spuctrl_noise_shift=8,
93
spuctrl_noise_mask=0x3f<<spuctrl_noise_shift
100
adpcmflag_loop_start=4
114
adsl_ar_mask=0x7f<<adsl_ar_shift,
116
adsl_dr_mask=0xf<<adsl_dr_shift,
125
srrr_sr_mask=0x7f<<srrr_sr_shift,
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,
140
output_buffer_size=65536/8,
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,
147
stream_marker_pool_size=64,
148
stream_marker_extend_size=64;
154
static const int filter_coef[5][2]=
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"
178
#define _voice_mask_register(_name) \
182
static const char *spu_register_names[256]=
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),
212
"keyon0-15", "keyon16-23",
213
"keyoff0-15", "keyoff16-23",
215
"noise0-15", "noise16-23",
216
"reverb0-15", "reverb16-23",
217
"chon0-15", "chon16-23",
232
const char *get_register_name(const unsigned int addr)
234
return spu_register_names[(addr&0x1ff)>>1];
238
//**************************************************************************
240
//**************************************************************************
242
reverb_params *spu_reverb_cfg=NULL;
244
float spu_device::freq_multiplier=1.0f;
246
//**************************************************************************
247
// DEVICE CONFIGURATION
248
//**************************************************************************
260
adpcm_decoder(const adpcm_decoder &other)
265
adpcm_decoder &operator =(const adpcm_decoder &other)
277
signed short *decode_packet(adpcm_packet *ap, signed short *dp);
284
struct spu_device::sample_cache
293
signed short *data,*loop,*dend;
294
adpcm_decoder decoder, update_decoder;
295
mutable int ref_count;
298
sample_loop_cache *loop_cache;
300
static unsigned int cache_size;
303
: invalid_start(0xffffffff),
305
last_update_end(0xffffffff),
316
void add_ref() const { ref_count++; }
317
void remove_ref() const
322
cache_size-=(dend-data)<<1;
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);
334
bool is_valid_pointer(signed short *ptr) const;
336
bool try_update(spu_device *spu);
339
unsigned int spu_device::sample_cache::cache_size;
345
struct spu_device::sample_loop_cache
348
unsigned int loopend,
351
signed short data[num_loop_cache_samples];
352
sample_loop_cache *next;
357
sample_cache::cache_size+=num_loop_cache_samples<<1;
362
sample_cache::cache_size-=num_loop_cache_samples<<1;
364
#ifdef log_loop_cache
365
log(log_spu,"spu: destroy loop cache %08x\n",this);
374
struct spu_device::cache_pointer
385
cache_pointer(const cache_pointer &other)
392
cache_pointer(signed short *_ptr, sample_cache *_cache)
396
if (cache) cache->add_ref();
405
cache_pointer &operator =(const cache_pointer &other);
406
bool update(spu_device *spu);
408
unsigned int get_address() const
412
return cache->get_sample_address(ptr);
419
operator bool() const { return cache!=NULL; }
421
bool is_valid() const { return ((cache) && (cache->is_valid_pointer(ptr))); }
428
struct spu_device::voiceinfo
430
cache_pointer play,loop;
431
sample_loop_cache *loop_cache;
476
unsigned char *buffer;
483
stream_marker *marker_head,
487
stream_buffer(const unsigned int _sector_size,
488
const unsigned int _num_sectors)
492
sector_size(_sector_size),
493
num_sectors(_num_sectors),
497
buffer_size=sector_size*num_sectors;
498
buffer=new unsigned char [buffer_size];
499
memset(buffer,0,buffer_size);
508
unsigned char *add_sector(const unsigned int sector)
510
stream_marker *xam=new stream_marker;
514
xam->prev=marker_tail;
517
marker_tail->next=xam;
524
unsigned char *ret=buffer+head;
525
head=(head+sector_size)%buffer_size;
530
void flush(const unsigned int sector)
532
// Remove markers from the end of the buffer if they are after
533
// the specified sector
535
while ((marker_tail) && (marker_tail->sector>=sector))
537
// debug_xa("flushing: %d\n", marker_tail->sector);
539
stream_marker *xam=marker_tail;
541
marker_tail=xam->prev;
542
if (marker_tail) marker_tail->next=NULL;
546
// Set marker head to NULL if the list is now empty
548
if (! marker_tail) marker_head=NULL;
550
// Adjust buffer size counter
553
if (sz<0) sz+=buffer_size;
560
// NOTE: ??what happens to the markers??
564
stream_marker *m=marker_head;
565
marker_head=marker_head->next;
569
marker_head=marker_tail=NULL;
573
void delete_markers(const unsigned int oldtail)
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));
584
// debug_xa("passed: %d\n",marker_head->sector);
586
stream_marker *xam=marker_head;
587
marker_head=xam->next;
589
if (marker_head) marker_head->prev=NULL;
592
if (! marker_head) marker_head=marker_tail=NULL;
595
unsigned int get_bytes_in() const { return in; }
596
unsigned int get_bytes_free() const { return buffer_size-in; }
598
unsigned char *get_tail_ptr() const { return buffer+tail; }
599
unsigned char *get_tail_ptr(const unsigned int offset) const
601
return buffer+((tail+offset)%buffer_size);
603
unsigned int get_tail_offset() const { return tail; }
604
void increment_tail(const unsigned int offset)
606
tail=(tail+offset)%buffer_size;
615
static inline int clamp(const int v)
617
if (v<-32768) return -32768;
618
if (v>32767) return 32767;
626
spu_device::sample_cache::~sample_cache()
631
sample_loop_cache *lc=loop_cache;
641
signed short *spu_device::sample_cache::get_sample_pointer(const unsigned int addr)
643
if ((addr>=start) && (addr<end))
645
return data+(((addr-start)>>4)*28);
656
bool spu_device::sample_cache::get_sample_pointer(const unsigned int addr, cache_pointer *cp)
659
if ((cp->ptr=get_sample_pointer(addr)))
672
bool spu_device::sample_cache::get_loop_pointer(cache_pointer *cp)
688
unsigned int spu_device::sample_cache::get_sample_address(const signed short *ptr) const
690
if ((ptr>=data) && (ptr<=dend))
692
return start+(((ptr-data)/28)<<4);
703
spu_device::sample_loop_cache *spu_device::sample_cache::find_loop_cache(const unsigned int lpend, const unsigned int lpstart)
705
sample_loop_cache *lc;
706
for (lc=loop_cache; lc; lc=lc->next)
707
if ((lc->loopend==lpend) && (lc->loopstart==lpstart)) break;
715
void spu_device::sample_cache::add_loop_cache(sample_loop_cache *lc)
725
bool spu_device::sample_cache::is_valid_pointer(signed short *ptr) const
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)))
738
bool spu_device::sample_cache::try_update(spu_device *spu)
740
if ((invalid_start>=start) && (invalid_end<=end))
742
adpcm_packet *ap=(adpcm_packet *)(spu->spu_ram+start);
746
for (a=start; a<=end; a+=16, ap++)
748
if ((ap->flags&adpcmflag_loop_start) && (ap->flags&adpcmflag_loop)) loop=a;
749
if (ap->flags&adpcmflag_end) break;
752
if ((a==(end-16)) && (loop==loopaddr))
754
#ifdef show_cache_update
755
printf("updating %p: ",this);
758
if (invalid_start==start)
760
#ifdef show_cache_update
761
printf("using end values");
764
update_decoder=decoder;
766
if (invalid_start!=last_update_end)
768
#ifdef show_cache_update
769
printf("resetting decoder (istrt=%08x lupd=%08x)",invalid_start,last_update_end);
772
update_decoder.reset();
774
#ifdef show_cache_update
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);
783
if (invalid_end==end)
785
#ifdef show_cache_update
786
printf("updating end values\n");
788
decoder=update_decoder;
790
last_update_end=invalid_end;
792
for (sample_loop_cache *lc=loop_cache; lc; lc=lc->next)
794
if (invalid_start==lc->loopstart)
796
adpcm_decoder tmp=decoder;
797
signed short *dp=lc->data,
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);
806
invalid_start=0xffffffff;
809
for (unsigned int a=start; a<end; a+=16, ap++)
811
spu->cache[a>>4]=this;
827
void spu_device::cache_pointer::reset()
841
spu_device::cache_pointer &spu_device::cache_pointer::operator =(const cache_pointer &other)
843
if (cache) cache->remove_ref();
846
if (cache) cache->add_ref();
854
bool spu_device::cache_pointer::update(spu_device *spu)
856
if ((cache) && (! cache->valid))
858
/* log(log_spu,"cache_pointer::update: block %08x-%08x invalidated %08x-%08x\n",
861
cache->invalid_start,
862
cache->invalid_end);*/
864
if (! cache->try_update(spu))
866
// Cache is invalid, calculate play address offset from start of
869
unsigned int off=ptr-cache->data,
872
// Release cache block and get updated one
874
spu->translate_sample_addr(addr,this);
876
// Calculate play address in new cache block
880
if (ptr>=cache->dend)
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
885
spu->translate_sample_addr(addr+((off/28)<<4),this);
890
// Return false if we do not have a cache block or the play address is invalid
892
if ((cache) && ((ptr>=cache->data) && (ptr<cache->dend)))
906
signed short *adpcm_decoder::decode_packet(adpcm_packet *ap, signed short *dp)
908
int shift=ap->info&0xf,
910
f0=filter_coef[filter][0],
911
f1=filter_coef[filter][1];
913
for (int i=0; i<14; i++)
915
unsigned char b=ap->data[i];
916
short bl=(b&0xf)<<12,
919
bl=(bl>>shift)+(((l0*f0)+(l1*f1)+32)>>6);
924
bh=(bh>>shift)+(((l0*f0)+(l1*f1)+32)>>6);
937
static int shift_register15(int &shift)
939
int bit0, bit1, bit14;
942
bit1 = (shift & 2) >> 1;
943
bit14 = (bit0 ^ bit1) ^ 1;
945
shift |= (bit14 << 14);
953
//-------------------------------------------------
954
// spu_device - constructor
955
//-------------------------------------------------
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),
962
status_enabled(false),
970
//-------------------------------------------------
971
// static_set_irqf - configuration helper to set
973
//-------------------------------------------------
975
void spu_device::static_set_irqf(device_t &device, void (*irqf)(device_t *device, UINT32 state))
977
spu_device &spu = downcast<spu_device &>(device);
981
void spu_device::device_start()
983
voice=new voiceinfo [24];
984
spu_ram=new unsigned char [spu_ram_size];
986
xa_buffer=new stream_buffer(xa_sector_size,xa_buffer_sectors);
987
cdda_buffer=new stream_buffer(cdda_sector_size,cdda_buffer_sectors);
991
cache=new sample_cache *[spu_ram_size>>4];
992
memset(cache,0,(spu_ram_size>>4)*sizeof(sample_cache *));
994
// register save state stuff
995
// per-voice variables
996
for (int v = 0; v < 24; v++)
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);
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);
1033
void spu_device::device_reset()
1035
cur_reverb_preset = NULL;
1036
cur_frame_sample = 0;
1038
sample_cache::cache_size = 0;
1040
status_enabled = false;
1041
xa_voll = xa_volr = 0x8000;
1051
memset(xa_last,0,sizeof(xa_last));
1056
memset(spu_ram,0,spu_ram_size);
1057
memset(reg,0,0x200);
1058
memset(voice,0,sizeof(voiceinfo)*24);
1060
spureg.status|=(1<<7)|(1<<10);
1062
memset(cache,0,(spu_ram_size>>4)*sizeof(sample_cache *));
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;
1070
noise_cur=shift_register15(noise_seed)?0x7fff:0x8000;
1073
void spu_device::device_post_load()
1075
// invalidate the SPURAM cache
1076
invalidate_cache(0, spu_ram_size);
1077
flush_output_buffer();
1079
// mark everything dirty
1082
// kill and reallocate reverb to avoid artifacts
1084
rev = new reverb(44100);
1086
// and do some update processing
1089
update_voice_state();
1096
void spu_device::device_stop()
1098
for (unsigned int i=0; i<4; i++)
1099
global_free(output_buf[i]);
1103
global_free(spu_ram);
1104
invalidate_cache(0,spu_ram_size);
1106
global_free(xa_buffer);
1107
global_free(cdda_buffer);
1114
void spu_device::init_stream()
1116
const unsigned int hz=44100;
1118
m_stream = machine().sound().stream_alloc(*this, 0, 2, hz);
1122
cdda_freq=(unsigned int)((44100.0f/(float)hz)*4096.0f);
1123
freq_multiplier=(float)spu_base_frequency_hz/(float)hz;
1130
void spu_device::kill_stream()
1140
void spu_device::reinit_sound()
1144
flush_output_buffer();
1145
dirty_flags|=dirtyflag_voice_mask;
1152
void spu_device::kill_sound()
1161
unsigned short spu_device::read_word(const unsigned int addr)
1163
unsigned short ret=0, *rp=(unsigned short *)(reg+(addr&0x1ff));
1165
assert((addr&1)==0);
1171
#ifdef debug_spu_registers
1172
printf("spu: read word %08x = %04x [%s]\n",
1175
get_register_name(addr));
1185
unsigned char spu_device::read_byte(const unsigned int addr)
1187
unsigned char ret=0,
1188
*rp=reg+(addr&0x1ff);
1192
#ifdef debug_spu_registers
1193
printf("spu: read byte %08x\n",addr);
1203
void spu_device::write_word(const unsigned int addr, const unsigned short data)
1205
#ifdef debug_spu_registers
1206
printf("spu: write %08x = %04x [%s]\n",
1209
get_register_name(addr));
1212
assert((addr&1)==0);
1216
const unsigned int a=addr&0x1ff;
1219
case spureg_trans_addr:
1220
spureg.trans_addr=data;
1225
dirty_flags|=dirtyflag_ram;
1231
unsigned short *rp=(unsigned short *)(reg+(addr&0x1ff));
1233
if ((a==spureg_irq_addr) ||
1234
((a==spureg_ctrl) && ((rp[0]^data)&spuctrl_irq_enable)))
1235
dirty_flags|=dirtyflag_irq;
1242
if ((a>spureg_reverb_config) && (a<=spureg_last))
1243
dirty_flags|=dirtyflag_reverb;
1245
if (a<=spureg_voice_last)
1247
unsigned int v=(a>>4),r=(a&0xf);
1250
voice[v].forceloop=true;
1253
dirty_flags|=(1<<v);
1258
update_voice_state();
1266
void spu_device::write_byte(const unsigned int addr, const unsigned char data)
1268
#ifdef debug_spu_registers
1269
printf("spu: write %08x = %02x\n",addr,data);
1272
const unsigned int a=addr&0x1ff;
1274
if ((a>spureg_reverb_config) && (a<=spureg_last))
1275
dirty_flags|=dirtyflag_reverb;
1283
void spu_device::update_vol(const unsigned int addr)
1287
unsigned int ch=(addr&0xf)>>1;
1290
unsigned int v=addr>>4;
1291
unsigned short newval=*(unsigned short *)(reg+addr);
1296
printf("cur=%04x on=%d",voice[v].vol[ch],(spureg.chon>>ch)&1);
1297
switch ((newval>>13)&3)
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;
1308
voice[v].vol[ch]=((int)newval<<17)>>17;
1318
void spu_device::write_data(const unsigned short data)
1320
#ifdef debug_spu_registers
1321
printf("spu: write data %04x @ %04x\n",data,taddr);
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;
1334
void spu_device::update_key()
1336
dirty_flags|=((spureg.keyon|spureg.keyoff)&dirtyflag_voice_mask);
1340
unsigned int d=spureg.keyoff;
1341
for (int i=0; i<24; i++, d>>=1)
1342
if (d&1) key_off(i);
1347
unsigned int d=spureg.keyon;
1348
for (int i=0; i<24; i++, d>>=1)
1350
spureg.chon|=spureg.keyon;
1353
spureg.keyon=spureg.keyoff=0;
1360
void spu_device::flush_cache(sample_cache *sc,
1361
const unsigned int istart,
1362
const unsigned int iend)
1364
for (unsigned int a=sc->start; a<sc->end; a+=16)
1367
/* log_static(log_spu,"cache_invalidate: %08x->%08x\n",
1371
sc->invalid_start=min(sc->invalid_start,istart);
1372
sc->invalid_end=max(sc->invalid_end,iend);
1381
void spu_device::invalidate_cache(const unsigned int st, const unsigned int en)
1383
for (unsigned int a=st; a<en; a+=16)
1384
if (cache[a>>4]) flush_cache(cache[a>>4],st,en);
1391
spu_device::sample_cache *spu_device::get_sample_cache(const unsigned int addr)
1393
// log_static(log_spu,"get_sample_cache: %08x\n",addr);
1395
assert(addr<spu_ram_size);
1396
sample_cache *sc=cache[addr>>4];
1399
unsigned int loop=0;
1401
sc=new sample_cache;
1406
adpcm_packet *ap=(adpcm_packet *)(spu_ram+sc->start);
1408
for (a=addr; a<(512*1024); a+=16, ap++)
1410
if (cache[a>>4]) flush_cache(cache[a>>4],a,a+16);
1413
if ((ap->flags&adpcmflag_loop_start) && (ap->flags&adpcmflag_loop)) loop=a;
1414
if (ap->flags&adpcmflag_end) break;
1417
if (ap->flags&adpcmflag_loop) sc->is_loop=true;
1419
sc->end=min(spu_ram_size,a+16);
1421
unsigned int sz=((sc->end-sc->start)>>4)*28;
1422
sc->data=new signed short [sz];
1423
sample_cache::cache_size+=sz<<1;
1425
if (loop) sc->loop=sc->data+(((loop-sc->start)>>4)*28);
1427
signed short *dp=sc->data;
1428
ap=(adpcm_packet *)(spu_ram+sc->start);
1430
for (a=sc->start; a<sc->end; a+=16, ap++)
1431
dp=sc->decoder.decode_packet(ap,dp);
1436
/* log_static(log_spu,"cache_add: %08x->%08x\n",
1447
bool spu_device::translate_sample_addr(const unsigned int addr, cache_pointer *cp)
1449
assert((addr&0xf)==0);
1451
if ((cp->cache=get_sample_cache(addr)))
1453
cp->ptr=cp->cache->data+(((addr-cp->cache->start)>>4)*28);
1454
cp->cache->add_ref();
1461
// Get distance in input samples to next IRQ for voice
1464
unsigned int spu_device::get_irq_distance(const voiceinfo *vi)
1466
if (spureg.ctrl&spuctrl_irq_enable)
1468
unsigned int irq_addr=spureg.irq_addr<<3;
1469
signed short *irq_ptr;
1471
if ((irq_ptr=vi->play.cache->get_sample_pointer(irq_addr)))
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
1477
if ((vi->play.ptr<irq_ptr) ||
1478
((! vi->hitirq) && (vi->play.ptr==irq_ptr)))
1480
return irq_ptr-vi->play.ptr;
1485
(irq_ptr=vi->loop.cache->get_sample_pointer(irq_addr)) &&
1486
(irq_ptr>=vi->loop.ptr))
1488
// IRQ address is inside this voices loop cache, return distance
1490
return (vi->play.cache->dend-vi->play.ptr)+
1491
(irq_ptr-vi->loop.ptr);
1495
// IRQs not enabled, or IRQ address not reachable by voice, distance is infinity
1504
void spu_device::update_voice_events(voiceinfo *vi)
1508
// Calculate time until end of sample in output samples
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)
1513
// Voice is inside loop cache, return time until end of that if lower
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);
1520
// Calculate time until next IRQ in output samples
1522
unsigned int irqdist=get_irq_distance(vi);
1523
if (irqdist!=infinity)
1525
// Convert IRQ input sample distance to output samples
1527
vi->samplestoirq=(unsigned int)(((((INT64)irqdist)<<12)-vi->dptr)+(vi->pitch-1))/vi->pitch;
1530
vi->samplestoirq=infinity;
1534
// Voice pitch is 0, distance to sample end and IRQ is infinity
1536
vi->samplestoend=vi->samplestoirq=infinity;
1544
bool spu_device::update_voice_state(const unsigned int v)
1546
voicereg *vr=&spureg.voice[v];
1547
voiceinfo *vi=&voice[v];
1549
// Update sample cache if necessary
1551
if (! vi->play.update(this))
1553
assert(vi->play.ptr<vi->play.cache->dend);
1555
// Get pitch from voice register and apply frequency multiplier if
1556
// there is one in effect
1558
vi->pitch=vr->pitch;
1559
vi->pitch=(unsigned int)(vi->pitch*freq_multiplier);
1561
// Update event times
1563
update_voice_events(vi);
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)
1574
// Check for existing loop cache
1576
sample_loop_cache *ret=lpcache->find_loop_cache(lpen,lpst);
1579
// No loop cache exists for this address pair, create a new one
1581
sample_loop_cache *lc=new sample_loop_cache;
1584
lpcache->add_loop_cache(lc);
1587
// Decode samples from start address using decoder state at end address
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);
1596
#ifdef log_loop_cache
1597
log(log_spu,"spu: add loop cache %08x %08x->%08x (end at %08x)\n",lc,lpen,lpst,adr);
1600
lc->len=dp-lc->data;
1610
void spu_device::update_voice_loop(const unsigned int v)
1612
// voicereg *vr=&spureg.voice[v];
1613
voiceinfo *vi=&voice[v];
1614
unsigned int ra = 0;
1616
// Check for looping using the voices repeat address register and get
1617
// a pointer to the loop position if enabled
1621
// If loop address is not forced get the loop pointer from the cache
1622
// block (if present)
1624
if ((! voice[v].forceloop) &&
1625
(vi->play.cache->get_loop_pointer(&vi->loop)))
1627
ra=vi->play.cache->loopaddr;
1630
// Otherwise use the address set in repaddr (if set)
1632
if ((! vi->loop) && (vi->play.cache->is_loop))
1634
ra=spureg.voice[v].repaddr<<3;
1636
const adpcm_packet *ap=ra?(adpcm_packet *)(spu_ram+ra):NULL;
1640
if (ap->flags&adpcmflag_loop)
1642
// Repeat address points to a block with loop flag set
1644
if (! vi->play.cache->get_sample_pointer(ra,&vi->loop))
1646
// Repeat address is in a different block
1648
translate_sample_addr(ra,&vi->loop);
1654
// Update loop cache
1658
vi->loop_cache=get_loop_cache(vi->play.cache,vi->play.cache->end,vi->loop.cache,ra);
1666
void spu_device::update_voice_state()
1668
// If RAM or irq state is dirty make all voices dirty
1670
if (dirty_flags&(dirtyflag_ram|dirtyflag_irq))
1672
dirty_flags|=dirtyflag_voice_mask;
1673
dirty_flags&=~(dirtyflag_ram|dirtyflag_irq);
1676
// Update state for dirty voices
1678
if (dirty_flags&dirtyflag_voice_mask)
1680
unsigned int voicemask=1;
1681
for (unsigned int i=0; i<24; i++, voicemask<<=1)
1682
if (dirty_flags&voicemask)
1684
update_voice_state(i);
1685
dirty_flags&=~voicemask;
1691
// Process voice state and build output segments
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
1697
// Output: bool true if voice is still playing
1700
bool spu_device::process_voice(const unsigned int v,
1701
const unsigned int sz,
1705
unsigned int *tleft)
1708
unsigned int voice_mask=1<<v,
1711
bool noise=((spureg.noise&voice_mask)!=0),
1712
fm=((spureg.fm&voice_mask)!=0);
1713
voiceinfo *vi=&voice[v];
1715
// Early exit if we don't have a sample cache block
1727
// Play up to end of sample, envelope event, or IRQ, whichever comes first
1729
unsigned int ntoplay=fm?1:num,
1730
nextevent=min(vi->samplestoend,
1731
min(vi->samplestoirq,vi->envsamples));
1732
ntoplay=min(ntoplay,nextevent);
1736
signed short *noisep=NULL;
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;
1747
noisep=(signed short *)fmnoise_ptr;
1751
signed short *dp=(signed short *)ptr,
1752
*outxp=(signed short *)outxptr;
1754
if (outxp) outxp+=off<<1;
1756
generate_voice(v, dp, noisep, outxp, ntoplay);
1761
vi->samplestoend-=ntoplay;
1762
if (vi->samplestoirq!=infinity) vi->samplestoirq-=ntoplay;
1763
if (vi->envsamples!=infinity) vi->envsamples-=ntoplay;
1767
// Determine which event(s) we hit
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);
1774
// Update loop cache pointer if we are playing a loop cache
1776
if ((vi->inloopcache) && (vi->lcptr>=vi->loop_cache->len))
1778
vi->inloopcache=false;
1779
hitend=(vi->play.ptr>=vi->play.cache->dend);
1781
#ifdef log_loop_cache
1782
log(log_spu,"spu: %d leave loop cache %08x, lcptr=%d, hitend=%d\n",
1794
// End of sample reached, calculate how far we overshot
1796
unsigned int poff=vi->play.ptr-vi->play.cache->dend;
1798
// Make sure loop info is up to date and end the current output segment
1800
update_voice_loop(v);
1803
// We are looping, set play address to loop address and account for
1809
vi->inloopcache=(poff<vi->loop_cache->len);
1811
#ifdef log_loop_cache
1812
if (vi->inloopcache)
1813
log(log_spu,"spu: %d enter loop cache %08x, lcptr=%d\n",
1819
// Check for IRQ at/just after repeat address
1821
if (spureg.ctrl&spuctrl_irq_enable)
1823
if (spureg.voice[v].repaddr==spureg.irq_addr)
1826
signed short *irq_ptr;
1827
unsigned int irq_addr=spureg.irq_addr<<3;
1829
if ((irq_ptr=vi->loop.cache->get_sample_pointer(irq_addr)))
1831
if ((irq_ptr>=vi->loop.ptr) &&
1832
(vi->play.ptr>=irq_ptr))
1838
// Not looping, stop voice
1840
spureg.reverb&=~(1<<v);
1843
// Check for IRQ at repeat address
1845
if (spureg.ctrl&spuctrl_irq_enable)
1847
if (spureg.voice[v].repaddr==spureg.irq_addr)
1852
assert((stopped) || (vi->play.ptr<vi->play.cache->dend));
1855
assert(vi->play.ptr<vi->play.cache->dend);
1860
// Went past IRQ address, trigger IRQ
1863
vi->samplestoirq=infinity;
1869
// Envelope event, update the envelope (stop if necessary), and start
1870
// a new output segment
1872
stopped=((stopped) || (! update_envelope(v)));
1877
// Voice is now stopped
1883
// Update voice event times
1885
update_voice_events(vi);
1888
// Set current volume register
1890
spureg.voice[v].curvol=(unsigned short)(vi->env_level*32767.0f);
1892
// Return how much time is left and whether or not the voice is still playing
1899
// Generate voice output samples
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
1906
void spu_device::generate_voice(const unsigned int v,
1910
const unsigned int sz)
1912
voiceinfo *vi=&voice[v];
1913
signed short *fp,*sp;
1916
// Get input pointer
1918
if (vi->inloopcache)
1920
sp=vi->loop_cache->data+vi->lcptr;
1927
unsigned int dptr=vi->dptr;
1929
// Get output pointer (and advance output offset)
1931
signed short *dp=(signed short *)ptr;
1932
signed short *outxp=(signed short *)outxptr;
1934
// Calculate fixed point envelope levels/deltas premultiplied by channel volume
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);
1943
// Update the segments envelope level
1945
vi->env_level+=(n*vi->env_delta);
1949
INT64 dptr=((INT64)n*vi->pitch)+vi->dptr;
1950
unsigned int d=(unsigned int)(dptr>>12);
1951
vi->dptr=(unsigned int)(dptr&0xfff);
1953
if (vi->inloopcache) vi->lcptr+=d;
1955
sp=(signed short *)noiseptr;
1972
l=(l*vi->vol[0])>>15;
1973
r=(r*vi->vol[1])>>15;
1975
dp[0]=clamp(l+dp[0]);
1976
dp[1]=clamp(r+dp[1]);
1990
dp[0]=clamp(l+dp[0]);
1991
dp[1]=clamp(r+dp[1]);
1997
if (1) //settings.sound_interpolate)
1999
unsigned int num_stitch=0;
2002
// Linear interpolation enabled, calculate how many samples we will
2003
// read from input data
2005
INT64 fracend=(((INT64)(n-1))*vi->pitch)+dptr;
2006
unsigned int end=(unsigned int)(fracend>>12);
2008
// Get pointer to last sample of input data
2010
if (vi->inloopcache)
2012
ep=vi->loop_cache->data+vi->loop_cache->len-1;
2015
ep=vi->play.cache->dend-1;
2018
// If we read the last sample "stitching" will be necessary (inerpolation
2019
// from last sample of segment to first sample of next segment)
2021
if (((sp+end)>=ep) && (vi->pitch))
2023
num_stitch=min(n,max(0x1fff/vi->pitch,1));
2035
v+=((sp[1]-v)*(int)dptr)>>12;
2037
int l=(v*env_l)>>15,
2046
l=(l*vi->vol[0])>>15;
2047
r=(r*vi->vol[1])>>15;
2049
dp[0]=clamp(l+dp[0]);
2050
dp[1]=clamp(r+dp[1]);
2064
v+=((sp[1]-v)*(int)dptr)>>12;
2066
int l=(v*env_l)>>15,
2071
dp[0]=clamp(l+dp[0]);
2072
dp[1]=clamp(r+dp[1]);
2083
// Stitch samples, get the first sample of the next segment
2085
signed short *nsp=NULL;
2087
if (vi->inloopcache)
2089
nsp=vi->play.ptr+(vi->loop_cache->len-vi->lcptr);
2090
if (nsp>=vi->play.cache->dend)
2096
update_voice_loop(v);
2097
if (vi->loop) nsp=vi->loop_cache->data;
2100
int ns=nsp?nsp[0]:0;
2102
// Generate stitch samples
2106
while (num_stitch--)
2109
nv=(sp>=ep)?ns:sp[1];
2111
v+=((nv-v)*(int)dptr)>>12;
2113
int l=(v*env_l)>>15,
2122
l=(l*vi->vol[0])>>15;
2123
r=(r*vi->vol[1])>>15;
2125
dp[0]=clamp(l+dp[0]);
2126
dp[1]=clamp(r+dp[1]);
2135
while (num_stitch--)
2138
nv=(sp>=ep)?ns:sp[1];
2140
v+=((nv-v)*(int)dptr)>>12;
2142
int l=(v*env_l)>>15,
2147
dp[0]=clamp(l+dp[0]);
2148
dp[1]=clamp(r+dp[1]);
2159
// Generate samples with no interpolation
2165
int l=(sp[0]*env_l)>>15,
2166
r=(sp[0]*env_r)>>15;
2174
l=(l*vi->vol[0])>>15;
2175
r=(r*vi->vol[1])>>15;
2177
dp[0]=clamp(l+dp[0]);
2178
dp[1]=clamp(r+dp[1]);
2189
int l=(sp[0]*env_l)>>15,
2190
r=(sp[0]*env_r)>>15;
2194
dp[0]=clamp(l+dp[0]);
2195
dp[1]=clamp(r+dp[1]);
2205
// Update segment pointer
2207
vi->play.ptr+=sp-fp;
2209
if (vi->inloopcache)
2210
vi->lcptr=sp-vi->loop_cache->data;
2218
bool spu_device::update_envelope(const int v)
2220
while (voice[v].envsamples==0)
2222
voice[v].env_state++;
2224
switch (voice[v].env_state)
2227
voice[v].env_level=1.0f;
2228
voice[v].env_delta=voice[v].env_dr;
2229
if (voice[v].env_dr!=0.0f)
2231
voice[v].envsamples=(unsigned int)((voice[v].env_sl-1.0f)/voice[v].env_dr);
2234
voice[v].envsamples=infinity;
2239
voice[v].env_level=voice[v].env_sl;
2240
voice[v].env_delta=voice[v].env_sr;
2242
if (voice[v].env_sr>0.0f)
2244
voice[v].envsamples=(unsigned int)((1.0f-voice[v].env_level)/voice[v].env_sr);
2246
if (voice[v].env_sr<0.0f)
2248
voice[v].envsamples=(unsigned int)(voice[v].env_level/-voice[v].env_sr);
2251
voice[v].envsamples=infinity;
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)
2260
voice[v].env_level=0.0f;
2264
voice[v].env_level=1.0f;
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
2273
voice[v].envsamples=infinity;
2277
voice[v].envsamples=(unsigned int)(voice[v].env_level/-voice[v].env_rr);
2281
case 5: // release end
2282
voice[v].env_level=0.0f;
2283
voice[v].env_delta=0.0f;
2284
voice[v].envsamples=infinity;
2295
void spu_device::key_on(const int v)
2297
voice[v].loop.reset();
2299
// printf("key_on: %d @ %x (pitch %x)\n", v, spureg.voice[v].addr<<3, spureg.voice[v].pitch);
2301
translate_sample_addr(spureg.voice[v].addr<<3,&voice[v].play);
2302
assert(voice[v].play.ptr<voice[v].play.cache->dend);
2304
voice[v].keyontime=0; //get_system_time();
2307
voice[v].inloopcache=false;
2309
voice[v].env_level=0.0f;
2310
voice[v].env_state=0;
2311
voice[v].forceloop=false;
2313
// Note: ChronoCross has note hang problems if this is 0 immediately
2315
spureg.voice[v].curvol=1;
2317
for (unsigned int ch=0; ch<2; ch++)
2320
voice[v].sweep_vol[ch]=1.0f;
2324
#ifdef warn_if_sweep_used
2325
static bool sweepused;
2326
if ((spureg.voice[v].vol_l|spureg.voice[v].vol_r)&0x8000)
2336
#ifdef assert_if_sweep_used
2337
assert(((spureg.voice[v].vol_l|spureg.voice[v].vol_r)&0x8000)==0);
2340
if (spureg.voice[v].adsl&adsl_am)
2342
voice[v].env_ar=get_pos_exp_rate((spureg.voice[v].adsl&adsl_ar_mask)>>adsl_ar_shift);
2345
voice[v].env_ar=get_linear_rate((spureg.voice[v].adsl&adsl_ar_mask)>>adsl_ar_shift);
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);
2351
if (spureg.voice[v].srrr&srrr_sm)
2353
if (spureg.voice[v].srrr&srrr_sd)
2355
voice[v].env_sr=get_neg_exp_rate((spureg.voice[v].srrr&srrr_sr_mask)>>srrr_sr_shift);
2358
voice[v].env_sr=get_pos_exp_rate((spureg.voice[v].srrr&srrr_sr_mask)>>srrr_sr_shift);
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;
2367
if (spureg.voice[v].srrr&srrr_rm)
2369
voice[v].env_rr=-get_exp_release_rate(spureg.voice[v].srrr&srrr_rr_mask);
2372
voice[v].env_rr=-get_linear_release_rate(spureg.voice[v].srrr&srrr_rr_mask);
2375
voice[v].envsamples=(unsigned int)(1.0f/voice[v].env_ar);
2376
voice[v].env_delta=voice[v].env_ar;
2383
void spu_device::set_xa_format(const float _freq, const int channels)
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();
2388
xa_freq=(unsigned int)((freq/44100.0)*4096.0f);
2389
xa_channels=channels;
2390
xa_spf=(unsigned int)(freq/60.0)*channels;
2397
void spu_device::generate_xa(void *ptr, const unsigned int sz)
2399
if (xa_buffer->get_bytes_in())
2401
// Don't start playing until 8 frames worth of data are in
2403
if ((! xa_playing) && (xa_buffer->get_bytes_in()<(xa_spf<<3)))
2405
// debug_xa("waiting...\n");
2411
// Init buffer pointers/counters
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();
2419
assert((xa_channels==1) || (xa_channels==2));
2423
int voll=spureg.cdvol_l,
2424
volr=spureg.cdvol_r;
2425
voll=(voll*xa_voll)>>15;
2426
volr=(volr*xa_volr)>>15;
2428
// Generate requested number of XA samples
2430
while ((xa_buffer->get_bytes_in()) && (n--))
2432
// Get left/right input samples
2435
vr=sp[xa_channels-1];
2437
// Linear interpolation
2439
if (1) //settings.sound_interpolate)
2441
signed short *nsp=(signed short *)xa_buffer->get_tail_ptr(noff);
2443
vdr=nsp[xa_channels-1]-vr;
2445
vl+=(vdl*(int)xa_cnt)>>12;
2446
vr+=(vdr*(int)xa_cnt)>>12;
2454
// Write to SPU XA buffer (for emulation purposes - some games read this
2455
// back to do analysers, etc...)
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;
2461
// Mix samples into output buffer
2463
dp[0]=clamp(dp[0]+vl);
2464
dp[1]=clamp(dp[1]+vr);
2467
// Advance input counter/pointer
2470
int ss=(xa_cnt>>12);
2476
ss=min(ss,(int)xa_buffer->get_bytes_in());
2478
xa_buffer->increment_tail(ss);
2479
sp=(signed short *)xa_buffer->get_tail_ptr();
2483
// Delete buffer markers we have passed
2485
xa_buffer->delete_markers(oldtail);
2488
// If we run out of input set status to stopped and clear the SPU XA buffer
2490
if (! xa_buffer->get_bytes_in())
2494
memset(spu_ram,0,0x1000);
2503
void spu_device::generate_cdda(void *ptr, const unsigned int sz)
2505
if (cdda_buffer->get_bytes_in())
2507
unsigned int cdda_spf=(44100*4)/60.0,
2508
freq=(unsigned int)((cdda_freq*60.0)/60.0);
2510
if ((! cdda_playing) && (cdda_buffer->get_bytes_in()<(cdda_spf<<3)))
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();
2520
int voll=spureg.cdvol_l,
2521
volr=spureg.cdvol_r;
2523
while ((cdda_buffer->get_bytes_in()) && (n--))
2525
dp[0]=clamp(dp[0]+((sp[0]*voll)>>15));
2526
dp[1]=clamp(dp[1]+((sp[1]*volr)>>15));
2530
int ss=(cdda_cnt>>12);
2537
cdda_buffer->increment_tail(ss);
2538
sp=(signed short *)cdda_buffer->get_tail_ptr();
2542
cdda_buffer->delete_markers(oldtail);
2544
if (! cdda_buffer->get_bytes_in())
2547
if (n>0) printf("cdda buffer underflow (n=%d cdda_in=%d spf=%d)\n",n,cdda_buffer->get_bytes_in(),cdda_spf);
2555
void spu_device::key_off(const int v)
2557
// printf("key_off: %d\n", v);
2559
if (voice[v].env_state<=3)
2561
voice[v].env_state=3;
2562
voice[v].envsamples=0;
2570
void spu_device::update_reverb()
2572
if (dirty_flags&dirtyflag_reverb)
2574
cur_reverb_preset=find_reverb_preset((unsigned short *)®[0x1c0]);
2576
if (cur_reverb_preset==NULL)
2578
// printf("spu: reverb=unknown (reg 1c0 = %x)\n", reg[0x1c0]);
2581
// printf("spu: reverb=%s\n",cur_reverb_preset->name);
2582
spu_reverb_cfg=&cur_reverb_preset->cfg;
2584
if ((mame_stricmp("reverb off",cur_reverb_preset->name)) && (spu_reverb_cfg->band_gain<=0.0f))
2586
// printf("spu: no reverb config for %s\n",cur_reverb_preset->name);
2590
dirty_flags&=~dirtyflag_reverb;
2598
void spu_device::flush_output_buffer()
2600
output_head=output_tail=output_size=0;
2607
void spu_device::generate(void *ptr, const unsigned int sz)
2609
cur_generate_sample+=sz>>2;
2610
process_until(cur_generate_sample);
2614
unsigned int left=sz;
2615
unsigned char *dp=(unsigned char *)ptr;
2617
while ((left) && (output_size))
2619
unsigned int n=min(min(left,output_buffer_size-output_head),output_size);
2620
memcpy(dp,output_buf[0]+output_head,n);
2622
rev->process((signed short *)dp,
2623
(signed short *)(output_buf[1]+output_head),
2625
(signed short)spureg.rvol_l,
2626
(signed short)spureg.rvol_r,
2631
output_head&=(output_buffer_size-1);
2641
generate_xa(ptr,sz);
2642
generate_cdda(ptr,sz);
2649
void spu_device::update_irq_event()
2651
if (spureg.ctrl&spuctrl_irq_enable)
2653
unsigned int samplestoirq=infinity;
2654
for (int i=0; i<24; i++)
2655
if (voice[i].samplestoirq!=infinity)
2657
if (voice[i].samplestoirq==0)
2661
voice[i].samplestoirq=infinity;
2662
voice[i].hitirq=true;
2665
samplestoirq=min(samplestoirq,voice[i].samplestoirq);
2676
void spu_device::generate_noise(void *ptr, const unsigned int num)
2678
unsigned int np=(unsigned int)(65536.0f/(0x40-((spureg.ctrl&spuctrl_noise_mask)>>spuctrl_noise_shift)));
2681
signed short *dp=(signed short *)ptr;
2683
for (unsigned int i=0; i<num; i++)
2685
signed short v=noise_cur;
2693
shift_register15(noise_seed);
2694
noise_cur=noise_seed<<1;
2703
void spu_device::process_until(const unsigned int tsample)
2705
while (tsample>cur_frame_sample)
2707
unsigned int process_samples=(unsigned int)(tsample-cur_frame_sample);
2709
// Drop samples from the head of the queue if its full
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)
2715
nsz-=output_buffer_size;
2719
output_head&=(output_buffer_size-1);
2722
// Decide how many samples to process taking into account buffer
2723
// wrap in output queue. Get pointers to the queues.
2725
process_samples=min(process_samples,
2726
(output_buffer_size-output_tail)>>2);
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;
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);
2738
// Intialise the output samples to 0 (process_voice always adds samples)
2740
memset(outptr,0,process_samples<<2);
2741
memset(reverbptr,0,process_samples<<2);
2743
// If noise is enabled for any channels generate noise samples
2745
if (spureg.noise&0xffffff)
2746
generate_noise(noiseptr,process_samples);
2748
unsigned int mask=1;
2749
for (int i=0; i<24; i++, mask<<=1)
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;
2757
// This channel is an FM input for the next channel - clear the
2761
memset(fmptr,0,process_samples<<2);
2763
if (spureg.chon&mask)
2767
if (! process_voice(i,
2769
isreverb?reverbptr:outptr,
2776
//spureg.reverb&=~mask;
2778
voice[i].play.reset();
2779
voice[i].loop.reset();
2783
spureg.voice[i].curvol=0;
2787
cur_frame_sample+=process_samples;
2795
void spu_device::update_timing()
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();
2806
void spu_device::sound_stream_update(sound_stream &stream, stream_sample_t **inputs, stream_sample_t **outputs, int samples)
2808
stream_sample_t *outL, *outR;
2809
INT16 temp[44100], *src;
2814
generate(temp, samples*4); // second parameter is bytes, * 2 (size of INT16) * 2 (stereo)
2817
for (int i = 0; i < samples; i++)
2828
void spu_device::start_dma(UINT8 *mainram, bool to_spu, UINT32 size)
2830
UINT32 st=spureg.trans_addr<<3, en=st+size;
2840
invalidate_cache(st,en);
2842
memcpy(spu_ram+(spureg.trans_addr<<3), mainram, size);
2844
dirty_flags|=dirtyflag_ram;
2848
memcpy(mainram, spu_ram+(spureg.trans_addr<<3), size);
2856
void spu_device::decode_xa_mono(const unsigned char *xa,
2859
signed short *dp=(signed short *)ptr;
2864
for (int b=0; b<18; b++)
2866
for (int s=0; s<4; s++)
2868
unsigned char flags=xa[4+(s<<1)],
2871
int f0=filter_coef[filter][0],
2872
f1=filter_coef[filter][1];
2875
for (i=0; i<28; i++)
2877
short d=(xa[16+(i<<2)+s]&0xf)<<12;
2878
d=clamp((d>>shift)+(((l0*f0)+(l1*f1)+32)>>6));
2887
f0=filter_coef[filter][0];
2888
f1=filter_coef[filter][1];
2890
for (i=0; i<28; i++)
2892
short d=(xa[16+(i<<2)+s]>>4)<<12;
2893
d=clamp((d>>shift)+(((l0*f0)+(l1*f1)+32)>>6));
2911
void spu_device::decode_xa_stereo(const unsigned char *xa,
2914
signed short *dp=(signed short *)ptr;
2921
for (int b=0; b<18; b++)
2923
for (int s=0; s<4; s++)
2925
unsigned char flags0=xa[4+(s<<1)],
2928
flags1=xa[5+(s<<1)],
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];
2937
for (int i=0; i<28; i++)
2939
short d=xa[16+(i<<2)+s],
2942
d0=clamp((int)(d0>>shift0)+(((l0*f0)+(l1*f1)+32)>>6));
2947
d1=clamp((int)(d1>>shift1)+(((l2*f2)+(l3*f3)+32)>>6));
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)
2979
bool spu_device::play_xa(const unsigned int sector, const unsigned char *xa)
2981
// Don't process the sector if the buffer is full
2983
if (xa_buffer->get_bytes_free()<xa_sector_size) return false;
2985
// debug_xa("play_xa: %d\n",sector);
2987
// Get XA format from sector header
2989
const unsigned char *hdr=xa+4;
2993
switch (hdr[3]&0x3f) // ignore emphasis and reserved bits
2997
freq=37800.0f; //18900.0f;
3007
freq=18900.0f; ///2.0f;
3012
freq=18900.0f; //37800.0f/2.0f;
3016
printf("play_xa: unhandled xa mode %08x\n",hdr[3]);
3020
set_xa_format(freq,channels);
3024
unsigned char *ptr=xa_buffer->add_sector(sector);
3026
// Decode the sector
3030
decode_xa_stereo(xa+8,ptr);
3033
decode_xa_mono(xa+8,ptr);
3036
// Return that we processed the sector
3042
// Flush everything after a given sector in the XA buffer
3045
void spu_device::flush_xa(const unsigned int sector)
3047
// debug_xa("flush_xa: %d\n",sector);
3051
xa_buffer->flush(sector);
3054
// Not playing, flush the entire buffer
3056
xa_buffer->flush_all();
3065
bool spu_device::play_cdda(const unsigned int sector, const unsigned char *cdda)
3067
if (cdda_buffer->get_bytes_free()<cdda_sector_size) return false;
3069
signed short *dp=(signed short *)cdda_buffer->add_sector(sector);
3070
memcpy(dp,cdda,cdda_sector_size);
3075
void spu_device::flush_cdda(const unsigned int sector)
3077
// debug_xa("flush_cdda: %d\n",sector);
3081
cdda_buffer->flush(sector);
3084
cdda_buffer->flush_all();
3089
// MAME I/O stuff. This can get cleaner when machine/psx.c does.
3091
void spu_device::dma_read( UINT32 n_address, INT32 n_size )
3093
UINT8 *psxram = (UINT8 *)memory_get_shared(machine(), "share1");
3095
start_dma(psxram + n_address, false, n_size*4);
3098
void spu_device::dma_write( UINT32 n_address, INT32 n_size )
3100
UINT8 *psxram = (UINT8 *)memory_get_shared(machine(), "share1");
3102
// printf("SPU DMA write from %x, size %x\n", n_address, n_size);
3104
start_dma(psxram + n_address, true, n_size*4);
3107
READ16_HANDLER( spu_r )
3109
spu_device *spu = space->machine().device<spu_device>("spu");
3116
return spu->read_word(offset*2);
3119
WRITE16_HANDLER( spu_w )
3121
spu_device *spu = space->machine().device<spu_device>("spu");
3128
spu->write_word(offset*2, data);