| 1 |
/** |
| 2 |
* Furnace Tracker - multi-system chiptune tracker |
| 3 |
* Copyright (C) 2021-2025 tildearrow and contributors |
| 4 |
* |
| 5 |
* This program is free software; you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation; either version 2 of the License, or |
| 8 |
* (at your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License along |
| 16 |
* with this program; if not, write to the Free Software Foundation, Inc., |
| 17 |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 |
*/ |
| 19 |
|
| 20 |
#ifndef _ES5506_H |
| 21 |
#define _ES5506_H |
| 22 |
|
| 23 |
#include "../dispatch.h" |
| 24 |
#include "../engine.h" |
| 25 |
#include "../../fixedQueue.h" |
| 26 |
#include "../macroInt.h" |
| 27 |
#include "../sample.h" |
| 28 |
#include "vgsound_emu/src/es550x/es5506.hpp" |
| 29 |
|
| 30 |
class DivPlatformES5506: public DivDispatch, public es550x_intf { |
| 31 |
struct Channel : public SharedChannel<int> { |
| 32 |
struct PCM { |
| 33 |
bool isNoteMap; |
| 34 |
int index, next; |
| 35 |
int note; |
| 36 |
double freqOffs; |
| 37 |
double nextFreqOffs; |
| 38 |
bool pause, direction; |
| 39 |
unsigned int bank; |
| 40 |
unsigned int start; |
| 41 |
unsigned int end; |
| 42 |
unsigned int length; |
| 43 |
unsigned int loopStart; |
| 44 |
unsigned int loopEnd; |
| 45 |
unsigned int nextPos; |
| 46 |
bool setPos; |
| 47 |
DivSampleLoopMode loopMode; |
| 48 |
PCM(): |
| 49 |
isNoteMap(false), |
| 50 |
index(-1), |
| 51 |
next(-1), |
| 52 |
note(0), |
| 53 |
freqOffs(1.0), |
| 54 |
nextFreqOffs(1.0), |
| 55 |
pause(false), |
| 56 |
direction(false), |
| 57 |
bank(0), |
| 58 |
start(0), |
| 59 |
end(0), |
| 60 |
length(0), |
| 61 |
loopStart(0), |
| 62 |
loopEnd(0), |
| 63 |
nextPos(0), |
| 64 |
setPos(false), |
| 65 |
loopMode(DIV_SAMPLE_LOOP_MAX) {} |
| 66 |
} pcm; |
| 67 |
int nextFreq, nextNote, currNote, wave; |
| 68 |
int volMacroMax, panMacroMax; |
| 69 |
bool useWave, isReverseLoop; |
| 70 |
unsigned short cr, crWriteVal, crDirVal; |
| 71 |
bool crChanged, crDirValChanged, crDirValInit; |
| 72 |
|
| 73 |
struct NoteChanged { // Note changed flags |
| 74 |
union { // pack flag bits in single byte |
| 75 |
struct { // flag bits |
| 76 |
unsigned char offs: 1; // frequency offset |
| 77 |
unsigned char note: 1; // note |
| 78 |
unsigned char freq: 1; // base frequency |
| 79 |
unsigned char dummy: 5; // dummy for bit padding |
| 80 |
}; |
| 81 |
unsigned char changed; // Packed flags are stored here |
| 82 |
}; |
| 83 |
|
| 84 |
NoteChanged() : |
| 85 |
changed(0) {} |
| 86 |
} noteChanged; |
| 87 |
|
| 88 |
struct VolChanged { // Volume changed flags |
| 89 |
union { // pack flag bits in single byte |
| 90 |
struct { // flag bits |
| 91 |
unsigned char lVol: 1; // left volume |
| 92 |
unsigned char rVol: 1; // right volume |
| 93 |
unsigned char ca: 1; // Channel assignment |
| 94 |
unsigned char dummy: 5; // dummy for bit padding |
| 95 |
}; |
| 96 |
unsigned char changed; // Packed flags are stored here |
| 97 |
}; |
| 98 |
|
| 99 |
VolChanged() : |
| 100 |
changed(0) {} |
| 101 |
} volChanged; |
| 102 |
|
| 103 |
struct FilterChanged { // Filter changed flags |
| 104 |
union { // pack flag bits in single byte |
| 105 |
struct { // flag bits |
| 106 |
unsigned char mode: 1; // Filter mode |
| 107 |
unsigned char k1: 1; // K1 |
| 108 |
unsigned char k2: 1; // K2 |
| 109 |
unsigned char dummy: 5; // dummy for bit padding |
| 110 |
}; |
| 111 |
unsigned char changed; // Packed flags are stored here |
| 112 |
}; |
| 113 |
|
| 114 |
FilterChanged(): |
| 115 |
changed(0) {} |
| 116 |
} filterChanged; |
| 117 |
|
| 118 |
struct EnvChanged { // Envelope changed flags |
| 119 |
union { // pack flag bits in single byte |
| 120 |
struct { // flag bits |
| 121 |
unsigned char ecount: 1; // Envelope count |
| 122 |
unsigned char lVRamp: 1; // Left volume Ramp |
| 123 |
unsigned char rVRamp: 1; // Right volume Ramp |
| 124 |
unsigned char k1Ramp: 1; // K1 Ramp w/Slow flag |
| 125 |
unsigned char k2Ramp: 1; // K2 Ramp w/Slow flag |
| 126 |
unsigned char dummy: 3; // dummy for bit padding |
| 127 |
}; |
| 128 |
unsigned char changed; // Packed flags are stored here |
| 129 |
}; |
| 130 |
|
| 131 |
EnvChanged(): |
| 132 |
changed(0) {} |
| 133 |
} envChanged; |
| 134 |
|
| 135 |
struct PCMChanged { |
| 136 |
union { |
| 137 |
struct { |
| 138 |
unsigned char index: 1; // sample index |
| 139 |
unsigned char slice: 1; // transwave slice |
| 140 |
unsigned char position: 1; // sample position in memory |
| 141 |
unsigned char loopBank: 1; // Loop mode and Bank |
| 142 |
unsigned char dummy: 4; // dummy for bit padding |
| 143 |
}; |
| 144 |
unsigned char changed; |
| 145 |
}; |
| 146 |
PCMChanged(): |
| 147 |
changed(0) {} |
| 148 |
} pcmChanged; |
| 149 |
|
| 150 |
struct Overwrite { |
| 151 |
DivInstrumentES5506::Filter filter; |
| 152 |
DivInstrumentES5506::Envelope envelope; |
| 153 |
|
| 154 |
struct State { |
| 155 |
// overwrited flag |
| 156 |
union { |
| 157 |
struct { |
| 158 |
unsigned char mode: 1; // filter mode |
| 159 |
unsigned char k1: 1; // k1 |
| 160 |
unsigned char k2: 1; // k2 |
| 161 |
unsigned char ecount: 1; // envelope count |
| 162 |
unsigned char lVRamp: 1; // left volume ramp |
| 163 |
unsigned char rVRamp: 1; // right volume ramp |
| 164 |
unsigned char k1Ramp: 1; // k1 ramp |
| 165 |
unsigned char k2Ramp: 1; // k2 ramp |
| 166 |
}; |
| 167 |
unsigned char overwrited; |
| 168 |
}; |
| 169 |
State(): |
| 170 |
overwrited(0) {} |
| 171 |
} state; |
| 172 |
|
| 173 |
Overwrite(): |
| 174 |
filter(DivInstrumentES5506::Filter()), |
| 175 |
envelope(DivInstrumentES5506::Envelope()), |
| 176 |
state(State()) {} |
| 177 |
} overwrite; |
| 178 |
|
| 179 |
unsigned char ca; |
| 180 |
signed int k1Offs, k2Offs; |
| 181 |
signed int k1Slide, k2Slide; |
| 182 |
signed int k1Prev, k2Prev; |
| 183 |
int lVol, rVol; |
| 184 |
int outLVol, outRVol; |
| 185 |
int resLVol, resRVol; |
| 186 |
signed int oscOut; |
| 187 |
DivInstrumentES5506::Filter filter; |
| 188 |
DivInstrumentES5506::Envelope envelope; |
| 189 |
Channel(): |
| 190 |
SharedChannel<int>(0xff), |
| 191 |
pcm(PCM()), |
| 192 |
nextFreq(0), |
| 193 |
nextNote(0), |
| 194 |
currNote(0), |
| 195 |
wave(-1), |
| 196 |
volMacroMax(0xfff), |
| 197 |
panMacroMax(0xfff), |
| 198 |
useWave(false), |
| 199 |
isReverseLoop(false), |
| 200 |
cr(0), |
| 201 |
crWriteVal(0), |
| 202 |
crChanged(false), |
| 203 |
crDirValChanged(false), |
| 204 |
crDirValInit(true), |
| 205 |
noteChanged(NoteChanged()), |
| 206 |
volChanged(VolChanged()), |
| 207 |
filterChanged(FilterChanged()), |
| 208 |
envChanged(EnvChanged()), |
| 209 |
pcmChanged(PCMChanged()), |
| 210 |
overwrite(Overwrite()), |
| 211 |
ca(0), |
| 212 |
k1Offs(0), |
| 213 |
k2Offs(0), |
| 214 |
k1Slide(0), |
| 215 |
k2Slide(0), |
| 216 |
k1Prev(0xffff), |
| 217 |
k2Prev(0xffff), |
| 218 |
lVol(0xff), |
| 219 |
rVol(0xff), |
| 220 |
outLVol(0xfff), |
| 221 |
outRVol(0xfff), |
| 222 |
resLVol(0xfff), |
| 223 |
resRVol(0xfff), |
| 224 |
oscOut(0), |
| 225 |
filter(DivInstrumentES5506::Filter()), |
| 226 |
envelope(DivInstrumentES5506::Envelope()) { |
| 227 |
outVol=0xfff; |
| 228 |
} |
| 229 |
}; |
| 230 |
Channel chan[32]; |
| 231 |
DivDispatchOscBuffer* oscBuf[32]; |
| 232 |
bool isMuted[32]; |
| 233 |
signed short* sampleMem; // ES5506 uses 16 bit data bus for samples |
| 234 |
size_t sampleMemLen; |
| 235 |
unsigned int* sampleOffES5506; |
| 236 |
bool* sampleLoaded; |
| 237 |
struct QueuedHostIntf { |
| 238 |
unsigned char state; |
| 239 |
unsigned char step; |
| 240 |
unsigned char addr; |
| 241 |
unsigned int val; |
| 242 |
unsigned int* read; |
| 243 |
unsigned short delay; |
| 244 |
bool isRead; |
| 245 |
QueuedHostIntf(): |
| 246 |
state(0), |
| 247 |
step(0), |
| 248 |
addr(0), |
| 249 |
val(0), |
| 250 |
read(NULL), |
| 251 |
delay(0), |
| 252 |
isRead(false) {} |
| 253 |
QueuedHostIntf(unsigned char s, unsigned char a, unsigned int v, unsigned short d=0): |
| 254 |
state(0), |
| 255 |
step(s), |
| 256 |
addr(a), |
| 257 |
val(v), |
| 258 |
read(NULL), |
| 259 |
delay(0), |
| 260 |
isRead(false) {} |
| 261 |
QueuedHostIntf(unsigned char st, unsigned char s, unsigned char a, unsigned int* r, unsigned short d=0): |
| 262 |
state(st), |
| 263 |
step(s), |
| 264 |
addr(a), |
| 265 |
val(0), |
| 266 |
read(r), |
| 267 |
delay(d), |
| 268 |
isRead(true) {} |
| 269 |
}; |
| 270 |
FixedQueue<QueuedHostIntf,2048> hostIntf32; |
| 271 |
FixedQueue<QueuedHostIntf,2048> hostIntf8; |
| 272 |
int cycle, curPage, volScale; |
| 273 |
unsigned int irqv; |
| 274 |
bool isReaded; |
| 275 |
bool irqTrigger, amigaVol, amigaPitch; |
| 276 |
unsigned int curCR; |
| 277 |
|
| 278 |
unsigned char initChanMax, chanMax; |
| 279 |
|
| 280 |
es5506_core es5506; |
| 281 |
DivMemoryComposition memCompo; |
| 282 |
unsigned char regPool[4*16*128]; // 7 bit page x 16 registers per page x 32 bit per registers |
| 283 |
|
| 284 |
void updateNoteChangesAsNeeded(int ch); |
| 285 |
|
| 286 |
friend void putDispatchChip(void*,int); |
| 287 |
friend void putDispatchChan(void*,int,int); |
| 288 |
|
| 289 |
public: |
| 290 |
virtual void e_pin(bool state) override; // E output |
| 291 |
virtual void irqb(bool state) override; // IRQB output |
| 292 |
virtual s16 read_sample(u8 bank, u32 address) override { |
| 293 |
if (sampleMem==NULL) return 0; |
| 294 |
return sampleMem[((bank&3)<<21)|(address&0x1fffff)]; |
| 295 |
} |
| 296 |
|
| 297 |
virtual void acquire(short** buf, size_t len) override; |
| 298 |
virtual int dispatch(DivCommand c) override; |
| 299 |
virtual void* getChanState(int chan) override; |
| 300 |
virtual DivMacroInt* getChanMacroInt(int ch) override; |
| 301 |
virtual unsigned short getPan(int chan) override; |
| 302 |
virtual DivDispatchOscBuffer* getOscBuffer(int chan) override; |
| 303 |
virtual unsigned char* getRegisterPool() override; |
| 304 |
virtual int getRegisterPoolSize() override; |
| 305 |
virtual void reset() override; |
| 306 |
virtual void forceIns() override; |
| 307 |
virtual void tick(bool sysTick=true) override; |
| 308 |
virtual void muteChannel(int ch, bool mute) override; |
| 309 |
virtual int getOutputCount() override; |
| 310 |
virtual bool keyOffAffectsArp(int ch) override; |
| 311 |
virtual void setFlags(const DivConfig& flags) override; |
| 312 |
virtual void notifyInsChange(int ins) override; |
| 313 |
virtual void notifyWaveChange(int wave) override; |
| 314 |
virtual void notifyInsDeletion(void* ins) override; |
| 315 |
virtual void poke(unsigned int addr, unsigned short val) override; |
| 316 |
virtual void poke(std::vector<DivRegWrite>& wlist) override; |
| 317 |
virtual const void* getSampleMem(int index = 0) override; |
| 318 |
virtual size_t getSampleMemCapacity(int index = 0) override; |
| 319 |
virtual size_t getSampleMemUsage(int index = 0) override; |
| 320 |
virtual bool isSampleLoaded(int index, int sample) override; |
| 321 |
virtual const DivMemoryComposition* getMemCompo(int index) override; |
| 322 |
virtual void renderSamples(int sysID) override; |
| 323 |
virtual const char** getRegisterSheet() override; |
| 324 |
virtual int init(DivEngine* parent, int channels, int sugRate, const DivConfig& flags) override; |
| 325 |
virtual void quit() override; |
| 326 |
DivPlatformES5506(); |
| 327 |
~DivPlatformES5506(); |
| 328 |
}; |
| 329 |
|
| 330 |
#endif |