Log inRegister an accountBrowse CSDbHelp & documentationFacts & StatisticsThe forumsAvailable RSS-feeds on CSDbSupport CSDb Commodore 64 Scene Database
 Welcome to our latest new user hllerena78 ! (Registered 2019-08-20) 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: 1241
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.
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
Alakran_64
Speedball/Cyber Design
Røly/MYD^STHD^TSA
Don Kichote/Samar Pr..
MaD ][/Starship
Lynx/ΤRIΛD
Guests online: 56
Top Demos
1 Unboxed  (9.7)
2 Uncensored  (9.7)
3 Coma Light 13  (9.7)
4 Edge of Disgrace  (9.6)
5 Comaland 100%  (9.6)
6 The Shores of Reflec..  (9.6)
7 Lunatico  (9.6)
8 Wonderland XII  (9.5)
9 C=Bit 18  (9.5)
10 Old Men in Used Cars  (9.5)
Top onefile Demos
1 LSR 64 V0.31  (10)
2 Smile to the Sky  (9.5)
3 Dawnfall V1.1  (9.5)
4 Crystal Gazer  (9.5)
5 Daah, Those Acid Pil..  (9.5)
6 Rewind  (9.5)
7 Instinct  (9.5)
8 Pandemoniac Part 5 o..  (9.5)
9 Innervasion  (9.4)
10 Bad Boy  (9.4)
Top Groups
1 Fossil  (9.8)
2 PriorArt  (9.7)
3 Performers  (9.6)
4 Oxyron  (9.4)
5 Censor Design  (9.4)
Top Musicians
1 Rob Hubbard  (9.7)
2 LMan  (9.7)
3 Jeroen Tel  (9.6)
4 Linus  (9.6)
5 Drax  (9.5)

Home - Disclaimer
Copyright © No Name 2001-2019
Page generated in: 0.076 sec.