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.
|
|
|
|
*/
|
|
|
|
|
2021-05-15 19:18:16 +00:00
|
|
|
#include "sms.h"
|
2021-05-15 21:42:48 +00:00
|
|
|
#include "../engine.h"
|
2021-05-15 19:18:16 +00:00
|
|
|
#include <math.h>
|
|
|
|
|
2022-01-25 21:52:55 +00:00
|
|
|
#define rWrite(v) {if (!skipRegisterWrites) {sn->write(v); if (dumpWrites) {addWrite(0x200,v);}}}
|
2022-01-17 18:29:35 +00:00
|
|
|
|
2022-01-28 05:55:51 +00:00
|
|
|
#define CHIP_DIVIDER 64
|
|
|
|
|
2022-02-02 07:14:42 +00:00
|
|
|
const char* regCheatSheetSN[]={
|
|
|
|
"DATA", "0",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2022-02-03 23:38:57 +00:00
|
|
|
const char** DivPlatformSMS::getRegisterSheet() {
|
|
|
|
return regCheatSheetSN;
|
|
|
|
}
|
|
|
|
|
2022-02-15 06:46:03 +00:00
|
|
|
const char* DivPlatformSMS::getEffectName(unsigned char effect) {
|
|
|
|
switch (effect) {
|
|
|
|
case 0x20:
|
|
|
|
return "20xy: Set noise mode (x: preset freq/ch3 freq; y: thin pulse/noise)";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-12-06 21:51:18 +00:00
|
|
|
void DivPlatformSMS::acquire(short* bufL, short* bufR, size_t start, size_t len) {
|
|
|
|
sn->sound_stream_update(bufL+start,len);
|
2021-12-06 10:21:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int DivPlatformSMS::acquireOne() {
|
2021-05-15 19:18:16 +00:00
|
|
|
short v;
|
|
|
|
sn->sound_stream_update(&v,1);
|
2021-12-06 10:21:42 +00:00
|
|
|
return v;
|
2021-05-15 19:18:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DivPlatformSMS::tick() {
|
2021-05-15 21:42:48 +00:00
|
|
|
for (int i=0; i<4; i++) {
|
|
|
|
chan[i].std.next();
|
2021-05-17 08:06:45 +00:00
|
|
|
if (chan[i].std.hadVol) {
|
2022-01-30 17:31:50 +00:00
|
|
|
chan[i].outVol=((chan[i].vol&15)*MIN(15,chan[i].std.vol))>>4;
|
2022-01-17 18:29:35 +00:00
|
|
|
rWrite(0x90|(i<<5)|(isMuted[i]?15:(15-(chan[i].outVol&15))));
|
2021-05-17 08:06:45 +00:00
|
|
|
}
|
2021-05-16 08:03:23 +00:00
|
|
|
if (chan[i].std.hadArp) {
|
2022-02-07 05:30:15 +00:00
|
|
|
if (!chan[i].inPorta) {
|
|
|
|
if (chan[i].std.arpMode) {
|
|
|
|
chan[i].baseFreq=NOTE_PERIODIC(chan[i].std.arp);
|
|
|
|
chan[i].actualNote=chan[i].std.arp;
|
|
|
|
} else {
|
|
|
|
chan[i].baseFreq=NOTE_PERIODIC(chan[i].note+chan[i].std.arp);
|
|
|
|
chan[i].actualNote=chan[i].note+chan[i].std.arp;
|
|
|
|
}
|
|
|
|
chan[i].freqChanged=true;
|
2021-05-16 17:47:05 +00:00
|
|
|
}
|
2021-12-29 07:08:50 +00:00
|
|
|
} else {
|
|
|
|
if (chan[i].std.arpMode && chan[i].std.finishedArp) {
|
2022-01-28 05:55:51 +00:00
|
|
|
chan[i].baseFreq=NOTE_PERIODIC(chan[i].note);
|
2022-01-30 22:10:15 +00:00
|
|
|
chan[i].actualNote=chan[i].note;
|
2021-12-29 07:08:50 +00:00
|
|
|
chan[i].freqChanged=true;
|
|
|
|
}
|
2021-05-16 08:03:23 +00:00
|
|
|
}
|
2021-12-29 07:08:50 +00:00
|
|
|
if (i==3) if (chan[i].std.hadDuty) {
|
|
|
|
snNoiseMode=chan[i].std.duty;
|
2021-05-17 06:51:14 +00:00
|
|
|
if (chan[i].std.duty<2) {
|
|
|
|
chan[3].freqChanged=false;
|
|
|
|
}
|
|
|
|
updateSNMode=true;
|
|
|
|
}
|
2021-05-15 21:42:48 +00:00
|
|
|
}
|
2021-05-15 19:18:16 +00:00
|
|
|
for (int i=0; i<3; i++) {
|
|
|
|
if (chan[i].freqChanged) {
|
2021-12-28 05:51:38 +00:00
|
|
|
chan[i].freq=parent->calcFreq(chan[i].baseFreq,chan[i].pitch,true);
|
2022-01-21 09:35:25 +00:00
|
|
|
if (chan[i].freq>1023) chan[i].freq=1023;
|
2022-01-30 22:10:15 +00:00
|
|
|
if (chan[i].actualNote>0x5d) chan[i].freq=0x01;
|
2022-01-17 18:29:35 +00:00
|
|
|
rWrite(0x80|i<<5|(chan[i].freq&15));
|
|
|
|
rWrite(chan[i].freq>>4);
|
2022-01-30 22:10:15 +00:00
|
|
|
if (i==2 && snNoiseMode&2) {
|
|
|
|
chan[3].baseFreq=chan[2].baseFreq;
|
|
|
|
chan[3].actualNote=chan[2].actualNote;
|
|
|
|
}
|
2021-05-15 19:18:16 +00:00
|
|
|
chan[i].freqChanged=false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (chan[3].freqChanged || updateSNMode) {
|
2021-12-29 07:08:50 +00:00
|
|
|
// seems arbitrary huh?
|
2022-01-29 05:20:27 +00:00
|
|
|
chan[3].freq=parent->calcFreq(chan[3].baseFreq,chan[3].pitch-1-(isRealSN?127:0),true);
|
2022-01-21 09:35:25 +00:00
|
|
|
if (chan[3].freq>1023) chan[3].freq=1023;
|
2022-01-30 22:10:15 +00:00
|
|
|
if (chan[3].actualNote>0x5d) chan[3].freq=0x01;
|
2021-05-15 19:18:16 +00:00
|
|
|
if (snNoiseMode&2) { // take period from channel 3
|
2022-02-07 23:34:04 +00:00
|
|
|
if (updateSNMode || resetPhase) {
|
|
|
|
if (snNoiseMode&1) {
|
|
|
|
rWrite(0xe7);
|
|
|
|
} else {
|
|
|
|
rWrite(0xe3);
|
|
|
|
}
|
2022-02-13 03:50:31 +00:00
|
|
|
if (updateSNMode) {
|
|
|
|
rWrite(0xdf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (chan[3].freqChanged) {
|
|
|
|
rWrite(0xc0|(chan[3].freq&15));
|
|
|
|
rWrite(chan[3].freq>>4);
|
2021-05-15 19:18:16 +00:00
|
|
|
}
|
|
|
|
} else { // 3 fixed values
|
2021-05-17 08:06:45 +00:00
|
|
|
unsigned char value;
|
|
|
|
if (chan[3].std.hadArp) {
|
|
|
|
if (chan[3].std.arpMode) {
|
|
|
|
value=chan[3].std.arp%12;
|
|
|
|
} else {
|
|
|
|
value=(chan[3].note+chan[3].std.arp)%12;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
value=chan[3].note%12;
|
|
|
|
}
|
|
|
|
if (value<3) {
|
|
|
|
value=2-value;
|
2022-02-14 04:49:01 +00:00
|
|
|
if (value!=oldValue || updateSNMode || resetPhase) {
|
|
|
|
oldValue=value;
|
|
|
|
rWrite(0xe0|value|((snNoiseMode&1)<<2));
|
|
|
|
}
|
2021-05-17 08:06:45 +00:00
|
|
|
}
|
2021-05-15 19:18:16 +00:00
|
|
|
}
|
2022-02-13 03:50:31 +00:00
|
|
|
chan[3].freqChanged=false;
|
2022-02-07 23:34:04 +00:00
|
|
|
updateSNMode=false;
|
2021-05-15 19:18:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int DivPlatformSMS::dispatch(DivCommand c) {
|
|
|
|
switch (c.cmd) {
|
|
|
|
case DIV_CMD_NOTE_ON:
|
2022-01-19 05:01:34 +00:00
|
|
|
if (c.value!=DIV_NOTE_NULL) {
|
2022-01-28 05:55:51 +00:00
|
|
|
chan[c.chan].baseFreq=NOTE_PERIODIC(c.value);
|
2022-01-19 05:01:34 +00:00
|
|
|
chan[c.chan].freqChanged=true;
|
|
|
|
chan[c.chan].note=c.value;
|
2022-01-30 22:10:15 +00:00
|
|
|
chan[c.chan].actualNote=c.value;
|
2022-01-19 05:01:34 +00:00
|
|
|
}
|
2021-05-15 19:18:16 +00:00
|
|
|
chan[c.chan].active=true;
|
2022-01-17 18:29:35 +00:00
|
|
|
rWrite(0x90|c.chan<<5|(isMuted[c.chan]?15:(15-(chan[c.chan].vol&15))));
|
2021-05-17 01:49:54 +00:00
|
|
|
chan[c.chan].std.init(parent->getIns(chan[c.chan].ins));
|
2021-05-15 19:18:16 +00:00
|
|
|
break;
|
|
|
|
case DIV_CMD_NOTE_OFF:
|
|
|
|
chan[c.chan].active=false;
|
2022-01-17 18:29:35 +00:00
|
|
|
rWrite(0x9f|c.chan<<5);
|
2021-05-15 21:59:57 +00:00
|
|
|
chan[c.chan].std.init(NULL);
|
2021-05-15 19:18:16 +00:00
|
|
|
break;
|
2022-02-08 18:11:04 +00:00
|
|
|
case DIV_CMD_NOTE_OFF_ENV:
|
|
|
|
case DIV_CMD_ENV_RELEASE:
|
|
|
|
chan[c.chan].std.release();
|
|
|
|
break;
|
2021-05-15 21:42:48 +00:00
|
|
|
case DIV_CMD_INSTRUMENT:
|
|
|
|
chan[c.chan].ins=c.value;
|
2021-05-17 01:49:54 +00:00
|
|
|
//chan[c.chan].std.init(parent->getIns(chan[c.chan].ins));
|
2021-05-15 21:42:48 +00:00
|
|
|
break;
|
2021-05-15 19:18:16 +00:00
|
|
|
case DIV_CMD_VOLUME:
|
2021-05-17 06:51:14 +00:00
|
|
|
if (chan[c.chan].vol!=c.value) {
|
|
|
|
chan[c.chan].vol=c.value;
|
2021-05-17 08:06:45 +00:00
|
|
|
if (!chan[c.chan].std.hasVol) {
|
|
|
|
chan[c.chan].outVol=c.value;
|
|
|
|
}
|
2022-01-17 18:29:35 +00:00
|
|
|
if (chan[c.chan].active) rWrite(0x90|c.chan<<5|(isMuted[c.chan]?15:(15-(chan[c.chan].vol&15))));
|
2021-05-17 06:51:14 +00:00
|
|
|
}
|
2021-05-15 19:18:16 +00:00
|
|
|
break;
|
2021-05-17 08:06:45 +00:00
|
|
|
case DIV_CMD_GET_VOLUME:
|
|
|
|
if (chan[c.chan].std.hasVol) {
|
|
|
|
return chan[c.chan].vol;
|
|
|
|
}
|
|
|
|
return chan[c.chan].outVol;
|
|
|
|
break;
|
2021-05-15 21:42:48 +00:00
|
|
|
case DIV_CMD_PITCH:
|
|
|
|
chan[c.chan].pitch=c.value;
|
|
|
|
chan[c.chan].freqChanged=true;
|
|
|
|
break;
|
2021-05-16 08:03:23 +00:00
|
|
|
case DIV_CMD_NOTE_PORTA: {
|
2022-01-28 05:55:51 +00:00
|
|
|
int destFreq=NOTE_PERIODIC(c.value2);
|
2021-05-16 08:03:23 +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;
|
2022-02-07 05:30:15 +00:00
|
|
|
if (return2) {
|
|
|
|
chan[c.chan].inPorta=false;
|
|
|
|
return 2;
|
|
|
|
}
|
2021-05-16 08:03:23 +00:00
|
|
|
break;
|
|
|
|
}
|
2021-05-15 19:18:16 +00:00
|
|
|
case DIV_CMD_STD_NOISE_MODE:
|
|
|
|
snNoiseMode=(c.value&1)|((c.value&16)>>3);
|
|
|
|
updateSNMode=true;
|
|
|
|
break;
|
2021-05-16 08:03:23 +00:00
|
|
|
case DIV_CMD_LEGATO:
|
2022-01-28 05:55:51 +00:00
|
|
|
chan[c.chan].baseFreq=NOTE_PERIODIC(c.value+((chan[c.chan].std.willArp && !chan[c.chan].std.arpMode)?(chan[c.chan].std.arp):(0)));
|
2021-05-16 08:03:23 +00:00
|
|
|
chan[c.chan].freqChanged=true;
|
|
|
|
chan[c.chan].note=c.value;
|
2022-01-30 22:10:15 +00:00
|
|
|
chan[c.chan].actualNote=c.value;
|
2021-05-16 08:03:23 +00:00
|
|
|
break;
|
2021-05-17 06:51:14 +00:00
|
|
|
case DIV_CMD_PRE_PORTA:
|
2022-02-09 03:49:52 +00:00
|
|
|
if (chan[c.chan].active && c.value2) {
|
2022-02-09 03:46:58 +00:00
|
|
|
if (parent->song.resetMacroOnPorta) chan[c.chan].std.init(parent->getIns(chan[c.chan].ins));
|
|
|
|
}
|
2022-02-07 05:30:15 +00:00
|
|
|
chan[c.chan].inPorta=c.value;
|
2021-05-17 06:51:14 +00:00
|
|
|
break;
|
2021-05-17 20:06:11 +00:00
|
|
|
case DIV_CMD_GET_VOLMAX:
|
|
|
|
return 15;
|
|
|
|
break;
|
2021-05-28 05:36:25 +00:00
|
|
|
case DIV_ALWAYS_SET_VOLUME:
|
|
|
|
return 0;
|
|
|
|
break;
|
2021-05-15 19:18:16 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-12-18 08:25:42 +00:00
|
|
|
void DivPlatformSMS::muteChannel(int ch, bool mute) {
|
|
|
|
isMuted[ch]=mute;
|
2022-01-17 18:29:35 +00:00
|
|
|
if (chan[ch].active) rWrite(0x90|ch<<5|(isMuted[ch]?15:(15-(chan[ch].outVol&15))));
|
2021-12-18 08:25:42 +00:00
|
|
|
}
|
|
|
|
|
2022-01-25 21:52:55 +00:00
|
|
|
void DivPlatformSMS::forceIns() {
|
|
|
|
for (int i=0; i<4; i++) {
|
|
|
|
if (chan[i].active) {
|
|
|
|
chan[i].insChanged=true;
|
|
|
|
chan[i].freqChanged=true;
|
|
|
|
}
|
|
|
|
}
|
2022-02-08 04:05:50 +00:00
|
|
|
updateSNMode=true;
|
2022-01-25 21:52:55 +00:00
|
|
|
}
|
|
|
|
|
2022-01-27 05:29:16 +00:00
|
|
|
void* DivPlatformSMS::getChanState(int ch) {
|
|
|
|
return &chan[ch];
|
|
|
|
}
|
|
|
|
|
2021-12-11 18:14:38 +00:00
|
|
|
void DivPlatformSMS::reset() {
|
|
|
|
for (int i=0; i<4; i++) {
|
|
|
|
chan[i]=DivPlatformSMS::Channel();
|
|
|
|
}
|
2022-01-25 23:46:27 +00:00
|
|
|
if (dumpWrites) {
|
|
|
|
addWrite(0xffffffff,0);
|
|
|
|
}
|
2021-12-11 18:14:38 +00:00
|
|
|
sn->device_start();
|
|
|
|
snNoiseMode=3;
|
2022-02-13 03:50:31 +00:00
|
|
|
rWrite(0xe7);
|
|
|
|
updateSNMode=false;
|
2022-02-14 04:49:01 +00:00
|
|
|
oldValue=0xff;
|
2021-12-11 18:14:38 +00:00
|
|
|
}
|
|
|
|
|
2021-12-08 05:33:00 +00:00
|
|
|
bool DivPlatformSMS::keyOffAffectsArp(int ch) {
|
2021-12-07 06:23:57 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-12-29 07:08:50 +00:00
|
|
|
bool DivPlatformSMS::keyOffAffectsPorta(int ch) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int DivPlatformSMS::getPortaFloor(int ch) {
|
|
|
|
return 12;
|
|
|
|
}
|
|
|
|
|
2022-01-14 00:36:02 +00:00
|
|
|
void DivPlatformSMS::notifyInsDeletion(void* ins) {
|
|
|
|
for (int i=0; i<4; i++) {
|
|
|
|
chan[i].std.notifyInsDeletion((DivInstrument*)ins);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-01 23:08:19 +00:00
|
|
|
void DivPlatformSMS::poke(unsigned int addr, unsigned short val) {
|
|
|
|
rWrite(val);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DivPlatformSMS::poke(std::vector<DivRegWrite>& wlist) {
|
|
|
|
for (DivRegWrite& i: wlist) rWrite(i.val);
|
|
|
|
}
|
|
|
|
|
2022-01-28 17:59:53 +00:00
|
|
|
void DivPlatformSMS::setFlags(unsigned int flags) {
|
2022-02-24 23:00:19 +00:00
|
|
|
if ((flags&3)==3) {
|
|
|
|
chipClock=COLOR_NTSC/2.0;
|
|
|
|
} else if ((flags&3)==2) {
|
2022-01-29 05:20:27 +00:00
|
|
|
chipClock=4000000;
|
|
|
|
} else if ((flags&3)==1) {
|
2022-01-28 05:55:51 +00:00
|
|
|
chipClock=COLOR_PAL*4.0/5.0;
|
2021-12-08 06:56:40 +00:00
|
|
|
} else {
|
2022-01-28 05:55:51 +00:00
|
|
|
chipClock=COLOR_NTSC;
|
2021-12-08 06:56:40 +00:00
|
|
|
}
|
2022-02-07 23:34:04 +00:00
|
|
|
resetPhase=!(flags&16);
|
2022-01-29 05:20:27 +00:00
|
|
|
if (sn!=NULL) delete sn;
|
2022-02-07 23:34:04 +00:00
|
|
|
switch ((flags>>2)&3) {
|
2022-01-29 05:20:27 +00:00
|
|
|
case 1: // TI
|
|
|
|
sn=new sn76496_base_device(0x4000, 0x4000, 0x01, 0x02, true, 1, false, true);
|
|
|
|
isRealSN=true;
|
|
|
|
break;
|
|
|
|
case 2: // TI+Atari
|
|
|
|
sn=new sn76496_base_device(0x4000, 0x0f35, 0x01, 0x02, true, 1, false, true);
|
|
|
|
isRealSN=true;
|
|
|
|
break;
|
|
|
|
case 3: // Game Gear (not fully emulated yet!)
|
|
|
|
sn=new sn76496_base_device(0x8000, 0x8000, 0x01, 0x08, false, 1, false, false);
|
|
|
|
isRealSN=false;
|
|
|
|
break;
|
|
|
|
default: // Sega
|
|
|
|
sn=new sn76496_base_device(0x8000, 0x8000, 0x01, 0x08, false, 1, false, false);
|
|
|
|
isRealSN=false;
|
|
|
|
break;
|
|
|
|
}
|
2022-01-28 05:55:51 +00:00
|
|
|
rate=chipClock/16;
|
2021-12-15 22:32:08 +00:00
|
|
|
}
|
|
|
|
|
2022-01-28 17:59:53 +00:00
|
|
|
int DivPlatformSMS::init(DivEngine* p, int channels, int sugRate, unsigned int flags) {
|
2021-12-15 22:32:08 +00:00
|
|
|
parent=p;
|
2022-01-17 04:21:27 +00:00
|
|
|
dumpWrites=false;
|
2021-12-21 21:02:31 +00:00
|
|
|
skipRegisterWrites=false;
|
2022-02-07 23:34:04 +00:00
|
|
|
resetPhase=false;
|
2022-02-14 04:49:01 +00:00
|
|
|
oldValue=0xff;
|
2021-12-18 08:25:42 +00:00
|
|
|
for (int i=0; i<4; i++) {
|
|
|
|
isMuted[i]=false;
|
|
|
|
}
|
2022-01-29 05:20:27 +00:00
|
|
|
sn=NULL;
|
2022-01-28 17:59:53 +00:00
|
|
|
setFlags(flags);
|
2021-12-11 18:14:38 +00:00
|
|
|
reset();
|
2021-05-15 19:18:16 +00:00
|
|
|
return 4;
|
|
|
|
}
|
2021-12-15 05:37:27 +00:00
|
|
|
|
|
|
|
void DivPlatformSMS::quit() {
|
2022-01-29 05:20:27 +00:00
|
|
|
if (sn!=NULL) delete sn;
|
2021-12-15 05:37:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DivPlatformSMS::~DivPlatformSMS() {
|
2022-01-17 18:29:35 +00:00
|
|
|
}
|