Saturday 5 August 2023

6502 : BEN2 : Interruptions

Buttons

Ben Eater included a number of buttons in his original board specification.  I installed a couple of buttons on BEN2's daughter board AMI but haven't found a reason to make use of them yet.  Buttons are the most basic input method for a DIY computer and potentially you could be stuck with button input and LED or LCD output.  Thankfully by adding the ACIA I have the ability to keyboard / terminal input / output making buttons unnecessary.
However, a button can be very useful as an interrupt mechanism.  For example, my simple blinking LED test cant easily be terminated.  It would be good to be able to leave the LED blinking until a button is pressed to stop it.

Bens tutorials

Ben provides a couple of tutorials, "interrupts" and "interrupt handling basics" to familiarise use with their use.  The first one describes 6502 IRQB and NMIB pins which implement controllable and non-maskable interrupts respectively.  IRQB interrupts assume that the device at the other end should clear the interrupt situation; it has a tendency to report a lot (hundreds and thousands) of interrupts.  In fact Ben's "binary converting to decimal" (BCD) routine is very useful to count them.  Non-maskable interrupts are very dangerous as they can interrupt absolutely any code, including interrupt service routines or themselves, so they are rarely appropriate.
Ben's second video describes how the W65C22 VIA provides an enhanced interrupt capability and shows how to to set it up, so that was the one I implemented.

Hardware connections

The VIA has two pins CA1 and CA2 which can be setup for interrupts.  As I previously used CA2 for my shift register I used CA1.
I connected up a button with a pull-up resistor to the positive rail so that when the button is pressed the signal on CA1 changes from 5V to GND.
I also need VIA pin 21 (IRQB) connected to 6502 pin 4 (IRQB) but that was already in place in my original build for BEN2.

Reset Vector

The 6502 IRQ vector is fixed at memory address $FFFE-$FFFF.  I set the contents to be the address $1003.  This allows me to load my test programs at $1000 as usual and, by making the first instruction a "jmp start", the interrupt service routine will follow on immediately at $1003.

Test program

My test program was very simple.  At the start of the program I initialise the interrupt on CA1 by setting values in IER (Interrupt Enable Register) and PCR (Peripheral Control register)
Before that there is a simple call to a subroutine for interrupt handling (button_int) the subroutine below followed by a RTI (Return from Interrupt) opcode.


My interrupt handling routine was very basic, I just displayed a letter 'j' on the screen - very dull.
Still, this is a useful addition to my system.

No comments:

Post a Comment