| 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 "su.h" |
| 21 |
#include "../engine.h" |
| 22 |
#include "../../ta-log.h" |
| 23 |
#include <math.h> |
| 24 |
|
| 25 |
//#define rWrite(a,v) pendingWrites[a]=v; |
| 26 |
#define rWrite(a,v) if (!skipRegisterWrites) {writes.push(QueuedWrite(a,v)); if (dumpWrites) {addWrite(a,v);} } |
| 27 |
#define chWrite(c,a,v) rWrite(((c)<<5)|(a),v); |
| 28 |
|
| 29 |
#define CHIP_DIVIDER 2 |
| 30 |
#define CHIP_FREQBASE 524288 |
| 31 |
|
| 32 |
const char** DivPlatformSoundUnit::getRegisterSheet() { |
| 33 |
return NULL; |
| 34 |
} |
| 35 |
|
| 36 |
double DivPlatformSoundUnit::NOTE_SU(int ch, int note) { |
| 37 |
if (chan[ch].switchRoles) { |
| 38 |
return NOTE_PERIODIC(note); |
| 39 |
} |
| 40 |
return NOTE_FREQUENCY(note); |
| 41 |
} |
| 42 |
|
| 43 |
void DivPlatformSoundUnit::acquire(short** buf, size_t len) { |
| 44 |
for (int i=0; i<8; i++) { |
| 45 |
oscBuf[i]->begin(len); |
| 46 |
} |
| 47 |
|
| 48 |
for (size_t h=0; h<len; h++) { |
| 49 |
while (!writes.empty()) { |
| 50 |
QueuedWrite w=writes.front(); |
| 51 |
su->Write(w.addr,w.val); |
| 52 |
writes.pop(); |
| 53 |
} |
| 54 |
su->NextSample(&buf[0][h],&buf[1][h]); |
| 55 |
for (int i=0; i<8; i++) { |
| 56 |
oscBuf[i]->putSample(h,su->GetSample(i)); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
for (int i=0; i<8; i++) { |
| 61 |
oscBuf[i]->end(len); |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
void DivPlatformSoundUnit::writeControl(int ch) { |
| 66 |
chWrite(ch,0x04,(chan[ch].wave&7)|(chan[ch].pcm<<3)|(chan[ch].control<<4)); |
| 67 |
} |
| 68 |
|
| 69 |
void DivPlatformSoundUnit::writeControlUpper(int ch) { |
| 70 |
chWrite(ch,0x05,((int)chan[ch].phaseReset)|(chan[ch].filterPhaseReset<<1)|(chan[ch].pcmLoop<<2)|(chan[ch].timerSync<<3)|(chan[ch].freqSweep<<4)|(chan[ch].volSweep<<5)|(chan[ch].cutSweep<<6)); |
| 71 |
chan[ch].phaseReset=false; |
| 72 |
chan[ch].filterPhaseReset=false; |
| 73 |
} |
| 74 |
|
| 75 |
void DivPlatformSoundUnit::tick(bool sysTick) { |
| 76 |
for (int i=0; i<8; i++) { |
| 77 |
chan[i].std.next(); |
| 78 |
if (sysTick) { |
| 79 |
if (chan[i].pw_slide!=0) { |
| 80 |
chan[i].virtual_duty-=chan[i].pw_slide; |
| 81 |
chan[i].virtual_duty=CLAMP(chan[i].virtual_duty,0,0xfff); |
| 82 |
chan[i].duty=chan[i].virtual_duty>>5; |
| 83 |
|
| 84 |
chWrite(i,0x08,chan[i].duty); |
| 85 |
} |
| 86 |
if (chan[i].cutoff_slide!=0) { |
| 87 |
chan[i].cutoff+=chan[i].cutoff_slide*4; |
| 88 |
chan[i].cutoff=CLAMP(chan[i].cutoff,0,0x3fff); |
| 89 |
|
| 90 |
chWrite(i,0x06,chan[i].cutoff&0xff); |
| 91 |
chWrite(i,0x07,chan[i].cutoff>>8); |
| 92 |
} |
| 93 |
} |
| 94 |
if (chan[i].std.vol.had) { |
| 95 |
DivInstrument* ins=parent->getIns(chan[i].ins,DIV_INS_SU); |
| 96 |
if (ins->type==DIV_INS_AMIGA) { |
| 97 |
chan[i].outVol=((chan[i].vol&127)*MIN(64,chan[i].std.vol.val))>>6; |
| 98 |
} else { |
| 99 |
chan[i].outVol=((chan[i].vol&127)*MIN(127,chan[i].std.vol.val))>>7; |
| 100 |
} |
| 101 |
chWrite(i,0x02,chan[i].outVol); |
| 102 |
} |
| 103 |
if (NEW_ARP_STRAT) { |
| 104 |
chan[i].handleArp(); |
| 105 |
} else if (chan[i].std.arp.had) { |
| 106 |
if (!chan[i].inPorta) { |
| 107 |
chan[i].baseFreq=NOTE_SU(i,parent->calcArp(chan[i].note,chan[i].std.arp.val)); |
| 108 |
} |
| 109 |
chan[i].freqChanged=true; |
| 110 |
} |
| 111 |
if (chan[i].std.duty.had) { |
| 112 |
chan[i].duty=chan[i].std.duty.val; |
| 113 |
chan[i].virtual_duty=(unsigned short)chan[i].duty<<5; |
| 114 |
chWrite(i,0x08,chan[i].duty); |
| 115 |
} |
| 116 |
if (chan[i].std.wave.had) { |
| 117 |
chan[i].wave=chan[i].std.wave.val&7; |
| 118 |
writeControl(i); |
| 119 |
} |
| 120 |
if (chan[i].std.phaseReset.had) { |
| 121 |
chan[i].phaseReset=chan[i].std.phaseReset.val; |
| 122 |
writeControlUpper(i); |
| 123 |
} |
| 124 |
if (chan[i].std.panL.had) { |
| 125 |
chan[i].pan=chan[i].std.panL.val; |
| 126 |
chWrite(i,0x03,chan[i].pan); |
| 127 |
} |
| 128 |
if (chan[i].std.pitch.had) { |
| 129 |
if (chan[i].std.pitch.mode) { |
| 130 |
chan[i].pitch2+=chan[i].std.pitch.val; |
| 131 |
CLAMP_VAR(chan[i].pitch2,-32768,32767); |
| 132 |
} else { |
| 133 |
chan[i].pitch2=chan[i].std.pitch.val; |
| 134 |
} |
| 135 |
chan[i].freqChanged=true; |
| 136 |
} |
| 137 |
if (chan[i].std.ex1.had) { |
| 138 |
chan[i].cutoff=((chan[i].std.ex1.val&16383)*chan[i].baseCutoff)/16380; |
| 139 |
chWrite(i,0x06,chan[i].cutoff&0xff); |
| 140 |
chWrite(i,0x07,chan[i].cutoff>>8); |
| 141 |
} |
| 142 |
if (chan[i].std.ex2.had) { |
| 143 |
chan[i].res=chan[i].std.ex2.val; |
| 144 |
chWrite(i,0x09,chan[i].res); |
| 145 |
} |
| 146 |
if (chan[i].std.ex3.had) { |
| 147 |
chan[i].control=chan[i].std.ex3.val&15; |
| 148 |
writeControl(i); |
| 149 |
} |
| 150 |
if (chan[i].std.ex4.had) { |
| 151 |
chan[i].syncTimer=chan[i].std.ex4.val&65535; |
| 152 |
chan[i].timerSync=(chan[i].syncTimer>0); |
| 153 |
if (chan[i].switchRoles) { |
| 154 |
chWrite(i,0x00,chan[i].syncTimer&0xff); |
| 155 |
chWrite(i,0x01,chan[i].syncTimer>>8); |
| 156 |
} else { |
| 157 |
chWrite(i,0x1e,chan[i].syncTimer&0xff); |
| 158 |
chWrite(i,0x1f,chan[i].syncTimer>>8); |
| 159 |
} |
| 160 |
writeControlUpper(i); |
| 161 |
} |
| 162 |
|
| 163 |
// run hardware sequence |
| 164 |
if (chan[i].active) { |
| 165 |
if (--chan[i].hwSeqDelay<=0) { |
| 166 |
chan[i].hwSeqDelay=0; |
| 167 |
DivInstrument* ins=parent->getIns(chan[i].ins,DIV_INS_SU); |
| 168 |
int hwSeqCount=0; |
| 169 |
while (chan[i].hwSeqPos<ins->su.hwSeqLen && hwSeqCount<8) { |
| 170 |
bool leave=false; |
| 171 |
unsigned char bound=ins->su.hwSeq[chan[i].hwSeqPos].bound; |
| 172 |
unsigned char val=ins->su.hwSeq[chan[i].hwSeqPos].val; |
| 173 |
unsigned short speed=ins->su.hwSeq[chan[i].hwSeqPos].speed; |
| 174 |
switch (ins->su.hwSeq[chan[i].hwSeqPos].cmd) { |
| 175 |
case DivInstrumentSoundUnit::DIV_SU_HWCMD_VOL: |
| 176 |
chan[i].volSweepP=speed; |
| 177 |
chan[i].volSweepV=val; |
| 178 |
chan[i].volSweepB=bound; |
| 179 |
chan[i].volSweep=(val>0); |
| 180 |
chWrite(i,0x14,chan[i].volSweepP&0xff); |
| 181 |
chWrite(i,0x15,chan[i].volSweepP>>8); |
| 182 |
chWrite(i,0x16,chan[i].volSweepV); |
| 183 |
chWrite(i,0x17,chan[i].volSweepB); |
| 184 |
writeControlUpper(i); |
| 185 |
break; |
| 186 |
case DivInstrumentSoundUnit::DIV_SU_HWCMD_PITCH: |
| 187 |
chan[i].freqSweepP=speed; |
| 188 |
chan[i].freqSweepV=val; |
| 189 |
chan[i].freqSweepB=bound; |
| 190 |
chan[i].freqSweep=(val>0); |
| 191 |
chWrite(i,0x10,chan[i].freqSweepP&0xff); |
| 192 |
chWrite(i,0x11,chan[i].freqSweepP>>8); |
| 193 |
chWrite(i,0x12,chan[i].freqSweepV); |
| 194 |
chWrite(i,0x13,chan[i].freqSweepB); |
| 195 |
writeControlUpper(i); |
| 196 |
break; |
| 197 |
case DivInstrumentSoundUnit::DIV_SU_HWCMD_CUT: |
| 198 |
chan[i].cutSweepP=speed; |
| 199 |
chan[i].cutSweepV=val; |
| 200 |
chan[i].cutSweepB=bound; |
| 201 |
chan[i].cutSweep=(val>0); |
| 202 |
chWrite(i,0x18,chan[i].cutSweepP&0xff); |
| 203 |
chWrite(i,0x19,chan[i].cutSweepP>>8); |
| 204 |
chWrite(i,0x1a,chan[i].cutSweepV); |
| 205 |
chWrite(i,0x1b,chan[i].cutSweepB); |
| 206 |
writeControlUpper(i); |
| 207 |
break; |
| 208 |
case DivInstrumentSoundUnit::DIV_SU_HWCMD_WAIT: |
| 209 |
chan[i].hwSeqDelay=(val+1)*parent->tickMult; |
| 210 |
leave=true; |
| 211 |
break; |
| 212 |
case DivInstrumentSoundUnit::DIV_SU_HWCMD_WAIT_REL: |
| 213 |
if (!chan[i].released) { |
| 214 |
chan[i].hwSeqPos--; |
| 215 |
leave=true; |
| 216 |
} |
| 217 |
break; |
| 218 |
case DivInstrumentSoundUnit::DIV_SU_HWCMD_LOOP: |
| 219 |
chan[i].hwSeqPos=val-1; |
| 220 |
break; |
| 221 |
case DivInstrumentSoundUnit::DIV_SU_HWCMD_LOOP_REL: |
| 222 |
if (!chan[i].released) { |
| 223 |
chan[i].hwSeqPos=val-1; |
| 224 |
} |
| 225 |
break; |
| 226 |
} |
| 227 |
|
| 228 |
chan[i].hwSeqPos++; |
| 229 |
if (leave) break; |
| 230 |
hwSeqCount++; |
| 231 |
} |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
if (chan[i].freqChanged || chan[i].keyOn || chan[i].keyOff) { |
| 236 |
//DivInstrument* ins=parent->getIns(chan[i].ins,DIV_INS_SU); |
| 237 |
chan[i].freq=parent->calcFreq(chan[i].baseFreq,chan[i].pitch,chan[i].fixedArp?chan[i].baseNoteOverride:chan[i].arpOff,chan[i].fixedArp,chan[i].switchRoles,2,chan[i].pitch2,chipClock,chan[i].switchRoles?CHIP_DIVIDER:CHIP_FREQBASE); |
| 238 |
if (chan[i].pcm) { |
| 239 |
DivSample* sample=parent->getSample(chan[i].sample); |
| 240 |
if (sample!=NULL) { |
| 241 |
double off=0.25; |
| 242 |
if (sample->centerRate<1) { |
| 243 |
off=0.25; |
| 244 |
} else { |
| 245 |
off=(double)sample->centerRate/(parent->getCenterRate()*4.0); |
| 246 |
} |
| 247 |
chan[i].freq=(double)chan[i].freq*off; |
| 248 |
} |
| 249 |
} |
| 250 |
if (chan[i].freq<0) chan[i].freq=0; |
| 251 |
if (chan[i].freq>65535) chan[i].freq=65535; |
| 252 |
if (chan[i].switchRoles) { |
| 253 |
chWrite(i,0x1e,chan[i].freq&0xff); |
| 254 |
chWrite(i,0x1f,chan[i].freq>>8); |
| 255 |
} else { |
| 256 |
chWrite(i,0x00,chan[i].freq&0xff); |
| 257 |
chWrite(i,0x01,chan[i].freq>>8); |
| 258 |
} |
| 259 |
if (chan[i].keyOn) { |
| 260 |
if (chan[i].pcm) { |
| 261 |
int sNum=chan[i].sample; |
| 262 |
DivSample* sample=parent->getSample(sNum); |
| 263 |
if (sample!=NULL && sNum>=0 && sNum<parent->song.sampleLen) { |
| 264 |
unsigned int sampleEnd=sampleOffSU[sNum]+(sample->getLoopEndPosition()); |
| 265 |
unsigned int off=sampleOffSU[sNum]+chan[i].hasOffset; |
| 266 |
chan[i].hasOffset=0; |
| 267 |
if (sampleEnd>=getSampleMemCapacity(0)) sampleEnd=getSampleMemCapacity(0)-1; |
| 268 |
chWrite(i,0x0a,off&0xff); |
| 269 |
chWrite(i,0x0b,off>>8); |
| 270 |
chWrite(i,0x0c,sampleEnd&0xff); |
| 271 |
chWrite(i,0x0d,sampleEnd>>8); |
| 272 |
if (sample->isLoopable()) { |
| 273 |
unsigned int sampleLoop=sampleOffSU[sNum]+sample->getLoopStartPosition(); |
| 274 |
if (sampleLoop>=getSampleMemCapacity(0)) sampleLoop=getSampleMemCapacity(0)-1; |
| 275 |
chWrite(i,0x0e,sampleLoop&0xff); |
| 276 |
chWrite(i,0x0f,sampleLoop>>8); |
| 277 |
chan[i].pcmLoop=true; |
| 278 |
} else { |
| 279 |
chan[i].pcmLoop=false; |
| 280 |
} |
| 281 |
writeControl(i); |
| 282 |
writeControlUpper(i); |
| 283 |
} |
| 284 |
} |
| 285 |
} |
| 286 |
if (chan[i].keyOff) { |
| 287 |
chWrite(i,0x02,0); |
| 288 |
} |
| 289 |
if (chan[i].keyOn) chan[i].keyOn=false; |
| 290 |
if (chan[i].keyOff) chan[i].keyOff=false; |
| 291 |
chan[i].freqChanged=false; |
| 292 |
} |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
int DivPlatformSoundUnit::dispatch(DivCommand c) { |
| 297 |
switch (c.cmd) { |
| 298 |
case DIV_CMD_NOTE_ON: { |
| 299 |
DivInstrument* ins=parent->getIns(chan[c.chan].ins,DIV_INS_SU); |
| 300 |
chan[c.chan].switchRoles=ins->su.switchRoles; |
| 301 |
if (chan[c.chan].pcm && !(ins->type==DIV_INS_AMIGA || ins->amiga.useSample)) { |
| 302 |
chan[c.chan].pcm=(ins->type==DIV_INS_AMIGA || ins->amiga.useSample); |
| 303 |
writeControl(c.chan); |
| 304 |
writeControlUpper(c.chan); |
| 305 |
} |
| 306 |
chan[c.chan].pcm=(ins->type==DIV_INS_AMIGA || ins->amiga.useSample); |
| 307 |
if (chan[c.chan].pcm) { |
| 308 |
if (c.value!=DIV_NOTE_NULL) { |
| 309 |
chan[c.chan].sample=ins->amiga.getSample(c.value); |
| 310 |
chan[c.chan].sampleNote=c.value; |
| 311 |
c.value=ins->amiga.getFreq(c.value); |
| 312 |
chan[c.chan].sampleNoteDelta=c.value-chan[c.chan].sampleNote; |
| 313 |
} |
| 314 |
} else { |
| 315 |
chan[c.chan].sampleNote=DIV_NOTE_NULL; |
| 316 |
chan[c.chan].sampleNoteDelta=0; |
| 317 |
} |
| 318 |
if (c.value!=DIV_NOTE_NULL) { |
| 319 |
chan[c.chan].baseFreq=NOTE_SU(c.chan,c.value); |
| 320 |
chan[c.chan].freqChanged=true; |
| 321 |
chan[c.chan].note=c.value; |
| 322 |
} |
| 323 |
chan[c.chan].active=true; |
| 324 |
chan[c.chan].keyOn=true; |
| 325 |
chan[c.chan].released=false; |
| 326 |
chan[c.chan].hwSeqPos=0; |
| 327 |
chan[c.chan].hwSeqDelay=0; |
| 328 |
chWrite(c.chan,0x02,chan[c.chan].vol); |
| 329 |
chan[c.chan].macroInit(ins); |
| 330 |
if (!parent->song.brokenOutVol && !chan[c.chan].std.vol.will) { |
| 331 |
chan[c.chan].outVol=chan[c.chan].vol; |
| 332 |
} |
| 333 |
chan[c.chan].insChanged=false; |
| 334 |
break; |
| 335 |
} |
| 336 |
case DIV_CMD_NOTE_OFF: |
| 337 |
chan[c.chan].active=false; |
| 338 |
chan[c.chan].keyOff=true; |
| 339 |
chan[c.chan].hwSeqPos=0; |
| 340 |
chan[c.chan].hwSeqDelay=0; |
| 341 |
chan[c.chan].macroInit(NULL); |
| 342 |
break; |
| 343 |
case DIV_CMD_NOTE_OFF_ENV: |
| 344 |
case DIV_CMD_ENV_RELEASE: |
| 345 |
chan[c.chan].std.release(); |
| 346 |
chan[c.chan].released=true; |
| 347 |
break; |
| 348 |
case DIV_CMD_INSTRUMENT: |
| 349 |
if (chan[c.chan].ins!=c.value || c.value2==1) { |
| 350 |
chan[c.chan].ins=c.value; |
| 351 |
chan[c.chan].insChanged=true; |
| 352 |
} |
| 353 |
break; |
| 354 |
case DIV_CMD_VOLUME: |
| 355 |
if (chan[c.chan].vol!=c.value) { |
| 356 |
chan[c.chan].vol=c.value; |
| 357 |
if (!chan[c.chan].std.vol.has) { |
| 358 |
chan[c.chan].outVol=c.value; |
| 359 |
if (chan[c.chan].active) chWrite(c.chan,0x02,chan[c.chan].outVol); |
| 360 |
} |
| 361 |
} |
| 362 |
break; |
| 363 |
case DIV_CMD_GET_VOLUME: |
| 364 |
if (chan[c.chan].std.vol.has) { |
| 365 |
return chan[c.chan].vol; |
| 366 |
} |
| 367 |
return chan[c.chan].outVol; |
| 368 |
break; |
| 369 |
case DIV_CMD_PITCH: |
| 370 |
chan[c.chan].pitch=c.value; |
| 371 |
chan[c.chan].freqChanged=true; |
| 372 |
break; |
| 373 |
case DIV_CMD_WAVE: |
| 374 |
chan[c.chan].wave=c.value&7; |
| 375 |
writeControl(c.chan); |
| 376 |
break; |
| 377 |
case DIV_CMD_STD_NOISE_MODE: |
| 378 |
chan[c.chan].duty=c.value&127; |
| 379 |
chan[c.chan].virtual_duty=(unsigned short)chan[c.chan].duty << 5; |
| 380 |
chWrite(c.chan,0x08,chan[c.chan].duty); |
| 381 |
break; |
| 382 |
case DIV_CMD_C64_RESONANCE: |
| 383 |
chan[c.chan].res=c.value; |
| 384 |
chWrite(c.chan,0x09,chan[c.chan].res); |
| 385 |
break; |
| 386 |
case DIV_CMD_C64_FILTER_MODE: |
| 387 |
chan[c.chan].control=c.value&15; |
| 388 |
break; |
| 389 |
case DIV_CMD_SU_SWEEP_PERIOD_LOW: { |
| 390 |
switch (c.value) { |
| 391 |
case 0: |
| 392 |
chan[c.chan].freqSweepP=(chan[c.chan].freqSweepP&0xff00)|c.value2; |
| 393 |
chWrite(c.chan,0x10,chan[c.chan].freqSweepP&0xff); |
| 394 |
break; |
| 395 |
case 1: |
| 396 |
chan[c.chan].volSweepP=(chan[c.chan].volSweepP&0xff00)|c.value2; |
| 397 |
chWrite(c.chan,0x14,chan[c.chan].volSweepP&0xff); |
| 398 |
break; |
| 399 |
case 2: |
| 400 |
chan[c.chan].cutSweepP=(chan[c.chan].cutSweepP&0xff00)|c.value2; |
| 401 |
chWrite(c.chan,0x18,chan[c.chan].cutSweepP&0xff); |
| 402 |
break; |
| 403 |
} |
| 404 |
break; |
| 405 |
} |
| 406 |
case DIV_CMD_SU_SWEEP_PERIOD_HIGH: { |
| 407 |
switch (c.value) { |
| 408 |
case 0: |
| 409 |
chan[c.chan].freqSweepP=(chan[c.chan].freqSweepP&0xff)|(c.value2<<8); |
| 410 |
chWrite(c.chan,0x11,chan[c.chan].freqSweepP>>8); |
| 411 |
break; |
| 412 |
case 1: |
| 413 |
chan[c.chan].volSweepP=(chan[c.chan].volSweepP&0xff)|(c.value2<<8); |
| 414 |
chWrite(c.chan,0x15,chan[c.chan].volSweepP>>8); |
| 415 |
break; |
| 416 |
case 2: |
| 417 |
chan[c.chan].cutSweepP=(chan[c.chan].cutSweepP&0xff)|(c.value2<<8); |
| 418 |
chWrite(c.chan,0x19,chan[c.chan].cutSweepP>>8); |
| 419 |
break; |
| 420 |
} |
| 421 |
break; |
| 422 |
} |
| 423 |
case DIV_CMD_SU_SWEEP_BOUND: { |
| 424 |
switch (c.value) { |
| 425 |
case 0: |
| 426 |
chan[c.chan].freqSweepB=c.value2; |
| 427 |
chWrite(c.chan,0x13,chan[c.chan].freqSweepB); |
| 428 |
break; |
| 429 |
case 1: |
| 430 |
chan[c.chan].volSweepB=c.value2; |
| 431 |
chWrite(c.chan,0x17,chan[c.chan].volSweepB); |
| 432 |
break; |
| 433 |
case 2: |
| 434 |
chan[c.chan].cutSweepB=c.value2; |
| 435 |
chWrite(c.chan,0x1b,chan[c.chan].cutSweepB); |
| 436 |
break; |
| 437 |
} |
| 438 |
break; |
| 439 |
} |
| 440 |
case DIV_CMD_SU_SWEEP_ENABLE: { |
| 441 |
switch (c.value) { |
| 442 |
case 0: |
| 443 |
chan[c.chan].freqSweepV=c.value2; |
| 444 |
chan[c.chan].freqSweep=(c.value2>0); |
| 445 |
chWrite(c.chan,0x12,chan[c.chan].freqSweepV); |
| 446 |
break; |
| 447 |
case 1: |
| 448 |
chan[c.chan].volSweepV=c.value2; |
| 449 |
chan[c.chan].volSweep=(c.value2>0); |
| 450 |
chWrite(c.chan,0x16,chan[c.chan].volSweepV); |
| 451 |
break; |
| 452 |
case 2: |
| 453 |
chan[c.chan].cutSweepV=c.value2; |
| 454 |
chan[c.chan].cutSweep=(c.value2>0); |
| 455 |
chWrite(c.chan,0x1a,chan[c.chan].cutSweepV); |
| 456 |
break; |
| 457 |
} |
| 458 |
writeControlUpper(c.chan); |
| 459 |
break; |
| 460 |
} |
| 461 |
case DIV_CMD_SU_SYNC_PERIOD_LOW: |
| 462 |
chan[c.chan].syncTimer=(chan[c.chan].syncTimer&0xff00)|c.value; |
| 463 |
chan[c.chan].timerSync=(chan[c.chan].syncTimer>0); |
| 464 |
chWrite(c.chan,0x1e,chan[c.chan].syncTimer&0xff); |
| 465 |
chWrite(c.chan,0x1f,chan[c.chan].syncTimer>>8); |
| 466 |
writeControlUpper(c.chan); |
| 467 |
break; |
| 468 |
case DIV_CMD_SU_SYNC_PERIOD_HIGH: |
| 469 |
chan[c.chan].syncTimer=(chan[c.chan].syncTimer&0xff)|(c.value<<8); |
| 470 |
chan[c.chan].timerSync=(chan[c.chan].syncTimer>0); |
| 471 |
chWrite(c.chan,0x1e,chan[c.chan].syncTimer&0xff); |
| 472 |
chWrite(c.chan,0x1f,chan[c.chan].syncTimer>>8); |
| 473 |
writeControlUpper(c.chan); |
| 474 |
break; |
| 475 |
case DIV_CMD_C64_FINE_CUTOFF: |
| 476 |
chan[c.chan].baseCutoff=c.value*4; |
| 477 |
if (!chan[c.chan].std.ex1.has) { |
| 478 |
chan[c.chan].cutoff=chan[c.chan].baseCutoff; |
| 479 |
chWrite(c.chan,0x06,chan[c.chan].cutoff&0xff); |
| 480 |
chWrite(c.chan,0x07,chan[c.chan].cutoff>>8); |
| 481 |
} |
| 482 |
break; |
| 483 |
case DIV_CMD_NOTE_PORTA: { |
| 484 |
int destFreq=NOTE_SU(c.chan,c.value2+chan[c.chan].sampleNoteDelta); |
| 485 |
bool return2=false; |
| 486 |
if (destFreq>chan[c.chan].baseFreq) { |
| 487 |
chan[c.chan].baseFreq+=c.value*((parent->song.linearPitch==2)?1:(1+(chan[c.chan].baseFreq>>9))); |
| 488 |
if (chan[c.chan].baseFreq>=destFreq) { |
| 489 |
chan[c.chan].baseFreq=destFreq; |
| 490 |
return2=true; |
| 491 |
} |
| 492 |
} else { |
| 493 |
chan[c.chan].baseFreq-=c.value*((parent->song.linearPitch==2)?1:(1+(chan[c.chan].baseFreq>>9))); |
| 494 |
if (chan[c.chan].baseFreq<=destFreq) { |
| 495 |
chan[c.chan].baseFreq=destFreq; |
| 496 |
return2=true; |
| 497 |
} |
| 498 |
} |
| 499 |
chan[c.chan].freqChanged=true; |
| 500 |
if (return2) { |
| 501 |
chan[c.chan].inPorta=false; |
| 502 |
return 2; |
| 503 |
} |
| 504 |
break; |
| 505 |
} |
| 506 |
case DIV_CMD_PANNING: { |
| 507 |
chan[c.chan].pan=parent->convertPanSplitToLinearLR(c.value,c.value2,254)-127; |
| 508 |
chWrite(c.chan,0x03,chan[c.chan].pan); |
| 509 |
break; |
| 510 |
} |
| 511 |
case DIV_CMD_SAMPLE_POS: |
| 512 |
chan[c.chan].hasOffset=c.value; |
| 513 |
chan[c.chan].keyOn=true; |
| 514 |
break; |
| 515 |
case DIV_CMD_LEGATO: |
| 516 |
chan[c.chan].baseFreq=NOTE_SU(c.chan,c.value+chan[c.chan].sampleNoteDelta+((HACKY_LEGATO_MESS)?(chan[c.chan].std.arp.val):(0))); |
| 517 |
chan[c.chan].freqChanged=true; |
| 518 |
chan[c.chan].note=c.value; |
| 519 |
break; |
| 520 |
case DIV_CMD_PRE_PORTA: |
| 521 |
if (chan[c.chan].active && c.value2) { |
| 522 |
if (parent->song.resetMacroOnPorta) chan[c.chan].macroInit(parent->getIns(chan[c.chan].ins,DIV_INS_SU)); |
| 523 |
} |
| 524 |
if (!chan[c.chan].inPorta && c.value && !parent->song.brokenPortaArp && chan[c.chan].std.arp.will && !NEW_ARP_STRAT) chan[c.chan].baseFreq=NOTE_SU(c.chan,chan[c.chan].note); |
| 525 |
chan[c.chan].inPorta=c.value; |
| 526 |
break; |
| 527 |
case DIV_CMD_C64_PW_SLIDE: |
| 528 |
chan[c.chan].pw_slide=c.value*c.value2; |
| 529 |
break; |
| 530 |
case DIV_CMD_C64_CUTOFF_SLIDE: |
| 531 |
chan[c.chan].cutoff_slide=c.value*c.value2; |
| 532 |
break; |
| 533 |
case DIV_CMD_GET_VOLMAX: |
| 534 |
return 127; |
| 535 |
break; |
| 536 |
case DIV_CMD_MACRO_OFF: |
| 537 |
chan[c.chan].std.mask(c.value,true); |
| 538 |
break; |
| 539 |
case DIV_CMD_MACRO_ON: |
| 540 |
chan[c.chan].std.mask(c.value,false); |
| 541 |
break; |
| 542 |
case DIV_CMD_MACRO_RESTART: |
| 543 |
chan[c.chan].std.restart(c.value); |
| 544 |
break; |
| 545 |
default: |
| 546 |
break; |
| 547 |
} |
| 548 |
return 1; |
| 549 |
} |
| 550 |
|
| 551 |
void DivPlatformSoundUnit::muteChannel(int ch, bool mute) { |
| 552 |
isMuted[ch]=mute; |
| 553 |
su->muted[ch]=mute; |
| 554 |
} |
| 555 |
|
| 556 |
void DivPlatformSoundUnit::forceIns() { |
| 557 |
for (int i=0; i<8; i++) { |
| 558 |
chan[i].insChanged=true; |
| 559 |
chan[i].freqChanged=true; |
| 560 |
|
| 561 |
// restore channel attributes |
| 562 |
chWrite(i,0x03,chan[i].pan); |
| 563 |
writeControl(i); |
| 564 |
writeControlUpper(i); |
| 565 |
chWrite(i,0x08,chan[i].duty); |
| 566 |
} |
| 567 |
} |
| 568 |
|
| 569 |
void* DivPlatformSoundUnit::getChanState(int ch) { |
| 570 |
return &chan[ch]; |
| 571 |
} |
| 572 |
|
| 573 |
DivMacroInt* DivPlatformSoundUnit::getChanMacroInt(int ch) { |
| 574 |
return &chan[ch].std; |
| 575 |
} |
| 576 |
|
| 577 |
unsigned short DivPlatformSoundUnit::getPan(int ch) { |
| 578 |
return parent->convertPanLinearToSplit(chan[ch].pan+127,8,255); |
| 579 |
} |
| 580 |
|
| 581 |
DivDispatchOscBuffer* DivPlatformSoundUnit::getOscBuffer(int ch) { |
| 582 |
return oscBuf[ch]; |
| 583 |
} |
| 584 |
|
| 585 |
unsigned char* DivPlatformSoundUnit::getRegisterPool() { |
| 586 |
return (unsigned char*)su->chan; |
| 587 |
} |
| 588 |
|
| 589 |
int DivPlatformSoundUnit::getRegisterPoolSize() { |
| 590 |
return 256; |
| 591 |
} |
| 592 |
|
| 593 |
void DivPlatformSoundUnit::reset() { |
| 594 |
while (!writes.empty()) writes.pop(); |
| 595 |
memset(regPool,0,128); |
| 596 |
for (int i=0; i<8; i++) { |
| 597 |
chan[i]=DivPlatformSoundUnit::Channel(); |
| 598 |
chan[i].std.setEngine(parent); |
| 599 |
|
| 600 |
chan[i].cutoff_slide=0; |
| 601 |
chan[i].pw_slide=0; |
| 602 |
|
| 603 |
chan[i].virtual_duty=0x800; // for some reason duty by default is 50% |
| 604 |
} |
| 605 |
if (dumpWrites) { |
| 606 |
addWrite(0xffffffff,0); |
| 607 |
} |
| 608 |
su->Reset(); |
| 609 |
for (int i=0; i<8; i++) { |
| 610 |
chWrite(i,0x08,0x3f); |
| 611 |
} |
| 612 |
lastPan=0xff; |
| 613 |
cycles=0; |
| 614 |
curChan=-1; |
| 615 |
sampleBank=0; |
| 616 |
lfoMode=0; |
| 617 |
lfoSpeed=255; |
| 618 |
delay=500; |
| 619 |
|
| 620 |
// set initial IL status |
| 621 |
ilCtrl=initIlCtrl; |
| 622 |
ilSize=initIlSize; |
| 623 |
fil1=initFil1; |
| 624 |
echoVol=initEchoVol; |
| 625 |
rWrite(0x9c,echoVol); |
| 626 |
rWrite(0x9d,ilCtrl); |
| 627 |
rWrite(0xbc,ilSize); |
| 628 |
rWrite(0xbd,fil1); |
| 629 |
|
| 630 |
// copy sample memory |
| 631 |
memcpy(su->pcm,sampleMem,sampleMemSize?65536:8192); |
| 632 |
} |
| 633 |
|
| 634 |
int DivPlatformSoundUnit::getOutputCount() { |
| 635 |
return 2; |
| 636 |
} |
| 637 |
|
| 638 |
bool DivPlatformSoundUnit::keyOffAffectsArp(int ch) { |
| 639 |
return true; |
| 640 |
} |
| 641 |
|
| 642 |
void DivPlatformSoundUnit::notifyInsDeletion(void* ins) { |
| 643 |
for (int i=0; i<8; i++) { |
| 644 |
chan[i].std.notifyInsDeletion((DivInstrument*)ins); |
| 645 |
} |
| 646 |
} |
| 647 |
|
| 648 |
void DivPlatformSoundUnit::setFlags(const DivConfig& flags) { |
| 649 |
if (flags.getInt("clockSel",0)) { |
| 650 |
chipClock=1190000; |
| 651 |
} else { |
| 652 |
chipClock=1236000; |
| 653 |
} |
| 654 |
CHECK_CUSTOM_CLOCK; |
| 655 |
rate=chipClock/4; |
| 656 |
for (int i=0; i<8; i++) { |
| 657 |
oscBuf[i]->setRate(rate); |
| 658 |
} |
| 659 |
bool echoOn=flags.getBool("echo",false); |
| 660 |
initIlCtrl=3|(echoOn?4:0); |
| 661 |
initIlSize=((flags.getInt("echoDelay",0))&63)|(echoOn?0x40:0)|(flags.getBool("swapEcho",false)?0x80:0); |
| 662 |
initFil1=flags.getInt("echoFeedback",0)|(flags.getInt("echoResolution",0)<<4); |
| 663 |
initEchoVol=flags.getInt("echoVol",0); |
| 664 |
|
| 665 |
sampleMemSize=flags.getInt("sampleMemSize",0); |
| 666 |
|
| 667 |
su->Init(sampleMemSize?65536:8192,flags.getBool("pdm",false)); |
| 668 |
renderSamples(sysIDCache); |
| 669 |
} |
| 670 |
|
| 671 |
void DivPlatformSoundUnit::poke(unsigned int addr, unsigned short val) { |
| 672 |
rWrite(addr,val); |
| 673 |
} |
| 674 |
|
| 675 |
void DivPlatformSoundUnit::poke(std::vector<DivRegWrite>& wlist) { |
| 676 |
for (DivRegWrite& i: wlist) rWrite(i.addr,i.val); |
| 677 |
} |
| 678 |
|
| 679 |
const void* DivPlatformSoundUnit::getSampleMem(int index) { |
| 680 |
return (index==0)?sampleMem:NULL; |
| 681 |
} |
| 682 |
|
| 683 |
size_t DivPlatformSoundUnit::getSampleMemCapacity(int index) { |
| 684 |
return (index==0)?((sampleMemSize?65536:8192)-((initIlSize&64)?((1+(initIlSize&63))<<7):0)):0; |
| 685 |
} |
| 686 |
|
| 687 |
size_t DivPlatformSoundUnit::getSampleMemUsage(int index) { |
| 688 |
return (index==0)?sampleMemLen:0; |
| 689 |
} |
| 690 |
|
| 691 |
bool DivPlatformSoundUnit::isSampleLoaded(int index, int sample) { |
| 692 |
if (index!=0) return false; |
| 693 |
if (sample<0 || sample>32767) return false; |
| 694 |
return sampleLoaded[sample]; |
| 695 |
} |
| 696 |
|
| 697 |
const DivMemoryComposition* DivPlatformSoundUnit::getMemCompo(int index) { |
| 698 |
if (index!=0) return NULL; |
| 699 |
return &memCompo; |
| 700 |
} |
| 701 |
|
| 702 |
void DivPlatformSoundUnit::renderSamples(int sysID) { |
| 703 |
memset(sampleMem,0,sampleMemSize?65536:8192); |
| 704 |
memset(sampleOffSU,0,32768*sizeof(unsigned int)); |
| 705 |
memset(sampleLoaded,0,32768*sizeof(bool)); |
| 706 |
|
| 707 |
memCompo=DivMemoryComposition(); |
| 708 |
memCompo.name="Sample RAM"; |
| 709 |
|
| 710 |
size_t memPos=0; |
| 711 |
for (int i=0; i<parent->song.sampleLen; i++) { |
| 712 |
DivSample* s=parent->song.sample[i]; |
| 713 |
if (s->data8==NULL) continue; |
| 714 |
if (!s->renderOn[0][sysID]) { |
| 715 |
sampleOffSU[i]=0; |
| 716 |
continue; |
| 717 |
} |
| 718 |
|
| 719 |
int paddedLen=s->length8; |
| 720 |
if (memPos>=getSampleMemCapacity(0)) { |
| 721 |
logW("out of PCM memory for sample %d!",i); |
| 722 |
break; |
| 723 |
} |
| 724 |
if (memPos+paddedLen>=getSampleMemCapacity(0)) { |
| 725 |
memcpy(sampleMem+memPos,s->data8,getSampleMemCapacity(0)-memPos); |
| 726 |
logW("out of PCM memory for sample %d!",i); |
| 727 |
} else { |
| 728 |
memcpy(sampleMem+memPos,s->data8,paddedLen); |
| 729 |
sampleLoaded[i]=true; |
| 730 |
} |
| 731 |
sampleOffSU[i]=memPos; |
| 732 |
memCompo.entries.push_back(DivMemoryEntry(DIV_MEMORY_SAMPLE,"Sample",i,memPos,memPos+paddedLen)); |
| 733 |
memPos+=paddedLen; |
| 734 |
} |
| 735 |
sampleMemLen=memPos; |
| 736 |
sysIDCache=sysID; |
| 737 |
|
| 738 |
memcpy(su->pcm,sampleMem,sampleMemSize?65536:8192); |
| 739 |
|
| 740 |
memCompo.used=sampleMemLen; |
| 741 |
memCompo.capacity=sampleMemSize?65536:8192; |
| 742 |
|
| 743 |
if (initIlSize&64) { |
| 744 |
memCompo.entries.push_back(DivMemoryEntry(DIV_MEMORY_ECHO,"Echo Buffer",-1,memCompo.capacity-((1+(initIlSize&63))<<7),memCompo.capacity)); |
| 745 |
} |
| 746 |
} |
| 747 |
|
| 748 |
int DivPlatformSoundUnit::init(DivEngine* p, int channels, int sugRate, const DivConfig& flags) { |
| 749 |
parent=p; |
| 750 |
dumpWrites=false; |
| 751 |
skipRegisterWrites=false; |
| 752 |
for (int i=0; i<8; i++) { |
| 753 |
isMuted[i]=false; |
| 754 |
oscBuf[i]=new DivDispatchOscBuffer; |
| 755 |
} |
| 756 |
su=new SoundUnit(); |
| 757 |
sampleMem=new unsigned char[65536]; |
| 758 |
memset(sampleMem,0,65536); |
| 759 |
sysIDCache=0; |
| 760 |
setFlags(flags); |
| 761 |
reset(); |
| 762 |
return 8; |
| 763 |
} |
| 764 |
|
| 765 |
void DivPlatformSoundUnit::quit() { |
| 766 |
for (int i=0; i<8; i++) { |
| 767 |
delete oscBuf[i]; |
| 768 |
} |
| 769 |
delete su; |
| 770 |
delete[] sampleMem; |
| 771 |
} |
| 772 |
|
| 773 |
// initialization of important arrays |
| 774 |
DivPlatformSoundUnit::DivPlatformSoundUnit() { |
| 775 |
sampleOffSU=new unsigned int[32768]; |
| 776 |
sampleLoaded=new bool[32768]; |
| 777 |
} |
| 778 |
|
| 779 |
DivPlatformSoundUnit::~DivPlatformSoundUnit() { |
| 780 |
delete[] sampleOffSU; |
| 781 |
delete[] sampleLoaded; |
| 782 |
} |