Debian Codegraph @

src/engine/platform/pv1000.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 "pv1000.h"
21 #include "../engine.h"
22 #include <math.h>
23
24 #define rWrite(a,v) {regPool[(a)]=(v)&0xff; d65010g031_write(&d65010g031,a,v);}
25
26 #define CHIP_DIVIDER 1024
27
28 const char* regCheatSheetPV1000[]={
29 "CH1_Pitch", "00",
30 "CH2_Pitch", "01",
31 "CH3_Pitch", "02",
32 "Control", "03",
33 NULL
34 };
35
36 const char** DivPlatformPV1000::getRegisterSheet() {
37 return regCheatSheetPV1000;
38 }
39
40 void DivPlatformPV1000::acquire(short** buf, size_t len) {
41 for (int i=0; i<3; i++) {
42 oscBuf[i]->begin(len);
43 }
44
45 for (size_t h=0; h<len; h++) {
46 short samp=d65010g031_sound_tick(&d65010g031,1);
47 buf[0][h]=samp;
48 for (int i=0; i<3; i++) {
49 oscBuf[i]->putSample(h,MAX(d65010g031.out[i]<<2,0));
50 }
51 }
52
53 for (int i=0; i<3; i++) {
54 oscBuf[i]->end(len);
55 }
56 }
57
58 void DivPlatformPV1000::tick(bool sysTick) {
59 for (int i=0; i<3; i++) {
60 chan[i].std.next();
61 if (chan[i].std.vol.had) {
62 chan[i].outVol=(chan[i].vol && chan[i].std.vol.val);
63 chan[i].freqChanged=true;
64 }
65 if (NEW_ARP_STRAT) {
66 chan[i].handleArp();
67 } else if (chan[i].std.arp.had) {
68 if (!chan[i].inPorta) {
69 chan[i].baseFreq=NOTE_PERIODIC(parent->calcArp(chan[i].note,chan[i].std.arp.val));
70 }
71 chan[i].freqChanged=true;
72 }
73 if (chan[i].std.pitch.had) {
74 if (chan[i].std.pitch.mode) {
75 chan[i].pitch2+=chan[i].std.pitch.val;
76 CLAMP_VAR(chan[i].pitch2,-32768,32767);
77 } else {
78 chan[i].pitch2=chan[i].std.pitch.val;
79 }
80 chan[i].freqChanged=true;
81 }
82 if (chan[i].freqChanged || chan[i].keyOn || chan[i].keyOff) {
83 chan[i].freq=0x3f-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);
84 if (chan[i].freq<0) chan[i].freq=0;
85 if (chan[i].freq>62) chan[i].freq=62;
86 if (isMuted[i]) chan[i].keyOn=false;
87 if (chan[i].keyOn) {
88 rWrite(i,(isMuted[i] || (chan[i].outVol<=0)) ? 0x3f : chan[i].freq);
89 chan[i].keyOn=false;
90 } else if (chan[i].freqChanged && chan[i].active && !isMuted[i]) {
91 rWrite(i,(isMuted[i] || (chan[i].outVol<=0)) ? 0x3f : chan[i].freq);
92 }
93 if (chan[i].keyOff) {
94 rWrite(i,0x3f);
95 chan[i].keyOff=false;
96 }
97 chan[i].freqChanged=false;
98 }
99 }
100 }
101
102 int DivPlatformPV1000::dispatch(DivCommand c) {
103 switch (c.cmd) {
104 case DIV_CMD_NOTE_ON: {
105 DivInstrument* ins=parent->getIns(chan[c.chan].ins,DIV_INS_PV1000);
106 if (c.value!=DIV_NOTE_NULL) {
107 chan[c.chan].baseFreq=NOTE_PERIODIC(c.value);
108 chan[c.chan].freqChanged=true;
109 chan[c.chan].note=c.value;
110 }
111 chan[c.chan].active=true;
112 chan[c.chan].keyOn=true;
113 chan[c.chan].macroInit(ins);
114 break;
115 }
116 case DIV_CMD_NOTE_OFF:
117 chan[c.chan].active=false;
118 chan[c.chan].keyOff=true;
119 chan[c.chan].macroInit(NULL);
120 break;
121 case DIV_CMD_NOTE_OFF_ENV:
122 case DIV_CMD_ENV_RELEASE:
123 chan[c.chan].std.release();
124 break;
125 case DIV_CMD_INSTRUMENT:
126 if (chan[c.chan].ins!=c.value || c.value2==1) {
127 chan[c.chan].ins=c.value;
128 }
129 break;
130 case DIV_CMD_VOLUME:
131 if (chan[c.chan].vol!=c.value) {
132 chan[c.chan].vol=c.value;
133 if (!chan[c.chan].std.vol.has) {
134 chan[c.chan].outVol=c.value;
135 }
136 if (chan[c.chan].active) {
137 chan[c.chan].freqChanged=true;
138 }
139 }
140 break;
141 case DIV_CMD_GET_VOLUME:
142 return chan[c.chan].vol;
143 break;
144 case DIV_CMD_PITCH:
145 chan[c.chan].pitch=c.value;
146 chan[c.chan].freqChanged=true;
147 break;
148 case DIV_CMD_STD_NOISE_MODE: // ring modulation
149 if (c.value&1) {
150 rWrite(3,3);
151 } else {
152 rWrite(3,2);
153 }
154 break;
155 case DIV_CMD_NOTE_PORTA: {
156 int destFreq=NOTE_PERIODIC(c.value2);
157 bool return2=false;
158 if (destFreq>chan[c.chan].baseFreq) {
159 chan[c.chan].baseFreq+=c.value;
160 if (chan[c.chan].baseFreq>=destFreq) {
161 chan[c.chan].baseFreq=destFreq;
162 return2=true;
163 }
164 } else {
165 chan[c.chan].baseFreq-=c.value;
166 if (chan[c.chan].baseFreq<=destFreq) {
167 chan[c.chan].baseFreq=destFreq;
168 return2=true;
169 }
170 }
171 chan[c.chan].freqChanged=true;
172 if (return2) {
173 chan[c.chan].inPorta=false;
174 return 2;
175 }
176 break;
177 }
178 case DIV_CMD_LEGATO:
179 chan[c.chan].baseFreq=NOTE_PERIODIC(c.value+((HACKY_LEGATO_MESS)?(chan[c.chan].std.arp.val):(0)));
180 chan[c.chan].freqChanged=true;
181 chan[c.chan].note=c.value;
182 break;
183 case DIV_CMD_PRE_PORTA:
184 if (chan[c.chan].active && c.value2) {
185 if (parent->song.resetMacroOnPorta) chan[c.chan].macroInit(parent->getIns(chan[c.chan].ins,DIV_INS_PV1000));
186 }
187 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);
188 chan[c.chan].inPorta=c.value;
189 break;
190 case DIV_CMD_GET_VOLMAX:
191 return 1;
192 break;
193 case DIV_CMD_MACRO_OFF:
194 chan[c.chan].std.mask(c.value,true);
195 break;
196 case DIV_CMD_MACRO_ON:
197 chan[c.chan].std.mask(c.value,false);
198 break;
199 case DIV_CMD_MACRO_RESTART:
200 chan[c.chan].std.restart(c.value);
201 break;
202 default:
203 break;
204 }
205 return 1;
206 }
207
208 void DivPlatformPV1000::muteChannel(int ch, bool mute) {
209 isMuted[ch]=mute;
210 if (mute) {
211 chan[ch].keyOff=true;
212 } else if (chan[ch].active) {
213 chan[ch].keyOn=true;
214 chan[ch].freqChanged=true;
215 }
216 }
217
218 void DivPlatformPV1000::forceIns() {
219 for (int i=0; i<3; i++) {
220 chan[i].insChanged=true;
221 chan[i].freqChanged=true;
222 }
223 }
224
225 void* DivPlatformPV1000::getChanState(int ch) {
226 return &chan[ch];
227 }
228
229 DivMacroInt* DivPlatformPV1000::getChanMacroInt(int ch) {
230 return &chan[ch].std;
231 }
232
233 DivDispatchOscBuffer* DivPlatformPV1000::getOscBuffer(int ch) {
234 return oscBuf[ch];
235 }
236
237 unsigned char* DivPlatformPV1000::getRegisterPool() {
238 return regPool;
239 }
240
241 int DivPlatformPV1000::getRegisterPoolSize() {
242 return 4;
243 }
244
245 void DivPlatformPV1000::reset() {
246 memset(regPool,0,4);
247 for (int i=0; i<3; i++) {
248 chan[i]=Channel();
249 chan[i].std.setEngine(parent);
250 }
251 d65010g031_reset(&d65010g031);
252 // mute
253 rWrite(0,0x3f);
254 rWrite(1,0x3f);
255 rWrite(2,0x3f);
256 rWrite(3,2);
257 }
258
259 int DivPlatformPV1000::getOutputCount() {
260 return 1;
261 }
262
263 void DivPlatformPV1000::notifyInsDeletion(void* ins) {
264 for (int i=0; i<3; i++) {
265 chan[i].std.notifyInsDeletion((DivInstrument*)ins);
266 }
267 }
268
269 void DivPlatformPV1000::setFlags(const DivConfig& flags) {
270 chipClock=COLOR_NTSC*5.0;
271 CHECK_CUSTOM_CLOCK;
272 rate=chipClock/1024;
273 for (int i=0; i<3; i++) {
274 oscBuf[i]->setRate(rate);
275 }
276 }
277
278 void DivPlatformPV1000::poke(unsigned int addr, unsigned short val) {
279 rWrite(addr,val);
280 }
281
282 void DivPlatformPV1000::poke(std::vector<DivRegWrite>& wlist) {
283 for (DivRegWrite& i: wlist) rWrite(i.addr,i.val);
284 }
285
286 bool DivPlatformPV1000::getDCOffRequired() {
287 return true;
288 }
289
290 int DivPlatformPV1000::init(DivEngine* p, int channels, int sugRate, const DivConfig& flags) {
291 parent=p;
292 dumpWrites=false;
293 skipRegisterWrites=false;
294 for (int i=0; i<3; i++) {
295 isMuted[i]=false;
296 oscBuf[i]=new DivDispatchOscBuffer;
297 }
298 setFlags(flags);
299 reset();
300 return 4;
301 }
302
303 void DivPlatformPV1000::quit() {
304 for (int i=0; i<3; i++) {
305 delete oscBuf[i];
306 }
307 }
308
309 DivPlatformPV1000::~DivPlatformPV1000() {
310 }