Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- rom = bytearray(open("tcg2.gbc", "r").read())
- headers = 0x1481
- banks = 0x1dd444
- songs = [
- ["TitleScreen", 0x1ea368, True],
- ["DuelTheme1", 0x1e20fe, True],
- ["DuelTheme2", 0x1e2737, True],
- ["DuelTheme3", 0x1e2f4d, True],
- ["PauseMenu", 0x1e3287, False],
- ["PCMainMenu", 0x1e3486, True],
- ["DeckMachine", 0x1e35cf, True],
- ["CardPop", 0x1e3735, True],
- ["Overworld", 0x1e3eb7, True],
- ["PokemonDome", 0x1e64e2, True],
- ["ChallengeHall", 0x1e5c5f, True],
- ["Club1", 0x1e60f1, True],
- ["Club2", 0x1f5865, True],
- ["Club3", 0x1e6a72, True],
- ["Ronald", 0x1e7027, True],
- ["Imakuni", 0x1e72bc, True],
- ["HallOfHonor", 0x1e74d4, True],
- ["Unused", 0x0, True],
- ["Unused", 0x0, True],
- ["HereComesGR", 0x1ead0d, True],
- ["GROverworld", 0x1eb67b, True],
- ["Fort1", 0x1edefc, True],
- ["Fort2", 0x1ee797, True],
- ["Fort3", 0x1ef1ec, True],
- ["Fort4past", 0x1f213c, True],
- ["GRCastle", 0x1f2d24, True],
- ["Unused", 0x0, True],
- ["GRChallengeCup", 0x1f3411, True],
- ["GameCorner", 0x1e7bfa, True],
- ["GRBlimp", 0x1e7fd6, True],
- ["GRDuelTheme1", 0x1f6517, True],
- ["GRDuelTheme2", 0x1efff9, True],
- ["GRDuelTheme3", 0x1f76dc, True],
- ["Unused", 0x0, True],
- ["Ishihara", 0x1de32a, True],
- ["Imakuni2", 0x1f3f40, True],
- ["Credits", 0x1dfd99, True],
- ["Unused", 0x0, True],
- ["Unused", 0x0, True],
- ["MatchStart1", 0x1eb6c1, False],
- ["MatchStart2", 0x1eb77a, False],
- ["MatchStart3", 0x1eb8cc, False],
- ["MatchVictory", 0x1eb999, True],
- ["MatchLoss", 0x1eba48, True],
- ["DarkDiddly", 0x1ebade, True],
- ["Unused", 0x0, True],
- ["BoosterPack", 0x1ebb76, True],
- ["Medal", 0x1ebc57, True],
- ["Unused", 0x0, True],
- ["Diddly1", 0x1ebd67, True],
- ["Diddly2", 0x1ebe5d, True],
- ["Diddly3", 0x1ebeae, True],
- ["Diddly4", 0x1ebefa, True],
- ["Diddly5", 0x1ebf3e, True],
- ["Unused", 0x0, True],
- ["Unused", 0x0, True],
- ["Unused", 0x0, True],
- ["Diddly6", 0x1f783b, True],
- ]
- channels = {
- 1: 1,
- 3: 2,
- 7: 3,
- 15: 4,
- }
- music_commands = {
- 0xd0: ["speed", {"type": "byte"}, 2],
- 0xd1: ["octave", 1],
- 0xd7: ["inc_octave", 1],
- 0xd8: ["dec_octave", 1],
- 0xd9: ["tie", 1],
- 0xdc: ["musicdc", {"type": "byte"}, 2],
- 0xdd: ["MainLoop", 1],
- 0xde: ["EndMainLoop", 1],
- 0xdf: ["Loop", {"type": "byte"}, 2],
- 0xe0: ["EndLoop", 1],
- #0xe1: ["music_jp", {"type": "label"}, 3],
- 0xe2: ["music_call", {"type": "label"}, 3],
- 0xe3: ["music_ret", 1],
- 0xe4: ["musice4", {"type": "byte"}, 2],
- 0xe5: ["duty", {"type": "duty"}, 2],
- 0xe6: ["volume", {"type": "byte"}, 2],
- 0xe7: ["wave", {"type": "byte"}, 2],
- 0xe8: ["musice8", {"type": "byte"}, 2],
- 0xe9: ["musice9", {"type": "byte"}, 2],
- 0xea: ["vibrato_rate", {"type": "byte"}, 2],
- 0xeb: ["vibrato_delay", {"type": "byte"}, 2],
- 0xec: ["musicec", {"type": "byte"}, 2],
- #0xed: ["musiced", {"type": "byte"}, 2],
- 0xee: ["musicee", {"type": "byte"}, 2],
- 0xef: ["musicef", {"type": "byte"}, 2],
- 0xf0: ["musicf0", {"type": "byte"}, 2],
- 0xf1: ["musicf1", {"type": "byte"}, 2],
- 0xf2: ["musicf2", 1],
- 0xf3: ["musicf3", 1],
- #0xf4: ["musicf4", 1],
- 0xff: ["music_end", 1],
- }
- param_lengths = {
- "nibble": 1,
- "byte": 1,
- "duty": 1,
- "label": 2,
- }
- music_notes = {
- 0x1: "C_",
- 0x2: "C#",
- 0x3: "D_",
- 0x4: "D#",
- 0x5: "E_",
- 0x6: "F_",
- 0x7: "F#",
- 0x8: "G_",
- 0x9: "G#",
- 0xa: "A_",
- 0xb: "A#",
- 0xc: "B_",
- }
- noise_instruments = {
- 0x1: "bass",
- 0x2: "noise2",
- 0x3: "snare1",
- #0x4: "noise4",
- 0x5: "snare2",
- 0x6: "noise6",
- 0x7: "snare3",
- 0x8: "noise8",
- 0x9: "snare4",
- 0xa: "noisea",
- 0xb: "noiseb",
- 0xc: "snare5",
- }
- for cursong, song in enumerate(songs):
- songname = song[0]
- if songname != "Unused":
- exception = song[2]
- songfile = open("music/" + songname.lower() + ".asm", 'w')
- bank = rom[banks + cursong]
- headerptr = headers + 0x4000 * bank + 2 * cursong
- header = rom[headerptr + 1] * 0x100 + rom[headerptr] + 0x4000 * (bank - 1)
- curchannel = 1
- lastchannel = channels[rom[header]]
- header += 1
- output = ''
- while 1:
- startingaddress = rom[header + 1] * 0x100 + rom[header] - 0x4000 + (0x4000 * bank)
- end = rom[header + 3] * 0x100 + rom[header + 2] - 0x4000 + (0x4000 * bank)
- if curchannel == lastchannel: end = song[1]
- if curchannel == 1 and exception:
- curchannel = 2
- startingaddress = rom[header + 3] * 0x100 + rom[header + 2] - 0x4000 + (0x4000 * bank)
- end = rom[header + 1] * 0x100 + rom[header] - 0x4000 + (0x4000 * bank)
- elif curchannel == 3 and exception:
- curchannel = 1
- startingaddress = rom[header - 1] * 0x100 + rom[header - 2] - 0x4000 + (0x4000 * bank)
- if lastchannel == 2:
- end = song[1]
- exception = False
- if lastchannel == 3:
- end = end = rom[header + 3] * 0x100 + rom[header + 2] - 0x4000 + (0x4000 * bank)
- elif curchannel == 2 and exception:
- if lastchannel == 3:
- end = song[1]
- curchannel = 3
- exception = False
- address = startingaddress
- labels = []
- # pass 1, build a list of all addresses pointed to by calls
- while address != end:
- byte = rom[address]
- if byte < 0xd0:
- command_length = 1
- else:
- if byte > 0xd0 and byte < 0xd7: byte = 0xd1
- command_length = music_commands[byte][-1]
- if byte == 0xe2: labels.append(rom[address + 2] * 0x100 + rom[address + 1])
- address += command_length
- # once the loop breaks, start over from first address
- address = startingaddress
- # pass 2, print commands and labels for addresses that are in labels
- while address != end:
- byte = rom[address]
- if address == startingaddress:
- output += "Music_{}_Ch{}: ; {:02x} ({:0x}:{:02x})\n".format(songname, curchannel, address, bank, address % 0x4000 + 0x4000)
- elif address % 0x4000 + 0x4000 in labels:
- output += "\nBranch_{:02x}:\n".format(address)
- if byte < 0x10:
- output += "\trest {}".format(byte % 0x10 + 1)
- command_length = 1
- elif byte < 0xd0:
- if curchannel != 4:
- output += "\tnote {}, {}".format(music_notes[byte >> 4], byte % 0x10 + 1)
- else:
- output += "\tnote {}, {}".format(noise_instruments[byte >> 4], byte % 0x10 + 1)
- command_length = 1
- else:
- if byte > 0xd0 and byte < 0xd7:
- command = music_commands[0xd1]
- output += "\t{} {}".format(command[0], byte % 0x10)
- byte = 0xd1
- else:
- command = music_commands[byte]
- output += "\t{}".format(command[0])
- command_length = 1
- params = 1
- # print all params for current command
- while params != len(command) - 1:
- param_type = command[params]["type"]
- address += command_length
- command_length = param_lengths[param_type]
- param = rom[address]
- if param_type == "nibble":
- output += " {}, {}".format(param >> 4, param % 0x10)
- elif param_type == "byte":
- output += " {}".format(param)
- elif param_type == "duty":
- output += " {}".format(param >> 6)
- else:
- param += rom[address + 1] * 0x100 - 0x4000 + (bank * 0x4000)
- output += " Branch_{:02x}".format(param)
- params += 1
- if params != len(command) - 1: output += ","
- output += "\n"
- address += command_length
- header += 2
- if songname == "Diddly5" and not exception:
- curchannel += 1
- if curchannel == lastchannel and not exception:
- output += "; {}".format(hex(address))
- songfile.write(output)
- break
- curchannel += 1
- output += "; {}".format(hex(address))
- output += "\n\n"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement