VIACore.java

/**
 * viacore.c - Core functions for VIA emulation.
 * <p>
 * Written by André Fachat <fachat@physik.tu-chemnitz.de> Andreas Boose <viceteam@t-online.de>
 * <p>
 * This file is part of VICE, the Versatile Commodore Emulator. See README for a copyright notice.
 * <p>
 * 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.components.c1541;

import libsidplay.common.Event;

import java.util.Arrays;

/**
 * <PRE>
 * 24jan97 a.fachat new interrupt handling, hopefully according to the specs now. All interrupts (note: not timer events
 * (i.e. alarms) are put into one interrupt flag. if an interrupt condition changes, the function (i.e. cpp macro)
 * update_myviairq() id called, that checks the IRQ line state. This is now possible, as ettore has decoupled A_* alarm
 * events from interrupts for performance reasons.
 * <p>
 * A new function for signaling rising/falling edges on the control lines is introduced:
 * myvia_signal(VIA_SIG_[CA1|CA2|CB1|CB2], VIA_SIG_[RISE|FALL]) which signals the corresponding edge to the VIA. The
 * constants are defined in via.h.
 * <p>
 * Input latching follows the MOS NMOS 6522 port-register and ACR descriptions.
 * </PRE>
 */
public abstract class VIACore {
    /* MOS 6522 registers */
    /**
     * Port B.
     */
    protected static final int VIA_PRB = 0;
    /**
     * Port A.
     */
    protected static final int VIA_PRA = 1;
    /**
     * Data direction register for port B.
     */
    protected static final int VIA_DDRB = 2;
    /**
     * Data direction register for port A.
     */
    protected static final int VIA_DDRA = 3;

    /**
     * Timer 1 count low.
     */
    protected static final int VIA_T1CL = 4;
    /**
     * Timer 1 count high.
     */
    protected static final int VIA_T1CH = 5;
    /**
     * Timer 1 latch low.
     */
    protected static final int VIA_T1LL = 6;
    /**
     * Timer 1 latch high.
     */
    protected static final int VIA_T1LH = 7;
    /**
     * Timer 2 count low read-only.
     */
    protected static final int VIA_T2CL = 8;
    /**
     * Timer 2 latch low - write-only.
     */
    protected static final int VIA_T2LL = 8;
    /**
     * Timer 2 latch/count high.
     */
    protected static final int VIA_T2CH = 9;

    /**
     * Serial port shift register.
     */
    protected static final int VIA_SR = 10;
    /**
     * Auxiliary control register.
     */
    protected static final int VIA_ACR = 11;
    /**
     * Peripheral control register.
     */
    protected static final int VIA_PCR = 12;

    /**
     * Interrupt flag register.
     */
    protected static final int VIA_IFR = 13;
    /**
     * Interrupt control register.
     */
    protected static final int VIA_IER = 14;
    /**
     * Port A with no handshake.
     */
    protected static final int VIA_PRA_NHS = 15;

    /* Interrupt Masks */
    /* MOS 6522 */
    /**
     * Control Bit.
     */
    protected static final int VIA_IM_IRQ = 128;
    /**
     * Timer 1 underflow.
     */
    protected static final int VIA_IM_T1 = 64;
    /**
     * Timer 2 underflow.
     */
    protected static final int VIA_IM_T2 = 32;
    /**
     * Handshake.
     */
    protected static final int VIA_IM_CB1 = 16;
    /**
     * Handshake.
     */
    protected static final int VIA_IM_CB2 = 8;
    /**
     * Shift Register completion.
     */
    protected static final int VIA_IM_SR = 4;
    /**
     * Handshake.
     */
    protected static final int VIA_IM_CA1 = 2;
    /**
     * Handshake.
     */
    protected static final int VIA_IM_CA2 = 1;

    /* Signal values (for signaling edges on the control lines) */
    public static final int VIA_SIG_CA1 = 0;
    protected static final int VIA_SIG_CA2 = 1;
    public static final int VIA_SIG_CB1 = 2;
    protected static final int VIA_SIG_CB2 = 3;

    public static final int VIA_SIG_FALL = 0;
    public static final int VIA_SIG_RISE = 1;

    /**
     * Registers
     */
    protected final byte[] via = new byte[16];

    protected int ifr;
    protected int ier;
    protected char tal;
    protected char tbl;
    /**
     * Timer 2 counter value while T2 is in PB6 pulse-counting mode.
     */
    private char t2Counter;

