| 1 |
/* |
| 2 |
** |
| 3 |
** File: ymdeltat.c |
| 4 |
** |
| 5 |
** YAMAHA DELTA-T adpcm sound emulation subroutine |
| 6 |
** used by fmopl.c (Y8950) and fm.c (YM2608 and YM2610/B) |
| 7 |
** |
| 8 |
** Base program is YM2610 emulator by Hiromitsu Shioya. |
| 9 |
** Written by Tatsuyuki Satoh |
| 10 |
** Improvements by Jarek Burczynski (bujar at mame dot net) |
| 11 |
** |
| 12 |
** |
| 13 |
** History: |
| 14 |
** |
| 15 |
** 03-08-2003 Jarek Burczynski: |
| 16 |
** - fixed BRDY flag implementation. |
| 17 |
** |
| 18 |
** 24-07-2003 Jarek Burczynski, Frits Hilderink: |
| 19 |
** - fixed delault value for control2 in YM_DELTAT_ADPCM_Reset |
| 20 |
** |
| 21 |
** 22-07-2003 Jarek Burczynski, Frits Hilderink: |
| 22 |
** - fixed external memory support |
| 23 |
** |
| 24 |
** 15-06-2003 Jarek Burczynski: |
| 25 |
** - implemented CPU -> AUDIO ADPCM synthesis (via writes to the ADPCM data reg $08) |
| 26 |
** - implemented support for the Limit address register |
| 27 |
** - supported two bits from the control register 2 ($01): RAM TYPE (x1 bit/x8 bit), ROM/RAM |
| 28 |
** - implemented external memory access (read/write) via the ADPCM data reg reads/writes |
| 29 |
** Thanks go to Frits Hilderink for the example code. |
| 30 |
** |
| 31 |
** 14-06-2003 Jarek Burczynski: |
| 32 |
** - various fixes to enable proper support for status register flags: BSRDY, PCM BSY, ZERO |
| 33 |
** - modified EOS handling |
| 34 |
** |
| 35 |
** 05-04-2003 Jarek Burczynski: |
| 36 |
** - implemented partial support for external/processor memory on sample replay |
| 37 |
** |
| 38 |
** 01-12-2002 Jarek Burczynski: |
| 39 |
** - fixed first missing sound in gigandes thanks to previous fix (interpolator) by ElSemi |
| 40 |
** - renamed/removed some YM_DELTAT struct fields |
| 41 |
** |
| 42 |
** 28-12-2001 Acho A. Tang |
| 43 |
** - added EOS status report on ADPCM playback. |
| 44 |
** |
| 45 |
** 05-08-2001 Jarek Burczynski: |
| 46 |
** - now_step is initialized with 0 at the start of play. |
| 47 |
** |
| 48 |
** 12-06-2001 Jarek Burczynski: |
| 49 |
** - corrected end of sample bug in YM_DELTAT_ADPCM_CALC. |
| 50 |
** Checked on real YM2610 chip - address register is 24 bits wide. |
| 51 |
** Thanks go to Stefan Jokisch ([email protected]) for tracking down the problem. |
| 52 |
** |
| 53 |
** TO DO: |
| 54 |
** Check size of the address register on the other chips.... |
| 55 |
** |
| 56 |
** Version 0.72 |
| 57 |
** |
| 58 |
** sound chips that have this unit: |
| 59 |
** YM2608 OPNA |
| 60 |
** YM2610/B OPNB |
| 61 |
** Y8950 MSX AUDIO |
| 62 |
** |
| 63 |
*/ |
| 64 |
|
| 65 |
#include "ymdeltat.h" |
| 66 |
|
| 67 |
#define YM_DELTAT_DELTA_MAX (24576) |
| 68 |
#define YM_DELTAT_DELTA_MIN (127) |
| 69 |
#define YM_DELTAT_DELTA_DEF (127) |
| 70 |
|
| 71 |
#define YM_DELTAT_DECODE_RANGE 32768 |
| 72 |
#define YM_DELTAT_DECODE_MIN (-(YM_DELTAT_DECODE_RANGE)) |
| 73 |
#define YM_DELTAT_DECODE_MAX ((YM_DELTAT_DECODE_RANGE)-1) |
| 74 |
|
| 75 |
|
| 76 |
/* Forecast to next Forecast (rate = *8) */ |
| 77 |
/* 1/8 , 3/8 , 5/8 , 7/8 , 9/8 , 11/8 , 13/8 , 15/8 */ |
| 78 |
static const INT32 ym_deltat_decode_tableB1[16] = { |
| 79 |
1, 3, 5, 7, 9, 11, 13, 15, |
| 80 |
-1, -3, -5, -7, -9, -11, -13, -15, |
| 81 |
}; |
| 82 |
/* delta to next delta (rate= *64) */ |
| 83 |
/* 0.9 , 0.9 , 0.9 , 0.9 , 1.2 , 1.6 , 2.0 , 2.4 */ |
| 84 |
static const INT32 ym_deltat_decode_tableB2[16] = { |
| 85 |
57, 57, 57, 57, 77, 102, 128, 153, |
| 86 |
57, 57, 57, 57, 77, 102, 128, 153 |
| 87 |
}; |
| 88 |
|
| 89 |
#if 0 |
| 90 |
void YM_DELTAT_BRDY_callback(YM_DELTAT *DELTAT) |
| 91 |
{ |
| 92 |
logerror("BRDY_callback reached (flag set) !\n"); |
| 93 |
|
| 94 |
/* set BRDY bit in status register */ |
| 95 |
if(DELTAT->status_set_handler != NULL && DELTAT->status_change_BRDY_bit) |
| 96 |
(DELTAT->status_set_handler)(DELTAT->status_change_which_chip, DELTAT->status_change_BRDY_bit); |
| 97 |
} |
| 98 |
#endif |
| 99 |
|
| 100 |
UINT8 YM_DELTAT_ADPCM_Read(YM_DELTAT *DELTAT) |
| 101 |
{ |
| 102 |
UINT8 v = 0; |
| 103 |
|
| 104 |
/* external memory read */ |
| 105 |
if ( (DELTAT->portstate & 0xe0)==0x20 ) |
| 106 |
{ |
| 107 |
/* two dummy reads */ |
| 108 |
if (DELTAT->memread) |
| 109 |
{ |
| 110 |
DELTAT->now_addr = DELTAT->start << 1; |
| 111 |
DELTAT->memread--; |
| 112 |
return 0; |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
if ( DELTAT->now_addr != (DELTAT->end<<1) ) |
| 117 |
{ |
| 118 |
v = DELTAT->memory[(DELTAT->now_addr>>1)&DELTAT->memory_mask]; |
| 119 |
|
| 120 |
/*logerror("YM Delta-T memory read $%08x, v=$%02x\n", DELTAT->now_addr >> 1, v);*/ |
| 121 |
|
| 122 |
DELTAT->now_addr+=2; /* two nibbles at a time */ |
| 123 |
|
| 124 |
/* reset BRDY bit in status register, which means we are reading the memory now */ |
| 125 |
if (DELTAT->status_reset_handler && DELTAT->status_change_BRDY_bit) |
| 126 |
(DELTAT->status_reset_handler)(DELTAT->status_change_which_chip, DELTAT->status_change_BRDY_bit); |
| 127 |
|
| 128 |
/* setup a timer that will callback us in 10 master clock cycles for Y8950 |
| 129 |
* in the callback set the BRDY flag to 1 , which means we have another data ready. |
| 130 |
* For now, we don't really do this; we simply reset and set the flag in zero time, so that the IRQ will work. |
| 131 |
*/ |
| 132 |
/* set BRDY bit in status register */ |
| 133 |
if (DELTAT->status_set_handler != NULL && DELTAT->status_change_BRDY_bit) |
| 134 |
(DELTAT->status_set_handler)(DELTAT->status_change_which_chip, DELTAT->status_change_BRDY_bit); |
| 135 |
} |
| 136 |
else |
| 137 |
{ |
| 138 |
/* set EOS bit in status register */ |
| 139 |
if (DELTAT->status_set_handler != NULL && DELTAT->status_change_EOS_bit) |
| 140 |
(DELTAT->status_set_handler)(DELTAT->status_change_which_chip, DELTAT->status_change_EOS_bit); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
return v; |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
/* 0-DRAM x1, 1-ROM, 2-DRAM x8, 3-ROM (3 is bad setting - not allowed by the manual) */ |
| 149 |
static const UINT8 dram_rightshift[4]={3,0,0,0}; |
| 150 |
|
| 151 |
/* DELTA-T ADPCM write register */ |
| 152 |
void YM_DELTAT_ADPCM_Write(YM_DELTAT *DELTAT,int r,int v) |
| 153 |
{ |
| 154 |
if(r>=0x10) return; |
| 155 |
DELTAT->reg[r] = (UINT8)v; /* stock data */ |
| 156 |
|
| 157 |
switch( r ) |
| 158 |
{ |
| 159 |
case 0x00: |
| 160 |
/* |
| 161 |
START: |
| 162 |
Accessing *external* memory is started when START bit (D7) is set to "1", so |
| 163 |
you must set all conditions needed for recording/playback before starting. |
| 164 |
If you access *CPU-managed* memory, recording/playback starts after |
| 165 |
read/write of ADPCM data register $08. |
| 166 |
|
| 167 |
REC: |
| 168 |
0 = ADPCM synthesis (playback) |
| 169 |
1 = ADPCM analysis (record) |
| 170 |
|
| 171 |
MEMDATA: |
| 172 |
0 = processor (*CPU-managed*) memory (means: using register $08) |
| 173 |
1 = external memory (using start/end/limit registers to access memory: RAM or ROM) |
| 174 |
|
| 175 |
|
| 176 |
SPOFF: |
| 177 |
controls output pin that should disable the speaker while ADPCM analysis |
| 178 |
|
| 179 |
RESET and REPEAT only work with external memory. |
| 180 |
|
| 181 |
|
| 182 |
some examples: |
| 183 |
value: START, REC, MEMDAT, REPEAT, SPOFF, x,x,RESET meaning: |
| 184 |
C8 1 1 0 0 1 0 0 0 Analysis (recording) from AUDIO to CPU (to reg $08), sample rate in PRESCALER register |
| 185 |
E8 1 1 1 0 1 0 0 0 Analysis (recording) from AUDIO to EXT.MEMORY, sample rate in PRESCALER register |
| 186 |
80 1 0 0 0 0 0 0 0 Synthesis (playing) from CPU (from reg $08) to AUDIO,sample rate in DELTA-N register |
| 187 |
a0 1 0 1 0 0 0 0 0 Synthesis (playing) from EXT.MEMORY to AUDIO, sample rate in DELTA-N register |
| 188 |
|
| 189 |
60 0 1 1 0 0 0 0 0 External memory write via ADPCM data register $08 |
| 190 |
20 0 0 1 0 0 0 0 0 External memory read via ADPCM data register $08 |
| 191 |
|
| 192 |
*/ |
| 193 |
/* handle emulation mode */ |
| 194 |
if(DELTAT->emulation_mode == YM_DELTAT_EMULATION_MODE_YM2610) |
| 195 |
{ |
| 196 |
v |= 0x20; /* YM2610 always uses external memory and doesn't even have memory flag bit. */ |
| 197 |
} |
| 198 |
|
| 199 |
DELTAT->portstate = v & (0x80|0x40|0x20|0x10|0x01); /* start, rec, memory mode, repeat flag copy, reset(bit0) */ |
| 200 |
|
| 201 |
if( DELTAT->portstate&0x80 )/* START,REC,MEMDATA,REPEAT,SPOFF,--,--,RESET */ |
| 202 |
{ |
| 203 |
/* set PCM BUSY bit */ |
| 204 |
DELTAT->PCM_BSY = 1; |
| 205 |
|
| 206 |
/* start ADPCM */ |
| 207 |
DELTAT->now_step = 0; |
| 208 |
DELTAT->acc = 0; |
| 209 |
DELTAT->prev_acc = 0; |
| 210 |
DELTAT->adpcml = 0; |
| 211 |
DELTAT->adpcmd = YM_DELTAT_DELTA_DEF; |
| 212 |
DELTAT->now_data = 0; |
| 213 |
/*if (DELTAT->start > DELTAT->end) |
| 214 |
//logerror("DeltaT-Warning: Start: %06X, End: %06X\n", DELTAT->start, DELTAT->end); |
| 215 |
logerror("DeltaT-Warning: Start: %06X, End: %06X, Limit %06X, MemMask %06X\n", DELTAT->start, DELTAT->end, DELTAT->limit, DELTAT->memory_mask);*/ |
| 216 |
} |
| 217 |
|
| 218 |
if( DELTAT->portstate&0x20 ) /* do we access external memory? */ |
| 219 |
{ |
| 220 |
DELTAT->now_addr = DELTAT->start << 1; |
| 221 |
DELTAT->memread = 2; /* two dummy reads needed before accesing external memory via register $08*/ |
| 222 |
|
| 223 |
/* if yes, then let's check if ADPCM memory is mapped and big enough */ |
| 224 |
if(DELTAT->memory == NULL) |
| 225 |
{ |
| 226 |
//logerror("YM Delta-T ADPCM rom not mapped\n"); |
| 227 |
DELTAT->portstate = 0x00; |
| 228 |
DELTAT->PCM_BSY = 0; |
| 229 |
} |
| 230 |
else |
| 231 |
{ |
| 232 |
if( (DELTAT->end & DELTAT->memory_mask) >= DELTAT->memory_size ) /* Check End in Range */ |
| 233 |
{ |
| 234 |
//logerror("YM Delta-T ADPCM end out of range: $%08x\n", DELTAT->end); |
| 235 |
//logerror("YM Delta-T ADPCM end out of range: %06X >= %06X\n", DELTAT->end, DELTAT->memory_size); |
| 236 |
DELTAT->end = (DELTAT->end & ~DELTAT->memory_mask) | (DELTAT->memory_size - 1); |
| 237 |
} |
| 238 |
if( (DELTAT->start & DELTAT->memory_mask) >= DELTAT->memory_size ) /* Check Start in Range */ |
| 239 |
{ |
| 240 |
//logerror("YM Delta-T ADPCM start out of range: $%08x\n", DELTAT->start); |
| 241 |
DELTAT->portstate = 0x00; |
| 242 |
DELTAT->PCM_BSY = 0; |
| 243 |
} |
| 244 |
} |
| 245 |
} |
| 246 |
else /* we access CPU memory (ADPCM data register $08) so we only reset now_addr here */ |
| 247 |
{ |
| 248 |
DELTAT->now_addr = 0; |
| 249 |
} |
| 250 |
|
| 251 |
if( DELTAT->portstate&0x01 ) |
| 252 |
{ |
| 253 |
DELTAT->portstate = 0x00; |
| 254 |
|
| 255 |
/* clear PCM BUSY bit (in status register) */ |
| 256 |
DELTAT->PCM_BSY = 0; |
| 257 |
|
| 258 |
/* set BRDY flag */ |
| 259 |
if (DELTAT->status_set_handler != NULL && DELTAT->status_change_BRDY_bit) |
| 260 |
(DELTAT->status_set_handler)(DELTAT->status_change_which_chip, DELTAT->status_change_BRDY_bit); |
| 261 |
} |
| 262 |
break; |
| 263 |
case 0x01: /* L,R,-,-,SAMPLE,DA/AD,RAMTYPE,ROM */ |
| 264 |
/* handle emulation mode */ |
| 265 |
if(DELTAT->emulation_mode == YM_DELTAT_EMULATION_MODE_YM2610) |
| 266 |
{ |
| 267 |
/* YM2610 always uses ROM as an external memory and doesn't have ROM/RAM memory flag bit. */ |
| 268 |
v = (v & ~3) | (DELTAT->control2 & 3); |
| 269 |
} |
| 270 |
|
| 271 |
DELTAT->pan = &DELTAT->output_pointer[(v>>6)&0x03]; |
| 272 |
if ((DELTAT->control2 & 3) != (v & 3)) |
| 273 |
{ |
| 274 |
/*0-DRAM x1, 1-ROM, 2-DRAM x8, 3-ROM (3 is bad setting - not allowed by the manual) */ |
| 275 |
UINT8 new_shift = DELTAT->portshift_base - dram_rightshift[v&3]; |
| 276 |
|
| 277 |
if (DELTAT->now_portshift != new_shift) |
| 278 |
{ |
| 279 |
DELTAT->now_portshift = new_shift; |
| 280 |
|
| 281 |
/* final shift value depends on chip type and memory type selected: |
| 282 |
8 for YM2610 (ROM only), |
| 283 |
5 for ROM for Y8950 and YM2608, |
| 284 |
5 for x8bit DRAMs for Y8950 and YM2608, |
| 285 |
2 for x1bit DRAMs for Y8950 and YM2608. |
| 286 |
*/ |
| 287 |
|
| 288 |
/* refresh addresses */ |
| 289 |
DELTAT->address_mask = (0x10000 << (DELTAT->now_portshift + 1)) - 1; |
| 290 |
DELTAT->start = (DELTAT->reg[0x3]*0x0100 | DELTAT->reg[0x2]) << DELTAT->now_portshift; |
| 291 |
DELTAT->end = (DELTAT->reg[0x5]*0x0100 | DELTAT->reg[0x4]) << DELTAT->now_portshift; |
| 292 |
DELTAT->end |= (1 << DELTAT->now_portshift ) - 1; |
| 293 |
DELTAT->limit = (DELTAT->reg[0xd]*0x0100 | DELTAT->reg[0xc]) << DELTAT->now_portshift; |
| 294 |
} |
| 295 |
} |
| 296 |
DELTAT->control2 = (UINT8)v; |
| 297 |
break; |
| 298 |
case 0x02: /* Start Address L */ |
| 299 |
case 0x03: /* Start Address H */ |
| 300 |
DELTAT->start = (DELTAT->reg[0x3]*0x0100 | DELTAT->reg[0x2]) << DELTAT->now_portshift; |
| 301 |
/*logerror("DELTAT start: 02=%2x 03=%2x addr=%8x\n",DELTAT->reg[0x2], DELTAT->reg[0x3],DELTAT->start );*/ |
| 302 |
break; |
| 303 |
case 0x04: /* Stop Address L */ |
| 304 |
case 0x05: /* Stop Address H */ |
| 305 |
DELTAT->end = (DELTAT->reg[0x5]*0x0100 | DELTAT->reg[0x4]) << DELTAT->now_portshift; |
| 306 |
DELTAT->end |= (1 << DELTAT->now_portshift ) - 1; |
| 307 |
/*logerror("DELTAT end : 04=%2x 05=%2x addr=%8x\n",DELTAT->reg[0x4], DELTAT->reg[0x5],DELTAT->end );*/ |
| 308 |
break; |
| 309 |
case 0x06: /* Prescale L (ADPCM and Record frq) */ |
| 310 |
case 0x07: /* Prescale H */ |
| 311 |
break; |
| 312 |
case 0x08: /* ADPCM data */ |
| 313 |
|
| 314 |
/* |
| 315 |
some examples: |
| 316 |
value: START, REC, MEMDAT, REPEAT, SPOFF, x,x,RESET meaning: |
| 317 |
C8 1 1 0 0 1 0 0 0 Analysis (recording) from AUDIO to CPU (to reg $08), sample rate in PRESCALER register |
| 318 |
E8 1 1 1 0 1 0 0 0 Analysis (recording) from AUDIO to EXT.MEMORY, sample rate in PRESCALER register |
| 319 |
80 1 0 0 0 0 0 0 0 Synthesis (playing) from CPU (from reg $08) to AUDIO,sample rate in DELTA-N register |
| 320 |
a0 1 0 1 0 0 0 0 0 Synthesis (playing) from EXT.MEMORY to AUDIO, sample rate in DELTA-N register |
| 321 |
|
| 322 |
60 0 1 1 0 0 0 0 0 External memory write via ADPCM data register $08 |
| 323 |
20 0 0 1 0 0 0 0 0 External memory read via ADPCM data register $08 |
| 324 |
|
| 325 |
*/ |
| 326 |
|
| 327 |
/* external memory write */ |
| 328 |
if ( (DELTAT->portstate & 0xe0)==0x60 ) |
| 329 |
{ |
| 330 |
if (DELTAT->memread) |
| 331 |
{ |
| 332 |
DELTAT->now_addr = DELTAT->start << 1; |
| 333 |
DELTAT->memread = 0; |
| 334 |
} |
| 335 |
|
| 336 |
/*logerror("YM Delta-T memory write $%08x, v=$%02x\n", DELTAT->now_addr >> 1, v);*/ |
| 337 |
|
| 338 |
if ( DELTAT->now_addr != (DELTAT->end<<1) ) |
| 339 |
{ |
| 340 |
DELTAT->memory[(DELTAT->now_addr>>1)&DELTAT->memory_mask] = (UINT8)v; |
| 341 |
DELTAT->now_addr+=2; /* two nibbles at a time */ |
| 342 |
|
| 343 |
/* reset BRDY bit in status register, which means we are processing the write */ |
| 344 |
if (DELTAT->status_reset_handler && DELTAT->status_change_BRDY_bit) |
| 345 |
(DELTAT->status_reset_handler)(DELTAT->status_change_which_chip, DELTAT->status_change_BRDY_bit); |
| 346 |
|
| 347 |
/* setup a timer that will callback us in 10 master clock cycles for Y8950 |
| 348 |
* in the callback set the BRDY flag to 1 , which means we have written the data. |
| 349 |
* For now, we don't really do this; we simply reset and set the flag in zero time, so that the IRQ will work. |
| 350 |
*/ |
| 351 |
/* set BRDY bit in status register */ |
| 352 |
if (DELTAT->status_set_handler != NULL && DELTAT->status_change_BRDY_bit) |
| 353 |
(DELTAT->status_set_handler)(DELTAT->status_change_which_chip, DELTAT->status_change_BRDY_bit); |
| 354 |
|
| 355 |
} |
| 356 |
else |
| 357 |
{ |
| 358 |
/* set EOS bit in status register */ |
| 359 |
if (DELTAT->status_set_handler != NULL && DELTAT->status_change_EOS_bit) |
| 360 |
(DELTAT->status_set_handler)(DELTAT->status_change_which_chip, DELTAT->status_change_EOS_bit); |
| 361 |
} |
| 362 |
|
| 363 |
return; |
| 364 |
} |
| 365 |
|
| 366 |
/* ADPCM synthesis from CPU */ |
| 367 |
if ( (DELTAT->portstate & 0xe0)==0x80 ) |
| 368 |
{ |
| 369 |
DELTAT->CPU_data = (UINT8)v; |
| 370 |
|
| 371 |
/* Reset BRDY bit in status register, which means we are full of data */ |
| 372 |
if (DELTAT->status_reset_handler && DELTAT->status_change_BRDY_bit) |
| 373 |
(DELTAT->status_reset_handler)(DELTAT->status_change_which_chip, DELTAT->status_change_BRDY_bit); |
| 374 |
return; |
| 375 |
} |
| 376 |
|
| 377 |
break; |
| 378 |
case 0x09: /* DELTA-N L (ADPCM Playback Prescaler) */ |
| 379 |
case 0x0a: /* DELTA-N H */ |
| 380 |
DELTAT->delta = (DELTAT->reg[0xa]*0x0100 | DELTAT->reg[0x9]); |
| 381 |
DELTAT->step = (UINT32)( (double)(DELTAT->delta /* *(1<<(YM_DELTAT_SHIFT-16)) */ ) * (DELTAT->freqbase) ); |
| 382 |
/*logerror("DELTAT deltan:09=%2x 0a=%2x\n",DELTAT->reg[0x9], DELTAT->reg[0xa]);*/ |
| 383 |
break; |
| 384 |
case 0x0b: /* Output level control (volume, linear) */ |
| 385 |
{ |
| 386 |
INT32 oldvol = DELTAT->volume; |
| 387 |
DELTAT->volume = (v&0xff) * (DELTAT->output_range/256) / YM_DELTAT_DECODE_RANGE; |
| 388 |
/* v * ((1<<16)>>8) >> 15; |
| 389 |
* thus: v * (1<<8) >> 15; |
| 390 |
* thus: output_range must be (1 << (15+8)) at least |
| 391 |
* v * ((1<<23)>>8) >> 15; |
| 392 |
* v * (1<<15) >> 15; |
| 393 |
*/ |
| 394 |
/*logerror("DELTAT vol = %2x\n",v&0xff);*/ |
| 395 |
if (oldvol != 0) |
| 396 |
{ |
| 397 |
DELTAT->adpcml = (int)((double)DELTAT->adpcml / (double)oldvol * (double)DELTAT->volume); |
| 398 |
} |
| 399 |
} |
| 400 |
break; |
| 401 |
case 0x0c: /* Limit Address L */ |
| 402 |
case 0x0d: /* Limit Address H */ |
| 403 |
{ |
| 404 |
//UINT32 oldLimit = DELTAT->limit; |
| 405 |
DELTAT->limit = (DELTAT->reg[0xd]*0x0100 | DELTAT->reg[0xc]) << DELTAT->now_portshift; |
| 406 |
/*logerror("DELTAT limit: 0c=%2x 0d=%2x addr=%8x\n",DELTAT->reg[0xc], DELTAT->reg[0xd],DELTAT->limit );*/ |
| 407 |
/*if (oldLimit != DELTAT->limit) |
| 408 |
logerror("DELTAT limit: %02x=%02x addr=%06x\n",r, DELTAT->reg[r],DELTAT->limit );*/ |
| 409 |
} |
| 410 |
break; |
| 411 |
} |
| 412 |
} |
| 413 |
|
| 414 |
void YM_DELTAT_ADPCM_Init(YM_DELTAT *DELTAT,int emulation_mode,int portshift,INT32* output_ptr,int output_range) |
| 415 |
{ |
| 416 |
DELTAT->emulation_mode = (UINT8)emulation_mode; |
| 417 |
if (DELTAT->emulation_mode == YM_DELTAT_EMULATION_MODE_YM2610) |
| 418 |
{ |
| 419 |
DELTAT->portstate = 0x20; |
| 420 |
DELTAT->control2 = 0x01; |
| 421 |
} |
| 422 |
else |
| 423 |
{ |
| 424 |
DELTAT->portstate = 0x00; |
| 425 |
DELTAT->control2 = 0x00; /* default setting depends on the emulation mode. MSX demo called "facdemo_4" doesn't setup control2 register at all and still works */ |
| 426 |
} |
| 427 |
DELTAT->portshift_base = (UINT8)portshift; |
| 428 |
DELTAT->output_pointer = output_ptr; |
| 429 |
DELTAT->output_range = output_range; |
| 430 |
DELTAT->now_portshift = DELTAT->portshift_base - dram_rightshift[DELTAT->control2 & 3]; |
| 431 |
DELTAT->address_mask = (0x10000 << (DELTAT->now_portshift + 1)) - 1; |
| 432 |
} |
| 433 |
|
| 434 |
void YM_DELTAT_ADPCM_Reset(YM_DELTAT *DELTAT,int pan) |
| 435 |
{ |
| 436 |
DELTAT->now_addr = 0; |
| 437 |
DELTAT->now_step = 0; |
| 438 |
DELTAT->step = 0; |
| 439 |
DELTAT->start = 0; |
| 440 |
DELTAT->end = 0; |
| 441 |
DELTAT->limit = (UINT32)~0; /* this way YM2610 and Y8950 (both of which don't have limit address reg) will still work */ |
| 442 |
DELTAT->volume = 0; |
| 443 |
DELTAT->pan = &DELTAT->output_pointer[pan]; |
| 444 |
DELTAT->acc = 0; |
| 445 |
DELTAT->prev_acc = 0; |
| 446 |
DELTAT->adpcmd = 127; |
| 447 |
DELTAT->adpcml = 0; |
| 448 |
if (DELTAT->emulation_mode == YM_DELTAT_EMULATION_MODE_YM2610) |
| 449 |
{ |
| 450 |
DELTAT->portstate &= 0x20; |
| 451 |
DELTAT->control2 &= 0x03; |
| 452 |
} |
| 453 |
else |
| 454 |
{ |
| 455 |
DELTAT->portstate = 0x00; |
| 456 |
DELTAT->control2 = 0x00; |
| 457 |
} |
| 458 |
DELTAT->now_portshift = DELTAT->portshift_base - dram_rightshift[DELTAT->control2 & 3]; |
| 459 |
DELTAT->address_mask = (0x10000 << (DELTAT->now_portshift + 1)) - 1; |
| 460 |
|
| 461 |
/* The flag mask register disables the BRDY after the reset, however |
| 462 |
** as soon as the mask is enabled the flag needs to be set. */ |
| 463 |
|
| 464 |
/* set BRDY bit in status register */ |
| 465 |
if (DELTAT->status_set_handler != NULL && DELTAT->status_change_BRDY_bit) |
| 466 |
(DELTAT->status_set_handler)(DELTAT->status_change_which_chip, DELTAT->status_change_BRDY_bit); |
| 467 |
} |
| 468 |
|
| 469 |
|
| 470 |
#define YM_DELTAT_Limit(val,max,min) \ |
| 471 |
{ \ |
| 472 |
if ( val > max ) val = max; \ |
| 473 |
else if ( val < min ) val = min; \ |
| 474 |
} |
| 475 |
|
| 476 |
INLINE void YM_DELTAT_synthesis_from_external_memory(YM_DELTAT *DELTAT) |
| 477 |
{ |
| 478 |
UINT32 step; |
| 479 |
int data; |
| 480 |
|
| 481 |
DELTAT->now_step += DELTAT->step; |
| 482 |
if ( DELTAT->now_step >= (1<<YM_DELTAT_SHIFT) ) |
| 483 |
{ |
| 484 |
step = DELTAT->now_step >> YM_DELTAT_SHIFT; |
| 485 |
DELTAT->now_step &= (1<<YM_DELTAT_SHIFT)-1; |
| 486 |
do{ |
| 487 |
|
| 488 |
if ( DELTAT->now_addr == (DELTAT->limit<<1) ) |
| 489 |
DELTAT->now_addr = 0; |
| 490 |
|
| 491 |
if ( DELTAT->now_addr == (DELTAT->end<<1) ) { /* 12-06-2001 JB: corrected comparison. Was > instead of == */ |
| 492 |
if( DELTAT->portstate&0x10 ){ |
| 493 |
/* repeat start */ |
| 494 |
DELTAT->now_addr = DELTAT->start<<1; |
| 495 |
DELTAT->acc = 0; |
| 496 |
DELTAT->adpcmd = YM_DELTAT_DELTA_DEF; |
| 497 |
DELTAT->prev_acc = 0; |
| 498 |
}else{ |
| 499 |
/* set EOS bit in status register */ |
| 500 |
if(DELTAT->status_set_handler != NULL && DELTAT->status_change_EOS_bit) |
| 501 |
(DELTAT->status_set_handler)(DELTAT->status_change_which_chip, DELTAT->status_change_EOS_bit); |
| 502 |
|
| 503 |
/* clear PCM BUSY bit (reflected in status register) */ |
| 504 |
DELTAT->PCM_BSY = 0; |
| 505 |
|
| 506 |
DELTAT->portstate = 0; |
| 507 |
DELTAT->adpcml = 0; |
| 508 |
DELTAT->prev_acc = 0; |
| 509 |
return; |
| 510 |
} |
| 511 |
} |
| 512 |
|
| 513 |
if( DELTAT->now_addr&1 ) data = DELTAT->now_data & 0x0f; |
| 514 |
else |
| 515 |
{ |
| 516 |
DELTAT->now_data = DELTAT->memory[(DELTAT->now_addr>>1)&DELTAT->memory_mask]; |
| 517 |
data = DELTAT->now_data >> 4; |
| 518 |
} |
| 519 |
|
| 520 |
DELTAT->now_addr++; |
| 521 |
/* 12-06-2001 JB: */ |
| 522 |
/* YM2610 address register is 24 bits wide.*/ |
| 523 |
/* The "+1" is there because we use 1 bit more for nibble calculations.*/ |
| 524 |
/* WARNING: */ |
| 525 |
/* Side effect: we should take the size of the mapped ROM into account */ |
| 526 |
//DELTAT->now_addr &= ( (1<<(24+1))-1); |
| 527 |
DELTAT->now_addr &= DELTAT->address_mask; |
| 528 |
|
| 529 |
|
| 530 |
/* store accumulator value */ |
| 531 |
DELTAT->prev_acc = DELTAT->acc; |
| 532 |
|
| 533 |
/* Forecast to next Forecast */ |
| 534 |
DELTAT->acc += (ym_deltat_decode_tableB1[data] * DELTAT->adpcmd / 8); |
| 535 |
YM_DELTAT_Limit(DELTAT->acc,YM_DELTAT_DECODE_MAX, YM_DELTAT_DECODE_MIN); |
| 536 |
|
| 537 |
/* delta to next delta */ |
| 538 |
DELTAT->adpcmd = (DELTAT->adpcmd * ym_deltat_decode_tableB2[data] ) / 64; |
| 539 |
YM_DELTAT_Limit(DELTAT->adpcmd,YM_DELTAT_DELTA_MAX, YM_DELTAT_DELTA_MIN ); |
| 540 |
|
| 541 |
/* ElSemi: Fix interpolator. */ |
| 542 |
/*DELTAT->prev_acc = prev_acc + ((DELTAT->acc - prev_acc) / 2 );*/ |
| 543 |
|
| 544 |
}while(--step); |
| 545 |
|
| 546 |
} |
| 547 |
|
| 548 |
/* ElSemi: Fix interpolator. */ |
| 549 |
DELTAT->adpcml = DELTAT->prev_acc * (int)((1<<YM_DELTAT_SHIFT)-DELTAT->now_step); |
| 550 |
DELTAT->adpcml += (DELTAT->acc * (int)DELTAT->now_step); |
| 551 |
DELTAT->adpcml = (DELTAT->adpcml>>YM_DELTAT_SHIFT) * (int)DELTAT->volume; |
| 552 |
|
| 553 |
/* output for work of output channels (outd[OPNxxxx])*/ |
| 554 |
*(DELTAT->pan) += DELTAT->adpcml; |
| 555 |
} |
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
INLINE void YM_DELTAT_synthesis_from_CPU_memory(YM_DELTAT *DELTAT) |
| 560 |
{ |
| 561 |
UINT32 step; |
| 562 |
int data; |
| 563 |
|
| 564 |
DELTAT->now_step += DELTAT->step; |
| 565 |
if ( DELTAT->now_step >= (1<<YM_DELTAT_SHIFT) ) |
| 566 |
{ |
| 567 |
step = DELTAT->now_step >> YM_DELTAT_SHIFT; |
| 568 |
DELTAT->now_step &= (1<<YM_DELTAT_SHIFT)-1; |
| 569 |
do{ |
| 570 |
|
| 571 |
if( DELTAT->now_addr&1 ) |
| 572 |
{ |
| 573 |
data = DELTAT->now_data & 0x0f; |
| 574 |
|
| 575 |
DELTAT->now_data = DELTAT->CPU_data; |
| 576 |
|
| 577 |
/* after we used CPU_data, we set BRDY bit in status register, |
| 578 |
* which means we are ready to accept another byte of data */ |
| 579 |
if(DELTAT->status_set_handler != NULL && DELTAT->status_change_BRDY_bit) |
| 580 |
(DELTAT->status_set_handler)(DELTAT->status_change_which_chip, DELTAT->status_change_BRDY_bit); |
| 581 |
} |
| 582 |
else |
| 583 |
{ |
| 584 |
data = DELTAT->now_data >> 4; |
| 585 |
} |
| 586 |
|
| 587 |
DELTAT->now_addr++; |
| 588 |
|
| 589 |
/* store accumulator value */ |
| 590 |
DELTAT->prev_acc = DELTAT->acc; |
| 591 |
|
| 592 |
/* Forecast to next Forecast */ |
| 593 |
DELTAT->acc += (ym_deltat_decode_tableB1[data] * DELTAT->adpcmd / 8); |
| 594 |
YM_DELTAT_Limit(DELTAT->acc,YM_DELTAT_DECODE_MAX, YM_DELTAT_DECODE_MIN); |
| 595 |
|
| 596 |
/* delta to next delta */ |
| 597 |
DELTAT->adpcmd = (DELTAT->adpcmd * ym_deltat_decode_tableB2[data] ) / 64; |
| 598 |
YM_DELTAT_Limit(DELTAT->adpcmd,YM_DELTAT_DELTA_MAX, YM_DELTAT_DELTA_MIN ); |
| 599 |
|
| 600 |
|
| 601 |
}while(--step); |
| 602 |
|
| 603 |
} |
| 604 |
|
| 605 |
/* ElSemi: Fix interpolator. */ |
| 606 |
DELTAT->adpcml = DELTAT->prev_acc * (int)((1<<YM_DELTAT_SHIFT)-DELTAT->now_step); |
| 607 |
DELTAT->adpcml += (DELTAT->acc * (int)DELTAT->now_step); |
| 608 |
DELTAT->adpcml = (DELTAT->adpcml>>YM_DELTAT_SHIFT) * (int)DELTAT->volume; |
| 609 |
|
| 610 |
/* output for work of output channels (outd[OPNxxxx])*/ |
| 611 |
*(DELTAT->pan) += DELTAT->adpcml; |
| 612 |
} |
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
/* ADPCM B (Delta-T control type) */ |
| 617 |
void YM_DELTAT_ADPCM_CALC(YM_DELTAT *DELTAT) |
| 618 |
{ |
| 619 |
|
| 620 |
/* |
| 621 |
some examples: |
| 622 |
value: START, REC, MEMDAT, REPEAT, SPOFF, x,x,RESET meaning: |
| 623 |
80 1 0 0 0 0 0 0 0 Synthesis (playing) from CPU (from reg $08) to AUDIO,sample rate in DELTA-N register |
| 624 |
a0 1 0 1 0 0 0 0 0 Synthesis (playing) from EXT.MEMORY to AUDIO, sample rate in DELTA-N register |
| 625 |
C8 1 1 0 0 1 0 0 0 Analysis (recording) from AUDIO to CPU (to reg $08), sample rate in PRESCALER register |
| 626 |
E8 1 1 1 0 1 0 0 0 Analysis (recording) from AUDIO to EXT.MEMORY, sample rate in PRESCALER register |
| 627 |
|
| 628 |
60 0 1 1 0 0 0 0 0 External memory write via ADPCM data register $08 |
| 629 |
20 0 0 1 0 0 0 0 0 External memory read via ADPCM data register $08 |
| 630 |
|
| 631 |
*/ |
| 632 |
|
| 633 |
if ( (DELTAT->portstate & 0xe0)==0xa0 ) |
| 634 |
{ |
| 635 |
YM_DELTAT_synthesis_from_external_memory(DELTAT); |
| 636 |
return; |
| 637 |
} |
| 638 |
|
| 639 |
if ( (DELTAT->portstate & 0xe0)==0x80 ) |
| 640 |
{ |
| 641 |
/* ADPCM synthesis from CPU-managed memory (from reg $08) */ |
| 642 |
YM_DELTAT_synthesis_from_CPU_memory(DELTAT); /* change output based on data in ADPCM data reg ($08) */ |
| 643 |
return; |
| 644 |
} |
| 645 |
|
| 646 |
//todo: ADPCM analysis |
| 647 |
// if ( (DELTAT->portstate & 0xe0)==0xc0 ) |
| 648 |
// if ( (DELTAT->portstate & 0xe0)==0xe0 ) |
| 649 |
|
| 650 |
return; |
| 651 |
} |
| 652 |
|
| 653 |
INLINE UINT32 pow2_mask(UINT32 v) |
| 654 |
{ |
| 655 |
if (v == 0) |
| 656 |
return 0; |
| 657 |
v --; |
| 658 |
v |= (v >> 1); |
| 659 |
v |= (v >> 2); |
| 660 |
v |= (v >> 4); |
| 661 |
v |= (v >> 8); |
| 662 |
v |= (v >> 16); |
| 663 |
return v; |
| 664 |
} |
| 665 |
|
| 666 |
void YM_DELTAT_calc_mem_mask(YM_DELTAT* DELTAT) |
| 667 |
{ |
| 668 |
DELTAT->memory_mask = pow2_mask(DELTAT->memory_size); |
| 669 |
|
| 670 |
return; |
| 671 |
} |