Prepare for C140

This commit is contained in:
cam900 2023-08-08 21:27:12 +09:00
parent cc8b5d28a8
commit c34233cf4f
7 changed files with 867 additions and 6 deletions

View File

@ -507,6 +507,8 @@ src/engine/platform/sound/d65modified.c
src/engine/platform/sound/ted-sound.c
src/engine/platform/sound/c140.c
src/engine/platform/oplAInterface.cpp
src/engine/platform/ym2608Interface.cpp
src/engine/platform/ym2610Interface.cpp
@ -599,6 +601,7 @@ src/engine/platform/sm8521.cpp
src/engine/platform/pv1000.cpp
src/engine/platform/k053260.cpp
src/engine/platform/ted.cpp
src/engine/platform/c140.cpp
src/engine/platform/pcmdac.cpp
src/engine/platform/dummy.cpp

View File

@ -0,0 +1,481 @@
/**
* Furnace Tracker - multi-system chiptune tracker
* Copyright (C) 2021-2023 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 "c140.h"
#include "../engine.h"
#include "../../ta-log.h"
#include <math.h>
#include <map>
#define CHIP_FREQBASE 12582912
#define rWrite(a,v) {if(!skipRegisterWrites) {writes.push(QueuedWrite(a,v)); if(dumpWrites) addWrite(a,v); }}
const char* regCheatSheetC140[]={
"CHx_RVol", "00+x*10",
"CHx_LVol", "01+x*10",
"CHx_FreqH", "02+x*10",
"CHx_FreqL", "03+x*10",
"CHx_Bank", "04+x*10",
"CHx_Ctrl", "05+x*10",
"CHx_StartH", "06+x*10",
"CHx_StartL", "07+x*10",
"CHx_EndH", "08+x*10",
"CHx_EndL", "09+x*10",
"CHx_LoopH", "0A+x*10",
"CHx_LoopL", "0B+x*10",
"Timer", "1FA",
"IRQ", "1FE",
NULL
};
const char** DivPlatformC140::getRegisterSheet() {
return regCheatSheetC140;
}
void DivPlatformC140::acquire(short** buf, size_t len) {
for (size_t h=0; h<len; h++) {
while (!writes.empty()) {
QueuedWrite w=writes.front();
c140_write(&c140, w.addr,w.val);
regPool[w.addr&0x1ff]=w.val;
writes.pop();
}
c140_tick(&c140, 1);
if (c140.lout<-32768) c140.lout=-32768;
if (c140.lout>32767) c140.lout=32767;
if (c140.rout<-32768) c140.rout=-32768;
if (c140.rout>32767) c140.rout=32767;
buf[0][h]=c140.lout;
buf[1][h]=c140.rout;
for (int i=0; i<24; i++) {
oscBuf[i]->data[oscBuf[i]->needle++]=(c140.voice[i].lout+c140.voice[i].rout)>>9;
}
}
}
void DivPlatformC140::tick(bool sysTick) {
for (int i=0; i<24; i++) {
chan[i].std.next();
if (chan[i].std.vol.had) {
chan[i].outVol=(chan[i].vol*MIN(chan[i].macroVolMul,chan[i].std.vol.val))/chan[i].macroVolMul;
chan[i].chVolL=(chan[i].outVol*chan[i].chPanL)/255;
chan[i].chVolR=(chan[i].outVol*chan[i].chPanR)/255;
rWrite(1+(i<<4),chan[i].chVolL);
rWrite(0+(i<<4),chan[i].chVolR);
}
if (NEW_ARP_STRAT) {
chan[i].handleArp();
} else if (chan[i].std.arp.had) {
if (!chan[i].inPorta) {
chan[i].baseFreq=NOTE_FREQUENCY(parent->calcArp(chan[i].note,chan[i].std.arp.val));
}
chan[i].freqChanged=true;
}
if (chan[i].std.pitch.had) {
if (chan[i].std.pitch.mode) {
chan[i].pitch2+=chan[i].std.pitch.val;
CLAMP_VAR(chan[i].pitch2,-32768,32767);
} else {
chan[i].pitch2=chan[i].std.pitch.val;
}
chan[i].freqChanged=true;
}
if (chan[i].std.panL.had) {
chan[i].chPanL=chan[i].std.panL.val&255;
chan[i].chVolL=(chan[i].outVol*chan[i].chPanL)/255;
rWrite(1+(i<<4),chan[i].chVolL);
}
if (chan[i].std.panR.had) {
chan[i].chPanR=chan[i].std.panR.val&255;
chan[i].chVolR=(chan[i].outVol*chan[i].chPanR)/255;
rWrite(0+(i<<4),chan[i].chVolR);
}
if (chan[i].std.phaseReset.had) {
if ((chan[i].std.phaseReset.val==1) && chan[i].active) {
chan[i].audPos=0;
chan[i].setPos=true;
}
}
if (chan[i].setPos) {
// force keyon
chan[i].keyOn=true;
chan[i].setPos=false;
} else {
chan[i].audPos=0;
}
if (chan[i].freqChanged || chan[i].keyOn || chan[i].keyOff) {
DivSample* s=parent->getSample(chan[i].sample);
unsigned char ctrl=0;
double off=(s->centerRate>=1)?((double)s->centerRate/8363.0):1.0;
chan[i].freq=(int)(off*parent->calcFreq(chan[i].baseFreq,chan[i].pitch,chan[i].fixedArp?chan[i].baseNoteOverride:chan[i].arpOff,chan[i].fixedArp,false,2,chan[i].pitch2,chipClock,CHIP_FREQBASE));
if (chan[i].freq<0) chan[i].freq=0;
if (chan[i].freq>65535) chan[i].freq=65535;
ctrl|=(chan[i].active?0x80:0)|((s->isLoopable())?0x10:0);
if (chan[i].keyOn) {
unsigned int start=0;
unsigned int loop=0;
unsigned int end=0;
if (chan[i].sample>=0 && chan[i].sample<parent->song.sampleLen) {
start=sampleOff[chan[i].sample];
end=MIN(start+s->getCurBufLen(),getSampleMemCapacity()-1);
}
if (chan[i].audPos>0) {
start=start+MIN(chan[i].audPos,s->length8);
}
if (s->isLoopable()) {
loop=start+s->loopStart;
}
rWrite(0x05+i*16,ctrl&~0x80); // force keyoff first
rWrite(0x06+i*16,(start>>8)&0xff);
rWrite(0x07+i*16,start&0xff);
rWrite(0x08+i*16,(end>>8)&0xff);
rWrite(0x09+i*16,end&0xff);
rWrite(0x0a+i*16,(loop>>8)&0xff);
rWrite(0x0b+i*16,loop&0xff);
if (!chan[i].std.vol.had) {
chan[i].outVol=chan[i].vol;
chan[i].chVolL=(chan[i].outVol*chan[i].chPanL)/255;
chan[i].chVolR=(chan[i].outVol*chan[i].chPanR)/255;
rWrite(1+(i<<4),chan[i].chVolL);
rWrite(0+(i<<4),chan[i].chVolR);
}
chan[i].keyOn=false;
}
if (chan[i].keyOff) {
chan[i].keyOff=false;
}
if (chan[i].freqChanged) {
rWrite(0x02+i*16,chan[i].freq&0xff);
rWrite(0x03+i*16,chan[i].freq>>8);
chan[i].freqChanged=false;
}
rWrite(0x05+i*16,ctrl);
}
}
}
int DivPlatformC140::dispatch(DivCommand c) {
switch (c.cmd) {
case DIV_CMD_NOTE_ON: {
DivInstrument* ins=parent->getIns(chan[c.chan].ins,DIV_INS_AMIGA);
chan[c.chan].isNewYMZ=ins->type==DIV_INS_YMZ280B;
chan[c.chan].macroVolMul=ins->type==DIV_INS_AMIGA?64:255;
if (c.value!=DIV_NOTE_NULL) {
chan[c.chan].sample=ins->amiga.getSample(c.value);
c.value=ins->amiga.getFreq(c.value);
}
if (c.value!=DIV_NOTE_NULL) {
chan[c.chan].baseFreq=NOTE_FREQUENCY(c.value);
}
if (chan[c.chan].sample<0 || chan[c.chan].sample>=parent->song.sampleLen) {
chan[c.chan].sample=-1;
}
if (c.value!=DIV_NOTE_NULL) {
chan[c.chan].freqChanged=true;
chan[c.chan].note=c.value;
}
chan[c.chan].active=true;
chan[c.chan].keyOn=true;
chan[c.chan].macroInit(ins);
if (!parent->song.brokenOutVol && !chan[c.chan].std.vol.will) {
chan[c.chan].outVol=chan[c.chan].vol;
chan[c.chan].chVolL=(chan[c.chan].outVol*chan[c.chan].chPanL)/255;
chan[c.chan].chVolR=(chan[c.chan].outVol*chan[c.chan].chPanR)/255;
rWrite(1+(c.chan<<4),chan[c.chan].chVolL);
rWrite(0+(c.chan<<4),chan[c.chan].chVolR);
}
break;
}
case DIV_CMD_NOTE_OFF:
chan[c.chan].sample=-1;
chan[c.chan].active=false;
chan[c.chan].keyOff=true;
chan[c.chan].macroInit(NULL);
break;
case DIV_CMD_NOTE_OFF_ENV:
case DIV_CMD_ENV_RELEASE:
chan[c.chan].std.release();
break;
case DIV_CMD_INSTRUMENT:
if (chan[c.chan].ins!=c.value || c.value2==1) {
chan[c.chan].ins=c.value;
}
break;
case DIV_CMD_VOLUME:
chan[c.chan].vol=c.value;
if (!chan[c.chan].std.vol.has) {
chan[c.chan].outVol=c.value;
}
chan[c.chan].chVolL=(c.value*chan[c.chan].chPanL)/255;
chan[c.chan].chVolR=(c.value*chan[c.chan].chPanR)/255;
rWrite(1+(c.chan<<4),chan[c.chan].chVolL);
rWrite(0+(c.chan<<4),chan[c.chan].chVolR);
break;
case DIV_CMD_GET_VOLUME:
if (chan[c.chan].std.vol.has) {
return chan[c.chan].vol;
}
return chan[c.chan].outVol;
break;
case DIV_CMD_PANNING:
chan[c.chan].chPanL=c.value;
chan[c.chan].chPanR=c.value2;
chan[c.chan].chVolL=(chan[c.chan].outVol*chan[c.chan].chPanL)/255;
chan[c.chan].chVolR=(chan[c.chan].outVol*chan[c.chan].chPanR)/255;
rWrite(1+(c.chan<<4),chan[c.chan].chVolL);
rWrite(0+(c.chan<<4),chan[c.chan].chVolR);
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_FREQUENCY(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=NOTE_FREQUENCY(c.value+((HACKY_LEGATO_MESS)?(chan[c.chan].std.arp.val-12):(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].macroInit(parent->getIns(chan[c.chan].ins,DIV_INS_AMIGA));
}
if (!chan[c.chan].inPorta && c.value && !parent->song.brokenPortaArp && chan[c.chan].std.arp.will && !NEW_ARP_STRAT) chan[c.chan].baseFreq=NOTE_FREQUENCY(chan[c.chan].note);
chan[c.chan].inPorta=c.value;
break;
case DIV_CMD_SAMPLE_POS:
chan[c.chan].audPos=c.value;
chan[c.chan].setPos=true;
break;
case DIV_CMD_GET_VOLMAX:
return 255;
break;
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;
case DIV_ALWAYS_SET_VOLUME:
return 1;
break;
default:
break;
}
return 1;
}
void DivPlatformC140::muteChannel(int ch, bool mute) {
isMuted[ch]=mute;
}
void DivPlatformC140::forceIns() {
for (int i=0; i<24; i++) {
chan[i].insChanged=true;
chan[i].freqChanged=true;
chan[i].sample=-1;
}
}
void* DivPlatformC140::getChanState(int ch) {
return &chan[ch];
}
DivMacroInt* DivPlatformC140::getChanMacroInt(int ch) {
return &chan[ch].std;
}
DivDispatchOscBuffer* DivPlatformC140::getOscBuffer(int ch) {
return oscBuf[ch];
}
void DivPlatformC140::reset() {
while (!writes.empty()) writes.pop();
memset(regPool,0,512);
c140_reset(&c140);
for (int i=0; i<24; i++) {
chan[i]=DivPlatformC140::Channel();
chan[i].std.setEngine(parent);
rWrite(0x05+i*16,0);
}
}
int DivPlatformC140::getOutputCount() {
return 2;
}
void DivPlatformC140::notifyInsChange(int ins) {
for (int i=0; i<24; i++) {
if (chan[i].ins==ins) {
chan[i].insChanged=true;
}
}
}
void DivPlatformC140::notifyWaveChange(int wave) {
// TODO when wavetables are added
// TODO they probably won't be added unless the samples reside in RAM
}
void DivPlatformC140::notifyInsDeletion(void* ins) {
for (int i=0; i<24; i++) {
chan[i].std.notifyInsDeletion((DivInstrument*)ins);
}
}
void DivPlatformC140::poke(unsigned int addr, unsigned short val) {
rWrite(addr,val);
}
void DivPlatformC140::poke(std::vector<DivRegWrite>& wlist) {
for (DivRegWrite& i: wlist) rWrite(i.addr,i.val);
}
unsigned char* DivPlatformC140::getRegisterPool() {
return regPool;
}
int DivPlatformC140::getRegisterPoolSize() {
return 512;
}
const void* DivPlatformC140::getSampleMem(int index) {
return index == 0 ? sampleMem : NULL;
}
size_t DivPlatformC140::getSampleMemCapacity(int index) {
return index == 0 ? 16777216 : 0;
}
size_t DivPlatformC140::getSampleMemUsage(int index) {
return index == 0 ? sampleMemLen : 0;
}
bool DivPlatformC140::isSampleLoaded(int index, int sample) {
if (index!=0) return false;
if (sample<0 || sample>255) return false;
return sampleLoaded[sample];
}
void DivPlatformC140::renderSamples(int sysID) {
memset(sampleMem,0,getSampleMemCapacity());
memset(sampleOff,0,256*sizeof(unsigned int));
memset(sampleLoaded,0,256*sizeof(bool));
size_t memPos=0;
for (int i=0; i<parent->song.sampleLen; i++) {
DivSample* s=parent->song.sample[i];
if (!s->renderOn[0][sysID]) {
sampleOff[i]=0;
continue;
}
unsigned int length=s->length16;
// fit sample size to single bank size
if (length>(131072)) {
length=131072;
}
if ((memPos&0xfe0000)!=((memPos+length)&0xfe0000)) {
memPos=((memPos+0x1ffff)&0xfe0000);
}
if (memPos>=(getSampleMemCapacity())) {
logW("out of C140 memory for sample %d!",i);
break;
}
if (memPos+length>=(getSampleMemCapacity())) {
memcpy(sampleMem+(memPos/sizeof(short)),s->data16,(getSampleMemCapacity())-memPos);
logW("out of C140 memory for sample %d!",i);
} else {
memcpy(sampleMem+(memPos/sizeof(short)),s->data16,length);
}
sampleOff[i]=memPos;
sampleLoaded[i]=true;
memPos+=length;
}
sampleMemLen=memPos+256;
}
void DivPlatformC140::setChipModel(int type) {
chipType=type;
}
void DivPlatformC140::setFlags(const DivConfig& flags) {
chipClock = 8192000;
CHECK_CUSTOM_CLOCK;
rate=chipClock/192;
for (int i=0; i<24; i++) {
oscBuf[i]->rate=rate;
}
}
int DivPlatformC140::init(DivEngine* p, int channels, int sugRate, const DivConfig& flags) {
parent=p;
dumpWrites=false;
skipRegisterWrites=false;
for (int i=0; i<24; i++) {
isMuted[i]=false;
oscBuf[i]=new DivDispatchOscBuffer;
}
sampleMem=new unsigned char[getSampleMemCapacity()];
sampleMemLen=0;
c140_init(&c140);
setFlags(flags);
reset();
return 24;
}
void DivPlatformC140::quit() {
delete[] sampleMem;
for (int i=0; i<24; i++) {
delete oscBuf[i];
}
}

102
src/engine/platform/c140.h Normal file
View File

@ -0,0 +1,102 @@
/**
* Furnace Tracker - multi-system chiptune tracker
* Copyright (C) 2021-2023 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 _C140_H
#define _C140_H
#include "../dispatch.h"
#include "sound/c140.h"
#include "../fixedQueue.h"
class DivPlatformC140: public DivDispatch {
struct Channel: public SharedChannel<int> {
unsigned int audPos;
int sample, wave;
int panning;
bool setPos, isNewYMZ;
int chPanL, chPanR;
int chVolL, chVolR;
int macroVolMul;
Channel():
SharedChannel<int>(255),
audPos(0),
sample(-1),
wave(-1),
panning(8),
setPos(false),
isNewYMZ(false),
chPanL(255),
chPanR(255),
chVolL(255),
chVolR(255),
macroVolMul(64) {}
};
Channel chan[24];
DivDispatchOscBuffer* oscBuf[24];
bool isMuted[24];
int chipType;
unsigned int sampleOff[256];
bool sampleLoaded[256];
unsigned char* sampleMem;
size_t sampleMemLen;
struct QueuedWrite {
unsigned short addr;
unsigned char val;
bool addrOrVal;
QueuedWrite(): addr(0), val(0), addrOrVal(false) {}
QueuedWrite(unsigned short a, unsigned char v): addr(a), val(v), addrOrVal(false) {}
};
FixedQueue<QueuedWrite,2048> writes;
struct c140_t c140;
unsigned char regPool[512];
friend void putDispatchChip(void*,int);
friend void putDispatchChan(void*,int,int);
public:
void acquire(short** buf, size_t len);
int dispatch(DivCommand c);
void* getChanState(int chan);
DivMacroInt* getChanMacroInt(int ch);
DivDispatchOscBuffer* getOscBuffer(int chan);
unsigned char* getRegisterPool();
int getRegisterPoolSize();
void reset();
void forceIns();
void tick(bool sysTick=true);
void muteChannel(int ch, bool mute);
int getOutputCount();
void setChipModel(int type);
void notifyInsChange(int ins);
void notifyWaveChange(int wave);
void notifyInsDeletion(void* ins);
void poke(unsigned int addr, unsigned short val);
void poke(std::vector<DivRegWrite>& wlist);
const char** getRegisterSheet();
const void* getSampleMem(int index = 0);
size_t getSampleMemCapacity(int index = 0);
size_t getSampleMemUsage(int index = 0);
bool isSampleLoaded(int index, int sample);
void renderSamples(int chipID);
void setFlags(const DivConfig& flags);
int init(DivEngine* parent, int channels, int sugRate, const DivConfig& flags);
void quit();
};
#endif

View File

@ -0,0 +1,183 @@
/*
============================================================================
Namco C140 sound emulator
by cam900
This file is licensed under zlib license.
============================================================================
zlib License
(C) 2023-present cam900 and contributors
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
============================================================================
TODO:
- unknown registers (Bit 6 of control register, etc)
- Internal timer
*/
#include "c140.h"
static int c140_max(int a, int b) { return (a > b) ? a : b; }
static int c140_min(int a, int b) { return (a < b) ? a : b; }
static int c140_clamp(int v, int min, int max) { return c140_min(c140_max(v,min),max); }
static int c140_bit(int val, int bit) { return (val >> bit) & 1; }
void c140_tick(struct c140_t *c140, const int cycle)
{
for (int i = 0; i < 24; i++)
{
c140_voice_tick(c140, i, cycle);
c140->lout += c140->voice[i].lout;
c140->rout += c140->voice[i].rout;
}
// scale as 16bit
c140->lout >>= 5;
c140->rout >>= 5;
}
void c140_voice_tick(struct c140_t *c140, const unsigned char voice, const int cycle)
{
struct c140_voice_t *c140_voice = &c140->voice[voice];
if (c140_voice->busy && c140_voice->keyon)
{
for (int c = 0; c < cycle; c++)
{
c140_voice->frac += c140_voice->freq;
if (c140_voice->frac > 0xffff)
{
c140_voice->addr += c140_voice->frac >> 16;
if (c140_voice->addr > c140_voice->end_addr)
{
if (c140_voice->loop)
{
c140_voice->addr = (c140_voice->addr + c140_voice->end_addr) - c140_voice->loop_addr;
}
else
{
c140_voice->keyon = false;
}
}
c140_voice->frac &= 0xffff;
}
// fetch 12 bit sample
signed short s1 = c140->read_sample(c140_voice->bank, c140_voice->addr) & ~0xf;
signed short s2 = c140->read_sample(c140_voice->bank, c140_voice->addr + 1) & ~0xf;
if (c140_voice->compressed)
{
s1 = c140->mulaw[(s1 >> 8) & 0xff];
s2 = c140->mulaw[(s2 >> 8) & 0xff];
}
// interpolate
signed int sample = s1 + (((c140_voice->frac) * (s2 - s1)) >> 16);
c140_voice->lout = sample * c140_voice->lvol;
c140_voice->rout = sample * c140_voice->rvol;
}
}
else
{
c140_voice->lout = 0;
c140_voice->rout = 0;
}
}
void c140_keyon(struct c140_voice_t *c140_voice)
{
c140_voice->busy = true;
c140_voice->keyon = true;
c140_voice->frac = 0;
c140_voice->addr = c140_voice->start_addr;
}
void c140_init(struct c140_t *c140)
{
// u-law table verified from Wii Virtual Console Arcade Starblade
for (int i = 0; i < 256; i++)
{
int j = (signed char)i;
signed char s1 = j & 7;
signed char s2 = abs(j >> 3) & 31;
c140->mulaw[i] = 0x80 << s1 & 0xff00;
c140->mulaw[i] += s2 << (s1 ? s1 + 3 : 4);
if (j < 0)
c140->mulaw[i] = -c140->mulaw[i];
}
}
void c140_reset(struct c140_t *c140)
{
for (int i = 0; i < 24; i++)
{
c140->voice[i].busy = false;
c140->voice[i].keyon = false;
c140->voice[i].freq = 0;
c140->voice[i].bank = 0;
c140->voice[i].start_addr = 0;
c140->voice[i].loop_addr = 0;
c140->voice[i].end_addr = 0;
c140->voice[i].lvol = 0;
c140->voice[i].rvol = 0;
c140->voice[i].compressed = false;
c140->voice[i].loop = false;
c140->voice[i].addr = 0;
c140->voice[i].frac = 0;
c140->voice[i].lout = 0;
c140->voice[i].rout = 0;
}
}
void c140_write(struct c140_t *c140, const unsigned short addr, const unsigned char data)
{
// voice register
if (addr < 0x180)
{
struct c140_voice_t *voice = &c140->voice[addr >> 4];
switch (addr & 0xf)
{
case 0x0: voice->rvol = data; break;
case 0x1: voice->lvol = data; break;
case 0x2: voice->freq = (voice->freq & ~0xff00) | (unsigned int)(data << 8); break;
case 0x3: voice->freq = (voice->freq & ~0x00ff) | data; break;
case 0x4: voice->bank = data; break;
case 0x5:
voice->compressed = c140_bit(data, 3);
voice->loop = c140_bit(data, 4);
if (data & 0x80)
c140_keyon(voice);
else
voice->busy = false;
break;
case 0x6: voice->start_addr = (voice->start_addr & ~0xff00) | (unsigned int)(data << 8); break;
case 0x7: voice->start_addr = (voice->start_addr & ~0x00ff) | data; break;
case 0x8: voice->end_addr = (voice->end_addr & ~0xff00) | (unsigned int)(data << 8); break;
case 0x9: voice->end_addr = (voice->end_addr & ~0x00ff) | data; break;
case 0xa: voice->loop_addr = (voice->loop_addr & ~0xff00) | (unsigned int)(data << 8); break;
case 0xb: voice->loop_addr = (voice->loop_addr & ~0x00ff) | data; break;
default: break;
}
}
// Timer
}

