| 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 "vic20.h" |
| 21 |
#include "../engine.h" |
| 22 |
#include "../../ta-log.h" |
| 23 |
#include <math.h> |
| 24 |
|
| 25 |
#define rWrite(a,v) {regPool[(a)]=(v)&0xff; vic_sound_machine_store(vic,a,(v)&0xff);} |
| 26 |
|
| 27 |
#define CHIP_DIVIDER 32 |
| 28 |
#define SAMP_DIVIDER 4 |
| 29 |
|
| 30 |
const char* regCheatSheetVIC[]={ |
| 31 |
"CH1_Pitch", "0A", |
| 32 |
"CH2_Pitch", "0B", |
| 33 |
"CH3_Pitch", "0C", |
| 34 |
"Noise_Pitch", "0D", |
| 35 |
"Volume", "0E", |
| 36 |
NULL |
| 37 |
}; |
| 38 |
|
| 39 |
const char** DivPlatformVIC20::getRegisterSheet() { |
| 40 |
return regCheatSheetVIC; |
| 41 |
} |
| 42 |
|
| 43 |
void DivPlatformVIC20::acquire(short** buf, size_t len) { |
| 44 |
const unsigned char loadFreq[3] = {0x7e, 0x7d, 0x7b}; |
| 45 |
const unsigned char wavePatterns[16] = { |
| 46 |
0b0, 0b10, 0b100, 0b110, 0b1000, 0b1010, 0b1011, 0b1110, |
| 47 |
0b10010, 0b10100, 0b10110, 0b11000, 0b11010, 0b100100, 0b101010, 0b101100 |
| 48 |
}; |
| 49 |
|
| 50 |
for (int i=0; i<4; i++) { |
| 51 |
oscBuf[i]->begin(len); |
| 52 |
} |
| 53 |
|
| 54 |
for (size_t h=0; h<len; h++) { |
| 55 |
if (hasWaveWrite) { |
| 56 |
hasWaveWrite=false; |
| 57 |
for (int i=0; i<3; i++) { |
| 58 |
if (chan[i].waveWriteCycle>=0) { |
| 59 |
if (chan[i].waveWriteCycle>=16*7) { |
| 60 |
// empty shift register first |
| 61 |
rWrite(10+i,126); |
| 62 |
} else if (chan[i].waveWriteCycle>=16) { |
| 63 |
unsigned bit=8-(chan[i].waveWriteCycle/16); |
| 64 |
rWrite(10+i,loadFreq[i]|((wavePatterns[chan[i].wave]<<bit)&0x80)); |
| 65 |
} else { |
| 66 |
rWrite(10+i,255-chan[i].freq); |
| 67 |
} |
| 68 |
chan[i].waveWriteCycle-=SAMP_DIVIDER; |
| 69 |
hasWaveWrite=true; |
| 70 |
} |
| 71 |
} |
| 72 |
} |
| 73 |
short samp; |
| 74 |
vic_sound_machine_calculate_samples(vic,&samp,1,1,0,SAMP_DIVIDER); |
| 75 |
buf[0][h]=samp; |
| 76 |
for (int i=0; i<4; i++) { |
| 77 |
oscBuf[i]->putSample(h,vic->ch[i].out?(vic->volume<<11):0); |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
for (int i=0; i<4; i++) { |
| 82 |
oscBuf[i]->end(len); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
void DivPlatformVIC20::calcAndWriteOutVol(int ch, int env) { |
| 87 |
chan[ch].outVol=MIN(chan[ch].vol*env/15,15); |
| 88 |
writeOutVol(ch); |
| 89 |
} |
| 90 |
|
| 91 |
void DivPlatformVIC20::writeOutVol(int ch) { |
| 92 |
rWrite(14,chan[ch].outVol); |
| 93 |
} |
| 94 |
|
| 95 |
void DivPlatformVIC20::tick(bool sysTick) { |
| 96 |
for (int i=0; i<4; i++) { |
| 97 |
chan[i].std.next(); |
| 98 |
if (chan[i].std.vol.had) { |
| 99 |
int env=chan[i].std.vol.val; |
| 100 |
calcAndWriteOutVol(i,env); |
| 101 |
} |
| 102 |
if (NEW_ARP_STRAT) { |
| 103 |
chan[i].handleArp(); |
| 104 |
} else if (chan[i].std.arp.had) { |
| 105 |
if (!chan[i].inPorta) { |
| 106 |
chan[i].baseFreq=NOTE_PERIODIC(parent->calcArp(chan[i].note,chan[i].std.arp.val)); |
| 107 |
} |
| 108 |
chan[i].freqChanged=true; |
| 109 |
} |
| 110 |
if (chan[i].std.duty.had) { |
| 111 |
if (chan[i].onOff!=(bool)chan[i].std.duty.val) { |
| 112 |
chan[i].onOff=(bool)chan[i].std.duty.val; |
| 113 |
if (chan[i].active) { |
| 114 |
if (chan[i].onOff) { |
| 115 |
chan[i].keyOn=true; |
| 116 |
chan[i].keyOff=false; |
| 117 |
} else { |
| 118 |
chan[i].keyOn=false; |
| 119 |
chan[i].keyOff=true; |
| 120 |
} |
| 121 |
} |
| 122 |
} |
| 123 |
} |
| 124 |
if (chan[i].std.wave.had) { |
| 125 |
if (chan[i].wave!=chan[i].std.wave.val) { |
| 126 |
chan[i].wave=chan[i].std.wave.val&0x0f; |
| 127 |
chan[i].keyOn=true; |
| 128 |
} |
| 129 |
} |
| 130 |
if (chan[i].std.pitch.had) { |
| 131 |
if (chan[i].std.pitch.mode) { |
| 132 |
chan[i].pitch2+=chan[i].std.pitch.val; |
| 133 |
CLAMP_VAR(chan[i].pitch2,-32768,32767); |
| 134 |
} else { |
| 135 |
chan[i].pitch2=chan[i].std.pitch.val; |
| 136 |
} |
| 137 |
chan[i].freqChanged=true; |
| 138 |
} |
| 139 |
if (chan[i].freqChanged || chan[i].keyOn || chan[i].keyOff) { |
| 140 |
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); |
| 141 |
if (i<3) { |
| 142 |
chan[i].freq>>=(2-i); |
| 143 |
} else { |
| 144 |
chan[i].freq>>=1; |
| 145 |
} |
| 146 |
if (chan[i].freq<1) chan[i].freq=1; |
| 147 |
if (chan[i].freq>127) chan[i].freq=0; |
| 148 |
if (isMuted[i]) chan[i].keyOn=false; |
| 149 |
if (chan[i].keyOn) { |
| 150 |
if (i<3) { |
| 151 |
// 128*16 cycles for lowest channel to finish counting at lowest freq |
| 152 |
// 2*16 cycles for lowest channel to empty first 2 bits |
| 153 |
// 7*16 cycles to write 7 bits |
| 154 |
chan[i].waveWriteCycle=137*16-1; |
| 155 |
hasWaveWrite=true; |
| 156 |
} else { |
| 157 |
rWrite(10+i,255-chan[i].freq); |
| 158 |
} |
| 159 |
chan[i].keyOn=false; |
| 160 |
} else if (chan[i].freqChanged && chan[i].active && !isMuted[i]) { |
| 161 |
rWrite(10+i,255-chan[i].freq); |
| 162 |
} |
| 163 |
if (chan[i].keyOff) { |
| 164 |
rWrite(10+i,0); |
| 165 |
chan[i].keyOff=false; |
| 166 |
} |
| 167 |
chan[i].freqChanged=false; |
| 168 |
} |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
int DivPlatformVIC20::dispatch(DivCommand c) { |
| 173 |
switch (c.cmd) { |
| 174 |
case DIV_CMD_NOTE_ON: { |
| 175 |
DivInstrument* ins=parent->getIns(chan[c.chan].ins,DIV_INS_VIC); |
| 176 |
if (c.value!=DIV_NOTE_NULL) { |
| 177 |
chan[c.chan].baseFreq=NOTE_PERIODIC(c.value); |
| 178 |
chan[c.chan].freqChanged=true; |
| 179 |
chan[c.chan].note=c.value; |
| 180 |
} |
| 181 |
chan[c.chan].onOff=true; |
| 182 |
chan[c.chan].active=true; |
| 183 |
chan[c.chan].keyOn=true; |
| 184 |
chan[c.chan].macroInit(ins); |
| 185 |
break; |
| 186 |
} |
| 187 |
case DIV_CMD_NOTE_OFF: |
| 188 |
chan[c.chan].active=false; |
| 189 |
chan[c.chan].keyOff=true; |
| 190 |
chan[c.chan].macroInit(NULL); |
| 191 |
break; |
| 192 |
case DIV_CMD_NOTE_OFF_ENV: |
| 193 |
case DIV_CMD_ENV_RELEASE: |
| 194 |
chan[c.chan].std.release(); |
| 195 |
break; |
| 196 |
case DIV_CMD_INSTRUMENT: |
| 197 |
if (chan[c.chan].ins!=c.value || c.value2==1) { |
| 198 |
chan[c.chan].ins=c.value; |
| 199 |
} |
| 200 |
break; |
| 201 |
case DIV_CMD_VOLUME: |
| 202 |
if (chan[c.chan].vol!=c.value) { |
| 203 |
chan[c.chan].vol=c.value; |
| 204 |
if (!chan[c.chan].std.vol.has) { |
| 205 |
calcAndWriteOutVol(c.chan,15); |
| 206 |
} |
| 207 |
} |
| 208 |
break; |
| 209 |
case DIV_CMD_GET_VOLUME: |
| 210 |
return chan[c.chan].vol; |
| 211 |
break; |
| 212 |
case DIV_CMD_PITCH: |
| 213 |
chan[c.chan].pitch=c.value; |
| 214 |
chan[c.chan].freqChanged=true; |
| 215 |
break; |
| 216 |
case DIV_CMD_WAVE: |
| 217 |
chan[c.chan].wave=c.value&0x0f; |
| 218 |
chan[c.chan].keyOn=true; |
| 219 |
break; |
| 220 |
case DIV_CMD_NOTE_PORTA: { |
| 221 |
int destFreq=NOTE_PERIODIC(c.value2); |
| 222 |
bool return2=false; |
| 223 |
if (destFreq>chan[c.chan].baseFreq) { |
| 224 |
chan[c.chan].baseFreq+=c.value; |
| 225 |
if (chan[c.chan].baseFreq>=destFreq) { |
| 226 |
chan[c.chan].baseFreq=destFreq; |
| 227 |
return2=true; |
| 228 |
} |
| 229 |
} else { |
| 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 |
} |
| 236 |
chan[c.chan].freqChanged=true; |
| 237 |
if (return2) { |
| 238 |
chan[c.chan].inPorta=false; |
| 239 |
return 2; |
| 240 |
} |
| 241 |
break; |
| 242 |
} |
| 243 |
case DIV_CMD_LEGATO: |
| 244 |
chan[c.chan].baseFreq=NOTE_PERIODIC(c.value+((HACKY_LEGATO_MESS)?(chan[c.chan].std.arp.val):(0))); |
| 245 |
chan[c.chan].freqChanged=true; |
| 246 |
chan[c.chan].note=c.value; |
| 247 |
break; |
| 248 |
case DIV_CMD_PRE_PORTA: |
| 249 |
if (chan[c.chan].active && c.value2) { |
| 250 |
if (parent->song.resetMacroOnPorta) chan[c.chan].macroInit(parent->getIns(chan[c.chan].ins,DIV_INS_VIC)); |
| 251 |
} |
| 252 |
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); |
| 253 |
chan[c.chan].inPorta=c.value; |
| 254 |
break; |
| 255 |
case DIV_CMD_GET_VOLMAX: |
| 256 |
return 15; |
| 257 |
break; |
| 258 |
case DIV_CMD_MACRO_OFF: |
| 259 |
chan[c.chan].std.mask(c.value,true); |
| 260 |
break; |
| 261 |
case DIV_CMD_MACRO_ON: |
| 262 |
chan[c.chan].std.mask(c.value,false); |
| 263 |
break; |
| 264 |
case DIV_CMD_MACRO_RESTART: |
| 265 |
chan[c.chan].std.restart(c.value); |
| 266 |
break; |
| 267 |
default: |
| 268 |
break; |
| 269 |
} |
| 270 |
return 1; |
| 271 |
} |
| 272 |
|
| 273 |
void DivPlatformVIC20::muteChannel(int ch, bool mute) { |
| 274 |
isMuted[ch]=mute; |
| 275 |
if (chan[ch].onOff) { |
| 276 |
if (mute) { |
| 277 |
chan[ch].keyOff=true; |
| 278 |
} else if (chan[ch].active) { |
| 279 |
chan[ch].keyOn=true; |
| 280 |
} |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
void DivPlatformVIC20::forceIns() { |
| 285 |
for (int i=0; i<4; i++) { |
| 286 |
chan[i].insChanged=true; |
| 287 |
if (chan[i].onOff && chan[i].active) { |
| 288 |
chan[i].freqChanged=true; |
| 289 |
} else { |
| 290 |
chan[i].freqChanged=false; |
| 291 |
chan[i].keyOff=true; |
| 292 |
chan[i].keyOn=false; |
| 293 |
chan[i].waveWriteCycle=-1; |
| 294 |
} |
| 295 |
writeOutVol(i); |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
bool DivPlatformVIC20::isVolGlobal() { |
| 300 |
return true; |
| 301 |
} |
| 302 |
|
| 303 |
void* DivPlatformVIC20::getChanState(int ch) { |
| 304 |
return &chan[ch]; |
| 305 |
} |
| 306 |
|
| 307 |
DivMacroInt* DivPlatformVIC20::getChanMacroInt(int ch) { |
| 308 |
return &chan[ch].std; |
| 309 |
} |
| 310 |
|
| 311 |
DivDispatchOscBuffer* DivPlatformVIC20::getOscBuffer(int ch) { |
| 312 |
return oscBuf[ch]; |
| 313 |
} |
| 314 |
|
| 315 |
unsigned char* DivPlatformVIC20::getRegisterPool() { |
| 316 |
return regPool; |
| 317 |
} |
| 318 |
|
| 319 |
int DivPlatformVIC20::getRegisterPoolSize() { |
| 320 |
return 16; |
| 321 |
} |
| 322 |
|
| 323 |
void DivPlatformVIC20::reset() { |
| 324 |
memset(regPool,0,16); |
| 325 |
for (int i=0; i<4; i++) { |
| 326 |
chan[i]=Channel(); |
| 327 |
chan[i].std.setEngine(parent); |
| 328 |
} |
| 329 |
vic_sound_machine_init(vic,rate,chipClock,filterOff); |
| 330 |
hasWaveWrite=false; |
| 331 |
rWrite(14,15); |
| 332 |
// hack: starting noise channel right away after this would result in a dead |
| 333 |
// channel as the LFSR state is 0, so clock it a bit |
| 334 |
vic_sound_clock(vic,4); |
| 335 |
} |
| 336 |
|
| 337 |
int DivPlatformVIC20::getOutputCount() { |
| 338 |
return 1; |
| 339 |
} |
| 340 |
|
| 341 |
void DivPlatformVIC20::notifyInsDeletion(void* ins) { |
| 342 |
for (int i=0; i<4; i++) { |
| 343 |
chan[i].std.notifyInsDeletion((DivInstrument*)ins); |
| 344 |
} |
| 345 |
} |
| 346 |
|
| 347 |
void DivPlatformVIC20::setFlags(const DivConfig& flags) { |
| 348 |
filterOff=flags.getBool("filterOff",false); |
| 349 |
if (flags.getInt("clockSel",0)) { |
| 350 |
chipClock=COLOR_PAL/4.0; |
| 351 |
} else { |
| 352 |
chipClock=COLOR_NTSC*2.0/7.0; |
| 353 |
} |
| 354 |
CHECK_CUSTOM_CLOCK; |
| 355 |
rate=chipClock/4; |
| 356 |
for (int i=0; i<4; i++) { |
| 357 |
oscBuf[i]->setRate(rate); |
| 358 |
} |
| 359 |
} |
| 360 |
|
| 361 |
void DivPlatformVIC20::poke(unsigned int addr, unsigned short val) { |
| 362 |
rWrite(addr,val); |
| 363 |
} |
| 364 |
|
| 365 |
void DivPlatformVIC20::poke(std::vector<DivRegWrite>& wlist) { |
| 366 |
for (DivRegWrite& i: wlist) rWrite(i.addr,i.val); |
| 367 |
} |
| 368 |
|
| 369 |
int DivPlatformVIC20::init(DivEngine* p, int channels, int sugRate, const DivConfig& flags) { |
| 370 |
parent=p; |
| 371 |
dumpWrites=false; |
| 372 |
skipRegisterWrites=false; |
| 373 |
for (int i=0; i<4; i++) { |
| 374 |
isMuted[i]=false; |
| 375 |
oscBuf[i]=new DivDispatchOscBuffer; |
| 376 |
} |
| 377 |
setFlags(flags); |
| 378 |
vic=new sound_vic20_t(); |
| 379 |
reset(); |
| 380 |
return 4; |
| 381 |
} |
| 382 |
|
| 383 |
void DivPlatformVIC20::quit() { |
| 384 |
for (int i=0; i<4; i++) { |
| 385 |
delete oscBuf[i]; |
| 386 |
} |
| 387 |
delete vic; |
| 388 |
} |
| 389 |
|
| 390 |
DivPlatformVIC20::~DivPlatformVIC20() { |
| 391 |
} |