| 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 "scc.h" |
| 21 |
#include "../engine.h" |
| 22 |
#include <math.h> |
| 23 |
|
| 24 |
#define CHIP_DIVIDER 16 |
| 25 |
|
| 26 |
#define rWrite(a,v) {if (!skipRegisterWrites) {scc->scc_w(true,a,v); regPool[a]=v; if (dumpWrites) addWrite(a,v); }} |
| 27 |
|
| 28 |
const char* regCheatSheetSCC[]={ |
| 29 |
"Ch1_Wave", "00", |
| 30 |
"Ch2_Wave", "20", |
| 31 |
"Ch3_Wave", "40", |
| 32 |
"Ch4_5_Wave", "60", |
| 33 |
"Ch1_FreqL", "80", |
| 34 |
"Ch1_FreqH", "81", |
| 35 |
"Ch2_FreqL", "82", |
| 36 |
"Ch2_FreqH", "83", |
| 37 |
"Ch3_FreqL", "84", |
| 38 |
"Ch3_FreqH", "85", |
| 39 |
"Ch4_FreqL", "86", |
| 40 |
"Ch4_FreqH", "87", |
| 41 |
"Ch5_FreqL", "88", |
| 42 |
"Ch5_FreqH", "89", |
| 43 |
"Ch1_Vol", "8a", |
| 44 |
"Ch2_Vol", "8b", |
| 45 |
"Ch3_Vol", "8c", |
| 46 |
"Ch4_Vol", "8d", |
| 47 |
"Ch5_Vol", "8e", |
| 48 |
"Output", "8f", |
| 49 |
"Test", "e0", |
| 50 |
NULL |
| 51 |
}; |
| 52 |
|
| 53 |
const char* regCheatSheetSCCPlus[]={ |
| 54 |
"Ch1_Wave", "00", |
| 55 |
"Ch2_Wave", "20", |
| 56 |
"Ch3_Wave", "40", |
| 57 |
"Ch4_Wave", "60", |
| 58 |
"Ch5_Wave", "80", |
| 59 |
"Ch1_FreqL", "a0", |
| 60 |
"Ch1_FreqH", "a1", |
| 61 |
"Ch2_FreqL", "a2", |
| 62 |
"Ch2_FreqH", "a3", |
| 63 |
"Ch3_FreqL", "a4", |
| 64 |
"Ch3_FreqH", "a5", |
| 65 |
"Ch4_FreqL", "a6", |
| 66 |
"Ch4_FreqH", "a7", |
| 67 |
"Ch5_FreqL", "a8", |
| 68 |
"Ch5_FreqH", "a9", |
| 69 |
"Ch1_Vol", "aa", |
| 70 |
"Ch2_Vol", "ab", |
| 71 |
"Ch3_Vol", "ac", |
| 72 |
"Ch4_Vol", "ad", |
| 73 |
"Ch5_Vol", "ae", |
| 74 |
"Output", "af", |
| 75 |
"Test", "c0", |
| 76 |
NULL |
| 77 |
}; |
| 78 |
|
| 79 |
const char** DivPlatformSCC::getRegisterSheet() { |
| 80 |
return isPlus ? regCheatSheetSCCPlus : regCheatSheetSCC; |
| 81 |
} |
| 82 |
|
| 83 |
void DivPlatformSCC::acquire(short** buf, size_t len) { |
| 84 |
} |
| 85 |
|
| 86 |
void DivPlatformSCC::acquireDirect(blip_buffer_t** bb, size_t len) { |
| 87 |
for (int i=0; i<5; i++) { |
| 88 |
oscBuf[i]->begin(len); |
| 89 |
} |
| 90 |
|
| 91 |
scc->tick(len,bb[0],oscBuf); |
| 92 |
//for (int i=0; i<5; i++) { |
| 93 |
// oscBuf[i]->putSample(h,scc->voice_out(i)<<7); |
| 94 |
//} |
| 95 |
|
| 96 |
for (int i=0; i<5; i++) { |
| 97 |
oscBuf[i]->end(len); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
void DivPlatformSCC::updateWave(int ch) { |
| 102 |
int dstCh=(!isPlus && ch>=4)?3:ch; |
| 103 |
if (ch==3) { |
| 104 |
lastUpdated34=3; |
| 105 |
} else if (ch==4) { |
| 106 |
lastUpdated34=4; |
| 107 |
} |
| 108 |
for (int i=0; i<32; i++) { |
| 109 |
rWrite(dstCh*32+i,(unsigned char)chan[ch].ws.output[i]-128); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
void DivPlatformSCC::tick(bool sysTick) { |
| 114 |
for (int i=0; i<5; i++) { |
| 115 |
chan[i].std.next(); |
| 116 |
if (chan[i].std.vol.had) { |
| 117 |
chan[i].outVol=((chan[i].vol&15)*MIN(15,chan[i].std.vol.val))/15; |
| 118 |
rWrite(regBase+10+i,chan[i].outVol); |
| 119 |
} |
| 120 |
if (NEW_ARP_STRAT) { |
| 121 |
chan[i].handleArp(); |
| 122 |
} else if (chan[i].std.arp.had) { |
| 123 |
if (!chan[i].inPorta) { |
| 124 |
chan[i].baseFreq=NOTE_PERIODIC(parent->calcArp(chan[i].note,chan[i].std.arp.val)); |
| 125 |
} |
| 126 |
chan[i].freqChanged=true; |
| 127 |
} |
| 128 |
if (chan[i].std.wave.had) { |
| 129 |
if (chan[i].wave!=chan[i].std.wave.val || chan[i].ws.activeChanged()) { |
| 130 |
chan[i].wave=chan[i].std.wave.val; |
| 131 |
chan[i].ws.changeWave1(chan[i].wave); |
| 132 |
} |
| 133 |
} |
| 134 |
if (chan[i].std.pitch.had) { |
| 135 |
if (chan[i].std.pitch.mode) { |
| 136 |
chan[i].pitch2+=chan[i].std.pitch.val; |
| 137 |
CLAMP_VAR(chan[i].pitch2,-32768,32767); |
| 138 |
} else { |
| 139 |
chan[i].pitch2=chan[i].std.pitch.val; |
| 140 |
} |
| 141 |
chan[i].freqChanged=true; |
| 142 |
} |
| 143 |
if (chan[i].active) { |
| 144 |
if (chan[i].ws.tick()) { |
| 145 |
updateWave(i); |
| 146 |
} |
| 147 |
} |
| 148 |
if (chan[i].freqChanged) { |
| 149 |
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)-1; |
| 150 |
if (chan[i].freq<0) chan[i].freq=0; |
| 151 |
if (chan[i].freq>4095) chan[i].freq=4095; |
| 152 |
if (!chan[i].freqInit || regPool[regBase+0+i*2]!=(chan[i].freq&0xff)) { |
| 153 |
rWrite(regBase+0+i*2,chan[i].freq&0xff); |
| 154 |
} |
| 155 |
if (!chan[i].freqInit || regPool[regBase+1+i*2]!=(chan[i].freq>>8)) { |
| 156 |
rWrite(regBase+1+i*2,chan[i].freq>>8); |
| 157 |
} |
| 158 |
chan[i].freqChanged=false; |
| 159 |
chan[i].freqInit=!skipRegisterWrites; |
| 160 |
} |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
int DivPlatformSCC::dispatch(DivCommand c) { |
| 165 |
switch (c.cmd) { |
| 166 |
case DIV_CMD_NOTE_ON: { |
| 167 |
DivInstrument* ins=parent->getIns(chan[c.chan].ins,DIV_INS_SCC); |
| 168 |
if (c.value!=DIV_NOTE_NULL) { |
| 169 |
chan[c.chan].baseFreq=NOTE_PERIODIC(c.value); |
| 170 |
chan[c.chan].freqChanged=true; |
| 171 |
chan[c.chan].note=c.value; |
| 172 |
} |
| 173 |
chan[c.chan].active=true; |
| 174 |
chan[c.chan].macroInit(ins); |
| 175 |
if (!parent->song.brokenOutVol && !chan[c.chan].std.vol.will) { |
| 176 |
chan[c.chan].outVol=chan[c.chan].vol; |
| 177 |
} |
| 178 |
if (!isMuted[c.chan]) { |
| 179 |
rWrite(regBase+15,regPool[regBase+15]|(1<<c.chan)); |
| 180 |
} |
| 181 |
if (chan[c.chan].wave<0) { |
| 182 |
chan[c.chan].wave=0; |
| 183 |
chan[c.chan].ws.changeWave1(chan[c.chan].wave); |
| 184 |
} |
| 185 |
chan[c.chan].ws.init(ins,32,255,chan[c.chan].insChanged); |
| 186 |
chan[c.chan].insChanged=false; |
| 187 |
break; |
| 188 |
} |
| 189 |
case DIV_CMD_NOTE_OFF: |
| 190 |
chan[c.chan].active=false; |
| 191 |
chan[c.chan].macroInit(NULL); |
| 192 |
rWrite(regBase+15,regPool[regBase+15]&~(1<<c.chan)); |
| 193 |
break; |
| 194 |
case DIV_CMD_NOTE_OFF_ENV: |
| 195 |
case DIV_CMD_ENV_RELEASE: |
| 196 |
chan[c.chan].std.release(); |
| 197 |
break; |
| 198 |
case DIV_CMD_INSTRUMENT: |
| 199 |
if (chan[c.chan].ins!=c.value || c.value2==1) { |
| 200 |
chan[c.chan].ins=c.value; |
| 201 |
} |
| 202 |
break; |
| 203 |
case DIV_CMD_VOLUME: |
| 204 |
if (chan[c.chan].vol!=c.value) { |
| 205 |
chan[c.chan].vol=c.value; |
| 206 |
if (!chan[c.chan].std.vol.has) { |
| 207 |
chan[c.chan].outVol=c.value; |
| 208 |
rWrite(regBase+10+c.chan,c.value); |
| 209 |
} |
| 210 |
} |
| 211 |
break; |
| 212 |
case DIV_CMD_GET_VOLUME: |
| 213 |
if (chan[c.chan].std.vol.has) { |
| 214 |
return chan[c.chan].vol; |
| 215 |
} |
| 216 |
return chan[c.chan].outVol; |
| 217 |
break; |
| 218 |
case DIV_CMD_PITCH: |
| 219 |
chan[c.chan].pitch=c.value; |
| 220 |
chan[c.chan].freqChanged=true; |
| 221 |
break; |
| 222 |
case DIV_CMD_WAVE: |
| 223 |
chan[c.chan].wave=c.value; |
| 224 |
chan[c.chan].ws.changeWave1(chan[c.chan].wave); |
| 225 |
break; |
| 226 |
case DIV_CMD_NOTE_PORTA: { |
| 227 |
int destFreq=NOTE_PERIODIC(c.value2); |
| 228 |
bool return2=false; |
| 229 |
if (destFreq>chan[c.chan].baseFreq) { |
| 230 |
chan[c.chan].baseFreq+=c.value; |
| 231 |
if (chan[c.chan].baseFreq>=destFreq) { |
| 232 |
chan[c.chan].baseFreq=destFreq; |
| 233 |
return2=true; |
| 234 |
} |
| 235 |
} else { |
| 236 |
chan[c.chan].baseFreq-=c.value; |
| 237 |
if (chan[c.chan].baseFreq<=destFreq) { |
| 238 |
chan[c.chan].baseFreq=destFreq; |
| 239 |
return2=true; |
| 240 |
} |
| 241 |
} |
| 242 |
chan[c.chan].freqChanged=true; |
| 243 |
if (return2) { |
| 244 |
chan[c.chan].inPorta=false; |
| 245 |
return 2; |
| 246 |
} |
| 247 |
break; |
| 248 |
} |
| 249 |
case DIV_CMD_LEGATO: |
| 250 |
chan[c.chan].baseFreq=NOTE_PERIODIC(c.value+((HACKY_LEGATO_MESS)?(chan[c.chan].std.arp.val):(0))); |
| 251 |
chan[c.chan].freqChanged=true; |
| 252 |
chan[c.chan].note=c.value; |
| 253 |
break; |
| 254 |
case DIV_CMD_PRE_PORTA: |
| 255 |
if (chan[c.chan].active && c.value2) { |
| 256 |
if (parent->song.resetMacroOnPorta) chan[c.chan].macroInit(parent->getIns(chan[c.chan].ins,DIV_INS_SCC)); |
| 257 |
} |
| 258 |
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); |
| 259 |
chan[c.chan].inPorta=c.value; |
| 260 |
break; |
| 261 |
case DIV_CMD_GET_VOLMAX: |
| 262 |
return 15; |
| 263 |
break; |
| 264 |
case DIV_CMD_MACRO_OFF: |
| 265 |
chan[c.chan].std.mask(c.value,true); |
| 266 |
break; |
| 267 |
case DIV_CMD_MACRO_ON: |
| 268 |
chan[c.chan].std.mask(c.value,false); |
| 269 |
break; |
| 270 |
case DIV_CMD_MACRO_RESTART: |
| 271 |
chan[c.chan].std.restart(c.value); |
| 272 |
break; |
| 273 |
default: |
| 274 |
break; |
| 275 |
} |
| 276 |
return 1; |
| 277 |
} |
| 278 |
|
| 279 |
void DivPlatformSCC::muteChannel(int ch, bool mute) { |
| 280 |
isMuted[ch]=mute; |
| 281 |
if (mute) { |
| 282 |
rWrite(regBase+15,regPool[regBase+15]&~(1<<ch)); |
| 283 |
} else if (chan[ch].active) { |
| 284 |
rWrite(regBase+15,regPool[regBase+15]|(1<<ch)); |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
void DivPlatformSCC::forceIns() { |
| 289 |
for (int i=0; i<5; i++) { |
| 290 |
chan[i].insChanged=true; |
| 291 |
chan[i].freqChanged=true; |
| 292 |
chan[i].freqInit=false; |
| 293 |
if (isPlus || i<3) { |
| 294 |
updateWave(i); |
| 295 |
} |
| 296 |
rWrite(regBase+10+i,chan[i].outVol); |
| 297 |
} |
| 298 |
if (!isPlus) { |
| 299 |
if (lastUpdated34>=3) { |
| 300 |
updateWave(lastUpdated34); |
| 301 |
} |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
void* DivPlatformSCC::getChanState(int ch) { |
| 306 |
return &chan[ch]; |
| 307 |
} |
| 308 |
|
| 309 |
DivMacroInt* DivPlatformSCC::getChanMacroInt(int ch) { |
| 310 |
return &chan[ch].std; |
| 311 |
} |
| 312 |
|
| 313 |
DivDispatchOscBuffer* DivPlatformSCC::getOscBuffer(int ch) { |
| 314 |
return oscBuf[ch]; |
| 315 |
} |
| 316 |
|
| 317 |
unsigned char* DivPlatformSCC::getRegisterPool() { |
| 318 |
return (unsigned char*)regPool; |
| 319 |
} |
| 320 |
|
| 321 |
int DivPlatformSCC::getRegisterPoolSize() { |
| 322 |
return 225; |
| 323 |
} |
| 324 |
|
| 325 |
void DivPlatformSCC::reset() { |
| 326 |
memset(regPool,0,225); |
| 327 |
scc->reset(); |
| 328 |
for (int i=0; i<5; i++) { |
| 329 |
chan[i]=DivPlatformSCC::Channel(); |
| 330 |
chan[i].std.setEngine(parent); |
| 331 |
chan[i].ws.setEngine(parent,128); |
| 332 |
chan[i].ws.init(NULL,32,255,false); |
| 333 |
chan[i].vol=15; |
| 334 |
chan[i].outVol=15; |
| 335 |
rWrite(regBase+10+i,15); |
| 336 |
} |
| 337 |
if (dumpWrites) { |
| 338 |
addWrite(0xffffffff,0); |
| 339 |
} |
| 340 |
lastUpdated34=0; |
| 341 |
} |
| 342 |
|
| 343 |
int DivPlatformSCC::getOutputCount() { |
| 344 |
return 1; |
| 345 |
} |
| 346 |
|
| 347 |
bool DivPlatformSCC::hasAcquireDirect() { |
| 348 |
return true; |
| 349 |
} |
| 350 |
|
| 351 |
void DivPlatformSCC::notifyWaveChange(int wave) { |
| 352 |
for (int i=0; i<5; i++) { |
| 353 |
if (chan[i].wave==wave) { |
| 354 |
chan[i].ws.changeWave1(chan[i].wave); |
| 355 |
if (chan[i].active) { |
| 356 |
updateWave(i); |
| 357 |
} |
| 358 |
} |
| 359 |
} |
| 360 |
} |
| 361 |
|
| 362 |
void DivPlatformSCC::notifyInsDeletion(void* ins) { |
| 363 |
for (int i=0; i<5; i++) { |
| 364 |
chan[i].std.notifyInsDeletion((DivInstrument*)ins); |
| 365 |
} |
| 366 |
} |
| 367 |
|
| 368 |
void DivPlatformSCC::poke(unsigned int addr, unsigned short val) { |
| 369 |
rWrite(addr,val); |
| 370 |
} |
| 371 |
|
| 372 |
void DivPlatformSCC::poke(std::vector<DivRegWrite>& wlist) { |
| 373 |
for (DivRegWrite& i: wlist) rWrite(i.addr,i.val); |
| 374 |
} |
| 375 |
|
| 376 |
void DivPlatformSCC::setChipModel(bool isplus) { |
| 377 |
isPlus=isplus; |
| 378 |
} |
| 379 |
|
| 380 |
void DivPlatformSCC::setFlags(const DivConfig& flags) { |
| 381 |
switch (flags.getInt("clockSel",0)) { |
| 382 |
case 0x01: |
| 383 |
chipClock=COLOR_PAL*2.0/5.0; |
| 384 |
break; |
| 385 |
case 0x02: |
| 386 |
chipClock=3000000.0/2.0; |
| 387 |
break; |
| 388 |
case 0x03: |
| 389 |
chipClock=4000000.0/2.0; |
| 390 |
break; |
| 391 |
default: |
| 392 |
chipClock=COLOR_NTSC/2.0; |
| 393 |
break; |
| 394 |
} |
| 395 |
CHECK_CUSTOM_CLOCK; |
| 396 |
rate=chipClock*2; |
| 397 |
for (int i=0; i<5; i++) { |
| 398 |
oscBuf[i]->setRate(rate); |
| 399 |
} |
| 400 |
} |
| 401 |
|
| 402 |
int DivPlatformSCC::init(DivEngine* p, int channels, int sugRate, const DivConfig& flags) { |
| 403 |
parent=p; |
| 404 |
dumpWrites=false; |
| 405 |
skipRegisterWrites=false; |
| 406 |
writeOscBuf=0; |
| 407 |
for (int i=0; i<5; i++) { |
| 408 |
isMuted[i]=false; |
| 409 |
oscBuf[i]=new DivDispatchOscBuffer; |
| 410 |
} |
| 411 |
setFlags(flags); |
| 412 |
if (isPlus) { |
| 413 |
scc=new k052539_scc_core; |
| 414 |
regBase=0xa0; |
| 415 |
} else { |
| 416 |
scc=new k051649_scc_core; |
| 417 |
regBase=0x80; |
| 418 |
} |
| 419 |
reset(); |
| 420 |
return 5; |
| 421 |
} |
| 422 |
|
| 423 |
void DivPlatformSCC::quit() { |
| 424 |
for (int i=0; i<5; i++) { |
| 425 |
delete oscBuf[i]; |
| 426 |
} |
| 427 |
if (scc!=NULL) { |
| 428 |
delete scc; |
| 429 |
} |
| 430 |
} |
| 431 |
|
| 432 |
DivPlatformSCC::~DivPlatformSCC() { |
| 433 |
} |