Merge branch 'x16'

This commit is contained in:
tildearrow 2022-03-10 17:31:32 -05:00
commit cbc8721a00
17 changed files with 820 additions and 15 deletions

View file

@ -0,0 +1,133 @@
// Commander X16 Emulator
// Copyright (c) 2020 Frank van den Hoef
// All rights reserved. License: 2-clause BSD
#include "vera_pcm.h"
#include <stdio.h>
static uint8_t volume_lut[16] = {0, 1, 2, 3, 4, 5, 6, 8, 11, 14, 18, 23, 30, 38, 49, 64};
static void
fifo_reset(struct VERA_PCM* pcm)
{
pcm->fifo_wridx = 0;
pcm->fifo_rdidx = 0;
pcm->fifo_cnt = 0;
}
void
pcm_reset(struct VERA_PCM* pcm)
{
fifo_reset(pcm);
pcm->ctrl = 0;
pcm->rate = 0;
pcm->cur_l = 0;
pcm->cur_r = 0;
pcm->phase = 0;
}
void
pcm_write_ctrl(struct VERA_PCM* pcm, uint8_t val)
{
if (val & 0x80) {
fifo_reset(pcm);
}
pcm->ctrl = val & 0x3F;
}
uint8_t
pcm_read_ctrl(struct VERA_PCM* pcm)
{
uint8_t result = pcm->ctrl;
if (pcm->fifo_cnt == sizeof(pcm->fifo)) {
result |= 0x80;
}
return result;
}
void
pcm_write_rate(struct VERA_PCM* pcm, uint8_t val)
{
pcm->rate = val;
}
uint8_t
pcm_read_rate(struct VERA_PCM* pcm)
{
return pcm->rate;
}
void
pcm_write_fifo(struct VERA_PCM* pcm, uint8_t val)
{
if (pcm->fifo_cnt < sizeof(pcm->fifo)) {
pcm->fifo[pcm->fifo_wridx++] = val;
if (pcm->fifo_wridx == sizeof(pcm->fifo)) {
pcm->fifo_wridx = 0;
}
pcm->fifo_cnt++;
}
}
static uint8_t
read_fifo(struct VERA_PCM* pcm)
{
if (pcm->fifo_cnt == 0) {
return 0;
}
uint8_t result = pcm->fifo[pcm->fifo_rdidx++];
if (pcm->fifo_rdidx == sizeof(pcm->fifo)) {
pcm->fifo_rdidx = 0;
}
pcm->fifo_cnt--;
return result;
}
bool
pcm_is_fifo_almost_empty(struct VERA_PCM* pcm)
{
return pcm->fifo_cnt < 1024;
}
void
pcm_render(struct VERA_PCM* pcm, int16_t* buf_l, int16_t* buf_r, unsigned num_samples)
{
while (num_samples--) {
uint8_t old_phase = pcm->phase;
pcm->phase += pcm->rate;
if ((old_phase & 0x80) != (pcm->phase & 0x80)) {
switch ((pcm->ctrl >> 4) & 3) {
case 0: { // mono 8-bit
pcm->cur_l = (int16_t)read_fifo(pcm) << 8;
pcm->cur_r = pcm->cur_l;
break;
}
case 1: { // stereo 8-bit
pcm->cur_l = read_fifo(pcm) << 8;
pcm->cur_r = read_fifo(pcm) << 8;
break;
}
case 2: { // mono 16-bit
pcm->cur_l = read_fifo(pcm);
pcm->cur_l |= read_fifo(pcm) << 8;
pcm->cur_r = pcm->cur_l;
break;
}
case 3: { // stereo 16-bit
pcm->cur_l = read_fifo(pcm);
pcm->cur_l |= read_fifo(pcm) << 8;
pcm->cur_r = read_fifo(pcm);
pcm->cur_r |= read_fifo(pcm) << 8;
break;
}
}
}
*(buf_l) += ((int)pcm->cur_l * (int)volume_lut[pcm->ctrl & 0xF]) >> 6;
*(buf_r) += ((int)pcm->cur_r * (int)volume_lut[pcm->ctrl & 0xF]) >> 6;
buf_l++;
buf_r++;
}
}

View file

