| 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 "vrc6.h" |
| 21 |
#include "../engine.h" |
| 22 |
#include <cstddef> |
| 23 |
#include <math.h> |
| 24 |
|
| 25 |
#define rWrite(a,v) if (!skipRegisterWrites) {writes.push(QueuedWrite(a,v)); if (dumpWrites) {addWrite(a,v);} } |
| 26 |
#define chWrite(c,a,v) rWrite(0x9000+(c<<12)+(a&3),v) |
| 27 |
|
| 28 |
const char* regCheatSheetVRC6[]={ |
| 29 |
"S0DutyVol", "9000", |
| 30 |
"S0PeriodL", "9001", |
| 31 |
"S0PeriodH", "9002", |
| 32 |
"GlobalCtl", "9003", |
| 33 |
"S1DutyVol", "A000", |
| 34 |
"S1PeriodL", "A001", |
| 35 |
"S1PeriodH", "A002", |
| 36 |
"SawVolume", "B000", |
| 37 |
"SawPeriodL", "B001", |
| 38 |
"SawPeriodH", "B002", |
| 39 |
"TimerLatch", "F000", |
| 40 |
"TimerCtl", "F001", |
| 41 |
"IRQAck", "F002", |
| 42 |
NULL |
| 43 |
}; |
| 44 |
|
| 45 |
const char** DivPlatformVRC6::getRegisterSheet() { |
| 46 |
return regCheatSheetVRC6; |
| 47 |
} |
| 48 |
|
| 49 |
void DivPlatformVRC6::acquireDirect(blip_buffer_t** bb, size_t len) { |
| 50 |
for (int i=0; i<3; i++) { |
| 51 |
oscBuf[i]->begin(len); |
| 52 |
} |
| 53 |
|
| 54 |
for (size_t h=0; h<len; h++) { |
| 55 |
// running heuristic |
| 56 |
int advance=vrc6.predict(); |
| 57 |
if ((int)(len-h)<advance) advance=len-h; |
| 58 |
for (int i=0; i<2; i++) { |
| 59 |
if (chan[i].pcm && chan[i].dacSample!=-1) { |
| 60 |
if (chan[i].dacRate<=0) continue; |
| 61 |
int remainTime=(rate-chan[i].dacPeriod+chan[i].dacRate-1)/chan[i].dacRate; |
| 62 |
if (remainTime<advance) advance=remainTime; |
| 63 |
if (remainTime<1) advance=1; |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
// PCM part |
| 68 |
for (int i=0; i<2; i++) { |
| 69 |
if (chan[i].pcm && chan[i].dacSample!=-1) { |
| 70 |
chan[i].dacPeriod+=chan[i].dacRate*advance; |
| 71 |
if (chan[i].dacPeriod>rate) { |
| 72 |
DivSample* s=parent->getSample(chan[i].dacSample); |
| 73 |
if (s->samples<=0 || chan[i].dacPos>=s->samples) { |
| 74 |
chan[i].dacSample=-1; |
| 75 |
chWrite(i,0,0); |
| 76 |
continue; |
| 77 |
} |
| 78 |
unsigned char dacData=(((unsigned char)s->data8[chan[i].dacPos]^0x80)>>4); |
| 79 |
chan[i].dacOut=MAX(0,MIN(15,(dacData*chan[i].outVol)>>4)); |
| 80 |
if (!isMuted[i]) { |
| 81 |
chWrite(i,0,0x80|chan[i].dacOut); |
| 82 |
} |
| 83 |
chan[i].dacPos++; |
| 84 |
if (s->isLoopable() && chan[i].dacPos>=(unsigned int)s->loopEnd) { |
| 85 |
chan[i].dacPos=s->loopStart; |
| 86 |
} else if (chan[i].dacPos>=s->samples) { |
| 87 |
chan[i].dacSample=-1; |
| 88 |
chWrite(i,0,0); |
| 89 |
} |
| 90 |
chan[i].dacPeriod-=rate; |
| 91 |
} |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
// VRC6 part |
| 96 |
vrc6.tick(advance); |
| 97 |
h+=advance-1; |
| 98 |
int sample=vrc6.out()<<9; // scale to 16 bit |
| 99 |
if (sample!=prevSample) { |
| 100 |
blip_add_delta(bb[0],h,sample-prevSample); |
| 101 |
prevSample=sample; |
| 102 |
} |
| 103 |
|
| 104 |
for (int i=0; i<2; i++) { |
| 105 |
oscBuf[i]->putSample(h,vrc6.pulse_out(i)<<11); |
| 106 |
} |
| 107 |
oscBuf[2]->putSample(h,vrc6.sawtooth_out()<<10); |
| 108 |
|
| 109 |
// Command part (what the heck why at the END?!) |
| 110 |
while (!writes.empty()) { |
| 111 |
QueuedWrite w=writes.front(); |
| 112 |
switch (w.addr&0xf000) { |
| 113 |
case 0x9000: // Pulse 1 |
| 114 |
if (w.addr<=0x9003) { |
| 115 |
if (w.addr==0x9003) { |
| 116 |
vrc6.control_w(w.val); |
| 117 |
} else if (w.addr<=0x9002) { |
| 118 |
vrc6.pulse_w(0,w.addr&3,w.val); |
| 119 |
} |
| 120 |
regPool[w.addr-0x9000]=w.val; |
| 121 |
} |
| 122 |
break; |
| 123 |
case 0xa000: // Pulse 2 |
| 124 |
if (w.addr<=0xa002) { |
| 125 |
vrc6.pulse_w(1,w.addr&3,w.val); |
| 126 |
regPool[(w.addr-0xa000)+4]=w.val; |
| 127 |
} |
| 128 |
break; |
| 129 |
case 0xb000: // Sawtooth |
| 130 |
if (w.addr<=0xb002) { |
| 131 |
vrc6.saw_w(w.addr&3,w.val); |
| 132 |
regPool[(w.addr-0xb000)+7]=w.val; |
| 133 |
} |
| 134 |
break; |
| 135 |
case 0xf000: // IRQ/Timer |
| 136 |
if (w.addr<=0xf002) { |
| 137 |
vrc6.timer_w(w.addr&3,w.val); |
| 138 |
regPool[(w.addr-0xf000)+10]=w.val; |
| 139 |
} |
| 140 |
break; |
| 141 |
} |
| 142 |
writes.pop(); |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
for (int i=0; i<3; i++) { |
| 147 |
oscBuf[i]->end(len); |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
void DivPlatformVRC6::tick(bool sysTick) { |
| 152 |
for (int i=0; i<3; i++) { |
| 153 |
// 16 for pulse; 14 for saw |
| 154 |
int CHIP_DIVIDER=(i==2)?14:16; |
| 155 |
chan[i].std.next(); |
| 156 |
if (chan[i].std.vol.had) { |
| 157 |
if (i==2) { // sawtooth |
| 158 |
chan[i].outVol=((chan[i].vol&63)*MIN(63,chan[i].std.vol.val))/63; |
| 159 |
if (chan[i].outVol<0) chan[i].outVol=0; |
| 160 |
if (chan[i].outVol>63) chan[i].outVol=63; |
| 161 |
if (!isMuted[i]) { |
| 162 |
chWrite(i,0,chan[i].outVol); |
| 163 |
} |
| 164 |
} else { // pulse |
| 165 |
chan[i].outVol=((chan[i].vol&15)*MIN(15,chan[i].std.vol.val))/15; |
| 166 |
if (chan[i].outVol<0) chan[i].outVol=0; |
| 167 |
if (chan[i].outVol>15) chan[i].outVol=15; |
| 168 |
if ((!isMuted[i]) && (!chan[i].pcm)) { |
| 169 |
chWrite(i,0,(chan[i].outVol&0xf)|((chan[i].duty&7)<<4)); |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
if (NEW_ARP_STRAT) { |
| 174 |
chan[i].handleArp(); |
| 175 |
} else if (chan[i].std.arp.had) { |
| 176 |
if (!chan[i].inPorta) { |
| 177 |
chan[i].baseFreq=NOTE_PERIODIC(parent->calcArp(chan[i].note,chan[i].std.arp.val)); |
| 178 |
} |
| 179 |
chan[i].freqChanged=true; |
| 180 |
} |
| 181 |
if (chan[i].std.duty.had) { |
| 182 |
chan[i].duty=chan[i].std.duty.val; |
| 183 |
if ((!isMuted[i]) && (i!=2) && (!chan[i].pcm)) { // pulse |
| 184 |
chWrite(i,0,(chan[i].outVol&0xf)|((chan[i].duty&7)<<4)); |
| 185 |
} |
| 186 |
} |
| 187 |
if (chan[i].std.pitch.had) { |
| 188 |
if (chan[i].std.pitch.mode) { |
| 189 |
chan[i].pitch2+=chan[i].std.pitch.val; |
| 190 |
CLAMP_VAR(chan[i].pitch2,-32768,32767); |
| 191 |
} else { |
| 192 |
chan[i].pitch2=chan[i].std.pitch.val; |
| 193 |
} |
| 194 |
chan[i].freqChanged=true; |
| 195 |
} |
| 196 |
if (chan[i].std.phaseReset.had) { |
| 197 |
if (chan[i].std.phaseReset.val && chan[i].active) { |
| 198 |
if ((i!=2) && (!chan[i].pcm)) { |
| 199 |
if (dumpWrites) addWrite(0xffff0002+(i<<8),0); |
| 200 |
if (chan[i].dacSample<0 || chan[i].dacSample>=parent->song.sampleLen) { |
| 201 |
if (dumpWrites) { |
| 202 |
chWrite(i,2,0x80); |
| 203 |
chWrite(i,0,isMuted[i]?0:0x80); |
| 204 |
addWrite(0xffff0000+(i<<8),chan[i].dacSample); |
| 205 |
} |
| 206 |
if (chan[i].setPos) { |
| 207 |
chan[i].setPos=false; |
| 208 |
} else { |
| 209 |
chan[i].dacPos=0; |
| 210 |
} |
| 211 |
chan[i].dacPeriod=0; |
| 212 |
chan[i].keyOn=true; |
| 213 |
} |
| 214 |
} |
| 215 |
} |
| 216 |
} |
| 217 |
if (chan[i].freqChanged || chan[i].keyOn || chan[i].keyOff) { |
| 218 |
if (i==2) { // sawtooth |
| 219 |
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,14)-1; |
| 220 |
} else { // pulse |
| 221 |
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,16)-1; |
| 222 |
if (chan[i].furnaceDac) { |
| 223 |
double off=1.0; |
| 224 |
if (chan[i].dacSample>=0 && chan[i].dacSample<parent->song.sampleLen) { |
| 225 |
DivSample* s=parent->getSample(chan[i].dacSample); |
| 226 |
if (s->centerRate<1) { |
| 227 |
off=1.0; |
| 228 |
} else { |
| 229 |
off=parent->getCenterRate()/(double)s->centerRate; |
| 230 |
} |
| 231 |
} |
| 232 |
chan[i].dacRate=((double)chipClock)/MAX(1,off*chan[i].freq); |
| 233 |
if (dumpWrites) addWrite(0xffff0001+(i<<8),chan[i].dacRate); |
| 234 |
} |
| 235 |
} |
| 236 |
if (chan[i].freq>4095) chan[i].freq=4095; |
| 237 |
if (chan[i].freq<0) chan[i].freq=0; |
| 238 |
if (chan[i].keyOff) { |
| 239 |
chWrite(i,2,0); |
| 240 |
} else if (chan[i].active) { |
| 241 |
chWrite(i,1,chan[i].freq&0xff); |
| 242 |
chWrite(i,2,0x80|((chan[i].freq>>8)&0xf)); |
| 243 |
} |
| 244 |
if (chan[i].keyOn) chan[i].keyOn=false; |
| 245 |
if (chan[i].keyOff) chan[i].keyOff=false; |
| 246 |
chan[i].freqChanged=false; |
| 247 |
} |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
int DivPlatformVRC6::dispatch(DivCommand c) { |
| 252 |
int CHIP_DIVIDER=(c.chan==2)?14:16; |
| 253 |
switch (c.cmd) { |
| 254 |
case DIV_CMD_NOTE_ON: |
| 255 |
if (c.chan!=2) { // pulse wave |
| 256 |
DivInstrument* ins=parent->getIns(chan[c.chan].ins,DIV_INS_VRC6); |
| 257 |
if (ins->type==DIV_INS_AMIGA || ins->amiga.useSample) { |
| 258 |
chan[c.chan].pcm=true; |
| 259 |
} else if (chan[c.chan].furnaceDac) { |
| 260 |
chan[c.chan].pcm=false; |
| 261 |
chan[c.chan].sampleNote=DIV_NOTE_NULL; |
| 262 |
chan[c.chan].sampleNoteDelta=0; |
| 263 |
} |
| 264 |
if (chan[c.chan].pcm) { |
| 265 |
if (skipRegisterWrites) break; |
| 266 |
if (ins->type==DIV_INS_AMIGA || ins->amiga.useSample) { |
| 267 |
if (c.value!=DIV_NOTE_NULL) { |
| 268 |
chan[c.chan].dacSample=ins->amiga.getSample(c.value); |
| 269 |
chan[c.chan].sampleNote=c.value; |
| 270 |
c.value=ins->amiga.getFreq(c.value); |
| 271 |
chan[c.chan].sampleNoteDelta=c.value-chan[c.chan].sampleNote; |
| 272 |
} else if (chan[c.chan].sampleNote!=DIV_NOTE_NULL) { |
| 273 |
chan[c.chan].dacSample=ins->amiga.getSample(chan[c.chan].sampleNote); |
| 274 |
c.value=ins->amiga.getFreq(chan[c.chan].sampleNote); |
| 275 |
} |
| 276 |
if (chan[c.chan].dacSample<0 || chan[c.chan].dacSample>=parent->song.sampleLen) { |
| 277 |
chan[c.chan].dacSample=-1; |
| 278 |
if (dumpWrites) addWrite(0xffff0002+(c.chan<<8),0); |
| 279 |
break; |
| 280 |
} else { |
| 281 |
if (dumpWrites) { |
| 282 |
chWrite(c.chan,2,0x80); |
| 283 |
chWrite(c.chan,0,isMuted[c.chan]?0:0x80); |
| 284 |
addWrite(0xffff0000+(c.chan<<8),chan[c.chan].dacSample); |
| 285 |
} |
| 286 |
} |
| 287 |
if (chan[c.chan].setPos) { |
| 288 |
chan[c.chan].setPos=false; |
| 289 |
} else { |
| 290 |
chan[c.chan].dacPos=0; |
| 291 |
} |
| 292 |
chan[c.chan].dacPeriod=0; |
| 293 |
if (c.value!=DIV_NOTE_NULL) { |
| 294 |
chan[c.chan].baseFreq=NOTE_PERIODIC(c.value); |
| 295 |
chan[c.chan].freqChanged=true; |
| 296 |
chan[c.chan].note=c.value; |
| 297 |
} |
| 298 |
chan[c.chan].active=true; |
| 299 |
chan[c.chan].macroInit(ins); |
| 300 |
if (!parent->song.brokenOutVol && !chan[c.chan].std.vol.will) { |
| 301 |
chan[c.chan].outVol=chan[c.chan].vol; |
| 302 |
} |
| 303 |
//chan[c.chan].keyOn=true; |
| 304 |
chan[c.chan].furnaceDac=true; |
| 305 |
} else { |
| 306 |
chan[c.chan].sampleNote=DIV_NOTE_NULL; |
| 307 |
chan[c.chan].sampleNoteDelta=0; |
| 308 |
if (c.value!=DIV_NOTE_NULL) { |
| 309 |
chan[c.chan].note=c.value; |
| 310 |
} |
| 311 |
chan[c.chan].dacSample=12*sampleBank+chan[c.chan].note%12; |
| 312 |
if (chan[c.chan].dacSample>=parent->song.sampleLen) { |
| 313 |
chan[c.chan].dacSample=-1; |
| 314 |
if (dumpWrites) addWrite(0xffff0002+(c.chan<<8),0); |
| 315 |
break; |
| 316 |
} else { |
| 317 |
if (dumpWrites) addWrite(0xffff0000+(c.chan<<8),chan[c.chan].dacSample); |
| 318 |
} |
| 319 |
if (chan[c.chan].setPos) { |
| 320 |
chan[c.chan].setPos=false; |
| 321 |
} else { |
| 322 |
chan[c.chan].dacPos=0; |
| 323 |
} |
| 324 |
chan[c.chan].dacPeriod=0; |
| 325 |
chan[c.chan].dacRate=parent->getSample(chan[c.chan].dacSample)->rate; |
| 326 |
if (dumpWrites) { |
| 327 |
chWrite(c.chan,2,0x80); |
| 328 |
chWrite(c.chan,0,isMuted[c.chan]?0:0x80); |
| 329 |
addWrite(0xffff0001+(c.chan<<8),chan[c.chan].dacRate); |
| 330 |
} |
| 331 |
chan[c.chan].furnaceDac=false; |
| 332 |
} |
| 333 |
break; |
| 334 |
} |
| 335 |
} |
| 336 |
if (c.value!=DIV_NOTE_NULL) { |
| 337 |
chan[c.chan].baseFreq=NOTE_PERIODIC(c.value); |
| 338 |
chan[c.chan].freqChanged=true; |
| 339 |
chan[c.chan].note=c.value; |
| 340 |
} |
| 341 |
chan[c.chan].active=true; |
| 342 |
chan[c.chan].keyOn=true; |
| 343 |
chan[c.chan].macroInit(parent->getIns(chan[c.chan].ins,DIV_INS_VRC6)); |
| 344 |
if (!isMuted[c.chan]) { |
| 345 |
if (c.chan==2) { // sawtooth |
| 346 |
chWrite(c.chan,0,chan[c.chan].vol); |
| 347 |
} else if (!chan[c.chan].pcm) { |
| 348 |
chWrite(c.chan,0,(chan[c.chan].vol&0xf)|((chan[c.chan].duty&7)<<4)); |
| 349 |
} |
| 350 |
} |
| 351 |
break; |
| 352 |
case DIV_CMD_NOTE_OFF: |
| 353 |
chan[c.chan].dacSample=-1; |
| 354 |
if (dumpWrites) addWrite(0xffff0002+(c.chan<<8),0); |
| 355 |
chan[c.chan].pcm=false; |
| 356 |
chan[c.chan].sampleNote=DIV_NOTE_NULL; |
| 357 |
chan[c.chan].sampleNoteDelta=0; |
| 358 |
chan[c.chan].active=false; |
| 359 |
chan[c.chan].keyOff=true; |
| 360 |
chan[c.chan].macroInit(NULL); |
| 361 |
break; |
| 362 |
case DIV_CMD_NOTE_OFF_ENV: |
| 363 |
case DIV_CMD_ENV_RELEASE: |
| 364 |
chan[c.chan].std.release(); |
| 365 |
break; |
| 366 |
case DIV_CMD_INSTRUMENT: |
| 367 |
if (chan[c.chan].ins!=c.value || c.value2==1) { |
| 368 |
chan[c.chan].ins=c.value; |
| 369 |
} |
| 370 |
break; |
| 371 |
case DIV_CMD_VOLUME: |
| 372 |
if (chan[c.chan].vol!=c.value) { |
| 373 |
chan[c.chan].vol=c.value; |
| 374 |
if (!chan[c.chan].std.vol.has) { |
| 375 |
chan[c.chan].outVol=c.value; |
| 376 |
} |
| 377 |
if (!isMuted[c.chan]) { |
| 378 |
if (chan[c.chan].active) { |
| 379 |
if (c.chan==2) { // sawtooth |
| 380 |
chWrite(c.chan,0,chan[c.chan].vol); |
| 381 |
} else if (!chan[c.chan].pcm) { |
| 382 |
chWrite(c.chan,0,(chan[c.chan].vol&0xf)|((chan[c.chan].duty&7)<<4)); |
| 383 |
} |
| 384 |
} |
| 385 |
} |
| 386 |
} |
| 387 |
break; |
| 388 |
case DIV_CMD_GET_VOLUME: |
| 389 |
return chan[c.chan].vol; |
| 390 |
break; |
| 391 |
case DIV_CMD_PITCH: |
| 392 |
chan[c.chan].pitch=c.value; |
| 393 |
chan[c.chan].freqChanged=true; |
| 394 |
break; |
| 395 |
case DIV_CMD_NOTE_PORTA: { |
| 396 |
int destFreq=NOTE_PERIODIC(c.value2+chan[c.chan].sampleNoteDelta); |
| 397 |
bool return2=false; |
| 398 |
if (destFreq>chan[c.chan].baseFreq) { |
| 399 |
chan[c.chan].baseFreq+=c.value; |
| 400 |
if (chan[c.chan].baseFreq>=destFreq) { |
| 401 |
chan[c.chan].baseFreq=destFreq; |
| 402 |
return2=true; |
| 403 |
} |
| 404 |
} else { |
| 405 |
chan[c.chan].baseFreq-=c.value; |
| 406 |
if (chan[c.chan].baseFreq<=destFreq) { |
| 407 |
chan[c.chan].baseFreq=destFreq; |
| 408 |
return2=true; |
| 409 |
} |
| 410 |
} |
| 411 |
chan[c.chan].freqChanged=true; |
| 412 |
if (return2) { |
| 413 |
chan[c.chan].inPorta=false; |
| 414 |
return 2; |
| 415 |
} |
| 416 |
break; |
| 417 |
} |
| 418 |
case DIV_CMD_STD_NOISE_MODE: |
| 419 |
if ((c.chan!=2) && (!chan[c.chan].pcm)) { // pulse |
| 420 |
chan[c.chan].duty=c.value; |
| 421 |
if (!isMuted[c.chan]) { // pulse |
| 422 |
chWrite(c.chan,0,(chan[c.chan].outVol&0xf)|((chan[c.chan].duty&7)<<4)); |
| 423 |
} |
| 424 |
} |
| 425 |
break; |
| 426 |
case DIV_CMD_SAMPLE_MODE: |
| 427 |
if (c.chan!=2) { // pulse |
| 428 |
chan[c.chan].pcm=c.value; |
| 429 |
if (!chan[c.chan].pcm) { |
| 430 |
chan[c.chan].sampleNote=DIV_NOTE_NULL; |
| 431 |
chan[c.chan].sampleNoteDelta=0; |
| 432 |
} |
| 433 |
} |
| 434 |
break; |
| 435 |
case DIV_CMD_SAMPLE_BANK: |
| 436 |
sampleBank=c.value; |
| 437 |
if (sampleBank>(parent->song.sample.size()/12)) { |
| 438 |
sampleBank=parent->song.sample.size()/12; |
| 439 |
} |
| 440 |
break; |
| 441 |
case DIV_CMD_SAMPLE_POS: |
| 442 |
chan[c.chan].dacPos=c.value; |
| 443 |
chan[c.chan].setPos=true; |
| 444 |
break; |
| 445 |
case DIV_CMD_LEGATO: |
| 446 |
chan[c.chan].baseFreq=NOTE_PERIODIC(c.value+chan[c.chan].sampleNoteDelta+((HACKY_LEGATO_MESS)?(chan[c.chan].std.arp.val):(0))); |
| 447 |
chan[c.chan].freqChanged=true; |
| 448 |
chan[c.chan].note=c.value; |
| 449 |
break; |
| 450 |
case DIV_CMD_PRE_PORTA: |
| 451 |
if (chan[c.chan].active && c.value2) { |
| 452 |
if (parent->song.resetMacroOnPorta) chan[c.chan].macroInit(parent->getIns(chan[c.chan].ins,DIV_INS_VRC6)); |
| 453 |
} |
| 454 |
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); |
| 455 |
chan[c.chan].inPorta=c.value; |
| 456 |
break; |
| 457 |
case DIV_CMD_GET_VOLMAX: |
| 458 |
if (c.chan==2) return 63; // sawtooth has 6 bit volume |
| 459 |
return 15; // pulse has 4 bit volume |
| 460 |
break; |
| 461 |
case DIV_CMD_MACRO_OFF: |
| 462 |
chan[c.chan].std.mask(c.value,true); |
| 463 |
break; |
| 464 |
case DIV_CMD_MACRO_ON: |
| 465 |
chan[c.chan].std.mask(c.value,false); |
| 466 |
break; |
| 467 |
case DIV_CMD_MACRO_RESTART: |
| 468 |
chan[c.chan].std.restart(c.value); |
| 469 |
break; |
| 470 |
default: |
| 471 |
break; |
| 472 |
} |
| 473 |
return 1; |
| 474 |
} |
| 475 |
|
| 476 |
void DivPlatformVRC6::muteChannel(int ch, bool mute) { |
| 477 |
isMuted[ch]=mute; |
| 478 |
if (isMuted[ch]) { |
| 479 |
chWrite(ch,0,0); |
| 480 |
} else if (chan[ch].active) { |
| 481 |
if (ch==2) { // sawtooth |
| 482 |
chWrite(ch,0,chan[ch].outVol); |
| 483 |
} else { |
| 484 |
chWrite(ch,0,chan[ch].pcm?chan[ch].dacOut:((chan[ch].outVol&0xf)|((chan[ch].duty&7)<<4))); |
| 485 |
} |
| 486 |
} |
| 487 |
} |
| 488 |
|
| 489 |
void DivPlatformVRC6::forceIns() { |
| 490 |
for (int i=0; i<3; i++) { |
| 491 |
chan[i].insChanged=true; |
| 492 |
chan[i].freqChanged=true; |
| 493 |
} |
| 494 |
} |
| 495 |
|
| 496 |
void* DivPlatformVRC6::getChanState(int ch) { |
| 497 |
return &chan[ch]; |
| 498 |
} |
| 499 |
|
| 500 |
DivMacroInt* DivPlatformVRC6::getChanMacroInt(int ch) { |
| 501 |
return &chan[ch].std; |
| 502 |
} |
| 503 |
|
| 504 |
DivSamplePos DivPlatformVRC6::getSamplePos(int ch) { |
| 505 |
if (ch>=2) return DivSamplePos(); |
| 506 |
if (!chan[ch].pcm) return DivSamplePos(); |
| 507 |
return DivSamplePos( |
| 508 |
chan[ch].dacSample, |
| 509 |
chan[ch].dacPos, |
| 510 |
chan[ch].dacRate |
| 511 |
); |
| 512 |
} |
| 513 |
|
| 514 |
DivDispatchOscBuffer* DivPlatformVRC6::getOscBuffer(int ch) { |
| 515 |
return oscBuf[ch]; |
| 516 |
} |
| 517 |
|
| 518 |
unsigned char* DivPlatformVRC6::getRegisterPool() { |
| 519 |
return regPool; |
| 520 |
} |
| 521 |
|
| 522 |
int DivPlatformVRC6::getRegisterPoolSize() { |
| 523 |
return 13; |
| 524 |
} |
| 525 |
|
| 526 |
void DivPlatformVRC6::reset() { |
| 527 |
for (int i=0; i<3; i++) { |
| 528 |
chan[i]=DivPlatformVRC6::Channel(); |
| 529 |
chan[i].std.setEngine(parent); |
| 530 |
} |
| 531 |
// HELP |
| 532 |
chan[2].vol=63; |
| 533 |
chan[2].outVol=63; |
| 534 |
if (dumpWrites) { |
| 535 |
addWrite(0xffffffff,0); |
| 536 |
} |
| 537 |
|
| 538 |
sampleBank=0; |
| 539 |
prevSample=0; |
| 540 |
|
| 541 |
vrc6.reset(); |
| 542 |
// Initialize control register |
| 543 |
rWrite(0x9003,0); |
| 544 |
// Clear internal IRQ |
| 545 |
rWrite(0xf000,0); |
| 546 |
rWrite(0xf001,0); |
| 547 |
rWrite(0xf002,0); |
| 548 |
} |
| 549 |
|
| 550 |
bool DivPlatformVRC6::keyOffAffectsArp(int ch) { |
| 551 |
return true; |
| 552 |
} |
| 553 |
|
| 554 |
bool DivPlatformVRC6::hasAcquireDirect() { |
| 555 |
return true; |
| 556 |
} |
| 557 |
|
| 558 |
void DivPlatformVRC6::setFlags(const DivConfig& flags) { |
| 559 |
int clockSel=flags.getInt("clockSel",0); |
| 560 |
if (clockSel==2) { // Dendy |
| 561 |
chipClock=COLOR_PAL*2.0/5.0; |
| 562 |
} else if (clockSel==1) { // PAL |
| 563 |
chipClock=COLOR_PAL*3.0/8.0; |
| 564 |
} else { // NTSC |
| 565 |
chipClock=COLOR_NTSC/2.0; |
| 566 |
} |
| 567 |
CHECK_CUSTOM_CLOCK; |
| 568 |
rate=chipClock; |
| 569 |
for (int i=0; i<3; i++) { |
| 570 |
oscBuf[i]->setRate(rate); |
| 571 |
} |
| 572 |
} |
| 573 |
|
| 574 |
void DivPlatformVRC6::notifyInsDeletion(void* ins) { |
| 575 |
for (int i=0; i<3; i++) { |
| 576 |
chan[i].std.notifyInsDeletion((DivInstrument*)ins); |
| 577 |
} |
| 578 |
} |
| 579 |
|
| 580 |
void DivPlatformVRC6::poke(unsigned int addr, unsigned short val) { |
| 581 |
rWrite(addr,val); |
| 582 |
} |
| 583 |
|
| 584 |
void DivPlatformVRC6::poke(std::vector<DivRegWrite>& wlist) { |
| 585 |
for (DivRegWrite& i: wlist) rWrite(i.addr,i.val); |
| 586 |
} |
| 587 |
|
| 588 |
int DivPlatformVRC6::init(DivEngine* p, int channels, int sugRate, const DivConfig& flags) { |
| 589 |
parent=p; |
| 590 |
dumpWrites=false; |
| 591 |
skipRegisterWrites=false; |
| 592 |
for (int i=0; i<3; i++) { |
| 593 |
isMuted[i]=false; |
| 594 |
oscBuf[i]=new DivDispatchOscBuffer; |
| 595 |
} |
| 596 |
setFlags(flags); |
| 597 |
reset(); |
| 598 |
return 3; |
| 599 |
} |
| 600 |
|
| 601 |
void DivPlatformVRC6::quit() { |
| 602 |
for (int i=0; i<3; i++) { |
| 603 |
delete oscBuf[i]; |
| 604 |
} |
| 605 |
} |
| 606 |
|
| 607 |
DivPlatformVRC6::~DivPlatformVRC6() { |
| 608 |
} |