Timers and interrupts problem in BBC assembler

bbc micro/electron/atom/risc os coding queries and routines
Post Reply
Griff
Posts: 10
Joined: Mon Nov 06, 2023 7:13 pm
Contact:

Timers and interrupts problem in BBC assembler

Post by Griff »

Hi.

I suspect this is a result of me being a 6502 assembler newbie (after over 40 years of using a BBC micro), but I'd be really grateful for another pair of eyes on this one.

I'm having a bit of a problem with setting up a timer and interrupt handler in a project involving sending MIDI data over a Music 2000 clone interface. I need a high-resolution timer so triggering an interrupt from one of the 1MHz VIA timers seems ideal for this job. I've read over the sections in the Advanced User Guide, "Advanced Machine Code Techniques for the BBC Micro", "Assembly Language Programming for the BBC Micro" and have watched Kieran Connell's excellent two-part introduction to interrupts and still can't get my code to work.

Below is a very, very basic setup that doesn't work for me. I had expected that this would start a timer that would set the T1 timer interrupt flag every 65535 clock cycles, thereby incrementing the "counter" counter. I would also expect to see the "handlerCalls" counter increment with every call to the handler.

What actually happens is:
- counter and handlerCalls remain at zero after running the program
- if I press Escape, handlerCalls increases by TWO. (counter remains zero; however, I would expect that as bit 6 of the IFR flag wouldn't be set)

Does anyone have any idea what I'm doing wrong? (I fully expect this to be in "schoolboy error" territory.)

Code: Select all

 10 DIM START 100
 20 
 30 counter=&70:?counter=0
 40 handlerCalls=&71:?handlerCalls=0
 50 oldIrq=&80
 51 
 60 FOR pass=0 TO 3 STEP 3
 61 P%=START
 70 [
 80 OPT pass
 90 
100 .init
110 SEI
120 
130 LDA #&C0  \ Enable interrupt on
140 STA &FE6E \ user VIA.
150 
160 LDA &204:STA oldIrq   \
170 LDA &205:STA oldIrq+1 \ Store old interrupt vectors
180 LDA #(handler MOD 256)\ and set new ones for new
190 STA &204              \ handler.
200 LDA #(handler DIV 256)\
210 STA &205              \
220 
230 LDA #&40              \ Set continuous
240 STA &FE6B             \ interrupts.
250 
260 LDA #&FF              \
270 STA &FE64             \ Set timer
280 LDA #&FF              \ parameters.
290 STA &FE65             \
300 
310 CLI
320 RTS
330 
340 .handler
350 LDA &FC:PHA
360 INC handlerCalls      \ Check if handler is invoked.
370 
380 LDA &FE6D             \ Check bit 6 of
390 AND #&40              \ IFR register and exit
400 BEQ returnToOS        \ if not set.
410 
420 LDA &FE64             \ Clear low byte latch,
430 INC counter           \ Increment number of times flag is set.
440 .returnToOS
450 PLA:STA &FC
460 JMP (oldIrq)
470 
480 ]
490 
500 NEXT pass
510 
520 CALL init
SteveF
Posts: 1697
Joined: Fri Aug 28, 2015 9:34 pm
Contact:

Re: Timers and interrupts problem in BBC assembler

Post by SteveF »

Definitely no expert here, but comparing your code with Kieran's:
  • Kieran's code appears to do "and #&80" where you do "and #&40" in the handler
  • Kieran's code sets the counter and latch values for timer 1 during the setup whereas you only appear to set the counter.
It's possible one or both of those is your problem. If not I'll wait for someone more knowledgeable to respond... :-)

Edit: Having had a look at the advanced user guide, your "and '#&40" looks correct to me (bit 7 is any interrupt, bit 6 is timer 1), but my experience with interrupts is pretty minimal.
Griff
Posts: 10
Joined: Mon Nov 06, 2023 7:13 pm
Contact:

Re: Timers and interrupts problem in BBC assembler

Post by Griff »

No worries, thanks for looking at this, Steve. I did try various permutations of register settings (to no avail) and set the latch values, too. (I think the latches are if you want to specify a different value for subsequent timeouts, though?)

One other thing I noticed: if I'd previously run the Basic Editor from ROM, my program actually crashes on pressing Escape, as in the entire system hangs and I need to reset, which again suggests I've done something wrong!
SteveF
Posts: 1697
Joined: Fri Aug 28, 2015 9:34 pm
Contact:

Re: Timers and interrupts problem in BBC assembler

Post by SteveF »

FWIW I just tried the code from your original post in b-em and it seems to be working fine. I can print ?&70 and ?&71 and they both change.

You're not running this with the tube active, are you? (I doubt it, but I had to ask.)
User avatar
tricky
Posts: 7719
Joined: Tue Jun 21, 2011 9:25 am
Contact:

Re: Timers and interrupts problem in BBC assembler

Post by tricky »

Nothing jumps out at me either, but I haven't really written anything for about a year!
I usually do:

Code: Select all

380 LDA &FE6D             \ Check bit 6 of
390 AND #&40              \ IFR register and exit
400 BEQ returnToOS        \ if not set.
410 
420 STA &FE60             \ Clear interrupt flag
but that is just because I have been copying that code around for over 10 years!
I think the behaviour of lda &FE64 might be controlled by PCR or ACR and the default might be different on the emulators, but only guessing here!
Griff
Posts: 10
Joined: Mon Nov 06, 2023 7:13 pm
Contact:

Re: Timers and interrupts problem in BBC assembler

Post by Griff »

Oh my god, I'm so sorry, guys. I was indeed running it with the tube enabled -- you hit the nail on the head, Steve! I turned that off and, lo and behold, it works fine.

I've been banging my head against a brick wall for two days over that one. Still, at least I'll remember to switch off the PiTube on my actual Beeb when I transfer it over, plus I know a lot more about the VIA timers than I did when I started :)
Griff
Posts: 10
Joined: Mon Nov 06, 2023 7:13 pm
Contact:

Re: Timers and interrupts problem in BBC assembler

Post by Griff »

Thanks for looking into it, both of you.
SteveF
Posts: 1697
Joined: Fri Aug 28, 2015 9:34 pm
Contact:

Re: Timers and interrupts problem in BBC assembler

Post by SteveF »

You're welcome, I'm chuffed my wild guess was right for once. :-) Glad you got it sorted.
User avatar
tricky
Posts: 7719
Joined: Tue Jun 21, 2011 9:25 am
Contact:

Re: Timers and interrupts problem in BBC assembler

Post by tricky »

If you set the load and run addresses to be in the beeb's memory space, you can probably leave it on.
&FFaaaa, or if writing from beebasm &30000 + &aaaa.
Griff
Posts: 10
Joined: Mon Nov 06, 2023 7:13 pm
Contact:

Re: Timers and interrupts problem in BBC assembler

Post by Griff »

tricky wrote: Fri May 10, 2024 6:33 am If you set the load and run addresses to be in the beeb's memory space, you can probably leave it on.
&FFaaaa, or if writing from beebasm &30000 + &aaaa.
That is very useful to know, since I was wondering whether there was any way I could use the extra RAM and/or additional CPU, since 32K doesn't leave you with much storage space for MIDI sequencer data. I will try and play around with that. Thank you!
Post Reply

Return to “programming”