@ -0,0 +1,31 @@
// Commander X16 Emulator
// Copyright (c) 2020 Frank van den Hoef
// All rights reserved. License: 2-clause BSD
#pragma once
#include <stdint.h>
#include <stdbool.h>
struct VERA_PCM {
uint8_t fifo[4096 - 1]; // Actual hardware FIFO is 4kB, but you can only use 4095 bytes.
unsigned fifo_wridx;
unsigned fifo_rdidx;
unsigned fifo_cnt;
uint8_t ctrl;
uint8_t rate;
int16_t cur_l, cur_r;
uint8_t phase;
};
void pcm_reset(struct VERA_PCM* pcm);
void pcm_write_ctrl(struct VERA_PCM* pcm, uint8_t val);
uint8_t pcm_read_ctrl(struct VERA_PCM* pcm);
void pcm_write_rate(struct VERA_PCM* pcm, uint8_t val);
uint8_t pcm_read_rate(struct VERA_PCM* pcm);
void pcm_write_fifo(struct VERA_PCM* pcm, uint8_t val);
void pcm_render(struct VERA_PCM* pcm, int16_t* buf_l, int16_t* buf_r, unsigned num_samples);
bool pcm_is_fifo_almost_empty(struct VERA_PCM* pcm);

View file

@ -0,0 +1,106 @@
// Commander X16 Emulator
// Copyright (c) 2020 Frank van den Hoef
// All rights reserved. License: 2-clause BSD
#include "vera_psg.h"
#include <stdlib.h>
#include <string.h>
enum waveform {
WF_PULSE = 0,
WF_SAWTOOTH,
WF_TRIANGLE,
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};
void
psg_reset(struct VERA_PSG* psg)
{
memset(psg->channels, 0, sizeof(psg->channels));
psg->noiseState=1;
psg->noiseOut=0;
}
void
psg_writereg(struct VERA_PSG* psg, uint8_t reg, uint8_t val)
{
reg &= 0x3f;
int ch = reg / 4;
int idx = reg & 3;
switch (idx) {
case 0: psg->channels[ch].freq = (psg->channels[ch].freq & 0xFF00) | val; break;
case 1: psg->channels[ch].freq = (psg->channels[ch].freq & 0x00FF) | (val << 8); break;
case 2: {
psg->channels[ch].right = (val & 0x80) != 0;
psg->channels[ch].left = (val & 0x40) != 0;
psg->channels[ch].volume = volume_lut[val & 0x3F];
break;
}
case 3: {
psg->channels[ch].pw = val & 0x3F;
psg->channels[ch].waveform = val >> 6;
break;
}
}
}
static inline void
render(struct VERA_PSG* psg, int16_t *left, int16_t *right)
{
int l = 0;
int r = 0;
// TODO this is a currently speculated noise generation
// as the hardware and sources for it are not out in the public
// and the official emulator just uses rand()
psg->noiseOut=((psg->noiseOut<<1)|(psg->noiseState&1))&63;
psg->noiseState=(psg->noiseState<<1)|(((psg->noiseState>>1)^(psg->noiseState>>2)^(psg->noiseState>>4)^(psg->noiseState>>15))&1);
for (int i = 0; i < 16; i++) {
struct VERAChannel *ch = &psg->channels[i];
unsigned new_phase = (ch->phase + ch->freq) & 0x1FFFF;
if ((ch->phase & 0x10000) != (new_phase & 0x10000)) {
ch->noiseval = psg->noiseOut;
}
ch->phase = new_phase;
uint8_t v = 0;
switch (ch->waveform) {
case WF_PULSE: v = (ch->phase >> 10) > ch->pw ? 0 : 63; break;
case WF_SAWTOOTH: v = ch->phase >> 11; break;
case WF_TRIANGLE: v = (ch->phase & 0x10000) ? (~(ch->phase >> 10) & 0x3F) : ((ch->phase >> 10) & 0x3F); break;
case WF_NOISE: v = ch->noiseval; break;
}
int8_t sv = (v ^ 0x20);
if (sv & 0x20) {
sv |= 0xC0;
}
int val = (int)sv * (int)ch->volume;
if (ch->left) {
l += val;
}
if (ch->right) {
r += val;
}
}
*left = l;
*right = r;
}
void
psg_render(struct VERA_PSG* psg, int16_t *bufL, int16_t *bufR, unsigned num_samples)
{
while (num_samples--) {
render(psg, bufL, bufR);
bufL++;
bufR++;
}
}

View file

