Beginner question on interrupts

bbc micro/electron/atom/risc os coding queries and routines
Post Reply
AJW
Posts: 907
Joined: Sun Feb 15, 2004 2:01 pm
Contact:

Beginner question on interrupts

Post by AJW » Wed Jun 24, 2020 1:41 pm

Is there a recommended book or guide to interrupts? Particularly for graphics or sound. In the advanced user guide all I see mainly are technical specs of the 6522.

For example in one machine code book the author says poking 5 to &FE40 is the VIA scroll-controlling register, but looking at the new advanced user guide the registers don't even seem to be explained.

User avatar
tricky
Posts: 4687
Joined: Tue Jun 21, 2011 9:25 am
Contact:

Re: Beginner question on interrupts

Post by tricky » Wed Jun 24, 2020 9:29 pm

That would be the addressable latch, the OS sets the bottom 4 bits write and the top 4 bits read, writing 5 would set B5 to 0 and possibly change where the screen wraps arround to when it goes past &7FFF.

Code: Select all

\\ 0 b0..b2 addressable latch bit, b3 value to write, b4,b5 joystick-buttons b6 speech-ready b7 speech-interrupt
\\   B0 Sound write enable, B1 Read select speech, B2 write select speech, B3 keyboard write enable
\\   B4,B5 screen wrap address (&8000-size) 11:20K, 00:16K, 10:10K, 01:8K, B6 Caps Lock LED B7 Shift Lock LED
I could be wrong!

Interrupts are mostly for timing, whether that is mid-screen palette changes, waiting until after a particular sprite has been displayed to clear it and draw it somewhere else or even to change MODE like elite. You can use them for updating sound, especially if your game may take an unknown amount of time to process a frame and you don't want your tune to change speed.
The code in the AUG is what I used when I learned them back in the early 80s.
I tend to have many during a frame (field 1/50th of a second), usually one every 16 scan lines for the way I display memory and then hang certain events off some of them. I will read joystick X on one and then Y off another, update music and sound effects and increment one or more frame counters. The game loop waits for a counter to have been updated by counting its own and comparing, this makes the display code wait until there will be no flicker, whilst still allowing the game to take a little over 1/50th if it needs (this only happens on Frogger).

Naomasa298
Posts: 391
Joined: Sat Feb 16, 2013 12:49 pm
Contact:

Re: Beginner question on interrupts

Post by Naomasa298 » Wed Jun 24, 2020 11:37 pm

tricky wrote:
Wed Jun 24, 2020 9:29 pm
I tend to have many during a frame (field 1/50th of a second), usually one every 16 scan lines for the way I display memory and then hang certain events off some of them. I will read joystick X on one and then Y off another, update music and sound effects and increment one or more frame counters. The game loop waits for a counter to have been updated by counting its own and comparing, this makes the display code wait until there will be no flicker, whilst still allowing the game to take a little over 1/50th if it needs (this only happens on Frogger).
tricky - we have an interrupt query on my Binary Land thread, can you take a look?

AJW
Posts: 907
Joined: Sun Feb 15, 2004 2:01 pm
Contact:

Re: Beginner question on interrupts

Post by AJW » Thu Jun 25, 2020 8:45 am

tricky wrote:
Wed Jun 24, 2020 9:29 pm
That would be the addressable latch, the OS sets the bottom 4 bits write and the top 4 bits read, writing 5 would set B5 to 0 and possibly change where the screen wraps arround to when it goes past &7FFF.

Code: Select all

\\ 0 b0..b2 addressable latch bit, b3 value to write, b4,b5 joystick-buttons b6 speech-ready b7 speech-interrupt
\\   B0 Sound write enable, B1 Read select speech, B2 write select speech, B3 keyboard write enable
\\   B4,B5 screen wrap address (&8000-size) 11:20K, 00:16K, 10:10K, 01:8K, B6 Caps Lock LED B7 Shift Lock LED
I could be wrong!

