| 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 "nds.h" |
| 21 |
#include "../engine.h" |
| 22 |
#include "../../ta-log.h" |
| 23 |
#include <math.h> |
| 24 |
|
| 25 |
#define CHIP_DIVIDER 32 |
| 26 |
|
| 27 |
#define rRead8(a) (nds.read8(a)) |
| 28 |
#define rWrite8(a,v) {if(!skipRegisterWrites){writes.push_back(QueuedWrite((a),1,(v)));regPool[(a)]=(v);if(dumpWrites)addWrite((a),(v));}} |
| 29 |
#define rWrite16(a,v) { \ |
| 30 |
if(!skipRegisterWrites) { \ |
| 31 |
writes.push_back(QueuedWrite((a),2,(v)));\ |
| 32 |
regPool[(a)+0]=(v)&0xff; \ |
| 33 |
regPool[(a)+1]=((v)>>8)&0xff; \ |
| 34 |
if(dumpWrites) addWrite((a)+0,(v)&0xff); \ |
| 35 |
if(dumpWrites) addWrite((a)+1,((v)>>8)&0xff); \ |
| 36 |
} \ |
| 37 |
} |
| 38 |
|
| 39 |
#define rWrite32(a,v) { \ |
| 40 |
if(!skipRegisterWrites) { \ |
| 41 |
writes.push_back(QueuedWrite((a),4,(v)));\ |
| 42 |
regPool[(a)+0]=(v)&0xff; \ |
| 43 |
regPool[(a)+1]=((v)>>8)&0xff; \ |
| 44 |
regPool[(a)+2]=((v)>>16)&0xff; \ |
| 45 |
regPool[(a)+3]=((v)>>24)&0xff; \ |
| 46 |
if(dumpWrites) addWrite((a)+0,(v)&0xff); \ |
| 47 |
if(dumpWrites) addWrite((a)+1,((v)>>8)&0xff); \ |
| 48 |
if(dumpWrites) addWrite((a)+2,((v)>>16)&0xff); \ |
| 49 |
if(dumpWrites) addWrite((a)+3,((v)>>24)&0xff); \ |
| 50 |
} \ |
| 51 |
} |
| 52 |
|
| 53 |
const char* regCheatSheetNDS[]={ |
| 54 |
"CHx_Control", "000+x*10", |
| 55 |
"CHx_Start", "004+x*10", |
| 56 |
"CHx_Freq", "008+x*10", |
| 57 |
"CHx_LoopStart", "00A+x*10", |
| 58 |
"CHx_Length", "00C+x*10", |
| 59 |
"Control", "100", |
| 60 |
"Bias", "104", |
| 61 |
"CAPx_Control", "108+x*1", |
| 62 |
"CAPx_Dest", "110+x*8", |
| 63 |
"CAPx_Length", "114+x*8", |
| 64 |
NULL |
| 65 |
}; |
| 66 |
|
| 67 |
const char** DivPlatformNDS::getRegisterSheet() { |
| 68 |
return regCheatSheetNDS; |
| 69 |
} |
| 70 |
|
| 71 |
void DivPlatformNDS::acquireDirect(blip_buffer_t** bb, size_t len) { |
| 72 |
for (int i=0; i<16; i++) { |
| 73 |
oscBuf[i]->begin(len); |
| 74 |
} |
| 75 |
|
| 76 |
nds.set_bb(bb[0],bb[1]); |
| 77 |
nds.set_oscbuf(oscBuf); |
| 78 |
nds.resetTS(0); |
| 79 |
|
| 80 |
while (!writes.empty()) { |
| 81 |
QueuedWrite w=writes.front(); |
| 82 |
if (w.size==4) { |
| 83 |
nds.write32(w.addr>>2,w.val); |
| 84 |
} else if (w.size==2) { |
| 85 |
nds.write16(w.addr>>1,w.val); |
| 86 |
} else { |
| 87 |
nds.write8(w.addr,w.val); |
| 88 |
} |
| 89 |
writes.pop(); |
| 90 |
} |
| 91 |
|
| 92 |
nds.tick(len); |
| 93 |
|
| 94 |
for (int i=0; i<16; i++) { |
| 95 |
oscBuf[i]->end(len); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
void DivPlatformNDS::postProcess(short* buf, int outIndex, size_t len, int sampleRate) { |
| 100 |
// this is where we handle global volume. it is faster than doing it on each blip... |
| 101 |
for (size_t i=0; i<len; i++) { |
| 102 |
buf[i]=((buf[i]*globalVolume)>>7); |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
u8 DivPlatformNDS::read_byte(u32 addr) { |
| 107 |
if (addr<getSampleMemCapacity()) { |
| 108 |
return sampleMem[addr]; |
| 109 |
} |
| 110 |
return 0; |
| 111 |
} |
| 112 |
|
| 113 |
void DivPlatformNDS::write_byte(u32 addr, u8 data) { |
| 114 |
if (addr<getSampleMemCapacity()) { |
| 115 |
sampleMem[addr]=data; |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
void DivPlatformNDS::tick(bool sysTick) { |
| 120 |
for (int i=0; i<16; i++) { |
| 121 |
chan[i].std.next(); |
| 122 |
if (chan[i].std.vol.had) { |
| 123 |
chan[i].outVol=((chan[i].vol&0x7f)*MIN(chan[i].macroVolMul,chan[i].std.vol.val))/chan[i].macroVolMul; |
| 124 |
writeOutVol(i); |
| 125 |
} |
| 126 |
if (NEW_ARP_STRAT) { |
| 127 |
chan[i].handleArp(); |
| 128 |
} else if (chan[i].std.arp.had) { |
| 129 |
if (!chan[i].inPorta) { |
| 130 |
chan[i].baseFreq=NOTE_PERIODIC(parent->calcArp(chan[i].note,chan[i].std.arp.val)); |
| 131 |
} |
| 132 |
chan[i].freqChanged=true; |
| 133 |
} |
| 134 |
if (chan[i].std.pitch.had) { |
| 135 |
if (chan[i].std.pitch.mode) { |
| 136 |
chan[i].pitch2+=chan[i].std.pitch.val; |
| 137 |
CLAMP_VAR(chan[i].pitch2,-32768,32767); |
| 138 |
} else { |
| 139 |
chan[i].pitch2=chan[i].std.pitch.val; |
| 140 |
} |
| 141 |
chan[i].freqChanged=true; |
| 142 |
} |
| 143 |
if ((i>=8) && (i<14)) { |
| 144 |
if (chan[i].std.duty.had) { |
| 145 |
chan[i].duty=chan[i].std.duty.val; |
| 146 |
if ((!chan[i].pcm)) { // pulse |
| 147 |
rWrite8(0x03+i*16,(rRead8(0x03+i*16)&0xe8)|(chan[i].duty&7)); |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
if (chan[i].std.panL.had) { // panning |
| 152 |
chan[i].panning=0x40+chan[i].std.panL.val; |
| 153 |
rWrite8(0x02+i*16,chan[i].panning); |
| 154 |
} |
| 155 |
if (chan[i].std.phaseReset.had) { |
| 156 |
if ((chan[i].std.phaseReset.val==1) && chan[i].active) { |
| 157 |
chan[i].audPos=0; |
| 158 |
chan[i].setPos=true; |
| 159 |
if ((rRead8(0x03+i*16)&0x80)==0) |
| 160 |
chan[i].busy=true; |
| 161 |
} |
| 162 |
} |
| 163 |
if (chan[i].setPos) { |
| 164 |
// force keyon |
| 165 |
chan[i].keyOn=true; |
| 166 |
chan[i].setPos=false; |
| 167 |
} else { |
| 168 |
chan[i].audPos=0; |
| 169 |
} |
| 170 |
if (chan[i].freqChanged || chan[i].keyOn || chan[i].keyOff) { |
| 171 |
unsigned char ctrl=0; |
| 172 |
if (chan[i].pcm || i<8) { |
| 173 |
DivSample* s=parent->getSample(chan[i].sample); |
| 174 |
switch (s->depth) { |
| 175 |
case DIV_SAMPLE_DEPTH_IMA_ADPCM: ctrl=0x40; break; |
| 176 |
case DIV_SAMPLE_DEPTH_16BIT: ctrl=0x20; break; |
| 177 |
default: ctrl=0x00; break; |
| 178 |
} |
| 179 |
double off=(s->centerRate>=1)?(parent->getCenterRate()/(double)s->centerRate):1.0; |
| 180 |
chan[i].freq=0x10000-(off*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)); |
| 181 |
if (chan[i].freq<0) chan[i].freq=0; |
| 182 |
if (chan[i].freq>65535) chan[i].freq=65535; |
| 183 |
if ((!chan[i].keyOn) && ((rRead8(0x03+i*16)&0x80)==0)) |
| 184 |
chan[i].busy=false; |
| 185 |
ctrl|=(chan[i].busy?0x80:0)|((s->isLoopable())?0x08:0x10); |
| 186 |
if (chan[i].keyOn) { |
| 187 |
unsigned int start=0; |
| 188 |
int loopStart=0; |
| 189 |
int loopEnd=0; |
| 190 |
int end=0; |
| 191 |
if (chan[i].sample>=0 && chan[i].sample<parent->song.sampleLen) { |
| 192 |
start=sampleOff[chan[i].sample]; |
| 193 |
end=s->getCurBufLen()/4; |
| 194 |
} |
| 195 |
if (chan[i].audPos>0) { |
| 196 |
switch (s->depth) { |
| 197 |
case DIV_SAMPLE_DEPTH_IMA_ADPCM: start+=chan[i].audPos/2; end-=(chan[i].audPos/8); break; |
| 198 |
case DIV_SAMPLE_DEPTH_16BIT: start+=chan[i].audPos*2; end-=(chan[i].audPos/2); break; |
| 199 |
default: start+=chan[i].audPos; end-=(chan[i].audPos/4); break; |
| 200 |
} |
| 201 |
} |
| 202 |
if (s->isLoopable()) { |
| 203 |
if (chan[i].audPos>0) { |
| 204 |
switch (s->depth) { |
| 205 |
case DIV_SAMPLE_DEPTH_IMA_ADPCM: |
| 206 |
loopStart=(s->loopStart-chan[i].audPos)/8; |
| 207 |
loopEnd=(s->loopEnd-s->loopStart)/8; |
| 208 |
if (chan[i].audPos>(unsigned int)s->loopStart) { |
| 209 |
loopStart=0; |
| 210 |
loopEnd-=(chan[i].audPos-s->loopStart)/8; |
| 211 |
} |
| 212 |
break; |
| 213 |
case DIV_SAMPLE_DEPTH_16BIT: |
| 214 |
loopStart=(s->loopStart-chan[i].audPos)/2; |
| 215 |
loopEnd=(s->loopEnd-s->loopStart)/2; |
| 216 |
if (chan[i].audPos>(unsigned int)s->loopStart) { |
| 217 |
loopStart=0; |
| 218 |
loopEnd-=(chan[i].audPos-s->loopStart)/2; |
| 219 |
} |
| 220 |
break; |
| 221 |
default: |
| 222 |
loopStart=(s->loopStart-chan[i].audPos)/4; |
| 223 |
loopEnd=(s->loopEnd-s->loopStart)/4; |
| 224 |
if (chan[i].audPos>(unsigned int)s->loopStart) { |
| 225 |
loopStart=0; |
| 226 |
loopEnd-=(chan[i].audPos-s->loopStart)/4; |
| 227 |
} |
| 228 |
break; |
| 229 |
} |
| 230 |
} else { |
| 231 |
switch (s->depth) { |
| 232 |
case DIV_SAMPLE_DEPTH_IMA_ADPCM: loopStart=s->loopStart/8; loopEnd=(s->loopEnd-s->loopStart)/8; break; |
| 233 |
case DIV_SAMPLE_DEPTH_16BIT: loopStart=s->loopStart/2; loopEnd=(s->loopEnd-s->loopStart)/2; break; |
| 234 |
default: loopStart=s->loopStart/4; loopEnd=(s->loopEnd-s->loopStart)/4; break; |
| 235 |
} |
| 236 |
} |
| 237 |
loopEnd=CLAMP(loopEnd,0,0x3fffff); |
| 238 |
loopStart=CLAMP(loopStart,0,0xffff); |
| 239 |
rWrite16(0x0a+i*16,loopStart); |
| 240 |
rWrite32(0x0c+i*16,loopEnd); |
| 241 |
} else { |
| 242 |
end=CLAMP(end,0,0x3fffff); |
| 243 |
rWrite16(0x0a+i*16,0); |
| 244 |
rWrite32(0x0c+i*16,end&0x3fffff); |
| 245 |
} |
| 246 |
rWrite8(0x03+i*16,ctrl&~0x80); // force keyoff first |
| 247 |
rWrite32(0x04+i*16,start&0x7fffffc); |
| 248 |
} |
| 249 |
} else { |
| 250 |
chan[i].freq=0x10000-(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,8)); |
| 251 |
if (chan[i].freq<0) chan[i].freq=0; |
| 252 |
if (chan[i].freq>65535) chan[i].freq=65535; |
| 253 |
ctrl=(chan[i].active?0xe8:0)|(chan[i].duty&7); |
| 254 |
if (chan[i].keyOff || chan[i].keyOn) { |
| 255 |
rWrite8(0x03+i*16,ctrl&~0x80); // force keyoff first |
| 256 |
} |
| 257 |
} |
| 258 |
chan[i].keyOn=false; |
| 259 |
if (chan[i].keyOff) { |
| 260 |
chan[i].keyOff=false; |
| 261 |
} |
| 262 |
if (chan[i].freqChanged) { |
| 263 |
rWrite16(0x08+i*16,chan[i].freq&0xffff); |
| 264 |
chan[i].freqChanged=false; |
| 265 |
} |
| 266 |
rWrite8(0x03+i*16,ctrl); |
| 267 |
} |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
int DivPlatformNDS::dispatch(DivCommand c) { |
| 272 |
switch (c.cmd) { |
| 273 |
case DIV_CMD_NOTE_ON: { |
| 274 |
DivInstrument* ins=parent->getIns(chan[c.chan].ins,DIV_INS_NDS); |
| 275 |
if (ins->type==DIV_INS_AMIGA || ins->amiga.useSample || (c.chan<8)) { |
| 276 |
chan[c.chan].pcm=true; |
| 277 |
} else { |
| 278 |
chan[c.chan].pcm=false; |
| 279 |
} |
| 280 |
if (chan[c.chan].pcm || (c.chan<8)) { |
| 281 |
chan[c.chan].macroVolMul=ins->type==DIV_INS_AMIGA?64:127; |
| 282 |
if (c.value!=DIV_NOTE_NULL) { |
| 283 |
chan[c.chan].sample=ins->amiga.getSample(c.value); |
| 284 |
chan[c.chan].sampleNote=c.value; |
| 285 |
c.value=ins->amiga.getFreq(c.value); |
| 286 |
chan[c.chan].sampleNoteDelta=c.value-chan[c.chan].sampleNote; |
| 287 |
} |
| 288 |
if (chan[c.chan].sample<0 || chan[c.chan].sample>=parent->song.sampleLen) { |
| 289 |
chan[c.chan].sample=-1; |
| 290 |
} |
| 291 |
} else { |
| 292 |
chan[c.chan].macroVolMul=127; |
| 293 |
} |
| 294 |
if (c.value!=DIV_NOTE_NULL) { |
| 295 |
chan[c.chan].baseFreq=NOTE_PERIODIC(c.value); |
| 296 |
chan[c.chan].freqChanged=true; |
| 297 |
chan[c.chan].note=c.value; |
| 298 |
} |
| 299 |
chan[c.chan].active=true; |
| 300 |
chan[c.chan].busy=true; |
| 301 |
chan[c.chan].keyOn=true; |
| 302 |
chan[c.chan].macroInit(ins); |
| 303 |
if (!parent->song.brokenOutVol && !chan[c.chan].std.vol.will) { |
| 304 |
chan[c.chan].outVol=chan[c.chan].vol; |
| 305 |
} |
| 306 |
break; |
| 307 |
} |
| 308 |
case DIV_CMD_NOTE_OFF: |
| 309 |
chan[c.chan].sample=-1; |
| 310 |
chan[c.chan].active=false; |
| 311 |
chan[c.chan].busy=false; |
| 312 |
chan[c.chan].keyOff=true; |
| 313 |
chan[c.chan].macroInit(NULL); |
| 314 |
break; |
| 315 |
case DIV_CMD_NOTE_OFF_ENV: |
| 316 |
case DIV_CMD_ENV_RELEASE: |
| 317 |
chan[c.chan].std.release(); |
| 318 |
break; |
| 319 |
case DIV_CMD_INSTRUMENT: |
| 320 |
if (chan[c.chan].ins!=c.value || c.value2==1) { |
| 321 |
chan[c.chan].ins=c.value; |
| 322 |
} |
| 323 |
break; |
| 324 |
case DIV_CMD_ADPCMA_GLOBAL_VOLUME: { |
| 325 |
if (globalVolume!=(c.value&0x7f)) { |
| 326 |
globalVolume=c.value&0x7f; |
| 327 |
rWrite32(0x100,0x8000|globalVolume); |
| 328 |
} |
| 329 |
break; |
| 330 |
} |
| 331 |
case DIV_CMD_VOLUME: |
| 332 |
if (chan[c.chan].vol!=c.value) { |
| 333 |
chan[c.chan].vol=c.value; |
| 334 |
if (!chan[c.chan].std.vol.has) { |
| 335 |
chan[c.chan].outVol=c.value; |
| 336 |
writeOutVol(c.chan); |
| 337 |
} |
| 338 |
} |
| 339 |
break; |
| 340 |
case DIV_CMD_GET_VOLUME: |
| 341 |
if (chan[c.chan].std.vol.has) { |
| 342 |
return chan[c.chan].vol; |
| 343 |
} |
| 344 |
return chan[c.chan].outVol; |
| 345 |
break; |
| 346 |
case DIV_CMD_PANNING: |
| 347 |
chan[c.chan].panning=MIN(parent->convertPanSplitToLinearLR(c.value,c.value2,127),127); |
| 348 |
rWrite8(0x02+c.chan*16,chan[c.chan].panning); |
| 349 |
break; |
| 350 |
case DIV_CMD_PITCH: |
| 351 |
chan[c.chan].pitch=c.value; |
| 352 |
chan[c.chan].freqChanged=true; |
| 353 |
break; |
| 354 |
case DIV_CMD_NOTE_PORTA: { |
| 355 |
int destFreq=NOTE_PERIODIC(c.value2+chan[c.chan].sampleNoteDelta); |
| 356 |
bool return2=false; |
| 357 |
if (destFreq>chan[c.chan].baseFreq) { |
| 358 |
chan[c.chan].baseFreq+=c.value; |
| 359 |
if (chan[c.chan].baseFreq>=destFreq) { |
| 360 |
chan[c.chan].baseFreq=destFreq; |
| 361 |
return2=true; |
| 362 |
} |
| 363 |
} else { |
| 364 |
chan[c.chan].baseFreq-=c.value; |
| 365 |
if (chan[c.chan].baseFreq<=destFreq) { |
| 366 |
chan[c.chan].baseFreq=destFreq; |
| 367 |
return2=true; |
| 368 |
} |
| 369 |
} |
| 370 |
chan[c.chan].freqChanged=true; |
| 371 |
if (return2) { |
| 372 |
chan[c.chan].inPorta=false; |
| 373 |
return 2; |
| 374 |
} |
| 375 |
break; |
| 376 |
} |
| 377 |
case DIV_CMD_STD_NOISE_MODE: |
| 378 |
if ((c.chan>=8) && (c.chan<14) && (!chan[c.chan].pcm)) { // pulse |
| 379 |
chan[c.chan].duty=c.value; |
| 380 |
rWrite8(0x03+c.chan*16,(rRead8(0x03+c.chan*16)&0xe8)|(chan[c.chan].duty&7)); |
| 381 |
} |
| 382 |
break; |
| 383 |
case DIV_CMD_LEGATO: { |
| 384 |
chan[c.chan].baseFreq=NOTE_PERIODIC(c.value+chan[c.chan].sampleNoteDelta+((HACKY_LEGATO_MESS)?(chan[c.chan].std.arp.val-12):(0))); |
| 385 |
chan[c.chan].freqChanged=true; |
| 386 |
chan[c.chan].note=c.value; |
| 387 |
break; |
| 388 |
} |
| 389 |
case DIV_CMD_PRE_PORTA: |
| 390 |
if (chan[c.chan].active && c.value2) { |
| 391 |
if (parent->song.resetMacroOnPorta) chan[c.chan].macroInit(parent->getIns(chan[c.chan].ins,DIV_INS_NDS)); |
| 392 |
} |
| 393 |
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); |
| 394 |
chan[c.chan].inPorta=c.value; |
| 395 |
break; |
| 396 |
case DIV_CMD_SAMPLE_POS: |
| 397 |
if (chan[c.chan].pcm || (c.chan<8)) { |
| 398 |
chan[c.chan].audPos=c.value; |
| 399 |
chan[c.chan].setPos=true; |
| 400 |
} |
| 401 |
break; |
| 402 |
case DIV_CMD_GET_VOLMAX: |
| 403 |
return 127; |
| 404 |
break; |
| 405 |
case DIV_CMD_MACRO_OFF: |
| 406 |
chan[c.chan].std.mask(c.value,true); |
| 407 |
break; |
| 408 |
case DIV_CMD_MACRO_ON: |
| 409 |
chan[c.chan].std.mask(c.value,false); |
| 410 |
break; |
| 411 |
case DIV_CMD_MACRO_RESTART: |
| 412 |
chan[c.chan].std.restart(c.value); |
| 413 |
break; |
| 414 |
default: |
| 415 |
break; |
| 416 |
} |
| 417 |
return 1; |
| 418 |
} |
| 419 |
|
| 420 |
void DivPlatformNDS::writeOutVol(int ch) { |
| 421 |
unsigned char val=isMuted[ch]?0:chan[ch].outVol; |
| 422 |
rWrite8(0x00+ch*16,val); |
| 423 |
} |
| 424 |
|
| 425 |
void DivPlatformNDS::muteChannel(int ch, bool mute) { |
| 426 |
isMuted[ch]=mute; |
| 427 |
writeOutVol(ch); |
| 428 |
} |
| 429 |
|
| 430 |
void DivPlatformNDS::forceIns() { |
| 431 |
for (int i=0; i<16; i++) { |
| 432 |
chan[i].insChanged=true; |
| 433 |
chan[i].freqChanged=true; |
| 434 |
chan[i].sample=-1; |
| 435 |
|
| 436 |
rWrite8(0x02+i*16,chan[i].panning); |
| 437 |
writeOutVol(i); |
| 438 |
} |
| 439 |
} |
| 440 |
|
| 441 |
void* DivPlatformNDS::getChanState(int ch) { |
| 442 |
return &chan[ch]; |
| 443 |
} |
| 444 |
|
| 445 |
DivMacroInt* DivPlatformNDS::getChanMacroInt(int ch) { |
| 446 |
return &chan[ch].std; |
| 447 |
} |
| 448 |
|
| 449 |
unsigned short DivPlatformNDS::getPan(int ch) { |
| 450 |
return parent->convertPanLinearToSplit(chan[ch].panning,8,127); |
| 451 |
} |
| 452 |
|
| 453 |
DivDispatchOscBuffer* DivPlatformNDS::getOscBuffer(int ch) { |
| 454 |
return oscBuf[ch]; |
| 455 |
} |
| 456 |
|
| 457 |
void DivPlatformNDS::reset() { |
| 458 |
memset(regPool,0,288); |
| 459 |
nds.reset(); |
| 460 |
writes.clear(); |
| 461 |
globalVolume=0x7f; |
| 462 |
lastOut[0]=0; |
| 463 |
lastOut[1]=0; |
| 464 |
rWrite32(0x100,0x8000|globalVolume); // enable keyon |
| 465 |
rWrite32(0x104,0x200); // initialize bias |
| 466 |
for (int i=0; i<16; i++) { |
| 467 |
chan[i]=DivPlatformNDS::Channel(); |
| 468 |
chan[i].std.setEngine(parent); |
| 469 |
rWrite32(0x00+i*16,isMuted[i]?0x400000:0x40007f); |
| 470 |
} |
| 471 |
} |
| 472 |
|
| 473 |
int DivPlatformNDS::getOutputCount() { |
| 474 |
return 2; |
| 475 |
} |
| 476 |
|
| 477 |
bool DivPlatformNDS::hasAcquireDirect() { |
| 478 |
return true; |
| 479 |
} |
| 480 |
|
| 481 |
void DivPlatformNDS::notifyInsChange(int ins) { |
| 482 |
for (int i=0; i<16; i++) { |
| 483 |
if (chan[i].ins==ins) { |
| 484 |
chan[i].insChanged=true; |
| 485 |
} |
| 486 |
} |
| 487 |
} |
| 488 |
|
| 489 |
void DivPlatformNDS::notifyWaveChange(int wave) { |
| 490 |
// TODO when wavetables are added |
| 491 |
// TODO they probably won't be added unless the samples reside in RAM |
| 492 |
} |
| 493 |
|
| 494 |
void DivPlatformNDS::notifyInsDeletion(void* ins) { |
| 495 |
for (int i=0; i<16; i++) { |
| 496 |
chan[i].std.notifyInsDeletion((DivInstrument*)ins); |
| 497 |
} |
| 498 |
} |
| 499 |
|
| 500 |
void DivPlatformNDS::poke(unsigned int addr, unsigned short val) { |
| 501 |
rWrite8(addr,val); |
| 502 |
} |
| 503 |
|
| 504 |
void DivPlatformNDS::poke(std::vector<DivRegWrite>& wlist) { |
| 505 |
for (DivRegWrite& i: wlist) rWrite8(i.addr,i.val); |
| 506 |
} |
| 507 |
|
| 508 |
unsigned char* DivPlatformNDS::getRegisterPool() { |
| 509 |
return regPool; |
| 510 |
} |
| 511 |
|
| 512 |
int DivPlatformNDS::getRegisterPoolSize() { |
| 513 |
return 288; |
| 514 |
} |
| 515 |
|
| 516 |
float DivPlatformNDS::getPostAmp() { |
| 517 |
return 1.0f; |
| 518 |
} |
| 519 |
|
| 520 |
const void* DivPlatformNDS::getSampleMem(int index) { |
| 521 |
return index == 0 ? sampleMem : NULL; |
| 522 |
} |
| 523 |
|
| 524 |
size_t DivPlatformNDS::getSampleMemCapacity(int index) { |
| 525 |
return index == 0 ? (isDSi?16777216:4194304) : 0; |
| 526 |
} |
| 527 |
|
| 528 |
size_t DivPlatformNDS::getSampleMemUsage(int index) { |
| 529 |
return index == 0 ? sampleMemLen : 0; |
| 530 |
} |
| 531 |
|
| 532 |
bool DivPlatformNDS::isSampleLoaded(int index, int sample) { |
| 533 |
if (index!=0) return false; |
| 534 |
if (sample<0 || sample>32767) return false; |
| 535 |
return sampleLoaded[sample]; |
| 536 |
} |
| 537 |
|
| 538 |
const DivMemoryComposition* DivPlatformNDS::getMemCompo(int index) { |
| 539 |
if (index!=0) return NULL; |
| 540 |
return &memCompo; |
| 541 |
} |
| 542 |
|
| 543 |
void DivPlatformNDS::renderSamples(int sysID) { |
| 544 |
memset(sampleMem,0,16777216); |
| 545 |
memset(sampleOff,0,32768*sizeof(unsigned int)); |
| 546 |
memset(sampleLoaded,0,32768*sizeof(bool)); |
| 547 |
|
| 548 |
memCompo=DivMemoryComposition(); |
| 549 |
memCompo.name="Main Memory"; |
| 550 |
|
| 551 |
size_t memPos=0; |
| 552 |
for (int i=0; i<parent->song.sampleLen; i++) { |
| 553 |
DivSample* s=parent->song.sample[i]; |
| 554 |
if (!s->renderOn[0][sysID]) { |
| 555 |
sampleOff[i]=0; |
| 556 |
continue; |
| 557 |
} |
| 558 |
|
| 559 |
int length=0; |
| 560 |
unsigned char* src=NULL; |
| 561 |
switch (s->depth) { |
| 562 |
case DIV_SAMPLE_DEPTH_16BIT: |
| 563 |
length=MIN(16777212,s->length16); |
| 564 |
src=(unsigned char*)s->data16; |
| 565 |
break; |
| 566 |
case DIV_SAMPLE_DEPTH_IMA_ADPCM: |
| 567 |
length=MIN(16777212,s->lengthIMA); |
| 568 |
src=(unsigned char*)s->dataIMA; |
| 569 |
break; |
| 570 |
default: |
| 571 |
length=MIN(16777212,s->length8); |
| 572 |
src=(unsigned char*)s->data8; |
| 573 |
break; |
| 574 |
} |
| 575 |
|
| 576 |
int actualLength=MIN((int)(getSampleMemCapacity()-memPos),length); |
| 577 |
if (actualLength>0) { |
| 578 |
memcpy(&sampleMem[memPos],src,actualLength); |
| 579 |
sampleOff[i]=memPos; |
| 580 |
memCompo.entries.push_back(DivMemoryEntry(DIV_MEMORY_SAMPLE,"Sample",i,memPos,memPos+actualLength)); |
| 581 |
memPos+=actualLength; |
| 582 |
} |
| 583 |
if (actualLength<length) { |
| 584 |
logW("out of NDS PCM memory for sample %d!",i); |
| 585 |
break; |
| 586 |
} |
| 587 |
// align memPos to int |
| 588 |
memPos=(memPos+3)&~3; |
| 589 |
sampleLoaded[i]=true; |
| 590 |
} |
| 591 |
sampleMemLen=memPos; |
| 592 |
|
| 593 |
memCompo.capacity=(isDSi?16777216:4194304); |
| 594 |
memCompo.used=sampleMemLen; |
| 595 |
} |
| 596 |
|
| 597 |
void DivPlatformNDS::setFlags(const DivConfig& flags) { |
| 598 |
isDSi=flags.getBool("chipType",0); |
| 599 |
chipClock=33513982; |
| 600 |
CHECK_CUSTOM_CLOCK; |
| 601 |
rate=chipClock/2; |
| 602 |
for (int i=0; i<16; i++) { |
| 603 |
oscBuf[i]->setRate(rate); |
| 604 |
} |
| 605 |
memCompo.capacity=(isDSi?16777216:4194304); |
| 606 |
} |
| 607 |
|
| 608 |
int DivPlatformNDS::init(DivEngine* p, int channels, int sugRate, const DivConfig& flags) { |
| 609 |
parent=p; |
| 610 |
dumpWrites=false; |
| 611 |
skipRegisterWrites=false; |
| 612 |
|
| 613 |
for (int i=0; i<16; i++) { |
| 614 |
isMuted[i]=false; |
| 615 |
oscBuf[i]=new DivDispatchOscBuffer; |
| 616 |
} |
| 617 |
sampleMem=new unsigned char[16777216]; |
| 618 |
sampleMemLen=0; |
| 619 |
nds.reset(); |
| 620 |
setFlags(flags); |
| 621 |
reset(); |
| 622 |
|
| 623 |
return 16; |
| 624 |
} |
| 625 |
|
| 626 |
void DivPlatformNDS::quit() { |
| 627 |
delete[] sampleMem; |
| 628 |
for (int i=0; i<16; i++) { |
| 629 |
delete oscBuf[i]; |
| 630 |
} |
| 631 |
} |
| 632 |
|
| 633 |
// initialization of important arrays |
| 634 |
DivPlatformNDS::DivPlatformNDS(): |
| 635 |
DivDispatch(), |
| 636 |
nds_sound_intf(), |
| 637 |
nds(*this) { |
| 638 |
sampleOff=new unsigned int[32768]; |
| 639 |
sampleLoaded=new bool[32768]; |
| 640 |
} |
| 641 |
|
| 642 |
DivPlatformNDS::~DivPlatformNDS() { |
| 643 |
delete[] sampleOff; |
| 644 |
delete[] sampleLoaded; |
| 645 |
} |