TryRead.java

package builder.netsiddev.commands;

import static server.netsiddev.Command.TRY_READ;
import static server.netsiddev.NetworkSIDProtocol.*;

public class TryRead extends TryWrite {
    private final byte sidNumToRead;
    private final int readCycles;
    private final byte readAddr;

    public TryRead(TryWrite tryWrite, byte sidNum, int cycles, byte addr) {
        this(sidNum, cycles, addr);
        for (Write write : tryWrite.writes) {
            addWrite(write.getCycles(), write.getReg(), write.getData());
        }
    }

    public TryRead(byte sidNum, int cycles, byte addr) {
        if (cycles < 0 || cycles > MAX_CYCLE_DELAY) {
            throw new IllegalArgumentException("Read delay must be between 0 and 65535 cycles");
        }
        sidNumToRead = sidNum;
        readCycles = cycles;
        readAddr = addr;
        cyclesSendToServer = cycles;
    }

    @Override
    public byte[] toByteArray() {
        int i = 0;
        byte[] cmd = new byte[HEADER_SIZE + writes.size() * LEGACY_WRITE_SIZE + LEGACY_READ_SIZE];
        cmd[i++] = (byte) TRY_READ.ordinal();
        cmd[i++] = sidNumToRead;
        cmd[i++] = 0;
        cmd[i++] = 0;
        for (Write write : writes) {
            cmd[i++] = (byte) (write.getCycles() >> 8 & 0xff);
            cmd[i++] = (byte) (write.getCycles() & 0xff);
            cmd[i++] = (byte) write.getReg();
            cmd[i++] = write.getData();
        }
        cmd[i++] = (byte) (readCycles >> 8 & 0xff);
        cmd[i++] = (byte) (readCycles & 0xff);
        cmd[i] = readAddr;
        return cmd;
    }
}