updateTable(dataId, rTrack, vTrack)

# preserved tracks
for vTrack in preserved:
    vTrackData = getVanillaTrackData(vTrack)
    dataId = (vTrackData['data_index'] + 1) * 3
    updateTable(dataId, vTrack, vTrack)

minAddr, maxAddr = (0xffffffff, 0x0)

# compare nspc data and dump if different than expected
for dataId, expected_nspc in expected_table.items():
    with open(expected_nspc, 'rb') as f:
        expected_music_data = f.read()
    sz = len(expected_music_data)
    snesAddr = rom.readLong(tableAddr + dataId)
    if snesAddr + sz > maxAddr:
        maxAddr = snesAddr + sz
    if snesAddr < minAddr:
        minAddr = snesAddr
    print("Data $%02x, $%06x - $%06x" % (dataId, snesAddr, snesAddr + sz - 1))
    addr = snes_to_pc(snesAddr)
    rom.seek(addr)
    music_data = rom.read(sz)
    if music_data != expected_music_data:
        print("Music data $%02x differ from the one of %s !" %
              (dataId, expected_nspc))
        out_nspc = "track_%02x.nspc" % dataId
        print("Dumping it in %s ..." % out_nspc)
        with open(out_nspc, 'wb') as f:
            f.write(music_data)
    [("The last Metroid is in captivity", None, "Vanilla Soundtrack - Sound Effects")],
    [("The galaxy is at peace", None, "Vanilla Soundtrack - Sound Effects")],
    # Song 0: Boss music,
    # Song 1: Pre-boss music,
    # Song 2: No music
    [("Baby Metroid - Apparition", "vanilla/boss2.spc", "Vanilla Soundtrack"),
     ("Baby Metroid - Tension", "vanilla/tension.spc", "Vanilla Soundtrack"),
     ("Baby Metroid - No music", None, "Vanilla Soundtrack - Sound Effects")],
    [("Mother Brain 3", "vanilla/samus_theme.spc", "Vanilla Soundtrack")]
]

metadata = {}

for i in range(len(vanillaMusicData)):
    addr = musicDataTable+i*3
    ptr = snes_to_pc(rom.readLong(addr))
    if i < len(vanillaMusicData)-1:
        # vanilla data ptrs are conveniently sorted
        endptr = snes_to_pc(rom.readLong(addr+3))
    else:
        endptr = musicDataEnd
    rom.seek(addr)
    nspc_path = "%s/vanilla_%06x_%06x.nspc" % (nspc_dir, ptr, endptr-1)
    print("Writing %s ..." % nspc_path)
    rom.seek(ptr)
    with open(nspc_path, "wb") as nspc:
        nspc.write(rom.read(endptr-ptr))
    musicdata = vanillaMusicData[i]
    for j in range(len(musicdata)):
        track, spc_path, group = musicdata[j]
        h,t=os.path.split(nspc_path)
    else:
        return (firstData, None)


def getMd5Sum(data):
    return hashlib.md5(bytes(data)).hexdigest()


# read table
tracksTable = {}
for trackName, data in vanillaTracks.items():
    if 'pc_addresses' not in data:
        continue
    addr = data['pc_addresses'][0]
    dataId = rom.readByte(addr)
    addr = snes_to_pc(rom.readLong(tableAddr + dataId))
    #print("dataId: {} - addr: {} for song: {}".format(hex(dataId), hex(addr), trackName))
    tracksTable[addr] = {"trackName": trackName, "dataId": dataId}

# get nspc data in rom and compute its md5 sum
for addr in sorted(tracksTable.keys()):
    trackData = tracksTable[addr]
    #print("{} {:4} {}".format(hex(pc_to_snes(addr)), hex(trackData["dataId"]), trackData["trackName"]))
    nspcData = readNspcData(rom, addr)
    if nspcData[0] is None and nspcData[1] is None:
        print("  Warning: no nspc end found for {}".format(
            trackData["trackName"]))
        tracksTable[addr]["nspcData"] = [None]
        tracksTable[addr]["nspc_md5sum"] = [None]
        continue