Debian Codegraph @

src/engine/platform/gb.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 "gb.h"
21 #include "../engine.h"
22 #include "../../ta-log.h"
23 #include <math.h>
24
25 #define rWrite(a,v) if (!skipRegisterWrites) {writes.push(QueuedWrite(a,v)); regPool[(a)&0x7f]=v; if (dumpWrites) {addWrite(a,v);} }
26 #define immWrite(a,v) {writes.push(QueuedWrite(a,v)); regPool[(a)&0x7f]=v; if (dumpWrites) {addWrite(a,v);} }
27
28 #define CHIP_DIVIDER 16
29
30 const char* regCheatSheetGB[]={
31 "NR10_Sweep", "10",
32 "NR11_DutyLen", "11",
33 "NR12_VolEnv", "12",
34 "NR13_FreqL", "13",
35 "NR14_FreqH", "14",
36
37 "NR21_DutyLen", "16",
38 "NR22_VolEnv", "17",
39 "NR23_FreqL", "18",
40 "NR24_FreqH", "19",
41
42 "NR30_WaveOn", "1A",
43 "NR31_Len", "1B",
44 "NR32_Vol", "1C",
45 "NR33_FreqL", "1D",
46 "NR34_FreqH", "1E",
47
48 "NR41_Len", "20",
49 "NR42_VolEnv", "21",
50 "NR43_Freq", "22",
51 "NR44_Control", "23",
52
53 "NR50_MasterVol", "24",
54 "NR51_Toggle", "25",
55 "NR52_PowerStat", "26",
56
57 "Wave", "30",
58 NULL
59 };
60
61 const char** DivPlatformGB::getRegisterSheet() {
62 return regCheatSheetGB;
63 }
64
65 void DivPlatformGB::acquire(short** buf, size_t len) {
66 for (int i=0; i<4; i++) {
67 oscBuf[i]->begin(len);
68 }
69 for (size_t i=0; i<len; i++) {
70 if (!writes.empty()) {
71 QueuedWrite& w=writes.front();
72 GB_apu_write(gb,w.addr,w.val);
73 writes.pop();
74 }
75
76 GB_advance_cycles(gb,coreQuality);
77 buf[0][i]=gb->apu_output.final_sample.left;
78 buf[1][i]=gb->apu_output.final_sample.right;
79
80 for (int j=0; j<4; j++) {
81 oscBuf[j]->putSample(i,(gb->apu_output.current_sample[j].left+gb->apu_output.current_sample[j].right)<<6);
82 }
83 }
84 for (int i=0; i<4; i++) {
85 oscBuf[i]->end(len);
86 }
87 }
88
89 void DivPlatformGB::updateWave() {
90 if (doubleWave) {
91 rWrite(0x1a,0x40); // select 1 -> write to bank 0
92 for (int i=0; i<16; i++) {
93 int nibble1=ws.output[((i<<1)+antiClickWavePos)&63];
94 int nibble2=ws.output[((1+(i<<1))+antiClickWavePos)&63];
95 if (invertWave) {
96 nibble1^=15;
97 nibble2^=15;
98 }
99 rWrite(0x30+i,(nibble1<<4)|nibble2);
100 }
101 rWrite(0x1a,0); // select 0 -> write to bank 1
102 for (int i=0; i<16; i++) {
103 int nibble1=ws.output[((32+(i<<1))+antiClickWavePos)&63];
104 int nibble2=ws.output[((33+(i<<1))+antiClickWavePos)&63];
105 if (invertWave) {
106 nibble1^=15;
107 nibble2^=15;
108 }
109 rWrite(0x30+i,(nibble1<<4)|nibble2);
110 }
111 antiClickWavePos&=63;
112 } else {
113 rWrite(0x1a,model==GB_MODEL_AGB_NATIVE?0x40:0);
114 for (int i=0; i<16; i++) {
115 int nibble1=ws.output[((i<<1)+antiClickWavePos)&31];
116 int nibble2=ws.output[((1+(i<<1))+antiClickWavePos)&31];
117 if (invertWave) {
118 nibble1^=15;
119 nibble2^=15;
120 }
121 rWrite(0x30+i,(nibble1<<4)|nibble2);
122 }
123 antiClickWavePos&=31;
124 }
125 }
126
127 static unsigned char chanMuteMask[4]={
128 0xee, 0xdd, 0xbb, 0x77
129 };
130
131 unsigned char DivPlatformGB::procMute() {
132 return lastPan&(isMuted[0]?chanMuteMask[0]:0xff)
133 &(isMuted[1]?chanMuteMask[1]:0xff)
134 &(isMuted[2]?chanMuteMask[2]:0xff)
135 &(isMuted[3]?chanMuteMask[3]:0xff);
136 }
137
138 static unsigned char gbVolMap[16]={
139 0x00, 0x00, 0x00, 0x00,
140 0x60, 0x60, 0x60, 0x60,
141 0x40, 0x40, 0x40, 0x40,
142 0x20, 0x20, 0x20, 0x20
143 };
144
145 static unsigned char gbVolMapEx[16]={
146 0x00, 0x00, 0x00, 0x00,
147 0x60, 0x60, 0x60, 0x60,
148 0x40, 0x40, 0x40, 0x40,
149 0xa0, 0xa0, 0x20, 0x20
150 };
151
152 static unsigned char noiseTable[256]={
153 0,
154 0xf7, 0xf6, 0xf5, 0xf4,
155 0xe7, 0xe6, 0xe5, 0xe4,
156 0xd7, 0xd6, 0xd5, 0xd4,
157 0xc7, 0xc6, 0xc5, 0xc4,
158 0xb7, 0xb6, 0xb5, 0xb4,
159 0xa7, 0xa6, 0xa5, 0xa4,
160 0x97, 0x96, 0x95, 0x94,
161 0x87, 0x86, 0x85, 0x84,
162 0x77, 0x76, 0x75, 0x74,
163 0x67, 0x66, 0x65, 0x64,
164 0x57, 0x56, 0x55, 0x54,
165 0x47, 0x46, 0x45, 0x44,
166 0x37, 0x36, 0x35, 0x34,
167 0x27, 0x26, 0x25, 0x24,
168 0x17, 0x16, 0x15, 0x14,
169 0x07, 0x06, 0x05, 0x04,
170 0x03, 0x02, 0x01, 0x00,
171 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
172 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
173 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
174 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
175 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
176 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
177 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
178 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
179 };
180
181 void DivPlatformGB::tick(bool sysTick) {
182 if (antiClickEnabled && sysTick && chan[2].freq>0) {
183 antiClickPeriodCount+=((chipClock>>1)/MAX(parent->getCurHz(),1.0f));
184 antiClickWavePos+=antiClickPeriodCount/chan[2].freq;
185 antiClickPeriodCount%=chan[2].freq;
186 }
187
188 for (int i=0; i<4; i++) {
189 chan[i].std.next();
190 if (chan[i].softEnv) {
191 if (chan[i].std.vol.had) {
192 chan[i].outVol=VOL_SCALE_LINEAR(chan[i].vol&15,MIN(15,chan[i].std.vol.val),15);
193 if (chan[i].outVol<0) chan[i].outVol=0;
194
195 if (i==2) {
196 rWrite(16+i*5+2,(model==GB_MODEL_AGB_NATIVE?gbVolMapEx:gbVolMap)[chan[i].outVol]);
197 chan[i].soundLen=64;
198 } else {
199 chan[i].envLen=0;
200 chan[i].envDir=1;
201 chan[i].envVol=chan[i].outVol;
202 chan[i].soundLen=64;
203
204 if (!chan[i].keyOn) chan[i].killIt=true;
205 }
206 }
207 }
208 if (NEW_ARP_STRAT && i!=3) {
209 chan[i].handleArp();
210 } else if (chan[i].std.arp.had) {
211 if (i==3) { // noise
212 chan[i].baseFreq=parent->calcArp(chan[i].note,chan[i].std.arp.val,24);
213 if (chan[i].baseFreq>255) chan[i].baseFreq=255;
214 if (chan[i].baseFreq<0) chan[i].baseFreq=0;
215 } else {
216 if (!chan[i].inPorta) {
217 chan[i].baseFreq=NOTE_PERIODIC(parent->calcArp(chan[i].note,chan[i].std.arp.val,24));
218 }
219 }
220 chan[i].freqChanged=true;
221 }
222 if (chan[i].std.duty.had) {
223 chan[i].duty=chan[i].std.duty.val;
224 if (i!=2) {
225 rWrite(16+i*5+1,((chan[i].duty&3)<<6)|(63-(chan[i].soundLen&63)));
226 } else if (!chan[i].softEnv) {
227 if (parent->song.waveDutyIsVol) {
228 rWrite(16+i*5+2,(model==GB_MODEL_AGB_NATIVE?gbVolMapEx:gbVolMap)[(chan[i].std.duty.val&3)<<2]);
229 }
230 }
231 }
232 if (i==2 && chan[i].std.wave.had) {
233 if (chan[i].wave!=chan[i].std.wave.val || ws.activeChanged()) {
234 chan[i].wave=chan[i].std.wave.val;
235 ws.changeWave1(chan[i].wave);
236 if (!chan[i].keyOff) chan[i].keyOn=true;
237 }
238 }
239 if (chan[i].std.panL.had) {
240 lastPan&=~(0x11<<i);
241 lastPan|=((chan[i].std.panL.val&1)<<i)|((chan[i].std.panL.val&2)<<(i+3));
242 rWrite(0x25,procMute());
243 }
244 if (chan[i].std.pitch.had) {
245 if (chan[i].std.pitch.mode) {
246 chan[i].pitch2+=chan[i].std.pitch.val;
247 CLAMP_VAR(chan[i].pitch2,-32768,32767);
248 } else {
249 chan[i].pitch2=chan[i].std.pitch.val;
250 }
251 chan[i].freqChanged=true;
252 }
253 if (chan[i].std.phaseReset.had) {
254 if (chan[i].std.phaseReset.val==1) {
255 chan[i].keyOn=true;
256 if (i==2) {
257 antiClickWavePos=0;
258 antiClickPeriodCount=0;
259 }
260 }
261 }
262 if (i==2) {
263 if (chan[i].active) {
264 if (ws.tick()) {
265 updateWave();
266 if (!chan[i].keyOff) chan[i].keyOn=true;
267 }
268 }
269 }
270 // run hardware sequence
271 if (chan[i].active) {
272 if (--chan[i].hwSeqDelay<=0) {
273 chan[i].hwSeqDelay=0;
274 DivInstrument* ins=parent->getIns(chan[i].ins,DIV_INS_GB);
275 int hwSeqCount=0;
276 while (chan[i].hwSeqPos<ins->gb.hwSeqLen && hwSeqCount<4) {
277 bool leave=false;
278 unsigned short data=ins->gb.hwSeq[chan[i].hwSeqPos].data;
279 switch (ins->gb.hwSeq[chan[i].hwSeqPos].cmd) {
280 case DivInstrumentGB::DIV_GB_HWCMD_ENVELOPE:
281 if (!chan[i].softEnv) {
282 chan[i].envLen=data&7;
283 chan[i].envDir=(data&8)?1:0;
284 chan[i].envVol=(data>>4)&15;
285 chan[i].soundLen=data>>8;
286 chan[i].keyOn=true;
287 }
288 break;
289 case DivInstrumentGB::DIV_GB_HWCMD_SWEEP:
290 chan[i].sweep=data;
291 chan[i].sweepChanged=true;
292 break;
293 case DivInstrumentGB::DIV_GB_HWCMD_WAIT:
294 chan[i].hwSeqDelay=(data+1)*parent->tickMult;
295 leave=true;
296 break;
297 case DivInstrumentGB::DIV_GB_HWCMD_WAIT_REL:
298 if (!chan[i].released) {
299 chan[i].hwSeqPos--;
300 leave=true;
301 }
302 break;
303 case DivInstrumentGB::DIV_GB_HWCMD_LOOP:
304 chan[i].hwSeqPos=data-1;
305 break;
306 case DivInstrumentGB::DIV_GB_HWCMD_LOOP_REL:
307 if (!chan[i].released) {
308 chan[i].hwSeqPos=data-1;
309 }
310 break;
311 }
312
313 chan[i].hwSeqPos++;
314 if (leave) break;
315 hwSeqCount++;
316 }
317 }
318 }
319
320 if (chan[i].sweepChanged) {
321 chan[i].sweepChanged=false;
322 if (i==0) {
323 rWrite(16+i*5,chan[i].sweep);
324 }
325 }
326
327 if (chan[i].freqChanged || chan[i].keyOn || chan[i].keyOff) {
328 if (i==3) { // noise
329 int ntPos=chan[i].baseFreq+chan[i].pitch2;
330 if (ntPos<0) ntPos=0;
331 if (ntPos>255) ntPos=255;
332 chan[i].freq=noiseTable[ntPos];
333 } else {
334 chan[i].freq=parent->calcFreq(chan[i].baseFreq,chan[i].pitch,chan[i].fixedArp?chan[i].baseNoteOverride:chan[i].arpOff,chan[i].fixedArp,true,0,chan[i].pitch2,chipClock,CHIP_DIVIDER);
335 if (chan[i].freq>2047) chan[i].freq=2047;
336 if (chan[i].freq<1) chan[i].freq=1;
337 }
338 if (chan[i].keyOn) {
339 if (i==2) { // wave
340 rWrite(16+i*5,0x00);
341 rWrite(16+i*5,doubleWave?0xa0:0x80);
342 rWrite(16+i*5+2,(model==GB_MODEL_AGB_NATIVE?gbVolMapEx:gbVolMap)[chan[i].outVol]);
343 } else {
344 rWrite(16+i*5+1,((chan[i].duty&3)<<6)|(63-(chan[i].soundLen&63)));
345 rWrite(16+i*5+2,((chan[i].envVol<<4))|(chan[i].envLen&7)|((chan[i].envDir&1)<<3));
346 chan[i].lastKill=chan[i].envVol;
347 }
348 }
349 if (chan[i].keyOff) {
350 if (i==2) {
351 rWrite(16+i*5+2,0);
352 } else {
353 rWrite(16+i*5+2,8);
354 }
355 }
356 if (i==3) { // noise
357 rWrite(16+i*5+3,(chan[i].freq&0xff)|(chan[i].duty?8:0));
358 rWrite(16+i*5+4,((chan[i].keyOn||chan[i].keyOff)?0x80:0x00)|((chan[i].soundLen<64)<<6));
359 } else {
360 rWrite(16+i*5+3,(2048-chan[i].freq)&0xff);
361 rWrite(16+i*5+4,(((2048-chan[i].freq)>>8)&7)|((chan[i].keyOn||(chan[i].keyOff && i!=2))?0x80:0x00)|((chan[i].soundLen<63)<<6));
362 }
363 if (enoughAlready) { // more compat garbage
364 rWrite(16+i*5+1,((chan[i].duty&3)<<6)|(63-(chan[i].soundLen&63)));
365 }
366 if (chan[i].keyOn) chan[i].keyOn=false;
367 if (chan[i].keyOff) chan[i].keyOff=false;
368 chan[i].freqChanged=false;
369 }
370 if (chan[i].killIt) {
371 if (i!=2) {
372 //rWrite(16+i*5+2,8);
373 int killDelta=chan[i].lastKill-chan[i].outVol+1;
374 if (killDelta<0) killDelta+=16;
375 chan[i].lastKill=chan[i].outVol;
376
377 if (killDelta!=1) {
378 rWrite(16+i*5+2,((chan[i].envVol<<4))|8);
379 for (int j=0; j<killDelta; j++) {
380 rWrite(16+i*5+2,0x09);
381 rWrite(16+i*5+2,0x11);
382 rWrite(16+i*5+2,0x08);
383 }
384 }
385 }
386 chan[i].killIt=false;
387 }
388
389 chan[i].soManyHacksToMakeItDefleCompatible=false;
390 }
391 }
392
393 void DivPlatformGB::muteChannel(int ch, bool mute) {
394 isMuted[ch]=mute;
395 rWrite(0x25,procMute());
396 }
397
398 int DivPlatformGB::dispatch(DivCommand c) {
399 switch (c.cmd) {
400 case DIV_CMD_NOTE_ON: {
401 DivInstrument* ins=parent->getIns(chan[c.chan].ins,DIV_INS_GB);
402 if (c.value!=DIV_NOTE_NULL) {
403 if (c.chan==3) { // noise
404 chan[c.chan].baseFreq=c.value;
405 } else {
406 chan[c.chan].baseFreq=NOTE_PERIODIC(c.value);
407 }
408 chan[c.chan].freqChanged=true;
409 chan[c.chan].note=c.value;
410 }
411 chan[c.chan].active=true;
412 chan[c.chan].keyOn=true;
413 chan[c.chan].hwSeqPos=0;
414 chan[c.chan].hwSeqDelay=0;
415 chan[c.chan].released=false;
416 chan[c.chan].softEnv=ins->gb.softEnv;
417 chan[c.chan].macroInit(ins);
418 if (c.chan==2) {
419 doubleWave=(model==GB_MODEL_AGB_NATIVE) && ins->gb.doubleWave;
420 if (chan[c.chan].wave<0) {
421 chan[c.chan].wave=0;
422 ws.changeWave1(chan[c.chan].wave);
423 }
424 ws.init(ins,doubleWave?64:32,15,chan[c.chan].insChanged);
425 if (doubleWave!=lastDoubleWave) {
426 ws.changeWave1(chan[c.chan].wave);
427 lastDoubleWave=doubleWave;
428 }
429 }
430 if ((chan[c.chan].insChanged || ins->gb.alwaysInit) && !chan[c.chan].softEnv) {
431 if (!chan[c.chan].soManyHacksToMakeItDefleCompatible && c.chan!=2) {
432 chan[c.chan].envVol=ins->gb.envVol;
433 }
434 chan[c.chan].envLen=ins->gb.envLen;
435 chan[c.chan].envDir=ins->gb.envDir;
436 chan[c.chan].soundLen=ins->gb.soundLen;
437 if (!chan[c.chan].soManyHacksToMakeItDefleCompatible && c.chan!=2) {
438 chan[c.chan].vol=chan[c.chan].envVol;
439 chan[c.chan].outVol=chan[c.chan].envVol;
440 }
441 } else if (chan[c.chan].softEnv && c.chan!=2) {
442 if (!parent->song.brokenOutVol && !chan[c.chan].std.vol.will) {
443 chan[c.chan].outVol=chan[c.chan].vol;
444 chan[c.chan].envVol=chan[c.chan].outVol;
445 }
446 chan[c.chan].envLen=0;
447 chan[c.chan].envDir=1;
448 chan[c.chan].soundLen=64;
449 }
450 if (c.chan==2 && chan[c.chan].softEnv) {
451 chan[c.chan].soundLen=64;
452 }
453 chan[c.chan].insChanged=false;
454 break;
455 }
456 case DIV_CMD_NOTE_OFF:
457 chan[c.chan].active=false;
458 chan[c.chan].keyOff=true;
459 chan[c.chan].hwSeqPos=0;
460 chan[c.chan].hwSeqDelay=0;
461 chan[c.chan].macroInit(NULL);
462 break;
463 case DIV_CMD_NOTE_OFF_ENV:
464 case DIV_CMD_ENV_RELEASE:
465 chan[c.chan].std.release();
466 chan[c.chan].released=true;
467 break;
468 case DIV_CMD_INSTRUMENT:
469 if (chan[c.chan].ins!=c.value || c.value2==1) {
470 chan[c.chan].ins=c.value;
471 chan[c.chan].insChanged=true;
472 if (c.chan!=2) {
473 DivInstrument* ins=parent->getIns(chan[c.chan].ins,DIV_INS_GB);
474 if (!ins->gb.softEnv) {
475 chan[c.chan].envVol=ins->gb.envVol;
476 chan[c.chan].envLen=ins->gb.envLen;
477 chan[c.chan].envDir=ins->gb.envDir;
478 chan[c.chan].soundLen=ins->gb.soundLen;
479 chan[c.chan].vol=chan[c.chan].envVol;
480 chan[c.chan].outVol=chan[c.chan].vol;
481 if (parent->song.gbInsAffectsEnvelope) {
482 rWrite(16+c.chan*5+2,((chan[c.chan].vol<<4))|(chan[c.chan].envLen&7)|((chan[c.chan].envDir&1)<<3));
483 }
484 }
485 }
486 }
487 break;
488 case DIV_CMD_VOLUME:
489 chan[c.chan].vol=c.value;
490 chan[c.chan].outVol=c.value;
491 if (c.chan==2) {
492 rWrite(16+c.chan*5+2,(model==GB_MODEL_AGB_NATIVE?gbVolMapEx:gbVolMap)[chan[c.chan].outVol]);
493 }
494 if (!chan[c.chan].softEnv) {
495 chan[c.chan].envVol=chan[c.chan].vol;
496 chan[c.chan].soManyHacksToMakeItDefleCompatible=true;
497 } else if (c.chan!=2) {
498 if (chan[c.chan].std.vol.will && !chan[c.chan].std.vol.finished) {
499 chan[c.chan].outVol=VOL_SCALE_LINEAR(chan[c.chan].vol&15,MIN(15,chan[c.chan].std.vol.val),15);
500 }
501 chan[c.chan].envVol=chan[c.chan].outVol;
502
503 if (!chan[c.chan].keyOn && chan[c.chan].active) chan[c.chan].killIt=true;
504 chan[c.chan].freqChanged=true;
505 }
506 break;
507 case DIV_CMD_GET_VOLUME:
508 return chan[c.chan].vol;
509 break;
510 case DIV_CMD_PITCH:
511 chan[c.chan].pitch=c.value;
512 chan[c.chan].freqChanged=true;
513 break;
514 case DIV_CMD_WAVE:
515 if (c.chan!=2) break;
516 chan[c.chan].wave=c.value;
517 ws.changeWave1(chan[c.chan].wave);
518 if (chan[c.chan].active) {
519 chan[c.chan].keyOn=true;
520 }
521 break;
522 case DIV_CMD_NOTE_PORTA: {
523 int destFreq=NOTE_PERIODIC(c.value2);
524 bool return2=false;
525 if (destFreq>chan[c.chan].baseFreq) {
526 chan[c.chan].baseFreq+=c.value;
527 if (chan[c.chan].baseFreq>=destFreq) {
528 chan[c.chan].baseFreq=destFreq;
529 return2=true;
530 }
531 } else {
532 chan[c.chan].baseFreq-=c.value;
533 if (chan[c.chan].baseFreq<=destFreq) {
534 chan[c.chan].baseFreq=destFreq;
535 return2=true;
536 }
537 }
538 chan[c.chan].freqChanged=true;
539 if (return2) {
540 chan[c.chan].inPorta=false;
541 return 2;
542 }
543 break;
544 }
545 case DIV_CMD_STD_NOISE_MODE:
546 chan[c.chan].duty=c.value;
547 if (c.chan!=2) {
548 chan[c.chan].freqChanged=true;
549 rWrite(16+c.chan*5+1,((chan[c.chan].duty&3)<<6)|(63-(parent->getIns(chan[c.chan].ins,DIV_INS_GB)->gb.soundLen&63)));
550 }
551 break;
552 case DIV_CMD_PANNING: {
553 lastPan&=~(0x11<<c.chan);
554 int pan=0;
555 if (c.value>0) pan|=0x10;
556 if (c.value2>0) pan|=0x01;
557 if (pan==0) pan=0x11;
558 lastPan|=pan<<c.chan;
559 rWrite(0x25,procMute());
560 break;
561 }
562 case DIV_CMD_LEGATO:
563 if (c.chan==3) break;
564 chan[c.chan].baseFreq=NOTE_PERIODIC(c.value+((HACKY_LEGATO_MESS)?(chan[c.chan].std.arp.val):(0)));
565 chan[c.chan].freqChanged=true;
566 chan[c.chan].note=c.value;
567 break;
568 case DIV_CMD_PRE_PORTA:
569 if (chan[c.chan].active && c.value2) {
570 if (parent->song.resetMacroOnPorta) chan[c.chan].macroInit(parent->getIns(chan[c.chan].ins,DIV_INS_GB));
571 }
572 if (!chan[c.chan].inPorta && c.value && !parent->song.brokenPortaArp && chan[c.chan].std.arp.will && !NEW_ARP_STRAT) chan[c.chan].baseFreq=NOTE_PERIODIC(chan[c.chan].note);
573 chan[c.chan].inPorta=c.value;
574 break;
575 case DIV_CMD_GB_SWEEP_DIR:
576 if (c.chan>0) break;
577 chan[c.chan].sweep&=0xf7;
578 if (c.value&1) {
579 chan[c.chan].sweep|=8;
580 }
581 chan[c.chan].sweepChanged=true;
582 break;
583 case DIV_CMD_GB_SWEEP_TIME:
584 if (c.chan>0) break;
585 chan[c.chan].sweep&=8;
586 chan[c.chan].sweep|=c.value&0x77;
587 chan[c.chan].sweepChanged=true;
588 break;
589 case DIV_CMD_GET_VOLMAX:
590 return 15;
591 break;
592 case DIV_CMD_MACRO_OFF:
593 chan[c.chan].std.mask(c.value,true);
594 break;
595 case DIV_CMD_MACRO_ON:
596 chan[c.chan].std.mask(c.value,false);
597 break;
598 case DIV_CMD_MACRO_RESTART:
599 chan[c.chan].std.restart(c.value);
600 break;
601 default:
602 break;
603 }
604 return 1;
605 }
606
607 void DivPlatformGB::forceIns() {
608 for (int i=0; i<4; i++) {
609 chan[i].insChanged=true;
610 }
611 immWrite(0x25,procMute());
612 updateWave();
613 }
614
615 void* DivPlatformGB::getChanState(int ch) {
616 return &chan[ch];
617 }
618
619 DivMacroInt* DivPlatformGB::getChanMacroInt(int ch) {
620 return &chan[ch].std;
621 }
622
623 unsigned short DivPlatformGB::getPan(int ch) {
624 unsigned char p=lastPan&(0x11<<ch);
625 return ((p&0xf0)?0x100:0)|((p&0x0f)?1:0);
626 }
627
628 DivDispatchOscBuffer* DivPlatformGB::getOscBuffer(int ch) {
629 return oscBuf[ch];
630 }
631
632 unsigned char* DivPlatformGB::getRegisterPool() {
633 return regPool;
634 }
635
636 int DivPlatformGB::getRegisterPoolSize() {
637 return 64;
638 }
639
640 void DivPlatformGB::reset() {
641 for (int i=0; i<4; i++) {
642 chan[i]=DivPlatformGB::Channel();
643 chan[i].std.setEngine(parent);
644 }
645 ws.setEngine(parent);
646 ws.init(NULL,32,15,false);
647 if (dumpWrites) {
648 addWrite(0xffffffff,0);
649 }
650 memset(gb,0,sizeof(GB_gameboy_t));
651 memset(regPool,0,128);
652 gb->model=model;
653 GB_apu_init(gb);
654 GB_set_sample_rate(gb,rate);
655 // enable all channels
656 immWrite(0x10,0);
657 immWrite(0x26,0x8f);
658 lastPan=0xff;
659 immWrite(0x25,procMute());
660 immWrite(0x24,0xff);
661
662 antiClickPeriodCount=0;
663 antiClickWavePos=0;
664 doubleWave=false;
665 lastDoubleWave=false;
666 }
667
668 int DivPlatformGB::getPortaFloor(int ch) {
669 return 24;
670 }
671
672 int DivPlatformGB::getOutputCount() {
673 return 2;
674 }
675
676 bool DivPlatformGB::getDCOffRequired() {
677 return (model==GB_MODEL_AGB_NATIVE);
678 }
679
680 void DivPlatformGB::notifyInsChange(int ins) {
681 for (int i=0; i<4; i++) {
682 if (chan[i].ins==ins) {
683 chan[i].insChanged=true;
684 }
685 }
686 }
687
688 void DivPlatformGB::notifyWaveChange(int wave) {
689 if (chan[2].wave==wave) {
690 ws.changeWave1(wave);
691 updateWave();
692 if (!chan[2].keyOff && chan[2].active) chan[2].keyOn=true;
693 }
694 }
695
696 void DivPlatformGB::notifyInsDeletion(void* ins) {
697 for (int i=0; i<4; i++) {
698 chan[i].std.notifyInsDeletion((DivInstrument*)ins);
699 }
700 }
701
702 void DivPlatformGB::poke(unsigned int addr, unsigned short val) {
703 immWrite(addr,val);
704 }
705
706 void DivPlatformGB::poke(std::vector<DivRegWrite>& wlist) {
707 for (DivRegWrite& i: wlist) immWrite(i.addr,i.val);
708 }
709
710 void DivPlatformGB::setFlags(const DivConfig& flags) {
711 antiClickEnabled=!flags.getBool("noAntiClick",false);
712 switch (flags.getInt("chipType",0)) {
713 case 0:
714 model=GB_MODEL_DMG_B;
715 break;
716 case 1:
717 model=GB_MODEL_CGB_C;
718 break;
719 case 2:
720 model=GB_MODEL_CGB_E;
721 break;
722 case 3:
723 model=GB_MODEL_AGB_NATIVE;
724 break;
725 }
726 invertWave=flags.getBool("invertWave",true);
727 enoughAlready=flags.getBool("enoughAlready",false);
728
729 chipClock=4194304;
730 CHECK_CUSTOM_CLOCK;
731 rate=chipClock/coreQuality;
732 for (int i=0; i<4; i++) {
733 oscBuf[i]->setRate(rate);
734 }
735 }
736
737 void DivPlatformGB::setCoreQuality(unsigned char q) {
738 switch (q) {
739 case 0:
740 // sorry...
741 coreQuality=64;
742 break;
743 case 1:
744 coreQuality=64;
745 break;
746 case 2:
747 coreQuality=32;
748 break;
749 case 3:
750 coreQuality=16;
751 break;
752 case 4:
753 coreQuality=4;
754 break;
755 case 5:
756 coreQuality=1;
757 break;
758 default:
759 coreQuality=16;
760 break;
761 }
762 }
763
764 int DivPlatformGB::init(DivEngine* p, int channels, int sugRate, const DivConfig& flags) {
765 parent=p;
766 dumpWrites=false;
767 skipRegisterWrites=false;
768 model=GB_MODEL_DMG_B;
769 gb=new GB_gameboy_t;
770
771 for (int i=0; i<4; i++) {
772 isMuted[i]=false;
773 oscBuf[i]=new DivDispatchOscBuffer;
774 }
775
776 setFlags(flags);
777 reset();
778 return 4;
779 }
780
781 void DivPlatformGB::quit() {
782 for (int i=0; i<4; i++) {
783 delete oscBuf[i];
784 }
785 delete gb;
786 }
787
788 DivPlatformGB::~DivPlatformGB() {
789 }