| 1 |
/* |
| 2 |
CheeseCutter v2 (C) Abaddon. Licensed under GNU GPL. |
| 3 |
*/ |
| 4 |
|
| 5 |
import com.cpu; |
| 6 |
import com.util; |
| 7 |
import ct.base; |
| 8 |
import ct.purge; |
| 9 |
import ct.dump; |
| 10 |
import ct.build; |
| 11 |
import std.stdio; |
| 12 |
import std.string; |
| 13 |
import std.conv; |
| 14 |
import std.file; |
| 15 |
|
| 16 |
enum Command { None, ExportPRG, ExportSID, Dump, Import, Init } |
| 17 |
const string[] exts = [ "", "prg", "sid", "s", "ct", "ct" ]; |
| 18 |
|
| 19 |
bool verbose = true; |
| 20 |
bool noPurge; |
| 21 |
|
| 22 |
void explain(string str) { |
| 23 |
if(verbose) |
| 24 |
writefln(str); |
| 25 |
} |
| 26 |
|
| 27 |
string defineOutfn(int cmd, string infn) { |
| 28 |
string name; |
| 29 |
int r = cast(int)(infn.lastIndexOf('.')); |
| 30 |
if(r <= 0) name = infn; |
| 31 |
else name = infn[0 .. infn.lastIndexOf('.')]; |
| 32 |
return name ~ "." ~ exts[cmd]; |
| 33 |
} |
| 34 |
|
| 35 |
bool doPurge(ref Song song) { |
| 36 |
if(noPurge) return true; |
| 37 |
explain("Purging data..."); |
| 38 |
Purge p = new Purge(song, verbose); |
| 39 |
try { |
| 40 |
p.purgeAll(); |
| 41 |
} |
| 42 |
catch(PurgeException e) { |
| 43 |
writeln(e); |
| 44 |
return false; |
| 45 |
} |
| 46 |
return true; |
| 47 |
} |
| 48 |
|
| 49 |
void validate(ref Song song) { |
| 50 |
explain("Checking validity..."); |
| 51 |
for(int i = 0; i < song.numInstr; i++) { |
| 52 |
//ubyte[] instr = song.getInstrument(i); |
| 53 |
int waveptr = song.wavetablePointer(i); |
| 54 |
int pulseptr = song.pulsetablePointer(i); |
| 55 |
int filtptr = song.filtertablePointer(i); |
| 56 |
if(!song.tWave.isValid(waveptr)) { |
| 57 |
throw new ValidateException(format("Error: instrument %d is not valid (wavetable does not wrap).", i)); |
| 58 |
} |
| 59 |
|
| 60 |
if(!song.tPulse.isValid(pulseptr)) { |
| 61 |
throw new ValidateException(format("Cannot save; pulse %d is not valid.", pulseptr)); |
| 62 |
} |
| 63 |
|
| 64 |
if(!song.tFilter.isValid(filtptr)) { |
| 65 |
throw new ValidateException(format("Cannot save; filter %d is not valid.", filtptr)); |
| 66 |
} |
| 67 |
|
| 68 |
song.seqIterator((int seqno, Sequence s, Element e) { |
| 69 |
if(e.cmd.value >= 0x80 && e.cmd.value <= 0x9f) { |
| 70 |
int idx = song.chordIndexTable[e.cmd.value & 0x1f]; |
| 71 |
for(int i = idx; i < 128; i++) { |
| 72 |
if(song.chordTable[i] >= 0x80) return; |
| 73 |
} |
| 74 |
throw new ValidateException(format("sequence $%02x, could not find end for chord %x. The song has a 8x command pointing to nonexistant chord program.", seqno, e.cmd.value & 0x1f)); |
| 75 |
|
| 76 |
} |
| 77 |
}); |
| 78 |
|
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
class ValidateException : Exception { |
| 83 |
this(string msg) { |
| 84 |
super(msg); |
| 85 |
} |
| 86 |
|
| 87 |
override string toString() { |
| 88 |
return "Validation error: " ~ msg; |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
int main(string[] args) { |
| 93 |
int relocAddress = 0x1000, zpAddress = 0; |
| 94 |
int[] speeds, masks; |
| 95 |
|
| 96 |
// these two use PSID ranges (1..32) |
| 97 |
int defaultTune = 1, singleSubtune = -1; |
| 98 |
|
| 99 |
bool outfnDefined = false, infnDefined = false; |
| 100 |
int command; |
| 101 |
Song insong; |
| 102 |
string infn, outfn; |
| 103 |
string[3] infns; |
| 104 |
|
| 105 |
speeds.length = 32; |
| 106 |
masks.length = 32; |
| 107 |
void printheader() { |
| 108 |
enum hdr = "CheeseCutter 2 utilities" ~ com.util.versionInfo; |
| 109 |
writefln(hdr); |
| 110 |
writefln("\nUsage: \t%s <command> <options> <infile> <-o outfile>",args[0]); |
| 111 |
writefln("\t%s import <infile> <infile2> <-o outfile>",args[0]); |
| 112 |
writefln("\t%s init <binaryfile> <-o outfile>",args[0]); |
| 113 |
writefln("\nCommands:"); |
| 114 |
writefln(" prg Export song (.ct) to PRG file"); |
| 115 |
writefln(" sid Export song (.ct) to SID file"); |
| 116 |
writefln(" dump Dump song data to assembler source (BETA)"); |
| 117 |
writefln(" import Copy data from another song without overwriting the player"); |
| 118 |
writefln(" init Create a fresh .ct from player binary"); |
| 119 |
writefln("\nGeneral options:"); |
| 120 |
writefln(" -o <outfile> Set output filename (by default gathered from input filename)"); |
| 121 |
writefln("\nExport options:"); |
| 122 |
// writefln(" -n Do not purge before exporting/dumping (leaves unused data)"); |
| 123 |
writefln(" -r <addr> Relocate output to address (default = $1000)"); |
| 124 |
writefln(" -d <num> Set the default subtune (1-" ~ to!string(ct.base.SUBTUNE_MAX) ~ ")"); |
| 125 |
// writefln(" -s [subtune]:[speed],... Set speeds for subtunes"); |
| 126 |
// writefln(" -c [subtune]:[voicemask],...Set voice bitmasks for subtunes"); |
| 127 |
writefln(" -s <num> Export single subtune (1-" ~ to!string(ct.base.SUBTUNE_MAX) ~ ") (disables -d)"); |
| 128 |
writefln(" -zp <num> Relocate zero page (valid range 2-$fe)"); |
| 129 |
writefln(" -q Don't output information"); |
| 130 |
writefln("\nPrefix value options with '0x' or '$' to indicate a hexadecimal value."); |
| 131 |
} |
| 132 |
|
| 133 |
if(args.length < 2) { |
| 134 |
printheader(); |
| 135 |
return 0; |
| 136 |
} |
| 137 |
|
| 138 |
try { |
| 139 |
switch(args[1]) { |
| 140 |
case "prg", "buildprg": |
| 141 |
command = Command.ExportPRG; |
| 142 |
break; |
| 143 |
case "sid", "build": |
| 144 |
command = Command.ExportSID; |
| 145 |
break; |
| 146 |
case "dump": |
| 147 |
command = Command.Dump; |
| 148 |
break; |
| 149 |
case "import": |
| 150 |
command = Command.Import; |
| 151 |
break; |
| 152 |
case "init": |
| 153 |
command = Command.Init; |
| 154 |
break; |
| 155 |
default: |
| 156 |
throw new UserException(format("command '%s' not recognized.",args[1])); |
| 157 |
} |
| 158 |
int infncounter = 0; |
| 159 |
|
| 160 |
if(args.length >= 2) { |
| 161 |
for(int argp = 2; argp < args.length; argp++) { |
| 162 |
string nextArg() { |
| 163 |
if(argp+1 >= args.length || args[argp+1][0] == '-') |
| 164 |
throw new UserException("Missing value for option '" ~ args[argp] ~"'"); |
| 165 |
argp++; |
| 166 |
return args[argp]; |
| 167 |
} |
| 168 |
switch(args[argp]) { |
| 169 |
case "-n": |
| 170 |
noPurge = true; |
| 171 |
break; |
| 172 |
case "-r": |
| 173 |
if(command != Command.ExportSID && |
| 174 |
command != Command.ExportPRG) |
| 175 |
throw new UserException("Option available only with exporting commands."); |
| 176 |
int r = str2Value2(nextArg()); |
| 177 |
if(r < 0x200 || r > 0xf900) |
| 178 |
throw new UserException("-r: reloc address out of range"); |
| 179 |
relocAddress = cast(ushort)r; |
| 180 |
break; |
| 181 |
case "-s": |
| 182 |
if(command != Command.ExportSID && |
| 183 |
command != Command.ExportPRG) |
| 184 |
throw new UserException("Option available only with exporting commands."); |
| 185 |
//parseList(speeds, nextArg()); |
| 186 |
int value = str2Value2(nextArg()); |
| 187 |
if(value < 1 || value > ct.base.SUBTUNE_MAX) |
| 188 |
throw new UserException(format("Valid range for subtunes is 1 - %d.", ct.base.SUBTUNE_MAX)); |
| 189 |
singleSubtune = value; |
| 190 |
break; |
| 191 |
case "-c": |
| 192 |
if(command != Command.ExportSID && |
| 193 |
command != Command.ExportPRG) |
| 194 |
throw new UserException("Option available only with exporting commands."); |
| 195 |
parseList(masks, nextArg()); |
| 196 |
break; |
| 197 |
case "-d": |
| 198 |
if(command != Command.ExportSID) |
| 199 |
throw new UserException("Option available only when exporting to SID."); |
| 200 |
defaultTune = str2Value2(nextArg()); |
| 201 |
if(defaultTune < 1 || defaultTune > ct.base.SUBTUNE_MAX) |
| 202 |
throw new UserException(format("Valid range for subtunes is 1 - %d.", ct.base.SUBTUNE_MAX)); |
| 203 |
break; |
| 204 |
case "-z", "-zp": |
| 205 |
if(command != Command.ExportSID && |
| 206 |
command != Command.ExportPRG) |
| 207 |
throw new UserException("Option available only with exporting commands."); |
| 208 |
zpAddress = str2Value2(nextArg()); |
| 209 |
if(zpAddress < 2 || zpAddress > 0xfe) { |
| 210 |
throw new UserException("Valid range for zero page is 2 - $fe"); |
| 211 |
} |
| 212 |
break; |
| 213 |
case "-o": |
| 214 |
if(outfnDefined) |
| 215 |
throw new UserException("Output file already defined."); |
| 216 |
outfn = args[argp+1]; |
| 217 |
outfnDefined = true; |
| 218 |
argp++; |
| 219 |
break; |
| 220 |
case "-q": |
| 221 |
verbose = false; |
| 222 |
break; |
| 223 |
default: |
| 224 |
if(args[argp][0] == '-') |
| 225 |
throw new UserException("Unrecognized option '" ~ args[argp] ~ "'"); |
| 226 |
if(infnDefined && command != Command.Import) |
| 227 |
throw new UserException("Input filename already defined. Use -o to define output file."); |
| 228 |
if(command == Command.Import) { |
| 229 |
if(infncounter > 1) |
| 230 |
throw new UserException("Infile & import filename already defined."); |
| 231 |
infns[infncounter++] = args[argp]; |
| 232 |
infn = infns[0]; |
| 233 |
} |
| 234 |
else infn = args[argp]; |
| 235 |
infnDefined = true; |
| 236 |
break; |
| 237 |
} |
| 238 |
} |
| 239 |
} |
| 240 |
assert(command != Command.None); |
| 241 |
if(!infnDefined) |
| 242 |
throw new UserException("Input filename not defined."); |
| 243 |
if(command == Command.Init && !outfnDefined) { |
| 244 |
throw new UserException("Command 'init' requires output filename to be defined (option -o)."); |
| 245 |
} |
| 246 |
else if(command == Command.Import && !outfnDefined) { |
| 247 |
throw new UserException("Command 'import' requires output filename to be defined (option -o)."); |
| 248 |
} |
| 249 |
|
| 250 |
if(!outfnDefined) { |
| 251 |
outfn = defineOutfn(command, infn); |
| 252 |
} |
| 253 |
|
| 254 |
if(!std.file.exists(infn)) |
| 255 |
throw new UserException(format("File %s does not exist", infn)); |
| 256 |
|
| 257 |
explain("Input file: " ~ infn); |
| 258 |
explain("Output file: " ~ outfn); |
| 259 |
if(command == Command.ExportSID || command == Command.ExportPRG) { |
| 260 |
explain(format("Relocating data to $%x", relocAddress)); |
| 261 |
} |
| 262 |
|
| 263 |
switch(command) { |
| 264 |
case Command.ExportPRG, Command.ExportSID: |
| 265 |
insong = new Song; |
| 266 |
insong.open(infn); |
| 267 |
if(singleSubtune >= 1) { |
| 268 |
for(int i = 0; i < ct.base.SUBTUNE_MAX; i++) { |
| 269 |
if(i == singleSubtune - 1) continue; |
| 270 |
insong.subtunes.clear(i); |
| 271 |
} |
| 272 |
insong.subtunes.swap(0, singleSubtune - 1); |
| 273 |
defaultTune = 1; |
| 274 |
} |
| 275 |
if(!doPurge(insong)) { |
| 276 |
writeln("Aborting"); |
| 277 |
return -1; |
| 278 |
} |
| 279 |
try { |
| 280 |
validate(insong); |
| 281 |
} |
| 282 |
catch(ValidateException e) { |
| 283 |
writeln(e); |
| 284 |
writeln("Aborting"); |
| 285 |
return -1; |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
ubyte[] data = doBuild(insong, relocAddress, zpAddress, |
| 290 |
command == Command.ExportSID, |
| 291 |
defaultTune, verbose); |
| 292 |
std.file.write(outfn, data); |
| 293 |
break; |
| 294 |
case Command.Import: |
| 295 |
if(infncounter < 2) |
| 296 |
throw new UserException("Import song not defined."); |
| 297 |
explain("Importing data from " ~ infns[1]); |
| 298 |
insong = new Song; |
| 299 |
insong.open(infns[0]); |
| 300 |
Song importsong = new Song(); |
| 301 |
importsong.open(infns[1]); |
| 302 |
insong.importData(importsong); |
| 303 |
insong.save(outfn); |
| 304 |
break; |
| 305 |
case Command.Dump: |
| 306 |
insong = new Song; |
| 307 |
insong.open(infn); |
| 308 |
doPurge(insong); |
| 309 |
string dumped = dumpOptimized(insong, 0x1000, 0, true, |
| 310 |
verbose); |
| 311 |
string header = format(";;; ACME dump for %s\n\n", infn); |
| 312 |
std.file.write(outfn, header ~ dumped); |
| 313 |
break; |
| 314 |
case Command.Init: |
| 315 |
insong = new Song(cast(ubyte[])std.file.read(infn)); |
| 316 |
insong.save(outfn); |
| 317 |
break; |
| 318 |
default: |
| 319 |
assert(0); |
| 320 |
} |
| 321 |
} |
| 322 |
catch(UserException e) { |
| 323 |
writeln("error: ", e); |
| 324 |
return -1; |
| 325 |
} |
| 326 |
catch(Exception e) { |
| 327 |
writeln(e); |
| 328 |
return -1; |
| 329 |
} |
| 330 |
scope(failure) { |
| 331 |
writeln("Aborted."); |
| 332 |
} |
| 333 |
scope(success) { |
| 334 |
explain("Done."); |
| 335 |
} |
| 336 |
return 0; |
| 337 |
} |