    private boolean pb6High;
    private final Event pb6Alarm = Event.of("VIA PB6 sample", event -> samplePb6());

    /**
     * Timer 2 high counter while the shift register puts T2 into 8-bit mode.
     */
    private int t2HighCounter;

    /**
     * A write to T2H permits one Timer 2 interrupt.
     */
    private boolean t2IrqAllowed;

    protected long tau;
    protected long tbu;
    protected long tai;
    protected long tbi;

    /**
     * Timer 1 output state on PB7. Either 0x00 or 0x80.
     */
    protected int t1Pb7;

    protected int pb7;
    protected int pb7x;
    protected int pb7o;
    protected int pb7xx;
    protected int pb7sx;
    protected byte oldpa;
    protected byte oldpb;
    protected byte ila;
    protected byte ilb;
    protected int ca2State;
    protected int cb2State;

    protected boolean enabled;

    private final Event t1Alarm;
    private final Event t2Alarm;

    private boolean lastState;

    private boolean srActive;
    private boolean srInput = true;
    private int srClock = 1;
    private int srBits;

    private final Event srAlarm = Event.of("VIA system-clock shift", event -> clockSerial());
    private final Event srInputAlarm = Event.of("VIA external serial input", event -> sampleSerialInput());
    private final Event ca2PulseAlarm = Event.of("VIA CA2 pulse end", event -> {
        ca2State = 1;
        setCa2(1);
    });
    private final Event cb2PulseAlarm = Event.of("VIA CB2 pulse end", event -> {
        cb2State = 1;
        setCb2(1);
    });

    private int shiftMode() {
        return via[VIA_ACR] >>> 2 & 7;
    }

    private void startSerial() {
        ifr &= ~VIA_IM_SR;
        checkInterrupts();
        alarmUnset(srAlarm);
        alarmUnset(srInputAlarm);
        srBits = 0;
        srActive = shiftMode() != 0;
        if (srClock != 1) {
            srClock = 1;
            setCb1(1);
        }
        if (shiftMode() == 2 || shiftMode() == 6) {
            alarmSet(srAlarm, cpuClk() + 1);
        }
    }

    private void stopSerial() {
        alarmUnset(srAlarm);
        alarmUnset(srInputAlarm);
        srActive = false;
        srBits = 0;
        if (srClock != 1) {
            srClock = 1;
            setCb1(1);
        }
        restoreCb2();
    }

    private void restoreCb2() {
        cb2State = (via[VIA_PCR] & 0xe0) == 0xc0 ? 0 : 1;
        setCb2(cb2State);
    }

    private void serialOutputBit() {
        int data = via[VIA_SR] & 255;
        cb2State = data >>> 7;
        setCb2(cb2State);
        via[VIA_SR] = (byte) (data << 1 | cb2State); // MOS output modes recirculate bit 7 into bit 0.
    }

    private void serialBitCompleted() {
        if (++srBits == 8) {
            srBits = 0;
            if (shiftMode() != 4) {
                ifr |= VIA_IM_SR;
                checkInterrupts();
            }
            // External-clock modes keep shifting; free-running output has no SR interrupt.
            if (shiftMode() != 3 && shiftMode() != 4 && shiftMode() != 7) {
                srActive = false;
                if (shiftMode() >= 4) restoreCb2();
            }
        }
    }

    private void sampleSerialInput() {
        if (srActive && shiftMode() < 4) {
            via[VIA_SR] = (byte) (via[VIA_SR] << 1 | (srInput ? 1 : 0));
            serialBitCompleted();
        }
    }

    private void serialHalfClock() {
        if (!srActive) return;
        srClock ^= 1;
        if (srClock == 0 && shiftMode() >= 4) serialOutputBit();
        setCb1(srClock);
        // CB1 interrupt/latch detection remains active for internally generated serial clocks.
        signal(VIA_SIG_CB1, srClock);
        if (srClock == 1) {
            if (shiftMode() < 4) sampleSerialInput();
            else serialBitCompleted();
        }
    }

    private void clockSerial() {
        serialHalfClock();
        if (srActive && (shiftMode() == 2 || shiftMode() == 6)) {
            alarmSet(srAlarm, cpuClk() + 1);
        }
    }

    /** Observe internally generated serial clocks. Existing drive wiring need not implement this hook. */
    protected void setCb1(int state) {
    }

