Command.java

package server.netsiddev;

import static server.netsiddev.NetworkSIDProtocol.*;

/**
 * Available client commands. Do not reorder: each ordinal is its wire command code.
 */
public enum Command {
    /* 0 */
    FLUSH, TRY_SET_SID_COUNT, MUTE, TRY_RESET,

    /* 4 */
    TRY_DELAY, TRY_WRITE(LEGACY_VERSION, LEGACY_WRITE_SIZE, 0),
    TRY_READ(LEGACY_VERSION, LEGACY_WRITE_SIZE, LEGACY_READ_SIZE), GET_VERSION,

    /* 8 */
    TRY_SET_SAMPLING(CONFIGURATION_VERSION), TRY_SET_CLOCKING(CONFIGURATION_VERSION),
    GET_CONFIG_COUNT(CONFIGURATION_VERSION), GET_CONFIG_INFO(CONFIGURATION_VERSION),

    /* 12 */
    SET_SID_POSITION(CONFIGURATION_VERSION), SET_SID_LEVEL(CONFIGURATION_VERSION),
    TRY_SET_SID_MODEL(CONFIGURATION_VERSION), SET_DELAY(METADATA_VERSION),

    /* 16 */
    SET_FADE_IN(METADATA_VERSION), SET_FADE_OUT(METADATA_VERSION), SET_SID_HEADER(METADATA_VERSION),

    /* 19 */
    TRY_WRITE_EX(EXTENDED_ADDRESS_VERSION, EXTENDED_WRITE_SIZE, 0),
    TRY_READ_EX(EXTENDED_ADDRESS_VERSION, EXTENDED_WRITE_SIZE, EXTENDED_READ_SIZE),
    TRY_SET_FM_OPL(EXTENDED_ADDRESS_VERSION);

    private final int minimumVersion;
    private final int writeSize;
    private final int readSize;

    Command() {
        this(LEGACY_VERSION);
    }

    Command(int minimumVersion) {
        this(minimumVersion, 0, 0);
    }

    Command(int minimumVersion, int writeSize, int readSize) {
        this.minimumVersion = minimumVersion;
        this.writeSize = writeSize;
        this.readSize = readSize;
    }

    /** Whether the negotiated protocol version includes this command. */
    public boolean isSupportedBy(int version) {
        return version >= minimumVersion;
    }

    /** Whether the command ends with a timed register read. */
    public boolean isRead() {
        return readSize != 0;
    }

    /** Whether the command uses the version-5 extended address format. */
    public boolean isExtended() {
        return writeSize == EXTENDED_WRITE_SIZE;
    }

    /** Size of each write event in bytes; zero for commands without write events. */
    public int getWriteSize() {
        return writeSize;
    }

    /** Size of the read tail in bytes; zero for commands without a read. */
    public int getReadSize() {
        return readSize;
    }

    /** Successful response type; BUSY and ERROR may be returned separately. */
    public Response getResponse() {
        if (isRead()) {
            return Response.READ;
        }
        return switch (this) {
            case GET_VERSION -> Response.VERSION;
            case GET_CONFIG_COUNT -> Response.COUNT;
            case GET_CONFIG_INFO -> Response.INFO;
            default -> Response.OK;
        };
    }
}