Interrupts are mostly for timing, whether that is mid-screen palette changes, waiting until after a particular sprite has been displayed to clear it and draw it somewhere else or even to change MODE like elite. You can use them for updating sound, especially if your game may take an unknown amount of time to process a frame and you don't want your tune to change speed.
The code in the AUG is what I used when I learned them back in the early 80s.
I tend to have many during a frame (field 1/50th of a second), usually one every 16 scan lines for the way I display memory and then hang certain events off some of them. I will read joystick X on one and then Y off another, update music and sound effects and increment one or more frame counters. The game loop waits for a counter to have been updated by counting its own and comparing, this makes the display code wait until there will be no flicker, whilst still allowing the game to take a little over 1/50th if it needs (this only happens on Frogger).
Thanks. I found something about the addressable latch in the interrupts chapter but then in the Sheila table (same chapter) it says FE40 is something else - IBR/OBR I think. So how to make the link, confusing.

User avatar
tricky
Posts: 4687
Joined: Tue Jun 21, 2011 9:25 am
Contact:

Re: Beginner question on interrupts

Post by tricky » Mon Jun 29, 2020 4:20 pm

FE40 is Data register B on the system via, its 8 "port B" pins are connected to the b0..b7 in my post and b0..b3 are collectivly known as the "addressable latch". This is just 8 bits/flags that are indexed by bits b0..b2 and set to the value of b3.
if you set it to 6, then 8+6, 8, 14, 8, 14 the CAPS lock LED should go on and off - if the OS isn't setting back before you notice (may do it every key press - I can't remember).

AJW
Posts: 907
Joined: Sun Feb 15, 2004 2:01 pm
Contact:

Re: Beginner question on interrupts

Post by AJW » Sat Jul 04, 2020 8:20 pm

Thanks I'll look into this with a view to understanding fully background music initially rather than screen refresh or hardwrare scroll etc.

AJW
Posts: 907
Joined: Sun Feb 15, 2004 2:01 pm
Contact:

Re: Beginner question on interrupts

Post by AJW » Thu Jul 16, 2020 12:54 pm

Some will have seen this code before, but I've commented it to mum understanding:

Code: Select all

1420 LDA #&5F \setting VIA up,95,bits 0,1,2,3,4,6 set; disable other interrupts
1430 STA &FE6E \interrupt  enable register
1440 LDA #&A0 \160 bits 5 and 7 set; clears interrupts and times out T2 timer
1450 STA &FE6D \interrupt flag reg
1460 STA &FE6E \IER; bits 5 and 7; enable interrupt using timer T2
1470 LDA #&96 \150,setbits 1,2,4,7;PB latch enable,shift out under control of T2, one-shot output from PB7
1480 STA &FE6B \aux control reg
1490 LDA speed+1
1500 STA &FE68 \T2 low order counter
1510 LDA speed+2
1520 STA &FE69 \ high order counter;3000 CPU cycles equivalent written in
I can understand most i.e. enable T2 timer interrupt. but then can't understand what's happening with the aux. control register. What is the PB latch, why does it need to shift data in the shift register and what is PB7?

User avatar
BigEd
Posts: 3452
Joined: Sun Jan 24, 2010 10:24 am
Location: West
Contact:

Re: Beginner question on interrupts

Post by BigEd » Thu Jul 16, 2020 3:13 pm

It's probably worth having the 6522 datasheet to hand as a reference:
http://archive.6502.org/datasheets/mos_ ... v_1977.pdf

User avatar
ChrisB
Posts: 78
Joined: Wed Oct 05, 2011 10:37 pm
Location: Surrey
Contact:

Re: Beginner question on interrupts

Post by ChrisB » Thu Jul 16, 2020 7:28 pm

I know this may be heresy but if the objective is just a regular (or irregular) "pulse" to drive some process rather than a deep understanding of the hardware then you might want to investigate the event handlers. These are pre-packaged interrupts with none of the messing around with hardware addresses (which I realise may be half the point). I understand the performance isn't as good as straight interrupt code but it's a lot easier to deal with.

I've just used event 5 (interval timer crossing zero) to create some basic music for my game. You could also use the "buffer becoming empty" or even the vsync.

User avatar
0xC0DE
Posts: 743
Joined: Tue Mar 19, 2019 7:52 pm
Location: The Netherlands
Contact:

