| 1 |
/* |
| 2 |
CheeseCutter v2 (C) Abaddon. Licensed under GNU GPL. |
| 3 |
*/ |
| 4 |
|
| 5 |
/** date to source dumper. very ugly quick and dirty hack |
| 6 |
*/ |
| 7 |
|
| 8 |
module ct.dump; |
| 9 |
|
| 10 |
import ct.base; |
| 11 |
import com.util; |
| 12 |
import std.string; |
| 13 |
import std.array; |
| 14 |
|
| 15 |
private const string byteOp = "!byte", wordOp = "!word"; |
| 16 |
|
| 17 |
string dumpData(Song sng) { |
| 18 |
auto app = appender!string(); |
| 19 |
|
| 20 |
int getHighestUsed(ubyte[] array) { |
| 21 |
for(int i = cast(int)array.length - 1; i >= 0; i--) { |
| 22 |
if(array[i] > 0) |
| 23 |
return cast(int)i; |
| 24 |
} |
| 25 |
return 0; |
| 26 |
} |
| 27 |
|
| 28 |
void append(string s) { |
| 29 |
app.put(s); |
| 30 |
} |
| 31 |
|
| 32 |
void hexdump(ubyte[] buf, int rowlen) { |
| 33 |
int c; |
| 34 |
append("\t\t" ~ byteOp ~ " "); |
| 35 |
foreach(i, b; buf) { |
| 36 |
append(format("$%02x", b)); |
| 37 |
c++; |
| 38 |
if(c >= rowlen) { |
| 39 |
c = 0; |
| 40 |
if(i < buf.length - 1) |
| 41 |
append("\n\t\t" ~ byteOp ~ " "); |
| 42 |
} |
| 43 |
else if(i < buf.length - 1) append(","); |
| 44 |
} |
| 45 |
append("\n"); |
| 46 |
} |
| 47 |
int tablen; |
| 48 |
ushort[][] trackpointers = new ushort[][sng.subtunes.numOf]; |
| 49 |
sng.subtunes.activate(0); |
| 50 |
auto packedTracks = sng.subtunes.compact(); |
| 51 |
|
| 52 |
append( "arp1 = *\n"); |
| 53 |
tablen = getHighestUsed(sng.wave1Table) + 1; |
| 54 |
hexdump(sng.wave1Table[0 .. tablen], 16); |
| 55 |
append( "arp2 = *\n"); |
| 56 |
hexdump(sng.wave2Table[0 .. tablen], 16); |
| 57 |
append( "filttab = *\n"); |
| 58 |
hexdump(sng.filterTable[0 .. getHighestUsed(sng.filterTable) + 4], 4); |
| 59 |
append( "pulstab = *\n"); |
| 60 |
hexdump(sng.pulseTable[0 .. getHighestUsed(sng.pulseTable) + 4], 4); |
| 61 |
append( "inst = *\n"); |
| 62 |
ubyte[512] instab = 0; |
| 63 |
|
| 64 |
int maxInsno; |
| 65 |
sng.seqIterator((Sequence s, Element e) { |
| 66 |
int insval = e.instr.value; |
| 67 |
if(insval > 0x2f) return; |
| 68 |
if(insval > maxInsno) maxInsno = insval; }); |
| 69 |
for(int i = 0; i < 8; i++) { |
| 70 |
append( format("\ninst%d = *\n",i)); |
| 71 |
hexdump(sng.instrumentTable[i * 48 .. i * 48 + (maxInsno+1)], 16); |
| 72 |
} |
| 73 |
|
| 74 |
// cmd ---------------------------------------- |
| 75 |
|
| 76 |
tablen = 1; |
| 77 |
append( "\nseqlo = *\n\t\t!8 "); |
| 78 |
for(int i = 0; i < sng.numOfSeqs(); i++) { |
| 79 |
append(format("<s%02x", i)); |
| 80 |
if(i < sng.numOfSeqs() - 1) |
| 81 |
append(","); |
| 82 |
} |
| 83 |
append( "\nseqhi = *\n\t\t!8 "); |
| 84 |
for(int i = 0; i < sng.numOfSeqs(); i++) { |
| 85 |
append(format(">s%02x", i)); |
| 86 |
if(i < sng.numOfSeqs() - 1) |
| 87 |
append(","); |
| 88 |
} |
| 89 |
|
| 90 |
sng.seqIterator((Sequence s, Element e) { |
| 91 |
int val = e.cmd.value; |
| 92 |
if(val == 0) return; |
| 93 |
if(val < 0x40 && val > tablen) { |
| 94 |
tablen = val; |
| 95 |
} |
| 96 |
}); |
| 97 |
|
| 98 |
++tablen; |
| 99 |
|
| 100 |
append( "\ncmd1 = *\n"); |
| 101 |
hexdump(sng.superTable[0..tablen], 16); |
| 102 |
append( "cmd2 = *\n"); |
| 103 |
hexdump(sng.superTable[64..64+tablen], 16); |
| 104 |
append( "cmd3 = *\n"); |
| 105 |
hexdump(sng.superTable[128..128+tablen], 16); |
| 106 |
|
| 107 |
// songsets ------------------------------------ |
| 108 |
|
| 109 |
append( "songsets = *\n"); |
| 110 |
for(int i = 0; i < sng.subtunes.numOf; i++) { |
| 111 |
append(wordOp ~ "\t"); |
| 112 |
for(int voice = 0; voice < 3; voice++) { |
| 113 |
if(voice > 0) |
| 114 |
append(","); |
| 115 |
append(" " ~ format("track%d_%d", i, voice)); |
| 116 |
} |
| 117 |
append( format("\n\t\t" ~ byteOp ~ " %d, 7\n", sng.songspeeds[i])); |
| 118 |
} |
| 119 |
|
| 120 |
// tracks ------------------------------------- |
| 121 |
|
| 122 |
foreach(i, ref subtune; packedTracks) { |
| 123 |
foreach(j, voice; subtune) { |
| 124 |
append(format("track%d_%d = *\n", i, j)); |
| 125 |
hexdump(voice, 16); |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
for(int i = 0; i < sng.numOfSeqs(); i++) { |
| 130 |
append( format("s%02x = *\n", i)); |
| 131 |
Sequence s = sng.seqs[i]; |
| 132 |
hexdump(s.compact(), 16); |
| 133 |
} |
| 134 |
|
| 135 |
// chords ------------------------------------- |
| 136 |
|
| 137 |
tablen = getHighestUsed(sng.chordTable) + 1; |
| 138 |
append("\nchord"); |
| 139 |
if(tablen < 1) tablen = 1; |
| 140 |
hexdump(sng.chordTable[0..tablen], 16); |
| 141 |
append("\nchordindex"); |
| 142 |
int highestChord = 0; |
| 143 |
sng.seqIterator((Sequence s, Element e) { |
| 144 |
if(e.cmd.value >= 0x80 && e.cmd.value <= 0x9f && |
| 145 |
(e.cmd.value & 0x1f) > highestChord) |
| 146 |
highestChord = e.cmd.value & 0x1f; |
| 147 |
}); |
| 148 |
hexdump(sng.chordIndexTable[0..highestChord+1], 16); |
| 149 |
return app.data; |
| 150 |
} |