    private boolean isCa2Indinput() {
        return (via[VIA_PCR] & 0x0a) == 0x02;
    }

    private boolean isCa2Handshake() {
        return (via[VIA_PCR] & 0x0c) == 0x08;
    }

    private boolean isCa2PulseMode() {
        return (via[VIA_PCR] & 0x0e) == 0x0a;
    }

    private boolean isCa2ToggleMode() {
        return (via[VIA_PCR] & 0x0e) == 0x08;
    }

    private boolean isCb2Handshake() {
        return (via[VIA_PCR] & 0xc0) == 0x80;
    }

    private boolean isCb2PulseMode() {
        return (via[VIA_PCR] & 0xe0) == 0xa0;
    }

    private boolean isCb2ToggleMode() {
        return (via[VIA_PCR] & 0xe0) == 0x80;
    }

    /*
     * <PRE>
     * 01apr98 a.fachat
     *
     * One-shot Timing (partly from 6522-VIA.txt):
     *
     * 	                     +-+ +-+ +-+ +-+ +-+ +-+   +-+ +-+ +-+ +-+ +-+ +-+
     * 	                02 --+ +-+ +-+ +-+ +-+ +-+ +-#-+ +-+ +-+ +-+ +-+ +-+ +-
     * 	                       |   |                           |
     * 	                       +---+                           |
     * 	       WRITE T1C-H ----+   +-----------------#-------------------------
     * 	        ___                |                           |
     * 	        IRQ OUTPUT --------------------------#---------+
     * 	                           |                           +---------------
     * 	                           |                           |
     * 	        PB7 OUTPUT --------+                           +---------------
     * 	                           +-----------------#---------+
     * 	         T1                | N |N-1|N-2|N-3|     | 0 | -1|N  |N-1|N-2|
     * 	         T2                | N |N-1|N-2|N-3|     | 0 | -1| -2| -3| -4|
     * 	                           |                           |
     * 	                           |<---- N + 1.5 CYCLES ----.|<--- N + 2 cycles --.
     * 	                                                         +---+
     * 	 myviat*u* clk ------------------------------------------+   +--------
     * 	                                                     |
     * 	                                                     |
     * 	                                                  call of
     * 	                                                int_myvia*
     * 	                                                   here
     *
     * 	   real myviatau value = myviatau* + TAUOFFSET
     * 	   myviatbu = myviatbu* + 0
     *
     *
     * IRQ and PB7 are set/toggled at the low-high transition of Phi2,
     * but int_* is called a half-cycle before that. Does that matter?
     *
     * PB7 output is still to be implemented
     * </PRE>
     */

    /**
     * timer values do not depend on a certain value here, but PB7 does...
     */
    private static final int TAUOFFSET = -1;

    protected void checkInterrupts() {
        boolean irq = (ifr & ier & 0x7f) != 0;
        if (lastState ^ irq) {
            setIRQ(irq);
            lastState = irq;
        }
    }

    /* the next two are used in myvia_read() */

    /**
     * T2 is an 8-bit low-byte timer when the shift register is driven by T2.
     * <p>
     * This covers:
     * <p>
     * $04 shift in under T2 $10 shift out free-running under T2 $14 shift out under T2
     */
    private boolean isT2ShiftRegisterMode() {
        final int mode = via[VIA_ACR] & 0x1c;
        return mode == 0x04 || mode == 0x10 || mode == 0x14;
    }

    private long myviata() {
        if (cpuClk() < tau - TAUOFFSET) {
            return tau - TAUOFFSET - cpuClk() - 2;
        } else {
            return tal - (cpuClk() - tau + TAUOFFSET) % (tal + 2);
        }
    }

    private long myviatb(final long rclk) {
        if ((via[VIA_ACR] & 0x20) != 0) {
            /* PB6 pulse-counting mode. */
            return t2Counter;
        }

        if (isT2ShiftRegisterMode()) {
            /*
             * In T2-controlled shift-register modes only the low byte counts
             * CPU clocks. It reloads from T2LL after every underflow while
             * the high byte is cascaded from those underflows.
             */
            final int low = (int) (tbu - rclk - 2) & 0xff;
            int high = t2HighCounter;

            if (rclk == tbi) {
                high = high - 1 & 0xff;
            }
            return high << 8 | low;
        }

        return tbu - rclk - 2;
    }

