Log inRegister an accountBrowse CSDbHelp & documentationFacts & StatisticsThe forumsAvailable RSS-feeds on CSDbSupport CSDb Commodore 64 Scene Database
You are not logged in 
CSDb User Forums


Forums > C64 Coding > Extending MOS 6522 VIA timers to more than 16 bits without interrupts
2019-07-14 23:31
Krill

Registered: Apr 2002
Posts: 1522
Extending MOS 6522 VIA timers to more than 16 bits without interrupts

This is a trick i came across on some Commodore/drive related newsgroup, which i've put to good use in my loader, and the question is this:

Is there a canonical name for this technique*? :)

And not just for the VIA, but generally in micro-controller usage.
(My SE-fu has failed me, couldn't find anything noteworthy on the net or 6502.org, nor could i find that original newsgroup post.)

Background: Unlike on the 6526 CIA, the 16-bit VIA timers cannot be linked in hardware for 32-bit timeouts.
65535 cycles at 1 MHz are about 65.5 milliseconds, which is just a bit more than 3 PAL videoframes and insufficient for many purposes.
The normal method to extend that timeout period is to have an interrupt for every timeout, which would increase a variable, effectively yielding more than 16 bits with the help of software.

Unfortunately, interrupts are often not feasible (especially in disk drive code), so here's the

Solution: Arithmetically couple two timers by giving them different timeouts.
Consider timer A with a timeout of 65535 ($ffff) cycles, and timer B with 65533 ($fffd) cycles.
As they reset with different periods, their counters will drift apart over time.
Thus, a simple check like this:
lda >counterA; timer A current counter value MSB
cmp >counterB; timer B current counter value MSB
bmi timedout ; hibyte(counterA - counterB) >= 128 -> timeout
makes it possible to have much longer timeouts than just 65535 cycles.
For this example, that's

65535 * 256 * 128 / (65535 - 65533) = 1,073,725,440 cycles

which is a 30-bit number and corresponds to roughly 18 minutes at 1 MHz.

The bigger the period difference is, the smaller the effective timeout is.
For practical purposes, the minimum is 2 as in this example.

* Note that it works without (co-)prime numbers, but is possibly related to those approaches.
2020-08-03 21:59
Copyfault

Registered: Dec 2001
Posts: 308
Quoting Krill
This is a trick i came across on some Commodore/drive related newsgroup, which i've put to good use in my loader, and the question is this:

Is there a canonical name for this technique*? :)
Not that I'd know, but just looking at the mathmatical context I'd say it's some kind of "simultaneous inequalities" in different modulo class rings (just like simultaneous congruences occur when applying the Chinese remainder theorem).

Quoting Krill
[...]
The normal method to extend that timeout period is to have an interrupt for every timeout, which would increase a variable, effectively yielding more than 16 bits with the help of software.

Unfortunately, interrupts are often not feasible (especially in disk drive code), ...

Hmm, but if you'd choose two different counters, like a "dedicated counter for every timer", it should be possible to increase them seperately and calculate the larger value e.g. by applying the extended euclidian algorithm. I'm sure I miss smth, especially since you write interrupts are often not feasible (especially in disk drive code). What's the reason for this?

Quoting Krill
Solution: Arithmetically couple two timers by giving them different timeouts.
Consider timer A with a timeout of 65535 ($ffff) cycles, and timer B with 65533 ($fffd) cycles.
As they reset with different periods, their counters will drift apart over time.
Thus, a simple check like this:
lda >counterA; timer A current counter value MSB
cmp >counterB; timer B current counter value MSB
bmi timedout ; hibyte(counterA - counterB) >= 128 -> timeout
makes it possible to have much longer timeouts than just 65535 cycles.
That's just excellent :) But just for further understanding: doesn't setting a timer to e.g. $ffff mean it effectively counts $ffff+1=$10000=65536 cycles? Afaik, CIA-timers work that way, i.e. the value set is one less than the no. of cycles counted.

Quoting Krill
For practical purposes, the minimum is 2 as in this example.
...
This is interesting: basically(=thinking purely theoretical) a difference of 1 should also work. What breaks this in practice?
2020-08-04 19:00
Oswald

Registered: Apr 2002
Posts: 4609
this trick is so cool :) its like one of those when you think hard on optimising code and you have a brilliant idea and you can do it with even skipping some steps :)
2020-08-07 09:43
mankeli

Registered: Oct 2010
Posts: 66
yes. very cool trick indeed! You can invent a name for it, but make sure it has a nice acronym. Like PDC = Phase Difference Counters
2020-08-07 11:48
Copyfault

Registered: Dec 2001
Posts: 308
Since the approach is coupled to the Chinese remainder theorem which I checked on Wikipedia the other day, I forgot to mention that the calculus that can be extracted from it has a name already: https://en.wikipedia.org/wiki/Residue_number_system

So maybe this could be adopted to name the timer coupling, i.e. Residue Counter System or whatever...


Anyway, wanted to point to that wikipedia-entry in the first place.
RefreshSubscribe to this thread:

You need to be logged in to post in the forum.

Search the forum:
Search   for   in  
All times are CET.
Search CSDb
Advanced
Users Online
Jazzcat/Onslaught
Radd Maxx/SWIM
Trasher/Fairlight
Alakran_64
goto80/HT
Mr. Spock/T'Pau
Guests online: 90
Top Demos
1 Uncensored  (9.7)
2 Edge of Disgrace  (9.6)
3 Coma Light 13  (9.6)
4 Memento Mori  (9.6)
5 Comaland 100%  (9.6)
6 The Shores of Reflec..  (9.6)
7 Unboxed  (9.6)
8 Lunatico  (9.6)
9 Remains  (9.5)
10 C=Bit 18  (9.5)
Top onefile Demos
1 Dawnfall V1.1  (9.5)
2 Gumbo Revised  (9.5)
3 Smile to the Sky  (9.5)
4 Daah, Those Acid Pil..  (9.5)
5 Bad Boy  (9.5)
6 Crystal Gazer  (9.5)
7 Cuarentenauta  (9.5)
8 Instinct  (9.5)
9 The Night It Snowed  (9.5)
10 Rewind  (9.4)
Top Groups
1 PriorArt  (9.4)
2 Booze Design  (9.4)
3 Censor Design  (9.4)
4 Fossil  (9.4)
5 Performers  (9.3)
Top Logo Graphicians
1 Mermaid  (9.4)
2 Pal  (9.3)
3 Jailbird  (8.9)
4 Elko  (8.9)
5 Shine  (8.8)

Home - Disclaimer
Copyright © No Name 2001-2020
Page generated in: 0.173 sec.