TryReadExt.java

package builder.netsiddev.commands;

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

public class TryReadExt extends TryWriteExt {
    private final int readCycles;
    private final int readAddr;

    public TryReadExt(TryWriteExt tryWrite, int cycles, int addr) {
        this(cycles, addr);
        for (Write write : tryWrite.writes) {
            addWrite(write.getCycles(), write.getReg(), write.getData());
        }
    }

    public TryReadExt(int cycles, int addr) {
        if (cycles < 0 || cycles > MAX_CYCLE_DELAY) {
            throw new IllegalArgumentException("Read delay must be between 0 and 65535 cycles");
        }
        if (addr < 0 || addr > MAX_EXTENDED_SID_ADDRESS) {
            throw new IllegalArgumentException("Extended reads require a SID address in 0x0000..0x01ff");
        }
        readCycles = cycles;
        readAddr = addr;
        cyclesSendToServer = cycles;
    }

    @Override
    public byte[] toByteArray() {
        int i = 0;
        byte[] cmd = new byte[HEADER_SIZE + writes.size() * EXTENDED_WRITE_SIZE + EXTENDED_READ_SIZE];
        cmd[i++] = (byte) TRY_READ_EX.ordinal();
        cmd[i++] = 0;
        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() >> 8 & 0xff);
            cmd[i++] = (byte) (write.getReg() & 0xff);
            cmd[i++] = write.getData();
        }
        cmd[i++] = (byte) (readCycles >> 8 & 0xff);
        cmd[i++] = (byte) (readCycles & 0xff);
        cmd[i++] = (byte) (readAddr >> 8 & 0xff);
        cmd[i] = (byte) (readAddr & 0xff);
        return cmd;
    }
}