    /** Sample the physical PB6 pin, independently of IRB and CPU port acknowledgement. */
    private void samplePb6() {
        final boolean high = (readPrbPins() & 0x40) != 0;
        if (pb6High && !high) {
            // Real NMOS hardware counts through zero: N+1 pulses, not N (De Jong, 1980).
            if (t2Counter-- == 0 && t2IrqAllowed) {
                t2IrqAllowed = false;
                ifr |= VIA_IM_T2;
                checkInterrupts();
            }
        }
        pb6High = high;
        alarmSet(pb6Alarm, cpuClk() + 1);
    }

    private long myviatb() {
        return myviatb(cpuClk());
    }

    /**
     * Update timer A latch value.
     */
    private void updateMyviatal(final long rclk) {
        pb7x = 0;
        pb7xx = 0;

        if (rclk > tau) {
            final int nuf = (int) ((tal + 1 + rclk - tau) / (tal + 2));

            if (0 == (via[VIA_ACR] & 0x40)) {
                if (nuf - pb7sx > 1 || 0 == pb7) {
                    pb7o = 1;
                    pb7sx = 0;
                }
            }
            pb7 ^= nuf & 1;

            tau = TAUOFFSET + tal + 2 + (rclk - (rclk - tau + TAUOFFSET) % (tal + 2));
            if (rclk == tau - tal - 1) {
                pb7xx = 1;
            }
        }

        if (tau == rclk) {
            pb7x = 1;
        }

        tal = (char) ((via[VIA_T1LL] & 0xff) + ((via[VIA_T1LH] & 0xff) << 8));
    }

    private void updateMyviatbl() {
        tbl = (char) ((via[VIA_T2CL] & 0xff) + ((via[VIA_T2CH] & 0xff) << 8));
    }

    /*
     * -------------------------------------------------------------------------
     */
    public final void disable() {
        alarmUnset(pb6Alarm);
        stopSerial();
        alarmUnset(ca2PulseAlarm);
        alarmUnset(cb2PulseAlarm);
        alarmUnset(t1Alarm);
        alarmUnset(t2Alarm);
        enabled = false;
    }

    /*
     * according to Rockwell, all internal registers are cleared, except for the
     * Timer (1 and 2, counter and latches) and the shift register.
     */
    public void reset /* viacore_reset */() {
        alarmUnset(pb6Alarm);
        pb6High = true;
        alarmUnset(t1Alarm);
        alarmUnset(t2Alarm);
        alarmUnset(srAlarm);
        alarmUnset(srInputAlarm);
        alarmUnset(ca2PulseAlarm);
        alarmUnset(cb2PulseAlarm);
        srActive = false;
        srInput = true;
        if (srClock != 1) {
            srClock = 1;
            setCb1(1);
        }
        srBits = 0;
        /* clear registers */
        Arrays.fill(via, (byte) 0);
        for (int i = 4; i < 10; i++) {
            via[i] = (byte) 0xff; /* AB 98.08.23 */
        }

        tal = 0xffff;
        tbl = 0xffff;
        t2Counter = 0xffff;
        t2HighCounter = 0xff;
        t2IrqAllowed = false;
        tau = cpuClk();
        tbu = cpuClk();

        t1Pb7 = 0x80;

        ier = 0;
        ifr = 0;

        pb7 = 0;
        pb7x = 0;
        pb7o = 0;
        pb7xx = 0;
        pb7sx = 0;

        /* disable vice interrupts */
        tai = 0;
        tbi = 0;
        lastState = false;

        oldpa = (byte) 0xff;
        oldpb = (byte) 0xff;

        ca2State = 1;
        cb2State = 1;
        setCa2(ca2State); /* input = high */
        setCb2(cb2State); /* input = high */

        enabled = true;
    }

