VIA6522BC.java

/*
 * via1d15xx.c - VIA1 emulation in the 1541, 1541II, 1570 and 1571 disk drive.
 *
 * Written by
 *  Andreas Boose <viceteam@t-online.de>
 *  André Fachat <fachat@physik.tu-chemnitz.de>
 *  Daniel Sladic <sladic@eecg.toronto.edu>
 *  Ettore Perazzoli <ettore@comm2000.it>
 *
 * This file is part of VICE, the Versatile Commodore Emulator.
 * See README for copyright notice.
 *
 *  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.
 *
 *  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.
 *
 *  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.components.c1541;

import libsidplay.components.iec.IECBus;

/**
 * Handling of the Bus Controller.
 *
 * @author Ken Händel
 *
 */
public abstract class VIA6522BC extends VIACore {
    /**
     * IEC Bus.
     */
    private final IECBus iecbus;

    /**
     * The parallel cable connected to the device.
     */
    private IParallelCable parallelCable;

    /**
     * The C1541 device ID.
     */
    private final int deviceId;

    /**
     * Set a parallel cable.
     *
     * @param cable parallel cable
     */
    public final void setParallelCable(final IParallelCable cable) {
        this.parallelCable = cable;
    }

    /**
     * Get the parallel cable.
     *
     * @return parallel cable
     */
    public final IParallelCable getParallelCable() {
        return parallelCable;
    }

    /**
     * Creates a new instance of VIA6522BC.
     *
     * @param id  the C1541 device ID
     * @param bus the IEC Bus
     */
    public VIA6522BC(final int id, final IECBus bus) {
        super("1541Drive" + id + "VIA6522BC");
        this.deviceId = id;
        this.iecbus = bus;
    }

    @Override
    public void setCa2(final int state) {
    }

    @Override
    public void setCb2(final int state) {
    }

    @Override
    public void storePra(final int addr, final byte value) {
        getParallelCable().driveWrite(value, (via[VIA_PCR] & 0xe) == 0xa, deviceId);
    }

    @Override
    public final void storePrb(final byte byt) {
        if (byt != oldpb) {
            iecbus.updateDrive(deviceId, byt);
        }
    }

    @Override
    public void storeAcr(final byte value) {
    }

    @Override
    public void storeSr(final byte value) {
    }

    @Override
    public void storeT2l(final byte value) {
    }

    @Override
    public byte readPra() {
        return (byte) (via[VIACore.VIA_PRA] & via[VIACore.VIA_DDRA]
                | getParallelCable().driveRead((via[VIA_PCR] & 0xe) == 0xa) & ~via[VIACore.VIA_DDRA]);
    }

    @Override
    protected byte readPraPins() {
        return (byte) (via[VIA_PRA] & via[VIA_DDRA]
                | getParallelCable().driveRead(false) & ~via[VIA_DDRA]);
    }

    @Override
    public final byte readPrb() {
        /*
         * VIA1 Port B:
         *
         * bit 7   ATN IN
         * bits 6-5 device address preset switches IN
         * bit 4   ATN acknowledge OUT
         * bit 3   CLOCK OUT
         * bit 2   CLOCK IN
         * bit 1   DATA OUT
         * bit 0   DATA IN
         *
         * IN mask:  1110 0101 = 0xe5
         * OUT mask: 0001 1010 = 0x1a
         *
         * Output pins configured as inputs through DDRB are inactive/high.
         */
        final int driveId = ((deviceId & ~0x08) << 5) & 0x60;
        final int ddrb = via[VIACore.VIA_DDRB] & 0xff;
        final int prb = via[VIACore.VIA_PRB] & 0xff;

        final int input = ((iecbus.deviceRead() & 0xff) ^ 0x85) | 0x1a | driveId;

        return (byte) ((prb & ddrb) | (input & ~ddrb & 0xff));
    }

}