Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ROM export command line #2293

Merged
merged 5 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions doc/8-advanced/command-line.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,26 @@ the following parameters may be used:
- `-cmdout path`: output command stream dump to `path`.
- you must provide a file, otherwise Furnace will quit.

- `-romout path`: output ROM file export to `path`.
- you must provide a file, otherwise Furnace will quit.
- there must be an available ROM export target for the system.

- `-romconf key=value`: set a configuration parameter for `-romout`.
- you may use this multiple times to set multiple parameters.
- Amiga Validation
- no parameters.
- Commander X16 ZSM
- `zsmrate`: tick rate (Hz), default: `60`
- `loop`: loop song, default: `true`
- `optimize`: optimize size, default: `true`
- Atari 2600 (TIunA)
- `baseLabel`: base song label name, default: `song`
- `firstBankSize`: max size in first bank, default: `3072`
- `otherBankSize`: max size in other banks, default: `4048`
- `sysToExport`: TIA chip index, default: `-1` (find first)
- Atari 8-bit SAP-R
- no parameters.

- `-txtout path`: output text file export to `path`.
- you must provide a file, otherwise Furnace will quit.

Expand Down
2 changes: 2 additions & 0 deletions src/engine/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,8 @@ class DivEngine {

// get ROM export definition
const DivROMExportDef* getROMExportDef(DivROMExportOptions opt);
// check whether ROM export option is viable for current song
bool isROMExportViable(DivROMExportOptions opt);

// convert sample rate format
int fileToDivRate(int frate);
Expand Down
45 changes: 45 additions & 0 deletions src/engine/exportDef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,51 @@ const DivROMExportDef* DivEngine::getROMExportDef(DivROMExportOptions opt) {
return romExportDefs[opt];
}

bool DivEngine::isROMExportViable(DivROMExportOptions opt)
{
const DivROMExportDef* newDef=getROMExportDef(opt);
if (newDef==NULL) {
return false;
}

unsigned char sysReqCount[DIV_SYSTEM_MAX];
memset(sysReqCount,0,DIV_SYSTEM_MAX);
for (int i=0; i<song.systemLen; i++) {
sysReqCount[song.system[i]]++;
}

unsigned char defReqCount[DIV_SYSTEM_MAX];
memset(defReqCount,0,DIV_SYSTEM_MAX);
for (DivSystem j: newDef->requisites) {
defReqCount[j]++;
}

switch (newDef->requisitePolicy) {
case DIV_REQPOL_EXACT:
for (int j=0; j<DIV_SYSTEM_MAX; j++) {
if (defReqCount[j]!=sysReqCount[j]) {
return false;
}
}
break;
case DIV_REQPOL_ANY:
for (int j=0; j<DIV_SYSTEM_MAX; j++) {
if (defReqCount[j]>sysReqCount[j]) {
return false;
}
}
break;
case DIV_REQPOL_LAX:
for (DivSystem j: newDef->requisites) {
if (defReqCount[j]<=sysReqCount[j]) {
return true;
}
}
return false;
}
return true;
}

void DivEngine::registerROMExports() {
logD("registering ROM exports...");

Expand Down
53 changes: 3 additions & 50 deletions src/gui/gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -769,60 +769,13 @@ void FurnaceGUI::autoDetectSystem() {
}

void FurnaceGUI::updateROMExportAvail() {
unsigned char sysReqCount[DIV_SYSTEM_MAX];
unsigned char defReqCount[DIV_SYSTEM_MAX];

memset(sysReqCount,0,DIV_SYSTEM_MAX);
for (int i=0; i<e->song.systemLen; i++) {
sysReqCount[e->song.system[i]]++;
}

memset(romExportAvail,0,sizeof(bool)*DIV_ROM_MAX);
romExportExists=false;

for (int i=0; i<DIV_ROM_MAX; i++) {
const DivROMExportDef* newDef=e->getROMExportDef((DivROMExportOptions)i);
if (newDef!=NULL) {
// check for viability
bool viable=true;

memset(defReqCount,0,DIV_SYSTEM_MAX);
for (DivSystem j: newDef->requisites) {
defReqCount[j]++;
}

switch (newDef->requisitePolicy) {
case DIV_REQPOL_EXACT:
for (int j=0; j<DIV_SYSTEM_MAX; j++) {
if (defReqCount[j]!=sysReqCount[j]) {
viable=false;
break;
}
}
break;
case DIV_REQPOL_ANY:
for (int j=0; j<DIV_SYSTEM_MAX; j++) {
if (defReqCount[j]>sysReqCount[j]) {
viable=false;
break;
}
}
break;
case DIV_REQPOL_LAX:
viable=false;
for (DivSystem j: newDef->requisites) {
if (defReqCount[j]<=sysReqCount[j]) {
viable=true;
break;
}
}
break;
}

if (viable) {
romExportAvail[i]=true;
romExportExists=true;
}
if (e->isROMExportViable((DivROMExportOptions)i)) {
romExportAvail[i]=true;
romExportExists=true;
}
}

Expand Down
82 changes: 81 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,12 @@ FurnaceCLI cli;
String outName;
String vgmOutName;
String cmdOutName;
String romOutName;
String txtOutName;
int benchMode=0;
int subsong=-1;
DivAudioExportOptions exportOptions;
DivConfig romExportConfig;

#ifdef HAVE_GUI
bool consoleMode=false;
Expand Down Expand Up @@ -438,6 +440,24 @@ TAParamResult pCmdOut(String val) {
return TA_PARAM_SUCCESS;
}

TAParamResult pROMOut(String val) {
romOutName=val;
e.setAudio(DIV_AUDIO_DUMMY);
return TA_PARAM_SUCCESS;
}

TAParamResult pROMConf(String val) {
size_t eqSplit=val.find_first_of('=');
if (eqSplit==String::npos) {
logE("invalid romconf parameter, must contain '=' as in: <key>=<value>");
return TA_PARAM_ERROR;
}
String key=val.substr(0,eqSplit);
String param=val.substr(eqSplit+1);
romExportConfig.set(key,param);
return TA_PARAM_SUCCESS;
}

TAParamResult pTxtOut(String val) {
txtOutName=val;
e.setAudio(DIV_AUDIO_DUMMY);
Expand All @@ -461,6 +481,8 @@ void initParams() {
params.push_back(TAParam("O","vgmout",true,pVGMOut,"<filename>","output .vgm data"));
params.push_back(TAParam("D","direct",false,pDirect,"","set VGM export direct stream mode"));
params.push_back(TAParam("C","cmdout",true,pCmdOut,"<filename>","output command stream"));
params.push_back(TAParam("r","romout",true,pROMOut,"<filename|path>","export ROM file, or path for multi-file export"));
params.push_back(TAParam("R","romconf",true,pROMConf,"<key>=<value>","set configuration parameter for ROM export"));
params.push_back(TAParam("t","txtout",true,pTxtOut,"<filename>","export as text file"));
params.push_back(TAParam("L","loglevel",true,pLogLevel,"debug|info|warning|error","set the log level (info by default)"));
params.push_back(TAParam("v","view",true,pView,"pattern|commands|nothing","set visualization (nothing by default)"));
Expand Down Expand Up @@ -557,6 +579,7 @@ int main(int argc, char** argv) {
outName="";
vgmOutName="";
cmdOutName="";
romOutName="";
txtOutName="";

// load config for locale
Expand Down Expand Up @@ -725,7 +748,7 @@ int main(int argc, char** argv) {
return 1;
}

const bool outputMode = outName!="" || vgmOutName!="" || cmdOutName!="" || txtOutName!="";
const bool outputMode = outName!="" || vgmOutName!="" || cmdOutName!="" || romOutName!="" || txtOutName!="";

if (fileName.empty() && (benchMode || infoMode || outputMode)) {
logE("provide a file!");
Expand Down Expand Up @@ -893,6 +916,63 @@ int main(int argc, char** argv) {
e.saveAudio(outName.c_str(),exportOptions);
e.waitAudioFile();
}
if (romOutName!="") {
e.setConsoleMode(true);
// select ROM target type
DivROMExportOptions romTarget = DIV_ROM_ABSTRACT;
String lowerCase=romOutName;
for (char& i: lowerCase) {
if (i>='A' && i<='Z') i+='a'-'A';
}
for (int i=0; i<DIV_ROM_MAX; i++) {
DivROMExportOptions opt = (DivROMExportOptions)i;
if (e.isROMExportViable(opt)) {
const DivROMExportDef* newDef=e.getROMExportDef((DivROMExportOptions)i);
if (newDef->fileExt &&
lowerCase.length()>=strlen(newDef->fileExt) &&
lowerCase.substr(lowerCase.length()-strlen(newDef->fileExt))==newDef->fileExt) {
romTarget = opt;
break; // extension matched, stop searching
}
if (romTarget == DIV_ROM_ABSTRACT) {
romTarget = opt; // use first viable, but keep searching for extension match
}
}
}
if (romTarget > DIV_ROM_ABSTRACT && romTarget < DIV_ROM_MAX) {
DivROMExport* pendingExport = e.buildROM(romTarget);
if (pendingExport==NULL) {
reportError(_("could not create exporter! you may want to report this issue..."));
} else {
pendingExport->setConf(romExportConfig);
if (pendingExport->go(&e)) {
pendingExport->wait();
if (!pendingExport->hasFailed()) {
for (DivROMExportOutput& i: pendingExport->getResult()) {
String path=romOutName;
if (e.getROMExportDef(romTarget)->multiOutput) {
path+=DIR_SEPARATOR_STR;
path+=i.name;
}
FILE* f=ps_fopen(path.c_str(),"wb");
if (f!=NULL) {
fwrite(i.data->getFinalBuf(),1,i.data->size(),f);
fclose(f);
} else {
reportError(fmt::sprintf(_("could not open file! (%s)"),strerror(errno)));
}
}
} else {
reportError(fmt::sprintf(_("ROM export failed! (%s)"),e.getLastError()));
}
} else {
reportError(_("could not begin exporting process! TODO: elaborate"));
}
}
} else {
reportError(_("no matching ROM export target is available."));
}
}
if (txtOutName!="") {
e.setConsoleMode(true);
SafeWriter* w=e.saveText(false);
Expand Down