dev213 - Merge pull request #1954 from akumanatt/snesveratia

SNES, VERA and TIA additions
This commit is contained in:
tildearrow 2024-06-23 03:52:48 -05:00 committed by GitHub
commit 54e9a31971
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
23 changed files with 884 additions and 37 deletions

View file

@ -221,7 +221,7 @@ void DivPlatformSNES::tick(bool sysTick) {
end=MIN(start+MAX(s->lengthBRR+((s->loop && s->depth!=DIV_SAMPLE_DEPTH_BRR)?9:0),1),getSampleMemCapacity());
loop=MAX(start,end-1);
if (chan[i].audPos>0) {
start=start+MIN(chan[i].audPos,s->lengthBRR-1)/16*9;
start=start+MIN(chan[i].audPos/16*9,end-start);
}
if (s->isLoopable()) {
loop=((s->depth!=DIV_SAMPLE_DEPTH_BRR)?9:0)+start+((s->loopStart/16)*9);
@ -463,7 +463,6 @@ int DivPlatformSNES::dispatch(DivCommand c) {
chan[c.chan].inPorta=c.value;
break;
case DIV_CMD_SAMPLE_POS:
// may have to remove this
chan[c.chan].audPos=c.value;
chan[c.chan].setPos=true;
break;
@ -933,7 +932,6 @@ const void* DivPlatformSNES::getSampleMem(int index) {
}
size_t DivPlatformSNES::getSampleMemCapacity(int index) {
// TODO change it based on current echo buffer size
return index == 0 ? (65536-echoDelay*2048) : 0;
}

View file

@ -2,6 +2,10 @@
// Copyright (c) 2020 Frank van den Hoef
// All rights reserved. License: 2-clause BSD
// Chip revisions
// 0: V 0.3.0
// 1: V 47.0.0 (9-bit volume, phase reset on mute)
#include "vera_psg.h"
#include <stdlib.h>
@ -14,7 +18,15 @@ enum waveform {
WF_NOISE,
};
static uint8_t volume_lut[64] = {0, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 6, 6, 7, 7, 7, 8, 8, 9, 9, 10, 11, 11, 12, 13, 14, 14, 15, 16, 17, 18, 19, 21, 22, 23, 25, 26, 28, 29, 31, 33, 35, 37, 39, 42, 44, 47, 50, 52, 56, 59, 63};
static uint16_t volume_lut[64] = {0, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 6, 6, 7, 7, 7, 8, 8, 9, 9, 10, 11, 11, 12, 13, 14, 14, 15, 16, 17, 18, 19, 21, 22, 23, 25, 26, 28, 29, 31, 33, 35, 37, 39, 42, 44, 47, 50, 52, 56, 59, 63};
static uint16_t volume_lut_47[64] = {
0, 4, 8, 12,
16, 17, 18, 20, 21, 22, 23, 25, 26, 28, 30, 31,
33, 35, 37, 40, 42, 45, 47, 50, 53, 56, 60, 63,
67, 71, 75, 80, 85, 90, 95, 101, 107, 113, 120, 127,
135, 143, 151, 160, 170, 180, 191, 202, 214, 227, 241, 255,
270, 286, 303, 321, 341, 361, 382, 405, 429, 455, 482, 511
};
void
psg_reset(struct VERA_PSG* psg)
@ -38,7 +50,7 @@ psg_writereg(struct VERA_PSG* psg, uint8_t reg, uint8_t val)
case 2: {
psg->channels[ch].right = (val & 0x80) != 0;
psg->channels[ch].left = (val & 0x40) != 0;
psg->channels[ch].volume = volume_lut[val & 0x3F];
psg->channels[ch].volume = ((psg->chipType < 1) ? volume_lut : volume_lut_47)[val & 0x3F];
break;
}
case 3: {
@ -65,8 +77,11 @@ render(struct VERA_PSG* psg, int16_t *left, int16_t *right)
struct VERAChannel *ch = &psg->channels[i];
unsigned new_phase = (ch->phase + ch->freq) & 0x1FFFF;
if ((psg->chipType >= 1) && (!ch->left && !ch->right)) {
new_phase = 0;
}
if ((ch->phase & 0x10000) != (new_phase & 0x10000)) {
ch->noiseval = psg->noiseOut;
ch->noiseval = (psg->chipType < 1) ? psg->noiseOut : (psg->noiseState >> 1) & 0x3f;
}
ch->phase = new_phase;
@ -85,14 +100,14 @@ render(struct VERA_PSG* psg, int16_t *left, int16_t *right)
int val = (int)sv * (int)ch->volume;
if (ch->left) {
l += val;
l += (psg->chipType < 1) ? val : val >> 3;
}
if (ch->right) {
r += val;
r += (psg->chipType < 1) ? val : val >> 3;
}
if (ch->left || ch->right) {
ch->lastOut = val;
ch->lastOut = (psg->chipType < 1) ? val << 3 : val;
} else {
ch->lastOut = 0;
}

View file

@ -9,7 +9,7 @@
struct VERAChannel {
uint16_t freq;
uint8_t volume;
uint16_t volume;
bool left, right;
uint8_t pw;
uint8_t waveform;
@ -20,7 +20,7 @@ struct VERAChannel {
};
struct VERA_PSG {
unsigned int noiseState, noiseOut;
unsigned int chipType, noiseState, noiseOut;
struct VERAChannel channels[16];
};

View file

@ -40,6 +40,30 @@ const char** DivPlatformTIA::getRegisterSheet() {
void DivPlatformTIA::acquire(short** buf, size_t len) {
for (size_t h=0; h<len; h++) {
if (softwarePitch) {
int i=-1;
tuneCounter++;
if (tuneCounter==228) {
i=0;
}
if (tuneCounter>=456) {
i=1;
tuneCounter=0;
}
if (i>=0) {
if (chan[i].tuneCtr++>=chan[i].curFreq) {
int freq=chan[i].freq;
chan[i].tuneAcc+=chan[i].tuneFreq;
if (chan[i].tuneAcc>=256) {
freq++;
chan[i].tuneAcc-=256;
}
chan[i].curFreq=freq;
chan[i].tuneCtr=0;
rWrite(0x17+i,freq);
}
}
}
tia.tick();
if (mixingType==2) {
buf[0][h]=tia.myCurrentSample[0];
@ -92,6 +116,44 @@ unsigned char DivPlatformTIA::dealWithFreq(unsigned char shape, int base, int pi
return ret;
}
int DivPlatformTIA::dealWithFreqNew(int shape, int bp) {
double mult=(parent->song.tuning*0.0625)*pow(2.0,double(768+bp)/(256.0*12.0));
double clock=chipClock/28.5;
switch (shape) {
case 1: // buzzy
mult*=30;
break;
case 2: // low buzzy
mult*=465;
break;
case 3: // flangy
mult*=58.125;
break;
case 4: case 5: // square
mult*=4;
break;
case 6: case 7: case 9: case 10: // pure buzzy/reedy
mult*=62;
break;
case 8: // noise
mult*=63.875;
break;
case 12: case 13: // low square
mult*=12;
break;
case 14: case 15: // low pure buzzy/reedy
mult*=186;
break;
}
if (mult<1) mult=1;
if (clock<mult) {
return 0;
}
double ret=floor(clock/mult);
int fract=((clock/mult)-ret)*256;
return ret*256+fract-256;
}
void DivPlatformTIA::tick(bool sysTick) {
for (int i=0; i<2; i++) {
chan[i].std.next();
@ -120,13 +182,17 @@ void DivPlatformTIA::tick(bool sysTick) {
chan[i].freqChanged=true;
}
if (chan[i].freqChanged || chan[i].keyOn || chan[i].keyOff) {
int bf=chan[i].baseFreq;
if (!chan[i].fixedArp) {
bf+=chan[i].arpOff<<8;
}
if (chan[i].fixedArp) {
chan[i].freq=chan[i].baseNoteOverride&31;
} else {
chan[i].tuneFreq=0;
if (!skipRegisterWrites && dumpWrites) {
addWrite(0xfffe0000+i,chan[i].freq*256);
}
} else if (oldPitch) {
int bf=chan[i].baseFreq;
if (!chan[i].fixedArp) {
bf+=chan[i].arpOff<<8;
}
chan[i].freq=dealWithFreq(chan[i].shape,bf,chan[i].pitch+chan[i].pitch2);
if (chan[i].shape==4 || chan[i].shape==5) {
if (bf<39*256) {
@ -140,12 +206,41 @@ void DivPlatformTIA::tick(bool sysTick) {
}
}
if (chan[i].freq>31) chan[i].freq=31;
chan[i].tuneFreq=0;
} else {
int bf=chan[i].baseFreq+(chan[i].arpOff<<8);
int shape=chan[i].shape;
if (shape==4 || shape==5) {
if (bf<40*256) {
shape=6;
rWrite(0x15+i,6);
} else if (bf<59*256) {
shape=12;
rWrite(0x15+i,12);
} else {
rWrite(0x15+i,4);
}
}
bf+=chan[i].pitch+chan[i].pitch2;
int freq=dealWithFreqNew(shape,bf);
if (freq>=31*256) freq=31*256;
if (softwarePitch && !skipRegisterWrites && dumpWrites) {
addWrite(0xfffe0000+i,freq);
}
chan[i].freq=freq>>8;
chan[i].tuneFreq=freq&255;
}
if (chan[i].keyOff) {
rWrite(0x19+i,0);
}
rWrite(0x17+i,chan[i].freq);
if (!softwarePitch) {
if (chan[i].tuneFreq>=128) chan[i].freq++;
rWrite(0x17+i,chan[i].freq);
if (!skipRegisterWrites && dumpWrites) {
addWrite(0xfffe0000+i,chan[i].freq<<8);
}
}
if (chan[i].keyOn) chan[i].keyOn=false;
if (chan[i].keyOff) chan[i].keyOff=false;
chan[i].freqChanged=false;
@ -272,6 +367,11 @@ int DivPlatformTIA::dispatch(DivCommand c) {
break;
case DIV_CMD_PRE_NOTE:
break;
case DIV_CMD_EXTERNAL:
if (!skipRegisterWrites && dumpWrites) {
addWrite(0xfffe0002,c.value);
}
break;
default:
//printf("WARNING: unimplemented command %d\n",c.cmd);
break;
@ -319,6 +419,7 @@ int DivPlatformTIA::getRegisterPoolSize() {
}
void DivPlatformTIA::reset() {
tuneCounter=0;
tia.reset(mixingType);
memset(regPool,0,16);
for (int i=0; i<2; i++) {
@ -367,6 +468,8 @@ void DivPlatformTIA::setFlags(const DivConfig& flags) {
CHECK_CUSTOM_CLOCK;
rate=chipClock;
mixingType=flags.getInt("mixingType",0)&3;
softwarePitch=flags.getBool("softwarePitch",false);
oldPitch=flags.getBool("oldPitch",false);
for (int i=0; i<2; i++) {
oscBuf[i]->rate=rate/114;
}

View file

@ -27,21 +27,31 @@ class DivPlatformTIA: public DivDispatch {
protected:
struct Channel: public SharedChannel<int> {
unsigned char shape;
unsigned char curFreq, tuneCtr, tuneFreq;
int tuneAcc;
Channel():
SharedChannel<int>(15),
shape(4) {}
shape(4),
curFreq(0),
tuneCtr(0),
tuneFreq(0),
tuneAcc(0) {}
};
Channel chan[2];
DivDispatchOscBuffer* oscBuf[2];
bool isMuted[2];
bool softwarePitch;
bool oldPitch;
unsigned char mixingType;
unsigned char chanOscCounter;
TIA::Audio tia;
unsigned char regPool[16];
int tuneCounter;
friend void putDispatchChip(void*,int);
friend void putDispatchChan(void*,int,int);
unsigned char dealWithFreq(unsigned char shape, int base, int pitch);
int dealWithFreqNew(int shape, int bp);
public:
void acquire(short** buf, size_t len);

View file

@ -119,7 +119,7 @@ void DivPlatformVERA::acquire(short** buf, size_t len) {
pos++;
for (int i=0; i<16; i++) {
oscBuf[i]->data[oscBuf[i]->needle++]=psg->channels[i].lastOut<<3;
oscBuf[i]->data[oscBuf[i]->needle++]=psg->channels[i].lastOut;
}
int pcmOut=(whyCallItBuf[2][i]+whyCallItBuf[3][i])>>1;
if (pcmOut<-32768) pcmOut=-32768;
@ -532,6 +532,7 @@ void DivPlatformVERA::poke(std::vector<DivRegWrite>& wlist) {
}
void DivPlatformVERA::setFlags(const DivConfig& flags) {
psg->chipType=flags.getInt("chipType",1);
chipClock=25000000;
CHECK_CUSTOM_CLOCK;
rate=chipClock/512;