Stop a running timer

Basic and Machine Language

Moderator: Moderators

Post Reply
User avatar
bjonte
Vic 20 Hobbyist
Posts: 110
Joined: Sun Jan 22, 2017 5:47 am
Location: Gothenburg

Stop a running timer

Post by bjonte »

I'd like to stop the VIA timers from running. What's the easiest way to do that? It looks like I would have to configure the timer to single interval mode, put zero in the timer register and acknowledge the IRQ. Is there an easier way to do it?
User avatar
Mike
Herr VC
Posts: 4841
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Re: Stop a running timer

Post by Mike »

I am sorry to tell you, but ...
bjonte wrote:I'd like to stop the VIA timers from running. What's the easiest way to do that?
... you probably should have phrased that question more in line of "Is there any way to do that?".

Unlike the 6526 CIA, there are no enable bits for the two timers in the 6522 VIA. Timer 1 can be operated in continuous or one-shot mode (the latter, which you call 'single interval mode'). Timer 2 only works in one-shot mode.

When in one-shot mode, both timers issue an interrupt when underflowing from 0 to $FFFF - provided the corresponding bit in the IER is set - and then continue counting down from $FFFF, but won't issue any further interrupts unless they are reloaded.

In continuous mode, timer 1 is reloaded from the latch value when it has underflowed. With a latch value of 0, the timer value alternates between 0 and $FFFF. A latch value of $FFFF will result in the timer value being $FFFF on two successive cycles and then counting down from there, for a period of 65537 cycles.

That being said, what application did you have in mind that led you want to stop the timers? Wasn't it sufficient to merely stop interrupts from occuring?
User avatar
bjonte
Vic 20 Hobbyist
Posts: 110
Joined: Sun Jan 22, 2017 5:47 am
Location: Gothenburg

Re: Stop a running timer

Post by bjonte »

Mike wrote: Thu Mar 11, 2021 5:07 pmThat being said, what application did you have in mind that led you want to stop the timers? Wasn't it sufficient to merely stop interrupts from occuring?
In order to make my program start to be deterministic I start by shutting off the IRQ and NMI interrupts. I'm thinking if there's a timer that continues to count it will set the interrupt flag sometime in the future and potentially trigger an intended interrupt later.

I'm now setting the timer to one-shot and acknowledge the IRQ when it has triggered to avoid this.
User avatar
srowe
Vic 20 Scientist
Posts: 1340
Joined: Mon Jun 16, 2014 3:19 pm

Re: Stop a running timer

Post by srowe »

bjonte wrote: Sat Mar 13, 2021 11:52 am In order to make my program start to be deterministic I start by shutting off the IRQ and NMI interrupts. I'm thinking if there's a timer that continues to count it will set the interrupt flag sometime in the future and potentially trigger an intended interrupt later.
If the IER bit is disabled no IRQ/NMI is generated, all that happens in the appropriate bit in the IFR register get set. This will be cleared when you next write to the timer latch.
User avatar
Noizer
Vic 20 Devotee
Posts: 297
Joined: Tue May 15, 2018 12:00 pm
Location: Europa

Re: Stop a running timer

Post by Noizer »

I would state SEI as first opcode at all.
BR
Valid rule today as earlier: 1 Byte = 8 Bits
-._/classes instead of masses\_.-
User avatar
bjonte
Vic 20 Hobbyist
Posts: 110
Joined: Sun Jan 22, 2017 5:47 am
Location: Gothenburg

Re: Stop a running timer

Post by bjonte »

srowe wrote: Sat Mar 13, 2021 12:43 pm If the IER bit is disabled no IRQ/NMI is generated, all that happens in the appropriate bit in the IFR register get set. This will be cleared when you next write to the timer latch.
Mmhm, true. If I clear the IRQ enable bits that should be enough because any attempts to enable them would also mean setting the timer value and reset the acknowledge bit.
User avatar
Mike
Herr VC
Posts: 4841
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Re: Stop a running timer

Post by Mike »

bjonte wrote:If I clear the IRQ enable bits that should be enough because any attempts to enable them would also mean setting the timer value and reset the acknowledge bit.
As long as solely your program runs, you're pretty much in control regarding this. When 3rd party code is executed from your program, all bets are off as that other code can reinit the VIA registers to its liking without any chance for your program to inhibit this or regain control.

Note, bits in the IER are not defined by writing them as zeroes or ones, rather when writing, bit 7 gives the required status (0 = clear, 1 = set) and the other bits indicate which enables should be changed. For example, writing %01111111 = $7F clears all IER flags, writing %10000001 = $81 explicitly enables timer 1 IRQs and leaves the enable status of all other sources unchanged.
In order to make my program start to be deterministic I start by shutting off the IRQ and NMI interrupts.
That still doesn't quite answer what you're up to. Is your program supposed to autostart from cartridge?
User avatar
srowe
Vic 20 Scientist
Posts: 1340
Joined: Mon Jun 16, 2014 3:19 pm

Re: Stop a running timer

Post by srowe »

Mike wrote: Sun Mar 14, 2021 3:58 am Note bits in the IER are not defined by writing them as zeroes or ones, rather when writing, bit 7 gives the required status (0 = clear, 1 = set) and the other bits indicate which enables should be changed. For example, writing %01111111 = $7F clears all IER flags, writing %10000001 = $81 explicitly enables timer 1 IRQs and leaves the enable status of all other sources unchanged.
It's surprising how much code doesn't do this properly, I've disassembled several programs/ROMs (including stuff written by CBM) that does the wrong thing.
groepaz
Vic 20 Scientist
Posts: 1187
Joined: Wed Aug 25, 2010 5:30 pm

Re: Stop a running timer

Post by groepaz »

cant you "stop" at least timer2 by setting it to count pb6 pulses (which never happen)?
I'm just a Software Guy who has no Idea how the Hardware works. Don't listen to me.
User avatar
bjonte
Vic 20 Hobbyist
Posts: 110
Joined: Sun Jan 22, 2017 5:47 am
Location: Gothenburg

Re: Stop a running timer

Post by bjonte »

Mike wrote: Sun Mar 14, 2021 3:58 amThat still doesn't quite answer what you're up to. Is your program supposed to autostart from cartridge?
Ah, sorry for the delay. I seem to be missing notifications. I made an invitation intro for a Commodore club so it's a standalone PRG file started from BASIC.
User avatar
bjonte
Vic 20 Hobbyist
Posts: 110
Joined: Sun Jan 22, 2017 5:47 am
Location: Gothenburg

Re: Stop a running timer

Post by bjonte »

groepaz wrote: Mon Mar 15, 2021 8:19 am cant you "stop" at least timer2 by setting it to count pb6 pulses (which never happen)?
Yes, I just don't know what people have connected so I'd avoid that.
User avatar
bjonte
Vic 20 Hobbyist
Posts: 110
Joined: Sun Jan 22, 2017 5:47 am
Location: Gothenburg

Re: Stop a running timer

Post by bjonte »

I went with clearing the IRQ enable bits and acknowledging everything.
Post Reply