@ -0,0 +1,28 @@
// Commander X16 Emulator
// Copyright (c) 2020 Frank van den Hoef
// All rights reserved. License: 2-clause BSD
#pragma once
#include <stdint.h>
#include <stdbool.h>
struct VERAChannel {
uint16_t freq;
uint8_t volume;
bool left, right;
uint8_t pw;
uint8_t waveform;
unsigned phase;
uint8_t noiseval;
};
struct VERA_PSG {
unsigned int noiseState, noiseOut;
struct VERAChannel channels[16];
};
void psg_reset(struct VERA_PSG* psg);
void psg_writereg(struct VERA_PSG* psg, uint8_t reg, uint8_t val);
void psg_render(struct VERA_PSG* psg, int16_t *bufL, int16_t *bufR, unsigned num_samples);

View file

@ -0,0 +1,345 @@
/**
* 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.
*/
#include "vera.h"
#include "../engine.h"
#include <string.h>
#include <math.h>
extern "C" {
#include "sound/vera_psg.h"
#include "sound/vera_pcm.h"
}
#define rWrite(c,a,d) {regPool[(c)*4+(a)]=(d); psg_writereg(psg,((c)*4+(a)),(d));}
#define rWriteLo(c,a,d) rWrite(c,a,(regPool[(c)*4+(a)]&(~0x3f))|((d)&0x3f))
#define rWriteHi(c,a,d) rWrite(c,a,(regPool[(c)*4+(a)]&(~0xc0))|(((d)<<6)&0xc0))
#define rWriteFIFOVol(d) rWrite(16,0,(regPool[64]&(~0x3f))|((d)&0x3f))
const char* regCheatSheetVERA[]={
"CHxFreq", "00+x*4",
"CHxVol", "02+x*4",
"CHxWave", "03+x*4",
"AUDIO_CTRL", "40",
"AUDIO_RATE", "41",
NULL
};
const char** DivPlatformVERA::getRegisterSheet() {
return regCheatSheetVERA;
}
const char* DivPlatformVERA::getEffectName(unsigned char effect) {
switch (effect) {
case 0x20:
return "20xx: Change waveform";
break;
case 0x22:
return "22xx: Set duty cycle (0 to 63)";
break;
}
return NULL;
}
// TODO: wire up PCM.
void DivPlatformVERA::acquire(short* bufL, short* bufR, size_t start, size_t len) {
psg_render(psg,bufL+start,bufR+start,len);
pcm_render(pcm,bufL+start,bufR+start,len);
}
void DivPlatformVERA::reset() {
for (int i=0; i<17; i++) {
chan[i]=Channel();
}
memset(regPool,0,66);
for (int i=0; i<16; i++) {
chan[i].vol=63;
chan[i].pan=3;
rWriteHi(i,2,isMuted[i]?0:3);
}
chan[16].vol=15;
chan[16].pan=3;
}
int DivPlatformVERA::calcNoteFreq(int ch, int note) {
if (ch<16) {
return parent->calcBaseFreq(chipClock,2097152,note,false);
} else {
double off=1.0;
if (chan[ch].pcm.sample>=0 && chan[ch].pcm.sample<parent->song.sampleLen) {
DivSample* s=parent->getSample(chan[ch].pcm.sample);
if (s->centerRate<1) {
off=1.0;
} else {
off=s->centerRate/8363.0;
}
}
return (int)(off*parent->calcBaseFreq(chipClock,65536,note,false));
}
}
void DivPlatformVERA::tick() {
for (int i=0; i<16; i++) {
chan[i].std.next();
if (chan[i].std.hadVol) {
chan[i].outVol=MAX(chan[i].vol+chan[i].std.vol-63,0);
rWriteLo(i,2,chan[i].outVol);
}
if (chan[i].std.hadArp) {
if (!chan[i].inPorta) {
if (chan[i].std.arpMode) {
chan[i].baseFreq=calcNoteFreq(0,chan[i].std.arp);
} else {
chan[i].baseFreq=calcNoteFreq(0,chan[i].note+chan[i].std.arp);
}
}
chan[i].freqChanged=true;
} else {
if (chan[i].std.arpMode && chan[i].std.finishedArp) {
chan[i].baseFreq=calcNoteFreq(0,chan[i].note);
chan[i].freqChanged=true;
}
}
if (chan[i].std.hadDuty) {
rWriteLo(i,3,chan[i].std.duty);
}
if (chan[i].std.hadWave) {
rWriteHi(i,3,chan[i].std.wave);
}
if (chan[i].freqChanged) {
chan[i].freq=parent->calcFreq(chan[i].baseFreq,chan[i].pitch,false,8);
if (chan[i].freq>65535) chan[i].freq=65535;
rWrite(i,0,chan[i].freq&0xff);
rWrite(i,1,(chan[i].freq>>8)&0xff);
chan[i].freqChanged=false;
}
}
// PCM
chan[16].std.next();
if (chan[16].std.hadVol) {
chan[16].outVol=MAX(chan[16].vol+MIN(chan[16].std.vol/4,15)-15,0);
rWriteFIFOVol(chan[16].outVol&15);
}
if (chan[16].std.hadArp) {
if (!chan[16].inPorta) {
if (chan[16].std.arpMode) {
chan[16].baseFreq=calcNoteFreq(16,chan[16].std.arp);
} else {
chan[16].baseFreq=calcNoteFreq(16,chan[16].note+chan[16].std.arp);
}
}
chan[16].freqChanged=true;
} else {
if (chan[16].std.arpMode && chan[16].std.finishedArp) {
chan[16].baseFreq=calcNoteFreq(16,chan[16].note);
chan[16].freqChanged=true;
}
}
if (chan[16].freqChanged) {
chan[16].freq=parent->calcFreq(chan[16].baseFreq,chan[16].pitch,false,8);
if (chan[16].freq>128) chan[16].freq=128;
rWrite(16,1,chan[16].freq&0xff);
chan[16].freqChanged=false;
}
}
int DivPlatformVERA::dispatch(DivCommand c) {
int tmp;
switch (c.cmd) {
case DIV_CMD_NOTE_ON:
if(c.chan<16) {
rWriteLo(c.chan,2,chan[c.chan].vol)
} else {
chan[c.chan].pcm.sample=parent->getIns(chan[16].ins)->amiga.initSample;
if (chan[c.chan].pcm.sample<0 || chan[c.chan].pcm.sample>=parent->song.sampleLen) {
chan[c.chan].pcm.sample=-1;
}
chan[16].pcm.pos=0;
rWriteFIFOVol(chan[c.chan].vol);
}
if (c.value!=DIV_NOTE_NULL) {
chan[c.chan].baseFreq=calcNoteFreq(c.chan,c.value);
chan[c.chan].freqChanged=true;
chan[c.chan].note=c.value;
}
chan[c.chan].active=true;
chan[c.chan].std.init(parent->getIns(chan[c.chan].ins));
break;
case DIV_CMD_NOTE_OFF:
chan[c.chan].active=false;
if(c.chan<16) {
rWriteLo(c.chan,2,0)
} else {
chan[16].pcm.sample=-1;
rWriteFIFOVol(0);
rWrite(16,1,0);
}
chan[c.chan].std.init(NULL);
break;
case DIV_CMD_NOTE_OFF_ENV:
case DIV_CMD_ENV_RELEASE:
chan[c.chan].std.release();
break;
case DIV_CMD_INSTRUMENT:
chan[c.chan].ins=(unsigned char)c.value;
break;
case DIV_CMD_VOLUME:
if (c.chan<16) {
tmp=c.value&0x3f;
chan[c.chan].vol=tmp;
rWriteLo(c.chan,2,tmp);
} else {
tmp=c.value&0x0f;
chan[c.chan].vol=tmp;
rWriteFIFOVol(tmp);
}
break;
case DIV_CMD_GET_VOLUME:
return chan[c.chan].vol;
break;
case DIV_CMD_PITCH:
chan[c.chan].pitch=c.value;
chan[c.chan].freqChanged=true;
break;
case DIV_CMD_NOTE_PORTA: {
int destFreq=calcNoteFreq(c.chan,c.value2);
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=calcNoteFreq(c.chan,c.value+((chan[c.chan].std.willArp && !chan[c.chan].std.arpMode)?(chan[c.chan].std.arp):(0)));
chan[c.chan].freqChanged=true;
chan[c.chan].note=c.value;
break;
case DIV_CMD_PRE_PORTA:
if (chan[c.chan].active && c.value2) {
if (parent->song.resetMacroOnPorta) chan[c.chan].std.init(parent->getIns(chan[c.chan].ins));
}
chan[c.chan].inPorta=c.value;
break;
case DIV_CMD_STD_NOISE_MODE:
if (c.chan<16) rWriteLo(c.chan,3,c.value);
break;
case DIV_CMD_WAVE:
if (c.chan<16) rWriteHi(c.chan,3,c.value);
break;
case DIV_CMD_PANNING: {
tmp=0;
tmp|=(c.value&0x10)?1:0;
tmp|=(c.value&0x01)?2:0;
chan[c.chan].pan=tmp&3;
if (c.chan<16) {
rWriteHi(c.chan,2,isMuted[c.chan]?0:chan[c.chan].pan);
}
break;
}
case DIV_CMD_GET_VOLMAX:
if(c.chan<16) {
return 63;
} else {
return 15;
}
break;
case DIV_ALWAYS_SET_VOLUME:
return 0;
break;
default:
break;
}
return 1;
}
void* DivPlatformVERA::getChanState(int ch) {
return &chan[ch];
}
unsigned char* DivPlatformVERA::getRegisterPool() {
return regPool;
}
int DivPlatformVERA::getRegisterPoolSize() {
return 66;
}
void DivPlatformVERA::muteChannel(int ch, bool mute) {
isMuted[ch]=mute;
if (ch<16) {
rWriteHi(ch,2,mute?0:chan[ch].pan);
}
}
bool DivPlatformVERA::isStereo() {
return true;
}
void DivPlatformVERA::notifyInsDeletion(void* ins) {
for (int i=0; i<2; i++) {
chan[i].std.notifyInsDeletion((DivInstrument*)ins);
}
}
void DivPlatformVERA::poke(unsigned int addr, unsigned short val) {
regPool[addr] = (unsigned char)val;
}
void DivPlatformVERA::poke(std::vector<DivRegWrite>& wlist) {
for (auto &i: wlist) regPool[i.addr] = (unsigned char)i.val;
}
int DivPlatformVERA::init(DivEngine* p, int channels, int sugRate, unsigned int flags) {
for (int i=0; i<17; i++) {
isMuted[i]=false;
}
parent=p;
psg=new struct VERA_PSG;
pcm=new struct VERA_PCM;
dumpWrites=false;
skipRegisterWrites=false;
chipClock=25000000;
rate=chipClock/512;
reset();
return 17;
}
void DivPlatformVERA::quit() {
delete psg;
delete pcm;
}
DivPlatformVERA::~DivPlatformVERA() {
}

View file

@ -0,0 +1,78 @@
/**
* 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.
*/
#ifndef _VERA_H
#define _VERA_H
#include "../dispatch.h"
#include "../instrument.h"
#include "../macroInt.h"
struct VERA_PSG;
struct VERA_PCM;
class DivPlatformVERA: public DivDispatch {
protected:
struct Channel {
int freq, baseFreq, pitch, note;
unsigned char ins, pan;
bool active, freqChanged, inPorta;
int vol, outVol;
unsigned accum;
int noiseval;
DivMacroInt std;
struct PCMChannel {
int sample;
int out_l, out_r;
unsigned pos;
unsigned len;
unsigned char freq;
PCMChannel(): sample(-1), out_l(0), out_r(0), pos(0), len(0), freq(0) {}
} pcm;
Channel(): freq(0), baseFreq(0), pitch(0), note(0), ins(-1), pan(0), active(false), freqChanged(false), inPorta(false), vol(0), outVol(0), accum(0), noiseval(0) {}
};
Channel chan[17];
bool isMuted[17];
unsigned char regPool[66];
struct VERA_PSG* psg;
struct VERA_PCM* pcm;
int calcNoteFreq(int ch, int note);
friend void putDispatchChan(void*,int,int);
public:
void acquire(short* bufL, short* bufR, size_t start, size_t len);
int dispatch(DivCommand c);
void* getChanState(int chan);
unsigned char* getRegisterPool();
int getRegisterPoolSize();
void reset();
void tick();
void muteChannel(int ch, bool mute);
void notifyInsDeletion(void* ins);
bool isStereo();
void poke(unsigned int addr, unsigned short val);
void poke(std::vector<DivRegWrite>& wlist);
const char** getRegisterSheet();
const char* getEffectName(unsigned char effect);
int init(DivEngine* parent, int channels, int sugRate, unsigned int flags);
void quit();
~DivPlatformVERA();
};
#endif