    public final void signal(final int line, final int edge) {
        if (line == VIA_SIG_CB2 && shiftMode() != 0) {
            srInput = edge != 0;
            return;
        }
        if (line == VIA_SIG_CB1 && srActive && (shiftMode() == 3 || shiftMode() == 7)) {
            if (edge == VIA_SIG_FALL && shiftMode() == 7) serialOutputBit();
            if (edge == VIA_SIG_RISE) {
                if (shiftMode() == 3) {
                    // MOS requires CB2 to stay valid through the cycle following CB1's rising edge.
                    alarmUnset(srInputAlarm);
                    alarmSet(srInputAlarm, cpuClk() + 1);
                } else serialBitCompleted();
            }
        }
        switch (line) {
            case VIA_SIG_CA1:
                if ((edge != 0 ? 1 : 0) == (via[VIA_PCR] & 0x01)) {
                    if ((via[VIA_ACR] & 1) != 0 && (ifr & VIA_IM_CA1) == 0) {
                        ila = readPraPins();
                    }
                    if (isCa2ToggleMode() && 0 == ca2State) {
                        ca2State = 1;
                        setCa2(ca2State);
                    }
                    ifr |= VIA_IM_CA1;
                    checkInterrupts();
                }
                break;
            case VIA_SIG_CA2:
                if (0 == (via[VIA_PCR] & 0x08)) {
                    ifr |= ((edge << 2 ^ via[VIA_PCR]) & 0x04) != 0 ? 0 : VIA_IM_CA2;
                    checkInterrupts();
                }
                break;
            case VIA_SIG_CB1:
                if ((edge != 0 ? 0x10 : 0) == (via[VIA_PCR] & 0x10)) {
                    if ((via[VIA_ACR] & 2) != 0 && (ifr & VIA_IM_CB1) == 0) {
                        ilb = (byte) (readPrbPins() & ~via[VIA_DDRB] | via[VIA_PRB] & via[VIA_DDRB]);
                    }
                    if (!srActive && isCb2ToggleMode() && 0 == cb2State) {
                        cb2State = 1;
                        setCb2(cb2State);
                    }
                    ifr |= VIA_IM_CB1;
                    checkInterrupts();
                }
                break;
            case VIA_SIG_CB2:
                if (0 == (via[VIA_PCR] & 0x80)) {
                    ifr |= ((edge << 6 ^ via[VIA_PCR]) & 0x40) != 0 ? 0 : VIA_IM_CB2;
                    checkInterrupts();
                }
                break;
        }
    }

