# Commodore 64 clock using the CIA TOD clock (from “Compute!”)

I’ve recently typed-in a nice program from Compute! magazine[1]Issue 43, December 1983 called “Commodore 64 clock”, by Paul F. Schatz.

The program doesn’t make use of the standard operating system software clock (the jiffy clock referenced by variables TI and TI\$) . Instead, it makes use of the Time of Day Clock of the CIA #1 chip.

Hours, minutes, seconds and tenths of a second are stored on the following CIA #1 registers[2]CIA #2 also has its own TOD clock.:

• 56331: this register is used to store hours. Bit 7 is used as a flag for AM/PM (1 = PM, 0 = AM);
• 56330: this register is used to store minutes. Bit 7 is unused;
• 56329: this register is used to store seconds. Again, bit 7 is left unused;
• 56328: this register is used to store tenths of seconds. Bits 4 to 7 are unused.

Numbers are stored on these registers using BCD format. BCD stands for Binary Coded Decimal. That means, BCD numbers are just base 10 numbers whose digits are represented using base 2.

For instance, the number 12 may be represented in BCD format by using a couple of 4 bits binary numbers, one for each digit.

110 = 00012;    210 = 00102

So, 1210 equals 00010010 in BCD format.

Now, in order to store it in memory, the BCD number must be seen as a standard binary number. And, 000100102 equals 1810. As a result, if we want to set hours to the value 12, we need to place the value 18 decimal on register 56331.

To store minutes and seconds on registers 56330 and 56329 the method is the same. Please note that bit 7 of these registers is unused, as we will never need it. For instance, in the AM/PM format, the first digit of hours will never be greater than 1. And, for minutes and seconds, the first digit will never be greater than 5. So, only 3 bits of the high nybble[3]A nybble is half a byte, that is four bits. are needed for the first digit of hours, minutes and seconds. For the second digit instead, all four bits of the low nybble are required.

On register 56328, only the low nybble is used, as we only have one digit for the tenths of seconds.

One important thing must be noticed. The TOD clock only starts when tenths of seconds are set. Furthermore, if hours are read, TOD clock registers update process is stopped. The TOD clock keeps running, it’s just that registers are “frozen”. Registers updating restarts only when tenths of seconds are also read. Why is that? For instance, if current time is 11.59.59, if you read the value 11 for hours, you may then risk to read a zero for minutes. That may result in a time of 11 hours and 0 minutes, instead of 12 hours and zero minutes. That’s why when you read hours, TOD registers are stopped being updated.

Remember, if you only need to read hours, you also have to read tenths of seconds. Otherwise, TOD clock registers will stay frozen.

### The program

Here is the program from “Compute!” I have typed-in for you.

The program makes use of a machine language routine to show current time on screen. The time display routine is hooked-up to the standard interrupt handler, so that current time can be shown while you are using BASIC. That’s quite handy, isn’t it?

You may ask, why BCD numbers? BCD numbers are used by TOD clock registers because it’s easier to print them on screen using machine language. Numeric output of regular base 10 numbers is more difficult.

As a final note, the TOD clock uses an external frequency as a reference. That’s the frequency of alternate current (50 Hz in Europe). So, if you only have +5 V DC on a Commodore 64 C (a “true” C with a 250469 board), the computer may seem functional, but the TOD clock won’t work (sound and tape won’t be available as well).

References

↑1 Issue 43, December 1983 CIA #2 also has its own TOD clock. A nybble is half a byte, that is four bits.

## 4 Replies to “Commodore 64 clock using the CIA TOD clock (from “Compute!”)”

1. Repose says:

This is also missing from the c64 reloaded. Shame, as one of the programs I’ve always wanted to run was to time and graph the variation in the power cycle. It’s kept very accurate in order to reduce power loss from sharing of different generators on the grid, and is an accurate way to track time.
According to some graphs I’ve found, it will “reset” accumulated errors at regular intervals. It can also be used to verify your location, from the hum picked up on audio recordings…

2. I used this program for years, and instead of displaying the characters, I manipulated the data so the bytes would become sprite pointers. Then I fabricated 10 sprites in the shapes of zero through nine, and had them positioned on the screen in double-height and double-width fashion. This made the clock visible across the room, or across town using a high-powered telescope. The next adventure was to program alarm events that would trigger external events by writing a data byte to the user port, and converting the logic levels with optoisolators. You cannot imagine how satisfying it is to turn on a coffee pot at exactly 4:09:23 AM with a Commodore 64.

1. retro64 says:

That’s neat! 🙂

3. Daniel Düsentrieb says:

Funny thing that should be cleared, especially for the C64 (would be great to read an article about!):
Some people fool others and tell them there´s machine-speed difference (CPU, GPU) when a device is connected to or manufactured for 50 Hz or 60 Hz power-supply frequency. They even mix it up with PAL and NTSC (read Wikipedia on that!) which certainly is independent from power-supply frequency due to exact timing-precision needed.
You can find those strange videos of conmans at youtube, and you know why? It´s too strange that e.g. Sega Megadrive has IN FACT different CPU & Co-CPU frequencies!! They differ 1 % but they differ! Fools buy PAL-to-NTSC switches for the Megadrive and think they then can play games 13% faster! What a scam!

Concerning the C64, power-supply frequency (50 Hz, 60Hz) might DO make a difference when you use the time-of-day (TOD) clocks of the CIAs. Somehow they seem to use power-supply frequency for that instead of the quartz-driven machine-cycle-clock.

@Repose:
*) Nice idea to make the power-net frequency (50 Hz Europe, 60 Hz US) visible, but I think without an external reference (like DCF77) this won´t make too much sense. Certainly CPU/VIC is quartz driven, but NOT thermally stabilized. So this clock signal might vary as well as the power-supply frequency, and you can´t make any conclusions on what signal is constant or not.
*) Nice info about forensics you gave, but you just can tell 50 Hz from 60 Hz, that´s all. Power-Grids are very big today, so even comparison of exact waterfall-spectra won´t give you any hints for better localisation.

Insert math as
$${}$$