| 1 |
/* |
| 2 |
|
| 3 |
Capcom DL-1425 QSound emulator |
| 4 |
============================== |
| 5 |
|
| 6 |
by superctr (Ian Karlsson) |
| 7 |
with thanks to Valley Bell |
| 8 |
|
| 9 |
2018-05-12 - 2018-05-15 |
| 10 |
|
| 11 |
*/ |
| 12 |
|
| 13 |
#include <string.h> |
| 14 |
#include <stdint.h> |
| 15 |
#include <stdlib.h> |
| 16 |
#include <math.h> |
| 17 |
|
| 18 |
#include "qsound.h" |
| 19 |
|
| 20 |
#define CLAMP(x, low, high) (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x))) |
| 21 |
|
| 22 |
// ============================================================================ |
| 23 |
|
| 24 |
static const int16_t qsound_dry_mix_table[33] = { |
| 25 |
-16384,-16384,-16384,-16384,-16384,-16384,-16384,-16384, |
| 26 |
-16384,-16384,-16384,-16384,-16384,-16384,-16384,-16384, |
| 27 |
-16384,-14746,-13107,-11633,-10486,-9175,-8520,-7209, |
| 28 |
-6226,-5226,-4588,-3768,-3277,-2703,-2130,-1802, |
| 29 |
0 |
| 30 |
}; |
| 31 |
|
| 32 |
static const int16_t qsound_wet_mix_table[33] = { |
| 33 |
0,-1638,-1966,-2458,-2949,-3441,-4096,-4669, |
| 34 |
-4915,-5120,-5489,-6144,-7537,-8831,-9339,-9830, |
| 35 |
-10240,-10322,-10486,-10568,-10650,-11796,-12288,-12288, |
| 36 |
-12534,-12648,-12780,-12829,-12943,-13107,-13418,-14090, |
| 37 |
-16384 |
| 38 |
}; |
| 39 |
|
| 40 |
static const int16_t qsound_linear_mix_table[33] = { |
| 41 |
-16379,-16338,-16257,-16135,-15973,-15772,-15531,-15251, |
| 42 |
-14934,-14580,-14189,-13763,-13303,-12810,-12284,-11729, |
| 43 |
-11729,-11144,-10531,-9893,-9229,-8543,-7836,-7109, |
| 44 |
-6364,-5604,-4829,-4043,-3246,-2442,-1631,-817, |
| 45 |
0 |
| 46 |
}; |
| 47 |
|
| 48 |
static const int16_t qsound_filter_data[5][95] = { |
| 49 |
{ // d53 - 0 |
| 50 |
0,0,0,6,44,-24,-53,-10,59,-40,-27,1,39,-27,56,127,174,36,-13,49, |
| 51 |
212,142,143,-73,-20,66,-108,-117,-399,-265,-392,-569,-473,-71,95,-319,-218,-230,331,638, |
| 52 |
449,477,-180,532,1107,750,9899,3828,-2418,1071,-176,191,-431,64,117,-150,-274,-97,-238,165, |
| 53 |
166,250,-19,4,37,204,186,-6,140,-77,-1,1,18,-10,-151,-149,-103,-9,55,23, |
| 54 |
-102,-97,-11,13,-48,-27,5,18,-61,-30,64,72,0,0,0, |
| 55 |
}, |
| 56 |
{ // db2 - 1 - default left filter |
| 57 |
0,0,0,85,24,-76,-123,-86,-29,-14,-20,-7,6,-28,-87,-89,-5,100,154,160, |
| 58 |
150,118,41,-48,-78,-23,59,83,-2,-176,-333,-344,-203,-66,-39,2,224,495,495,280, |
| 59 |
432,1340,2483,5377,1905,658,0,97,347,285,35,-95,-78,-82,-151,-192,-171,-149,-147,-113, |
| 60 |
-22,71,118,129,127,110,71,31,20,36,46,23,-27,-63,-53,-21,-19,-60,-92,-69, |
| 61 |
-12,25,29,30,40,41,29,30,46,39,-15,-74,0,0,0, |
| 62 |
}, |
| 63 |
{ // e11 - 2 - default right filter |
| 64 |
0,0,0,23,42,47,29,10,2,-14,-54,-92,-93,-70,-64,-77,-57,18,94,113, |
| 65 |
87,69,67,50,25,29,58,62,24,-39,-131,-256,-325,-234,-45,58,78,223,485,496, |
| 66 |
127,6,857,2283,2683,4928,1328,132,79,314,189,-80,-90,35,-21,-186,-195,-99,-136,-258, |
| 67 |
-189,82,257,185,53,41,84,68,38,63,77,14,-60,-71,-71,-120,-151,-84,14,29, |
| 68 |
-8,7,66,69,12,-3,54,92,52,-6,-15,-2,0,0,0, |
| 69 |
}, |
| 70 |
{ // e70 - 3 |
| 71 |
0,0,0,2,-28,-37,-17,0,-9,-22,-3,35,52,39,20,7,-6,2,55,121, |
| 72 |
129,67,8,1,9,-6,-16,16,66,96,118,130,75,-47,-92,43,223,239,151,219, |
| 73 |
440,475,226,206,940,2100,2663,4980,865,49,-33,186,231,103,42,114,191,184,116,29, |
| 74 |
-47,-72,-21,60,96,68,31,32,63,87,76,39,7,14,55,85,67,18,-12,-3, |
| 75 |
21,34,29,6,-27,-49,-37,-2,16,0,-21,-16,0,0,0, |
| 76 |
}, |
| 77 |
{ // ecf - 4 |
| 78 |
0,0,0,48,7,-22,-29,-10,24,54,59,29,-36,-117,-185,-213,-185,-99,13,90, |
| 79 |
83,24,-5,23,53,47,38,56,67,57,75,107,16,-242,-440,-355,-120,-33,-47,152, |
| 80 |
501,472,-57,-292,544,1937,2277,6145,1240,153,47,200,152,36,64,134,74,-82,-208,-266, |
| 81 |
-268,-188,-42,65,74,56,89,133,114,44,-3,-1,17,29,29,-2,-76,-156,-187,-151, |
| 82 |
-85,-31,-5,7,20,32,24,-5,-20,6,48,62,0,0,0, |
| 83 |
} |
| 84 |
}; |
| 85 |
|
| 86 |
static const int16_t qsound_filter_data2[209] = { |
| 87 |
// f2e - following 95 values used for "disable output" filter |
| 88 |
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 89 |
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 90 |
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 91 |
0,0,0,0,0,0,0,0,0, |
| 92 |
|
| 93 |
// f73 - following 45 values used for "mode 2" filter (overlaps with f2e) |
| 94 |
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 95 |
0,0,0,0,0,0, |
| 96 |
-371,-196,-268,-512,-303,-315,-184,-76,276,-256,298,196,990,236,1114,-126,4377,6549,791, |
| 97 |
|
| 98 |
// fa0 - filtering disabled (for 95-taps) (use fa3 or fa4 for mode2 filters) |
| 99 |
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 100 |
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 101 |
0,0,0,0,0,0,0,-16384,0,0,0,0,0,0,0,0,0,0,0,0, |
| 102 |
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 103 |
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 |
| 104 |
}; |
| 105 |
|
| 106 |
static const int16_t adpcm_step_table[16] = { |
| 107 |
154, 154, 128, 102, 77, 58, 58, 58, |
| 108 |
58, 58, 58, 58, 77, 102, 128, 154 |
| 109 |
}; |
| 110 |
|
| 111 |
// DSP states |
| 112 |
enum { |
| 113 |
STATE_INIT1 = 0x288, |
| 114 |
STATE_INIT2 = 0x61a, |
| 115 |
STATE_REFRESH1 = 0x039, |
| 116 |
STATE_REFRESH2 = 0x04f, |
| 117 |
STATE_NORMAL1 = 0x314, |
| 118 |
STATE_NORMAL2 = 0x6b2, |
| 119 |
}; |
| 120 |
|
| 121 |
enum { |
| 122 |
PANTBL_LEFT = 0, |
| 123 |
PANTBL_RIGHT = 1, |
| 124 |
PANTBL_DRY = 0, |
| 125 |
PANTBL_WET = 1, |
| 126 |
}; |
| 127 |
|
| 128 |
static void init_pan_tables(struct qsound_chip *chip); |
| 129 |
static void init_register_map(struct qsound_chip *chip); |
| 130 |
|
| 131 |
static void state_init(struct qsound_chip *chip); |
| 132 |
static void state_refresh_filter_1(struct qsound_chip *chip); |
| 133 |
static void state_refresh_filter_2(struct qsound_chip *chip); |
| 134 |
static void state_normal_update(struct qsound_chip *chip); |
| 135 |
|
| 136 |
static inline int16_t get_sample(struct qsound_chip *chip, uint16_t bank,uint16_t address); |
| 137 |
static inline int16_t* get_filter_table(struct qsound_chip *chip, uint16_t offset); |
| 138 |
static inline int16_t pcm_update(struct qsound_chip *chip, int voice_no, int32_t *echo_out); |
| 139 |
static inline void adpcm_update(struct qsound_chip *chip, int voice_no, int nibble); |
| 140 |
static inline int16_t echo(struct qsound_echo *r,int32_t input); |
| 141 |
static inline int32_t fir(struct qsound_fir *f, int16_t input); |
| 142 |
static inline int32_t delay(struct qsound_delay *d, int32_t input); |
| 143 |
static inline void delay_update(struct qsound_delay *d); |
| 144 |
|
| 145 |
// ============================================================================ |
| 146 |
|
| 147 |
long qsound_start(struct qsound_chip *chip, int clock) |
| 148 |
{ |
| 149 |
memset(chip,0,sizeof(*chip)); |
| 150 |
|
| 151 |
init_pan_tables(chip); |
| 152 |
init_register_map(chip); |
| 153 |
|
| 154 |
return clock / 2 / 1248; // DSP program uses 1248 machine cycles per iteration |
| 155 |
} |
| 156 |
|
| 157 |
void qsound_reset(struct qsound_chip *chip) |
| 158 |
{ |
| 159 |
chip->ready_flag = 0; |
| 160 |
chip->out[0] = chip->out[1] = 0; |
| 161 |
chip->state = 0; |
| 162 |
chip->state_counter = 0; |
| 163 |
} |
| 164 |
|
| 165 |
void qsound_stream_update(struct qsound_chip *chip, int16_t **outputs, int samples) |
| 166 |
{ |
| 167 |
// Clear the buffers |
| 168 |
memset(outputs[0], 0, samples * sizeof(*outputs[0])); |
| 169 |
memset(outputs[1], 0, samples * sizeof(*outputs[1])); |
| 170 |
|
| 171 |
for (int i = 0; i < samples; i ++) |
| 172 |
{ |
| 173 |
qsound_update(chip); |
| 174 |
outputs[0][i] = chip->out[0]; |
| 175 |
outputs[1][i] = chip->out[1]; |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
void qsound_w(struct qsound_chip *chip, uint8_t offset, uint8_t data) |
| 180 |
{ |
| 181 |
switch (offset) |
| 182 |
{ |
| 183 |
case 0: |
| 184 |
chip->data_latch = (chip->data_latch & 0x00ff) | (data << 8); |
| 185 |
break; |
| 186 |
case 1: |
| 187 |
chip->data_latch = (chip->data_latch & 0xff00) | data; |
| 188 |
break; |
| 189 |
case 2: |
| 190 |
qsound_write_data(chip, data, chip->data_latch); |
| 191 |
break; |
| 192 |
default: |
| 193 |
break; |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
uint8_t qsound_r(struct qsound_chip *chip) |
| 198 |
{ |
| 199 |
// ready bit (0x00 = busy, 0x80 == ready) |
| 200 |
return chip->ready_flag; |
| 201 |
} |
| 202 |
|
| 203 |
void qsound_write_data(struct qsound_chip *chip, uint8_t address, uint16_t data) |
| 204 |
{ |
| 205 |
uint16_t *destination = chip->register_map[address]; |
| 206 |
if(destination) |
| 207 |
*destination = data; |
| 208 |
chip->ready_flag = 0; |
| 209 |
} |
| 210 |
|
| 211 |
uint16_t qsound_read_data(struct qsound_chip *chip, uint8_t address) |
| 212 |
{ |
| 213 |
uint16_t data = 0; |
| 214 |
uint16_t *source = chip->register_map[address]; |
| 215 |
if(source) |
| 216 |
data = *source; |
| 217 |
return data; |
| 218 |
} |
| 219 |
|
| 220 |
// ============================================================================ |
| 221 |
|
| 222 |
static void init_pan_tables(struct qsound_chip *chip) |
| 223 |
{ |
| 224 |
int i; |
| 225 |
for(i=0;i<33;i++) |
| 226 |
{ |
| 227 |
// dry mixing levels |
| 228 |
chip->pan_tables[PANTBL_LEFT][PANTBL_DRY][i] = qsound_dry_mix_table[i]; |
| 229 |
chip->pan_tables[PANTBL_RIGHT][PANTBL_DRY][i] = qsound_dry_mix_table[32-i]; |
| 230 |
// wet mixing levels |
| 231 |
chip->pan_tables[PANTBL_LEFT][PANTBL_WET][i] = qsound_wet_mix_table[i]; |
| 232 |
chip->pan_tables[PANTBL_RIGHT][PANTBL_WET][i] = qsound_wet_mix_table[32-i]; |
| 233 |
// linear panning, only for dry component. wet component is muted. |
| 234 |
chip->pan_tables[PANTBL_LEFT][PANTBL_DRY][i+0x30] = qsound_linear_mix_table[i]; |
| 235 |
chip->pan_tables[PANTBL_RIGHT][PANTBL_DRY][i+0x30] = qsound_linear_mix_table[32-i]; |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
static void init_register_map(struct qsound_chip *chip) |
| 240 |
{ |
| 241 |
int i; |
| 242 |
|
| 243 |
// unused registers |
| 244 |
for(i=0;i<256;i++) |
| 245 |
chip->register_map[i] = NULL; |
| 246 |
|
| 247 |
// PCM registers |
| 248 |
for(i=0;i<16;i++) // PCM voices |
| 249 |
{ |
| 250 |
chip->register_map[(i<<3)+0] = (uint16_t*)&chip->voice[(i+1)%16].bank; // Bank applies to the next channel |
| 251 |
chip->register_map[(i<<3)+1] = (uint16_t*)&chip->voice[i].addr; // Current sample position and start position. |
| 252 |
chip->register_map[(i<<3)+2] = (uint16_t*)&chip->voice[i].rate; // 4.12 fixed point decimal. |
| 253 |
chip->register_map[(i<<3)+3] = (uint16_t*)&chip->voice[i].phase; |
| 254 |
chip->register_map[(i<<3)+4] = (uint16_t*)&chip->voice[i].loop_len; |
| 255 |
chip->register_map[(i<<3)+5] = (uint16_t*)&chip->voice[i].end_addr; |
| 256 |
chip->register_map[(i<<3)+6] = (uint16_t*)&chip->voice[i].volume; |
| 257 |
chip->register_map[(i<<3)+7] = NULL; // unused |
| 258 |
chip->register_map[i+0x80] = (uint16_t*)&chip->voice_pan[i]; |
| 259 |
chip->register_map[i+0xba] = (uint16_t*)&chip->voice[i].echo; |
| 260 |
} |
| 261 |
|
| 262 |
// ADPCM registers |
| 263 |
for(i=0;i<3;i++) // ADPCM voices |
| 264 |
{ |
| 265 |
// ADPCM sample rate is fixed to 8khz. (one channel is updated every third sample) |
| 266 |
chip->register_map[(i<<2)+0xca] = (uint16_t*)&chip->adpcm[i].start_addr; |
| 267 |
chip->register_map[(i<<2)+0xcb] = (uint16_t*)&chip->adpcm[i].end_addr; |
| 268 |
chip->register_map[(i<<2)+0xcc] = (uint16_t*)&chip->adpcm[i].bank; |
| 269 |
chip->register_map[(i<<2)+0xcd] = (uint16_t*)&chip->adpcm[i].volume; |
| 270 |
chip->register_map[i+0xd6] = (uint16_t*)&chip->adpcm[i].flag; // non-zero to start ADPCM playback |
| 271 |
chip->register_map[i+0x90] = (uint16_t*)&chip->voice_pan[16+i]; |
| 272 |
} |
| 273 |
|
| 274 |
// QSound registers |
| 275 |
chip->register_map[0x93] = (uint16_t*)&chip->echo.feedback; |
| 276 |
chip->register_map[0xd9] = (uint16_t*)&chip->echo.end_pos; |
| 277 |
chip->register_map[0xe2] = (uint16_t*)&chip->delay_update; // non-zero to update delays |
| 278 |
chip->register_map[0xe3] = (uint16_t*)&chip->next_state; |
| 279 |
for(i=0;i<2;i++) // left, right |
| 280 |
{ |
| 281 |
// Wet |
| 282 |
chip->register_map[(i<<1)+0xda] = (uint16_t*)&chip->filter[i].table_pos; |
| 283 |
chip->register_map[(i<<1)+0xde] = (uint16_t*)&chip->wet[i].delay; |
| 284 |
chip->register_map[(i<<1)+0xe4] = (uint16_t*)&chip->wet[i].volume; |
| 285 |
// Dry |
| 286 |
chip->register_map[(i<<1)+0xdb] = (uint16_t*)&chip->alt_filter[i].table_pos; |
| 287 |
chip->register_map[(i<<1)+0xdf] = (uint16_t*)&chip->dry[i].delay; |
| 288 |
chip->register_map[(i<<1)+0xe5] = (uint16_t*)&chip->dry[i].volume; |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
static inline int16_t get_sample(struct qsound_chip *chip, uint16_t bank,uint16_t address) |
| 293 |
{ |
| 294 |
uint32_t rom_addr; |
| 295 |
uint8_t sample_data; |
| 296 |
|
| 297 |
if (! chip->rom_mask) |
| 298 |
return 0; // no ROM loaded |
| 299 |
if (! (bank & 0x8000)) |
| 300 |
return 0; // ignore attempts to read from DSP program ROM |
| 301 |
|
| 302 |
bank &= 0x7FFF; |
| 303 |
rom_addr = (bank << 16) | (address << 0); |
| 304 |
|
| 305 |
sample_data = chip->rom_data[rom_addr]; |
| 306 |
|
| 307 |
return (int16_t)((sample_data << 8) | (sample_data << 0)); // MAME currently expands the 8 bit ROM data to 16 bits this way. |
| 308 |
} |
| 309 |
|
| 310 |
static inline int16_t* get_filter_table(struct qsound_chip *chip, uint16_t offset) |
| 311 |
{ |
| 312 |
int index; |
| 313 |
|
| 314 |
if (offset >= 0xf2e && offset < 0xfff) |
| 315 |
return (int16_t*)&qsound_filter_data2[offset-0xf2e]; // overlapping filter data |
| 316 |
|
| 317 |
index = (offset-0xd53)/95; |
| 318 |
if(index >= 0 && index < 5) |
| 319 |
return (int16_t*)&qsound_filter_data[index]; // normal tables |
| 320 |
|
| 321 |
return NULL; // no filter found. |
| 322 |
} |
| 323 |
|
| 324 |
/********************************************************************/ |
| 325 |
|
| 326 |
// updates one DSP sample |
| 327 |
void qsound_update(struct qsound_chip *chip) |
| 328 |
{ |
| 329 |
switch(chip->state) |
| 330 |
{ |
| 331 |
default: |
| 332 |
case STATE_INIT1: |
| 333 |
case STATE_INIT2: |
| 334 |
state_init(chip); return; |
| 335 |
case STATE_REFRESH1: |
| 336 |
state_refresh_filter_1(chip); return; |
| 337 |
case STATE_REFRESH2: |
| 338 |
state_refresh_filter_2(chip); return; |
| 339 |
case STATE_NORMAL1: |
| 340 |
case STATE_NORMAL2: |
| 341 |
state_normal_update(chip); return; |
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
// Initialization routine |
| 346 |
static void state_init(struct qsound_chip *chip) |
| 347 |
{ |
| 348 |
int mode = (chip->state == STATE_INIT2) ? 1 : 0; |
| 349 |
int i; |
| 350 |
|
| 351 |
// we're busy for 4 samples, including the filter refresh. |
| 352 |
if(chip->state_counter >= 2) |
| 353 |
{ |
| 354 |
chip->state_counter = 0; |
| 355 |
chip->state = chip->next_state; |
| 356 |
return; |
| 357 |
} |
| 358 |
else if(chip->state_counter == 1) |
| 359 |
{ |
| 360 |
chip->state_counter++; |
| 361 |
return; |
| 362 |
} |
| 363 |
|
| 364 |
memset(chip->voice, 0, sizeof(chip->voice)); |
| 365 |
memset(chip->adpcm, 0, sizeof(chip->adpcm)); |
| 366 |
memset(chip->filter, 0, sizeof(chip->filter)); |
| 367 |
memset(chip->alt_filter, 0, sizeof(chip->alt_filter)); |
| 368 |
memset(chip->wet, 0, sizeof(chip->wet)); |
| 369 |
memset(chip->dry, 0, sizeof(chip->dry)); |
| 370 |
memset(&chip->echo, 0, sizeof(chip->echo)); |
| 371 |
|
| 372 |
for(i=0;i<19;i++) |
| 373 |
{ |
| 374 |
chip->voice_pan[i] = 0x120; |
| 375 |
chip->voice_output[i] = 0; |
| 376 |
} |
| 377 |
|
| 378 |
for(i=0;i<16;i++) |
| 379 |
chip->voice[i].bank = 0x8000; |
| 380 |
for(i=0;i<3;i++) |
| 381 |
chip->adpcm[i].bank = 0x8000; |
| 382 |
|
| 383 |
if(mode == 0) |
| 384 |
{ |
| 385 |
// mode 1 |
| 386 |
chip->wet[0].delay = 0; |
| 387 |
chip->dry[0].delay = 46; |
| 388 |
chip->wet[1].delay = 0; |
| 389 |
chip->dry[1].delay = 48; |
| 390 |
chip->filter[0].table_pos = 0xdb2; |
| 391 |
chip->filter[1].table_pos = 0xe11; |
| 392 |
chip->echo.end_pos = 0x554 + 6; |
| 393 |
chip->next_state = STATE_REFRESH1; |
| 394 |
} |
| 395 |
else |
| 396 |
{ |
| 397 |
// mode 2 |
| 398 |
chip->wet[0].delay = 1; |
| 399 |
chip->dry[0].delay = 0; |
| 400 |
chip->wet[1].delay = 0; |
| 401 |
chip->dry[1].delay = 0; |
| 402 |
chip->filter[0].table_pos = 0xf73; |
| 403 |
chip->filter[1].table_pos = 0xfa4; |
| 404 |
chip->alt_filter[0].table_pos = 0xf73; |
| 405 |
chip->alt_filter[1].table_pos = 0xfa4; |
| 406 |
chip->echo.end_pos = 0x53c + 6; |
| 407 |
chip->next_state = STATE_REFRESH2; |
| 408 |
} |
| 409 |
|
| 410 |
chip->wet[0].volume = 0x3fff; |
| 411 |
chip->dry[0].volume = 0x3fff; |
| 412 |
chip->wet[1].volume = 0x3fff; |
| 413 |
chip->dry[1].volume = 0x3fff; |
| 414 |
|
| 415 |
chip->delay_update = 1; |
| 416 |
chip->ready_flag = 0; |
| 417 |
chip->state_counter = 1; |
| 418 |
} |
| 419 |
|
| 420 |
// Updates filter parameters for mode 1 |
| 421 |
static void state_refresh_filter_1(struct qsound_chip *chip) |
| 422 |
{ |
| 423 |
const int16_t *table; |
| 424 |
|
| 425 |
for(int ch=0; ch<2; ch++) |
| 426 |
{ |
| 427 |
chip->filter[ch].delay_pos = 0; |
| 428 |
chip->filter[ch].tap_count = 95; |
| 429 |
|
| 430 |
table = get_filter_table(chip,chip->filter[ch].table_pos); |
| 431 |
if (table != NULL) |
| 432 |
memcpy(chip->filter[ch].taps, table, 95 * sizeof(int16_t)); |
| 433 |
} |
| 434 |
|
| 435 |
chip->state = chip->next_state = STATE_NORMAL1; |
| 436 |
} |
| 437 |
|
| 438 |
// Updates filter parameters for mode 2 |
| 439 |
static void state_refresh_filter_2(struct qsound_chip *chip) |
| 440 |
{ |
| 441 |
const int16_t *table; |
| 442 |
|
| 443 |
for(int ch=0; ch<2; ch++) |
| 444 |
{ |
| 445 |
chip->filter[ch].delay_pos = 0; |
| 446 |
chip->filter[ch].tap_count = 45; |
| 447 |
|
| 448 |
table = get_filter_table(chip,chip->filter[ch].table_pos); |
| 449 |
if (table != NULL) |
| 450 |
memcpy(chip->filter[ch].taps, table, 45 * sizeof(int16_t)); |
| 451 |
|
| 452 |
chip->alt_filter[ch].delay_pos = 0; |
| 453 |
chip->alt_filter[ch].tap_count = 44; |
| 454 |
|
| 455 |
table = get_filter_table(chip,chip->alt_filter[ch].table_pos); |
| 456 |
if (table != NULL) |
| 457 |
memcpy(chip->alt_filter[ch].taps, table, 44 * sizeof(int16_t)); |
| 458 |
} |
| 459 |
|
| 460 |
chip->state = chip->next_state = STATE_NORMAL2; |
| 461 |
} |
| 462 |
|
| 463 |
// Updates a PCM voice. There are 16 voices, each are updated every sample |
| 464 |
// with full rate and volume control. |
| 465 |
static inline int16_t pcm_update(struct qsound_chip *chip, int voice_no, int32_t *echo_out) |
| 466 |
{ |
| 467 |
struct qsound_voice* v = &chip->voice[voice_no]; |
| 468 |
|
| 469 |
int32_t new_phase; |
| 470 |
int16_t output = 0; |
| 471 |
|
| 472 |
if(!(chip->mute_mask & (1<<voice_no))) |
| 473 |
{ |
| 474 |
// Read sample from rom and apply volume |
| 475 |
output = (v->volume * get_sample(chip, v->bank, v->addr))>>14; |
| 476 |
*echo_out += (output * v->echo)<<2; |
| 477 |
} |
| 478 |
|
| 479 |
// Add delta to the phase and loop back if required |
| 480 |
new_phase = v->rate + ((v->addr<<12) | (v->phase>>4)); |
| 481 |
|
| 482 |
if((new_phase>>12) >= v->end_addr) |
| 483 |
new_phase -= (v->loop_len<<12); |
| 484 |
|
| 485 |
new_phase = CLAMP(new_phase, -0x8000000, 0x7FFFFFF); |
| 486 |
v->addr = new_phase>>12; |
| 487 |
v->phase = (new_phase<<4)&0xffff; |
| 488 |
|
| 489 |
return output; |
| 490 |
} |
| 491 |
|
| 492 |
// Updates an ADPCM voice. There are 3 voices, one is updated every sample |
| 493 |
// (effectively making the ADPCM rate 1/3 of the master sample rate), and |
| 494 |
// volume is set when starting samples only. |
| 495 |
// The ADPCM algorithm is supposedly similar to Yamaha ADPCM. It also seems |
| 496 |
// like Capcom never used it, so this was not emulated in the earlier QSound |
| 497 |
// emulators. |
| 498 |
static inline void adpcm_update(struct qsound_chip *chip, int voice_no, int nibble) |
| 499 |
{ |
| 500 |
struct qsound_adpcm *v = &chip->adpcm[voice_no]; |
| 501 |
|
| 502 |
int32_t delta; |
| 503 |
int8_t step; |
| 504 |
|
| 505 |
if(!nibble) |
| 506 |
{ |
| 507 |
// Mute voice when it reaches the end address. |
| 508 |
if(v->cur_addr == v->end_addr) |
| 509 |
v->cur_vol = 0; |
| 510 |
|
| 511 |
// Playback start flag |
| 512 |
if(v->flag) |
| 513 |
{ |
| 514 |
chip->voice_output[16+voice_no] = 0; |
| 515 |
v->flag = 0; |
| 516 |
v->step_size = 10; |
| 517 |
v->cur_vol = v->volume; |
| 518 |
v->cur_addr = v->start_addr; |
| 519 |
} |
| 520 |
|
| 521 |
// get top nibble |
| 522 |
step = get_sample(chip, v->bank, v->cur_addr) >> 8; |
| 523 |
} |
| 524 |
else |
| 525 |
{ |
| 526 |
// get bottom nibble |
| 527 |
step = get_sample(chip, v->bank, v->cur_addr++) >> 4; |
| 528 |
} |
| 529 |
|
| 530 |
// shift with sign extend |
| 531 |
step >>= 4; |
| 532 |
|
| 533 |
// delta = (0.5 + abs(v->step)) * v->step_size |
| 534 |
delta = ((1+abs(step<<1)) * v->step_size)>>1; |
| 535 |
if(step <= 0) |
| 536 |
delta = -delta; |
| 537 |
delta += chip->voice_output[16+voice_no]; |
| 538 |
delta = CLAMP(delta,-32768,32767); |
| 539 |
|
| 540 |
if(chip->mute_mask & (1<<(16+voice_no))) |
| 541 |
chip->voice_output[16+voice_no] = 0; |
| 542 |
else |
| 543 |
chip->voice_output[16+voice_no] = (delta * v->cur_vol)>>16; |
| 544 |
|
| 545 |
v->step_size = (adpcm_step_table[8+step] * v->step_size) >> 6; |
| 546 |
v->step_size = CLAMP(v->step_size, 1, 2000); |
| 547 |
} |
| 548 |
|
| 549 |
// The echo effect is pretty simple. A moving average filter is used on |
| 550 |
// the output from the delay line to smooth samples over time. |
| 551 |
static inline int16_t echo(struct qsound_echo *r,int32_t input) |
| 552 |
{ |
| 553 |
// get average of last 2 samples from the delay line |
| 554 |
int32_t new_sample; |
| 555 |
int32_t old_sample = r->delay_line[r->delay_pos]; |
| 556 |
int32_t last_sample = r->last_sample; |
| 557 |
|
| 558 |
r->last_sample = old_sample; |
| 559 |
old_sample = (old_sample+last_sample) >> 1; |
| 560 |
|
| 561 |
// add current sample to the delay line |
| 562 |
new_sample = input + ((old_sample * r->feedback)<<2); |
| 563 |
r->delay_line[r->delay_pos++] = new_sample>>16; |
| 564 |
|
| 565 |
if(r->delay_pos >= r->length) |
| 566 |
r->delay_pos = 0; |
| 567 |
|
| 568 |
return old_sample; |
| 569 |
} |
| 570 |
|
| 571 |
// Process a sample update |
| 572 |
static void state_normal_update(struct qsound_chip *chip) |
| 573 |
{ |
| 574 |
int v, ch; |
| 575 |
int32_t echo_input = 0; |
| 576 |
int16_t echo_output; |
| 577 |
|
| 578 |
chip->ready_flag = 0x80; |
| 579 |
|
| 580 |
// recalculate echo length |
| 581 |
if(chip->state == STATE_NORMAL2) |
| 582 |
chip->echo.length = chip->echo.end_pos - 0x53c; |
| 583 |
else |
| 584 |
chip->echo.length = chip->echo.end_pos - 0x554; |
| 585 |
|
| 586 |
chip->echo.length = CLAMP(chip->echo.length, 0, 1024); |
| 587 |
|
| 588 |
// update PCM voices |
| 589 |
for(v=0; v<16; v++) |
| 590 |
{ |
| 591 |
chip->voice_output[v] = pcm_update(chip, v, &echo_input); |
| 592 |
} |
| 593 |
|
| 594 |
// update ADPCM voices (one every third sample) |
| 595 |
adpcm_update(chip, chip->state_counter % 3, chip->state_counter / 3); |
| 596 |
|
| 597 |
echo_output = echo(&chip->echo,echo_input); |
| 598 |
|
| 599 |
// now, we do the magic stuff |
| 600 |
for(ch=0; ch<2; ch++) |
| 601 |
{ |
| 602 |
// Echo is output on the unfiltered component of the left channel and |
| 603 |
// the filtered component of the right channel. |
| 604 |
int32_t wet = (ch == 1) ? echo_output<<14 : 0; |
| 605 |
int32_t dry = (ch == 0) ? echo_output<<14 : 0; |
| 606 |
int32_t output = 0; |
| 607 |
|
| 608 |
for(int v=0; v<19; v++) |
| 609 |
{ |
| 610 |
uint16_t pan_index = chip->voice_pan[v]-0x110; |
| 611 |
if(pan_index > 97) |
| 612 |
pan_index = 97; |
| 613 |
|
| 614 |
// Apply different volume tables on the dry and wet inputs. |
| 615 |
dry -= (chip->voice_output[v] * chip->pan_tables[ch][PANTBL_DRY][pan_index]); |
| 616 |
wet -= (chip->voice_output[v] * chip->pan_tables[ch][PANTBL_WET][pan_index]); |
| 617 |
} |
| 618 |
|
| 619 |
// Saturate accumulated voices |
| 620 |
dry = CLAMP(dry, -0x1fffffff, 0x1fffffff) << 2; |
| 621 |
wet = CLAMP(wet, -0x1fffffff, 0x1fffffff) << 2; |
| 622 |
|
| 623 |
// Apply FIR filter on 'wet' input |
| 624 |
wet = fir(&chip->filter[ch], wet >> 16); |
| 625 |
|
| 626 |
// in mode 2, we do this on the 'dry' input too |
| 627 |
if(chip->state == STATE_NORMAL2) |
| 628 |
dry = fir(&chip->alt_filter[ch], dry >> 16); |
| 629 |
|
| 630 |
// output goes through a delay line and attenuation |
| 631 |
output = (delay(&chip->wet[ch], wet) + delay(&chip->dry[ch], dry)); |
| 632 |
|
| 633 |
// DSP round function |
| 634 |
output = (output + 0x2000) >> 14; |
| 635 |
chip->out[ch] = CLAMP(output, -0x7fff, 0x7fff); |
| 636 |
|
| 637 |
if(chip->delay_update) |
| 638 |
{ |
| 639 |
delay_update(&chip->wet[ch]); |
| 640 |
delay_update(&chip->dry[ch]); |
| 641 |
} |
| 642 |
} |
| 643 |
|
| 644 |
chip->delay_update = 0; |
| 645 |
|
| 646 |
// after 6 samples, the next state is executed. |
| 647 |
chip->state_counter++; |
| 648 |
if(chip->state_counter > 5) |
| 649 |
{ |
| 650 |
chip->state_counter = 0; |
| 651 |
chip->state = chip->next_state; |
| 652 |
} |
| 653 |
} |
| 654 |
|
| 655 |
// Apply the FIR filter used as the Q1 transfer function |
| 656 |
static inline int32_t fir(struct qsound_fir *f, int16_t input) |
| 657 |
{ |
| 658 |
int32_t output = 0, tap = 0; |
| 659 |
|
| 660 |
for(; tap < (f->tap_count-1); tap++) |
| 661 |
{ |
| 662 |
output -= (f->taps[tap] * f->delay_line[f->delay_pos++])<<2; |
| 663 |
|
| 664 |
if(f->delay_pos >= f->tap_count-1) |
| 665 |
f->delay_pos = 0; |
| 666 |
} |
| 667 |
|
| 668 |
output -= (f->taps[tap] * input)<<2; |
| 669 |
|
| 670 |
f->delay_line[f->delay_pos++] = input; |
| 671 |
if(f->delay_pos >= f->tap_count-1) |
| 672 |
f->delay_pos = 0; |
| 673 |
|
| 674 |
return output; |
| 675 |
} |
| 676 |
|
| 677 |
// Apply delay line and component volume |
| 678 |
static inline int32_t delay(struct qsound_delay *d, int32_t input) |
| 679 |
{ |
| 680 |
int32_t output; |
| 681 |
|
| 682 |
d->delay_line[d->write_pos++] = input>>16; |
| 683 |
if(d->write_pos >= 51) |
| 684 |
d->write_pos = 0; |
| 685 |
|
| 686 |
output = d->delay_line[d->read_pos++]*d->volume; |
| 687 |
if(d->read_pos >= 51) |
| 688 |
d->read_pos = 0; |
| 689 |
|
| 690 |
return output; |
| 691 |
} |
| 692 |
|
| 693 |
// Update the delay read position to match new delay length |
| 694 |
static inline void delay_update(struct qsound_delay *d) |
| 695 |
{ |
| 696 |
int16_t new_read_pos = (d->write_pos - d->delay) % 51; |
| 697 |
if(new_read_pos < 0) |
| 698 |
new_read_pos += 51; |
| 699 |
|
| 700 |
d->read_pos = new_read_pos; |
| 701 |
} |
| 702 |
|