User Tools

Site Tools


base:efficient_tod_initialisation

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Next revisionBoth sides next revision
base:efficient_tod_initialisation [2019-05-12 20:31] – [TOD initialisation - the problem] silverdrbase:efficient_tod_initialisation [2019-05-12 20:34] silverdr
Line 1: Line 1:
 ====== TOD initialisation - the problem ====== ====== TOD initialisation - the problem ======
-Each [[https://en.wikipedia.org/wiki/MOS_Technology_CIA|6526 CIA]] chip as used in many Commodore machines has the so-called TOD (for Time Of Day) clock, capable of tracking elapsed time in human readable format (hours, minutes, seconds and tenths of seconds). While not offering the granularity of regular, binary timers, It is highly useful for measuring and displaying time in the form we are well conditioned to use every day. The registers' output is [[https://en.wikipedia.org/wiki/Binary-coded_decimal|BCD encoded]] to make it even easier to work with base-10 digits and numbers. There is however one caveat. TOD clock circuitry requires pulses of stable frequency of either 50 or 60 Hz being supplied to a dedicated pin. Such pulses can be inexpensively derived from the mains power AC, which was presumably the reason for designing the chip this way. The ability to work with both standard power line frequencies allowed a single version of the 6526 to be used in countries with either 50 or 60 Hz mains power lines. The only thing, which needs to be taken care of is informing 6526 which frequency is actually supplied. This is done by setting/clearing  bit seven of CRA ($0e) register of the CIA. KERNAL operating system in C64 initialises both CIA chips as part of the startup/reset sequence, inside IOINIT routine. This routine however does not try to identify what frequency is supplied but unconditionally sets both 6526s to expect 60Hz instead. Obviously, whenever in reality this is not the case (look C64 machines used in virtually all European countries), TOD clock drifts immediately away. Because of this, every application software, before attempting to utilise TOD as its timekeeping/alarm helper, needs to identify the frequency supplied to the TOD pin and initialise the required CIA's CRA register accordingly. Here comes the question "how" to reliably identify the actually supplied frequency?+Each [[https://en.wikipedia.org/wiki/MOS_Technology_CIA|6526 CIA]] chip as used in many Commodore machines has the so-called TOD (for Time Of Day) clock, capable of tracking elapsed time in human readable format (hours, minutes, seconds and tenths of seconds). While not offering the granularity of regular, binary timers, It is highly useful for measuring and displaying time in the form we are well conditioned to use every day. The registers' output is [[https://en.wikipedia.org/wiki/Binary-coded_decimal|BCD encoded]] to make it even easier to work with base-10 digits and numbers. There is however one caveat. TOD clock circuitry requires pulses of stable frequency of either 50 or 60 Hz being supplied to a dedicated pin. Such pulses can be inexpensively derived from the mains power AC, which was presumably the reason for designing the chip this way. The ability to work with both standard power line frequencies allowed a single version of the 6526 to be used in countries with either 50 or 60 Hz mains power lines. The only thing, which needs to be taken care of is informing 6526 which frequency is actually supplied. This is done by setting/clearing  bit seven of CRA ($0e) register of the CIA. KERNAL operating system in C64 initialises both CIA chips as part of the startup/reset sequence, inside IOINIT routine. This routine however does not try to identify what frequency is supplied but unconditionally sets both 6526s to expect 60Hz instead. Obviously, whenever in reality this is not the case (e.g. in C64 machines used in virtually all European countries), TOD clock drifts immediately away. Because of this, every application software, before attempting to utilise TOD as its timekeeping/alarm helper, needs to identify the frequency supplied to the TOD pin and initialise the required CIA's CRA register accordingly. Here comes the question "how" to reliably identify the actually supplied frequency?
 ===== Popular misconception ===== ===== Popular misconception =====
 First let's say how NOT to do it. It is important to note that contrary to a very popular and utterly wrong belief, NTSC or PAL video/colour encoding standards are NOT directly linked to mains power frequency. This means that checking which video standard the machine is of, should never be used to make assumptions about what the TOD frequency is. Not only PAL computers can be used in countries with 60Hz AC (and vice versa - NTSC computers can be run of 50Hz power) but in addition to that some computers do not derive their TOD clock frequency from the mains power source at all. For example both NTSC and PAL variants of Commodore SX-64 use 60 Hz TOD clock supplied by a dedicated crystal. First let's say how NOT to do it. It is important to note that contrary to a very popular and utterly wrong belief, NTSC or PAL video/colour encoding standards are NOT directly linked to mains power frequency. This means that checking which video standard the machine is of, should never be used to make assumptions about what the TOD frequency is. Not only PAL computers can be used in countries with 60Hz AC (and vice versa - NTSC computers can be run of 50Hz power) but in addition to that some computers do not derive their TOD clock frequency from the mains power source at all. For example both NTSC and PAL variants of Commodore SX-64 use 60 Hz TOD clock supplied by a dedicated crystal.
 ===== Proper solution ===== ===== Proper solution =====
-While there are various approaches to this problem, we believe the one presented below is not only the most compact but can also serve dual purpose, increasing the byte-size savings even further.+While there are various approaches to this problem, we believe the one presented below is not only the most compact but can also serve dual purpose (see below), increasing the byte-size savings even further.
 <code 6502acme> <code 6502acme>
 ; Detecting TOD frequency by Silver Dream ! / Thorgal / W.F.M.H. ; Detecting TOD frequency by Silver Dream ! / Thorgal / W.F.M.H.
base/efficient_tod_initialisation.txt · Last modified: 2020-10-27 02:31 by silverdr