1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
|
# coding: utf-8
import os
from math import ceil
from gbz80disasm import get_global_address, get_local_address
import crystal
from crystal import music_classes as sound_classes
from crystal import Command
from crystal import load_rom
rom = load_rom()
rom = bytearray(rom)
import config
conf = config.Config()
class NybbleParam:
size = 0.5
byte_type = 'dn'
which = None
def __init__(self, address, name):
if self.which == None:
self.which = {0.0: 'lo', 0.5: 'hi'}[address % 1]
self.address = int(address)
self.name = name
self.parse()
def parse(self):
self.nybble = (rom[self.address] >> {'lo': 0, 'hi': 4}[self.which]) & 0xf
class HiNybbleParam(NybbleParam):
which = 'hi'
def to_asm(self):
return '%d' % self.nybble
class LoNybbleParam(NybbleParam):
which = 'lo'
def to_asm(self):
return '%d' % self.nybble
class PitchParam(HiNybbleParam):
def to_asm(self):
if self.nybble == 0:
pitch = 'Rst'
else:
pitch = 'CDEFGAB'[(self.nybble - 1) / 2]
if not self.nybble & 1:
pitch += '#'
return pitch
class Note(Command):
macro_name = "note"
size = 0
end = False
param_types = {
0: {"name": "pitch", "class": PitchParam},
1: {"name": "duration", "class": LoNybbleParam},
}
allowed_lengths = [2]
def parse(self):
self.params = []
byte = rom[self.address]
current_address = self.address
for (key, param_type) in self.param_types.items():
name = param_type["name"]
class_ = param_type["class"]
# by making an instance, obj.parse() is called
obj = class_(address=int(current_address), name=name)
self.params += [obj]
current_address += obj.size
self.size += obj.size
self.params = dict(enumerate(self.params))
# obj sizes were 0.5, but were working with ints
current_address = int(ceil(current_address))
self.size = int(ceil(self.size))
self.last_address = current_address
return True
class Channel:
"""A sound channel data parser."""
def __init__(self, address, channel=1, base_label='Sound'):
self.start_address = address
self.address = address
self.channel = channel
self.base_label = base_label
self.output = []
self.parse()
def parse(self):
noise = False
done = False
while not done:
cmd = rom[self.address]
class_ = self.get_sound_class(cmd)(address=self.address)
# notetype loses the intensity param on channel 4
if class_.macro_name == 'notetype':
if self.channel in [4, 8]:
class_.size -= 1
del class_.params[class_.size - 1]
# togglenoise only has a param when toggled on
elif class_.macro_name in ['togglenoise', 'sfxtogglenoise']:
if noise:
class_.size -= 1
del class_.params[class_.size - 1]
noise = not noise
asm = class_.to_asm()
# label any jumps or calls
for key, param in class_.param_types.items():
if param['class'] == crystal.PointerLabelParam:
label_address = class_.params[key].parsed_address
label = '%s_branch_%x' % (
self.base_label,
label_address
)
label_output = (
label_address,
'%s: ; %x' % (label, label_address)
)
if label_output not in self.output:
self.output += [label_output]
asm = asm.replace(
'$%x' % (get_local_address(label_address)),
label
)
self.output += [(self.address, '\t' + asm)]
self.address += class_.size
done = class_.end
# infinite loops are enders
if class_.macro_name == 'loopchannel':
if class_.params[0].byte == 0:
done = True
# keep going past enders if theres more to parse
if any(self.address <= address for address, asm in self.output):
if done:
self.output += [(self.address, '; %x' % self.address)]
done = False
# dumb safety checks
if (
self.address >= len(rom) or
self.address / 0x4000 != self.start_address / 0x4000
) and not done:
done = True
raise Exception, 'reached the end of the bank without finishing!'
def to_asm(self):
self.output = sorted(
self.output,
# comment then label then asm
key=lambda (x, y):(x, not y.startswith(';'), ':' not in y)
)
text = ''
for i, (address, asm) in enumerate(self.output):
if ':' in asm:
# dont print labels for empty chunks
for (x, y) in self.output[i:]:
if ':' not in y:
text += '\n' + asm + '\n'
break
else:
text += asm + '\n'
text += '; %x' % (address + 1) + '\n'
return text
def get_sound_class(self, i):
for class_ in sound_classes:
if class_.id == i:
return class_
return Note
class Sound:
"""Interprets a sound data header."""
def __init__(self, address, name=''):
self.start_address = address
self.bank = address / 0x4000
self.address = address
self.name = name
self.base_label = 'Sound_%x' % self.start_address
if self.name != '':
self.base_label = self.name
self.output = []
self.parse()
def parse(self):
self.num_channels = (rom[self.address] >> 6) + 1
self.channels = []
for ch in xrange(self.num_channels):
current_channel = (rom[self.address] & 0xf) + 1
self.address += 1
address = rom[self.address] + rom[self.address + 1] * 0x100
address = self.bank * 0x4000 + address % 0x4000
self.address += 2
channel = Channel(address, current_channel, self.base_label)
self.channels += [(current_channel, channel)]
def to_asm(self):
asms = {}
text = ''
text += '%s: ; %x' % (self.base_label, self.start_address) + '\n'
for num, channel in self.channels:
text += '\tchannel %d, %s_Ch%d' % (num, self.base_label, num) + '\n'
text += '; %x' % self.address + '\n'
asms[self.start_address] = text
text = ''
for ch, (num, channel) in enumerate(self.channels):
text += '%s_Ch%d: ; %x' % (
self.base_label,
num,
channel.start_address
) + '\n'
# stack labels at the same address
if ch < len(self.channels) - 1:
next_channel = self.channels[ch + 1][1]
if next_channel.start_address == channel.start_address:
continue
text += channel.to_asm()
asms[channel.start_address] = text
text = ''
return '\n'.join(asm for address, asm in sorted(asms.items()))
def dump_sounds(origin, names, path, base_label='Sound_'):
"""Dump sound data from a pointer table."""
for i, name in enumerate(names):
addr = origin + i * 3
bank, address = rom[addr], rom[addr+1] + rom[addr+2] * 0x100
sound_at = get_global_address(address, bank)
sound = Sound(sound_at, base_label + name)
output = sound.to_asm()
filename = name.lower() + '.asm'
with open(os.path.join(path, filename), 'w') as out:
out.write(output)
def dump_crystal_music():
from song_names import song_names
dump_sounds(0xe906e, song_names, os.path.join(conf.path, 'audio', 'music'), 'Music_')
def generate_crystal_music_pointers():
from song_names import song_names
return '\n'.join('\tdbw BANK({0}), {0}'.format('Music_' + label) for label in song_names)
def dump_crystal_sfx():
from sfx_names import sfx_names
dump_sounds(0xe927c, sfx_names, os.path.join(conf.path, 'audio', 'sfx'), 'Sfx_')
def generate_crystal_sfx_pointers():
from sfx_names import sfx_names
return '\n'.join('\tdbw BANK({0}), {0}'.format('Sfx_' + label) for label in sfx_names)
if __name__ == '__main__':
dump_crystal_music()
dump_crystal_sfx()
|