C140/C219: several fixes

loop point fix
crash fix
This commit is contained in:
tildearrow 2023-09-24 16:54:19 -05:00
parent d904c63f9d
commit 67c3a67a0d
2 changed files with 41 additions and 16 deletions

View file

@ -245,10 +245,10 @@ void DivPlatformC140::tick(bool sysTick) {
if (chan[i].sample>=0 && chan[i].sample<parent->song.sampleLen && s->isLoopable()) { if (chan[i].sample>=0 && chan[i].sample<parent->song.sampleLen && s->isLoopable()) {
if (is219) { if (is219) {
loop=MIN(start+(s->loopStart>>1),65535); loop=MIN(start+(s->loopStart>>1),65535);
end=MIN(start+(s->loopEnd>>1)-1,65535); end=MIN(start+(s->loopEnd>>1),65535);
} else { } else {
loop=MIN(start+s->loopStart,65535); loop=MIN(start+s->loopStart+1,65535);
end=MIN(start+s->loopEnd-1,65535); end=MIN(start+s->loopEnd+1,65535);
} }
} else if (chan[i].noise && is219) { } else if (chan[i].noise && is219) {
loop=0; loop=0;
@ -575,7 +575,7 @@ void DivPlatformC140::renderSamples(int sysID) {
} }
if (is219) { // C219 (8-bit) if (is219) { // C219 (8-bit)
unsigned int length=s->length8; unsigned int length=s->length8+4;
// fit sample size to single bank size // fit sample size to single bank size
if (length>131072) { if (length>131072) {
length=131072; length=131072;
@ -594,27 +594,39 @@ void DivPlatformC140::renderSamples(int sysID) {
logW("out of C219 memory for sample %d!",i); logW("out of C219 memory for sample %d!",i);
} }
if (s->depth==DIV_SAMPLE_DEPTH_C219) { if (s->depth==DIV_SAMPLE_DEPTH_C219) {
unsigned char next=0;
unsigned int sPos=0;
for (unsigned int i=0; i<length; i++) { for (unsigned int i=0; i<length; i++) {
if (i>=s->lengthC219) { if (sPos<s->lengthC219) {
sampleMem[(memPos+i)^1]=0; next=s->dataC219[sPos++];
} else { if (s->isLoopable()) {
sampleMem[(memPos+i)^1]=s->dataC219[i]; if ((int)sPos>=s->loopEnd) {
sPos=s->loopStart;
}
}
} }
sampleMem[(memPos+i)^1]=next;
} }
} else { } else {
signed char next=0;
unsigned int sPos=0;
for (unsigned int i=0; i<length; i++) { for (unsigned int i=0; i<length; i++) {
if (i>=s->length8) { if (sPos<s->length8) {
sampleMem[(memPos+i)^1]=0; next=s->data8[sPos++];
} else { if (s->isLoopable()) {
sampleMem[(memPos+i)^1]=s->data8[i]; if ((int)sPos>=s->loopEnd) {
sPos=s->loopStart;
}
}
} }
sampleMem[(memPos+i)^1]=next;
} }
} }
sampleOff[i]=memPos>>1; sampleOff[i]=memPos>>1;
sampleLoaded[i]=true; sampleLoaded[i]=true;
memPos+=length; memPos+=length;
} else { // C140 (16-bit) } else { // C140 (16-bit)
unsigned int length=s->length16; unsigned int length=s->length16+4;
// fit sample size to single bank size // fit sample size to single bank size
if (length>(131072)) { if (length>(131072)) {
length=131072; length=131072;
@ -641,7 +653,20 @@ void DivPlatformC140::renderSamples(int sysID) {
sampleMem[1+i+memPos]=c140Mu; sampleMem[1+i+memPos]=c140Mu;
} }
} else { } else {
memcpy(sampleMem+memPos,s->data16,length); short next=0;
unsigned int sPos=0;
for (unsigned int i=0; i<length; i+=2) {
if (sPos<s->samples) {
next=s->data16[sPos++];
if (s->isLoopable()) {
if ((int)sPos>=s->loopEnd) {
sPos=s->loopStart;
}
}
}
sampleMem[memPos+i]=((unsigned short)next);
sampleMem[memPos+i+1]=((unsigned short)next)>>8;
}
} }
sampleOff[i]=memPos>>1; sampleOff[i]=memPos>>1;
sampleLoaded[i]=true; sampleLoaded[i]=true;

View file

@ -104,7 +104,7 @@ void c140_voice_tick(struct c140_t *c140, const unsigned char v, const int cycle
if (!voice->muted) if (!voice->muted)
{ {
// fetch 12 bit sample // fetch 12 bit sample
signed short s1 = c140->sample_mem[((unsigned int)(voice->bank) << 16) | voice->addr] & ~0xf; signed short s1 = c140->sample_mem[((unsigned int)(voice->bank) << 16) | (voice->addr & 0xffff)] & ~0xf;
signed short s2 = c140->sample_mem[((unsigned int)(voice->bank) << 16) | ((voice->addr + 1) & 0xffff)] & ~0xf; signed short s2 = c140->sample_mem[((unsigned int)(voice->bank) << 16) | ((voice->addr + 1) & 0xffff)] & ~0xf;
if (voice->compressed) if (voice->compressed)
{ {
@ -171,7 +171,7 @@ void c219_voice_tick(struct c219_t *c219, const unsigned char v, const int cycle
else else
{ {
// fetch 8 bit sample // fetch 8 bit sample
signed short s1 = c219->sample_mem[((unsigned int)(c219->bank[(v >> 2) & 3]) << 17) | (voice->addr^1)]; signed short s1 = c219->sample_mem[((unsigned int)(c219->bank[(v >> 2) & 3]) << 17) | ((voice->addr^1) & 0x1ffff)];
signed short s2 = c219->sample_mem[((unsigned int)(c219->bank[(v >> 2) & 3]) << 17) | (((voice->addr + 1) & 0x1ffff)^1)]; signed short s2 = c219->sample_mem[((unsigned int)(c219->bank[(v >> 2) & 3]) << 17) | (((voice->addr + 1) & 0x1ffff)^1)];
if (voice->compressed) if (voice->compressed)
{ {