When should keyboard interrupts be enabled during startup?

want to talk about MESS/model b/beebem/b-em/electrem/elkulator? do it here!
Post Reply
murraypaul
Posts: 2
Joined: Sun Feb 18, 2018 11:49 am
Contact:

When should keyboard interrupts be enabled during startup?

Post by murraypaul » Sun Feb 18, 2018 12:21 pm

I hope this is the right forum, the question is about developing an emulator rather than using one, which seemed to fit into this group and into Projects.

I'm been working on a BBC B emulator as a hobby, and and all the pieces emulated to a state where keyboard handling should work, but it doesn't.
The matrix auto-scans, detects a keypress and signals CA2 on the system VIA. The CPU never gets the interrupt though, and after spending a while debugging my code, I realised that this is correct, the OS has not enabled keyboard interrupts during startup.

I can't see how they would ever get enabled though.
I'm using the MOS disassembly at http://mdfs.net/Docs/Comp/BBC/OS1-20/ as my reference.

There are only five references to the system VIA interrupt flag (FE4E) in the entire code.
At D9D7 it is read to determine if this is a power on. The comment confirm that the system should start with all interrupts disabled.
At DA82 it is set, and keyboard interrupts are not enabled. (set to &F2 == 11110010, lowest bit is CA2.)
At DD0E it is read as part of interrupt handling
At EEE4 it is enabled for keyboard interrupts, but as far as I can see this is part of a routine which is only called in response to a keypress, which would require keyboard interrupts to be enabled.
At EF06 it is disabled for keyboard interrupts.

So I have a chicken and the egg problem, the interrupt is only enabled when handling the interrupt?

Clearly I'm missing something, but at the moment I can't see what. I had a quick look at the BASIC disassembly, and couldn't see an OSBYTE &97 call which could be setting it, and when I boot up and BASIC runs, program flow never hits EEE4, it calls OSWORD 0 to get a line from the buffer, but nothing ever gets put into the buffer, so cycles forever.

Can anyone point me in the right direction?

User avatar
hoglet
Posts: 7117
Joined: Sat Oct 13, 2012 6:21 pm
Location: Bristol
Contact:

Re: When should keyboard interrupts be enabled during startup?

Post by hoglet » Sun Feb 18, 2018 12:42 pm

murraypaul wrote: Can anyone point me in the right direction?
I have a suspicion the keyboard status is normally polled off the 100Hz timer interrupt.

Dave

RobC
Posts: 2182
Joined: Sat Sep 01, 2007 9:41 pm
Contact:

Re: When should keyboard interrupts be enabled during startup?

Post by RobC » Sun Feb 18, 2018 12:49 pm

hoglet wrote:I have a suspicion the keyboard status is normally polled off the 100Hz timer interrupt.
Yes - the System VIA Timer 1 is set to go off every 10ms and this triggers the polling of the keyboard.

murraypaul
Posts: 2
Joined: Sun Feb 18, 2018 11:49 am
Contact:

Re: When should keyboard interrupts be enabled during startup?

Post by murraypaul » Sun Feb 18, 2018 1:06 pm

Aha, I was working through the 6522 implementation and started with keyboards, so I haven't done the timers yet.
Thanks for both your answers, should be able to get this working now.

User avatar
jgharston
Posts: 3057
Joined: Thu Sep 24, 2009 11:22 am
Location: Whitby/Sheffield
Contact:

Re: When should keyboard interrupts be enabled during startup?

Post by jgharston » Sun Feb 18, 2018 5:07 pm

Code: Select all

.LDA03 
\**+********** set up system VIA *****************************************
LDX #&0F            :\ set PORT B to output on bits 0-3 Input 4-7
STX LFE40+2

.LDA08 
\* set addressable latch IC 32 for peripherals via PORT B */
DEX                 :\ loop start
STX LFE40           :\ write latch IC32
CPX #&09            :\ is it 9
BCS LDA08           :\ if so go back and do it again
                    :\ X=8 at this point
                    :\ Caps lock On, SHIFT lock undetermined
                    :\ Keyboard Autoscan on
                    :\ sound disabled (may still sound)
INX                 :\ X=9
.LDA11 
At this point the keyboard is set up to scan the key matrix. The System VIA IRQs have not yet been enabled.

Code: Select all

; &DA6B
\************** clear interrupt and enable registers of Both VIAs ********
LDA #&7F            :\ A=%011111111 - Enable all
INX                 :\ X=&01
.LDA6E 
STA LFE40+13,X      :\ Enable all System VIA IRQs
STA LFE60+13,X      :\ Enable all User VIA IRQs
DEX                 :\ 
BPL LDA6E           :\ 
At this point the System VIA is now set up so that the keyboard can generate interupts.

Code: Select all

.LDA80 
LDX #&F2:STX LFE40+14 :\ enable interrupts 1,4,5,6 of system VIA
\ 1=Frame sync pulse, 4=End of A/D conversion
\ 5=T2 counter (for speech), 6=T1 counter (10 mSec intervals)
The centisecond timer is enabled so centisecond key processing can now work.

Code: Select all

LDA #&0E:STA LFE40+6 :\ Set system VIA T1 counter (Low)
...
LDA #&27             :\ set T1 (hi) to &27 this sets T1 to &270E (9998 uS)
STA LFE40+7          :\ or 10msec, interrupts occur every 10msec therefore
STA LFE40+5
The centisecond timer is set to interupt every centisecond.

From this point keyboard processing will work. Note that IRQs are disabled, so the MOS will not respond to any interupts, which is why all languages have to start with:

LDX #&FF:TXS :\ Reset stack
CLI :\ Enable IRQs

Once you get past this point, the MOS will respond to interupts from the keyboard through the System VIA. Also, OSRDCH explicitly enables IRQs, so even if IRQs are disabled, calling OSRDCH will read from the keyboard - though no background processing will occur.

Code: Select all

$ bbcbasic
PDP11 BBC BASIC IV Version 0.25
(C) Copyright J.G.Harston 1989,2005-2015
>_

Post Reply