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