furnace/src/engine/platform/ay8930.cpp

583 lines
15 KiB
C++
Raw Normal View History

2022-02-15 03:12:20 +00:00
/**
* Furnace Tracker - multi-system chiptune tracker
* Copyright (C) 2021-2022 tildearrow and contributors
*
* 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.
*/
2022-01-14 05:02:10 +00:00
#include "ay8930.h"
#include "../engine.h"
#include "sound/ay8910.h"
#include <string.h>
#include <math.h>
#define rWrite(a,v) if (!skipRegisterWrites) {pendingWrites[a]=v;}
2022-01-17 18:29:35 +00:00
#define immWrite(a,v) if (!skipRegisterWrites) {writes.emplace(a,v); if (dumpWrites) {addWrite(a,v);} }
2022-01-14 05:02:10 +00:00
#define CHIP_DIVIDER 8
2022-01-14 05:02:10 +00:00
2022-02-02 07:14:42 +00:00
const char* regCheatSheetAY8930[]={
"FreqL_A", "00",
"FreqH_A", "01",
"FreqL_B", "02",
"FreqH_B", "03",
"FreqL_C", "04",
"FreqH_C", "05",
"FreqNoise", "06",
"Enable", "07",
"Volume_A", "08",
"Volume_B", "09",
"Volume_C", "0A",
"FreqL_EnvA", "0B",
"FreqH_EnvA", "0C",
"Control_EnvA", "0D",
"PortA", "0E",
"PortB", "0F",
"FreqL_EnvB", "10",
"FreqH_EnvB", "11",
"FreqL_EnvC", "12",
"FreqH_EnvC", "13",
"Control_EnvB", "14",
"Control_EnvC", "15",
"Duty_A", "16",
"Duty_B", "17",
"Duty_C", "18",
"NoiseAND", "19",
"NoiseOR", "1A",
"TEST", "1F",
NULL
};
2022-02-03 23:38:57 +00:00
const char** DivPlatformAY8930::getRegisterSheet() {
return regCheatSheetAY8930;
}
2022-02-15 06:46:03 +00:00
const char* DivPlatformAY8930::getEffectName(unsigned char effect) {
switch (effect) {
case 0x12:
return "12xx: Set duty cycle (0 to 8)";
break;
case 0x20:
return "20xx: Set channel mode (bit 0: square; bit 1: noise; bit 2: envelope)";
break;
case 0x21:
return "21xx: Set noise frequency (0 to 1F)";
break;
case 0x22:
return "22xy: Set envelope mode (x: shape, y: enable for this channel)";
break;
case 0x23:
return "23xx: Set envelope period low byte";
break;
case 0x24:
return "24xx: Set envelope period high byte";
break;
case 0x25:
return "25xx: Envelope slide up";
break;
case 0x26:
return "26xx: Envelope slide down";
break;
case 0x27:
return "27xx: Set noise AND mask";
break;
case 0x28:
return "28xx: Set noise OR mask";
break;
case 0x29:
return "29xy: Set auto-envelope (x: numerator; y: denominator)";
break;
}
return NULL;
}
2022-01-14 05:02:10 +00:00
void DivPlatformAY8930::acquire(short* bufL, short* bufR, size_t start, size_t len) {
if (ayBufLen<len) {
ayBufLen=len;
for (int i=0; i<3; i++) {
delete[] ayBuf[i];
ayBuf[i]=new short[ayBufLen];
}
}
while (!writes.empty()) {
QueuedWrite w=writes.front();
if (bank!=(w.addr>>4)) {
bank=w.addr>>4;
ay->address_w(0x0d);
ay->data_w(0xa0|(bank<<4)|ayEnvMode[0]);
2022-01-14 05:02:10 +00:00
}
ay->address_w(w.addr&15);
if (w.addr==0x0d) {
ay->data_w(0xa0|(bank<<4)|(w.val&15));
} else {
ay->data_w(w.val);
}
regPool[w.addr&0x1f]=w.val;
2022-01-14 05:02:10 +00:00
writes.pop();
}
ay->sound_stream_update(ayBuf,len);
2022-01-29 05:20:27 +00:00
if (stereo) {
for (size_t i=0; i<len; i++) {
bufL[i+start]=ayBuf[0][i]+ayBuf[1][i];
bufR[i+start]=ayBuf[1][i]+ayBuf[2][i];
}
} else {
for (size_t i=0; i<len; i++) {
bufL[i+start]=ayBuf[0][i]+ayBuf[1][i]+ayBuf[2][i];
bufR[i+start]=bufL[i+start];
}
2022-01-14 05:02:10 +00:00
}
}
const unsigned char regPeriodL[3]={
0x0b, 0x10, 0x12
};
const unsigned char regPeriodH[3]={
0x0c, 0x11, 0x13
};
const unsigned char regMode[3]={
0x0d, 0x14, 0x15
};
void DivPlatformAY8930::tick() {
// PSG
for (int i=0; i<3; i++) {
chan[i].std.next();
if (chan[i].std.hadVol) {
chan[i].outVol=MIN(31,chan[i].std.vol)-(31-(chan[i].vol&31));
2022-01-14 05:02:10 +00:00
if (chan[i].outVol<0) chan[i].outVol=0;
if (isMuted[i]) {
rWrite(0x08+i,0);
} else {
rWrite(0x08+i,(chan[i].outVol&31)|((chan[i].psgMode&4)<<3));
}
}
if (chan[i].std.hadArp) {
if (!chan[i].inPorta) {
if (chan[i].std.arpMode) {
chan[i].baseFreq=NOTE_PERIODIC(chan[i].std.arp);
2022-01-14 05:02:10 +00:00
} else {
chan[i].baseFreq=NOTE_PERIODIC(chan[i].note+chan[i].std.arp);
2022-01-14 05:02:10 +00:00
}
}
chan[i].freqChanged=true;
} else {
if (chan[i].std.arpMode && chan[i].std.finishedArp) {
chan[i].baseFreq=NOTE_PERIODIC(chan[i].note);
2022-01-14 05:02:10 +00:00
chan[i].freqChanged=true;
}
}
if (chan[i].std.hadDuty) {
rWrite(0x06,chan[i].std.duty);
}
if (chan[i].std.hadWave) {
2022-01-18 20:48:40 +00:00
chan[i].psgMode=(chan[i].std.wave+1)&7;
if (isMuted[i]) {
rWrite(0x08+i,0);
} else {
rWrite(0x08+i,(chan[i].outVol&31)|((chan[i].psgMode&4)<<3));
}
2022-01-14 05:02:10 +00:00
}
if (chan[i].std.hadEx1) { // duty
rWrite(0x16+i,chan[i].std.ex1);
}
if (chan[i].std.hadEx2) {
ayEnvMode[i]=chan[i].std.ex2;
rWrite(regMode[i],ayEnvMode[i]);
}
if (chan[i].std.hadEx3) {
chan[i].autoEnvNum=chan[i].std.ex3;
chan[i].freqChanged=true;
if (!chan[i].std.willAlg) chan[i].autoEnvDen=1;
}
if (chan[i].std.hadAlg) {
chan[i].autoEnvDen=chan[i].std.alg;
chan[i].freqChanged=true;
if (!chan[i].std.willEx3) chan[i].autoEnvNum=1;
}
if (chan[i].std.hadFb) {
ayNoiseAnd=chan[i].std.fb;
immWrite(0x19,ayNoiseAnd);
}
if (chan[i].std.hadFms) {
ayNoiseOr=chan[i].std.fms;
immWrite(0x1a,ayNoiseOr);
}
2022-01-14 05:02:10 +00:00
if (chan[i].freqChanged || chan[i].keyOn || chan[i].keyOff) {
chan[i].freq=parent->calcFreq(chan[i].baseFreq,chan[i].pitch,true);
2022-02-03 07:46:42 +00:00
if (chan[i].freq>65535) chan[i].freq=65535;
2022-01-14 05:02:10 +00:00
if (chan[i].keyOn) {
if (chan[i].insChanged) {
if (!chan[i].std.willEx1) immWrite(0x16+i,chan[i].duty);
chan[i].insChanged=false;
}
}
if (chan[i].keyOff) {
rWrite(0x08+i,0);
}
rWrite((i)<<1,chan[i].freq&0xff);
rWrite(1+((i)<<1),chan[i].freq>>8);
if (chan[i].keyOn) chan[i].keyOn=false;
if (chan[i].keyOff) chan[i].keyOff=false;
if (chan[i].freqChanged && chan[i].autoEnvNum>0 && chan[i].autoEnvDen>0) {
ayEnvPeriod[i]=(chan[i].freq*chan[i].autoEnvDen/chan[i].autoEnvNum)>>4;
immWrite(regPeriodL[i],ayEnvPeriod[i]);
immWrite(regPeriodH[i],ayEnvPeriod[i]>>8);
}
2022-01-14 05:02:10 +00:00
chan[i].freqChanged=false;
}
if (ayEnvSlide[i]!=0) {
ayEnvSlideLow[i]+=ayEnvSlide[i];
while (ayEnvSlideLow[i]>7) {
ayEnvSlideLow[i]-=8;
if (ayEnvPeriod[i]<0xffff) {
ayEnvPeriod[i]++;
immWrite(regPeriodL[i],ayEnvPeriod[i]);
immWrite(regPeriodH[i],ayEnvPeriod[i]>>8);
}
}
while (ayEnvSlideLow[i]<-7) {
ayEnvSlideLow[i]+=8;
if (ayEnvPeriod[i]>0) {
ayEnvPeriod[i]--;
immWrite(regPeriodL[i],ayEnvPeriod[i]);
immWrite(regPeriodH[i],ayEnvPeriod[i]>>8);
}
}
}
}
rWrite(0x07,
~((chan[0].psgMode&1)|
((chan[1].psgMode&1)<<1)|
((chan[2].psgMode&1)<<2)|
((chan[0].psgMode&2)<<2)|
((chan[1].psgMode&2)<<3)|
((chan[2].psgMode&2)<<4)));
for (int i=0; i<32; i++) {
if (pendingWrites[i]!=oldWrites[i]) {
immWrite(i,pendingWrites[i]&0xff);
oldWrites[i]=pendingWrites[i];
}
}
}
int DivPlatformAY8930::dispatch(DivCommand c) {
switch (c.cmd) {
case DIV_CMD_NOTE_ON: {
DivInstrument* ins=parent->getIns(chan[c.chan].ins);
if (c.value!=DIV_NOTE_NULL) {
chan[c.chan].baseFreq=NOTE_PERIODIC(c.value);
chan[c.chan].freqChanged=true;
chan[c.chan].note=c.value;
}
2022-01-14 05:02:10 +00:00
chan[c.chan].active=true;
chan[c.chan].keyOn=true;
chan[c.chan].std.init(ins);
if (isMuted[c.chan]) {
rWrite(0x08+c.chan,0);
} else {
rWrite(0x08+c.chan,(chan[c.chan].vol&31)|((chan[c.chan].psgMode&4)<<3));
}
break;
}
case DIV_CMD_NOTE_OFF:
chan[c.chan].keyOff=true;
chan[c.chan].active=false;
chan[c.chan].std.init(NULL);
break;
case DIV_CMD_NOTE_OFF_ENV:
case DIV_CMD_ENV_RELEASE:
chan[c.chan].std.release();
break;
2022-01-14 05:02:10 +00:00
case DIV_CMD_VOLUME: {
chan[c.chan].vol=c.value;
if (!chan[c.chan].std.hasVol) {
chan[c.chan].outVol=c.value;
}
if (isMuted[c.chan]) {
rWrite(0x08+c.chan,0);
} else {
if (chan[c.chan].active) rWrite(0x08+c.chan,(chan[c.chan].vol&31)|((chan[c.chan].psgMode&4)<<3));
}
break;
break;
}
case DIV_CMD_GET_VOLUME: {
return chan[c.chan].vol;
break;
}
case DIV_CMD_INSTRUMENT:
if (chan[c.chan].ins!=c.value || c.value2==1) {
2022-01-14 05:02:10 +00:00
chan[c.chan].insChanged=true;
}
chan[c.chan].ins=c.value;
break;
case DIV_CMD_PITCH: {
chan[c.chan].pitch=c.value;
chan[c.chan].freqChanged=true;
break;
}
case DIV_CMD_NOTE_PORTA: {
int destFreq=NOTE_PERIODIC(c.value2);
2022-01-14 05:02:10 +00:00
bool return2=false;
if (destFreq>chan[c.chan].baseFreq) {
chan[c.chan].baseFreq+=c.value;
if (chan[c.chan].baseFreq>=destFreq) {
chan[c.chan].baseFreq=destFreq;
return2=true;
}
} else {
chan[c.chan].baseFreq-=c.value;
if (chan[c.chan].baseFreq<=destFreq) {
chan[c.chan].baseFreq=destFreq;
return2=true;
}
}
chan[c.chan].freqChanged=true;
if (return2) {
chan[c.chan].inPorta=false;
return 2;
}
break;
}
case DIV_CMD_LEGATO: {
chan[c.chan].baseFreq=NOTE_PERIODIC(c.value);
2022-01-14 05:02:10 +00:00
chan[c.chan].freqChanged=true;
break;
}
case DIV_CMD_STD_NOISE_MODE:
if (c.value<0x10) {
2022-01-18 02:14:35 +00:00
chan[c.chan].psgMode=(c.value+1)&7;
2022-01-18 20:48:40 +00:00
if (isMuted[c.chan]) {
rWrite(0x08+c.chan,0);
} else if (chan[c.chan].active) {
rWrite(0x08+c.chan,(chan[c.chan].outVol&31)|((chan[c.chan].psgMode&4)<<3));
}
2022-01-14 05:02:10 +00:00
} else {
chan[c.chan].duty=c.value&15;
2022-03-04 07:26:47 +00:00
immWrite(0x16+c.chan,chan[c.chan].duty);
2022-01-14 05:02:10 +00:00
}
break;
case DIV_CMD_STD_NOISE_FREQ:
rWrite(0x06,c.value);
break;
case DIV_CMD_AY_ENVELOPE_SET:
ayEnvMode[c.chan]=c.value>>4;
rWrite(regMode[c.chan],ayEnvMode[c.chan]);
if (c.value&15) {
chan[c.chan].psgMode|=4;
} else {
chan[c.chan].psgMode&=~4;
}
if (isMuted[c.chan]) {
rWrite(0x08+c.chan,0);
} else {
rWrite(0x08+c.chan,(chan[c.chan].vol&31)|((chan[c.chan].psgMode&4)<<3));
}
break;
case DIV_CMD_AY_ENVELOPE_LOW:
ayEnvPeriod[c.chan]&=0xff00;
ayEnvPeriod[c.chan]|=c.value;
immWrite(regPeriodL[c.chan],ayEnvPeriod[c.chan]);
immWrite(regPeriodH[c.chan],ayEnvPeriod[c.chan]>>8);
break;
case DIV_CMD_AY_ENVELOPE_HIGH:
ayEnvPeriod[c.chan]&=0xff;
ayEnvPeriod[c.chan]|=c.value<<8;
immWrite(regPeriodL[c.chan],ayEnvPeriod[c.chan]);
immWrite(regPeriodH[c.chan],ayEnvPeriod[c.chan]>>8);
break;
case DIV_CMD_AY_ENVELOPE_SLIDE:
ayEnvSlide[c.chan]=c.value;
break;
case DIV_CMD_AY_NOISE_MASK_AND:
ayNoiseAnd=c.value;
immWrite(0x19,ayNoiseAnd);
break;
case DIV_CMD_AY_NOISE_MASK_OR:
ayNoiseOr=c.value;
immWrite(0x1a,ayNoiseOr);
break;
case DIV_CMD_AY_AUTO_ENVELOPE:
chan[c.chan].autoEnvNum=c.value>>4;
chan[c.chan].autoEnvDen=c.value&15;
chan[c.chan].freqChanged=true;
break;
2022-01-14 05:02:10 +00:00
case DIV_ALWAYS_SET_VOLUME:
return 0;
break;
case DIV_CMD_GET_VOLMAX:
return 31;
break;
case DIV_CMD_PRE_PORTA:
2022-02-09 03:49:52 +00:00
if (chan[c.chan].active && c.value2) {
if (parent->song.resetMacroOnPorta) chan[c.chan].std.init(parent->getIns(chan[c.chan].ins));
}
2022-01-14 05:02:10 +00:00
chan[c.chan].inPorta=c.value;
break;
case DIV_CMD_PRE_NOTE:
break;
default:
//printf("WARNING: unimplemented command %d\n",c.cmd);
break;
}
return 1;
}
void DivPlatformAY8930::muteChannel(int ch, bool mute) {
isMuted[ch]=mute;
if (isMuted[ch]) {
rWrite(0x08+ch,0);
} else {
rWrite(0x08+ch,(chan[ch].outVol&31)|((chan[ch].psgMode&4)<<3));
}
}
void DivPlatformAY8930::forceIns() {
for (int i=0; i<3; i++) {
chan[i].insChanged=true;
immWrite(regPeriodL[i],ayEnvPeriod[i]);
immWrite(regPeriodH[i],ayEnvPeriod[i]>>8);
immWrite(regMode[i],ayEnvMode[i]);
}
}
2022-01-27 05:29:16 +00:00
void* DivPlatformAY8930::getChanState(int ch) {
return &chan[ch];
}
unsigned char* DivPlatformAY8930::getRegisterPool() {
return regPool;
}
int DivPlatformAY8930::getRegisterPoolSize() {
return 32;
}
2022-01-14 05:02:10 +00:00
void DivPlatformAY8930::reset() {
while (!writes.empty()) writes.pop();
ay->device_reset();
memset(regPool,0,32);
2022-01-14 05:02:10 +00:00
for (int i=0; i<3; i++) {
chan[i]=DivPlatformAY8930::Channel();
chan[i].vol=31;
ayEnvPeriod[i]=0;
ayEnvMode[i]=0;
ayEnvSlide[i]=0;
ayEnvSlideLow[i]=0;
}
if (dumpWrites) {
addWrite(0xffffffff,0);
}
2022-01-14 05:02:10 +00:00
for (int i=0; i<32; i++) {
oldWrites[i]=-1;
pendingWrites[i]=-1;
}
2022-01-31 22:28:17 +00:00
ayNoiseAnd=2;
ayNoiseOr=0;
2022-01-14 05:02:10 +00:00
delay=0;
extMode=false;
bank=false;
immWrite(0x0d,0xa0);
2022-01-31 22:28:17 +00:00
immWrite(0x19,2); // and mask
2022-01-31 22:00:52 +00:00
immWrite(0x1a,0x00); // or mask
2022-01-14 05:02:10 +00:00
}
bool DivPlatformAY8930::isStereo() {
2022-01-29 05:20:27 +00:00
return true;
2022-01-14 05:02:10 +00:00
}
bool DivPlatformAY8930::keyOffAffectsArp(int ch) {
return true;
}
void DivPlatformAY8930::notifyInsDeletion(void* ins) {
for (int i=0; i<3; i++) {
chan[i].std.notifyInsDeletion((DivInstrument*)ins);
}
}
2022-02-01 23:08:19 +00:00
void DivPlatformAY8930::poke(unsigned int addr, unsigned short val) {
immWrite(addr,val);
}
void DivPlatformAY8930::poke(std::vector<DivRegWrite>& wlist) {
for (DivRegWrite& i: wlist) immWrite(i.addr,i.val);
}
2022-01-28 17:59:53 +00:00
void DivPlatformAY8930::setFlags(unsigned int flags) {
2022-01-29 05:20:27 +00:00
switch (flags&15) {
case 1:
chipClock=COLOR_PAL*2.0/5.0;
break;
case 2:
chipClock=1750000;
break;
case 3:
chipClock=2000000;
break;
case 4:
chipClock=1500000;
break;
case 5:
chipClock=1000000;
break;
case 6:
chipClock=COLOR_NTSC/4.0;
break;
case 7:
chipClock=COLOR_PAL*3.0/8.0;
break;
case 8:
chipClock=COLOR_PAL*3.0/16.0;
break;
default:
chipClock=COLOR_NTSC/2.0;
break;
2022-01-14 05:26:10 +00:00
}
rate=chipClock/8;
2022-01-29 05:20:27 +00:00
stereo=flags>>6;
2022-01-14 05:26:10 +00:00
}
2022-01-28 17:59:53 +00:00
int DivPlatformAY8930::init(DivEngine* p, int channels, int sugRate, unsigned int flags) {
2022-01-14 05:02:10 +00:00
parent=p;
2022-01-17 04:21:27 +00:00
dumpWrites=false;
2022-01-14 05:02:10 +00:00
skipRegisterWrites=false;
for (int i=0; i<3; i++) {
isMuted[i]=false;
}
2022-01-28 17:59:53 +00:00
setFlags(flags);
2022-01-14 05:02:10 +00:00
ay=new ay8930_device(rate);
ay->device_start();
ayBufLen=65536;
for (int i=0; i<3; i++) ayBuf[i]=new short[ayBufLen];
reset();
return 3;
}
void DivPlatformAY8930::quit() {
for (int i=0; i<3; i++) delete[] ayBuf[i];
delete ay;
2022-01-17 18:29:35 +00:00
}