Debian Codegraph @

src/engine/platform/pokemini.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 "pokemini.h"
21 #include "../engine.h"
22 #include "../../ta-log.h"
23 #include <math.h>
24
25 #define PCSPKR_DIVIDER 16
26 #define CHIP_DIVIDER 1
27
28 const char* regCheatSheetPokeMini[]={
29 "TMR3_SCALE", "1C",
30 "TMR3_OSC", "1D",
31 "TMR3_CTRL_L", "48",
32 "TMR3_CTRL_H", "49",
33 "TMR3_PRE_L", "4A",
34 "TMR3_PRE_H", "4B",
35 "TMR3_PVT_L", "4C",
36 "TMR3_PVT_H", "4D",
37 "TMR3_CNT_L", "4E",
38 "TMR3_CNT_H", "4F",
39 "IO_DIR", "60",
40 "IO_DATA", "61",
41 "AUD_CTRL", "70",
42 "AUD_VOL", "71",
43 NULL
44 };
45
46 const short volTable[4]={
47 0, 16384, 16384, 32767
48 };
49
50 const short scaleTable[8]={
51 1, 7, 31, 63, 127, 255, 1023, 4095
52 };
53
54 const char** DivPlatformPokeMini::getRegisterSheet() {
55 return regCheatSheetPokeMini;
56 }
57
58 void DivPlatformPokeMini::rWrite(unsigned char addr, unsigned char val) {
59 if (addr<128) regPool[addr]=val;
60 switch (addr) {
61 case 0x1c:
62 // ignore
63 break;
64 case 0x1d:
65 // ignore
66 break;
67 case 0x48: case 0x49:
68 on=val&4;
69 if (val&2) pos=0;
70 break;
71 case 0x4a:
72 preset=(preset&0xff00)|val;
73 break;
74 case 0x4b:
75 preset=(preset&0xff)|(val<<8);
76 break;
77 case 0x4c:
78 pivot=(pivot&0xff00)|val;
79 break;
80 case 0x4d:
81 pivot=(pivot&0xff)|(val<<8);
82 break;
83 case 0x71:
84 vol=val&3;
85 break;
86 }
87 }
88
89 void DivPlatformPokeMini::acquire(short** buf, size_t len) {
90 int out=0;
91 oscBuf->begin(len);
92 for (size_t i=0; i<len; i++) {
93 for (int j=0; j<PCSPKR_DIVIDER; j++) {
94 elapsedMain++;
95 if (on) {
96 if ((elapsedMain&scaleTable[timerScale&7])==0) {
97 pos--;
98 if (pos<0) {
99 pos=preset;
100 }
101 }
102 }
103 }
104 if (on) {
105 out=(pos>=pivot && !isMuted[0])?volTable[vol&3]:0;
106 buf[0][i]=out;
107 oscBuf->putSample(i,out);
108 } else {
109 buf[0][i]=0;
110 oscBuf->putSample(i,0);
111 }
112 }
113 oscBuf->end(len);
114 }
115
116 void DivPlatformPokeMini::tick(bool sysTick) {
117 for (int i=0; i<1; i++) {
118 chan[i].std.next();
119 if (chan[i].std.vol.had) {
120 chan[i].outVol=VOL_SCALE_LINEAR(chan[i].vol,chan[i].std.vol.val,2);
121 rWrite(0x71,(chan[i].outVol==2)?3:chan[i].outVol);
122 }
123 if (NEW_ARP_STRAT) {
124 chan[i].handleArp();
125 } else if (chan[i].std.arp.had) {
126 if (!chan[i].inPorta) {
127 chan[i].baseFreq=NOTE_PERIODIC(parent->calcArp(chan[i].note,chan[i].std.arp.val));
128 }
129 chan[i].freqChanged=true;
130 }
131 if (chan[i].std.duty.had) {
132 chan[i].duty=chan[i].std.duty.val;
133 chan[i].freqChanged=true;
134 }
135 if (chan[i].std.pitch.had) {
136 if (chan[i].std.pitch.mode) {
137 chan[i].pitch2+=chan[i].std.pitch.val;
138 CLAMP_VAR(chan[i].pitch2,-32768,32767);
139 } else {
140 chan[i].pitch2=chan[i].std.pitch.val;
141 }
142 chan[i].freqChanged=true;
143 }
144 if (chan[i].freqChanged || chan[i].keyOn || chan[i].keyOff) {
145 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)-1;
146 if (chan[i].freq<0) chan[i].freq=0;
147 if (chan[i].freq>65535) chan[i].freq=65535;
148 if (chan[i].keyOn) {
149 rWrite(0x48,4);
150 }
151 if (chan[i].keyOff) {
152 rWrite(0x48,0);
153 }
154 rWrite(0x4a,chan[i].freq&0xff);
155 rWrite(0x4b,chan[i].freq>>8);
156 int pvt=(chan[i].duty*chan[i].freq)>>8;
157 rWrite(0x4c,pvt&0xff);
158 rWrite(0x4d,pvt>>8);
159 if (chan[i].keyOn) chan[i].keyOn=false;
160 if (chan[i].keyOff) chan[i].keyOff=false;
161 chan[i].freqChanged=false;
162 }
163 }
164 }
165
166 int DivPlatformPokeMini::dispatch(DivCommand c) {
167 switch (c.cmd) {
168 case DIV_CMD_NOTE_ON:
169 if (c.value!=DIV_NOTE_NULL) {
170 chan[c.chan].baseFreq=NOTE_PERIODIC(c.value);
171 chan[c.chan].freqChanged=true;
172 chan[c.chan].note=c.value;
173 }
174 rWrite(0x71,(chan[c.chan].outVol==2)?3:chan[c.chan].outVol);
175 chan[c.chan].active=true;
176 chan[c.chan].keyOn=true;
177 chan[c.chan].macroInit(parent->getIns(chan[c.chan].ins,DIV_INS_POKEMINI));
178 if (!parent->song.brokenOutVol && !chan[c.chan].std.vol.will) {
179 chan[c.chan].outVol=chan[c.chan].vol;
180 }
181 break;
182 case DIV_CMD_NOTE_OFF:
183 chan[c.chan].active=false;
184 chan[c.chan].keyOff=true;
185 chan[c.chan].macroInit(NULL);
186 break;
187 case DIV_CMD_NOTE_OFF_ENV:
188 case DIV_CMD_ENV_RELEASE:
189 chan[c.chan].std.release();
190 break;
191 case DIV_CMD_INSTRUMENT:
192 if (chan[c.chan].ins!=c.value || c.value2==1) {
193 chan[c.chan].ins=c.value;
194 }
195 break;
196 case DIV_CMD_VOLUME:
197 if (chan[c.chan].vol!=c.value) {
198 chan[c.chan].vol=c.value;
199 if (!chan[c.chan].std.vol.has) {
200 chan[c.chan].outVol=c.value;
201 }
202 if (chan[c.chan].active) {
203 rWrite(0x71,(chan[c.chan].outVol==2)?3:chan[c.chan].outVol);
204 }
205 }
206 break;
207 case DIV_CMD_GET_VOLUME:
208 return chan[c.chan].vol;
209 break;
210 case DIV_CMD_PITCH:
211 chan[c.chan].pitch=c.value;
212 chan[c.chan].freqChanged=true;
213 break;
214 case DIV_CMD_NOTE_PORTA: {
215 int destFreq=NOTE_PERIODIC(c.value2);
216 bool return2=false;
217 if (destFreq>chan[c.chan].baseFreq) {
218 chan[c.chan].baseFreq+=c.value;
219 if (chan[c.chan].baseFreq>=destFreq) {
220 chan[c.chan].baseFreq=destFreq;
221 return2=true;
222 }
223 } else {
224 chan[c.chan].baseFreq-=c.value;
225 if (chan[c.chan].baseFreq<=destFreq) {
226 chan[c.chan].baseFreq=destFreq;
227 return2=true;
228 }
229 }
230 chan[c.chan].freqChanged=true;
231 if (return2) {
232 chan[c.chan].inPorta=false;
233 return 2;
234 }
235 break;
236 }
237 case DIV_CMD_LEGATO:
238 chan[c.chan].baseFreq=NOTE_PERIODIC(c.value+((HACKY_LEGATO_MESS)?(chan[c.chan].std.arp.val):(0)));
239 chan[c.chan].freqChanged=true;
240 chan[c.chan].note=c.value;
241 break;
242 case DIV_CMD_PRE_PORTA:
243 if (chan[c.chan].active && c.value2) {
244 if (parent->song.resetMacroOnPorta) chan[c.chan].macroInit(parent->getIns(chan[c.chan].ins,DIV_INS_POKEMINI));
245 }
246 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);
247 chan[c.chan].inPorta=c.value;
248 break;
249 case DIV_CMD_GET_VOLMAX:
250 return 2;
251 break;
252 case DIV_CMD_MACRO_OFF:
253 chan[c.chan].std.mask(c.value,true);
254 break;
255 case DIV_CMD_MACRO_ON:
256 chan[c.chan].std.mask(c.value,false);
257 break;
258 case DIV_CMD_MACRO_RESTART:
259 chan[c.chan].std.restart(c.value);
260 break;
261 default:
262 break;
263 }
264 return 1;
265 }
266
267 void DivPlatformPokeMini::muteChannel(int ch, bool mute) {
268 isMuted[ch]=mute;
269 }
270
271 void DivPlatformPokeMini::forceIns() {
272 for (int i=0; i<1; i++) {
273 chan[i].insChanged=true;
274 }
275 }
276
277 void* DivPlatformPokeMini::getChanState(int ch) {
278 return &chan[ch];
279 }
280
281 DivMacroInt* DivPlatformPokeMini::getChanMacroInt(int ch) {
282 return &chan[ch].std;
283 }
284
285 DivDispatchOscBuffer* DivPlatformPokeMini::getOscBuffer(int ch) {
286 return oscBuf;
287 }
288
289 unsigned char* DivPlatformPokeMini::getRegisterPool() {
290 return regPool;
291 }
292
293 int DivPlatformPokeMini::getRegisterPoolSize() {
294 return 128;
295 }
296
297 void DivPlatformPokeMini::reset() {
298 for (int i=0; i<1; i++) {
299 chan[i]=DivPlatformPokeMini::Channel();
300 chan[i].std.setEngine(parent);
301 }
302 if (dumpWrites) {
303 addWrite(0xffffffff,0);
304 }
305
306 on=false;
307 preset=0;
308 pivot=0;
309 pos=0;
310 timerScale=0;
311 vol=0;
312 preset=0;
313 pivot=0;
314 elapsedMain=0;
315
316 memset(regPool,0,128);
317 }
318
319 bool DivPlatformPokeMini::keyOffAffectsArp(int ch) {
320 return true;
321 }
322
323 void DivPlatformPokeMini::setFlags(const DivConfig& flags) {
324 chipClock=4000000;
325 CHECK_CUSTOM_CLOCK;
326
327 rate=chipClock/PCSPKR_DIVIDER;
328 oscBuf->setRate(rate);
329 }
330
331 void DivPlatformPokeMini::notifyInsDeletion(void* ins) {
332 for (int i=0; i<1; i++) {
333 chan[i].std.notifyInsDeletion((DivInstrument*)ins);
334 }
335 }
336
337 void DivPlatformPokeMini::poke(unsigned int addr, unsigned short val) {
338 rWrite(addr,val);
339 }
340
341 void DivPlatformPokeMini::poke(std::vector<DivRegWrite>& wlist) {
342 for (DivRegWrite& i: wlist) rWrite(i.addr,i.val);
343 }
344
345 int DivPlatformPokeMini::init(DivEngine* p, int channels, int sugRate, const DivConfig& flags) {
346 parent=p;
347 dumpWrites=false;
348 skipRegisterWrites=false;
349 for (int i=0; i<1; i++) {
350 isMuted[i]=false;
351 }
352 oscBuf=new DivDispatchOscBuffer;
353 setFlags(flags);
354
355 reset();
356 return 5;
357 }
358
359 void DivPlatformPokeMini::quit() {
360 delete oscBuf;
361 }
362
363 DivPlatformPokeMini::~DivPlatformPokeMini() {
364 }