Debian Codegraph @

src/engine/export/amigaValidation.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 "amigaValidation.h"
21 #include "../engine.h"
22 #include "../platform/amiga.h"
23
24 struct WaveEntry {
25 unsigned int pos;
26 short width;
27 signed char data[256];
28 WaveEntry():
29 pos(0),
30 width(32) {
31 memset(data,0,256);
32 }
33 };
34
35 struct SampleBookEntry {
36 unsigned int loc;
37 unsigned short len;
38 SampleBookEntry():
39 loc(0),
40 len(0) {}
41 };
42
43 void DivExportAmigaValidation::run() {
44 std::vector<WaveEntry> waves;
45 std::vector<SampleBookEntry> sampleBook;
46 unsigned int wavesDataPtr=0;
47 WaveEntry curWaveState[4];
48 unsigned int sampleBookLoc=0;
49
50 DivPlatformAmiga* amiga=(DivPlatformAmiga*)e->getDispatch(0);
51
52 e->stop();
53 e->repeatPattern=false;
54 e->setOrder(0);
55 EXTERN_BUSY_BEGIN_SOFT;
56
57 // determine loop point
58 int loopOrder=0;
59 int loopRow=0;
60 int loopEnd=0;
61 e->walkSong(loopOrder,loopRow,loopEnd);
62
63 e->curOrder=0;
64 e->freelance=false;
65 e->playing=false;
66 e->extValuePresent=false;
67 e->remainingLoops=-1;
68
69 // play the song ourselves
70 bool done=false;
71
72 // sample.bin
73 logAppend("writing samples...");
74 SafeWriter* sample=new SafeWriter;
75 sample->init();
76 for (int i=0; i<256; i++) {
77 sample->writeI(0);
78 }
79 sample->write(&((const unsigned char*)amiga->getSampleMem(0))[0x400],amiga->getSampleMemUsage(0)-0x400);
80 if (sample->tell()&1) sample->writeC(0);
81
82 // seq.bin
83 logAppend("making sequence...");
84 SafeWriter* seq=new SafeWriter;
85 seq->init();
86
87 amiga->toggleRegisterDump(true);
88
89 // write song data
90 e->playSub(false);
91 size_t songTick=0;
92 size_t lastTick=0;
93 //bool writeLoop=false;
94 int loopPos=-1;
95 for (int i=0; i<e->chans; i++) {
96 e->chan[i].wentThroughNote=false;
97 e->chan[i].goneThroughNote=false;
98 }
99 while (!done) {
100 if (loopPos==-1) {
101 if (loopOrder==e->curOrder && loopRow==e->curRow && e->ticks==1) {
102 //writeLoop=true;
103 }
104 }
105 if (e->nextTick(false,true)) {
106 done=true;
107 amiga->getRegisterWrites().clear();
108 if (lastTick!=songTick) {
109 int delta=songTick-lastTick;
110 if (delta==1) {
111 seq->writeC(0xf1);
112 } else if (delta<256) {
113 seq->writeC(0xf2);
114 seq->writeC(delta-1);
115 } else if (delta<32768) {
116 seq->writeC(0xf3);
117 seq->writeS_BE(delta-1);
118 }
119 lastTick=songTick;
120 }
121 break;
122 }
123 // check wavetable changes
124 for (int i=0; i<4; i++) {
125 if (amiga->chan[i].useWave) {
126 if ((amiga->chan[i].audLen*2)!=curWaveState[i].width || memcmp(curWaveState[i].data,&(((signed char*)amiga->getSampleMem())[i<<8]),amiga->chan[i].audLen*2)!=0) {
127 curWaveState[i].width=amiga->chan[i].audLen*2;
128 memcpy(curWaveState[i].data,&(((signed char*)amiga->getSampleMem())[i<<8]),amiga->chan[i].audLen*2);
129
130 int waveNum=-1;
131 for (size_t j=0; j<waves.size(); j++) {
132 if (waves[j].width!=curWaveState[i].width) continue;
133 if (memcmp(waves[j].data,curWaveState[i].data,curWaveState[i].width)==0) {
134 waveNum=j;
135 break;
136 }
137 }
138
139 if (waveNum==-1) {
140 // write new wavetable
141 waveNum=(int)waves.size();
142 curWaveState[i].pos=wavesDataPtr;
143 waves.push_back(curWaveState[i]);
144 wavesDataPtr+=curWaveState[i].width;
145 }
146
147 if (waveNum<256) {
148 seq->writeC((i<<4)|3);
149 seq->writeC(waveNum);
150 } else if (waveNum<65536) {
151 seq->writeC((i<<4)|4);
152 seq->writeS_BE(waveNum);
153 } else {
154 seq->writeC((i<<4)|1);
155 seq->writeC(waves[waveNum].pos>>16);
156 seq->writeC(waves[waveNum].pos>>8);
157 seq->writeC(waves[waveNum].pos);
158 seq->writeS_BE(waves[waveNum].width);
159 }
160 }
161 }
162 }
163
164 // get register writes
165 std::vector<DivRegWrite>& writes=amiga->getRegisterWrites();
166 for (DivRegWrite& j: writes) {
167 if (lastTick!=songTick) {
168 int delta=songTick-lastTick;
169 if (delta==1) {
170 seq->writeC(0xf1);
171 } else if (delta<256) {
172 seq->writeC(0xf2);
173 seq->writeC(delta-1);
174 } else if (delta<32768) {
175 seq->writeC(0xf3);
176 seq->writeS_BE(delta-1);
177 }
178 lastTick=songTick;
179 }
180 if (j.addr>=0x200) { // direct loc/len change
181 if (j.addr&4) { // len
182 int sampleBookIndex=-1;
183 for (size_t i=0; i<sampleBook.size(); i++) {
184 if (sampleBook[i].loc==sampleBookLoc && sampleBook[i].len==(j.val&0xffff)) {
185 sampleBookIndex=i;
186 break;
187 }
188 }
189 if (sampleBookIndex==-1) {
190 if (sampleBook.size()<256) {
191 SampleBookEntry e;
192 e.loc=sampleBookLoc;
193 e.len=j.val&0xffff;
194 sampleBookIndex=sampleBook.size();
195 sampleBook.push_back(e);
196 }
197 }
198
199 if (sampleBookIndex==-1) {
200 seq->writeC((j.addr&3)<<4);
201 seq->writeC(sampleBookLoc>>16);
202 seq->writeC(sampleBookLoc>>8);
203 seq->writeC(sampleBookLoc);
204 seq->writeS_BE(j.val);
205 } else {
206 seq->writeC(((j.addr&3)<<4)|2);
207 seq->writeC(sampleBookIndex);
208 }
209 } else { // loc
210 sampleBookLoc=j.val;
211 }
212 } else if (j.addr<0xa0) {
213 // don't write INTENA
214 if ((j.addr&15)!=10) {
215 seq->writeC(0xf0|(j.addr&15));
216 seq->writeS_BE(j.val);
217 }
218 } else if ((j.addr&15)!=0 && (j.addr&15)!=2 && (j.addr&15)!=4) {
219 seq->writeC(j.addr-0xa0);
220 if ((j.addr&15)==8) {
221 seq->writeC(j.val);
222 } else {
223 seq->writeS_BE(j.val);
224 }
225 }
226 }
227 writes.clear();
228
229 songTick++;
230 }
231 // end of song
232 seq->writeC(0xff);
233
234 amiga->toggleRegisterDump(false);
235
236 e->remainingLoops=-1;
237 e->playing=false;
238 e->freelance=false;
239 e->extValuePresent=false;
240
241 EXTERN_BUSY_END;
242
243 // wave.bin
244 logAppend("writing wavetables...");
245 SafeWriter* wave=new SafeWriter;
246 wave->init();
247 for (WaveEntry& i: waves) {
248 wave->write(i.data,i.width);
249 }
250
251 // sbook.bin
252 logAppend("writing sample book...");
253 SafeWriter* sbook=new SafeWriter;
254 sbook->init();
255 for (SampleBookEntry& i: sampleBook) {
256 // 8 bytes per entry
257 sbook->writeI_BE(i.loc);
258 sbook->writeI_BE(i.len);
259 }
260
261 // wbook.bin
262 logAppend("writing wavetable book...");
263 SafeWriter* wbook=new SafeWriter;
264 wbook->init();
265 for (WaveEntry& i: waves) {
266 wbook->writeC(i.width);
267 wbook->writeC(i.pos>>16);
268 wbook->writeC(i.pos>>8);
269 wbook->writeC(i.pos);
270 }
271
272 // finish
273 output.reserve(5);
274 output.push_back(DivROMExportOutput("sbook.bin",sbook));
275 output.push_back(DivROMExportOutput("wbook.bin",wbook));
276 output.push_back(DivROMExportOutput("sample.bin",sample));
277 output.push_back(DivROMExportOutput("wave.bin",wave));
278 output.push_back(DivROMExportOutput("seq.bin",seq));
279
280 logAppend("finished!");
281
282 running=false;
283 }
284
285 bool DivExportAmigaValidation::go(DivEngine* eng) {
286 e=eng;
287 running=true;
288 exportThread=new std::thread(&DivExportAmigaValidation::run,this);
289 return true;
290 }
291
292 void DivExportAmigaValidation::wait() {
293 if (exportThread!=NULL) {
294 exportThread->join();
295 delete exportThread;
296 }
297 }
298
299 void DivExportAmigaValidation::abort() {
300 wait();
301 }
302
303 bool DivExportAmigaValidation::isRunning() {
304 return running;
305 }
306
307 bool DivExportAmigaValidation::hasFailed() {
308 return false;
309 }