2022-03-21 14:02:51 +00:00
|
|
|
/**
|
|
|
|
* Furnace Tracker - multi-system chiptune tracker
|
2023-01-20 00:18:40 +00:00
|
|
|
* Copyright (C) 2021-2023 tildearrow and contributors
|
2022-03-21 14:02:51 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "pet.h"
|
|
|
|
#include "../engine.h"
|
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
#define CHIP_DIVIDER 16
|
|
|
|
#define SAMP_DIVIDER 4
|
|
|
|
|
|
|
|
const char* regCheatSheet6522[]={
|
|
|
|
"T2L", "08",
|
2022-06-22 19:42:11 +00:00
|
|
|
"T2H", "09",
|
2022-03-21 14:02:51 +00:00
|
|
|
"SR", "0A",
|
|
|
|
"ACR", "0B",
|
2022-06-22 19:42:11 +00:00
|
|
|
"PCR", "0C",
|
2022-03-21 14:02:51 +00:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
const char** DivPlatformPET::getRegisterSheet() {
|
|
|
|
return regCheatSheet6522;
|
|
|
|
}
|
|
|
|
|
2022-06-22 19:42:11 +00:00
|
|
|
// high-level emulation of 6522 shift register and driver software for now
|
|
|
|
void DivPlatformPET::rWrite(unsigned int addr, unsigned char val) {
|
|
|
|
bool hwSROutput=((regPool[11]>>2)&7)==4;
|
|
|
|
switch (addr) {
|
|
|
|
case 9:
|
|
|
|
// simulate phase reset from switching between hw/sw shift registers
|
|
|
|
if ((regPool[9]==0)^(val==0)) {
|
2022-12-17 05:09:56 +00:00
|
|
|
chan[0].sreg=chan[0].wave;
|
2022-06-22 19:42:11 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 10:
|
2022-12-17 05:09:56 +00:00
|
|
|
chan[0].sreg=val;
|
|
|
|
if (hwSROutput) chan[0].cnt=2;
|
2022-06-22 19:42:11 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
regPool[addr]=val;
|
|
|
|
}
|
|
|
|
|
2023-01-02 09:53:37 +00:00
|
|
|
void DivPlatformPET::acquire(short** buf, size_t len) {
|
2022-06-22 19:42:11 +00:00
|
|
|
bool hwSROutput=((regPool[11]>>2)&7)==4;
|
2022-12-17 05:09:56 +00:00
|
|
|
if (chan[0].enable) {
|
2022-06-22 19:42:11 +00:00
|
|
|
int reload=regPool[8]*2+4;
|
|
|
|
if (!hwSROutput) {
|
|
|
|
reload+=regPool[9]*512;
|
|
|
|
}
|
2023-01-03 06:09:46 +00:00
|
|
|
for (size_t h=0; h<len; h++) {
|
2022-12-17 05:09:56 +00:00
|
|
|
if (SAMP_DIVIDER>chan[0].cnt) {
|
|
|
|
chan[0].out=(chan[0].sreg&1)*32767;
|
|
|
|
chan[0].sreg=(chan[0].sreg>>1)|((chan[0].sreg&1)<<7);
|
|
|
|
chan[0].cnt+=reload-SAMP_DIVIDER;
|
2022-06-22 19:42:11 +00:00
|
|
|
} else {
|
2022-12-17 05:09:56 +00:00
|
|
|
chan[0].cnt-=SAMP_DIVIDER;
|
2022-03-21 14:02:51 +00:00
|
|
|
}
|
2023-01-02 09:53:37 +00:00
|
|
|
buf[0][h]=chan[0].out;
|
2022-12-17 05:09:56 +00:00
|
|
|
oscBuf->data[oscBuf->needle++]=chan[0].out;
|
2022-03-21 14:02:51 +00:00
|
|
|
}
|
2022-06-22 19:42:11 +00:00
|
|
|
// emulate driver writes to PCR
|
2022-12-17 05:09:56 +00:00
|
|
|
if (!hwSROutput) regPool[12]=chan[0].out?0xe0:0xc0;
|
2022-03-21 14:02:51 +00:00
|
|
|
} else {
|
2022-12-17 05:09:56 +00:00
|
|
|
chan[0].out=0;
|
2023-01-03 06:09:46 +00:00
|
|
|
for (size_t h=0; h<len; h++) {
|
2023-01-02 09:53:37 +00:00
|
|
|
buf[0][h]=0;
|
per-channel oscilloscope, part 5
SAA1099 (SAASound and MAME), Lynx, MMC5, N163, PC Engine, PC Speaker,
PET, QSound, WonderSwan, VERA, VIC-20, VRC6 and X1-010!
2022-05-01 07:40:03 +00:00
|
|
|
oscBuf->data[oscBuf->needle++]=0;
|
2022-03-21 14:02:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DivPlatformPET::writeOutVol() {
|
2022-12-17 05:09:56 +00:00
|
|
|
if (chan[0].active && !isMuted && chan[0].outVol>0) {
|
|
|
|
chan[0].enable=true;
|
2022-06-22 19:42:11 +00:00
|
|
|
rWrite(11,regPool[9]==0?16:0);
|
2022-03-21 14:02:51 +00:00
|
|
|
} else {
|
2022-12-17 05:09:56 +00:00
|
|
|
chan[0].enable=false;
|
2022-03-21 14:02:51 +00:00
|
|
|
rWrite(11,0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-15 20:01:11 +00:00
|
|
|
void DivPlatformPET::tick(bool sysTick) {
|
2022-12-17 05:09:56 +00:00
|
|
|
chan[0].std.next();
|
|
|
|
if (chan[0].std.vol.had) {
|
|
|
|
chan[0].outVol=chan[0].std.vol.val&chan[0].vol;
|
2022-03-21 14:02:51 +00:00
|
|
|
writeOutVol();
|
|
|
|
}
|
2022-12-17 06:21:08 +00:00
|
|
|
if (NEW_ARP_STRAT) {
|
|
|
|
chan[0].handleArp();
|
|
|
|
} else if (chan[0].std.arp.had) {
|
2022-12-17 05:09:56 +00:00
|
|
|
if (!chan[0].inPorta) {
|
|
|
|
chan[0].baseFreq=NOTE_PERIODIC(parent->calcArp(chan[0].note,chan[0].std.arp.val));
|
2022-03-21 14:02:51 +00:00
|
|
|
}
|
2022-12-17 05:09:56 +00:00
|
|
|
chan[0].freqChanged=true;
|
2022-03-21 14:02:51 +00:00
|
|
|
}
|
2022-12-17 05:09:56 +00:00
|
|
|
if (chan[0].std.wave.had) {
|
|
|
|
if (chan[0].wave!=chan[0].std.wave.val) {
|
|
|
|
chan[0].wave=chan[0].std.wave.val;
|
|
|
|
rWrite(10,chan[0].wave);
|
2022-03-21 14:02:51 +00:00
|
|
|
}
|
|
|
|
}
|
2022-12-17 05:09:56 +00:00
|
|
|
if (chan[0].std.pitch.had) {
|
|
|
|
if (chan[0].std.pitch.mode) {
|
|
|
|
chan[0].pitch2+=chan[0].std.pitch.val;
|
|
|
|
CLAMP_VAR(chan[0].pitch2,-32768,32767);
|
2022-08-16 02:46:36 +00:00
|
|
|
} else {
|
2022-12-17 05:09:56 +00:00
|
|
|
chan[0].pitch2=chan[0].std.pitch.val;
|
2022-04-16 06:39:40 +00:00
|
|
|
}
|
2022-12-17 05:09:56 +00:00
|
|
|
chan[0].freqChanged=true;
|
2022-08-16 02:46:36 +00:00
|
|
|
}
|
2022-12-17 05:09:56 +00:00
|
|
|
if (chan[0].freqChanged || chan[0].keyOn || chan[0].keyOff) {
|
2022-12-17 07:07:24 +00:00
|
|
|
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;
|
2022-12-17 05:09:56 +00:00
|
|
|
if (chan[0].freq>65535) chan[0].freq=65535;
|
|
|
|
if (chan[0].freq<0) chan[0].freq=0;
|
|
|
|
rWrite(8,chan[0].freq&0xff);
|
|
|
|
rWrite(9,chan[0].freq>>8);
|
|
|
|
if (chan[0].keyOn) {
|
|
|
|
if (!chan[0].std.vol.will) {
|
|
|
|
chan[0].outVol=chan[0].vol;
|
2022-03-21 14:02:51 +00:00
|
|
|
}
|
2022-12-17 05:09:56 +00:00
|
|
|
chan[0].keyOn=false;
|
2022-03-21 14:02:51 +00:00
|
|
|
}
|
2022-12-17 05:09:56 +00:00
|
|
|
if (chan[0].keyOff) {
|
|
|
|
chan[0].keyOff=false;
|
2022-03-21 14:02:51 +00:00
|
|
|
}
|
2022-06-22 19:42:11 +00:00
|
|
|
// update mode setting and channel enable
|
|
|
|
writeOutVol();
|
2022-12-17 05:09:56 +00:00
|
|
|
chan[0].freqChanged=false;
|
2022-03-21 14:02:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int DivPlatformPET::dispatch(DivCommand c) {
|
|
|
|
switch (c.cmd) {
|
|
|
|
case DIV_CMD_NOTE_ON: {
|
2022-12-17 05:09:56 +00:00
|
|
|
DivInstrument* ins=parent->getIns(chan[0].ins,DIV_INS_PET);
|
2022-03-21 14:02:51 +00:00
|
|
|
if (c.value!=DIV_NOTE_NULL) {
|
2022-12-17 05:09:56 +00:00
|
|
|
chan[0].baseFreq=NOTE_PERIODIC(c.value);
|
|
|
|
chan[0].freqChanged=true;
|
|
|
|
chan[0].note=c.value;
|
2022-03-21 14:02:51 +00:00
|
|
|
}
|
2022-12-17 05:09:56 +00:00
|
|
|
chan[0].active=true;
|
|
|
|
chan[0].keyOn=true;
|
|
|
|
chan[0].macroInit(ins);
|
|
|
|
if (!parent->song.brokenOutVol && !chan[0].std.vol.will) {
|
|
|
|
chan[0].outVol=chan[0].vol;
|
2022-06-01 00:03:31 +00:00
|
|
|
}
|
2022-03-21 14:02:51 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case DIV_CMD_NOTE_OFF:
|
2022-12-17 05:09:56 +00:00
|
|
|
chan[0].active=false;
|
|
|
|
chan[0].keyOff=true;
|
|
|
|
chan[0].macroInit(NULL);
|
2022-03-21 14:02:51 +00:00
|
|
|
break;
|
|
|
|
case DIV_CMD_NOTE_OFF_ENV:
|
|
|
|
case DIV_CMD_ENV_RELEASE:
|
2022-12-17 05:09:56 +00:00
|
|
|
chan[0].std.release();
|
2022-03-21 14:02:51 +00:00
|
|
|
break;
|
|
|
|
case DIV_CMD_INSTRUMENT:
|
2022-12-17 05:09:56 +00:00
|
|
|
if (chan[0].ins!=c.value || c.value2==1) {
|
|
|
|
chan[0].ins=c.value;
|
2022-03-21 14:02:51 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case DIV_CMD_VOLUME:
|
2022-12-17 05:09:56 +00:00
|
|
|
if (chan[0].vol!=c.value) {
|
|
|
|
chan[0].vol=c.value;
|
|
|
|
if (!chan[0].std.vol.had) {
|
|
|
|
chan[0].outVol=chan[0].vol;
|
2022-03-21 14:02:51 +00:00
|
|
|
writeOutVol();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case DIV_CMD_GET_VOLUME:
|
2022-12-17 05:09:56 +00:00
|
|
|
return chan[0].vol;
|
2022-03-21 14:02:51 +00:00
|
|
|
break;
|
|
|
|
case DIV_CMD_PITCH:
|
2022-12-17 05:09:56 +00:00
|
|
|
chan[0].pitch=c.value;
|
|
|
|
chan[0].freqChanged=true;
|
2022-03-21 14:02:51 +00:00
|
|
|
break;
|
|
|
|
case DIV_CMD_WAVE:
|
2022-12-17 05:09:56 +00:00
|
|
|
chan[0].wave=c.value;
|
|
|
|
rWrite(10,chan[0].wave);
|
2022-03-21 14:02:51 +00:00
|
|
|
break;
|
|
|
|
case DIV_CMD_NOTE_PORTA: {
|
|
|
|
int destFreq=NOTE_PERIODIC(c.value2);
|
|
|
|
bool return2=false;
|
2022-12-17 05:09:56 +00:00
|
|
|
if (destFreq>chan[0].baseFreq) {
|
|
|
|
chan[0].baseFreq+=c.value;
|
|
|
|
if (chan[0].baseFreq>=destFreq) {
|
|
|
|
chan[0].baseFreq=destFreq;
|
2022-03-21 14:02:51 +00:00
|
|
|
return2=true;
|
|
|
|
}
|
|
|
|
} else {
|
2022-12-17 05:09:56 +00:00
|
|
|
chan[0].baseFreq-=c.value;
|
|
|
|
if (chan[0].baseFreq<=destFreq) {
|
|
|
|
chan[0].baseFreq=destFreq;
|
2022-03-21 14:02:51 +00:00
|
|
|
return2=true;
|
|
|
|
}
|
|
|
|
}
|
2022-12-17 05:09:56 +00:00
|
|
|
chan[0].freqChanged=true;
|
2022-03-21 14:02:51 +00:00
|
|
|
if (return2) {
|
2022-12-17 05:09:56 +00:00
|
|
|
chan[0].inPorta=false;
|
2022-03-21 14:02:51 +00:00
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case DIV_CMD_LEGATO:
|
2022-12-17 07:07:24 +00:00
|
|
|
chan[0].baseFreq=NOTE_PERIODIC(c.value+((HACKY_LEGATO_MESS)?(chan[0].std.arp.val):(0)));
|
2022-12-17 05:09:56 +00:00
|
|
|
chan[0].freqChanged=true;
|
|
|
|
chan[0].note=c.value;
|
2022-03-21 14:02:51 +00:00
|
|
|
break;
|
|
|
|
case DIV_CMD_PRE_PORTA:
|
2022-12-17 05:09:56 +00:00
|
|
|
if (chan[0].active && c.value2) {
|
|
|
|
if (parent->song.resetMacroOnPorta) chan[0].macroInit(parent->getIns(chan[0].ins,DIV_INS_PET));
|
2022-03-21 14:02:51 +00:00
|
|
|
}
|
2022-12-17 07:10:20 +00:00
|
|
|
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);
|
2022-12-17 05:09:56 +00:00
|
|
|
chan[0].inPorta=c.value;
|
2022-03-21 14:02:51 +00:00
|
|
|
break;
|
|
|
|
case DIV_CMD_GET_VOLMAX:
|
|
|
|
return 1;
|
|
|
|
break;
|
2022-12-17 05:09:56 +00:00
|
|
|
case DIV_CMD_MACRO_OFF:
|
|
|
|
chan[c.chan].std.mask(c.value,true);
|
|
|
|
break;
|
|
|
|
case DIV_CMD_MACRO_ON:
|
|
|
|
chan[c.chan].std.mask(c.value,false);
|
|
|
|
break;
|
2022-03-21 14:02:51 +00:00
|
|
|
case DIV_ALWAYS_SET_VOLUME:
|
|
|
|
return 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DivPlatformPET::muteChannel(int ch, bool mute) {
|
|
|
|
isMuted=mute;
|
|
|
|
writeOutVol();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DivPlatformPET::forceIns() {
|
2022-12-17 05:09:56 +00:00
|
|
|
chan[0].insChanged=true;
|
|
|
|
chan[0].freqChanged=true;
|
2022-03-21 14:02:51 +00:00
|
|
|
writeOutVol();
|
|
|
|
}
|
|
|
|
|
|
|
|
void* DivPlatformPET::getChanState(int ch) {
|
|
|
|
return &chan;
|
|
|
|
}
|
|
|
|
|
2022-06-05 23:17:00 +00:00
|
|
|
DivMacroInt* DivPlatformPET::getChanMacroInt(int ch) {
|
2022-12-17 05:09:56 +00:00
|
|
|
return &chan[0].std;
|
2022-06-05 23:17:00 +00:00
|
|
|
}
|
|
|
|
|
per-channel oscilloscope, part 5
SAA1099 (SAASound and MAME), Lynx, MMC5, N163, PC Engine, PC Speaker,
PET, QSound, WonderSwan, VERA, VIC-20, VRC6 and X1-010!
2022-05-01 07:40:03 +00:00
|
|
|
DivDispatchOscBuffer* DivPlatformPET::getOscBuffer(int ch) {
|
|
|
|
return oscBuf;
|
|
|
|
}
|
|
|
|
|
2022-03-21 14:02:51 +00:00
|
|
|
unsigned char* DivPlatformPET::getRegisterPool() {
|
|
|
|
return regPool;
|
|
|
|
}
|
|
|
|
|
|
|
|
int DivPlatformPET::getRegisterPoolSize() {
|
|
|
|
return 16;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DivPlatformPET::reset() {
|
|
|
|
memset(regPool,0,16);
|
2022-12-17 05:09:56 +00:00
|
|
|
chan[0]=Channel();
|
|
|
|
chan[0].std.setEngine(parent);
|
2022-03-21 14:02:51 +00:00
|
|
|
}
|
|
|
|
|
2023-01-02 00:46:08 +00:00
|
|
|
int DivPlatformPET::getOutputCount() {
|
|
|
|
return 1;
|
2022-03-21 14:02:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DivPlatformPET::notifyInsDeletion(void* ins) {
|
2022-12-17 05:09:56 +00:00
|
|
|
chan[0].std.notifyInsDeletion((DivInstrument*)ins);
|
2022-03-21 14:02:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DivPlatformPET::poke(unsigned int addr, unsigned short val) {
|
|
|
|
rWrite(addr,val);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DivPlatformPET::poke(std::vector<DivRegWrite>& wlist) {
|
|
|
|
for (DivRegWrite& i: wlist) rWrite(i.addr,i.val);
|
|
|
|
}
|
|
|
|
|
2022-09-30 01:13:40 +00:00
|
|
|
int DivPlatformPET::init(DivEngine* p, int channels, int sugRate, const DivConfig& flags) {
|
2022-03-21 14:02:51 +00:00
|
|
|
parent=p;
|
|
|
|
dumpWrites=false;
|
|
|
|
skipRegisterWrites=false;
|
|
|
|
chipClock=1000000;
|
2022-12-04 07:04:42 +00:00
|
|
|
CHECK_CUSTOM_CLOCK;
|
2022-03-21 14:02:51 +00:00
|
|
|
rate=chipClock/SAMP_DIVIDER; // = 250000kHz
|
|
|
|
isMuted=false;
|
per-channel oscilloscope, part 5
SAA1099 (SAASound and MAME), Lynx, MMC5, N163, PC Engine, PC Speaker,
PET, QSound, WonderSwan, VERA, VIC-20, VRC6 and X1-010!
2022-05-01 07:40:03 +00:00
|
|
|
oscBuf=new DivDispatchOscBuffer;
|
|
|
|
oscBuf->rate=rate;
|
2022-03-21 14:02:51 +00:00
|
|
|
reset();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
per-channel oscilloscope, part 5
SAA1099 (SAASound and MAME), Lynx, MMC5, N163, PC Engine, PC Speaker,
PET, QSound, WonderSwan, VERA, VIC-20, VRC6 and X1-010!
2022-05-01 07:40:03 +00:00
|
|
|
void DivPlatformPET::quit() {
|
|
|
|
delete oscBuf;
|
|
|
|
}
|
|
|
|
|
2022-03-21 14:02:51 +00:00
|
|
|
DivPlatformPET::~DivPlatformPET() {
|
|
|
|
}
|