Mus.java

/**
 * Sidplayer and Stereo Sidplayer format support. ---------------------------------------------- This program is free
 * software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License or (at your option) any later version.
 * <p>
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 * <p>
 * You should have received a copy of the GNU General Public License along with this program; if not, write to the Free
 * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */
package libsidplay.sidtune;

import libsidutils.CBMCodeUtils;
import libsidutils.IOUtils;
import libsidutils.assembler.KickAssembler;
import libsidutils.assembler.KickAssemblerResult;

import java.io.*;
import java.net.URL;
import java.util.*;

import static java.util.Objects.requireNonNull;
import static libsidplay.sidtune.SidTune.Compatibility.PSIDv2;

class Mus extends PSid {

    private static final String MUS_DRIVER1_ASM = "/libsidplay/sidtune/musdriver1.asm";
    private static final String MUS_DRIVER1_BIN = "/libsidplay/sidtune/musdriver1.bin";
    private static final String MUS_DRIVER2_ASM = "/libsidplay/sidtune/musdriver2.asm";
    private static final String MUS_DRIVER2_BIN = "/libsidplay/sidtune/musdriver2.bin";

    /**
     * Known SID names. MUS loader scans for these.
     */
    private static final List<String> DEFAULT_MUS_NAMES = Arrays.asList(".mus", ".str", "_a.mus", "_b.mus");

    private static final String ERR_SIDTUNE_INVALID = "MUS: file contains invalid data";

    private static final String ERR_SIDTUNE_2ND_INVALID = "MUS: 2nd file contains invalid data";

    private static final int MUS_HLT_CMD = 0x14F;

    private static final int MUS_DATA_ADDR = 0x0900;

    private static final int DUAL_SID_BASE = 0xd500;

    /**
     * Needed for MUS/STR player installation.
     */
    private int musDataLen;

    private KickAssemblerResult preparedDriver;
    private KickAssemblerResult preparedStereoDriver;

    @Override
    public Integer placeProgramInMemory(final byte[] c64buf) {
        if (USE_KICKASSEMBLER) {
            assembleAndinstallMusPlayers(c64buf);
        } else {
            relocateAndInstallMusPlayers(c64buf);
        }
        return super.placeProgramInMemory(c64buf);
    }

    @Override
    public Integer placeProgramInMemory(byte[] ram, byte[] driver, byte[] musDriver1, byte[] musDriver2) {
        installMusPlayers(ram, musDriver1, musDriver2);
        return super.placeProgramInMemory(ram, driver, musDriver1, musDriver2);
    }

    private int detect(String name, final byte[] buffer, final String errorMessage) throws SidTuneError {
        String suffix = IOUtils.getFilenameSuffix(name.toLowerCase(Locale.ENGLISH));
        if (DEFAULT_MUS_NAMES.stream().noneMatch(suffix::endsWith)) {
            throw new SidTuneError(errorMessage);
        }
        if (buffer == null || buffer.length < 8) {
            throw new SidTuneError(errorMessage);
        }
        // Add length of voice 1 data.
        final int voice1DataEnd = 2 + 3 * 2 + (buffer[2] & 0xff) + ((buffer[3] & 0xff) << 8);
        // Add length of voice 2 data.
        final int voice2DataEnd = voice1DataEnd + (buffer[4] & 0xff) + ((buffer[5] & 0xff) << 8);
        // Add length of voice 3 data.
        final int voice3DataEnd = voice2DataEnd + (buffer[6] & 0xff) + ((buffer[7] & 0xff) << 8);

        // validate that voice3 is still inside the buffer and that each track
        // ends with HLT
        if (!(voice1DataEnd >= 10 && voice2DataEnd >= voice1DataEnd + 2
                && voice3DataEnd >= voice2DataEnd + 2 && voice3DataEnd - 1 < buffer.length
                && (buffer[voice1DataEnd - 1] & 0xff) + ((buffer[voice1DataEnd - 2] & 0xff) << 8) == MUS_HLT_CMD
                && (buffer[voice2DataEnd - 1] & 0xff) + ((buffer[voice2DataEnd - 2] & 0xff) << 8) == MUS_HLT_CMD
                && (buffer[voice3DataEnd - 1] & 0xff) + ((buffer[voice3DataEnd - 2] & 0xff) << 8) == MUS_HLT_CMD)) {
            throw new SidTuneError(errorMessage);
        }
        return voice3DataEnd;
    }

    /**
     * Load MUS mono or stereo tune (find paired stereo files: MUS/STR or _A.MUS/_B.MUS)
     */
    protected static SidTune load(final File musFile, final byte[] dataBuf) throws SidTuneError {
        File stereo = getStereoTune(musFile);
        byte[] stereoContents = null;
        if (stereo != null) {
            try {
                stereoContents = getContents(stereo);
            } catch (IOException e) {
                // A missing optional companion still permits mono playback.
            }
        }
        return load(musFile.getName(), dataBuf, stereo == null ? null : stereo.getName(), stereoContents);
    }