View File

@ -0,0 +1,91 @@
/*
============================================================================
Namco C140 sound emulator
by cam900
This file is licensed under zlib license.
============================================================================
zlib License
(C) 2023-present cam900 and contributors
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
============================================================================
TODO:
- unknown registers (Bit 6 of control register, etc)
- Internal timer
*/
#ifndef _C140_EMU_H
#define _C140_EMU_H
#include <stdbool.h>
#ifdef __cplusplus
extern "C"
{
#endif
struct c140_voice_t
{
bool busy; // busy flag
bool keyon; // key on flag
unsigned short freq; // sample frequency
unsigned char bank; // sample bank
unsigned short start_addr; // sample start address
unsigned short loop_addr; // sample loop address
unsigned short end_addr; // sample end address
int lvol, rvol; // left/right volume
bool compressed; // compressed sample flag
bool loop; // loop flag
unsigned short addr; // sample address
int frac; // frequency counter (.16 fixed point)
int lout, rout; // left/right output
};
struct c140_t
{
struct c140_voice_t voice[24];
signed int lout, rout;
signed short mulaw[256];
unsigned short (*read_sample)(unsigned char bank, unsigned short addr);
};
void c140_tick(struct c140_t *c140, const int cycle);
void c140_voice_tick(struct c140_t *c140, const unsigned char voice, const int cycle);
void c140_keyon(struct c140_voice_t *c140_voice);
void c140_write(struct c140_t *c140, const unsigned short addr, const unsigned char data);
void c140_init(struct c140_t *c140);
void c140_reset(struct c140_t *c140);
#ifdef __cplusplus
} // extern "C"
#endif
#endif // _C140_EMU_H

