| 1 |
/* |
| 2 |
|
| 3 |
============================================================================ |
| 4 |
|
| 5 |
MODIFIED Namco C140/C219 sound emulator - MODIFIED VERSION |
| 6 |
by cam900 |
| 7 |
|
| 8 |
MODIFICATION by tildearrow - adds muting function and fixes overflow |
| 9 |
THIS IS NOT THE ORIGINAL VERSION - you can find the original one in |
| 10 |
commit 72d04777c013988ed8cf6da27c62a9d784a59dff |
| 11 |
|
| 12 |
This file is licensed under zlib license. |
| 13 |
|
| 14 |
============================================================================ |
| 15 |
|
| 16 |
zlib License |
| 17 |
|
| 18 |
(C) 2023-present cam900 and contributors |
| 19 |
|
| 20 |
This software is provided 'as-is', without any express or implied |
| 21 |
warranty. In no event will the authors be held liable for any damages |
| 22 |
arising from the use of this software. |
| 23 |
|
| 24 |
Permission is granted to anyone to use this software for any purpose, |
| 25 |
including commercial applications, and to alter it and redistribute it |
| 26 |
freely, subject to the following restrictions: |
| 27 |
|
| 28 |
1. The origin of this software must not be misrepresented; you must not |
| 29 |
claim that you wrote the original software. If you use this software |
| 30 |
in a product, an acknowledgment in the product documentation would be |
| 31 |
appreciated but is not required. |
| 32 |
2. Altered source versions must be plainly marked as such, and must not be |
| 33 |
misrepresented as being the original software. |
| 34 |
3. This notice may not be removed or altered from any source distribution. |
| 35 |
|
| 36 |
============================================================================ |
| 37 |
|
| 38 |
TODO: |
| 39 |
- unknown registers (Bit 6 of control register, etc) |
| 40 |
- Internal timer |
| 41 |
|
| 42 |
*/ |
| 43 |
|
| 44 |
#include "c140_c219.h" |
| 45 |
|
| 46 |
static int c140_max(int a, int b) { return (a > b) ? a : b; } |
| 47 |
static int c140_min(int a, int b) { return (a < b) ? a : b; } |
| 48 |
static int c140_clamp(int v, int min, int max) { return c140_min(c140_max(v,min),max); } |
| 49 |
static int c140_bit(int val, int bit) { return (val >> bit) & 1; } |
| 50 |
static int c140_bitfield(int val, int bit, int len) { return (val >> bit) & ((1 << len) - 1);} |
| 51 |
|
| 52 |
void c140_tick(struct c140_t *c140, const int cycle) |
| 53 |
{ |
| 54 |
c140->lout = 0; |
| 55 |
c140->rout = 0; |
| 56 |
for (int i = 0; i < 24; i++) |
| 57 |
{ |
| 58 |
c140_voice_tick(c140, i, cycle); |
| 59 |
c140->lout += c140->voice[i].lout; |
| 60 |
c140->rout += c140->voice[i].rout; |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
void c219_tick(struct c219_t *c219, const int cycle) |
| 65 |
{ |
| 66 |
c219->lout = 0; |
| 67 |
c219->rout = 0; |
| 68 |
for (int i = 0; i < 16; i++) |
| 69 |
{ |
| 70 |
c219_voice_tick(c219, i, cycle); |
| 71 |
c219->lout += c219->voice[i].lout; |
| 72 |
c219->rout += c219->voice[i].rout; |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
void c140_voice_tick(struct c140_t *c140, const unsigned char v, const int cycle) |
| 77 |
{ |
| 78 |
struct c140_voice_t *voice = &c140->voice[v]; |
| 79 |
if (voice->busy && voice->keyon) |
| 80 |
{ |
| 81 |
for (int c = 0; c < cycle; c++) |
| 82 |
{ |
| 83 |
voice->frac += voice->freq; |
| 84 |
if (voice->frac > 0xffff) |
| 85 |
{ |
| 86 |
voice->addr += voice->frac >> 16; |
| 87 |
if (voice->addr > voice->end_addr) |
| 88 |
{ |
| 89 |
if (voice->loop) |
| 90 |
{ |
| 91 |
voice->addr = (voice->addr + voice->loop_addr) - voice->end_addr; |
| 92 |
} |
| 93 |
else |
| 94 |
{ |
| 95 |
voice->keyon = false; |
| 96 |
voice->lout = 0; |
| 97 |
voice->rout = 0; |
| 98 |
return; |
| 99 |
} |
| 100 |
} |
| 101 |
voice->frac &= 0xffff; |
| 102 |
} |
| 103 |
} |
| 104 |
if (!voice->muted) |
| 105 |
{ |
| 106 |
// fetch 12 bit sample |
| 107 |
signed short s1 = c140->sample_mem[((unsigned int)(voice->bank) << 16) | (voice->addr & 0xffff)] & ~0xf; |
| 108 |
signed short s2 = c140->sample_mem[((unsigned int)(voice->bank) << 16) | ((voice->addr + 1) & 0xffff)] & ~0xf; |
| 109 |
if (voice->compressed) |
| 110 |
{ |
| 111 |
s1 = c140->mulaw[(s1 >> 8) & 0xff]; |
| 112 |
s2 = c140->mulaw[(s2 >> 8) & 0xff]; |
| 113 |
} |
| 114 |
// interpolate (originally was >>16, but I had to reduce it to 15 to prevent overflow) |
| 115 |
signed int sample = s1 + (((voice->frac >> 1) * (s2 - s1)) >> 15); |
| 116 |
voice->lout = sample * voice->lvol; |
| 117 |
voice->rout = sample * voice->rvol; |
| 118 |
} |
| 119 |
else |
| 120 |
{ |
| 121 |
voice->lout = 0; |
| 122 |
voice->rout = 0; |
| 123 |
} |
| 124 |
} |
| 125 |
else |
| 126 |
{ |
| 127 |
voice->lout = 0; |
| 128 |
voice->rout = 0; |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
void c219_voice_tick(struct c219_t *c219, const unsigned char v, const int cycle) |
| 133 |
{ |
| 134 |
struct c140_voice_t *voice = &c219->voice[v]; |
| 135 |
if (voice->busy && voice->keyon) |
| 136 |
{ |
| 137 |
for (int c = 0; c < cycle; c++) |
| 138 |
{ |
| 139 |
voice->frac += voice->freq; |
| 140 |
if (voice->frac > 0xffff) |
| 141 |
{ |
| 142 |
voice->addr += voice->frac >> 16; |
| 143 |
if ((voice->addr >> 1) > voice->end_addr) |
| 144 |
{ |
| 145 |
if (voice->loop) |
| 146 |
{ |
| 147 |
voice->addr = (voice->addr + (voice->loop_addr << 1)) - (voice->end_addr << 1); |
| 148 |
} |
| 149 |
else |
| 150 |
{ |
| 151 |
voice->keyon = false; |
| 152 |
voice->lout = 0; |
| 153 |
voice->rout = 0; |
| 154 |
return; |
| 155 |
} |
| 156 |
} |
| 157 |
if (voice->noise) |
| 158 |
{ |
| 159 |
c219->lfsr = (c219->lfsr >> 1) ^ ((-(c219->lfsr & 1)) & 0xfff6); |
| 160 |
} |
| 161 |
voice->frac &= 0xffff; |
| 162 |
} |
| 163 |
} |
| 164 |
if (!voice->muted) |
| 165 |
{ |
| 166 |
signed int sample = 0; |
| 167 |
if (voice->noise) |
| 168 |
{ |
| 169 |
sample = (signed int)((signed short)(c219->lfsr)); |
| 170 |
} |
| 171 |
else |
| 172 |
{ |
| 173 |
// fetch 8 bit sample |
| 174 |
signed short s1 = c219->sample_mem[((unsigned int)(c219->bank[(v >> 2) & 3]) << 17) | ((voice->addr^1) & 0x1ffff)]; |
| 175 |
signed short s2 = c219->sample_mem[((unsigned int)(c219->bank[(v >> 2) & 3]) << 17) | (((voice->addr + 1) & 0x1ffff)^1)]; |
| 176 |
if (voice->compressed) |
| 177 |
{ |
| 178 |
s1 = c219->mulaw[s1&0xff]; |
| 179 |
s2 = c219->mulaw[s2&0xff]; |
| 180 |
} |
| 181 |
else |
| 182 |
{ |
| 183 |
s1 = (signed short)((signed char)(s1) << 8); |
| 184 |
s2 = (signed short)((signed char)(s2) << 8); |
| 185 |
} |
| 186 |
if (voice->inv_sign) |
| 187 |
{ |
| 188 |
s1 = -s1; |
| 189 |
s2 = -s2; |
| 190 |
} |
| 191 |
// interpolate (originally was >>16, but I had to reduce it to 15 to prevent overflow) |
| 192 |
sample = s1 + (((voice->frac >> 1) * (s2 - s1)) >> 15); |
| 193 |
} |
| 194 |
voice->lout = (voice->inv_lout ? (-sample) : sample) * voice->lvol; |
| 195 |
voice->rout = sample * voice->rvol; |
| 196 |
} |
| 197 |
else |
| 198 |
{ |
| 199 |
voice->lout = 0; |
| 200 |
voice->rout = 0; |
| 201 |
} |
| 202 |
} |
| 203 |
else |
| 204 |
{ |
| 205 |
voice->lout = 0; |
| 206 |
voice->rout = 0; |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
void c140_keyon(struct c140_voice_t *c140_voice) |
| 211 |
{ |
| 212 |
c140_voice->busy = true; |
| 213 |
c140_voice->keyon = true; |
| 214 |
c140_voice->frac = 0; |
| 215 |
c140_voice->addr = c140_voice->start_addr; |
| 216 |
} |
| 217 |
|
| 218 |
void c219_keyon(struct c140_voice_t *c140_voice) |
| 219 |
{ |
| 220 |
c140_voice->busy = true; |
| 221 |
c140_voice->keyon = true; |
| 222 |
c140_voice->frac = 0; |
| 223 |
c140_voice->addr = c140_voice->start_addr << 1; |
| 224 |
} |
| 225 |
|
| 226 |
void c140_init(struct c140_t *c140) |
| 227 |
{ |
| 228 |
// u-law table verified from Wii Virtual Console Arcade Starblade |
| 229 |
for (int i = 0; i < 256; i++) |
| 230 |
{ |
| 231 |
const unsigned char exponent = c140_bitfield(i, 0, 3); |
| 232 |
const unsigned char mantissa = c140_bitfield(i, 3, 4); |
| 233 |
if (c140_bit(i, 7)) |
| 234 |
{ |
| 235 |
c140->mulaw[i] = (signed short)(((exponent ? 0xfe00 : 0xff00) | (mantissa << 4)) |
| 236 |
<< (exponent ? exponent - 1 : 0)); |
| 237 |
} |
| 238 |
else |
| 239 |
{ |
| 240 |
c140->mulaw[i] = (signed short)(((exponent ? 0x100 : 0) | (mantissa << 4)) |
| 241 |
<< (exponent ? exponent - 1 : 0)); |
| 242 |
} |
| 243 |
} |
| 244 |
for (int i = 0; i < 24; i++) |
| 245 |
{ |
| 246 |
c140->voice[i].muted = false; |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
void c219_init(struct c219_t *c219) |
| 251 |
{ |
| 252 |
// u-law table verified from Wii Virtual Console Arcade Knuckle Heads |
| 253 |
for (int i = 0; i < 128; i++) |
| 254 |
{ |
| 255 |
signed int compressed_sample = 0; |
| 256 |
if (i < 16) |
| 257 |
{ |
| 258 |
compressed_sample = 0x20 * i; |
| 259 |
} |
| 260 |
else if (i < 24) |
| 261 |
{ |
| 262 |
compressed_sample = (0x200 + (0x40 * i)) - 0x400; |
| 263 |
} |
| 264 |
else if (i < 48) |
| 265 |
{ |
| 266 |
compressed_sample = (0x400 + (0x80 * i)) - 0xc00; |
| 267 |
} |
| 268 |
else if (i < 100) |
| 269 |
{ |
| 270 |
compressed_sample = (0x1000 + (0x100 * i)) - 0x3000; |
| 271 |
} |
| 272 |
else |
| 273 |
{ |
| 274 |
compressed_sample = (0x4400 + (0x200 * i)) - 0xc800; |
| 275 |
} |
| 276 |
c219->mulaw[i] = compressed_sample; |
| 277 |
c219->mulaw[i + 128] = (~compressed_sample) & 0xffe0; |
| 278 |
} |
| 279 |
for (int i = 0; i < 16; i++) |
| 280 |
{ |
| 281 |
c219->voice[i].muted = false; |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
void c140_reset(struct c140_t *c140) |
| 286 |
{ |
| 287 |
for (int i = 0; i < 24; i++) |
| 288 |
{ |
| 289 |
c140->voice[i].busy = false; |
| 290 |
c140->voice[i].keyon = false; |
| 291 |
c140->voice[i].freq = 0; |
| 292 |
c140->voice[i].bank = 0; |
| 293 |
c140->voice[i].start_addr = 0; |
| 294 |
c140->voice[i].loop_addr = 0; |
| 295 |
c140->voice[i].end_addr = 0; |
| 296 |
c140->voice[i].lvol = 0; |
| 297 |
c140->voice[i].rvol = 0; |
| 298 |
c140->voice[i].compressed = false; |
| 299 |
c140->voice[i].loop = false; |
| 300 |
c140->voice[i].addr = 0; |
| 301 |
c140->voice[i].frac = 0; |
| 302 |
c140->voice[i].lout = 0; |
| 303 |
c140->voice[i].rout = 0; |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
void c219_reset(struct c219_t *c219) |
| 308 |
{ |
| 309 |
for (int i = 0; i < 16; i++) |
| 310 |
{ |
| 311 |
c219->voice[i].busy = false; |
| 312 |
c219->voice[i].keyon = false; |
| 313 |
c219->voice[i].freq = 0; |
| 314 |
c219->voice[i].start_addr = 0; |
| 315 |
c219->voice[i].loop_addr = 0; |
| 316 |
c219->voice[i].end_addr = 0; |
| 317 |
c219->voice[i].lvol = 0; |
| 318 |
c219->voice[i].rvol = 0; |
| 319 |
c219->voice[i].noise = false; |
| 320 |
c219->voice[i].inv_lout = false; |
| 321 |
c219->voice[i].inv_sign = false; |
| 322 |
c219->voice[i].compressed = false; |
| 323 |
c219->voice[i].loop = false; |
| 324 |
c219->voice[i].addr = 0; |
| 325 |
c219->voice[i].frac = 0; |
| 326 |
c219->voice[i].lout = 0; |
| 327 |
c219->voice[i].rout = 0; |
| 328 |
} |
| 329 |
c219->lfsr = 0x1234; |
| 330 |
for (int i = 0; i < 4; i++) |
| 331 |
{ |
| 332 |
c219->bank[i] = 0; |
| 333 |
} |
| 334 |
} |
| 335 |
|
| 336 |
// TILDEARROW |
| 337 |
void c140_bank_type(struct c140_t *c140, unsigned char type) { |
| 338 |
c140->bank_type = type; |
| 339 |
} |
| 340 |
|
| 341 |
void c140_write(struct c140_t *c140, const unsigned short addr, const unsigned char data) |
| 342 |
{ |
| 343 |
// voice register |
| 344 |
if (addr < 0x180) |
| 345 |
{ |
| 346 |
struct c140_voice_t *voice = &c140->voice[addr >> 4]; |
| 347 |
switch (addr & 0xf) |
| 348 |
{ |
| 349 |
case 0x0: voice->rvol = data; break; |
| 350 |
case 0x1: voice->lvol = data; break; |
| 351 |
case 0x2: voice->freq = (voice->freq & ~0xff00) | (unsigned int)(data << 8); break; |
| 352 |
case 0x3: voice->freq = (voice->freq & ~0x00ff) | data; break; |
| 353 |
case 0x4: { // TILDEARROW |
| 354 |
if (c140->bank_type == 0) { |
| 355 |
voice->bank = ((data&0x20)>>2)|(data&7); |
| 356 |
} else if (c140->bank_type == 1) { |
| 357 |
voice->bank = ((data&0x30)>>1)|(data&7); |
| 358 |
} else { |
| 359 |
voice->bank = data; |
| 360 |
} |
| 361 |
break; |
| 362 |
} |
| 363 |
case 0x5: |
| 364 |
voice->compressed = c140_bit(data, 3); |
| 365 |
voice->loop = c140_bit(data, 4); |
| 366 |
if (data & 0x80) |
| 367 |
c140_keyon(voice); |
| 368 |
else |
| 369 |
voice->busy = false; |
| 370 |
break; |
| 371 |
case 0x6: voice->start_addr = (voice->start_addr & ~0xff00) | (unsigned int)(data << 8); break; |
| 372 |
case 0x7: voice->start_addr = (voice->start_addr & ~0x00ff) | data; break; |
| 373 |
case 0x8: voice->end_addr = (voice->end_addr & ~0xff00) | (unsigned int)(data << 8); break; |
| 374 |
case 0x9: voice->end_addr = (voice->end_addr & ~0x00ff) | data; break; |
| 375 |
case 0xa: voice->loop_addr = (voice->loop_addr & ~0xff00) | (unsigned int)(data << 8); break; |
| 376 |
case 0xb: voice->loop_addr = (voice->loop_addr & ~0x00ff) | data; break; |
| 377 |
default: break; |
| 378 |
} |
| 379 |
} |
| 380 |
// Timer |
| 381 |
} |
| 382 |
|
| 383 |
void c219_write(struct c219_t *c219, const unsigned short addr, const unsigned char data) |
| 384 |
{ |
| 385 |
// voice register |
| 386 |
if (addr < 0x180) |
| 387 |
{ |
| 388 |
struct c140_voice_t *voice = &c219->voice[addr >> 4]; |
| 389 |
switch (addr & 0xf) |
| 390 |
{ |
| 391 |
case 0x0: voice->rvol = data; break; |
| 392 |
case 0x1: voice->lvol = data; break; |
| 393 |
case 0x2: voice->freq = (voice->freq & ~0xff00) | (unsigned int)(data << 8); break; |
| 394 |
case 0x3: voice->freq = (voice->freq & ~0x00ff) | data; break; |
| 395 |
//case 0x4: break; // Unknown |
| 396 |
case 0x5: |
| 397 |
voice->compressed = c140_bit(data, 0); |
| 398 |
voice->noise = c140_bit(data, 2); |
| 399 |
voice->inv_lout = c140_bit(data, 3); |
| 400 |
voice->loop = c140_bit(data, 4); |
| 401 |
voice->inv_sign = c140_bit(data, 6); |
| 402 |
if (data & 0x80) |
| 403 |
c219_keyon(voice); |
| 404 |
else |
| 405 |
voice->busy = false; |
| 406 |
break; |
| 407 |
case 0x6: voice->start_addr = (voice->start_addr & ~0xff00) | (unsigned int)(data << 8); break; |
| 408 |
case 0x7: voice->start_addr = (voice->start_addr & ~0x00ff) | data; break; |
| 409 |
case 0x8: voice->end_addr = (voice->end_addr & ~0xff00) | (unsigned int)(data << 8); break; |
| 410 |
case 0x9: voice->end_addr = (voice->end_addr & ~0x00ff) | data; break; |
| 411 |
case 0xa: voice->loop_addr = (voice->loop_addr & ~0xff00) | (unsigned int)(data << 8); break; |
| 412 |
case 0xb: voice->loop_addr = (voice->loop_addr & ~0x00ff) | data; break; |
| 413 |
default: break; |
| 414 |
} |
| 415 |
} |
| 416 |
// bank |
| 417 |
else if (addr >= 0x1f0) |
| 418 |
{ |
| 419 |
if (addr & 1) |
| 420 |
{ |
| 421 |
const unsigned short bankaddr = (addr >> 1) & 3; |
| 422 |
c219->bank[(bankaddr + 1) & 3] = (data & 3); |
| 423 |
} |
| 424 |
} |
| 425 |
} |