Re: Beginner question on interrupts

Post by 0xC0DE » Thu Jul 16, 2020 7:42 pm

ChrisB wrote:
Thu Jul 16, 2020 7:28 pm
I know this may be heresy but if the objective is just a regular (or irregular) "pulse" to drive some process rather than a deep understanding of the hardware then you might want to investigate the event handlers. These are pre-packaged interrupts with none of the messing around with hardware addresses (which I realise may be half the point). I understand the performance isn't as good as straight interrupt code but it's a lot easier to deal with.

I've just used event 5 (interval timer crossing zero) to create some basic music for my game. You could also use the "buffer becoming empty" or even the vsync.
Good advice!
0xC0DE
"I program my home computer / Beam myself into the future"
:arrow: Follow me on Twitter
:arrow: Visit my YouTube channel featuring my demos for Acorn Electron and BBC Micro

julie_m
Posts: 236
Joined: Wed Jul 24, 2019 9:53 pm
Location: Derby, UK
Contact:

Re: Beginner question on interrupts

Post by julie_m » Thu Jul 16, 2020 8:01 pm

I used the VSync event for a cassette loading music player. It counted calls to itself every 20ms. On every Nth time, it carried on to check the buffer state; and, if there was room, call OSWORD with some pitch and duration data. Otherwise it just restored Y, X, A and P and did an RTS back to whatever had been interrupted.

User avatar
sweh
Posts: 2263
Joined: Sat Mar 10, 2012 12:05 pm
Location: New York, New York
Contact:

Re: Beginner question on interrupts

Post by sweh » Fri Jul 17, 2020 2:55 am

ChrisB wrote:
Thu Jul 16, 2020 7:28 pm
I know this may be heresy but if the objective is just a regular (or irregular) "pulse" to drive some process rather than a deep understanding of the hardware then you might want to investigate the event handlers. These are pre-packaged interrupts with none of the messing around with hardware addresses (which I realise may be half the point). I understand the performance isn't as good as straight interrupt code but it's a lot easier to deal with.
It's not heresy; it's good advice. "What problem are you trying to solve; is this the best approach" is always a good question to ask.
Rgds
Stephen

User avatar
richardtoohey
Posts: 3986
Joined: Thu Dec 29, 2011 5:13 am
Location: Tauranga, New Zealand
Contact:

Re: Beginner question on interrupts

Post by richardtoohey » Fri Jul 17, 2020 3:28 am

This thread tickled my memory of this sort of thing ... http://www.acornelectron.co.uk/eug/71/h-lock.html

Not sure it's useful to this discussion but in the same sort of area ...

AJW
Posts: 907
Joined: Sun Feb 15, 2004 2:01 pm
Contact:

Re: Beginner question on interrupts

Post by AJW » Fri Jul 17, 2020 2:01 pm

sweh wrote:
Fri Jul 17, 2020 2:55 am
ChrisB wrote:
Thu Jul 16, 2020 7:28 pm
I know this may be heresy but if the objective is just a regular (or irregular) "pulse" to drive some process rather than a deep understanding of the hardware then you might want to investigate the event handlers. These are pre-packaged interrupts with none of the messing around with hardware addresses (which I realise may be half the point). I understand the performance isn't as good as straight interrupt code but it's a lot easier to deal with.
It's not heresy; it's good advice. "What problem are you trying to solve; is this the best approach" is always a good question to ask.
The code works well, just trying to fully understand it.

AJW
Posts: 907
Joined: Sun Feb 15, 2004 2:01 pm
Contact:

Re: Beginner question on interrupts

Post by AJW » Fri Jul 17, 2020 10:07 pm

So just for the record and this might be inaccurate but to my current understanding I've re-commented:

Code: Select all

570 LDA #&96 \150,setbits 1,2,4,7;PB latch enable,shift out under control of T2, one-shot output pulse to PB7 (peripheral B -user- port7,used by timers) from CB2 via shift reg (8bits); physically shift data to peripheral device/routine using interrupt
  580 STA &FE6B \aux control reg

Post Reply

Return to “programming”