Debian Codegraph @

src/engine/platform/t6w28.cpp

raw
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 #include "t6w28.h"
21 #include "../engine.h"
22 #include "sound/t6w28/T6W28_Apu.h"
23 #include <math.h>
24
25 //#define rWrite(a,v) pendingWrites[a]=v;
26 #define rWrite(a,v) if (!skipRegisterWrites) {writes.push(QueuedWrite(a,v)); if (dumpWrites) {addWrite(a,v);} }
27
28 const char* regCheatSheetT6W28[]={
29 "Data0", "0",
30 "Data1", "1",
31 NULL
32 };
33
34 const char** DivPlatformT6W28::getRegisterSheet() {
35 return regCheatSheetT6W28;
36 }
37
38 void DivPlatformT6W28::acquireDirect(blip_buffer_t** bb, size_t len) {
39 for (int i=0; i<4; i++) {
40 oscBuf[i]->begin(len);
41 }
42 t6w->output(bb[0],bb[1]);
43 for (int i=0; i<4; i++) {
44 t6w->osc_output(i,oscBuf[i]);
45 }
46
47 while (!writes.empty()) {
48 QueuedWrite w=writes.front();
49 if (w.addr) {
50 t6w->write_data_right(0,w.val);
51 } else {
52 t6w->write_data_left(0,w.val);
53 }
54 regPool[w.addr&1]=w.val;
55 writes.pop();
56 }
57 t6w->end_frame(len);
58 /*
59 for (size_t h=0; h<len; h++) {
60
61 tempL=0;
62 tempR=0;
63 for (int i=0; i<4; i++) {
64 oscBuf[i]->putSample(h,(out[i][1].curValue+out[i][2].curValue)<<7);
65 tempL+=out[i][1].curValue<<7;
66 tempR+=out[i][2].curValue<<7;
67 }
68
69 if (tempL<-32768) tempL=-32768;
70 if (tempL>32767) tempL=32767;
71 if (tempR<-32768) tempR=-32768;
72 if (tempR>32767) tempR=32767;
73
74 buf[0][h]=tempL;
75 buf[1][h]=tempR;
76 }*/
77
78 for (int i=0; i<4; i++) {
79 oscBuf[i]->end(len);
80 }
81 }
82
83 void DivPlatformT6W28::writeOutVol(int ch) {
84 if (chan[ch].active) {
85 int left=15-CLAMP(chan[ch].outVol+chan[ch].panL-15,0,15);
86 int right=15-CLAMP(chan[ch].outVol+chan[ch].panR-15,0,15);
87 rWrite(0,0x90|(ch<<5)|(isMuted[ch]?15:left));
88 rWrite(1,0x90|(ch<<5)|(isMuted[ch]?15:right));
89 } else {
90 rWrite(0,0x9f|(ch<<5));
91 rWrite(1,0x9f|(ch<<5));
92 }
93 }
94
95 double DivPlatformT6W28::NOTE_SN(int ch, int note) {
96 double CHIP_DIVIDER=16;
97 if (ch==3) CHIP_DIVIDER=15;
98 if (parent->song.linearPitch==2 || !easyNoise) {
99 return NOTE_PERIODIC(note);
100 }
101 if (note>107) {
102 return MAX(0,13-(note-107));
103 }
104 return NOTE_PERIODIC(note);
105 }
106
107 int DivPlatformT6W28::snCalcFreq(int ch) {
108 if (parent->song.linearPitch==2 && easyNoise && chan[ch].baseFreq+chan[ch].pitch+chan[ch].pitch2>(107<<7)) {
109 int ret=(((13<<7)+0x40)-(chan[ch].baseFreq+chan[ch].pitch+chan[ch].pitch2-(107<<7)))>>7;
110 if (ret<0) ret=0;
111 return ret;
112 }
113 return parent->calcFreq(chan[ch].baseFreq,chan[ch].pitch,chan[ch].fixedArp?chan[ch].baseNoteOverride:chan[ch].arpOff,chan[ch].fixedArp,true,0,chan[ch].pitch2,chipClock,ch==3?15:16);
114 }
115
116 void DivPlatformT6W28::tick(bool sysTick) {
117 for (int i=0; i<4; i++) {
118 chan[i].std.next();
119 if (chan[i].std.vol.had) {
120 chan[i].outVol=VOL_SCALE_LOG_BROKEN(chan[i].vol&15,MIN(15,chan[i].std.vol.val),15);
121 }
122 if (NEW_ARP_STRAT) {
123 chan[i].handleArp();
124 } else if (chan[i].std.arp.had) {
125 if (!chan[i].inPorta) {
126 int noiseSeek=parent->calcArp(chan[i].note,chan[i].std.arp.val);
127 chan[i].baseFreq=NOTE_SN(i,noiseSeek);
128 }
129 chan[i].freqChanged=true;
130 }
131 if (i==3 && chan[i].std.duty.had) {
132 if (chan[i].duty!=(((chan[i].std.duty.val==1)?4:0)|3)) {
133 chan[i].duty=((chan[i].std.duty.val==1)?4:0)|3;
134 rWrite(1,0xe0+chan[i].duty);
135 }
136 }
137 if (chan[i].std.panL.had) {
138 chan[i].panL=chan[i].std.panL.val&15;
139 }
140 if (chan[i].std.panR.had) {
141 chan[i].panR=chan[i].std.panR.val&15;
142 }
143 if (chan[i].std.vol.had || chan[i].std.panL.had || chan[i].std.panR.had) {
144 writeOutVol(i);
145 }
146 if (chan[i].std.pitch.had) {
147 if (chan[i].std.pitch.mode) {
148 chan[i].pitch2+=chan[i].std.pitch.val;
149 CLAMP_VAR(chan[i].pitch2,-32768,32767);
150 } else {
151 chan[i].pitch2=chan[i].std.pitch.val;
152 }
153 chan[i].freqChanged=true;
154 }
155 if (chan[i].std.phaseReset.had) {
156 if (chan[i].std.phaseReset.val==1) {
157 rWrite(1,0xe0+chan[i].duty);
158 }
159 }
160 if (chan[i].freqChanged || chan[i].keyOn || chan[i].keyOff) {
161 chan[i].freq=snCalcFreq(i);
162 if (chan[i].freq<0) chan[i].freq=0;
163 if (chan[i].freq>1023) chan[i].freq=1023;
164 if (i==3) {
165 rWrite(1,0x80|(2<<5)|(chan[3].freq&15));
166 rWrite(1,chan[3].freq>>4);
167 } else {
168 rWrite(0,0x80|i<<5|(chan[i].freq&15));
169 rWrite(0,chan[i].freq>>4);
170 }
171 if (chan[i].keyOn) chan[i].keyOn=false;
172 if (chan[i].keyOff) chan[i].keyOff=false;
173 chan[i].freqChanged=false;
174 }
175 }
176 }
177
178 int DivPlatformT6W28::dispatch(DivCommand c) {
179 switch (c.cmd) {
180 case DIV_CMD_NOTE_ON: {
181 DivInstrument* ins=parent->getIns(chan[c.chan].ins,DIV_INS_PCE);
182 if (c.value!=DIV_NOTE_NULL) {
183 chan[c.chan].baseFreq=NOTE_SN(c.chan,c.value);
184 chan[c.chan].freqChanged=true;
185 chan[c.chan].note=c.value;
186 }
187 chan[c.chan].active=true;
188 chan[c.chan].keyOn=true;
189 chan[c.chan].macroInit(ins);
190 if (!parent->song.brokenOutVol && !chan[c.chan].std.vol.will) {
191 chan[c.chan].outVol=chan[c.chan].vol;
192 writeOutVol(c.chan);
193 }
194 chan[c.chan].insChanged=false;
195 break;
196 }
197 case DIV_CMD_NOTE_OFF:
198 chan[c.chan].active=false;
199 chan[c.chan].keyOff=true;
200 chan[c.chan].macroInit(NULL);
201 writeOutVol(c.chan);
202 break;
203 case DIV_CMD_NOTE_OFF_ENV:
204 case DIV_CMD_ENV_RELEASE:
205 chan[c.chan].std.release();
206 break;
207 case DIV_CMD_INSTRUMENT:
208 if (chan[c.chan].ins!=c.value || c.value2==1) {
209 chan[c.chan].ins=c.value;
210 chan[c.chan].insChanged=true;
211 }
212 break;
213 case DIV_CMD_VOLUME:
214 if (chan[c.chan].vol!=c.value) {
215 chan[c.chan].vol=c.value;
216 if (!chan[c.chan].std.vol.has) {
217 chan[c.chan].outVol=c.value;
218 writeOutVol(c.chan);
219 }
220 }
221 break;
222 case DIV_CMD_GET_VOLUME:
223 if (chan[c.chan].std.vol.has) {
224 return chan[c.chan].vol;
225 }
226 return chan[c.chan].outVol;
227 break;
228 case DIV_CMD_PITCH:
229 chan[c.chan].pitch=c.value;
230 chan[c.chan].freqChanged=true;
231 break;
232 case DIV_CMD_NOTE_PORTA: {
233 int destFreq=NOTE_SN(c.chan,c.value2);
234 bool return2=false;
235 if (destFreq>chan[c.chan].baseFreq) {
236 chan[c.chan].baseFreq+=c.value;
237 if (chan[c.chan].baseFreq>=destFreq) {
238 chan[c.chan].baseFreq=destFreq;
239 return2=true;
240 }
241 } else {
242 chan[c.chan].baseFreq-=c.value;
243 if (chan[c.chan].baseFreq<=destFreq) {
244 chan[c.chan].baseFreq=destFreq;
245 return2=true;
246 }
247 }
248 chan[c.chan].freqChanged=true;
249 if (return2) {
250 chan[c.chan].inPorta=false;
251 return 2;
252 }
253 break;
254 }
255 case DIV_CMD_STD_NOISE_MODE:
256 if (c.chan!=3) break;
257 chan[c.chan].duty=(((c.value&15)==1)?4:0)|3;
258 rWrite(1,0xe0+chan[c.chan].duty);
259 break;
260 case DIV_CMD_PANNING: {
261 chan[c.chan].panL=c.value>>4;
262 chan[c.chan].panR=c.value2>>4;
263 writeOutVol(c.chan);
264 break;
265 }
266 case DIV_CMD_LEGATO:
267 chan[c.chan].baseFreq=NOTE_SN(c.chan,c.value+((HACKY_LEGATO_MESS)?(chan[c.chan].std.arp.val):(0)));
268 chan[c.chan].freqChanged=true;
269 chan[c.chan].note=c.value;
270 break;
271 case DIV_CMD_PRE_PORTA:
272 if (chan[c.chan].active && c.value2) {
273 if (parent->song.resetMacroOnPorta) chan[c.chan].macroInit(parent->getIns(chan[c.chan].ins,DIV_INS_T6W28));
274 }
275 if (!chan[c.chan].inPorta && c.value && !parent->song.brokenPortaArp && chan[c.chan].std.arp.will && !NEW_ARP_STRAT) chan[c.chan].baseFreq=NOTE_SN(c.chan,chan[c.chan].note);
276 chan[c.chan].inPorta=c.value;
277 break;
278 case DIV_CMD_GET_VOLMAX:
279 return 15;
280 break;
281 case DIV_CMD_MACRO_OFF:
282 chan[c.chan].std.mask(c.value,true);
283 break;
284 case DIV_CMD_MACRO_ON:
285 chan[c.chan].std.mask(c.value,false);
286 break;
287 case DIV_CMD_MACRO_RESTART:
288 chan[c.chan].std.restart(c.value);
289 break;
290 default:
291 break;
292 }
293 return 1;
294 }
295
296 void DivPlatformT6W28::muteChannel(int ch, bool mute) {
297 isMuted[ch]=mute;
298 writeOutVol(ch);
299 }
300
301 void DivPlatformT6W28::forceIns() {
302 for (int i=0; i<4; i++) {
303 chan[i].insChanged=true;
304 chan[i].freqChanged=true;
305 }
306 rWrite(1,0xe0+chan[3].duty);
307 }
308
309 void* DivPlatformT6W28::getChanState(int ch) {
310 return &chan[ch];
311 }
312
313 DivMacroInt* DivPlatformT6W28::getChanMacroInt(int ch) {
314 return &chan[ch].std;
315 }
316
317 unsigned short DivPlatformT6W28::getPan(int ch) {
318 return (chan[ch].panL<<8)|chan[ch].panR;
319 }
320
321 DivDispatchOscBuffer* DivPlatformT6W28::getOscBuffer(int ch) {
322 return oscBuf[ch];
323 }
324
325 int DivPlatformT6W28::mapVelocity(int ch, float vel) {
326 return round(15.0*pow(vel,0.33));
327 }
328
329 unsigned char* DivPlatformT6W28::getRegisterPool() {
330 return regPool;
331 }
332
333 int DivPlatformT6W28::getRegisterPoolSize() {
334 return 112;
335 }
336
337 void DivPlatformT6W28::reset() {
338 while (!writes.empty()) writes.pop();
339 memset(regPool,0,128);
340 for (int i=0; i<4; i++) {
341 chan[i]=DivPlatformT6W28::Channel();
342 chan[i].std.setEngine(parent);
343 }
344 if (dumpWrites) {
345 addWrite(0xffffffff,0);
346 }
347 t6w->reset();
348 lastPan=0xff;
349 tempL=0;
350 tempR=0;
351 curChan=-1;
352 delay=0;
353 // default noise mode
354 rWrite(1,0xe7);
355 }
356
357 int DivPlatformT6W28::getOutputCount() {
358 return 2;
359 }
360
361 bool DivPlatformT6W28::keyOffAffectsArp(int ch) {
362 return true;
363 }
364
365 bool DivPlatformT6W28::hasAcquireDirect() {
366 return true;
367 }
368
369 void DivPlatformT6W28::notifyInsDeletion(void* ins) {
370 for (int i=0; i<4; i++) {
371 chan[i].std.notifyInsDeletion((DivInstrument*)ins);
372 }
373 }
374
375 void DivPlatformT6W28::setFlags(const DivConfig& flags) {
376 chipClock=3072000.0;
377 CHECK_CUSTOM_CLOCK;
378 rate=chipClock;
379 for (int i=0; i<4; i++) {
380 oscBuf[i]->setRate(rate);
381 }
382 easyNoise=!flags.getBool("noEasyNoise",false);
383
384 if (t6w!=NULL) {
385 delete t6w;
386 t6w=NULL;
387 }
388 t6w=new MDFN_IEN_NGP::T6W28_Apu;
389 for (int i=0; i<4; i++) {
390 t6w->osc_output(i,oscBuf[i]);
391 }
392 }
393
394 void DivPlatformT6W28::poke(unsigned int addr, unsigned short val) {
395 rWrite(addr,val);
396 }
397
398 void DivPlatformT6W28::poke(std::vector<DivRegWrite>& wlist) {
399 for (DivRegWrite& i: wlist) rWrite(i.addr,i.val);
400 }
401
402 int DivPlatformT6W28::init(DivEngine* p, int channels, int sugRate, const DivConfig& flags) {
403 parent=p;
404 dumpWrites=false;
405 skipRegisterWrites=false;
406 for (int i=0; i<4; i++) {
407 isMuted[i]=false;
408 oscBuf[i]=new DivDispatchOscBuffer;
409 }
410 t6w=NULL;
411 setFlags(flags);
412 reset();
413 return 6;
414 }
415
416 void DivPlatformT6W28::quit() {
417 for (int i=0; i<4; i++) {
418 delete oscBuf[i];
419 }
420 if (t6w!=NULL) {
421 delete t6w;
422 t6w=NULL;
423 }
424 }
425
426 DivPlatformT6W28::~DivPlatformT6W28() {
427 }