    public static SidTune load(String name, byte[] contents, String companionName, byte[] companion)
            throws SidTuneError {
        Mus mus = new Mus();
        if (companion != null && CompanionFiles.isSecondary(name, companionName)) {
            mus.loadWithProvidedMetadata(companionName, companion, name, contents);
        } else {
            mus.loadWithProvidedMetadata(name, contents, companionName, companion);
        }
        return mus;
    }

    private void loadWithProvidedMetadata(String name, byte[] musBuf, String stereoName, byte[] strBuf)
            throws SidTuneError {
        int voice3DataEnd = detect(name, musBuf, ERR_SIDTUNE_INVALID);

        musDataLen = musBuf.length;
        convertOldStyleSpeedToTables(0xffffffffL);

        /*
         * FIXME: dealing with credits is problematic. The data is unstructured, and
         * longer than fits into DB index. We could assume that first line is title,
         * second is author, third is release, but that's actually not very likely.
         */
        if (voice3DataEnd < musBuf.length) {
            String credits = getCredits(musBuf, voice3DataEnd);
            info.commentString.add(credits);
            voice3DataEnd += credits.length() + 1;
        }

        if (strBuf != null) {
            voice3DataEnd = detect(stereoName, strBuf, ERR_SIDTUNE_2ND_INVALID);

            if (voice3DataEnd < strBuf.length) {
                info.commentString.add(getCredits(strBuf, voice3DataEnd));
            }

            info.sidChipBase[1] = DUAL_SID_BASE;
        }

        info.initAddr = strBuf == null ? 0xec60 : 0xfc90;
        info.playAddr = strBuf == null ? 0xec80 : 0xfc96;
        info.loadAddr = MUS_DATA_ADDR;
        info.compatibility = PSIDv2;
        info.infoString.add(IOUtils.getFilenameWithoutSuffix(name));
        info.infoString.add("<?>");
        info.infoString.add("<?>");

        program = new byte[musBuf.length + (strBuf != null ? strBuf.length : 0)];
        if (program.length > 0xe000 - MUS_DATA_ADDR) {
            throw new SidTuneError("MUS: music data overlaps the player drivers");
        }
        info.c64dataLen = program.length;
        System.arraycopy(musBuf, 0, program, 0, musBuf.length);
        if (strBuf != null) {
            System.arraycopy(strBuf, 0, program, musBuf.length, strBuf.length);
        }
        findPlaceForDriver();
    }

    private String getCredits(byte[] musBuf, int voice3DataEnd) {
        byte[] creditsBytes = Arrays.copyOfRange(musBuf, voice3DataEnd, musBuf.length - 1);
        return CBMCodeUtils.petsciiToIso88591(creditsBytes);
    }

    @Override
    public byte[] getPSidHeader() {
        return null;
    }

    /**
     * Get stereo music file by naming convention. Couples are *.mus/*.str or *_a.mus/*_b.mus .
     *
     * @param file file to get the stereo tune for (e.g., name.mus).
     * @return stereo file (e.g., name.str)
     */
    private static File getStereoTune(final File file) {
        File parent = file.getParentFile();
        if (parent == null) parent = file.getAbsoluteFile().getParentFile();
        File[] siblings = parent.listFiles();
        if (siblings == null) return null;
        String companion = CompanionFiles.findStereoCompanion(file.getName(),
                Arrays.stream(siblings).filter(File::isFile).map(File::getName).toArray(String[]::new));
        if (companion == null) return null;
        for (File sibling : siblings) if (sibling.getName().equals(companion)) return sibling;
        return null;
    }

    /**
     * Linux ALSA is very sensible for timing: therefore, we assemble before we open AudioLine
     */
    @Override
    public void prepare() {
        if (USE_KICKASSEMBLER) {
            // Assemble MUS player 1
            InputStream asm = Mus.class.getResourceAsStream(MUS_DRIVER1_ASM);
            preparedDriver = KickAssembler.assemble(MUS_DRIVER1_ASM, asm, new HashMap<>());
            // Install MUS player 1
            checkLabels(MUS_DRIVER1_ASM, preparedDriver.getResolvedSymbols(), "data_low", "data_high", "init", "start");
            info.initAddr = preparedDriver.getResolvedSymbols().get("init");
            info.playAddr = preparedDriver.getResolvedSymbols().get("start");

            if (info.getSIDChipBase(1) != 0) {
                // Assemble MUS player 2
                InputStream stereoAsm = Mus.class.getResourceAsStream(MUS_DRIVER2_ASM);
                preparedStereoDriver = KickAssembler.assemble(MUS_DRIVER2_ASM, stereoAsm, new HashMap<>());
                // Install MUS player 2
                checkLabels(MUS_DRIVER2_ASM, preparedStereoDriver.getResolvedSymbols(), "data_low", "data_high", "init",
                        "start");
                info.initAddr = preparedStereoDriver.getResolvedSymbols().get("init");
                info.playAddr = preparedStereoDriver.getResolvedSymbols().get("start");
            }
            super.prepare();
        }
    }

