| 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 "scvtone.h" |
| 21 |
#include "../engine.h" |
| 22 |
#include "../../ta-log.h" |
| 23 |
#include "furIcons.h" |
| 24 |
#include <math.h> |
| 25 |
|
| 26 |
//#define rWrite(a,v) pendingWrites[a]=v; |
| 27 |
#define rWrite(a,v) if (!skipRegisterWrites) {packet[a]=v; writePacket=true; if (dumpWrites) {addWrite(a,v);} } |
| 28 |
|
| 29 |
const char* regCheatSheetUPD1771cTone[]={ |
| 30 |
NULL |
| 31 |
}; |
| 32 |
|
| 33 |
const char** DivPlatformSCV::getRegisterSheet() { |
| 34 |
return regCheatSheetUPD1771cTone; |
| 35 |
} |
| 36 |
|
| 37 |
void DivPlatformSCV::acquire(short** buf, size_t len) { |
| 38 |
for (int i=0; i<4; i++) { |
| 39 |
oscBuf[i]->begin(len); |
| 40 |
} |
| 41 |
|
| 42 |
for (size_t h=0; h<len; h++) { |
| 43 |
while (!writes.empty()) { |
| 44 |
QueuedWrite w=writes.front(); |
| 45 |
scv.write(w.val); |
| 46 |
regPool[w.addr&0xf]=w.val; |
| 47 |
writes.pop(); |
| 48 |
} |
| 49 |
|
| 50 |
scv.sound_stream_update(&buf[0][h],1); |
| 51 |
oscBuf[0]->putSample(h,scv.chout[0]<<3); |
| 52 |
oscBuf[1]->putSample(h,scv.chout[1]<<3); |
| 53 |
oscBuf[2]->putSample(h,scv.chout[2]<<3); |
| 54 |
oscBuf[3]->putSample(h,scv.chout[3]<<3); |
| 55 |
} |
| 56 |
|
| 57 |
for (int i=0; i<4; i++) { |
| 58 |
oscBuf[i]->end(len); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
void DivPlatformSCV::tick(bool sysTick) { |
| 63 |
for (int i=0; i<4; i++) { |
| 64 |
int CHIP_DIVIDER=(i<3)?512:64; |
| 65 |
chan[i].std.next(); |
| 66 |
if (chan[i].std.vol.had) { |
| 67 |
chan[i].outVol=VOL_SCALE_LINEAR(chan[i].vol&31,MIN(31,chan[i].std.vol.val),31); |
| 68 |
} |
| 69 |
if (NEW_ARP_STRAT) { |
| 70 |
chan[i].handleArp(); |
| 71 |
} else if (chan[i].std.arp.had) { |
| 72 |
if (!chan[i].inPorta) { |
| 73 |
int f=parent->calcArp(chan[i].note,chan[i].std.arp.val); |
| 74 |
chan[i].baseFreq=NOTE_PERIODIC(f); |
| 75 |
} |
| 76 |
chan[i].freqChanged=true; |
| 77 |
} |
| 78 |
if (chan[i].std.wave.had) { |
| 79 |
chan[i].wave=chan[i].std.wave.val&7; |
| 80 |
} |
| 81 |
if (chan[i].std.duty.had && i==3) { |
| 82 |
waveMode=chan[i].std.duty.val&1; |
| 83 |
} |
| 84 |
if (chan[i].std.ex1.had) { |
| 85 |
chan[i].pos=chan[i].std.ex1.val&31; |
| 86 |
} |
| 87 |
if (chan[i].std.pitch.had) { |
| 88 |
if (chan[i].std.pitch.mode) { |
| 89 |
chan[i].pitch2+=chan[i].std.pitch.val; |
| 90 |
CLAMP_VAR(chan[i].pitch2,-32768,32767); |
| 91 |
} else { |
| 92 |
chan[i].pitch2=chan[i].std.pitch.val; |
| 93 |
} |
| 94 |
chan[i].freqChanged=true; |
| 95 |
} |
| 96 |
if (chan[i].freqChanged || chan[i].keyOn || chan[i].keyOff) { |
| 97 |
if (i==3) { |
| 98 |
if (waveMode) { |
| 99 |
chan[i].freq=parent->calcFreq(chan[i].baseFreq,chan[i].pitch,chan[i].fixedArp?chan[i].baseNoteOverride:chan[i].arpOff,chan[i].fixedArp,true,0,chan[i].pitch2,chipClock,CHIP_DIVIDER); |
| 100 |
} else { |
| 101 |
chan[i].freq=(chan[i].baseFreq+chan[i].pitch+chan[i].pitch2+143); |
| 102 |
} |
| 103 |
if (!parent->song.oldArpStrategy) { |
| 104 |
if (chan[i].fixedArp) { |
| 105 |
chan[i].freq=(chan[i].baseNoteOverride)+chan[i].pitch+chan[i].pitch2; |
| 106 |
} else { |
| 107 |
chan[i].freq+=chan[i].arpOff; |
| 108 |
} |
| 109 |
} |
| 110 |
} else { |
| 111 |
chan[i].freq=parent->calcFreq(chan[i].baseFreq,chan[i].pitch,chan[i].fixedArp?chan[i].baseNoteOverride:chan[i].arpOff,chan[i].fixedArp,true,0,chan[i].pitch2,chipClock,CHIP_DIVIDER); |
| 112 |
} |
| 113 |
if (i==3) { |
| 114 |
if (waveMode) { |
| 115 |
if (chan[i].keyOn) kon[i]=1; |
| 116 |
if (chan[i].keyOff) kon[i]=0; |
| 117 |
} |
| 118 |
if (chan[i].freq<0) chan[i].freq=0; |
| 119 |
} else { |
| 120 |
if (chan[i].freq<1) chan[i].freq=1; |
| 121 |
} |
| 122 |
if (chan[i].freq>255) chan[i].freq=255; |
| 123 |
if (chan[i].keyOn) chan[i].keyOn=false; |
| 124 |
if (chan[i].keyOff) chan[i].keyOff=false; |
| 125 |
chan[i].freqChanged=false; |
| 126 |
} |
| 127 |
|
| 128 |
if (waveMode) { |
| 129 |
if (kon[i]) { |
| 130 |
if (i==3) { |
| 131 |
rWrite(0,2); |
| 132 |
rWrite(1,(chan[i].wave<<5)|chan[i].pos); |
| 133 |
// TODO: improve |
| 134 |
float p = ((float)chan[i].freq)/((float)(31-chan[i].pos))*31.0; |
| 135 |
rWrite(2,MIN(MAX((int)p,0),255)); |
| 136 |
rWrite(3,chan[i].outVol); |
| 137 |
} |
| 138 |
} else { |
| 139 |
if (i == 0) { |
| 140 |
rWrite(0,0); |
| 141 |
} |
| 142 |
} |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
if (!waveMode) { |
| 147 |
rWrite(0,1); |
| 148 |
rWrite(1,chan[3].wave<<5); |
| 149 |
rWrite(2,(0xff-chan[3].freq)); |
| 150 |
rWrite(3,(chan[3].active && !isMuted[3])?(chan[3].outVol&0x1f):0); |
| 151 |
rWrite(4,chan[0].freq-1); |
| 152 |
rWrite(5,chan[1].freq-1); |
| 153 |
rWrite(6,chan[2].freq-1); |
| 154 |
rWrite(7,(chan[0].active && !isMuted[0])?chan[0].outVol:0); |
| 155 |
rWrite(8,(chan[1].active && !isMuted[1])?chan[1].outVol:0); |
| 156 |
rWrite(9,(chan[2].active && !isMuted[2])?chan[2].outVol:0); |
| 157 |
} |
| 158 |
|
| 159 |
// if need be, write packet |
| 160 |
if (writePacket) { |
| 161 |
writePacket=false; |
| 162 |
int len=1; |
| 163 |
if (packet[0]==2) { |
| 164 |
len=4; |
| 165 |
} else if (packet[0]==1) { |
| 166 |
len=10; |
| 167 |
} |
| 168 |
|
| 169 |
for (int i=0; i<len; i++) { |
| 170 |
writes.push(QueuedWrite(0,packet[i])); |
| 171 |
} |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
int DivPlatformSCV::dispatch(DivCommand c) { |
| 176 |
int CHIP_DIVIDER=(c.chan<3)?512:64; |
| 177 |
switch (c.cmd) { |
| 178 |
case DIV_CMD_NOTE_ON: { |
| 179 |
DivInstrument* ins=parent->getIns(chan[c.chan].ins,DIV_INS_UPD1771C); |
| 180 |
if (c.value!=DIV_NOTE_NULL) { |
| 181 |
chan[c.chan].baseFreq=NOTE_PERIODIC(c.value); |
| 182 |
chan[c.chan].freqChanged=true; |
| 183 |
chan[c.chan].note=c.value; |
| 184 |
} |
| 185 |
chan[c.chan].active=true; |
| 186 |
chan[c.chan].keyOn=true; |
| 187 |
//chwrite(c.chan,0x04,0x80|chan[c.chan].vol); |
| 188 |
chan[c.chan].macroInit(ins); |
| 189 |
if (!parent->song.brokenOutVol && !chan[c.chan].std.vol.will) { |
| 190 |
chan[c.chan].outVol=chan[c.chan].vol; |
| 191 |
} |
| 192 |
chan[c.chan].insChanged=false; |
| 193 |
break; |
| 194 |
} |
| 195 |
case DIV_CMD_NOTE_OFF: |
| 196 |
chan[c.chan].active=false; |
| 197 |
chan[c.chan].keyOff=true; |
| 198 |
chan[c.chan].macroInit(NULL); |
| 199 |
break; |
| 200 |
case DIV_CMD_NOTE_OFF_ENV: |
| 201 |
case DIV_CMD_ENV_RELEASE: |
| 202 |
chan[c.chan].std.release(); |
| 203 |
break; |
| 204 |
case DIV_CMD_INSTRUMENT: |
| 205 |
if (chan[c.chan].ins!=c.value || c.value2==1) { |
| 206 |
chan[c.chan].ins=c.value; |
| 207 |
chan[c.chan].insChanged=true; |
| 208 |
} |
| 209 |
break; |
| 210 |
case DIV_CMD_VOLUME: |
| 211 |
if (chan[c.chan].vol!=c.value) { |
| 212 |
chan[c.chan].vol=c.value; |
| 213 |
if (!chan[c.chan].std.vol.has) { |
| 214 |
chan[c.chan].outVol=c.value; |
| 215 |
if (chan[c.chan].active) { |
| 216 |
//chwrite(c.chan,0x04,0x80|chan[c.chan].outVol); |
| 217 |
} |
| 218 |
} |
| 219 |
} |
| 220 |
break; |
| 221 |
case DIV_CMD_GET_VOLUME: |
| 222 |
if (chan[c.chan].std.vol.has) { |
| 223 |
return chan[c.chan].vol; |
| 224 |
} |
| 225 |
return chan[c.chan].outVol; |
| 226 |
break; |
| 227 |
case DIV_CMD_PITCH: |
| 228 |
chan[c.chan].pitch=c.value; |
| 229 |
chan[c.chan].freqChanged=true; |
| 230 |
break; |
| 231 |
case DIV_CMD_NOTE_PORTA: { |
| 232 |
int destFreq=NOTE_PERIODIC(c.value2); |
| 233 |
bool return2=false; |
| 234 |
if (destFreq>chan[c.chan].baseFreq) { |
| 235 |
chan[c.chan].baseFreq+=c.value; |
| 236 |
if (chan[c.chan].baseFreq>=destFreq) { |
| 237 |
chan[c.chan].baseFreq=destFreq; |
| 238 |
return2=true; |
| 239 |
} |
| 240 |
} else { |
| 241 |
chan[c.chan].baseFreq-=c.value; |
| 242 |
if (chan[c.chan].baseFreq<=destFreq) { |
| 243 |
chan[c.chan].baseFreq=destFreq; |
| 244 |
return2=true; |
| 245 |
} |
| 246 |
} |
| 247 |
chan[c.chan].freqChanged=true; |
| 248 |
if (return2) { |
| 249 |
chan[c.chan].inPorta=false; |
| 250 |
return 2; |
| 251 |
} |
| 252 |
break; |
| 253 |
} |
| 254 |
case DIV_CMD_STD_NOISE_MODE: |
| 255 |
chan[c.chan].wave=c.value&7; |
| 256 |
chan[c.chan].duty=c.value>>4&1; |
| 257 |
break; |
| 258 |
case DIV_CMD_N163_WAVE_POSITION: |
| 259 |
chan[c.chan].pos=c.value; |
| 260 |
break; |
| 261 |
case DIV_CMD_LEGATO: { |
| 262 |
int newNote=c.value+((HACKY_LEGATO_MESS)?(chan[c.chan].std.arp.val):(0)); |
| 263 |
chan[c.chan].baseFreq=NOTE_PERIODIC(newNote); |
| 264 |
chan[c.chan].freqChanged=true; |
| 265 |
chan[c.chan].note=c.value; |
| 266 |
break; |
| 267 |
} |
| 268 |
case DIV_CMD_PRE_PORTA: |
| 269 |
if (chan[c.chan].active && c.value2) { |
| 270 |
if (parent->song.resetMacroOnPorta) chan[c.chan].macroInit(parent->getIns(chan[c.chan].ins,DIV_INS_UPD1771C)); |
| 271 |
} |
| 272 |
if (!chan[c.chan].inPorta && c.value && !parent->song.brokenPortaArp && chan[c.chan].std.arp.will && !NEW_ARP_STRAT) { |
| 273 |
chan[c.chan].baseFreq=NOTE_PERIODIC(chan[c.chan].note); |
| 274 |
} |
| 275 |
chan[c.chan].inPorta=c.value; |
| 276 |
break; |
| 277 |
case DIV_CMD_GET_VOLMAX: |
| 278 |
return 31; |
| 279 |
break; |
| 280 |
case DIV_CMD_MACRO_OFF: |
| 281 |
chan[c.chan].std.mask(c.value,true); |
| 282 |
break; |
| 283 |
case DIV_CMD_MACRO_ON: |
| 284 |
chan[c.chan].std.mask(c.value,false); |
| 285 |
break; |
| 286 |
case DIV_CMD_MACRO_RESTART: |
| 287 |
chan[c.chan].std.restart(c.value); |
| 288 |
break; |
| 289 |
default: |
| 290 |
break; |
| 291 |
} |
| 292 |
return 1; |
| 293 |
} |
| 294 |
|
| 295 |
void DivPlatformSCV::muteChannel(int ch, bool mute) { |
| 296 |
isMuted[ch]=mute; |
| 297 |
} |
| 298 |
|
| 299 |
void DivPlatformSCV::forceIns() { |
| 300 |
for (int i=0; i<4; i++) { |
| 301 |
chan[i].insChanged=true; |
| 302 |
chan[i].freqChanged=true; |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
void* DivPlatformSCV::getChanState(int ch) { |
| 307 |
return &chan[ch]; |
| 308 |
} |
| 309 |
|
| 310 |
DivMacroInt* DivPlatformSCV::getChanMacroInt(int ch) { |
| 311 |
return &chan[ch].std; |
| 312 |
} |
| 313 |
|
| 314 |
DivDispatchOscBuffer* DivPlatformSCV::getOscBuffer(int ch) { |
| 315 |
return oscBuf[ch]; |
| 316 |
} |
| 317 |
|
| 318 |
unsigned char* DivPlatformSCV::getRegisterPool() { |
| 319 |
return regPool; |
| 320 |
} |
| 321 |
|
| 322 |
int DivPlatformSCV::getRegisterPoolSize() { |
| 323 |
return 16; |
| 324 |
} |
| 325 |
|
| 326 |
void DivPlatformSCV::reset() { |
| 327 |
writes.clear(); |
| 328 |
memset(regPool,0,16); |
| 329 |
for (int i=0; i<4; i++) { |
| 330 |
chan[i]=DivPlatformSCV::Channel(); |
| 331 |
chan[i].std.setEngine(parent); |
| 332 |
} |
| 333 |
if (dumpWrites) { |
| 334 |
addWrite(0xffffffff,0); |
| 335 |
} |
| 336 |
scv.device_reset(); |
| 337 |
memset(tempL,0,32*sizeof(int)); |
| 338 |
memset(tempR,0,32*sizeof(int)); |
| 339 |
memset(kon,0,4); |
| 340 |
memset(initWrite,1,4); |
| 341 |
memset(packet,0,16); |
| 342 |
writePacket=false; |
| 343 |
waveMode=false; |
| 344 |
} |
| 345 |
|
| 346 |
int DivPlatformSCV::getOutputCount() { |
| 347 |
return 1; |
| 348 |
} |
| 349 |
|
| 350 |
bool DivPlatformSCV::keyOffAffectsArp(int ch) { |
| 351 |
return true; |
| 352 |
} |
| 353 |
|
| 354 |
void DivPlatformSCV::notifyInsDeletion(void* ins) { |
| 355 |
for (int i=0; i<4; i++) { |
| 356 |
chan[i].std.notifyInsDeletion((DivInstrument*)ins); |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
void DivPlatformSCV::setFlags(const DivConfig& flags) { |
| 361 |
chipClock=6000000; |
| 362 |
CHECK_CUSTOM_CLOCK; |
| 363 |
rate=chipClock/4; |
| 364 |
for (int i=0; i<4; i++) { |
| 365 |
oscBuf[i]->setRate(rate); |
| 366 |
} |
| 367 |
//upd1771c_sound_set_clock(&scv,(unsigned int)chipClock,8); |
| 368 |
} |
| 369 |
|
| 370 |
void DivPlatformSCV::poke(unsigned int addr, unsigned short val) { |
| 371 |
rWrite(addr,val); |
| 372 |
} |
| 373 |
|
| 374 |
void DivPlatformSCV::poke(std::vector<DivRegWrite>& wlist) { |
| 375 |
for (DivRegWrite& i: wlist) rWrite(i.addr,i.val); |
| 376 |
} |
| 377 |
|
| 378 |
int DivPlatformSCV::init(DivEngine* p, int channels, int sugRate, const DivConfig& flags) { |
| 379 |
parent=p; |
| 380 |
dumpWrites=false; |
| 381 |
skipRegisterWrites=false; |
| 382 |
for (int i=0; i<4; i++) { |
| 383 |
isMuted[i]=false; |
| 384 |
oscBuf[i]=new DivDispatchOscBuffer; |
| 385 |
} |
| 386 |
setFlags(flags); |
| 387 |
reset(); |
| 388 |
return 1; |
| 389 |
} |
| 390 |
|
| 391 |
void DivPlatformSCV::quit() { |
| 392 |
for (int i=0; i<4; i++) { |
| 393 |
delete oscBuf[i]; |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
DivPlatformSCV::~DivPlatformSCV() { |
| 398 |
} |