View File

@ -192,12 +192,10 @@ const char* aboutLine[]={
"mzpokeysnd POKEY emulator by Michael Borisov",
"ASAP POKEY emulator by Piotr Fusik",
"ported by laoo to C++",
"K005289 emulator by cam900",
"Namco 163 emulator by cam900",
"Seta X1-010 emulator by cam900",
"Konami VRC6 emulator by cam900",
"Konami SCC emulator by cam900",
"MSM6295 emulator by cam900",
"vgsound_emu (second version, modified version) by cam900",
"SM8521 emulator (modified version) by cam900",
"D65010G031 emulator (modified version) by cam900",
"Namco C140 emulator by cam900",
"",
"greetings to:",
"NEOART Costa Rica",

View File

@ -208,6 +208,9 @@ TAParamResult pVersion(String) {
printf("- MAME GA20 core by Acho A. Tang, R. Belmont, Valley Bell (BSD 3-clause)\n");
printf("- Atari800 mzpokeysnd POKEY emulator by Michael Borisov (GPLv2)\n");
printf("- ASAP POKEY emulator by Piotr Fusik ported to C++ by laoo (GPLv2)\n");
printf("- SM8521 emulator (modified version) by cam900 (zlib license)\n");
printf("- D65010G031 emulator (modified version) by cam900 (zlib license)\n");
printf("- C140 emulator by cam900 (zlib license)\n");
return TA_PARAM_QUIT;
}