    public final void write(int addr, byte b) {
        long rclk = cpuClk();

        switch (addr) {

            /* these are done with saving the value */
            case VIA_PRA: /* port A */
                ifr &= ~VIA_IM_CA1;
                if (!isCa2Indinput()) {
                    ifr &= ~VIA_IM_CA2;
                }
                if (isCa2Handshake()) {
                    ca2State = 0;
                    setCa2(ca2State);
                    if (isCa2PulseMode()) {
                        alarmUnset(ca2PulseAlarm);
                        alarmSet(ca2PulseAlarm, cpuClk() + 1);
                    }
                }
                if ((ier & (VIA_IM_CA1 | VIA_IM_CA2)) != 0) {
                    checkInterrupts();
                }
                // $FALL-THROUGH$

            case VIA_PRA_NHS: /* port A, no handshake */
                via[VIA_PRA_NHS] = b;
                addr = VIA_PRA;
                // $FALL-THROUGH$

            case VIA_DDRA:
                via[addr] = b;
                b = (byte) (via[VIA_PRA] | ~via[VIA_DDRA]);
                storePra(addr, b);
                oldpa = b;
                break;

            case VIA_PRB: /* port B */
                ifr &= ~VIA_IM_CB1;
                if ((via[VIA_PCR] & 0xa0) != 0x20) {
                    ifr &= ~VIA_IM_CB2;
                }
                if (!srActive && isCb2Handshake()) {
                    cb2State = 0;
                    setCb2(cb2State);
                    if (isCb2PulseMode()) {
                        alarmUnset(cb2PulseAlarm);
                        alarmSet(cb2PulseAlarm, cpuClk() + 1);
                    }
                }
                if ((ier & (VIA_IM_CB1 | VIA_IM_CB2)) != 0) {
                    checkInterrupts();
                }
                // $FALL-THROUGH$

            case VIA_DDRB:
                via[addr] = b;
                b = (byte) (via[VIA_PRB] | ~via[VIA_DDRB]);
                if ((via[VIA_ACR] & 0x80) != 0) {
                    b = (byte) (b & 0x7f | t1Pb7);
                }
                storePrb(b);
                oldpb = b;
                break;

            case VIA_SR: /* Serial Port output buffer */
                via[addr] = b;
                storeSr(b);
                startSerial();
                break;

            /* Timers */

            case VIA_T1CL:
            case VIA_T1LL:
                via[VIA_T1LL] = b;
                updateMyviatal(rclk);
                break;

            case VIA_T1CH: /* Write timer A high */
                via[VIA_T1LH] = b;
                updateMyviatal(rclk);
                /* load counter with latch value */
                tau = rclk + tal + 3 + TAUOFFSET;
                tai = rclk + tal + 2;
                alarmUnset(t1Alarm);
                alarmSet(t1Alarm, tai);

                /* set pb7 state */
                t1Pb7 = 0;
                pb7 = 0;
                pb7o = 0;

                /* Clear T1 interrupt */
                ifr &= ~VIA_IM_T1;
                checkInterrupts();
                break;

            case VIA_T1LH: /* Write timer A high-order latch */
                via[addr] = b;
                updateMyviatal(rclk);

                /* Clear T1 interrupt */
                ifr &= ~VIA_IM_T1;
                checkInterrupts();
                break;

            case VIA_T2LL: /* Write timer 2 low latch */
                via[VIA_T2LL] = b;
                updateMyviatbl();
                storeT2l(b);
                break;

            case VIA_T2CH: /* Write timer 2 high */
                via[VIA_T2CH] = b;
                updateMyviatbl();

                /* Writing T2 high loads both bytes of the counter. */
                t2Counter = tbl;
                t2HighCounter = b & 0xff;
                t2IrqAllowed = true;

                alarmUnset(t2Alarm);

                if ((via[VIA_ACR] & 0x20) == 0) {
                    if (isT2ShiftRegisterMode()) {
                        /*
                         * T2-controlled shift-register modes use the low byte
                         * as a repeating 8-bit timer. The high byte is still
                         * cascaded from low-byte underflows.
                         */
                        final int low = via[VIA_T2LL] & 0xff;
                        tbu = rclk + low + 3;
                        tbi = rclk + low + 2;
                    } else {
                        /* Normal 16-bit Timer 2 mode. */
                        tbu = rclk + tbl + 3;
                        tbi = rclk + tbl + 2;
                    }
                    alarmSet(t2Alarm, tbi);
                }

                /* Clear T2 interrupt */
                ifr &= ~VIA_IM_T2;
                checkInterrupts();
                break;

            /* Interrupts */

            case VIA_IFR: /* 6522 Interrupt Flag Register */
                ifr &= ~b;
                checkInterrupts();
                break;

            case VIA_IER: /* Interrupt Enable Register */
                if ((b & VIA_IM_IRQ) != 0) {
                    /* set interrupts */
                    ier |= b & 0x7f;
                } else {
                    /* clear interrupts */
                    ier &= ~b;
                }
                checkInterrupts();
                break;

            /* Control */

            case VIA_ACR:
                final int activeShiftMode = shiftMode();
                /* bit 7 timer 1 output to PB7 */
                updateMyviatal(rclk);
                if (((via[VIA_ACR] ^ b) & 0x80) != 0) {
                    if ((b & 0x80) != 0) {
                        /* Rising edge of PB7 timer-output enable sets PB7 high. */
                        t1Pb7 = 0x80;
                        pb7 = 1 ^ pb7x;
                    }
                }
                if (((via[VIA_ACR] ^ b) & 0x40) != 0) {
                    pb7 ^= pb7sx;
                    if ((b & 0x40) != 0) {
                        if (pb7x != 0 || pb7xx != 0) {
                            if (tal != 0) {
                                pb7o = 1;
                            } else {
                                pb7o = 0;
                                if ((via[VIA_ACR] & 0x80) != 0 && pb7x != 0 && 0 == pb7xx) {
                                    pb7 ^= 1;
                                }
                            }
                        }
                    }
                }
                pb7sx = pb7x;

                /*
                 * Bit 5 switches Timer 2 between CPU-clock mode and PB6
                 * pulse-counting mode.
                 */
                if (((via[VIA_ACR] ^ b) & 0x20) != 0) {
                    if ((b & 0x20) != 0) {
                        /*
                         * Timer -> PB6 pulse counting.
                         *
                         * Stopping the timer takes one cycle, therefore the
                         * counter decreases once more before becoming static.
                         */
                        t2Counter = (char) ((myviatb(rclk) - 1) & 0xffff);
                        alarmUnset(t2Alarm);
                        pb6High = (readPrbPins() & 0x40) != 0;
                        if (enabled) alarmSet(pb6Alarm, rclk + 1);
                    } else {
                        /*
                         * PB6 pulse counting -> timer.
                         *
                         * Starting the timer also takes one cycle. Keep the
                         * frozen value visible for that cycle before countdown
                         * resumes.
                         */
                        alarmUnset(pb6Alarm);
                        final int current = t2Counter;
                        t2HighCounter = current >> 8 & 0xff;

                        if ((b & 0x1c) == 0x04
                                || (b & 0x1c) == 0x10
                                || (b & 0x1c) == 0x14) {
                            final int low = current & 0xff;
                            tbu = rclk + low + 3;
                            tbi = rclk + low + 2;
                        } else {
                            tbu = rclk + current + 3;
                            tbi = rclk + current + 2;
                        }

                        alarmUnset(t2Alarm);
                        alarmSet(t2Alarm, tbi);
                    }
                }
                // A disabled latch follows its pins even if C1 is already pending.
                // Enabling it closes that transparent latch on the current value.
                if ((b & ~via[VIA_ACR] & 1) != 0 && (ifr & VIA_IM_CA1) != 0) {
                    ila = readPraPins();
                }
                if ((b & ~via[VIA_ACR] & 2) != 0 && (ifr & VIA_IM_CB1) != 0) {
                    ilb = (byte) (readPrbPins() & ~via[VIA_DDRB] | via[VIA_PRB] & via[VIA_DDRB]);
                }
                via[addr] = b;
                storeAcr(b);


                /* A mode change stops an in-progress shift; the next SR access starts a byte. */
                if (srActive && shiftMode() != activeShiftMode) stopSerial();
                break;

            case VIA_PCR:
                alarmUnset(ca2PulseAlarm);
                alarmUnset(cb2PulseAlarm);

                /* bit 7, 6, 5 CB2 handshake/interrupt control */
                /* bit 4 CB1 interrupt control */

                /* bit 3, 2, 1 CA2 handshake/interrupt control */
                /* bit 0 CA1 interrupt control */

                if ((b & 0x0e) == 0x0c) { /* set output low */
                    ca2State = 0;
                } else if ((b & 0x0e) == 0x0e) { /* set output high */
                    ca2State = 1;
                } else { /* set to toggle/pulse/input */
                    /* FIXME: is this correct if handshake is already active? */
                    ca2State = 1;
                }
                setCa2(ca2State);

                // During a shift CB2 belongs to the serial port, not the PCR output modes.
                if (!srActive) {
                    if ((b & 0xe0) == 0xc0) { /* set output low */
                        cb2State = 0;
                    } else if ((b & 0xe0) == 0xe0) { /* set output high */
                        cb2State = 1;
                    } else { /* set to toggle/pulse/input */
                        /* FIXME: is this correct if handshake is already active? */
                        cb2State = 1;
                    }
                    setCb2(cb2State);
                }

                via[addr] = b;

                break;

            default:
                via[addr] = b;

        } /* switch */
    }

