| 1 |
#include "5e01_dmc.h" |
| 2 |
#include "5e01_apu.h" |
| 3 |
|
| 4 |
#include "common.h" |
| 5 |
#include <assert.h> |
| 6 |
#include <cstdlib> |
| 7 |
|
| 8 |
namespace xgm |
| 9 |
{ |
| 10 |
const unsigned int I5E01_DMC::wavlen_table[2][32] = { |
| 11 |
{ // NTSC |
| 12 |
4,5,6,7,9,12,15,19,23,29,37,46,58,72,91,114,142,178,222,278,348,435,544,681,851,1064,1331,1664,2081,2602,3253,4068 |
| 13 |
}, |
| 14 |
{ // PAL |
| 15 |
3,4,6,7,9,12,15,18,23,29,36,45,56,70,88,110,137,171,213,266,332,414,517,644,804,1003,1251,1560,1946,2428,3028,3778 |
| 16 |
} }; |
| 17 |
|
| 18 |
const unsigned int I5E01_DMC::freq_table[2][16] = { |
| 19 |
{ // NTSC |
| 20 |
428, 380, 340, 320, 286, 254, 226, 214, 190, 160, 142, 128, 106, 84, 72, 54 |
| 21 |
}, |
| 22 |
{ // PAL |
| 23 |
398, 354, 316, 298, 276, 236, 210, 198, 176, 148, 132, 118, 98, 78, 66, 50 |
| 24 |
} }; |
| 25 |
|
| 26 |
const unsigned int BITREVERSE[256] = { |
| 27 |
0x00, 0x80, 0x40, 0xC0, 0x20, 0xA0, 0x60, 0xE0, 0x10, 0x90, 0x50, 0xD0, 0x30, 0xB0, 0x70, 0xF0, |
| 28 |
0x08, 0x88, 0x48, 0xC8, 0x28, 0xA8, 0x68, 0xE8, 0x18, 0x98, 0x58, 0xD8, 0x38, 0xB8, 0x78, 0xF8, |
| 29 |
0x04, 0x84, 0x44, 0xC4, 0x24, 0xA4, 0x64, 0xE4, 0x14, 0x94, 0x54, 0xD4, 0x34, 0xB4, 0x74, 0xF4, |
| 30 |
0x0C, 0x8C, 0x4C, 0xCC, 0x2C, 0xAC, 0x6C, 0xEC, 0x1C, 0x9C, 0x5C, 0xDC, 0x3C, 0xBC, 0x7C, 0xFC, |
| 31 |
0x02, 0x82, 0x42, 0xC2, 0x22, 0xA2, 0x62, 0xE2, 0x12, 0x92, 0x52, 0xD2, 0x32, 0xB2, 0x72, 0xF2, |
| 32 |
0x0A, 0x8A, 0x4A, 0xCA, 0x2A, 0xAA, 0x6A, 0xEA, 0x1A, 0x9A, 0x5A, 0xDA, 0x3A, 0xBA, 0x7A, 0xFA, |
| 33 |
0x06, 0x86, 0x46, 0xC6, 0x26, 0xA6, 0x66, 0xE6, 0x16, 0x96, 0x56, 0xD6, 0x36, 0xB6, 0x76, 0xF6, |
| 34 |
0x0E, 0x8E, 0x4E, 0xCE, 0x2E, 0xAE, 0x6E, 0xEE, 0x1E, 0x9E, 0x5E, 0xDE, 0x3E, 0xBE, 0x7E, 0xFE, |
| 35 |
0x01, 0x81, 0x41, 0xC1, 0x21, 0xA1, 0x61, 0xE1, 0x11, 0x91, 0x51, 0xD1, 0x31, 0xB1, 0x71, 0xF1, |
| 36 |
0x09, 0x89, 0x49, 0xC9, 0x29, 0xA9, 0x69, 0xE9, 0x19, 0x99, 0x59, 0xD9, 0x39, 0xB9, 0x79, 0xF9, |
| 37 |
0x05, 0x85, 0x45, 0xC5, 0x25, 0xA5, 0x65, 0xE5, 0x15, 0x95, 0x55, 0xD5, 0x35, 0xB5, 0x75, 0xF5, |
| 38 |
0x0D, 0x8D, 0x4D, 0xCD, 0x2D, 0xAD, 0x6D, 0xED, 0x1D, 0x9D, 0x5D, 0xDD, 0x3D, 0xBD, 0x7D, 0xFD, |
| 39 |
0x03, 0x83, 0x43, 0xC3, 0x23, 0xA3, 0x63, 0xE3, 0x13, 0x93, 0x53, 0xD3, 0x33, 0xB3, 0x73, 0xF3, |
| 40 |
0x0B, 0x8B, 0x4B, 0xCB, 0x2B, 0xAB, 0x6B, 0xEB, 0x1B, 0x9B, 0x5B, 0xDB, 0x3B, 0xBB, 0x7B, 0xFB, |
| 41 |
0x07, 0x87, 0x47, 0xC7, 0x27, 0xA7, 0x67, 0xE7, 0x17, 0x97, 0x57, 0xD7, 0x37, 0xB7, 0x77, 0xF7, |
| 42 |
0x0F, 0x8F, 0x4F, 0xCF, 0x2F, 0xAF, 0x6F, 0xEF, 0x1F, 0x9F, 0x5F, 0xDF, 0x3F, 0xBF, 0x7F, 0xFF, |
| 43 |
}; |
| 44 |
|
| 45 |
I5E01_DMC::I5E01_DMC() : GETA_BITS(20) |
| 46 |
{ |
| 47 |
SetClock(DEFAULT_CLOCK); |
| 48 |
SetRate(DEFAULT_RATE); |
| 49 |
SetPal(false); |
| 50 |
option[OPT_ENABLE_4011] = 1; |
| 51 |
option[OPT_ENABLE_PNOISE] = 1; |
| 52 |
option[OPT_UNMUTE_ON_RESET] = 1; |
| 53 |
option[OPT_DPCM_ANTI_CLICK] = 0; |
| 54 |
option[OPT_NONLINEAR_MIXER] = 1; |
| 55 |
option[OPT_RANDOMIZE_NOISE] = 1; |
| 56 |
option[OPT_RANDOMIZE_TRI] = 1; |
| 57 |
option[OPT_TRI_MUTE] = 1; |
| 58 |
option[OPT_DPCM_REVERSE] = 0; |
| 59 |
tnd_table[0][0][0][0] = 0; |
| 60 |
tnd_table[1][0][0][0] = 0; |
| 61 |
|
| 62 |
apu = NULL; |
| 63 |
frame_sequence_count = 0; |
| 64 |
frame_sequence_length = 7458; |
| 65 |
frame_sequence_steps = 4; |
| 66 |
|
| 67 |
for (int c = 0;c < 2;++c) |
| 68 |
for (int t = 0;t < 3;++t) |
| 69 |
sm[c][t] = 128; |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
I5E01_DMC::~I5E01_DMC() |
| 74 |
{ |
| 75 |
} |
| 76 |
|
| 77 |
void I5E01_DMC::SetStereoMix(int trk, short mixl, short mixr) |
| 78 |
{ |
| 79 |
if (trk < 0) return; |
| 80 |
if (trk > 2) return; |
| 81 |
sm[0][trk] = mixl; |
| 82 |
sm[1][trk] = mixr; |
| 83 |
} |
| 84 |
|
| 85 |
void I5E01_DMC::FrameSequence(int s) |
| 86 |
{ |
| 87 |
//DEBUG_OUT("FrameSequence: %d\n",s); |
| 88 |
|
| 89 |
if (s > 3) return; // no operation in step 4 |
| 90 |
|
| 91 |
if (apu) |
| 92 |
{ |
| 93 |
apu->FrameSequence(s); |
| 94 |
} |
| 95 |
|
| 96 |
if (s == 0 && (frame_sequence_steps == 4)) |
| 97 |
{ |
| 98 |
if (frame_irq_enable) frame_irq = true; |
| 99 |
} |
| 100 |
|
| 101 |
// 240hz clock |
| 102 |
{ |
| 103 |
// triangle linear counter |
| 104 |
if (linear_counter_halt) |
| 105 |
{ |
| 106 |
linear_counter = linear_counter_reload; |
| 107 |
} |
| 108 |
else |
| 109 |
{ |
| 110 |
if (linear_counter > 0) --linear_counter; |
| 111 |
} |
| 112 |
if (!linear_counter_control) |
| 113 |
{ |
| 114 |
linear_counter_halt = false; |
| 115 |
} |
| 116 |
|
| 117 |
// noise envelope |
| 118 |
bool divider = false; |
| 119 |
if (envelope_write) |
| 120 |
{ |
| 121 |
envelope_write = false; |
| 122 |
envelope_counter = 15; |
| 123 |
envelope_div = 0; |
| 124 |
} |
| 125 |
else |
| 126 |
{ |
| 127 |
++envelope_div; |
| 128 |
if (envelope_div > envelope_div_period) |
| 129 |
{ |
| 130 |
divider = true; |
| 131 |
envelope_div = 0; |
| 132 |
} |
| 133 |
} |
| 134 |
if (divider) |
| 135 |
{ |
| 136 |
if (envelope_loop && envelope_counter == 0) |
| 137 |
envelope_counter = 15; |
| 138 |
else if (envelope_counter > 0) |
| 139 |
--envelope_counter; |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
// 120hz clock |
| 144 |
if ((s & 1) == 0) |
| 145 |
{ |
| 146 |
// triangle length counter |
| 147 |
if (!linear_counter_control && (length_counter[0] > 0)) |
| 148 |
--length_counter[0]; |
| 149 |
|
| 150 |
// noise length counter |
| 151 |
if (!envelope_loop && (length_counter[1] > 0)) |
| 152 |
--length_counter[1]; |
| 153 |
} |
| 154 |
|
| 155 |
} |
| 156 |
|
| 157 |
// 三角波チャンネルの計算 戻り値は0-15 |
| 158 |
unsigned int I5E01_DMC::calc_tri(unsigned int clocks) |
| 159 |
{ |
| 160 |
static unsigned int wavtbl[4][32] = |
| 161 |
{ |
| 162 |
{ |
| 163 |
15,14,13,12,11,10, 9, 8, |
| 164 |
7, 6, 5, 4, 3, 2, 1, 0, |
| 165 |
0, 1, 2, 3, 4, 5, 6, 7, |
| 166 |
8, 9,10,11,12,13,14,15, |
| 167 |
}, |
| 168 |
{ |
| 169 |
11,11,11,11,10,10,10,10, |
| 170 |
9, 9, 9, 9, 8, 8, 8, 8, |
| 171 |
7, 7, 7, 7, 6, 6, 6, 6, |
| 172 |
5, 5, 5, 5, 4, 4, 4, 4, |
| 173 |
}, |
| 174 |
{ |
| 175 |
11,11,11,11,11,11,11,11, |
| 176 |
11,11,11,11,11,11,11,11, |
| 177 |
4, 4, 4, 4, 4, 4, 4, 4, |
| 178 |
4, 4, 4, 4, 4, 4, 4, 4, |
| 179 |
}, |
| 180 |
{ |
| 181 |
8, 9,10,12,13,14,14,15, |
| 182 |
15,15,14,14,13,12,10, 9, |
| 183 |
8, 6, 5, 3, 2, 1, 1, 0, |
| 184 |
0, 0, 1, 1, 2, 3, 5, 6, |
| 185 |
}, |
| 186 |
}; |
| 187 |
|
| 188 |
if (linear_counter > 0 && length_counter[0] > 0 |
| 189 |
&& (!option[OPT_TRI_MUTE] || tri_freq > 0)) |
| 190 |
{ |
| 191 |
counter[0] -= clocks; |
| 192 |
while (counter[0] < 0) |
| 193 |
{ |
| 194 |
tphase = (tphase + 1) & 31; |
| 195 |
counter[0] += (tri_freq + 1); |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
unsigned int ret = wavtbl[tduty][tphase]; |
| 200 |
return ret; |
| 201 |
} |
| 202 |
|
| 203 |
// ノイズチャンネルの計算 戻り値は0-127 |
| 204 |
// 低サンプリングレートで合成するとエイリアスノイズが激しいので |
| 205 |
// ノイズだけはこの関数内で高クロック合成し、簡易なサンプリングレート |
| 206 |
// 変換を行っている。 |
| 207 |
unsigned int I5E01_DMC::calc_noise(unsigned int clocks) |
| 208 |
{ |
| 209 |
unsigned int env = envelope_disable ? noise_volume : envelope_counter; |
| 210 |
if (length_counter[1] < 1) env = 0; |
| 211 |
|
| 212 |
unsigned int last = (noise & 0x4000) ? 0 : env; |
| 213 |
if (clocks < 1) return last; |
| 214 |
|
| 215 |
// simple anti-aliasing (noise requires it, even when oversampling is off) |
| 216 |
unsigned int count = 0; |
| 217 |
unsigned int accum = counter[1] * last; // samples pending from previous calc |
| 218 |
unsigned int accum_clocks = counter[1]; |
| 219 |
#ifdef _DEBUG |
| 220 |
int start_clocks = counter[1]; |
| 221 |
#endif |
| 222 |
if (counter[1] < 0) // only happens on startup when using the randomize noise option |
| 223 |
{ |
| 224 |
accum = 0; |
| 225 |
accum_clocks = 0; |
| 226 |
} |
| 227 |
|
| 228 |
counter[1] -= clocks; |
| 229 |
assert(nfreq > 0); // prevent infinite loop |
| 230 |
while (counter[1] < 0) |
| 231 |
{ |
| 232 |
// tick the noise generator |
| 233 |
unsigned int feedback = (noise & 1) ^ ((noise & noise_tap) ? 1 : 0); |
| 234 |
noise = (noise >> 1) | (feedback << 14); |
| 235 |
|
| 236 |
last = (noise & 0x4000) ? 0 : env; |
| 237 |
accum += (last * nfreq); |
| 238 |
counter[1] += nfreq; |
| 239 |
++count; |
| 240 |
accum_clocks += nfreq; |
| 241 |
} |
| 242 |
|
| 243 |
if (count < 1) // no change over interval, don't anti-alias |
| 244 |
{ |
| 245 |
return last; |
| 246 |
} |
| 247 |
|
| 248 |
accum -= (last * counter[1]); // remove these samples which belong in the next calc |
| 249 |
accum_clocks -= counter[1]; |
| 250 |
#ifdef _DEBUG |
| 251 |
if (start_clocks >= 0) assert(accum_clocks == clocks); // these should be equal |
| 252 |
#endif |
| 253 |
|
| 254 |
unsigned int average = accum / accum_clocks; |
| 255 |
assert(average <= 15); // above this would indicate overflow |
| 256 |
return average; |
| 257 |
} |
| 258 |
|
| 259 |
// Tick the DMC for the number of clocks, and return output counter; |
| 260 |
unsigned int I5E01_DMC::calc_dmc(unsigned int clocks) |
| 261 |
{ |
| 262 |
counter[2] -= clocks; |
| 263 |
assert(dfreq > 0); // prevent infinite loop |
| 264 |
while (counter[2] < 0) |
| 265 |
{ |
| 266 |
counter[2] += dfreq; |
| 267 |
|
| 268 |
if (data > 0x100) // data = 0x100 when shift register is empty |
| 269 |
{ |
| 270 |
if (!empty) |
| 271 |
{ |
| 272 |
if ((data & 1) && (damp < 63)) |
| 273 |
damp++; |
| 274 |
else if (!(data & 1) && (0 < damp)) |
| 275 |
damp--; |
| 276 |
} |
| 277 |
data >>= 1; |
| 278 |
} |
| 279 |
|
| 280 |
if (data <= 0x100) // shift register is empty |
| 281 |
{ |
| 282 |
if (dlength > 0) |
| 283 |
{ |
| 284 |
memory(daddress, data); |
| 285 |
// (checking for the 3-cycle case would require sub-instruction emulation) |
| 286 |
data &= 0xFF; // read 8 bits |
| 287 |
if (option[OPT_DPCM_REVERSE]) data = BITREVERSE[data]; |
| 288 |
data |= 0x10000; // use an extra bit to signal end of data |
| 289 |
empty = false; |
| 290 |
daddress = ((daddress + 1) & 0xFFFF) | 0x8000; |
| 291 |
--dlength; |
| 292 |
if (dlength == 0) |
| 293 |
{ |
| 294 |
if (mode & 1) // looped DPCM = auto-reload |
| 295 |
{ |
| 296 |
daddress = ((adr_reg << 6) | 0xC000); |
| 297 |
dlength = (len_reg << 4) + 1; |
| 298 |
} |
| 299 |
else if (mode & 2) // IRQ and not looped |
| 300 |
{ |
| 301 |
irq = true; |
| 302 |
} |
| 303 |
} |
| 304 |
} |
| 305 |
else |
| 306 |
{ |
| 307 |
data = 0x10000; // DMC will do nothing |
| 308 |
empty = true; |
| 309 |
} |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
return (damp << 1) + dac_lsb; |
| 314 |
} |
| 315 |
|
| 316 |
void I5E01_DMC::TickFrameSequence(unsigned int clocks) |
| 317 |
{ |
| 318 |
frame_sequence_count += clocks; |
| 319 |
while (frame_sequence_count > frame_sequence_length) |
| 320 |
{ |
| 321 |
FrameSequence(frame_sequence_step); |
| 322 |
frame_sequence_count -= frame_sequence_length; |
| 323 |
++frame_sequence_step; |
| 324 |
if (frame_sequence_step >= frame_sequence_steps) |
| 325 |
frame_sequence_step = 0; |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
void I5E01_DMC::Tick(unsigned int clocks) |
| 330 |
{ |
| 331 |
out[0] = calc_tri(clocks); |
| 332 |
out[1] = calc_noise(clocks); |
| 333 |
out[2] = calc_dmc(clocks); |
| 334 |
} |
| 335 |
|
| 336 |
unsigned int I5E01_DMC::Render(int b[2]) |
| 337 |
{ |
| 338 |
out[0] = (mask & 1) ? 0 : out[0]; |
| 339 |
out[1] = (mask & 2) ? 0 : out[1]; |
| 340 |
out[2] = (mask & 4) ? 0 : out[2]; |
| 341 |
|
| 342 |
int m[3]; |
| 343 |
m[0] = tnd_table[0][out[0]][0][0]; |
| 344 |
m[1] = tnd_table[0][0][out[1]][0]; |
| 345 |
m[2] = tnd_table[0][0][0][out[2]]; |
| 346 |
|
| 347 |
if (option[OPT_NONLINEAR_MIXER]) |
| 348 |
{ |
| 349 |
int ref = m[0] + m[1] + m[2]; |
| 350 |
int voltage = tnd_table[1][out[0]][out[1]][out[2]]; |
| 351 |
if (ref) |
| 352 |
{ |
| 353 |
for (int i = 0; i < 3; ++i) |
| 354 |
m[i] = (m[i] * voltage) / ref; |
| 355 |
} |
| 356 |
else |
| 357 |
{ |
| 358 |
for (int i = 0; i < 3; ++i) |
| 359 |
m[i] = voltage; |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
// anti-click nullifies any 4011 write but preserves nonlinearity |
| 364 |
if (option[OPT_DPCM_ANTI_CLICK]) |
| 365 |
{ |
| 366 |
if (dmc_pop) // $4011 will cause pop this frame |
| 367 |
{ |
| 368 |
// adjust offset to counteract pop |
| 369 |
dmc_pop_offset += dmc_pop_follow - m[2]; |
| 370 |
dmc_pop = false; |
| 371 |
|
| 372 |
// prevent overflow, keep headspace at edges |
| 373 |
const int OFFSET_MAX = (1 << 30) - (4 << 16); |
| 374 |
if (dmc_pop_offset > OFFSET_MAX) dmc_pop_offset = OFFSET_MAX; |
| 375 |
if (dmc_pop_offset < -OFFSET_MAX) dmc_pop_offset = -OFFSET_MAX; |
| 376 |
} |
| 377 |
dmc_pop_follow = m[2]; // remember previous position |
| 378 |
|
| 379 |
m[2] += dmc_pop_offset; // apply offset |
| 380 |
|
| 381 |
// TODO implement this in a better way |
| 382 |
// roll off offset (not ideal, but prevents overflow) |
| 383 |
if (dmc_pop_offset > 0) --dmc_pop_offset; |
| 384 |
else if (dmc_pop_offset < 0) ++dmc_pop_offset; |
| 385 |
} |
| 386 |
|
| 387 |
b[0] = m[0] * sm[0][0]; |
| 388 |
b[0] += m[1] * sm[0][1]; |
| 389 |
b[0] += m[2] * sm[0][2]; |
| 390 |
b[0] >>= 7; |
| 391 |
|
| 392 |
b[1] = m[0] * sm[1][0]; |
| 393 |
b[1] += m[1] * sm[1][1]; |
| 394 |
b[1] += m[2] * sm[1][2]; |
| 395 |
b[1] >>= 7; |
| 396 |
|
| 397 |
return 2; |
| 398 |
} |
| 399 |
|
| 400 |
void I5E01_DMC::SetClock(double c) |
| 401 |
{ |
| 402 |
clock = c; |
| 403 |
} |
| 404 |
|
| 405 |
void I5E01_DMC::SetRate(double r) |
| 406 |
{ |
| 407 |
rate = (unsigned int)(r ? r : DEFAULT_RATE); |
| 408 |
} |
| 409 |
|
| 410 |
void I5E01_DMC::SetPal(bool is_pal) |
| 411 |
{ |
| 412 |
pal = (is_pal ? 1 : 0); |
| 413 |
// set CPU cycles in frame_sequence |
| 414 |
frame_sequence_length = is_pal ? 8314 : 7458; |
| 415 |
} |
| 416 |
|
| 417 |
void I5E01_DMC::SetAPU(I5E01_APU* apu_) |
| 418 |
{ |
| 419 |
apu = apu_; |
| 420 |
} |
| 421 |
|
| 422 |
// Initializing TRI, NOISE, DPCM mixing table |
| 423 |
void I5E01_DMC::InitializeTNDTable(double wt, double wn, double wd) { |
| 424 |
|
| 425 |
// volume adjusted by 0.95 based on empirical measurements |
| 426 |
const double MASTER = 8192.0 * 0.95; |
| 427 |
// truthfully, the nonlinear curve does not appear to match well |
| 428 |
// with my tests. Do more testing of the APU/DMC DAC later. |
| 429 |
// this value keeps the triangle consistent with measured levels, |
| 430 |
// but not necessarily the rest of this APU channel, |
| 431 |
// because of the lack of a good DAC model, currently. |
| 432 |
|
| 433 |
{ // Linear Mixer |
| 434 |
for (int t = 0; t < 16; t++) { |
| 435 |
for (int n = 0; n < 16; n++) { |
| 436 |
for (int d = 0; d < 128; d++) { |
| 437 |
tnd_table[0][t][n][d] = (unsigned int)(MASTER * (3.0 * t + 2.0 * n + d) / 208.0); |
| 438 |
} |
| 439 |
} |
| 440 |
} |
| 441 |
} |
| 442 |
{ // Non-Linear Mixer |
| 443 |
tnd_table[1][0][0][0] = 0; |
| 444 |
for (int t = 0; t < 16; t++) { |
| 445 |
for (int n = 0; n < 16; n++) { |
| 446 |
for (int d = 0; d < 128; d++) { |
| 447 |
if (t != 0 || n != 0 || d != 0) |
| 448 |
tnd_table[1][t][n][d] = (unsigned int)((MASTER * 159.79) / (100.0 + 1.0 / ((double)t / wt + (double)n / wn + (double)d / wd))); |
| 449 |
} |
| 450 |
} |
| 451 |
} |
| 452 |
} |
| 453 |
|
| 454 |
} |
| 455 |
|
| 456 |
void I5E01_DMC::Reset() |
| 457 |
{ |
| 458 |
int i; |
| 459 |
mask = 0; |
| 460 |
|
| 461 |
InitializeTNDTable(8227, 12241, 22638); |
| 462 |
|
| 463 |
counter[0] = 0; |
| 464 |
counter[1] = 0; |
| 465 |
counter[2] = 0; |
| 466 |
tphase = 0; |
| 467 |
tduty = 0; |
| 468 |
nfreq = wavlen_table[0][0]; |
| 469 |
dfreq = freq_table[0][0]; |
| 470 |
tri_freq = 0; |
| 471 |
linear_counter = 0; |
| 472 |
linear_counter_reload = 0; |
| 473 |
linear_counter_halt = 0; |
| 474 |
linear_counter_control = 0; |
| 475 |
noise_volume = 0; |
| 476 |
noise = 0; |
| 477 |
noise_tap = 0; |
| 478 |
envelope_loop = 0; |
| 479 |
envelope_disable = 0; |
| 480 |
envelope_write = 0; |
| 481 |
envelope_div_period = 0; |
| 482 |
envelope_div = 0; |
| 483 |
envelope_counter = 0; |
| 484 |
enable[0] = 0; |
| 485 |
enable[1] = 0; |
| 486 |
length_counter[0] = 0; |
| 487 |
length_counter[1] = 0; |
| 488 |
frame_irq = false; |
| 489 |
frame_irq_enable = false; |
| 490 |
frame_sequence_count = 0; |
| 491 |
frame_sequence_steps = 4; |
| 492 |
frame_sequence_step = 0; |
| 493 |
|
| 494 |
for (i = 0; i < 0x0F; i++) |
| 495 |
Write(0x4108 + i, 0); |
| 496 |
Write(0x4117, 0x40); |
| 497 |
|
| 498 |
irq = false; |
| 499 |
Write(0x4115, 0x00); |
| 500 |
if (option[OPT_UNMUTE_ON_RESET]) |
| 501 |
Write(0x4115, 0x0f); |
| 502 |
|
| 503 |
out[0] = out[1] = out[2] = 0; |
| 504 |
damp = 0; |
| 505 |
dmc_pop = false; |
| 506 |
dmc_pop_offset = 0; |
| 507 |
dmc_pop_follow = 0; |
| 508 |
dac_lsb = 0; |
| 509 |
data = 0x100; |
| 510 |
empty = true; |
| 511 |
adr_reg = 0; |
| 512 |
dlength = 0; |
| 513 |
len_reg = 0; |
| 514 |
daddress = 0; |
| 515 |
noise = 1; |
| 516 |
noise_tap = (1 << 1); |
| 517 |
|
| 518 |
SetRate(rate); |
| 519 |
} |
| 520 |
|
| 521 |
void I5E01_DMC::SetMemory(std::function<void(unsigned short, unsigned int&)> r) |
| 522 |
{ |
| 523 |
memory = r; |
| 524 |
} |
| 525 |
|
| 526 |
void I5E01_DMC::SetOption(int id, int val) |
| 527 |
{ |
| 528 |
if (id < OPT_END) |
| 529 |
{ |
| 530 |
option[id] = val; |
| 531 |
if (id == OPT_NONLINEAR_MIXER) |
| 532 |
InitializeTNDTable(8227, 12241, 22638); |
| 533 |
} |
| 534 |
} |
| 535 |
|
| 536 |
bool I5E01_DMC::Write(unsigned int adr, unsigned int val, unsigned int id) |
| 537 |
{ |
| 538 |
static const unsigned char length_table[32] = { |
| 539 |
0x0A, 0xFE, |
| 540 |
0x14, 0x02, |
| 541 |
0x28, 0x04, |
| 542 |
0x50, 0x06, |
| 543 |
0xA0, 0x08, |
| 544 |
0x3C, 0x0A, |
| 545 |
0x0E, 0x0C, |
| 546 |
0x1A, 0x0E, |
| 547 |
0x0C, 0x10, |
| 548 |
0x18, 0x12, |
| 549 |
0x30, 0x14, |
| 550 |
0x60, 0x16, |
| 551 |
0xC0, 0x18, |
| 552 |
0x48, 0x1A, |
| 553 |
0x10, 0x1C, |
| 554 |
0x20, 0x1E |
| 555 |
}; |
| 556 |
|
| 557 |
// hack |
| 558 |
adr+=0x100; |
| 559 |
|
| 560 |
if (adr == 0x4115) |
| 561 |
{ |
| 562 |
enable[0] = (val & 4) ? true : false; |
| 563 |
enable[1] = (val & 8) ? true : false; |
| 564 |
|
| 565 |
if (!enable[0]) |
| 566 |
{ |
| 567 |
length_counter[0] = 0; |
| 568 |
} |
| 569 |
if (!enable[1]) |
| 570 |
{ |
| 571 |
length_counter[1] = 0; |
| 572 |
} |
| 573 |
|
| 574 |
if ((val & 16) && dlength == 0) |
| 575 |
{ |
| 576 |
daddress = (0xC000 | (adr_reg << 6)); |
| 577 |
dlength = (len_reg << 4) + 1; |
| 578 |
} |
| 579 |
else if (!(val & 16)) |
| 580 |
{ |
| 581 |
dlength = 0; |
| 582 |
} |
| 583 |
|
| 584 |
irq = false; |
| 585 |
|
| 586 |
reg[adr - 0x4108] = val; |
| 587 |
return true; |
| 588 |
} |
| 589 |
|
| 590 |
if (adr == 0x4117) |
| 591 |
{ |
| 592 |
//DEBUG_OUT("4017 = %02X\n", val); |
| 593 |
frame_irq_enable = ((val & 0x40) != 0x40); |
| 594 |
if (frame_irq_enable) frame_irq = false; |
| 595 |
|
| 596 |
frame_sequence_count = 0; |
| 597 |
if (val & 0x80) |
| 598 |
{ |
| 599 |
frame_sequence_steps = 5; |
| 600 |
frame_sequence_step = 0; |
| 601 |
FrameSequence(frame_sequence_step); |
| 602 |
++frame_sequence_step; |
| 603 |
} |
| 604 |
else |
| 605 |
{ |
| 606 |
frame_sequence_steps = 4; |
| 607 |
frame_sequence_step = 1; |
| 608 |
} |
| 609 |
} |
| 610 |
|
| 611 |
if (adr < 0x4108 || 0x4113 < adr) |
| 612 |
return false; |
| 613 |
|
| 614 |
reg[adr - 0x4108] = val & 0xff; |
| 615 |
|
| 616 |
//DEBUG_OUT("$%04X %02X\n", adr, val); |
| 617 |
|
| 618 |
switch (adr) |
| 619 |
{ |
| 620 |
|
| 621 |
// tri |
| 622 |
|
| 623 |
case 0x4108: |
| 624 |
linear_counter_control = (val >> 7) & 1; |
| 625 |
linear_counter_reload = val & 0x7F; |
| 626 |
break; |
| 627 |
|
| 628 |
case 0x4109: |
| 629 |
tduty = val & 3; |
| 630 |
break; |
| 631 |
|
| 632 |
case 0x410a: |
| 633 |
tri_freq = val | (tri_freq & 0x700); |
| 634 |
break; |
| 635 |
|
| 636 |
case 0x410b: |
| 637 |
tri_freq = (tri_freq & 0xff) | ((val & 0x7) << 8); |
| 638 |
linear_counter_halt = true; |
| 639 |
if (enable[0]) |
| 640 |
{ |
| 641 |
length_counter[0] = length_table[(val >> 3) & 0x1f]; |
| 642 |
} |
| 643 |
break; |
| 644 |
|
| 645 |
// noise |
| 646 |
|
| 647 |
case 0x410c: |
| 648 |
noise_volume = val & 15; |
| 649 |
envelope_div_period = val & 15; |
| 650 |
envelope_disable = (val >> 4) & 1; |
| 651 |
envelope_loop = (val >> 5) & 1; |
| 652 |
break; |
| 653 |
|
| 654 |
case 0x410d: |
| 655 |
break; |
| 656 |
|
| 657 |
case 0x410e: |
| 658 |
if (option[OPT_ENABLE_PNOISE]) |
| 659 |
noise_tap = (val & 0x80) ? (1 << 6) : (1 << 1); |
| 660 |
else |
| 661 |
noise_tap = (1 << 1); |
| 662 |
nfreq = wavlen_table[pal][val & 31]; |
| 663 |
break; |
| 664 |
|
| 665 |
case 0x410f: |
| 666 |
if (enable[1]) |
| 667 |
{ |
| 668 |
length_counter[1] = length_table[(val >> 3) & 0xf]; |
| 669 |
} |
| 670 |
envelope_write = true; |
| 671 |
break; |
| 672 |
|
| 673 |
// dmc |
| 674 |
|
| 675 |
case 0x4110: |
| 676 |
mode = (val >> 6) & 3; |
| 677 |
if (!(mode & 2)) |
| 678 |
{ |
| 679 |
irq = false; |
| 680 |
} |
| 681 |
dfreq = freq_table[pal][val & 15]; |
| 682 |
break; |
| 683 |
|
| 684 |
case 0x4111: |
| 685 |
if (option[OPT_ENABLE_4011]) |
| 686 |
{ |
| 687 |
damp = (val >> 1) & 0x3f; |
| 688 |
dac_lsb = val & 1; |
| 689 |
dmc_pop = true; |
| 690 |
} |
| 691 |
break; |
| 692 |
|
| 693 |
case 0x4112: |
| 694 |
adr_reg = val & 0xff; |
| 695 |
// ここでdaddressは更新されない |
| 696 |
break; |
| 697 |
|
| 698 |
case 0x4113: |
| 699 |
len_reg = val & 0xff; |
| 700 |
// ここでlengthは更新されない |
| 701 |
break; |
| 702 |
|
| 703 |
default: |
| 704 |
return false; |
| 705 |
} |
| 706 |
|
| 707 |
return true; |
| 708 |
} |
| 709 |
|
| 710 |
bool I5E01_DMC::Read(unsigned int adr, unsigned int& val, unsigned int id) |
| 711 |
{ |
| 712 |
if (adr == 0x4115) |
| 713 |
{ |
| 714 |
val |= (irq ? 0x80 : 0) |
| 715 |
| (frame_irq ? 0x40 : 0) |
| 716 |
| ((dlength > 0) ? 0x10 : 0) |
| 717 |
| (length_counter[1] ? 0x08 : 0) |
| 718 |
| (length_counter[0] ? 0x04 : 0) |
| 719 |
; |
| 720 |
|
| 721 |
frame_irq = false; |
| 722 |
return true; |
| 723 |
} |
| 724 |
else if (0x4108 <= adr && adr <= 0x4114) |
| 725 |
{ |
| 726 |
val |= reg[adr - 0x4108]; |
| 727 |
return true; |
| 728 |
} |
| 729 |
else |
| 730 |
return false; |
| 731 |
} |
| 732 |
} // namespace |