| 1 |
/** |
| 2 |
* Furnace Tracker - multi-system chiptune tracker |
| 3 |
* Copyright (C) 2021-2025 tildearrow and contributors |
| 4 |
* |
| 5 |
* This program is free software; you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation; either version 2 of the License, or |
| 8 |
* (at your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License along |
| 16 |
* with this program; if not, write to the Free Software Foundation, Inc., |
| 17 |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 |
*/ |
| 19 |
|
| 20 |
#include "nes.h" |
| 21 |
#include "sound/nes/cpu_inline.h" |
| 22 |
#include "../engine.h" |
| 23 |
#include "../../ta-log.h" |
| 24 |
#include <stddef.h> |
| 25 |
#include <math.h> |
| 26 |
|
| 27 |
struct _nla_table nla_table; |
| 28 |
|
| 29 |
#define CHIP_DIVIDER 16 |
| 30 |
|
| 31 |
#define rWrite(a,v) if (!skipRegisterWrites) {writes.push(QueuedWrite((a),v)); if (dumpWrites) {addWrite((a),v);} } |
| 32 |
|
| 33 |
const char* regCheatSheetNES[]={ |
| 34 |
"S0Volume", "4000", |
| 35 |
"S0Sweep", "4001", |
| 36 |
"S0PeriodL", "4002", |
| 37 |
"S0PeriodH", "4003", |
| 38 |
"S1Volume", "4004", |
| 39 |
"S1Sweep", "4005", |
| 40 |
"S1PeriodL", "4006", |
| 41 |
"S1PeriodH", "4007", |
| 42 |
"TRVolume", "4008", |
| 43 |
"TRPeriodL", "400A", |
| 44 |
"TRPeriodH", "400B", |
| 45 |
"NSVolume", "400C", |
| 46 |
"NSPeriod", "400E", |
| 47 |
"NSLength", "400F", |
| 48 |
"DMCControl", "4010", |
| 49 |
"DMCLoad", "4011", |
| 50 |
"DMCAddr", "4012", |
| 51 |
"DMCLength", "4013", |
| 52 |
"APUControl", "4015", |
| 53 |
"APUFrameCtl", "4017", |
| 54 |
NULL |
| 55 |
}; |
| 56 |
|
| 57 |
unsigned char _readDMC(void* user, unsigned short addr) { |
| 58 |
return ((DivPlatformNES*)user)->readDMC(addr); |
| 59 |
} |
| 60 |
|
| 61 |
const char** DivPlatformNES::getRegisterSheet() { |
| 62 |
return regCheatSheetNES; |
| 63 |
} |
| 64 |
|
| 65 |
void DivPlatformNES::doWrite(int ts, unsigned short addr, unsigned char data) { |
| 66 |
if (useNP) { |
| 67 |
if (isE) { |
| 68 |
e1_NP->Write(addr,data); |
| 69 |
e2_NP->Write(addr,data); |
| 70 |
} else { |
| 71 |
nes1_NP->Write(addr,data); |
| 72 |
nes2_NP->Write(addr,data); |
| 73 |
} |
| 74 |
} else { |
| 75 |
apu_wr_reg(nes,ts,addr,data); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
#define doPCM \ |
| 80 |
if (!dpcmMode && dacSample!=-1) { \ |
| 81 |
dacPeriod+=dacRate*pcmAdvance; \ |
| 82 |
if (dacPeriod>=rate) { \ |
| 83 |
DivSample* s=parent->getSample(dacSample); \ |
| 84 |
if (s->samples>0 && dacPos<s->samples) { \ |
| 85 |
if (!isMuted[4]) { \ |
| 86 |
unsigned char next=((unsigned char)s->data8[dacPos]+0x80)>>1; \ |
| 87 |
if (dacAntiClickOn && dacAntiClick<next) { \ |
| 88 |
dacAntiClick+=8; \ |
| 89 |
doWrite(i,0x4011,dacAntiClick); \ |
| 90 |
} else { \ |
| 91 |
dacAntiClickOn=false; \ |
| 92 |
doWrite(i,0x4011,next); \ |
| 93 |
} \ |
| 94 |
} \ |
| 95 |
dacPos++; \ |
| 96 |
if (s->isLoopable() && dacPos>=(unsigned int)s->loopEnd) { \ |
| 97 |
dacPos=s->loopStart; \ |
| 98 |
} else if (dacPos>=s->samples) { \ |
| 99 |
dacSample=-1; \ |
| 100 |
} \ |
| 101 |
dacPeriod-=rate; \ |
| 102 |
} else { \ |
| 103 |
dacSample=-1; \ |
| 104 |
} \ |
| 105 |
} \ |
| 106 |
} |
| 107 |
|
| 108 |
void DivPlatformNES::acquire_puNES(blip_buffer_t** bb, size_t len) { |
| 109 |
for (int i=0; i<5; i++) { |
| 110 |
oscBuf[i]->begin(len); |
| 111 |
nes->oscBuf[i]=oscBuf[i]; |
| 112 |
} |
| 113 |
|
| 114 |
nes->timestamp=0; |
| 115 |
nes->bb=bb[0]; |
| 116 |
|
| 117 |
for (size_t i=0; i<len; i++) { |
| 118 |
// heuristic |
| 119 |
int pcmAdvance=1; |
| 120 |
if (writes.empty()) { |
| 121 |
if (dpcmMode || dacSample==-1) { |
| 122 |
break; |
| 123 |
} else { |
| 124 |
pcmAdvance=len-i; |
| 125 |
if (dacRate>0) { |
| 126 |
int remainTime=(rate-dacPeriod+dacRate-1)/dacRate; |
| 127 |
if (remainTime<pcmAdvance) pcmAdvance=remainTime; |
| 128 |
if (remainTime<1) pcmAdvance=1; |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
if (!writes.empty()) { |
| 134 |
pcmAdvance=1; |
| 135 |
QueuedWrite w=writes.front(); |
| 136 |
doWrite(i,w.addr,w.val); |
| 137 |
regPool[w.addr&0x1f]=w.val; |
| 138 |
writes.pop(); |
| 139 |
} |
| 140 |
|
| 141 |
i+=pcmAdvance-1; |
| 142 |
|
| 143 |
doPCM; |
| 144 |
} |
| 145 |
apu_tick(nes,len); |
| 146 |
|
| 147 |
for (int i=0; i<5; i++) { |
| 148 |
oscBuf[i]->end(len); |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
void DivPlatformNES::acquire_NSFPlay(short** buf, size_t len) { |
| 153 |
int out1[2]; |
| 154 |
int out2[2]; |
| 155 |
const int pcmAdvance=1; |
| 156 |
|
| 157 |
for (int i=0; i<5; i++) { |
| 158 |
oscBuf[i]->begin(len); |
| 159 |
} |
| 160 |
|
| 161 |
for (size_t i=0; i<len; i++) { |
| 162 |
doPCM; |
| 163 |
|
| 164 |
if (!writes.empty()) { |
| 165 |
QueuedWrite w=writes.front(); |
| 166 |
doWrite(i,w.addr,w.val); |
| 167 |
regPool[w.addr&0x1f]=w.val; |
| 168 |
writes.pop(); |
| 169 |
} |
| 170 |
|
| 171 |
nes1_NP->Tick(8); |
| 172 |
nes2_NP->TickFrameSequence(8); |
| 173 |
nes2_NP->Tick(8); |
| 174 |
nes1_NP->Render(out1); |
| 175 |
nes2_NP->Render(out2); |
| 176 |
|
| 177 |
int sample=(out1[0]+out1[1]+out2[0]+out2[1])<<1; |
| 178 |
if (sample>32767) sample=32767; |
| 179 |
if (sample<-32768) sample=-32768; |
| 180 |
buf[0][i]=sample; |
| 181 |
if (++writeOscBuf>=4) { |
| 182 |
writeOscBuf=0; |
| 183 |
oscBuf[0]->putSample(i,nes1_NP->out[0]<<11); |
| 184 |
oscBuf[1]->putSample(i,nes1_NP->out[1]<<11); |
| 185 |
oscBuf[2]->putSample(i,nes2_NP->out[0]<<11); |
| 186 |
oscBuf[3]->putSample(i,nes2_NP->out[1]<<11); |
| 187 |
oscBuf[4]->putSample(i,nes2_NP->out[2]<<8); |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
for (int i=0; i<5; i++) { |
| 192 |
oscBuf[i]->end(len); |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
void DivPlatformNES::acquire_NSFPlayE(short** buf, size_t len) { |
| 197 |
int out1[2]; |
| 198 |
int out2[2]; |
| 199 |
const int pcmAdvance=1; |
| 200 |
|
| 201 |
for (int i=0; i<5; i++) { |
| 202 |
oscBuf[i]->begin(len); |
| 203 |
} |
| 204 |
|
| 205 |
for (size_t i=0; i<len; i++) { |
| 206 |
doPCM; |
| 207 |
|
| 208 |
if (!writes.empty()) { |
| 209 |
QueuedWrite w=writes.front(); |
| 210 |
doWrite(i,w.addr,w.val); |
| 211 |
regPool[w.addr&0x1f]=w.val; |
| 212 |
writes.pop(); |
| 213 |
} |
| 214 |
|
| 215 |
e1_NP->Tick(8); |
| 216 |
e2_NP->TickFrameSequence(8); |
| 217 |
e2_NP->Tick(8); |
| 218 |
e1_NP->Render(out1); |
| 219 |
e2_NP->Render(out2); |
| 220 |
|
| 221 |
int sample=(out1[0]+out1[1]+out2[0]+out2[1])<<1; |
| 222 |
if (sample>32767) sample=32767; |
| 223 |
if (sample<-32768) sample=-32768; |
| 224 |
buf[0][i]=sample; |
| 225 |
if (++writeOscBuf>=4) { |
| 226 |
writeOscBuf=0; |
| 227 |
oscBuf[0]->putSample(i,e1_NP->out[0]<<11); |
| 228 |
oscBuf[1]->putSample(i,e1_NP->out[1]<<11); |
| 229 |
oscBuf[2]->putSample(i,e2_NP->out[0]<<11); |
| 230 |
oscBuf[3]->putSample(i,e2_NP->out[1]<<11); |
| 231 |
oscBuf[4]->putSample(i,e2_NP->out[2]<<8); |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
for (int i=0; i<5; i++) { |
| 236 |
oscBuf[i]->end(len); |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
void DivPlatformNES::acquire(short** buf, size_t len) { |
| 241 |
if (!useNP) return; |
| 242 |
if (isE) { |
| 243 |
acquire_NSFPlayE(buf,len); |
| 244 |
} else { |
| 245 |
acquire_NSFPlay(buf,len); |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
void DivPlatformNES::acquireDirect(blip_buffer_t** bb, size_t len) { |
| 250 |
if (useNP) return; |
| 251 |
acquire_puNES(bb,len); |
| 252 |
} |
| 253 |
|
| 254 |
static unsigned char noiseTable[253]={ |
| 255 |
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 4, |
| 256 |
15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, |
| 257 |
15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, |
| 258 |
3, 2, 1, 0, 11, 10, 9, 8, 7, 6, 5, 4, |
| 259 |
15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, |
| 260 |
3, 2, 1, 0, 11, 10, 9, 8, 7, 6, 5, 4, |
| 261 |
15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, |
| 262 |
3, 2, 1, 0, 11, 10, 9, 8, 7, 6, 5, 4, |
| 263 |
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, |
| 264 |
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, |
| 265 |
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, |
| 266 |
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, |
| 267 |
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, |
| 268 |
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, |
| 269 |
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, |
| 270 |
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, |
| 271 |
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, |
| 272 |
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, |
| 273 |
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, |
| 274 |
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, |
| 275 |
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, |
| 276 |
15 |
| 277 |
}; |
| 278 |
|
| 279 |
unsigned char DivPlatformNES::calcDPCMRate(int inRate) { |
| 280 |
if (inRate<4450) return 0; |
| 281 |
if (inRate<5000) return 1; |
| 282 |
if (inRate<5400) return 2; |
| 283 |
if (inRate<5900) return 3; |
| 284 |
if (inRate<6650) return 4; |
| 285 |
if (inRate<7450) return 5; |
| 286 |
if (inRate<8100) return 6; |
| 287 |
if (inRate<8800) return 7; |
| 288 |
if (inRate<10200) return 8; |
| 289 |
if (inRate<11700) return 9; |
| 290 |
if (inRate<13300) return 10; |
| 291 |
if (inRate<15900) return 11; |
| 292 |
if (inRate<18900) return 12; |
| 293 |
if (inRate<23500) return 13; |
| 294 |
if (inRate<29000) return 14; |
| 295 |
return 15; |
| 296 |
} |
| 297 |
|
| 298 |
void DivPlatformNES::tick(bool sysTick) { |
| 299 |
for (int i=0; i<4; i++) { |
| 300 |
chan[i].std.next(); |
| 301 |
if (chan[i].std.vol.had) { |
| 302 |
// ok, why are the volumes like that? |
| 303 |
chan[i].outVol=VOL_SCALE_LINEAR_BROKEN(chan[i].vol&15,MIN(15,chan[i].std.vol.val),15); |
| 304 |
if (chan[i].outVol<0) chan[i].outVol=0; |
| 305 |
if (i==2) { // triangle |
| 306 |
rWrite(0x4000+i*4,(chan[i].outVol==0)?0:linearCount); |
| 307 |
chan[i].freqChanged=true; |
| 308 |
} else { |
| 309 |
rWrite(0x4000+i*4,(chan[i].envMode<<4)|chan[i].outVol|((chan[i].duty&3)<<6)); |
| 310 |
} |
| 311 |
} |
| 312 |
if (NEW_ARP_STRAT) { |
| 313 |
chan[i].handleArp(); |
| 314 |
} else if (chan[i].std.arp.had) { |
| 315 |
if (i==3) { // noise |
| 316 |
chan[i].baseFreq=parent->calcArp(chan[i].note,chan[i].std.arp.val); |
| 317 |
if (chan[i].baseFreq>255) chan[i].baseFreq=255; |
| 318 |
if (chan[i].baseFreq<0) chan[i].baseFreq=0; |
| 319 |
} else { |
| 320 |
if (!chan[i].inPorta) { |
| 321 |
chan[i].baseFreq=NOTE_PERIODIC(parent->calcArp(chan[i].note,chan[i].std.arp.val)); |
| 322 |
} |
| 323 |
} |
| 324 |
chan[i].freqChanged=true; |
| 325 |
} |
| 326 |
if (chan[i].std.duty.had) { |
| 327 |
chan[i].duty=chan[i].std.duty.val; |
| 328 |
if (i==3) { |
| 329 |
if (parent->song.properNoiseLayout) { |
| 330 |
chan[i].duty&=1; |
| 331 |
} else if (chan[i].duty>1) { |
| 332 |
chan[i].duty=1; |
| 333 |
} |
| 334 |
} |
| 335 |
if (i!=2) { |
| 336 |
rWrite(0x4000+i*4,(chan[i].envMode<<4)|chan[i].outVol|((chan[i].duty&3)<<6)); |
| 337 |
} else if (isE) { |
| 338 |
rWrite(0x4000+9,chan[i].duty); |
| 339 |
} |
| 340 |
if (i==3) { // noise |
| 341 |
chan[i].freqChanged=true; |
| 342 |
} |
| 343 |
} |
| 344 |
if (chan[i].std.pitch.had) { |
| 345 |
if (chan[i].std.pitch.mode) { |
| 346 |
chan[i].pitch2+=chan[i].std.pitch.val; |
| 347 |
CLAMP_VAR(chan[i].pitch2,-32768,32767); |
| 348 |
} else { |
| 349 |
chan[i].pitch2=chan[i].std.pitch.val; |
| 350 |
} |
| 351 |
chan[i].freqChanged=true; |
| 352 |
} |
| 353 |
if (chan[i].sweepChanged) { |
| 354 |
chan[i].sweepChanged=false; |
| 355 |
} |
| 356 |
if (i<3) if (chan[i].std.phaseReset.had) { |
| 357 |
if (chan[i].std.phaseReset.val==1) { |
| 358 |
chan[i].freqChanged=true; |
| 359 |
chan[i].prevFreq=-1; |
| 360 |
} |
| 361 |
} |
| 362 |
if (chan[i].freqChanged || chan[i].keyOn || chan[i].keyOff) { |
| 363 |
if (i==3) { // noise |
| 364 |
int ntPos=chan[i].baseFreq; |
| 365 |
if (NEW_ARP_STRAT) { |
| 366 |
if (chan[i].fixedArp) { |
| 367 |
ntPos=chan[i].baseNoteOverride; |
| 368 |
} else { |
| 369 |
ntPos+=chan[i].arpOff; |
| 370 |
} |
| 371 |
} |
| 372 |
ntPos+=chan[i].pitch2; |
| 373 |
if (isE) { |
| 374 |
chan[i].freq=31-(ntPos&31); |
| 375 |
} else if (parent->song.properNoiseLayout) { |
| 376 |
chan[i].freq=15-(ntPos&15); |
| 377 |
} else { |
| 378 |
if (ntPos<0) ntPos=0; |
| 379 |
if (ntPos>252) ntPos=252; |
| 380 |
chan[i].freq=noiseTable[ntPos]; |
| 381 |
} |
| 382 |
} else { |
| 383 |
chan[i].freq=parent->calcFreq(chan[i].baseFreq,chan[i].pitch,chan[i].fixedArp?chan[i].baseNoteOverride:chan[i].arpOff,chan[i].fixedArp,true,0,chan[i].pitch2,chipClock,CHIP_DIVIDER)-1; |
| 384 |
if (chan[i].freq>2047) chan[i].freq=2047; |
| 385 |
if (chan[i].freq<0) chan[i].freq=0; |
| 386 |
} |
| 387 |
if (chan[i].keyOn) { |
| 388 |
// retrigger if sweep is on |
| 389 |
if (chan[i].sweep!=0x08) { |
| 390 |
chan[i].prevFreq=-1; |
| 391 |
} |
| 392 |
} |
| 393 |
if (chan[i].keyOff) { |
| 394 |
// rWrite(16+i*5+2,8); |
| 395 |
if (i==2) { // triangle |
| 396 |
rWrite(0x4000+i*4,0x00); |
| 397 |
} else { |
| 398 |
rWrite(0x4000+i*4,0x30); |
| 399 |
} |
| 400 |
} |
| 401 |
if (i==3) { // noise |
| 402 |
rWrite(0x4002+i*4,(chan[i].duty<<7)|chan[i].freq); |
| 403 |
rWrite(0x4003+i*4,(chan[i].len<<3)); |
| 404 |
} else { |
| 405 |
rWrite(0x4002+i*4,chan[i].freq&0xff); |
| 406 |
if ((chan[i].prevFreq>>8)!=(chan[i].freq>>8) || i==2) { |
| 407 |
rWrite(0x4003+i*4,(chan[i].len<<3)|(chan[i].freq>>8)); |
| 408 |
} |
| 409 |
if (chan[i].freq!=65535 && chan[i].freq!=0) { |
| 410 |
chan[i].prevFreq=chan[i].freq; |
| 411 |
} |
| 412 |
} |
| 413 |
if (chan[i].keyOn) chan[i].keyOn=false; |
| 414 |
if (chan[i].keyOff) chan[i].keyOff=false; |
| 415 |
chan[i].freqChanged=false; |
| 416 |
} |
| 417 |
} |
| 418 |
|
| 419 |
// PCM |
| 420 |
if (chan[4].freqChanged || chan[4].keyOn) { |
| 421 |
chan[4].freq=parent->calcFreq(chan[4].baseFreq,chan[4].pitch,chan[4].fixedArp?chan[4].baseNoteOverride:chan[4].arpOff,chan[4].fixedArp,false); |
| 422 |
if (chan[4].furnaceDac) { |
| 423 |
double off=1.0; |
| 424 |
if (dacSample>=0 && dacSample<parent->song.sampleLen) { |
| 425 |
DivSample* s=parent->getSample(dacSample); |
| 426 |
off=(double)s->centerRate/parent->getCenterRate(); |
| 427 |
} |
| 428 |
dacRate=MIN(chan[4].freq*off,32000); |
| 429 |
if (chan[4].keyOn) { |
| 430 |
if (dpcmMode && !skipRegisterWrites && dacSample>=0 && dacSample<parent->song.sampleLen) { |
| 431 |
unsigned int dpcmAddr=sampleOffDPCM[dacSample]+(dacPos>>3); |
| 432 |
int dpcmLen=(parent->getSample(dacSample)->lengthDPCM-(dacPos>>3))>>4; |
| 433 |
if (dpcmLen<0) dpcmLen=0; |
| 434 |
if (dpcmLen>255) dpcmLen=255; |
| 435 |
goingToLoop=parent->getSample(dacSample)->isLoopable(); |
| 436 |
// write DPCM |
| 437 |
rWrite(0x4015,15); |
| 438 |
if (nextDPCMFreq>=0) { |
| 439 |
rWrite(0x4010,nextDPCMFreq|(goingToLoop?0x40:0)); |
| 440 |
nextDPCMFreq=-1; |
| 441 |
} else { |
| 442 |
rWrite(0x4010,calcDPCMRate(dacRate)|(goingToLoop?0x40:0)); |
| 443 |
} |
| 444 |
if (nextDPCMDelta>=0) { |
| 445 |
rWrite(0x4011,nextDPCMDelta); |
| 446 |
nextDPCMDelta=-1; |
| 447 |
} |
| 448 |
rWrite(0x4012,(dpcmAddr>>6)&0xff); |
| 449 |
rWrite(0x4013,dpcmLen&0xff); |
| 450 |
rWrite(0x4015,31); |
| 451 |
if (dpcmBank!=(dpcmAddr>>14)) { |
| 452 |
dpcmBank=dpcmAddr>>14; |
| 453 |
logV("switching bank to %d",dpcmBank); |
| 454 |
if (dumpWrites) addWrite(0xffff0004,dpcmBank); |
| 455 |
} |
| 456 |
|
| 457 |
// sample custom loop point... |
| 458 |
DivSample* lsamp=parent->getSample(dacSample); |
| 459 |
|
| 460 |
// how it works: |
| 461 |
// when the initial sample info is written (see above) and playback is launched, |
| 462 |
// the parameters (start point in memory and length) are locked until sample end |
| 463 |
// is reached. |
| 464 |
|
| 465 |
// thus, if we write new data after just several APU clock cycles, it will be used only when |
| 466 |
// sample finishes one full loop. |
| 467 |
|
| 468 |
// thus we can write sample's loop point as "start address" and sample's looped part length |
| 469 |
// as "full sample length". |
| 470 |
|
| 471 |
// APU will play full sample once and then repeatedly cycle through the looped part. |
| 472 |
|
| 473 |
// sources: |
| 474 |
// https://www.nesdev.org/wiki/APU_DMC |
| 475 |
// https://www.youtube.com/watch?v=vB4P8x2Am6Y |
| 476 |
|
| 477 |
if (lsamp->loopEnd>lsamp->loopStart && goingToLoop) { |
| 478 |
int loopStartAddr=sampleOffDPCM[dacSample]+(lsamp->loopStart>>3); |
| 479 |
int loopLen=(lsamp->loopEnd-lsamp->loopStart)>>3; |
| 480 |
|
| 481 |
rWrite(0x4012,(loopStartAddr>>6)&0xff); |
| 482 |
rWrite(0x4013,(loopLen>>4)&0xff); |
| 483 |
} |
| 484 |
} |
| 485 |
} else { |
| 486 |
if (nextDPCMFreq>=0) { |
| 487 |
rWrite(0x4010,nextDPCMFreq|(goingToLoop?0x40:0)); |
| 488 |
nextDPCMFreq=-1; |
| 489 |
} else { |
| 490 |
rWrite(0x4010,calcDPCMRate(dacRate)|(goingToLoop?0x40:0)); |
| 491 |
} |
| 492 |
} |
| 493 |
if (dumpWrites && !dpcmMode) addWrite(0xffff0001,dacRate); |
| 494 |
} |
| 495 |
if (chan[4].keyOn) chan[4].keyOn=false; |
| 496 |
chan[4].freqChanged=false; |
| 497 |
} |
| 498 |
|
| 499 |
nextDPCMFreq=-1; |
| 500 |
} |
| 501 |
|
| 502 |
int DivPlatformNES::dispatch(DivCommand c) { |
| 503 |
switch (c.cmd) { |
| 504 |
case DIV_CMD_NOTE_ON: |
| 505 |
if (c.chan==4) { // PCM |
| 506 |
DivInstrument* ins=parent->getIns(chan[c.chan].ins,DIV_INS_NES); |
| 507 |
if (ins->type==DIV_INS_AMIGA || (ins->type==DIV_INS_NES && !parent->song.oldDPCM)) { |
| 508 |
if (ins->type==DIV_INS_NES) { |
| 509 |
if (!dpcmMode) { |
| 510 |
dpcmMode=true; |
| 511 |
if (dumpWrites) addWrite(0xffff0002,0); |
| 512 |
dacSample=-1; |
| 513 |
rWrite(0x4015,15); |
| 514 |
rWrite(0x4010,0); |
| 515 |
rWrite(0x4012,0); |
| 516 |
rWrite(0x4013,0); |
| 517 |
rWrite(0x4015,31); |
| 518 |
} |
| 519 |
|
| 520 |
if (ins->amiga.useNoteMap) { |
| 521 |
nextDPCMFreq=ins->amiga.getDPCMFreq(c.value); |
| 522 |
if (nextDPCMFreq<0 || nextDPCMFreq>15) nextDPCMFreq=lastDPCMFreq; |
| 523 |
lastDPCMFreq=nextDPCMFreq; |
| 524 |
nextDPCMDelta=ins->amiga.getDPCMDelta(c.value); |
| 525 |
} else { |
| 526 |
if (c.value==DIV_NOTE_NULL) { |
| 527 |
nextDPCMFreq=lastDPCMFreq; |
| 528 |
} else { |
| 529 |
nextDPCMFreq=c.value&15; |
| 530 |
} |
| 531 |
} |
| 532 |
} |
| 533 |
if (c.value!=DIV_NOTE_NULL) { |
| 534 |
dacSample=(int)ins->amiga.getSample(c.value); |
| 535 |
if (ins->type==DIV_INS_AMIGA) { |
| 536 |
chan[c.chan].sampleNote=c.value; |
| 537 |
c.value=ins->amiga.getFreq(c.value); |
| 538 |
chan[c.chan].sampleNoteDelta=c.value-chan[c.chan].sampleNote; |
| 539 |
} |
| 540 |
} else if (chan[c.chan].sampleNote!=DIV_NOTE_NULL) { |
| 541 |
dacSample=(int)ins->amiga.getSample(chan[c.chan].sampleNote); |
| 542 |
if (ins->type==DIV_INS_AMIGA) { |
| 543 |
c.value=ins->amiga.getFreq(chan[c.chan].sampleNote); |
| 544 |
} |
| 545 |
} |
| 546 |
if (dacSample<0 || dacSample>=parent->song.sampleLen) { |
| 547 |
dacSample=-1; |
| 548 |
if (dumpWrites && !dpcmMode) addWrite(0xffff0002,0); |
| 549 |
break; |
| 550 |
} else { |
| 551 |
if (dumpWrites && !dpcmMode) addWrite(0xffff0000,dacSample); |
| 552 |
} |
| 553 |
if (chan[c.chan].setPos) { |
| 554 |
chan[c.chan].setPos=false; |
| 555 |
} else { |
| 556 |
dacPos=0; |
| 557 |
} |
| 558 |
dacPeriod=0; |
| 559 |
if (c.value!=DIV_NOTE_NULL) { |
| 560 |
chan[c.chan].baseFreq=parent->calcBaseFreq(1,1,c.value,false); |
| 561 |
chan[c.chan].freqChanged=true; |
| 562 |
chan[c.chan].note=c.value; |
| 563 |
} |
| 564 |
chan[c.chan].active=true; |
| 565 |
chan[c.chan].keyOn=true; |
| 566 |
chan[c.chan].furnaceDac=true; |
| 567 |
} else { |
| 568 |
if (c.value!=DIV_NOTE_NULL) { |
| 569 |
chan[c.chan].note=c.value; |
| 570 |
} |
| 571 |
dacSample=12*sampleBank+chan[c.chan].note%12; |
| 572 |
if (dacSample>=parent->song.sampleLen) { |
| 573 |
dacSample=-1; |
| 574 |
if (dumpWrites && !dpcmMode) addWrite(0xffff0002,0); |
| 575 |
break; |
| 576 |
} else { |
| 577 |
if (dumpWrites && !dpcmMode) addWrite(0xffff0000,dacSample); |
| 578 |
} |
| 579 |
if (chan[c.chan].setPos) { |
| 580 |
chan[c.chan].setPos=false; |
| 581 |
} else { |
| 582 |
dacPos=0; |
| 583 |
} |
| 584 |
dacPeriod=0; |
| 585 |
dacRate=parent->getSample(dacSample)->rate; |
| 586 |
if (dumpWrites && !dpcmMode) addWrite(0xffff0001,dacRate); |
| 587 |
chan[c.chan].furnaceDac=false; |
| 588 |
if (dpcmMode && !skipRegisterWrites) { |
| 589 |
unsigned int dpcmAddr=sampleOffDPCM[dacSample]+(dacPos>>3); |
| 590 |
int dpcmLen=(parent->getSample(dacSample)->lengthDPCM-(dacPos>>3))>>4; |
| 591 |
if (dpcmLen<0) dpcmLen=0; |
| 592 |
if (dpcmLen>255) dpcmLen=255; |
| 593 |
goingToLoop=parent->getSample(dacSample)->isLoopable(); |
| 594 |
// write DPCM |
| 595 |
rWrite(0x4015,15); |
| 596 |
if (nextDPCMFreq>=0) { |
| 597 |
rWrite(0x4010,nextDPCMFreq|(goingToLoop?0x40:0)); |
| 598 |
nextDPCMFreq=-1; |
| 599 |
} else { |
| 600 |
rWrite(0x4010,calcDPCMRate(dacRate)|(goingToLoop?0x40:0)); |
| 601 |
} |
| 602 |
rWrite(0x4012,(dpcmAddr>>6)&0xff); |
| 603 |
rWrite(0x4013,dpcmLen&0xff); |
| 604 |
rWrite(0x4015,31); |
| 605 |
if (dpcmBank!=(dpcmAddr>>14)) { |
| 606 |
dpcmBank=dpcmAddr>>14; |
| 607 |
logV("switching bank to %d",dpcmBank); |
| 608 |
if (dumpWrites) addWrite(0xffff0004,dpcmBank); |
| 609 |
} |
| 610 |
} |
| 611 |
} |
| 612 |
break; |
| 613 |
} else if (c.chan==3) { // noise |
| 614 |
if (c.value!=DIV_NOTE_NULL) { |
| 615 |
chan[c.chan].baseFreq=c.value; |
| 616 |
} |
| 617 |
} else { |
| 618 |
if (c.value!=DIV_NOTE_NULL) { |
| 619 |
chan[c.chan].baseFreq=NOTE_PERIODIC(c.value); |
| 620 |
} |
| 621 |
} |
| 622 |
if (c.value!=DIV_NOTE_NULL) { |
| 623 |
chan[c.chan].freqChanged=true; |
| 624 |
chan[c.chan].note=c.value; |
| 625 |
} |
| 626 |
chan[c.chan].active=true; |
| 627 |
chan[c.chan].keyOn=true; |
| 628 |
chan[c.chan].macroInit(parent->getIns(chan[c.chan].ins,DIV_INS_NES)); |
| 629 |
if (!parent->song.brokenOutVol && !chan[c.chan].std.vol.will) { |
| 630 |
chan[c.chan].outVol=chan[c.chan].vol; |
| 631 |
} |
| 632 |
if (c.chan==2) { |
| 633 |
rWrite(0x4000+c.chan*4,linearCount); |
| 634 |
} else if (!parent->song.brokenOutVol2) { |
| 635 |
rWrite(0x4000+c.chan*4,(chan[c.chan].envMode<<4)|chan[c.chan].vol|((chan[c.chan].duty&3)<<6)); |
| 636 |
} |
| 637 |
if (resetSweep && c.chan<2) { |
| 638 |
if (chan[c.chan].sweep!=0x08 && !chan[c.chan].sweepChanged) { |
| 639 |
chan[c.chan].sweep=0x08; |
| 640 |
chan[c.chan].prevFreq=-1; |
| 641 |
rWrite(0x4001+(c.chan*4),chan[c.chan].sweep); |
| 642 |
} |
| 643 |
} |
| 644 |
break; |
| 645 |
case DIV_CMD_NOTE_OFF: |
| 646 |
if (c.chan==4) { |
| 647 |
dacSample=-1; |
| 648 |
if (dumpWrites) addWrite(0xffff0002,0); |
| 649 |
if (dpcmMode && !skipRegisterWrites) rWrite(0x4015,15); |
| 650 |
} |
| 651 |
chan[c.chan].active=false; |
| 652 |
chan[c.chan].keyOff=true; |
| 653 |
chan[c.chan].macroInit(NULL); |
| 654 |
break; |
| 655 |
case DIV_CMD_NOTE_OFF_ENV: |
| 656 |
case DIV_CMD_ENV_RELEASE: |
| 657 |
chan[c.chan].std.release(); |
| 658 |
break; |
| 659 |
case DIV_CMD_INSTRUMENT: |
| 660 |
if (chan[c.chan].ins!=c.value || c.value2==1) { |
| 661 |
chan[c.chan].ins=c.value; |
| 662 |
} |
| 663 |
break; |
| 664 |
case DIV_CMD_VOLUME: |
| 665 |
if (chan[c.chan].vol!=c.value) { |
| 666 |
chan[c.chan].vol=c.value; |
| 667 |
if (!chan[c.chan].std.vol.has) { |
| 668 |
chan[c.chan].outVol=c.value; |
| 669 |
} |
| 670 |
if (chan[c.chan].active) { |
| 671 |
if (c.chan==2) { |
| 672 |
rWrite(0x4000+c.chan*4,linearCount); |
| 673 |
} else { |
| 674 |
rWrite(0x4000+c.chan*4,(chan[c.chan].envMode<<4)|chan[c.chan].vol|((chan[c.chan].duty&3)<<6)); |
| 675 |
} |
| 676 |
} |
| 677 |
} |
| 678 |
break; |
| 679 |
case DIV_CMD_GET_VOLUME: |
| 680 |
return chan[c.chan].vol; |
| 681 |
break; |
| 682 |
case DIV_CMD_PITCH: |
| 683 |
chan[c.chan].pitch=c.value; |
| 684 |
chan[c.chan].freqChanged=true; |
| 685 |
break; |
| 686 |
case DIV_CMD_NOTE_PORTA: { |
| 687 |
int destFreq=(c.chan==4)?(parent->calcBaseFreq(1,1,c.value2+chan[c.chan].sampleNoteDelta,false)):(NOTE_PERIODIC(c.value2)); |
| 688 |
bool return2=false; |
| 689 |
if (destFreq>chan[c.chan].baseFreq) { |
| 690 |
chan[c.chan].baseFreq+=c.value; |
| 691 |
if (chan[c.chan].baseFreq>=destFreq) { |
| 692 |
chan[c.chan].baseFreq=destFreq; |
| 693 |
return2=true; |
| 694 |
} |
| 695 |
} else { |
| 696 |
chan[c.chan].baseFreq-=c.value; |
| 697 |
if (chan[c.chan].baseFreq<=destFreq) { |
| 698 |
chan[c.chan].baseFreq=destFreq; |
| 699 |
return2=true; |
| 700 |
} |
| 701 |
} |
| 702 |
chan[c.chan].freqChanged=true; |
| 703 |
if (return2) { |
| 704 |
chan[c.chan].inPorta=false; |
| 705 |
return 2; |
| 706 |
} |
| 707 |
break; |
| 708 |
} |
| 709 |
case DIV_CMD_STD_NOISE_MODE: |
| 710 |
chan[c.chan].duty=c.value; |
| 711 |
if (c.chan==3) { // noise |
| 712 |
chan[c.chan].freqChanged=true; |
| 713 |
} else if (c.chan==2 && isE) { |
| 714 |
rWrite(0x4000+9,chan[c.chan].duty); |
| 715 |
} else if (c.chan<2) { |
| 716 |
rWrite(0x4000+c.chan*4,(chan[c.chan].active?((chan[c.chan].envMode<<4)|chan[c.chan].outVol):0x30)|((chan[c.chan].duty&3)<<6)); |
| 717 |
} |
| 718 |
break; |
| 719 |
case DIV_CMD_NES_SWEEP: |
| 720 |
if (c.chan>1) break; |
| 721 |
if (c.value2==0) { |
| 722 |
chan[c.chan].sweep=0x08; |
| 723 |
} else { |
| 724 |
if (!c.value) { // down |
| 725 |
chan[c.chan].sweep=0x88|(c.value2&0x77); |
| 726 |
} else { // up |
| 727 |
chan[c.chan].sweep=0x80|(c.value2&0x77); |
| 728 |
} |
| 729 |
} |
| 730 |
rWrite(0x4001+(c.chan*4),chan[c.chan].sweep); |
| 731 |
chan[c.chan].sweepChanged=true; |
| 732 |
break; |
| 733 |
case DIV_CMD_NES_ENV_MODE: |
| 734 |
chan[c.chan].envMode=c.value&3; |
| 735 |
if (c.chan==3) { // noise |
| 736 |
chan[c.chan].freqChanged=true; |
| 737 |
} else if (c.chan<2) { |
| 738 |
rWrite(0x4000+c.chan*4,(chan[c.chan].active?((chan[c.chan].envMode<<4)|chan[c.chan].outVol):0x30)|((chan[c.chan].duty&3)<<6)); |
| 739 |
} |
| 740 |
break; |
| 741 |
case DIV_CMD_NES_LENGTH: |
| 742 |
if (c.chan>=4) break; |
| 743 |
chan[c.chan].len=c.value&0x1f; |
| 744 |
chan[c.chan].freqChanged=true; |
| 745 |
chan[c.chan].prevFreq=-1; |
| 746 |
break; |
| 747 |
case DIV_CMD_NES_COUNT_MODE: |
| 748 |
countMode=c.value; |
| 749 |
rWrite(0x4017,countMode?0x80:0); |
| 750 |
break; |
| 751 |
case DIV_CMD_NES_LINEAR_LENGTH: |
| 752 |
if (c.chan==2) { |
| 753 |
linearCount=c.value; |
| 754 |
if (chan[c.chan].active) { |
| 755 |
rWrite(0x4000+c.chan*4,(chan[c.chan].outVol==0)?0:linearCount); |
| 756 |
chan[c.chan].freqChanged=true; |
| 757 |
chan[c.chan].prevFreq=-1; |
| 758 |
} |
| 759 |
} |
| 760 |
break; |
| 761 |
case DIV_CMD_NES_DMC: |
| 762 |
rWrite(0x4011,c.value&0x7f); |
| 763 |
break; |
| 764 |
case DIV_CMD_SAMPLE_MODE: |
| 765 |
dpcmMode=c.value; |
| 766 |
if (dumpWrites && dpcmMode) addWrite(0xffff0002,0); |
| 767 |
dacSample=-1; |
| 768 |
rWrite(0x4015,15); |
| 769 |
rWrite(0x4010,0); |
| 770 |
rWrite(0x4012,0); |
| 771 |
rWrite(0x4013,0); |
| 772 |
rWrite(0x4015,31); |
| 773 |
break; |
| 774 |
case DIV_CMD_SAMPLE_FREQ: { |
| 775 |
bool goingToLoop=parent->getSample(dacSample)->isLoopable(); |
| 776 |
if (dpcmMode) { |
| 777 |
nextDPCMFreq=c.value&15; |
| 778 |
rWrite(0x4010,(c.value&15)|(goingToLoop?0x40:0)); |
| 779 |
} |
| 780 |
break; |
| 781 |
} |
| 782 |
case DIV_CMD_SAMPLE_BANK: |
| 783 |
sampleBank=c.value; |
| 784 |
if (sampleBank>(parent->song.sample.size()/12)) { |
| 785 |
sampleBank=parent->song.sample.size()/12; |
| 786 |
} |
| 787 |
break; |
| 788 |
case DIV_CMD_SAMPLE_POS: |
| 789 |
if (c.chan!=4) break; |
| 790 |
dacPos=c.value; |
| 791 |
dpcmPos=c.value; |
| 792 |
chan[c.chan].setPos=true; |
| 793 |
if (chan[c.chan].active) { |
| 794 |
chan[c.chan].keyOn=true; |
| 795 |
} |
| 796 |
break; |
| 797 |
case DIV_CMD_LEGATO: |
| 798 |
if (c.chan==3) break; |
| 799 |
if (c.chan==4) { |
| 800 |
chan[c.chan].baseFreq=parent->calcBaseFreq(1,1,c.value+chan[c.chan].sampleNoteDelta+((HACKY_LEGATO_MESS)?(chan[c.chan].std.arp.val):(0)),false); |
| 801 |
} else { |
| 802 |
chan[c.chan].baseFreq=NOTE_PERIODIC(c.value+((HACKY_LEGATO_MESS)?(chan[c.chan].std.arp.val):(0))); |
| 803 |
} |
| 804 |
chan[c.chan].freqChanged=true; |
| 805 |
chan[c.chan].note=c.value; |
| 806 |
break; |
| 807 |
case DIV_CMD_PRE_PORTA: |
| 808 |
if (chan[c.chan].active && c.value2) { |
| 809 |
if (parent->song.resetMacroOnPorta) chan[c.chan].macroInit(parent->getIns(chan[c.chan].ins,DIV_INS_NES)); |
| 810 |
} |
| 811 |
if (!chan[c.chan].inPorta && c.value && !parent->song.brokenPortaArp && chan[c.chan].std.arp.will && !NEW_ARP_STRAT) chan[c.chan].baseFreq=NOTE_PERIODIC(chan[c.chan].note); |
| 812 |
chan[c.chan].inPorta=c.value; |
| 813 |
break; |
| 814 |
case DIV_CMD_GET_VOLMAX: |
| 815 |
return 15; |
| 816 |
break; |
| 817 |
case DIV_CMD_MACRO_OFF: |
| 818 |
chan[c.chan].std.mask(c.value,true); |
| 819 |
break; |
| 820 |
case DIV_CMD_MACRO_ON: |
| 821 |
chan[c.chan].std.mask(c.value,false); |
| 822 |
break; |
| 823 |
case DIV_CMD_MACRO_RESTART: |
| 824 |
chan[c.chan].std.restart(c.value); |
| 825 |
break; |
| 826 |
default: |
| 827 |
break; |
| 828 |
} |
| 829 |
return 1; |
| 830 |
} |
| 831 |
|
| 832 |
void DivPlatformNES::muteChannel(int ch, bool mute) { |
| 833 |
isMuted[ch]=mute; |
| 834 |
if (useNP) { |
| 835 |
if (isE) { |
| 836 |
e1_NP->SetMask(((int)isMuted[0])|(isMuted[1]<<1)); |
| 837 |
e2_NP->SetMask(((int)isMuted[2])|(isMuted[3]<<1)|(isMuted[4]<<2)); |
| 838 |
} else { |
| 839 |
nes1_NP->SetMask(((int)isMuted[0])|(isMuted[1]<<1)); |
| 840 |
nes2_NP->SetMask(((int)isMuted[2])|(isMuted[3]<<1)|(isMuted[4]<<2)); |
| 841 |
} |
| 842 |
} else { |
| 843 |
nes->muted[ch]=mute; |
| 844 |
} |
| 845 |
} |
| 846 |
|
| 847 |
void DivPlatformNES::forceIns() { |
| 848 |
for (int i=0; i<5; i++) { |
| 849 |
chan[i].insChanged=true; |
| 850 |
chan[i].prevFreq=65535; |
| 851 |
if (i<4) { |
| 852 |
if (i==2) { // triangle |
| 853 |
rWrite(0x4000+i*4,(chan[i].outVol==0 || !chan[i].active)?0:linearCount); |
| 854 |
} else { |
| 855 |
rWrite(0x4000+i*4,(chan[i].envMode<<4)|(chan[i].active?chan[i].outVol:0)|((chan[i].duty&3)<<6)); |
| 856 |
} |
| 857 |
} |
| 858 |
} |
| 859 |
rWrite(0x4001,chan[0].sweep); |
| 860 |
rWrite(0x4005,chan[1].sweep); |
| 861 |
rWrite(0x4017,countMode?0x80:0); |
| 862 |
} |
| 863 |
|
| 864 |
void* DivPlatformNES::getChanState(int ch) { |
| 865 |
return &chan[ch]; |
| 866 |
} |
| 867 |
|
| 868 |
DivMacroInt* DivPlatformNES::getChanMacroInt(int ch) { |
| 869 |
return &chan[ch].std; |
| 870 |
} |
| 871 |
|
| 872 |
DivDispatchOscBuffer* DivPlatformNES::getOscBuffer(int ch) { |
| 873 |
return oscBuf[ch]; |
| 874 |
} |
| 875 |
|
| 876 |
unsigned char* DivPlatformNES::getRegisterPool() { |
| 877 |
return regPool; |
| 878 |
} |
| 879 |
|
| 880 |
int DivPlatformNES::getRegisterPoolSize() { |
| 881 |
return 32; |
| 882 |
} |
| 883 |
|
| 884 |
float DivPlatformNES::getPostAmp() { |
| 885 |
return 2.0f; |
| 886 |
} |
| 887 |
|
| 888 |
void DivPlatformNES::reset() { |
| 889 |
while (!writes.empty()) writes.pop(); |
| 890 |
for (int i=0; i<5; i++) { |
| 891 |
chan[i]=DivPlatformNES::Channel(); |
| 892 |
chan[i].std.setEngine(parent); |
| 893 |
} |
| 894 |
if (dumpWrites) { |
| 895 |
addWrite(0xffffffff,0); |
| 896 |
} |
| 897 |
|
| 898 |
dacPeriod=0; |
| 899 |
dacPos=0; |
| 900 |
dpcmPos=0; |
| 901 |
dacRate=0; |
| 902 |
dacSample=-1; |
| 903 |
sampleBank=0; |
| 904 |
dpcmBank=0; |
| 905 |
dpcmMode=dpcmModeDefault; |
| 906 |
goingToLoop=false; |
| 907 |
countMode=false; |
| 908 |
nextDPCMFreq=-1; |
| 909 |
nextDPCMDelta=-1; |
| 910 |
lastDPCMFreq=15; |
| 911 |
linearCount=255; |
| 912 |
|
| 913 |
if (useNP) { |
| 914 |
if (isE) { |
| 915 |
e1_NP->Reset(); |
| 916 |
e2_NP->Reset(); |
| 917 |
e1_NP->SetMask(((int)isMuted[0])|(isMuted[1]<<1)); |
| 918 |
e2_NP->SetMask(((int)isMuted[2])|(isMuted[3]<<1)|(isMuted[4]<<2)); |
| 919 |
} else { |
| 920 |
nes1_NP->Reset(); |
| 921 |
nes2_NP->Reset(); |
| 922 |
nes1_NP->SetMask(((int)isMuted[0])|(isMuted[1]<<1)); |
| 923 |
nes2_NP->SetMask(((int)isMuted[2])|(isMuted[3]<<1)|(isMuted[4]<<2)); |
| 924 |
} |
| 925 |
} else { |
| 926 |
apu_turn_on(nes,apuType); |
| 927 |
nes->apu.cpu_cycles=0; |
| 928 |
nes->apu.cpu_opcode_cycle=0; |
| 929 |
} |
| 930 |
memset(regPool,0,128); |
| 931 |
|
| 932 |
rWrite(0x4015,0x1f); |
| 933 |
rWrite(0x4001,chan[0].sweep); |
| 934 |
rWrite(0x4005,chan[1].sweep); |
| 935 |
|
| 936 |
for (int i=0; i<4; i++) { |
| 937 |
if (i<4) { |
| 938 |
if (i==2) { // triangle |
| 939 |
rWrite(0x4000+i*4,0); |
| 940 |
} else { |
| 941 |
rWrite(0x4000+i*4,(chan[i].envMode<<4)|0|((chan[i].duty&3)<<6)); |
| 942 |
} |
| 943 |
} |
| 944 |
} |
| 945 |
|
| 946 |
dacAntiClickOn=true; |
| 947 |
dacAntiClick=0; |
| 948 |
} |
| 949 |
|
| 950 |
bool DivPlatformNES::keyOffAffectsArp(int ch) { |
| 951 |
return true; |
| 952 |
} |
| 953 |
|
| 954 |
bool DivPlatformNES::hasAcquireDirect() { |
| 955 |
return (!useNP && !isE); |
| 956 |
} |
| 957 |
|
| 958 |
void DivPlatformNES::setFlags(const DivConfig& flags) { |
| 959 |
int clockSel=flags.getInt("clockSel",0); |
| 960 |
if (clockSel==2) { // Dendy |
| 961 |
chipClock=COLOR_PAL*2.0/5.0; |
| 962 |
apuType=2; |
| 963 |
} else if (clockSel==1) { // PAL |
| 964 |
chipClock=COLOR_PAL*3.0/8.0; |
| 965 |
apuType=1; |
| 966 |
} else { // NTSC |
| 967 |
chipClock=COLOR_NTSC/2.0; |
| 968 |
apuType=0; |
| 969 |
} |
| 970 |
if (useNP) { |
| 971 |
if (isE) { |
| 972 |
e1_NP->SetClock(rate); |
| 973 |
e1_NP->SetRate(rate); |
| 974 |
e2_NP->SetClock(rate); |
| 975 |
e2_NP->SetRate(rate); |
| 976 |
e2_NP->SetPal(apuType==1); |
| 977 |
} else { |
| 978 |
nes1_NP->SetClock(rate); |
| 979 |
nes1_NP->SetRate(rate); |
| 980 |
nes2_NP->SetClock(rate); |
| 981 |
nes2_NP->SetRate(rate); |
| 982 |
nes2_NP->SetPal(apuType==1); |
| 983 |
} |
| 984 |
} else { |
| 985 |
nes->apu.type=apuType; |
| 986 |
} |
| 987 |
CHECK_CUSTOM_CLOCK; |
| 988 |
rate=chipClock; |
| 989 |
if (useNP) { |
| 990 |
rate/=8; |
| 991 |
} |
| 992 |
for (int i=0; i<5; i++) { |
| 993 |
oscBuf[i]->setRate(rate); |
| 994 |
} |
| 995 |
|
| 996 |
dpcmModeDefault=flags.getBool("dpcmMode",true); |
| 997 |
resetSweep=flags.getBool("resetSweep",false); |
| 998 |
} |
| 999 |
|
| 1000 |
void DivPlatformNES::notifyInsDeletion(void* ins) { |
| 1001 |
for (int i=0; i<5; i++) { |
| 1002 |
chan[i].std.notifyInsDeletion((DivInstrument*)ins); |
| 1003 |
} |
| 1004 |
} |
| 1005 |
|
| 1006 |
void DivPlatformNES::poke(unsigned int addr, unsigned short val) { |
| 1007 |
rWrite(addr,val); |
| 1008 |
} |
| 1009 |
|
| 1010 |
void DivPlatformNES::poke(std::vector<DivRegWrite>& wlist) { |
| 1011 |
for (DivRegWrite& i: wlist) rWrite(i.addr,i.val); |
| 1012 |
} |
| 1013 |
|
| 1014 |
void DivPlatformNES::setNSFPlay(bool use) { |
| 1015 |
useNP=use; |
| 1016 |
} |
| 1017 |
|
| 1018 |
void DivPlatformNES::set5E01(bool use) { |
| 1019 |
isE=use; |
| 1020 |
// for now |
| 1021 |
if (isE) useNP=true; |
| 1022 |
} |
| 1023 |
|
| 1024 |
unsigned char DivPlatformNES::readDMC(unsigned short addr) { |
| 1025 |
return dpcmMem[(addr&0x3fff)|((dpcmBank&15)<<14)]; |
| 1026 |
} |
| 1027 |
|
| 1028 |
const void* DivPlatformNES::getSampleMem(int index) { |
| 1029 |
return index==0?dpcmMem:NULL; |
| 1030 |
} |
| 1031 |
|
| 1032 |
size_t DivPlatformNES::getSampleMemCapacity(int index) { |
| 1033 |
return index==0?262144:0; |
| 1034 |
} |
| 1035 |
|
| 1036 |
size_t DivPlatformNES::getSampleMemUsage(int index) { |
| 1037 |
return index==0?dpcmMemLen:0; |
| 1038 |
} |
| 1039 |
|
| 1040 |
bool DivPlatformNES::isSampleLoaded(int index, int sample) { |
| 1041 |
if (index!=0) return false; |
| 1042 |
if (sample<0 || sample>32767) return false; |
| 1043 |
return sampleLoaded[sample]; |
| 1044 |
} |
| 1045 |
|
| 1046 |
const DivMemoryComposition* DivPlatformNES::getMemCompo(int index) { |
| 1047 |
if (index!=0) return NULL; |
| 1048 |
return &memCompo; |
| 1049 |
} |
| 1050 |
|
| 1051 |
void DivPlatformNES::renderSamples(int sysID) { |
| 1052 |
memset(dpcmMem,0,getSampleMemCapacity(0)); |
| 1053 |
memset(sampleOffDPCM,0,32768*sizeof(unsigned int)); |
| 1054 |
memset(sampleLoaded,0,32768*sizeof(bool)); |
| 1055 |
|
| 1056 |
memCompo=DivMemoryComposition(); |
| 1057 |
memCompo.name="DPCM"; |
| 1058 |
|
| 1059 |
size_t memPos=0; |
| 1060 |
for (int i=0; i<parent->song.sampleLen; i++) { |
| 1061 |
DivSample* s=parent->song.sample[i]; |
| 1062 |
if (!s->renderOn[0][sysID]) { |
| 1063 |
sampleOffDPCM[i]=0; |
| 1064 |
continue; |
| 1065 |
} |
| 1066 |
|
| 1067 |
unsigned int paddedLen=(s->lengthDPCM+63)&(~0x3f); |
| 1068 |
logV("%d padded length: %d",i,paddedLen); |
| 1069 |
if ((memPos&(~0x3fff))!=((memPos+paddedLen)&(~0x3fff))) { |
| 1070 |
memPos=(memPos+0x3fff)&(~0x3fff); |
| 1071 |
} |
| 1072 |
if (paddedLen>4081) { |
| 1073 |
paddedLen=4096; |
| 1074 |
} |
| 1075 |
if (memPos>=getSampleMemCapacity(0)) { |
| 1076 |
logW("out of DPCM memory for sample %d!",i); |
| 1077 |
break; |
| 1078 |
} |
| 1079 |
if (memPos+paddedLen>=getSampleMemCapacity(0)) { |
| 1080 |
memcpy(dpcmMem+memPos,s->dataDPCM,getSampleMemCapacity(0)-memPos); |
| 1081 |
logW("out of DPCM memory for sample %d!",i); |
| 1082 |
} else { |
| 1083 |
memcpy(dpcmMem+memPos,s->dataDPCM,MIN(s->lengthDPCM,paddedLen)); |
| 1084 |
sampleLoaded[i]=true; |
| 1085 |
} |
| 1086 |
sampleOffDPCM[i]=memPos; |
| 1087 |
memCompo.entries.push_back(DivMemoryEntry(DIV_MEMORY_SAMPLE,"Sample",i,memPos,memPos+paddedLen)); |
| 1088 |
memPos+=paddedLen; |
| 1089 |
} |
| 1090 |
dpcmMemLen=memPos; |
| 1091 |
|
| 1092 |
memCompo.capacity=262144; |
| 1093 |
memCompo.used=dpcmMemLen; |
| 1094 |
} |
| 1095 |
|
| 1096 |
int DivPlatformNES::init(DivEngine* p, int channels, int sugRate, const DivConfig& flags) { |
| 1097 |
parent=p; |
| 1098 |
dumpWrites=false; |
| 1099 |
skipRegisterWrites=false; |
| 1100 |
if (useNP) { |
| 1101 |
if (isE) { |
| 1102 |
e1_NP=new xgm::I5E01_APU; |
| 1103 |
e1_NP->SetOption(xgm::I5E01_APU::OPT_NONLINEAR_MIXER,1); |
| 1104 |
e2_NP=new xgm::I5E01_DMC; |
| 1105 |
e2_NP->SetOption(xgm::I5E01_DMC::OPT_NONLINEAR_MIXER,1); |
| 1106 |
e2_NP->SetMemory([this](unsigned short addr, unsigned int& data) { |
| 1107 |
data=readDMC(addr); |
| 1108 |
}); |
| 1109 |
e2_NP->SetAPU(e1_NP); |
| 1110 |
} else { |
| 1111 |
nes1_NP=new xgm::NES_APU; |
| 1112 |
nes1_NP->SetOption(xgm::NES_APU::OPT_NONLINEAR_MIXER,1); |
| 1113 |
nes2_NP=new xgm::NES_DMC; |
| 1114 |
nes2_NP->SetOption(xgm::NES_DMC::OPT_NONLINEAR_MIXER,1); |
| 1115 |
nes2_NP->SetMemory([this](unsigned short addr, unsigned int& data) { |
| 1116 |
data=readDMC(addr); |
| 1117 |
}); |
| 1118 |
nes2_NP->SetAPU(nes1_NP); |
| 1119 |
} |
| 1120 |
} else { |
| 1121 |
nes=new struct NESAPU; |
| 1122 |
nes->readDMC=_readDMC; |
| 1123 |
nes->readDMCUser=this; |
| 1124 |
} |
| 1125 |
writeOscBuf=0; |
| 1126 |
for (int i=0; i<5; i++) { |
| 1127 |
isMuted[i]=false; |
| 1128 |
if (!useNP) nes->muted[i]=false; |
| 1129 |
oscBuf[i]=new DivDispatchOscBuffer; |
| 1130 |
} |
| 1131 |
setFlags(flags); |
| 1132 |
|
| 1133 |
dpcmMem=new unsigned char[262144]; |
| 1134 |
dpcmMemLen=0; |
| 1135 |
dpcmBank=0; |
| 1136 |
if (dumpWrites) addWrite(0xffff0004,dpcmBank); |
| 1137 |
|
| 1138 |
init_nla_table(500,500); |
| 1139 |
reset(); |
| 1140 |
return 5; |
| 1141 |
} |
| 1142 |
|
| 1143 |
void DivPlatformNES::quit() { |
| 1144 |
for (int i=0; i<5; i++) { |
| 1145 |
delete oscBuf[i]; |
| 1146 |
} |
| 1147 |
if (useNP) { |
| 1148 |
if (isE) { |
| 1149 |
delete e1_NP; |
| 1150 |
delete e2_NP; |
| 1151 |
} else { |
| 1152 |
delete nes1_NP; |
| 1153 |
delete nes2_NP; |
| 1154 |
} |
| 1155 |
} else { |
| 1156 |
delete nes; |
| 1157 |
} |
| 1158 |
} |
| 1159 |
|
| 1160 |
// initialization of important arrays |
| 1161 |
DivPlatformNES::DivPlatformNES() { |
| 1162 |
sampleOffDPCM=new unsigned int[32768]; |
| 1163 |
sampleLoaded=new bool[32768]; |
| 1164 |
} |
| 1165 |
|
| 1166 |
DivPlatformNES::~DivPlatformNES() { |
| 1167 |
delete[] sampleOffDPCM; |
| 1168 |
delete[] sampleLoaded; |
| 1169 |
} |