Debian Codegraph @

src/engine/export/grub.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 "grub.h"
21 #include "../engine.h"
22 #include "../ta-log.h"
23 #include <fmt/printf.h>
24 #include <array>
25 #include <vector>
26
27 void DivExportGRUB::run() {
28 bool grubExportBin=conf.getBool("exportBin",false);
29
30 int BEEPER=-1;
31 int IGNORED=0;
32
33 // Locate system index.
34 for (int i=0; i<e->song.systemLen; i++) {
35 if (e->song.system[i] == DIV_SYSTEM_PCSPKR) {
36 if (BEEPER>=0) {
37 IGNORED++;
38 logAppendf("Ignoring duplicate Beeper id %d",i);
39 break;
40 }
41 BEEPER=i;
42 logAppendf("PC Speaker detected as chip id %d",i);
43 break;
44 } else {
45 IGNORED++;
46 logAppendf("Ignoring chip id %d, system id %d",i,(int)e->song.system[i]);
47 break;
48 }
49 }
50 if (BEEPER<0) {
51 logAppendf("ERROR: Could not find PC Speaker/Beeper");
52 failed=true;
53 running=false;
54 return;
55 }
56 if (IGNORED>0) {
57 logAppendf("WARNING: GRUB export ignoring unsup sys count: %d",IGNORED);
58 }
59
60 size_t tickCount=0;
61
62 e->stop();
63 e->repeatPattern=false;
64 e->setOrder(0);
65
66 logAppend("playing and logging register writes...");
67
68 int oldFreq = 0;
69 int freq = 0;
70
71 e->synchronizedSoft([&]() {
72 double origRate = e->got.rate;
73 double rate = MIN(e->curSubSong->hz,1000.0);
74 logAppendf("export rate is %d hz",(int)rate);
75 int tempo = (int)(60000.0/(1000.0/rate));
76 e->got.rate=rate;
77
78 // Determine loop point.
79 int loopOrder=0;
80 int loopRow=0;
81 int loopEnd=0;
82 e->walkSong(loopOrder,loopRow,loopEnd);
83 logAppendf("loop point: %d %d",loopOrder,loopRow);
84 e->warnings="";
85
86 auto w = new SafeWriter;
87 w->init();
88
89 // Reset the playback state.
90 e->curOrder=0;
91 e->freelance=false;
92 e->playing=false;
93 e->extValuePresent=false;
94 e->remainingLoops=-1;
95
96 e->disCont[BEEPER].dispatch->toggleRegisterDump(true);
97
98 // Prepare to write song data.
99 e->playSub(false);
100 bool done=false;
101
102 logAppend("writing data...");
103 progress[0].amount=0.15f;
104
105 int wait_tempo = 0;
106 if (grubExportBin)
107 w->writeI(tempo); // write tempo
108 else
109 w->writeText(fmt::sprintf("%d",tempo)); // write tempo
110
111 while (!done) {
112 if (e->nextTick(false,true) || !e->playing) {
113 done=true;
114 }
115
116 // get register dumps
117 uint8_t* regPool = e->disCont[BEEPER].dispatch->getRegisterPool();
118 int chipClock = e->disCont[BEEPER].dispatch->chipClock;
119 freq = (int)(regPool[0]|(regPool[1]<<8));
120 if (freq > 0) freq = chipClock/freq;
121
122 // write wait
123 tickCount++;
124 int totalWait=e->cycles;
125 if (totalWait>0 && !done) {
126 while (totalWait) {
127 wait_tempo++;
128 if (freq != oldFreq || wait_tempo == 65535) {
129 if (grubExportBin) {
130 w->writeS(oldFreq); // pitch
131 w->writeS(wait_tempo); // duration
132 } else {
133 w->writeText(fmt::sprintf(" %d %d", oldFreq, wait_tempo));
134 }
135 oldFreq = freq;
136 wait_tempo = 0;
137 }
138 totalWait--;
139 tickCount++;
140 }
141 }
142 }
143
144 if (!grubExportBin) w->writeText(fmt::sprintf("\n")); // end song
145 // end of song
146
147 // done - close out.
148 e->got.rate=origRate;
149 e->disCont[BEEPER].dispatch->getRegisterWrites().clear();
150 e->disCont[BEEPER].dispatch->toggleRegisterDump(false);
151
152 e->remainingLoops=-1;
153 e->playing=false;
154 e->freelance=false;
155 e->extValuePresent=false;
156
157 output.push_back(DivROMExportOutput(grubExportBin?"export.bin":"export.txt",w));
158 });
159
160
161 progress[0].amount=1.0f;
162
163 logAppend("finished!");
164
165 running=false;
166 }
167
168 bool DivExportGRUB::go(DivEngine* eng) {
169 progress[0].name="Progress";
170 progress[0].amount=0.0f;
171
172 e=eng;
173 running=true;
174 failed=false;
175 mustAbort=false;
176 exportThread=new std::thread(&DivExportGRUB::run,this);
177 return true;
178 }
179
180 void DivExportGRUB::wait() {
181 if (exportThread!=NULL) {
182 logV("waiting for export thread...");
183 exportThread->join();
184 delete exportThread;
185 }
186 }
187
188 void DivExportGRUB::abort() {
189 mustAbort=true;
190 wait();
191 }
192
193 bool DivExportGRUB::isRunning() {
194 return running;
195 }
196
197 bool DivExportGRUB::hasFailed() {
198 return failed;
199 }
200
201 DivROMExportProgress DivExportGRUB::getProgress(int index) {
202 if (index<0 || index>1) return progress[1];
203 return progress[index];
204 }