| 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 |
// thanks asiekierka! |
| 21 |
// I have ported your code to this ROM export framework. |
| 22 |
|
| 23 |
#include "sapr.h" |
| 24 |
#include "../engine.h" |
| 25 |
#include "../ta-log.h" |
| 26 |
#include <fmt/printf.h> |
| 27 |
#include <array> |
| 28 |
#include <vector> |
| 29 |
|
| 30 |
static String ticksToTime(double rate, int ticks) { |
| 31 |
double timing = ticks / rate; |
| 32 |
|
| 33 |
return fmt::sprintf("%02d:%02d.%03d", |
| 34 |
(int) timing / 60, |
| 35 |
(int) timing % 60, |
| 36 |
(int) (timing * 1000) % 1000 |
| 37 |
); |
| 38 |
} |
| 39 |
|
| 40 |
void DivExportSAPR::run() { |
| 41 |
int sapScanlines=0; // TODO: property! |
| 42 |
int POKEY=-1; |
| 43 |
int IGNORED=0; |
| 44 |
|
| 45 |
// Locate system index. |
| 46 |
for (int i=0; i<e->song.systemLen; i++) { |
| 47 |
if (e->song.system[i] == DIV_SYSTEM_POKEY) { |
| 48 |
if (POKEY>=0) { |
| 49 |
IGNORED++; |
| 50 |
logAppendf("Ignoring duplicate POKEY id %d",i); |
| 51 |
break; |
| 52 |
} |
| 53 |
POKEY=i; |
| 54 |
logAppendf("POKEY detected as chip id %d",i); |
| 55 |
break; |
| 56 |
} else { |
| 57 |
IGNORED++; |
| 58 |
logAppendf("Ignoring chip id %d, system id %d",i,(int)e->song.system[i]); |
| 59 |
break; |
| 60 |
} |
| 61 |
} |
| 62 |
if (POKEY<0) { |
| 63 |
logAppendf("ERROR: Could not find POKEY"); |
| 64 |
failed=true; |
| 65 |
running=false; |
| 66 |
return; |
| 67 |
} |
| 68 |
if (IGNORED>0) { |
| 69 |
logAppendf("WARNING: SAP export ignoring %d unsupported system%c",IGNORED,IGNORED>1?'s':' '); |
| 70 |
} |
| 71 |
|
| 72 |
bool palTiming=(e->song.systemFlags[POKEY].getInt("clockSel",0) != 0); |
| 73 |
int scanlinesPerFrame = (palTiming?312:262); |
| 74 |
size_t tickCount=0; |
| 75 |
std::vector<std::array<uint8_t, 9>> regs; |
| 76 |
|
| 77 |
if (sapScanlines <= 0) { |
| 78 |
sapScanlines = scanlinesPerFrame; |
| 79 |
} |
| 80 |
//double sapRate = (palTiming?49.86:59.92) * scanlinesPerFrame / sapScanlines; |
| 81 |
double sapRate = (palTiming?50:60) * (double)scanlinesPerFrame / (double)sapScanlines; |
| 82 |
|
| 83 |
|
| 84 |
e->stop(); |
| 85 |
e->repeatPattern=false; |
| 86 |
e->setOrder(0); |
| 87 |
|
| 88 |
logAppend("playing and logging register writes..."); |
| 89 |
|
| 90 |
e->synchronizedSoft([&]() { |
| 91 |
double origRate = e->got.rate; |
| 92 |
e->got.rate=sapRate; |
| 93 |
|
| 94 |
// Determine loop point. |
| 95 |
int loopOrder=0; |
| 96 |
int loopRow=0; |
| 97 |
int loopEnd=0; |
| 98 |
e->walkSong(loopOrder,loopRow,loopEnd); |
| 99 |
logAppendf("loop point: %d %d",loopOrder,loopRow); |
| 100 |
e->warnings=""; |
| 101 |
|
| 102 |
// Reset the playback state. |
| 103 |
e->curOrder=0; |
| 104 |
e->freelance=false; |
| 105 |
e->playing=false; |
| 106 |
e->extValuePresent=false; |
| 107 |
e->remainingLoops=-1; |
| 108 |
|
| 109 |
// Prepare to write song data. |
| 110 |
e->playSub(false); |
| 111 |
bool done=false; |
| 112 |
e->disCont[POKEY].dispatch->toggleRegisterDump(true); |
| 113 |
std::array<uint8_t, 9> currRegs; |
| 114 |
|
| 115 |
while (!done) { |
| 116 |
if (e->nextTick(false,true) || !e->playing) { |
| 117 |
done=true; |
| 118 |
for (int i=0; i<e->song.systemLen; i++) { |
| 119 |
e->disCont[i].dispatch->getRegisterWrites().clear(); |
| 120 |
} |
| 121 |
break; |
| 122 |
} |
| 123 |
// get register dumps |
| 124 |
std::vector<DivRegWrite>& writes=e->disCont[POKEY].dispatch->getRegisterWrites(); |
| 125 |
if (writes.size() > 0) { |
| 126 |
logAppendf("saprOps: found %d messages",writes.size()); |
| 127 |
for (DivRegWrite& write: writes) |
| 128 |
if ((write.addr & 0xF) < 9) |
| 129 |
currRegs[write.addr & 0xF] = write.val; |
| 130 |
writes.clear(); |
| 131 |
} |
| 132 |
|
| 133 |
// write wait |
| 134 |
tickCount++; |
| 135 |
int totalWait=e->cycles; |
| 136 |
if (totalWait>0 && !done) { |
| 137 |
while (totalWait) { |
| 138 |
regs.push_back(currRegs); |
| 139 |
totalWait--; |
| 140 |
tickCount++; |
| 141 |
} |
| 142 |
} |
| 143 |
} |
| 144 |
// end of song |
| 145 |
|
| 146 |
// done - close out. |
| 147 |
e->got.rate=origRate; |
| 148 |
e->disCont[POKEY].dispatch->toggleRegisterDump(false); |
| 149 |
|
| 150 |
e->remainingLoops=-1; |
| 151 |
e->playing=false; |
| 152 |
e->freelance=false; |
| 153 |
e->extValuePresent=false; |
| 154 |
}); |
| 155 |
|
| 156 |
logAppend("writing data..."); |
| 157 |
progress[0].amount=0.95f; |
| 158 |
|
| 159 |
auto w = new SafeWriter; |
| 160 |
w->init(); |
| 161 |
// Write SAP header: Author, name, timing, type. |
| 162 |
w->writeText(fmt::sprintf("SAP\r\nAUTHOR \"%s\"\r\nNAME \"%s\"\r\n%s\r\nTYPE R\r\n", |
| 163 |
e->song.author, e->song.name, palTiming ? "PAL" : "NTSC" |
| 164 |
)); |
| 165 |
if (sapScanlines != scanlinesPerFrame) { |
| 166 |
// Fastplay. |
| 167 |
w->writeText(fmt::sprintf("FASTPLAY %d\r\n", sapScanlines)); |
| 168 |
} |
| 169 |
// Time. |
| 170 |
w->writeText(fmt::sprintf("TIME %s\r\n", ticksToTime(sapRate, tickCount))); |
| 171 |
w->writeText("\r\n"); |
| 172 |
// Registers. |
| 173 |
for (auto atRegs : regs) { |
| 174 |
w->write(atRegs.data(), 9 * sizeof(uint8_t)); |
| 175 |
} |
| 176 |
|
| 177 |
output.push_back(DivROMExportOutput("export.sap",w)); |
| 178 |
|
| 179 |
progress[0].amount=1.0f; |
| 180 |
|
| 181 |
logAppend("finished!"); |
| 182 |
|
| 183 |
running=false; |
| 184 |
} |
| 185 |
|
| 186 |
bool DivExportSAPR::go(DivEngine* eng) { |
| 187 |
progress[0].name="Progress"; |
| 188 |
progress[0].amount=0.0f; |
| 189 |
|
| 190 |
e=eng; |
| 191 |
running=true; |
| 192 |
failed=false; |
| 193 |
mustAbort=false; |
| 194 |
exportThread=new std::thread(&DivExportSAPR::run,this); |
| 195 |
return true; |
| 196 |
} |
| 197 |
|
| 198 |
void DivExportSAPR::wait() { |
| 199 |
if (exportThread!=NULL) { |
| 200 |
logV("waiting for export thread..."); |
| 201 |
exportThread->join(); |
| 202 |
delete exportThread; |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
void DivExportSAPR::abort() { |
| 207 |
mustAbort=true; |
| 208 |
wait(); |
| 209 |
} |
| 210 |
|
| 211 |
bool DivExportSAPR::isRunning() { |
| 212 |
return running; |
| 213 |
} |
| 214 |
|
| 215 |
bool DivExportSAPR::hasFailed() { |
| 216 |
return failed; |
| 217 |
} |
| 218 |
|
| 219 |
DivROMExportProgress DivExportSAPR::getProgress(int index) { |
| 220 |
if (index<0 || index>1) return progress[1]; |
| 221 |
return progress[index]; |
| 222 |
} |