    private void assembleAndinstallMusPlayers(final byte[] c64buf) {
        if (preparedDriver == null) {
            prepare();
        }
        installMusPlayer(c64buf, MUS_DATA_ADDR, preparedDriver.getData(),
                preparedDriver.getResolvedSymbols().get("data_low"),
                preparedDriver.getResolvedSymbols().get("data_high"));

        if (info.getSIDChipBase(1) != 0) {
            installMusPlayer(c64buf, MUS_DATA_ADDR + musDataLen, preparedStereoDriver.getData(),
                    preparedStereoDriver.getResolvedSymbols().get("data_low"),
                    preparedStereoDriver.getResolvedSymbols().get("data_high"));
        }
    }

    private void checkLabels(String asmSource, Map<String, Integer> resolvedSymbols, String data_low, String data_high,
                             String init, String start) {
        if (resolvedSymbols.get(data_low) == null) {
            throw new RuntimeException("Symbol data_low not found in " + asmSource);
        }
        if (resolvedSymbols.get(data_high) == null) {
            throw new RuntimeException("Symbol data_high not found in " + asmSource);
        }
        if (resolvedSymbols.get(init) == null) {
            throw new RuntimeException("Symbol init not found in " + asmSource);
        }
        if (resolvedSymbols.get(start) == null) {
            throw new RuntimeException("Symbol start not found in " + asmSource);
        }
    }

    private void relocateAndInstallMusPlayers(byte[] c64buf) {
        // Install MUS player #1
        byte[] MUS_DRIVER1;
        try (DataInputStream is = new DataInputStream(requireNonNull(Mus.class.getResourceAsStream(MUS_DRIVER1_BIN)))) {
            URL url = Mus.class.getResource(MUS_DRIVER1_BIN);
            MUS_DRIVER1 = new byte[requireNonNull(url).openConnection().getContentLength()];
            is.readFully(MUS_DRIVER1);
        } catch (IOException e) {
            throw new RuntimeException("Load failed for resource: " + MUS_DRIVER1_BIN);
        }
        byte[] MUS_DRIVER2 = null;
        if (info.getSIDChipBase(1) != 0) {
            try (DataInputStream is = new DataInputStream(requireNonNull(Mus.class.getResourceAsStream(MUS_DRIVER2_BIN)))) {
                URL url = Mus.class.getResource(MUS_DRIVER2_BIN);
                MUS_DRIVER2 = new byte[requireNonNull(url).openConnection().getContentLength()];
                is.readFully(MUS_DRIVER2);
            } catch (IOException e) {
                throw new RuntimeException("Load failed for resource: " + MUS_DRIVER2_BIN);
            }
        }
        installMusPlayers(c64buf, MUS_DRIVER1, MUS_DRIVER2);
    }

    private void installMusPlayers(byte[] ram, byte[] first, byte[] second) {
        int dest = (first[0] & 255) | (first[1] & 255) << 8;
        installMusPlayer(ram, MUS_DATA_ADDR, first, dest + 0xc6e - 1, dest + 0xc70 - 1);
        info.initAddr = 0xec60;
        info.playAddr = 0xec80;
        if (info.getSIDChipBase(1) != 0) {
            dest = (second[0] & 255) | (second[1] & 255) << 8;
            installMusPlayer(ram, MUS_DATA_ADDR + musDataLen, second, dest + 0xc6e - 1, dest + 0xc70 - 1);
            info.initAddr = 0xfc90;
            info.playAddr = 0xfc96;
        }
    }

    private void installMusPlayer(final byte[] c64buf, final int musDataAddr, final byte[] driver,
                                  final Integer data_low, final Integer data_high) {
        // Install MUS player
        int driverAddr = (driver[0] & 0xff) + ((driver[1] & 0xff) << 8);
        System.arraycopy(driver, 2, c64buf, driverAddr, driver.length - 2);

        // Point MUS player to data.
        c64buf[data_low + 1] = (byte) (2 + musDataAddr & 0xFF);
        c64buf[data_high + 1] = (byte) (2 + musDataAddr >> 8);

    }

    @Override
    public void save(final String name) throws IOException {
        try (FileOutputStream fos = new FileOutputStream(!name.endsWith(".mus") ? name + ".mus" : name)) {
            fos.write(program);
        }
    }

}