| 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 "pet.h" |
| 21 |
#include "../engine.h" |
| 22 |
#include <math.h> |
| 23 |
|
| 24 |
#define CHIP_DIVIDER 16 |
| 25 |
#define SAMP_DIVIDER 4 |
| 26 |
|
| 27 |
const char* regCheatSheet6522[]={ |
| 28 |
"T2L", "08", |
| 29 |
"T2H", "09", |
| 30 |
"SR", "0A", |
| 31 |
"ACR", "0B", |
| 32 |
"PCR", "0C", |
| 33 |
NULL |
| 34 |
}; |
| 35 |
|
| 36 |
const char** DivPlatformPET::getRegisterSheet() { |
| 37 |
return regCheatSheet6522; |
| 38 |
} |
| 39 |
|
| 40 |
// high-level emulation of 6522 shift register and driver software for now |
| 41 |
void DivPlatformPET::rWrite(unsigned int addr, unsigned char val) { |
| 42 |
bool hwSROutput=((regPool[11]>>2)&7)==4; |
| 43 |
switch (addr) { |
| 44 |
case 9: |
| 45 |
// simulate phase reset from switching between hw/sw shift registers |
| 46 |
if ((regPool[9]==0)^(val==0)) { |
| 47 |
chan[0].sreg=chan[0].wave; |
| 48 |
} |
| 49 |
break; |
| 50 |
case 10: |
| 51 |
chan[0].sreg=val; |
| 52 |
if (hwSROutput) chan[0].cnt=2; |
| 53 |
break; |
| 54 |
} |
| 55 |
regPool[addr]=val; |
| 56 |
} |
| 57 |
|
| 58 |
void DivPlatformPET::acquire(short** buf, size_t len) { |
| 59 |
bool hwSROutput=((regPool[11]>>2)&7)==4; |
| 60 |
oscBuf->begin(len); |
| 61 |
if (chan[0].enable) { |
| 62 |
int reload=regPool[8]*2+4; |
| 63 |
if (!hwSROutput) { |
| 64 |
reload+=regPool[9]*512; |
| 65 |
} |
| 66 |
for (size_t h=0; h<len; h++) { |
| 67 |
if (SAMP_DIVIDER>chan[0].cnt) { |
| 68 |
chan[0].out=(chan[0].sreg&1)*32767; |
| 69 |
chan[0].sreg=(chan[0].sreg>>1)|((chan[0].sreg&1)<<7); |
| 70 |
chan[0].cnt+=reload-SAMP_DIVIDER; |
| 71 |
} else { |
| 72 |
chan[0].cnt-=SAMP_DIVIDER; |
| 73 |
} |
| 74 |
buf[0][h]=chan[0].out; |
| 75 |
oscBuf->putSample(h,chan[0].out); |
| 76 |
} |
| 77 |
// emulate driver writes to PCR |
| 78 |
if (!hwSROutput) regPool[12]=chan[0].out?0xe0:0xc0; |
| 79 |
} else { |
| 80 |
chan[0].out=0; |
| 81 |
for (size_t h=0; h<len; h++) { |
| 82 |
buf[0][h]=0; |
| 83 |
oscBuf->putSample(h,0); |
| 84 |
} |
| 85 |
} |
| 86 |
oscBuf->end(len); |
| 87 |
} |
| 88 |
|
| 89 |
void DivPlatformPET::writeOutVol() { |
| 90 |
if (chan[0].active && !isMuted && chan[0].outVol>0) { |
| 91 |
chan[0].enable=true; |
| 92 |
rWrite(11,regPool[9]==0?16:0); |
| 93 |
} else { |
| 94 |
chan[0].enable=false; |
| 95 |
rWrite(11,0); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
void DivPlatformPET::tick(bool sysTick) { |
| 100 |
chan[0].std.next(); |
| 101 |
if (chan[0].std.vol.had) { |
| 102 |
chan[0].outVol=chan[0].std.vol.val&chan[0].vol; |
| 103 |
writeOutVol(); |
| 104 |
} |
| 105 |
if (NEW_ARP_STRAT) { |
| 106 |
chan[0].handleArp(); |
| 107 |
} else if (chan[0].std.arp.had) { |
| 108 |
if (!chan[0].inPorta) { |
| 109 |
chan[0].baseFreq=NOTE_PERIODIC(parent->calcArp(chan[0].note,chan[0].std.arp.val)); |
| 110 |
} |
| 111 |
chan[0].freqChanged=true; |
| 112 |
} |
| 113 |
if (chan[0].std.wave.had) { |
| 114 |
if (chan[0].wave!=chan[0].std.wave.val) { |
| 115 |
chan[0].wave=chan[0].std.wave.val; |
| 116 |
rWrite(10,chan[0].wave); |
| 117 |
} |
| 118 |
} |
| 119 |
if (chan[0].std.pitch.had) { |
| 120 |
if (chan[0].std.pitch.mode) { |
| 121 |
chan[0].pitch2+=chan[0].std.pitch.val; |
| 122 |
CLAMP_VAR(chan[0].pitch2,-32768,32767); |
| 123 |
} else { |
| 124 |
chan[0].pitch2=chan[0].std.pitch.val; |
| 125 |
} |
| 126 |
chan[0].freqChanged=true; |
| 127 |
} |
| 128 |
if (chan[0].freqChanged || chan[0].keyOn || chan[0].keyOff) { |
| 129 |
chan[0].freq=parent->calcFreq(chan[0].baseFreq,chan[0].pitch,chan[0].fixedArp?chan[0].baseNoteOverride:chan[0].arpOff,chan[0].fixedArp,true,0,chan[0].pitch2,chipClock,CHIP_DIVIDER)-2; |
| 130 |
if (chan[0].freq>65535) chan[0].freq=65535; |
| 131 |
if (chan[0].freq<0) chan[0].freq=0; |
| 132 |
rWrite(8,chan[0].freq&0xff); |
| 133 |
rWrite(9,chan[0].freq>>8); |
| 134 |
if (chan[0].keyOn) { |
| 135 |
if (!chan[0].std.vol.will) { |
| 136 |
chan[0].outVol=chan[0].vol; |
| 137 |
} |
| 138 |
chan[0].keyOn=false; |
| 139 |
} |
| 140 |
if (chan[0].keyOff) { |
| 141 |
chan[0].keyOff=false; |
| 142 |
} |
| 143 |
// update mode setting and channel enable |
| 144 |
writeOutVol(); |
| 145 |
chan[0].freqChanged=false; |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
int DivPlatformPET::dispatch(DivCommand c) { |
| 150 |
switch (c.cmd) { |
| 151 |
case DIV_CMD_NOTE_ON: { |
| 152 |
DivInstrument* ins=parent->getIns(chan[0].ins,DIV_INS_PET); |
| 153 |
if (c.value!=DIV_NOTE_NULL) { |
| 154 |
chan[0].baseFreq=NOTE_PERIODIC(c.value); |
| 155 |
chan[0].freqChanged=true; |
| 156 |
chan[0].note=c.value; |
| 157 |
} |
| 158 |
chan[0].active=true; |
| 159 |
chan[0].keyOn=true; |
| 160 |
chan[0].macroInit(ins); |
| 161 |
if (!parent->song.brokenOutVol && !chan[0].std.vol.will) { |
| 162 |
chan[0].outVol=chan[0].vol; |
| 163 |
} |
| 164 |
break; |
| 165 |
} |
| 166 |
case DIV_CMD_NOTE_OFF: |
| 167 |
chan[0].active=false; |
| 168 |
chan[0].keyOff=true; |
| 169 |
chan[0].macroInit(NULL); |
| 170 |
break; |
| 171 |
case DIV_CMD_NOTE_OFF_ENV: |
| 172 |
case DIV_CMD_ENV_RELEASE: |
| 173 |
chan[0].std.release(); |
| 174 |
break; |
| 175 |
case DIV_CMD_INSTRUMENT: |
| 176 |
if (chan[0].ins!=c.value || c.value2==1) { |
| 177 |
chan[0].ins=c.value; |
| 178 |
} |
| 179 |
break; |
| 180 |
case DIV_CMD_VOLUME: |
| 181 |
if (chan[0].vol!=c.value) { |
| 182 |
chan[0].vol=c.value; |
| 183 |
if (!chan[0].std.vol.had) { |
| 184 |
chan[0].outVol=chan[0].vol; |
| 185 |
writeOutVol(); |
| 186 |
} |
| 187 |
} |
| 188 |
break; |
| 189 |
case DIV_CMD_GET_VOLUME: |
| 190 |
return chan[0].vol; |
| 191 |
break; |
| 192 |
case DIV_CMD_PITCH: |
| 193 |
chan[0].pitch=c.value; |
| 194 |
chan[0].freqChanged=true; |
| 195 |
break; |
| 196 |
case DIV_CMD_WAVE: |
| 197 |
chan[0].wave=c.value; |
| 198 |
rWrite(10,chan[0].wave); |
| 199 |
break; |
| 200 |
case DIV_CMD_NOTE_PORTA: { |
| 201 |
int destFreq=NOTE_PERIODIC(c.value2); |
| 202 |
bool return2=false; |
| 203 |
if (destFreq>chan[0].baseFreq) { |
| 204 |
chan[0].baseFreq+=c.value; |
| 205 |
if (chan[0].baseFreq>=destFreq) { |
| 206 |
chan[0].baseFreq=destFreq; |
| 207 |
return2=true; |
| 208 |
} |
| 209 |
} else { |
| 210 |
chan[0].baseFreq-=c.value; |
| 211 |
if (chan[0].baseFreq<=destFreq) { |
| 212 |
chan[0].baseFreq=destFreq; |
| 213 |
return2=true; |
| 214 |
} |
| 215 |
} |
| 216 |
chan[0].freqChanged=true; |
| 217 |
if (return2) { |
| 218 |
chan[0].inPorta=false; |
| 219 |
return 2; |
| 220 |
} |
| 221 |
break; |
| 222 |
} |
| 223 |
case DIV_CMD_LEGATO: |
| 224 |
chan[0].baseFreq=NOTE_PERIODIC(c.value+((HACKY_LEGATO_MESS)?(chan[0].std.arp.val):(0))); |
| 225 |
chan[0].freqChanged=true; |
| 226 |
chan[0].note=c.value; |
| 227 |
break; |
| 228 |
case DIV_CMD_PRE_PORTA: |
| 229 |
if (chan[0].active && c.value2) { |
| 230 |
if (parent->song.resetMacroOnPorta) chan[0].macroInit(parent->getIns(chan[0].ins,DIV_INS_PET)); |
| 231 |
} |
| 232 |
if (!chan[0].inPorta && c.value && !parent->song.brokenPortaArp && chan[0].std.arp.will && !NEW_ARP_STRAT) chan[0].baseFreq=NOTE_PERIODIC(chan[0].note); |
| 233 |
chan[0].inPorta=c.value; |
| 234 |
break; |
| 235 |
case DIV_CMD_GET_VOLMAX: |
| 236 |
return 1; |
| 237 |
break; |
| 238 |
case DIV_CMD_MACRO_OFF: |
| 239 |
chan[c.chan].std.mask(c.value,true); |
| 240 |
break; |
| 241 |
case DIV_CMD_MACRO_ON: |
| 242 |
chan[c.chan].std.mask(c.value,false); |
| 243 |
break; |
| 244 |
case DIV_CMD_MACRO_RESTART: |
| 245 |
chan[c.chan].std.restart(c.value); |
| 246 |
break; |
| 247 |
default: |
| 248 |
break; |
| 249 |
} |
| 250 |
return 1; |
| 251 |
} |
| 252 |
|
| 253 |
void DivPlatformPET::muteChannel(int ch, bool mute) { |
| 254 |
isMuted=mute; |
| 255 |
writeOutVol(); |
| 256 |
} |
| 257 |
|
| 258 |
void DivPlatformPET::forceIns() { |
| 259 |
chan[0].insChanged=true; |
| 260 |
chan[0].freqChanged=true; |
| 261 |
writeOutVol(); |
| 262 |
} |
| 263 |
|
| 264 |
void* DivPlatformPET::getChanState(int ch) { |
| 265 |
return &chan; |
| 266 |
} |
| 267 |
|
| 268 |
DivMacroInt* DivPlatformPET::getChanMacroInt(int ch) { |
| 269 |
return &chan[0].std; |
| 270 |
} |
| 271 |
|
| 272 |
DivDispatchOscBuffer* DivPlatformPET::getOscBuffer(int ch) { |
| 273 |
return oscBuf; |
| 274 |
} |
| 275 |
|
| 276 |
unsigned char* DivPlatformPET::getRegisterPool() { |
| 277 |
return regPool; |
| 278 |
} |
| 279 |
|
| 280 |
int DivPlatformPET::getRegisterPoolSize() { |
| 281 |
return 16; |
| 282 |
} |
| 283 |
|
| 284 |
void DivPlatformPET::reset() { |
| 285 |
memset(regPool,0,16); |
| 286 |
chan[0]=Channel(); |
| 287 |
chan[0].std.setEngine(parent); |
| 288 |
rWrite(10,chan[0].wave); |
| 289 |
} |
| 290 |
|
| 291 |
int DivPlatformPET::getOutputCount() { |
| 292 |
return 1; |
| 293 |
} |
| 294 |
|
| 295 |
void DivPlatformPET::notifyInsDeletion(void* ins) { |
| 296 |
chan[0].std.notifyInsDeletion((DivInstrument*)ins); |
| 297 |
} |
| 298 |
|
| 299 |
void DivPlatformPET::poke(unsigned int addr, unsigned short val) { |
| 300 |
rWrite(addr,val); |
| 301 |
} |
| 302 |
|
| 303 |
void DivPlatformPET::poke(std::vector<DivRegWrite>& wlist) { |
| 304 |
for (DivRegWrite& i: wlist) rWrite(i.addr,i.val); |
| 305 |
} |
| 306 |
|
| 307 |
int DivPlatformPET::init(DivEngine* p, int channels, int sugRate, const DivConfig& flags) { |
| 308 |
parent=p; |
| 309 |
dumpWrites=false; |
| 310 |
skipRegisterWrites=false; |
| 311 |
chipClock=1000000; |
| 312 |
CHECK_CUSTOM_CLOCK; |
| 313 |
rate=chipClock/SAMP_DIVIDER; // = 250000kHz |
| 314 |
isMuted=false; |
| 315 |
oscBuf=new DivDispatchOscBuffer; |
| 316 |
oscBuf->setRate(rate); |
| 317 |
reset(); |
| 318 |
return 1; |
| 319 |
} |
| 320 |
|
| 321 |
void DivPlatformPET::quit() { |
| 322 |
delete oscBuf; |
| 323 |
} |
| 324 |
|
| 325 |
DivPlatformPET::~DivPlatformPET() { |
| 326 |
} |