    /* --------------------------------------------------------------------- */

    public final byte read/* viacore_read */(int addr) {
        long rclk = cpuClk();
        // Choose the pending sample before the port access acknowledges C1.
        final boolean latchedA = (via[VIA_ACR] & 1) != 0 && (ifr & VIA_IM_CA1) != 0;
        final boolean latchedB = (via[VIA_ACR] & 2) != 0 && (ifr & VIA_IM_CB1) != 0;
        final byte portA = ila, portB = ilb;

        switch (addr) {

            case VIA_PRA: /* port A */
                ifr &= ~VIA_IM_CA1;
                if ((via[VIA_PCR] & 0x0a) != 0x02) {
                    ifr &= ~VIA_IM_CA2;
                }
                if (isCa2Handshake()) {
                    ca2State = 0;
                    setCa2(ca2State);
                    if (isCa2PulseMode()) {
                        alarmUnset(ca2PulseAlarm);
                        alarmSet(ca2PulseAlarm, cpuClk() + 1);
                    }
                }
                if ((ier & (VIA_IM_CA1 | VIA_IM_CA2)) != 0) {
                    checkInterrupts();
                }
                // $FALL-THROUGH$

            case VIA_PRA_NHS: { /* port A, no handshake */
                /*
                 * WARNING: this pin reads the voltage of the output pins, not the ORA value as
                 * the other port. Value read might be different from what is expected due to
                 * an excessive load.
                 */
                byte b = readPra(); // Preserve the device's CPU-access side effects even when latched.
                return latchedA ? portA : b;
            }

            case VIA_PRB: { /* port B */
                ifr &= ~VIA_IM_CB1;
                if ((via[VIA_PCR] & 0xa0) != 0x20) {
                    ifr &= ~VIA_IM_CB2;
                }
                if ((ier & (VIA_IM_CB1 | VIA_IM_CB2)) != 0) {
                    checkInterrupts();
                }

                /*
                 * WARNING: this pin reads the ORA for output pins, not the voltage on the pins
                 * as the other port.
                 */
                byte b = readPrb();
                b = latchedB ? portB : (byte) (b & ~via[VIA_DDRB] | via[VIA_PRB] & via[VIA_DDRB]);

                if ((via[VIA_ACR] & 0x80) != 0) {
                    updateMyviatal(rclk);
                    b = (byte) (b & 0x7f | t1Pb7);
                }
                return b;
            }

            /* Timers */
            case VIA_T1CL /* TIMER_AL */: /* timer A low */
                ifr &= ~VIA_IM_T1;
                checkInterrupts();
                return (byte) myviata();

            case VIA_T1CH /* TIMER_AH */: /* timer A high */
                return (byte) (myviata() >> 8);

            case VIA_T2CL /* TIMER_BL */: /* timer B low */
                ifr &= ~VIA_IM_T2;
                checkInterrupts();
                return (byte) myviatb();

            case VIA_T2CH /* TIMER_BH */: /* timer B high */
                return (byte) (myviatb() >> 8 & 0xff);

            case VIA_SR: { /* Serial Port Shift Register */
                byte data = via[addr];
                startSerial();
                return data;
            }

            /* Interrupts */
            case VIA_IFR: {/* Interrupt Flag Register */
                byte t = (byte) ifr;
                if ((ifr & ier /* [VIA_IER] */) != 0) {
                    t |= 0x80;
                }
                return t;
            }

            case VIA_IER: /* 6522 Interrupt Control Register */
                return (byte) (ier /* [VIA_IER] */ | 0x80);

        } /* switch */

        return via[addr];
    }

