Debian Codegraph @

src/engine/platform/zxbeeper.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 "zxbeeper.h"
21 #include "../engine.h"
22 #include <math.h>
23
24 #define CHIP_FREQBASE 8192*6
25
26 const char** DivPlatformZXBeeper::getRegisterSheet() {
27 return NULL;
28 }
29
30 void DivPlatformZXBeeper::acquire(short** buf, size_t len) {
31 for (int i=0; i<1; i++) {
32 oscBuf[i]->begin(len);
33 }
34
35 bool o=false;
36 for (size_t h=0; h<len; h++) {
37 // clock here
38 if (curSample>=0 && curSample<parent->song.sampleLen) {
39 if (--curSamplePeriod<0) {
40 DivSample* s=parent->getSample(curSample);
41 if (s->samples>0) {
42 sampleOut=(s->data8[curSamplePos++]>0);
43 if (curSamplePos>=s->samples) curSample=-1;
44 // 256 bits
45 if (curSamplePos>2047) curSample=-1;
46
47 curSamplePeriod=15;
48 } else {
49 curSample=-1;
50 }
51 }
52 o=sampleOut;
53 buf[0][h]=o?16384:0;
54 oscBuf[0]->putSample(h,o?16384:-16384);
55 continue;
56 }
57
58 unsigned short oldPos=chan[curChan].sPosition;
59 o=false;
60
61 if (sOffTimer) {
62 sOffTimer--;
63 o=true;
64 }
65 chan[curChan].sPosition+=chan[curChan].freq;
66 if (oldPos>chan[curChan].sPosition) {
67 if (!isMuted[curChan] && chan[curChan].outVol) sOffTimer+=chan[curChan].duty;
68 }
69 if (++curChan>=6) curChan=0;
70
71 buf[0][h]=o?16384:0;
72 oscBuf[0]->putSample(h,o?16384:-16384);
73 }
74
75 for (int i=0; i<1; i++) {
76 oscBuf[i]->end(len);
77 }
78 }
79
80 void DivPlatformZXBeeper::tick(bool sysTick) {
81 for (int i=0; i<6; i++) {
82 chan[i].std.next();
83 if (chan[i].std.vol.had) {
84 chan[i].outVol=((chan[i].vol&1)*MIN(1,chan[i].std.vol.val));
85 }
86 if (chan[i].std.duty.had) {
87 chan[i].duty=chan[i].std.duty.val;
88 chan[i].freqChanged=true;
89 }
90 if (NEW_ARP_STRAT) {
91 chan[i].handleArp();
92 } else if (chan[i].std.arp.had) {
93 if (!chan[i].inPorta) {
94 chan[i].baseFreq=NOTE_FREQUENCY(parent->calcArp(chan[i].note,chan[i].std.arp.val));
95 }
96 chan[i].freqChanged=true;
97 }
98 if (chan[i].std.pitch.had) {
99 if (chan[i].std.pitch.mode) {
100 chan[i].pitch2+=chan[i].std.pitch.val;
101 CLAMP_VAR(chan[i].pitch2,-65535,65535);
102 } else {
103 chan[i].pitch2=chan[i].std.pitch.val;
104 }
105 chan[i].freqChanged=true;
106 }
107 if (chan[i].freqChanged || chan[i].keyOn || chan[i].keyOff) {
108 if (chan[i].active) {
109 chan[i].freq=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);
110 if (chan[i].freq<0) chan[i].freq=0;
111 if (chan[i].freq>65535) chan[i].freq=65535;
112 }
113 if (chan[i].keyOn) {
114 //rWrite(16+i*5,0x80);
115 //chWrite(i,0x04,0x80|chan[i].vol);
116 }
117 if (chan[i].keyOff) {
118 chan[i].freq=0;
119 }
120 if (chan[i].keyOn) chan[i].keyOn=false;
121 if (chan[i].keyOff) chan[i].keyOff=false;
122 chan[i].freqChanged=false;
123 }
124 }
125 }
126
127 int DivPlatformZXBeeper::dispatch(DivCommand c) {
128 switch (c.cmd) {
129 case DIV_CMD_NOTE_ON: {
130 DivInstrument* ins=parent->getIns(chan[c.chan].ins,DIV_INS_BEEPER);
131 if (c.value!=DIV_NOTE_NULL) {
132 chan[c.chan].baseFreq=NOTE_FREQUENCY(c.value);
133 chan[c.chan].freqChanged=true;
134 chan[c.chan].note=c.value;
135 }
136 chan[c.chan].active=true;
137 chan[c.chan].keyOn=true;
138 chan[c.chan].macroInit(ins);
139 chan[c.chan].insChanged=false;
140 break;
141 }
142 case DIV_CMD_NOTE_OFF:
143 if (dumpWrites) addWrite(0xffff0002+(c.chan<<8),0);
144 chan[c.chan].active=false;
145 chan[c.chan].keyOff=true;
146 chan[c.chan].macroInit(NULL);
147 break;
148 case DIV_CMD_NOTE_OFF_ENV:
149 case DIV_CMD_ENV_RELEASE:
150 chan[c.chan].std.release();
151 break;
152 case DIV_CMD_INSTRUMENT:
153 if (chan[c.chan].ins!=c.value || c.value2==1) {
154 chan[c.chan].ins=c.value;
155 chan[c.chan].insChanged=true;
156 }
157 break;
158 case DIV_CMD_VOLUME:
159 if (chan[c.chan].vol!=c.value) {
160 chan[c.chan].vol=c.value;
161 if (!chan[c.chan].std.vol.has) {
162 chan[c.chan].outVol=c.value;
163 }
164 }
165 break;
166 case DIV_CMD_GET_VOLUME:
167 if (chan[c.chan].std.vol.has) {
168 return chan[c.chan].vol;
169 }
170 return chan[c.chan].outVol;
171 break;
172 case DIV_CMD_PITCH:
173 chan[c.chan].pitch=c.value;
174 chan[c.chan].freqChanged=true;
175 break;
176 case DIV_CMD_NOTE_PORTA: {
177 int destFreq=NOTE_FREQUENCY(c.value2);
178 bool return2=false;
179 if (destFreq>chan[c.chan].baseFreq) {
180 chan[c.chan].baseFreq+=c.value;
181 if (chan[c.chan].baseFreq>=destFreq) {
182 chan[c.chan].baseFreq=destFreq;
183 return2=true;
184 }
185 } else {
186 chan[c.chan].baseFreq-=c.value;
187 if (chan[c.chan].baseFreq<=destFreq) {
188 chan[c.chan].baseFreq=destFreq;
189 return2=true;
190 }
191 }
192 chan[c.chan].freqChanged=true;
193 if (return2) {
194 chan[c.chan].inPorta=false;
195 return 2;
196 }
197 break;
198 }
199 case DIV_CMD_STD_NOISE_MODE:
200 chan[c.chan].duty=c.value;
201 break;
202 case DIV_CMD_SAMPLE_MODE:
203 if (isMuted[c.chan]) break;
204 curSample=c.value;
205 curSamplePos=0;
206 curSamplePeriod=0;
207 break;
208 case DIV_CMD_LEGATO:
209 chan[c.chan].baseFreq=NOTE_FREQUENCY(c.value+((HACKY_LEGATO_MESS)?(chan[c.chan].std.arp.val):(0)));
210 chan[c.chan].freqChanged=true;
211 chan[c.chan].note=c.value;
212 break;
213 case DIV_CMD_PRE_PORTA:
214 if (chan[c.chan].active && c.value2) {
215 if (parent->song.resetMacroOnPorta) chan[c.chan].macroInit(parent->getIns(chan[c.chan].ins,DIV_INS_BEEPER));
216 }
217 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);
218 chan[c.chan].inPorta=c.value;
219 break;
220 case DIV_CMD_GET_VOLMAX:
221 return 1;
222 break;
223 case DIV_CMD_MACRO_OFF:
224 chan[c.chan].std.mask(c.value,true);
225 break;
226 case DIV_CMD_MACRO_ON:
227 chan[c.chan].std.mask(c.value,false);
228 break;
229 case DIV_CMD_MACRO_RESTART:
230 chan[c.chan].std.restart(c.value);
231 break;
232 default:
233 break;
234 }
235 return 1;
236 }
237
238 void DivPlatformZXBeeper::muteChannel(int ch, bool mute) {
239 isMuted[ch]=mute;
240 }
241
242 void DivPlatformZXBeeper::forceIns() {
243 for (int i=0; i<6; i++) {
244 chan[i].insChanged=true;
245 chan[i].freqChanged=true;
246 }
247 }
248
249 void* DivPlatformZXBeeper::getChanState(int ch) {
250 return &chan[ch];
251 }
252
253 DivMacroInt* DivPlatformZXBeeper::getChanMacroInt(int ch) {
254 return &chan[ch].std;
255 }
256
257 DivDispatchOscBuffer* DivPlatformZXBeeper::getOscBuffer(int ch) {
258 return (ch<1)?oscBuf[ch]:NULL;
259 }
260
261 unsigned char* DivPlatformZXBeeper::getRegisterPool() {
262 ulaOut=sOffTimer?0x10:0x08;
263 return &ulaOut;
264 }
265
266 int DivPlatformZXBeeper::getRegisterPoolSize() {
267 return 1;
268 }
269
270 void DivPlatformZXBeeper::reset() {
271 memset(regPool,0,128);
272 for (int i=0; i<6; i++) {
273 chan[i]=DivPlatformZXBeeper::Channel();
274 chan[i].std.setEngine(parent);
275 }
276 if (dumpWrites) {
277 addWrite(0xffffffff,0);
278 }
279 lastPan=0xff;
280 memset(tempL,0,32*sizeof(int));
281 memset(tempR,0,32*sizeof(int));
282 cycles=0;
283 curChan=0;
284 sOffTimer=0;
285 ulaOut=0;
286 curSample=-1;
287 curSamplePos=0;
288 curSamplePeriod=0;
289 sampleOut=false;
290 }
291
292 bool DivPlatformZXBeeper::keyOffAffectsArp(int ch) {
293 return true;
294 }
295
296 void DivPlatformZXBeeper::notifyWaveChange(int wave) {
297 }
298
299 void DivPlatformZXBeeper::notifyInsDeletion(void* ins) {
300 for (int i=0; i<6; i++) {
301 chan[i].std.notifyInsDeletion((DivInstrument*)ins);
302 }
303 }
304
305 void DivPlatformZXBeeper::setFlags(const DivConfig& flags) {
306 // TODO: where's ZX Spectrum 48K?!
307 if (flags.getInt("clockSel",0)) {
308 chipClock=COLOR_PAL*4.0/5.0;
309 } else {
310 chipClock=COLOR_NTSC;
311 }
312 CHECK_CUSTOM_CLOCK;
313 rate=chipClock/4;
314 for (int i=0; i<6; i++) {
315 oscBuf[i]->setRate(rate);
316 }
317 }
318
319 void DivPlatformZXBeeper::poke(unsigned int addr, unsigned short val) {
320
321 }
322
323 void DivPlatformZXBeeper::poke(std::vector<DivRegWrite>& wlist) {
324
325 }
326
327 int DivPlatformZXBeeper::init(DivEngine* p, int channels, int sugRate, const DivConfig& flags) {
328 parent=p;
329 dumpWrites=false;
330 skipRegisterWrites=false;
331 for (int i=0; i<6; i++) {
332 isMuted[i]=false;
333 oscBuf[i]=new DivDispatchOscBuffer;
334 }
335 setFlags(flags);
336 reset();
337 return 6;
338 }
339
340 void DivPlatformZXBeeper::quit() {
341 for (int i=0; i<6; i++) {
342 delete oscBuf[i];
343 }
344 }
345
346 DivPlatformZXBeeper::~DivPlatformZXBeeper() {
347 }