| 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 "gbaminmod.h" |
| 21 |
#include "../engine.h" |
| 22 |
#include "../../ta-log.h" |
| 23 |
#include <math.h> |
| 24 |
|
| 25 |
#define CHIP_FREQBASE 16777216 |
| 26 |
|
| 27 |
#define rWrite(a,v) {regPool[a]=v;} |
| 28 |
|
| 29 |
const char* regCheatSheetMinMod[]={ |
| 30 |
"CHx_Counter", "x0", |
| 31 |
"CHx_Address", "x2", |
| 32 |
"CHx_LastLeft", "x4", |
| 33 |
"CHx_LastRight", "x6", |
| 34 |
"CHx_Freq", "x8", |
| 35 |
"CHx_LoopEnd", "xA", |
| 36 |
"CHx_LoopStart", "xC", |
| 37 |
"CHx_VolumeLeft", "xE", |
| 38 |
"CHx_VolumeRight", "xF", |
| 39 |
NULL |
| 40 |
}; |
| 41 |
|
| 42 |
const char** DivPlatformGBAMinMod::getRegisterSheet() { |
| 43 |
return regCheatSheetMinMod; |
| 44 |
} |
| 45 |
|
| 46 |
void DivPlatformGBAMinMod::acquire(short** buf, size_t len) { |
| 47 |
size_t sampPos=mixBufReadPos&3; |
| 48 |
bool newSamp=false; |
| 49 |
// cache channel registers that might change |
| 50 |
struct { |
| 51 |
uint64_t address; |
| 52 |
unsigned int freq, loopEnd, loopStart; |
| 53 |
short volL, volR; |
| 54 |
} chState[16]; |
| 55 |
for (int i=0; i<chanMax; i++) { |
| 56 |
unsigned short* chReg=®Pool[i*16]; |
| 57 |
chState[i].address=chReg[0]|((uint64_t)chReg[1]<<16)|((uint64_t)chReg[2]<<32)|((uint64_t)chReg[3]<<48); |
| 58 |
chState[i].freq=chReg[8]|((unsigned int)chReg[9]<<16); |
| 59 |
chState[i].loopEnd=chReg[10]|((unsigned int)chReg[11]<<16); |
| 60 |
chState[i].loopStart=chReg[12]|((unsigned int)chReg[13]<<16); |
| 61 |
chState[i].volL=(short)chReg[14]; |
| 62 |
chState[i].volR=(short)chReg[15]; |
| 63 |
} |
| 64 |
for (int i=0; i<chanMax; i++) { |
| 65 |
oscBuf[i]->begin(len); |
| 66 |
} |
| 67 |
for (size_t h=0; h<len; h++) { |
| 68 |
while (sampTimer>=sampCycles) { |
| 69 |
// the driver generates 4 samples at a time and can be start-offset |
| 70 |
sampPos=mixBufReadPos&3; |
| 71 |
if (sampPos==mixBufOffset) { |
| 72 |
for (size_t j=mixBufOffset; j<4; j++) { |
| 73 |
mixOut[0][j]=0; |
| 74 |
mixOut[1][j]=0; |
| 75 |
} |
| 76 |
for (int i=0; i<chanMax; i++) { |
| 77 |
for (size_t j=mixBufOffset; j<4; j++) { |
| 78 |
unsigned int lastAddr=chState[i].address>>32; |
| 79 |
chState[i].address+=((uint64_t)chState[i].freq)<<8; |
| 80 |
unsigned int newAddr=chState[i].address>>32; |
| 81 |
if (newAddr!=lastAddr) { |
| 82 |
if (newAddr>=chState[i].loopEnd) { |
| 83 |
newAddr=newAddr-chState[i].loopEnd+chState[i].loopStart; |
| 84 |
chState[i].address=(chState[i].address&0xffffffff)|((uint64_t)newAddr<<32); |
| 85 |
} |
| 86 |
int newSamp=0; |
| 87 |
switch (newAddr>>24) { |
| 88 |
case 2: // wavetable |
| 89 |
newAddr&=0x0003ffff; |
| 90 |
if (newAddr<sizeof(wtMem)) { |
| 91 |
newSamp=wtMem[newAddr]; |
| 92 |
} |
| 93 |
break; |
| 94 |
case 3: // echo |
| 95 |
newAddr&=0x00007fff; |
| 96 |
if (newAddr>0x800) { |
| 97 |
newSamp=mixBuf[(newAddr-0x800)/1024][newAddr&1023]; |
| 98 |
} |
| 99 |
break; |
| 100 |
case 8: // sample |
| 101 |
case 9: |
| 102 |
case 10: |
| 103 |
case 11: |
| 104 |
case 12: |
| 105 |
newSamp=sampleMem[newAddr&0x01ffffff]; |
| 106 |
break; |
| 107 |
} |
| 108 |
chanOut[i][0]=newSamp*chState[i].volL; |
| 109 |
chanOut[i][1]=newSamp*chState[i].volR; |
| 110 |
} |
| 111 |
int outL=chanOut[i][0]; |
| 112 |
int outR=chanOut[i][1]; |
| 113 |
int outA=(chan[i].invertL==chan[i].invertR)?outL+outR:outL-outR; |
| 114 |
mixOut[0][j]+=(unsigned char)(outL>>15); |
| 115 |
mixOut[1][j]+=(unsigned char)(outR>>15); |
| 116 |
oscOut[i][j]=volScale>0?outA*64/volScale:0; |
| 117 |
} |
| 118 |
} |
| 119 |
for (size_t j=mixBufOffset; j<4; j++) { |
| 120 |
mixBuf[mixBufPage][mixBufWritePos]=mixOut[0][j]; |
| 121 |
mixBuf[mixBufPage+1][mixBufWritePos]=mixOut[1][j]; |
| 122 |
mixBufWritePos++; |
| 123 |
} |
| 124 |
mixBufOffset=0; |
| 125 |
} |
| 126 |
newSamp=true; |
| 127 |
mixBufReadPos++; |
| 128 |
sampTimer-=sampCycles; |
| 129 |
} |
| 130 |
if (newSamp) { |
| 131 |
// assuming max PCM FIFO volume |
| 132 |
sampL=((short)mixOut[0][sampPos]<<8)&(0xff80<<(9-dacDepth)); |
| 133 |
sampR=((short)mixOut[1][sampPos]<<8)&(0xff80<<(9-dacDepth)); |
| 134 |
newSamp=false; |
| 135 |
} |
| 136 |
buf[0][h]=sampL; |
| 137 |
buf[1][h]=sampR; |
| 138 |
for (int i=0; i<chanMax; i++) { |
| 139 |
oscBuf[i]->putSample(h,oscOut[i][sampPos]); |
| 140 |
} |
| 141 |
for (int i=chanMax; i<16; i++) { |
| 142 |
oscBuf[i]->putSample(h,0); |
| 143 |
} |
| 144 |
while (updTimer>=updCycles) { |
| 145 |
// flip buffer |
| 146 |
// logV("ut=%d,pg=%d,w=%d,r=%d,sc=%d,st=%d",updTimer,mixBufPage,mixBufWritePos,mixBufReadPos,sampCycles,sampTimer); |
| 147 |
mixMemCompo.entries[mixBufPage].end=mixMemCompo.entries[mixBufPage].begin+mixBufWritePos; |
| 148 |
mixMemCompo.entries[mixBufPage+1].end=mixMemCompo.entries[mixBufPage+1].begin+mixBufWritePos; |
| 149 |
mixBufPage=(mixBufPage+2)%(mixBufs*2); |
| 150 |
memset(mixBuf[mixBufPage],0,sizeof(mixBuf[mixBufPage])); |
| 151 |
memset(mixBuf[mixBufPage+1],0,sizeof(mixBuf[mixBufPage+1])); |
| 152 |
// emulate buffer loss prevention and buffer copying |
| 153 |
sampsRendered+=mixBufReadPos; |
| 154 |
mixBufOffset=mixBufWritePos-mixBufReadPos; |
| 155 |
for (size_t j=0; j<mixBufOffset; j++) { |
| 156 |
mixOut[0][j]=mixOut[0][(mixBufReadPos&3)+j]; |
| 157 |
mixOut[1][j]=mixOut[1][(mixBufReadPos&3)+j]; |
| 158 |
mixBuf[mixBufPage][j]=mixOut[0][j]; |
| 159 |
mixBuf[mixBufPage+1][j]=mixOut[1][j]; |
| 160 |
} |
| 161 |
mixBufReadPos=0; |
| 162 |
mixBufWritePos=mixBufOffset; |
| 163 |
// check for echo channels and give them proper addresses |
| 164 |
for (int i=0; i<chanMax; i++) { |
| 165 |
unsigned char echoDelay=MIN(chan[i].echo&0x0f,mixBufs-1); |
| 166 |
if(echoDelay) { |
| 167 |
echoDelay=echoDelay*2-((chan[i].echo&0x10)?0:1); |
| 168 |
size_t echoPage=(mixBufPage+mixBufs*2-echoDelay)%(mixBufs*2); |
| 169 |
chState[i].address=(0x03000800ULL+echoPage*1024)<<32; |
| 170 |
chState[i].loopStart=0-echoDelay; |
| 171 |
chState[i].loopEnd=0-echoDelay; |
| 172 |
} |
| 173 |
} |
| 174 |
updTimer-=updCycles; |
| 175 |
updCyclesTotal+=updCycles; |
| 176 |
// recalculate update timer from a new tick rate |
| 177 |
float hz=parent->getCurHz(); |
| 178 |
float updCyclesNew=(hz>=1)?(16777216.f/hz):1; |
| 179 |
// the maximum buffer size in the default multi-rate config is 1024 samples |
| 180 |
// (so 15 left/right buffers + mixer code fit the entire 32k of internal RAM) |
| 181 |
// if the driver determines that the current tick rate is too low, it will |
| 182 |
// internally double the rate until the resulting buffer size fits |
| 183 |
while (true) { |
| 184 |
updCycles=floorf(updCyclesNew); |
| 185 |
// emulate prescaler rounding |
| 186 |
if (updCycles>=65536*256) { |
| 187 |
updCycles&=~1024; |
| 188 |
} else if (updCycles>=65536*64) { |
| 189 |
updCycles&=~256; |
| 190 |
} else if (updCycles>=65536) { |
| 191 |
updCycles&=~64; |
| 192 |
} |
| 193 |
unsigned int bufSize=(updCycles/sampCycles+3)&~3; |
| 194 |
if (bufSize<1024 || updCyclesNew<1) { |
| 195 |
break; |
| 196 |
} |
| 197 |
updCyclesNew/=2; |
| 198 |
} |
| 199 |
} |
| 200 |
updTimer+=1<<dacDepth; |
| 201 |
sampTimer+=1<<dacDepth; |
| 202 |
} |
| 203 |
for (int i=0; i<chanMax; i++) { |
| 204 |
oscBuf[i]->end(len); |
| 205 |
} |
| 206 |
// write back changed cached channel registers |
| 207 |
for (int i=0; i<chanMax; i++) { |
| 208 |
unsigned short* chReg=®Pool[i*16]; |
| 209 |
chReg[0]=chState[i].address&0xffff; |
| 210 |
chReg[1]=(chState[i].address>>16)&0xffff; |
| 211 |
chReg[2]=(chState[i].address>>32)&0xffff; |
| 212 |
chReg[3]=(chState[i].address>>48)&0xffff; |
| 213 |
chReg[4]=(chanOut[i][0]>>7)&0xff00; |
| 214 |
chReg[5]=0; |
| 215 |
chReg[6]=(chanOut[i][1]>>7)&0xff00; |
| 216 |
chReg[7]=0; |
| 217 |
chReg[10]=chState[i].loopEnd&0xffff; |
| 218 |
chReg[11]=(chState[i].loopEnd>>16)&0xffff; |
| 219 |
chReg[12]=chState[i].loopStart&0xffff; |
| 220 |
chReg[13]=(chState[i].loopStart>>16)&0xffff; |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
void DivPlatformGBAMinMod::tick(bool sysTick) { |
| 225 |
// collect stats for display in chip config |
| 226 |
// logV("rendered=%d,updTot=%d",sampsRendered,updCyclesTotal); |
| 227 |
if (sysTick && updCyclesTotal>0) { |
| 228 |
// assuming new sample, L!=R and lowest ROM access wait in all channels |
| 229 |
// this gives 39.5 cycles/sample, rounded up to 40 for loops |
| 230 |
maxCPU=(float)sampsRendered*chanMax*40/(float)updCyclesTotal; |
| 231 |
} |
| 232 |
sampsRendered=0; |
| 233 |
updCyclesTotal=0; |
| 234 |
|
| 235 |
for (int i=0; i<chanMax; i++) { |
| 236 |
chan[i].std.next(); |
| 237 |
if (chan[i].std.vol.had) { |
| 238 |
chan[i].outVol=(chan[i].vol*MIN(chan[i].macroVolMul,chan[i].std.vol.val))/chan[i].macroVolMul; |
| 239 |
chan[i].volChangedL=true; |
| 240 |
chan[i].volChangedR=true; |
| 241 |
} |
| 242 |
if (NEW_ARP_STRAT) { |
| 243 |
chan[i].handleArp(); |
| 244 |
} else if (chan[i].std.arp.had) { |
| 245 |
if (!chan[i].inPorta) { |
| 246 |
chan[i].baseFreq=NOTE_FREQUENCY(parent->calcArp(chan[i].note,chan[i].std.arp.val)); |
| 247 |
} |
| 248 |
chan[i].freqChanged=true; |
| 249 |
} |
| 250 |
if (chan[i].useWave && chan[i].std.wave.had) { |
| 251 |
if (chan[i].wave!=chan[i].std.wave.val || chan[i].ws.activeChanged()) { |
| 252 |
chan[i].wave=chan[i].std.wave.val; |
| 253 |
chan[i].ws.changeWave1(chan[i].wave); |
| 254 |
} |
| 255 |
} |
| 256 |
if (chan[i].std.pitch.had) { |
| 257 |
if (chan[i].std.pitch.mode) { |
| 258 |
chan[i].pitch2+=chan[i].std.pitch.val; |
| 259 |
CLAMP_VAR(chan[i].pitch2,-32768,32767); |
| 260 |
} else { |
| 261 |
chan[i].pitch2=chan[i].std.pitch.val; |
| 262 |
} |
| 263 |
chan[i].freqChanged=true; |
| 264 |
} |
| 265 |
if (chan[i].std.panL.had) { |
| 266 |
chan[i].chPanL=(255*(chan[i].std.panL.val&255))/chan[i].macroPanMul; |
| 267 |
chan[i].volChangedL=true; |
| 268 |
} |
| 269 |
|
| 270 |
if (chan[i].std.panR.had) { |
| 271 |
chan[i].chPanR=(255*(chan[i].std.panR.val&255))/chan[i].macroPanMul; |
| 272 |
chan[i].volChangedR=true; |
| 273 |
} |
| 274 |
if (chan[i].std.phaseReset.had) { |
| 275 |
if ((chan[i].std.phaseReset.val==1) && chan[i].active) { |
| 276 |
chan[i].audPos=0; |
| 277 |
chan[i].setPos=true; |
| 278 |
} |
| 279 |
} |
| 280 |
if (chan[i].std.ex1.had) { |
| 281 |
if (chan[i].invertL!=(bool)(chan[i].std.ex1.val&16)) { |
| 282 |
chan[i].invertL=chan[i].std.ex1.val&2; |
| 283 |
chan[i].volChangedL=true; |
| 284 |
} |
| 285 |
if (chan[i].invertR!=(bool)(chan[i].std.ex1.val&8)) { |
| 286 |
chan[i].invertR=chan[i].std.ex1.val&1; |
| 287 |
chan[i].volChangedR=true; |
| 288 |
} |
| 289 |
} |
| 290 |
if (chan[i].setPos) { |
| 291 |
// force keyon |
| 292 |
chan[i].keyOn=true; |
| 293 |
chan[i].setPos=false; |
| 294 |
} else { |
| 295 |
chan[i].audPos=0; |
| 296 |
} |
| 297 |
if (chan[i].useWave && chan[i].active) { |
| 298 |
if (chan[i].ws.tick()) { |
| 299 |
updateWave(i); |
| 300 |
} |
| 301 |
} |
| 302 |
if (chan[i].freqChanged || chan[i].keyOn || chan[i].keyOff) { |
| 303 |
DivSample* s=parent->getSample(chan[i].sample); |
| 304 |
double off=(s->centerRate>=1)?((double)s->centerRate/parent->getCenterRate()):1.0; |
| 305 |
chan[i].freq=(int)(off*parent->calcFreq(chan[i].baseFreq,chan[i].pitch,chan[i].fixedArp?chan[i].baseNoteOverride:chan[i].arpOff,chan[i].fixedArp,false,2,chan[i].pitch2,chipClock,CHIP_FREQBASE)); |
| 306 |
if (chan[i].keyOn) { |
| 307 |
unsigned int start, end, loop; |
| 308 |
if ((chan[i].echo&0xf)!=0) { |
| 309 |
// make sure echo channels' frequency can't be faster than the sample rate |
| 310 |
if (chan[i].freq>CHIP_FREQBASE) { |
| 311 |
chan[i].freq=CHIP_FREQBASE; |
| 312 |
} |
| 313 |
// this is only to match the HLE implementation |
| 314 |
// the actual engine will handle mid-frame echo switch differently |
| 315 |
start=loop=0x08000000; |
| 316 |
end=0x08000001; |
| 317 |
} else if (chan[i].useWave) { |
| 318 |
start=(i*256)|0x02000000; |
| 319 |
end=start+chan[i].wtLen; |
| 320 |
loop=start; |
| 321 |
} else { |
| 322 |
size_t maxPos=getSampleMemCapacity(); |
| 323 |
start=sampleOff[chan[i].sample]; |
| 324 |
if (s->isLoopable()) { |
| 325 |
end=MIN(start+MAX(s->loopEnd,1),maxPos); |
| 326 |
loop=start+s->loopStart; |
| 327 |
} else { |
| 328 |
end=MIN(start+s->length8+16,maxPos); |
| 329 |
loop=MIN(start+s->length8,maxPos); |
| 330 |
} |
| 331 |
if (chan[i].audPos>0) { |
| 332 |
start=start+MIN(chan[i].audPos,end); |
| 333 |
} |
| 334 |
start|=0x08000000; |
| 335 |
end|=0x08000000; |
| 336 |
loop|=0x08000000; |
| 337 |
} |
| 338 |
rWrite(2+i*16,start&0xffff); |
| 339 |
rWrite(3+i*16,start>>16); |
| 340 |
rWrite(10+i*16,end&0xffff); |
| 341 |
rWrite(11+i*16,end>>16); |
| 342 |
rWrite(12+i*16,loop&0xffff); |
| 343 |
rWrite(13+i*16,loop>>16); |
| 344 |
if (!chan[i].std.vol.had) { |
| 345 |
chan[i].outVol=chan[i].vol; |
| 346 |
} |
| 347 |
chan[i].volChangedL=true; |
| 348 |
chan[i].volChangedR=true; |
| 349 |
chan[i].keyOn=false; |
| 350 |
} |
| 351 |
if (chan[i].keyOff) { |
| 352 |
chan[i].volChangedL=true; |
| 353 |
chan[i].volChangedR=true; |
| 354 |
chan[i].keyOff=false; |
| 355 |
} |
| 356 |
if (chan[i].freqChanged) { |
| 357 |
rWrite(8+i*16,chan[i].freq&0xffff); |
| 358 |
rWrite(9+i*16,chan[i].freq>>16); |
| 359 |
chan[i].freqChanged=false; |
| 360 |
} |
| 361 |
} |
| 362 |
// don't scale echo channels |
| 363 |
if (chan[i].volChangedL) { |
| 364 |
int out=chan[i].outVol*chan[i].chPanL; |
| 365 |
if ((chan[i].echo&0xf)==0) out=(out*volScale)>>16; |
| 366 |
else out=out>>1; |
| 367 |
if (chan[i].invertL) out=-out; |
| 368 |
rWrite(14+i*16,(isMuted[i] || !chan[i].active)?0:out); |
| 369 |
chan[i].volChangedL=false; |
| 370 |
} |
| 371 |
if (chan[i].volChangedR) { |
| 372 |
int out=chan[i].outVol*chan[i].chPanR; |
| 373 |
if ((chan[i].echo&0xf)==0) out=(out*volScale)>>16; |
| 374 |
else out=out>>1; |
| 375 |
if (chan[i].invertR) out=-out; |
| 376 |
rWrite(15+i*16,(isMuted[i] || !chan[i].active)?0:out); |
| 377 |
chan[i].volChangedR=false; |
| 378 |
} |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
int DivPlatformGBAMinMod::dispatch(DivCommand c) { |
| 383 |
switch (c.cmd) { |
| 384 |
case DIV_CMD_NOTE_ON: { |
| 385 |
DivInstrument* ins=parent->getIns(chan[c.chan].ins,DIV_INS_AMIGA); |
| 386 |
chan[c.chan].macroVolMul=ins->type==DIV_INS_AMIGA?64:255; |
| 387 |
chan[c.chan].macroPanMul=ins->type==DIV_INS_AMIGA?127:255; |
| 388 |
if (ins->amiga.useWave) { |
| 389 |
chan[c.chan].useWave=true; |
| 390 |
chan[c.chan].wtLen=ins->amiga.waveLen+1; |
| 391 |
if (c.chan<chanMax) { |
| 392 |
wtMemCompo.entries[c.chan].end=wtMemCompo.entries[c.chan].begin+chan[c.chan].wtLen; |
| 393 |
} |
| 394 |
if (chan[c.chan].insChanged) { |
| 395 |
if (chan[c.chan].wave<0) { |
| 396 |
chan[c.chan].wave=0; |
| 397 |
} |
| 398 |
chan[c.chan].ws.setWidth(chan[c.chan].wtLen); |
| 399 |
chan[c.chan].ws.changeWave1(chan[c.chan].wave); |
| 400 |
} |
| 401 |
chan[c.chan].ws.init(ins,chan[c.chan].wtLen,255,chan[c.chan].insChanged); |
| 402 |
} else { |
| 403 |
if (c.value!=DIV_NOTE_NULL) { |
| 404 |
chan[c.chan].sample=ins->amiga.getSample(c.value); |
| 405 |
c.value=ins->amiga.getFreq(c.value); |
| 406 |
} |
| 407 |
chan[c.chan].useWave=false; |
| 408 |
} |
| 409 |
if (chan[c.chan].useWave || chan[c.chan].sample<0 || chan[c.chan].sample>=parent->song.sampleLen) { |
| 410 |
chan[c.chan].sample=-1; |
| 411 |
} |
| 412 |
if (c.value!=DIV_NOTE_NULL) { |
| 413 |
chan[c.chan].baseFreq=round(NOTE_FREQUENCY(c.value)); |
| 414 |
chan[c.chan].freqChanged=true; |
| 415 |
chan[c.chan].note=c.value; |
| 416 |
} |
| 417 |
chan[c.chan].active=true; |
| 418 |
chan[c.chan].keyOn=true; |
| 419 |
chan[c.chan].macroInit(ins); |
| 420 |
if (!parent->song.brokenOutVol && !chan[c.chan].std.vol.will) { |
| 421 |
chan[c.chan].outVol=chan[c.chan].vol; |
| 422 |
} |
| 423 |
break; |
| 424 |
} |
| 425 |
case DIV_CMD_NOTE_OFF: |
| 426 |
chan[c.chan].sample=-1; |
| 427 |
chan[c.chan].active=false; |
| 428 |
chan[c.chan].keyOff=true; |
| 429 |
chan[c.chan].macroInit(NULL); |
| 430 |
break; |
| 431 |
case DIV_CMD_NOTE_OFF_ENV: |
| 432 |
case DIV_CMD_ENV_RELEASE: |
| 433 |
chan[c.chan].std.release(); |
| 434 |
break; |
| 435 |
case DIV_CMD_INSTRUMENT: |
| 436 |
if (chan[c.chan].ins!=c.value || c.value2==1) { |
| 437 |
chan[c.chan].ins=c.value; |
| 438 |
} |
| 439 |
break; |
| 440 |
case DIV_CMD_VOLUME: |
| 441 |
chan[c.chan].vol=c.value; |
| 442 |
if (!chan[c.chan].std.vol.has) { |
| 443 |
chan[c.chan].outVol=c.value; |
| 444 |
} |
| 445 |
chan[c.chan].volChangedL=true; |
| 446 |
chan[c.chan].volChangedR=true; |
| 447 |
break; |
| 448 |
case DIV_CMD_GET_VOLUME: |
| 449 |
if (chan[c.chan].std.vol.has) { |
| 450 |
return chan[c.chan].vol; |
| 451 |
} |
| 452 |
return chan[c.chan].outVol; |
| 453 |
break; |
| 454 |
case DIV_CMD_SNES_INVERT: |
| 455 |
chan[c.chan].invertL=(c.value>>4); |
| 456 |
chan[c.chan].invertR=c.value&15; |
| 457 |
chan[c.chan].volChangedL=true; |
| 458 |
chan[c.chan].volChangedR=true; |
| 459 |
break; |
| 460 |
case DIV_CMD_PANNING: |
| 461 |
chan[c.chan].chPanL=c.value; |
| 462 |
chan[c.chan].chPanR=c.value2; |
| 463 |
chan[c.chan].volChangedL=true; |
| 464 |
chan[c.chan].volChangedR=true; |
| 465 |
break; |
| 466 |
case DIV_CMD_PITCH: |
| 467 |
chan[c.chan].pitch=c.value; |
| 468 |
chan[c.chan].freqChanged=true; |
| 469 |
break; |
| 470 |
case DIV_CMD_WAVE: |
| 471 |
if (!chan[c.chan].useWave) break; |
| 472 |
chan[c.chan].wave=c.value; |
| 473 |
chan[c.chan].ws.changeWave1(chan[c.chan].wave); |
| 474 |
break; |
| 475 |
case DIV_CMD_NOTE_PORTA: { |
| 476 |
int destFreq=NOTE_FREQUENCY(c.value2); |
| 477 |
bool return2=false; |
| 478 |
if (destFreq>chan[c.chan].baseFreq) { |
| 479 |
chan[c.chan].baseFreq+=c.value; |
| 480 |
if (chan[c.chan].baseFreq>=destFreq) { |
| 481 |
chan[c.chan].baseFreq=destFreq; |
| 482 |
return2=true; |
| 483 |
} |
| 484 |
} else { |
| 485 |
chan[c.chan].baseFreq-=c.value; |
| 486 |
if (chan[c.chan].baseFreq<=destFreq) { |
| 487 |
chan[c.chan].baseFreq=destFreq; |
| 488 |
return2=true; |
| 489 |
} |
| 490 |
} |
| 491 |
chan[c.chan].freqChanged=true; |
| 492 |
if (return2) { |
| 493 |
chan[c.chan].inPorta=false; |
| 494 |
return 2; |
| 495 |
} |
| 496 |
break; |
| 497 |
} |
| 498 |
case DIV_CMD_LEGATO: { |
| 499 |
chan[c.chan].baseFreq=NOTE_FREQUENCY(c.value+((HACKY_LEGATO_MESS)?(chan[c.chan].std.arp.val-12):(0))); |
| 500 |
chan[c.chan].freqChanged=true; |
| 501 |
chan[c.chan].note=c.value; |
| 502 |
break; |
| 503 |
} |
| 504 |
case DIV_CMD_PRE_PORTA: |
| 505 |
if (chan[c.chan].active && c.value2) { |
| 506 |
if (parent->song.resetMacroOnPorta) chan[c.chan].macroInit(parent->getIns(chan[c.chan].ins,DIV_INS_AMIGA)); |
| 507 |
} |
| 508 |
if (!chan[c.chan].inPorta && c.value && !parent->song.brokenPortaArp && chan[c.chan].std.arp.will && !NEW_ARP_STRAT) chan[c.chan].baseFreq=NOTE_FREQUENCY(chan[c.chan].note); |
| 509 |
chan[c.chan].inPorta=c.value; |
| 510 |
break; |
| 511 |
case DIV_CMD_SAMPLE_POS: |
| 512 |
chan[c.chan].audPos=c.value; |
| 513 |
chan[c.chan].setPos=true; |
| 514 |
break; |
| 515 |
case DIV_CMD_MINMOD_ECHO: |
| 516 |
chan[c.chan].echo=c.value; |
| 517 |
break; |
| 518 |
case DIV_CMD_GET_VOLMAX: |
| 519 |
return 255; |
| 520 |
break; |
| 521 |
case DIV_CMD_MACRO_OFF: |
| 522 |
chan[c.chan].std.mask(c.value,true); |
| 523 |
break; |
| 524 |
case DIV_CMD_MACRO_ON: |
| 525 |
chan[c.chan].std.mask(c.value,false); |
| 526 |
break; |
| 527 |
case DIV_CMD_MACRO_RESTART: |
| 528 |
chan[c.chan].std.restart(c.value); |
| 529 |
break; |
| 530 |
default: |
| 531 |
break; |
| 532 |
} |
| 533 |
return 1; |
| 534 |
} |
| 535 |
|
| 536 |
void DivPlatformGBAMinMod::updateWave(int ch) { |
| 537 |
int addr=ch*256; |
| 538 |
for (unsigned int i=0; i<chan[ch].wtLen; i++) { |
| 539 |
wtMem[addr+i]=(signed char)(chan[ch].ws.output[i]-128); |
| 540 |
} |
| 541 |
} |
| 542 |
|
| 543 |
void DivPlatformGBAMinMod::muteChannel(int ch, bool mute) { |
| 544 |
isMuted[ch]=mute; |
| 545 |
chan[ch].volChangedL=true; |
| 546 |
chan[ch].volChangedR=true; |
| 547 |
} |
| 548 |
|
| 549 |
void DivPlatformGBAMinMod::forceIns() { |
| 550 |
for (int i=0; i<chanMax; i++) { |
| 551 |
chan[i].insChanged=true; |
| 552 |
chan[i].volChangedL=true; |
| 553 |
chan[i].volChangedR=true; |
| 554 |
chan[i].sample=-1; |
| 555 |
chan[i].active=false; |
| 556 |
} |
| 557 |
} |
| 558 |
|
| 559 |
void* DivPlatformGBAMinMod::getChanState(int ch) { |
| 560 |
return &chan[ch]; |
| 561 |
} |
| 562 |
|
| 563 |
DivMacroInt* DivPlatformGBAMinMod::getChanMacroInt(int ch) { |
| 564 |
return &chan[ch].std; |
| 565 |
} |
| 566 |
|
| 567 |
unsigned short DivPlatformGBAMinMod::getPan(int ch) { |
| 568 |
return (chan[ch].chPanL<<8)|(chan[ch].chPanR); |
| 569 |
} |
| 570 |
|
| 571 |
DivSamplePos DivPlatformGBAMinMod::getSamplePos(int ch) { |
| 572 |
if (ch>=chanMax || |
| 573 |
chan[ch].sample<0 || chan[ch].sample>=parent->song.sampleLen || |
| 574 |
!chan[ch].active || (chan[ch].echo&0xf)!=0 |
| 575 |
) { |
| 576 |
return DivSamplePos(); |
| 577 |
} |
| 578 |
return DivSamplePos( |
| 579 |
chan[ch].sample, |
| 580 |
(((int)regPool[ch*16+2]|((int)regPool[ch*16+3]<<16))&0x01ffffff)-sampleOff[chan[ch].sample], |
| 581 |
(int64_t)chan[ch].freq*chipClock/CHIP_FREQBASE |
| 582 |
); |
| 583 |
} |
| 584 |
|
| 585 |
DivDispatchOscBuffer* DivPlatformGBAMinMod::getOscBuffer(int ch) { |
| 586 |
return oscBuf[ch]; |
| 587 |
} |
| 588 |
|
| 589 |
void DivPlatformGBAMinMod::reset() { |
| 590 |
resetMixer(); |
| 591 |
memset(regPool,0,sizeof(regPool)); |
| 592 |
memset(wtMem,0,sizeof(wtMem)); |
| 593 |
for (int i=0; i<16; i++) { |
| 594 |
chan[i]=DivPlatformGBAMinMod::Channel(); |
| 595 |
chan[i].std.setEngine(parent); |
| 596 |
chan[i].ws.setEngine(parent); |
| 597 |
chan[i].ws.init(NULL,32,255); |
| 598 |
} |
| 599 |
} |
| 600 |
|
| 601 |
void DivPlatformGBAMinMod::resetMixer() { |
| 602 |
sampTimer=sampCycles; |
| 603 |
updTimer=0; |
| 604 |
updCycles=0; |
| 605 |
mixBufReadPos=0; |
| 606 |
mixBufWritePos=0; |
| 607 |
mixBufPage=0; |
| 608 |
mixBufOffset=0; |
| 609 |
sampsRendered=0; |
| 610 |
sampL=0; |
| 611 |
sampR=0; |
| 612 |
memset(mixBuf,0,sizeof(mixBuf)); |
| 613 |
memset(chanOut,0,sizeof(chanOut)); |
| 614 |
} |
| 615 |
|
| 616 |
int DivPlatformGBAMinMod::getOutputCount() { |
| 617 |
return 2; |
| 618 |
} |
| 619 |
|
| 620 |
void DivPlatformGBAMinMod::notifyInsChange(int ins) { |
| 621 |
for (int i=0; i<16; i++) { |
| 622 |
if (chan[i].ins==ins) { |
| 623 |
chan[i].insChanged=true; |
| 624 |
} |
| 625 |
} |
| 626 |
} |
| 627 |
|
| 628 |
void DivPlatformGBAMinMod::notifyWaveChange(int wave) { |
| 629 |
for (int i=0; i<16; i++) { |
| 630 |
if (chan[i].useWave && chan[i].wave==wave) { |
| 631 |
chan[i].ws.changeWave1(wave); |
| 632 |
updateWave(i); |
| 633 |
} |
| 634 |
} |
| 635 |
} |
| 636 |
|
| 637 |
void DivPlatformGBAMinMod::notifyInsDeletion(void* ins) { |
| 638 |
for (int i=0; i<16; i++) { |
| 639 |
chan[i].std.notifyInsDeletion((DivInstrument*)ins); |
| 640 |
} |
| 641 |
} |
| 642 |
|
| 643 |
void DivPlatformGBAMinMod::poke(unsigned int addr, unsigned short val) { |
| 644 |
rWrite(addr,val); |
| 645 |
} |
| 646 |
|
| 647 |
void DivPlatformGBAMinMod::poke(std::vector<DivRegWrite>& wlist) { |
| 648 |
for (DivRegWrite& i: wlist) rWrite(i.addr,i.val); |
| 649 |
} |
| 650 |
|
| 651 |
unsigned char* DivPlatformGBAMinMod::getRegisterPool() { |
| 652 |
return (unsigned char*)regPool; |
| 653 |
} |
| 654 |
|
| 655 |
int DivPlatformGBAMinMod::getRegisterPoolSize() { |
| 656 |
return 256; |
| 657 |
} |
| 658 |
|
| 659 |
int DivPlatformGBAMinMod::getRegisterPoolDepth() { |
| 660 |
return 16; |
| 661 |
} |
| 662 |
|
| 663 |
const void* DivPlatformGBAMinMod::getSampleMem(int index) { |
| 664 |
return index == 0 ? sampleMem : NULL; |
| 665 |
} |
| 666 |
|
| 667 |
size_t DivPlatformGBAMinMod::getSampleMemCapacity(int index) { |
| 668 |
return index == 0 ? 33554432 : 0; |
| 669 |
} |
| 670 |
|
| 671 |
size_t DivPlatformGBAMinMod::getSampleMemUsage(int index) { |
| 672 |
return index == 0 ? sampleMemLen : 0; |
| 673 |
} |
| 674 |
|
| 675 |
bool DivPlatformGBAMinMod::isSampleLoaded(int index, int sample) { |
| 676 |
if (index!=0) return false; |
| 677 |
if (sample<0 || sample>32767) return false; |
| 678 |
return sampleLoaded[sample]; |
| 679 |
} |
| 680 |
|
| 681 |
const DivMemoryComposition* DivPlatformGBAMinMod::getMemCompo(int index) { |
| 682 |
switch (index) { |
| 683 |
case 0: return &romMemCompo; |
| 684 |
case 1: return &wtMemCompo; |
| 685 |
case 2: return &mixMemCompo; |
| 686 |
} |
| 687 |
return NULL; |
| 688 |
} |
| 689 |
|
| 690 |
void DivPlatformGBAMinMod::renderSamples(int sysID) { |
| 691 |
size_t maxPos=getSampleMemCapacity(); |
| 692 |
memset(sampleMem,0,maxPos); |
| 693 |
memset(sampleOff,0,32768*sizeof(unsigned int)); |
| 694 |
memset(sampleLoaded,0,32768*sizeof(bool)); |
| 695 |
romMemCompo.entries.clear(); |
| 696 |
romMemCompo.capacity=maxPos; |
| 697 |
|
| 698 |
// dummy zero-length samples are at pos 0 as the engine still outputs them |
| 699 |
size_t memPos=1; |
| 700 |
for (int i=0; i<parent->song.sampleLen; i++) { |
| 701 |
DivSample* s=parent->song.sample[i]; |
| 702 |
if (!s->renderOn[0][sysID]) { |
| 703 |
sampleOff[i]=0; |
| 704 |
continue; |
| 705 |
} |
| 706 |
int length=s->length8; |
| 707 |
int actualLength=MIN((int)(maxPos-memPos),length); |
| 708 |
if (actualLength>0) { |
| 709 |
sampleOff[i]=memPos; |
| 710 |
memcpy(&sampleMem[memPos],s->data8,actualLength); |
| 711 |
memPos+=actualLength; |
| 712 |
romMemCompo.entries.push_back(DivMemoryEntry(DIV_MEMORY_SAMPLE,"PCM",i,sampleOff[i],memPos)); |
| 713 |
// if it's one-shot, add 16 silent samples for looping area |
| 714 |
// this should be enough for most cases even though the |
| 715 |
// frequency register can make position jump by up to 256 samples |
| 716 |
if (!s->isLoopable()) { |
| 717 |
int oneShotLen=MIN((int)maxPos-memPos,16); |
| 718 |
memset(&sampleMem[memPos],0,oneShotLen); |
| 719 |
memPos+=oneShotLen; |
| 720 |
} |
| 721 |
} |
| 722 |
if (actualLength<length) { |
| 723 |
logW("out of GBA MinMod PCM memory for sample %d!",i); |
| 724 |
break; |
| 725 |
} |
| 726 |
sampleLoaded[i]=true; |
| 727 |
} |
| 728 |
sampleMemLen=memPos; |
| 729 |
romMemCompo.used=sampleMemLen; |
| 730 |
} |
| 731 |
|
| 732 |
void DivPlatformGBAMinMod::setFlags(const DivConfig& flags) { |
| 733 |
volScale=flags.getInt("volScale",4096); |
| 734 |
mixBufs=flags.getInt("mixBufs",15); |
| 735 |
dacDepth=flags.getInt("dacDepth",9); |
| 736 |
chanMax=flags.getInt("channels",16); |
| 737 |
rate=16777216>>dacDepth; |
| 738 |
for (int i=0; i<16; i++) { |
| 739 |
oscBuf[i]->setRate(rate); |
| 740 |
} |
| 741 |
sampCycles=16777216/flags.getInt("sampRate",21845); |
| 742 |
chipClock=16777216/sampCycles; |
| 743 |
resetMixer(); |
| 744 |
wtMemCompo.used=256*chanMax; |
| 745 |
mixMemCompo.used=2048*mixBufs; |
| 746 |
wtMemCompo.entries.clear(); |
| 747 |
mixMemCompo.entries.clear(); |
| 748 |
for (int i=0; i<chanMax; i++) { |
| 749 |
wtMemCompo.entries.push_back(DivMemoryEntry(DIV_MEMORY_WAVE_RAM, fmt::sprintf("Channel %d",i),-1,i*256,i*256)); |
| 750 |
} |
| 751 |
for (int i=0; i<(int)mixBufs; i++) { |
| 752 |
mixMemCompo.entries.push_back(DivMemoryEntry(DIV_MEMORY_ECHO, fmt::sprintf("Buffer %d Left",i),-1,i*2048,i*2048)); |
| 753 |
mixMemCompo.entries.push_back(DivMemoryEntry(DIV_MEMORY_ECHO, fmt::sprintf("Buffer %d Right",i),-1,i*2048+1024,i*2048+1024)); |
| 754 |
} |
| 755 |
} |
| 756 |
|
| 757 |
int DivPlatformGBAMinMod::init(DivEngine* p, int channels, int sugRate, const DivConfig& flags) { |
| 758 |
parent=p; |
| 759 |
dumpWrites=false; |
| 760 |
skipRegisterWrites=false; |
| 761 |
for (int i=0; i<16; i++) { |
| 762 |
isMuted[i]=false; |
| 763 |
oscBuf[i]=new DivDispatchOscBuffer; |
| 764 |
} |
| 765 |
sampleMem=new signed char[getSampleMemCapacity()]; |
| 766 |
sampleMemLen=0; |
| 767 |
romMemCompo=DivMemoryComposition(); |
| 768 |
romMemCompo.name="Sample ROM"; |
| 769 |
wtMemCompo=DivMemoryComposition(); |
| 770 |
wtMemCompo.name="Wavetable RAM"; |
| 771 |
wtMemCompo.capacity=256*16; |
| 772 |
wtMemCompo.memory=(unsigned char*)wtMem; |
| 773 |
wtMemCompo.waveformView=DIV_MEMORY_WAVE_8BIT_SIGNED; |
| 774 |
mixMemCompo=DivMemoryComposition(); |
| 775 |
mixMemCompo.name="Mix/Echo Buffer"; |
| 776 |
mixMemCompo.capacity=2048*15; |
| 777 |
mixMemCompo.memory=(unsigned char*)mixBuf; |
| 778 |
mixMemCompo.waveformView=DIV_MEMORY_WAVE_8BIT_SIGNED; |
| 779 |
setFlags(flags); |
| 780 |
reset(); |
| 781 |
|
| 782 |
maxCPU=0.0; // initialize! |
| 783 |
|
| 784 |
return 16; |
| 785 |
} |
| 786 |
|
| 787 |
void DivPlatformGBAMinMod::quit() { |
| 788 |
delete[] sampleMem; |
| 789 |
for (int i=0; i<16; i++) { |
| 790 |
delete oscBuf[i]; |
| 791 |
} |
| 792 |
} |
| 793 |
|
| 794 |
// initialization of important arrays |
| 795 |
DivPlatformGBAMinMod::DivPlatformGBAMinMod() { |
| 796 |
sampleOff=new unsigned int[32768]; |
| 797 |
sampleLoaded=new bool[32768]; |
| 798 |
} |
| 799 |
|
| 800 |
DivPlatformGBAMinMod::~DivPlatformGBAMinMod() { |
| 801 |
delete[] sampleOff; |
| 802 |
delete[] sampleLoaded; |
| 803 |
} |