    protected VIACore(String name) {
        t1Alarm = Event.of(name + "T1", event -> {
            /* handle continuous mode */
            if (0 != (via[VIA_ACR] & 0x40)) {
                tai += tal + 2;
                alarmSet(event, tai);
            }

            t1Pb7 ^= 0x80;
            ifr |= VIA_IM_T1;
            checkInterrupts();
        });

        t2Alarm = Event.of(name + "T2", event -> {
            if (isT2ShiftRegisterMode()) {
                serialHalfClock();
                /*
                 * T2 low underflow in shift-register mode:
                 *
                 * - decrement the cascaded high byte
                 * - reload the low byte from T2LL
                 * - continue running
                 * - generate one T2 interrupt when the complete 16-bit
                 *   counter crosses $0000 -> $ffff
                 */
                t2HighCounter = t2HighCounter - 1 & 0xff;

                if (t2HighCounter == 0xff && t2IrqAllowed) {
                    ifr |= VIA_IM_T2;
                    t2IrqAllowed = false;
                    checkInterrupts();
                }

                final int period = (via[VIA_T2LL] & 0xff) + 2;
                tbu += period;
                tbi += period;
                alarmSet(event, tbi);
            } else {
                ifr |= VIA_IM_T2;
                t2IrqAllowed = false;
                checkInterrupts();
            }
        });
    }

    protected abstract void alarmSet(final Event alarm, final long ti);

    protected abstract void alarmUnset(final Event alarm);

    protected abstract long cpuClk();

    protected abstract void setIRQ(boolean irq);

    protected abstract void storePra(int addr, byte value);

    protected abstract void storePrb(byte value);

    protected abstract void storeAcr(byte value);

    protected abstract void storeSr(byte value);

    protected abstract void storeT2l(byte value);

    /** Pin sample without CPU-read acknowledgement or a handshake. Override for devices with read side effects. */
    protected byte readPraPins() { return readPra(); }

    /** Physical PB pins, before input latching; must not acknowledge a CPU port access. */
    protected byte readPrbPins() { return readPrb(); }

    protected abstract byte readPra();

    protected abstract byte readPrb();

    protected abstract void setCa2(int state);

    protected abstract void setCb2(int state);
}