KIM-1 Clock

Over on hackaday.io, [Arduino Enigma] posted the code for his clock that runs on a KIM Uno (the KIM-1 clone we mentioned late last year). Although the KIM Uno has a few demos preloaded (including Microchess and a scientific calculator), all of them take some interaction. The clock makes the KIM Uno a more dynamic desk display since it does something useful without any user interaction (once you set the clock, of course).

The project shows the code stored in ROM, but you can’t directly enter the program into ROM (which is really EEPROM on the host Arduino). The trick is to enter the address (that is press AD and then 0, 4, 0, 0) and then mash down the reset button for about a second. Then you can press DA and enter the hex codes provided (pressing + after each byte). Since the code is in nonvolatile storage, you can start it at any time by setting the time in RAM and executing the code at address 400.

The program is short and sweet, making it is easy to enter and a great opportunity to brush up on understanding your 6502. However, the simplicity means it doesn’t range check your initial time settings, so don’t tell it that it is 32:99 or something like that. The code isn’t commented, but it is pretty simple, once you realize one thing: the first instruction, SED, sets the 6502’s decimal mode so no conversion between hex and decimal is needed.

Each part of the time (hours, minutes, and seconds) is stored in RAM at locations 0024, 0025, and 0026. The NEXTD routine uses the X register to scan through each part of the time, adding 1 or 0 as appropriate (stored in RAM location 0029). If the time part matches the corresponding table entry at CARRYT (that is, 60, 60, or 24 also indexed by the X register), then the code does not take the branch to CONTIN and reloads the increment (location 0029) with zero, so the next digits will not advance. If it does match, the increment stays as 1 and the current part returns to a zero value.

You may notice the time is stored back to locations starting at 0024 and 00F9. That’s because 00F9 is where the KIM-1 ROM looks for data to write to the display when calling the ROM-resident subroutine at 1F1F. The X register ranges from 0 to 2, corresponding to the seconds, minutes, and hours. Once all the time is updated, the loop at L1 displays the time and delays for about one second.

A neat piece of coding and a great example of the power of the 6502’s decimal mode. This would look even better with [Scott’s] enhanced version of the KIM-1 UNO.

24 thoughts on “KIM-1 Clock

    1. Why would the cpu affect the acuracy of the clock? The clock is crystal controlled on the Kim-1 and the execution time of each op-code is constant, so a “software” clock could be as accurate as the programmer wants it to be.

      1. The Kim-1 has no real time clock, so it can only be as accurate as the the programmer is willing to work, but there is no external reference to ‘sync-up’ with…

        All you know (as the programmer) is the starting time, once you start cycling through times you have to know what instructions have executed, how many click cycles each uses, and then calculate the new time. If an interrupt occurs, or if someone pauses execution, you have no programmatic way to know how long the interrupt was, so all time is lost.

        Imagine a ship attempting to navigate across the Atlantic, but you can’t take a compass with you in the ship, all you know is travel time & speed of travel to calculate position.

        1. Yes, but if all you are making is a clock, you do/should know what instructions have executed and the Kim-1 IS the real-time clock. You also have control over the interrupts.

          The “real time clock” you mention is no more than a similar processor programmed to do nothing but keep track of the time. It could even be a Kim-1.

          So we could have a Kim-1 being a real-time clock for another Kim-1 that is doing something else from time to time :)

          1. The issue is the nearly millions of rounding errors inherent in such a software-only clock. Your trivialization of the task leads me to believe you either never worked on a computer without an internal clock or never programmed computers at the machine code level. And as a reminder, this is not an ACTUAL KIM-1, it is emulated on top of another processor, an arduino.

            Ideally you’d have an RTC chip on the board and you’d set it to interrupt the CPU once a second (minus the time needed to update the displayed time).

            Imagine trying to keep time by counting (extremely accurate) “mississippi’s”, no matter how accurate your pace of counting “1 Mississippi, 2 Mississippi, etc.” you’d slowly drift off and the Kim-1 has no ability to sync-up with an outside time source.

          2. On a real KIM it would fairly straightforward to get this right. As you can see, the program listing is just a few dozen lines, and only contains a couple of loops. It’s just a matter of looking up the cycle count, and make sure they add up to the right amount. There are no “millions of rounding errors”.

          3. Right…. There’s a magic number you can plug into an assembler loop and it will NEVER take any more or less time to execute, the CPU clock will be accurate ‘forever’, no drift.

            You forgot the two magic words in your post – “in theory”.

            >

          4. Returning to your original statement: 6809 software clocks will have a drift of +- 2 to 3 seconds per hour. (corrected to 6502).

            We are not talking about clocks on emulated machines. We are talking about using a 6502 or 6809 as the processor for an accurate real time clock. I maintain it is not difficult.

            Your trivialization of the task leads me to believe you either never worked on a computer without an internal clock or never programmed computers at the machine code level.

            ROFL.

            BTW, what do you think is in a “real time clock”? Probably a processor of about the same power as a 6502.

    2. I had to chuckle at all the responses trivializing the requirements of clock accuracy. That was pretty entertaining. I’d suggest they write a clock in ASM on a 6502 or other 8-bit system using real hardware or even the Kim-uno. Then try to figure out why it drifts and then try to figure out why the drift varies. I don’t think they have an understanding of how difficult accurate timing really is.

      1. I programmed many clocks and timing dependent programs on 6502s and other 8 bit processors (in the 1970s). That is why I questioned the OP who declared that a 6502 was inherently inaccurate to the tune of seconds per hour.

        The 6502 will replicate the loop instructions exactly the same every time. So the accuracy of the clock is only dependent on the crystal. In the Kim-1, a clock program would be as accurate as a similar quartz crystal controlled wristwatch or wall clock of 1976 vintage, assuming the programmer counted the cycles properly, and/or adjusted the timing by observation over a long period.

        For better accuracy, the 1 MHz crystal could be temperature controlled. In 1976, doing that would have made a highly accurate clock for the time.

        As for an emulated Kim like the uno, that would be a different story and depends on the emulation, the underlying processor, interrupts, etc. Not recommended. Similarly, it would be more difficult to get the timing right on a modern multi-core CPU with far less predictability.

        1. I too wrote ASM in the late 70’s & 80’s, mainly with CDP1802 chips but I also had access to an 6800 trainer that I wrote a clock on. Everything in the circuit is subject to timing drift and your crystal can’t be counted on for accuracy either; keeping your clock (crystal) to a constant temperature is also a bit non-trivial.

          I found that regardless of how I tweaked my code, the clock would not keep time accurately and you will always get drift when compared to a real timing standard. The best way I could think of back in the day was to use the 60 cycle wave from the A/C as a timing reference but I never actually adopted that in practice. I just accepted that the time would not be 100% accurate.

      2. I liked how commenters are convinced that theory = reality, and that by carefully counting clock cycles per machine instruction, a nearly perfect time piece was a trivial task…

        Reality is messy, and the clock in a computer doesn’t have to be all that accurate, it just, like a bass drum, provides the beat that keeps the various components in sync. Early computers, like the original KIM-1, frequently had single step switches, where the clock is bypassed, and each clock cycle is replaced with a series of toggling a single step switch.

        >

        1. Yup, there’s a big difference between what should happen in theory and what actually does happen and I think what’s also forgotten is that there’s a difference in the degree of accuracy of time keeping of the 70’s and today. All of the texts I read about time keeping back in the day said that you could calculate time with a loop, taking into consideration clock speed & cycles per instruction but they never claimed to produce accurate time keeping. In practice it would work but it was a bit wonky. You might get decent accuracy with hardware like this by having one 6502 as the dedicated RTC and the other as the clock UI.

  1. Wow, I’m conflicted. The first computer I owned was a SYM-1, and I still have my original ‘Book of Kim’ book, but the creator had to inject #IStandWithAhmed into an otherwise great hack…

    Well, OK – here’s who you are standing with:

    http://www.dallasnews.com/news/community-news/northwest-dallas-county/headlines/20150926-before-ahmeds-fame-fantastic-inventions-and-a-fight-with-authority.ece

    A clever, troublesome child with a history of – wait for it – bringing inventions to school with the intention of disrupting class…

    As a character in a movie once said ‘…and that’s all I’m gonna say about that’.

    1. After reading that article I fail to see your point.

      I’m now more convinced that Ahmed is an intelligent and rational kid who is underwhelmed by his public school education and is seeking acceptance from his peers by using his strengths despite constant bullying. Surely most HaD readers identify with him.

    2. Ken, I’m coming down on chango’s side as well. He’s a bright, socially inept 14-year-old kid, who likes to play with techie stuff and doesn’t react well to mindless authority. Like many of us geeks.

      I wish Ahmed well.

      1. @Antron @Chango…
        Socially inept or not, intelligent or not, he’s living in a world with laws and preconceptions. Aside from the legality issue, I can’t show a gun in a courtroom claiming my 1st and 2nd amendment rights without making other people preconceive that I am being unreasonable.
        If I want to do it, I need to follow protocol… Ahmed did not follow protocol.
        Protocol is essential to hacking anything MCU related, who can argue?

        …Ahmed made a point, a political one. I too wish him well. My concerns start if he continues to disregard preconceptions that people have and their reactions as well.

    3. +1

      …I haste to think that perhaps one contributer of HaD (Mr. A. Fabio) had 1 of 2 problems with his post.
      1. Clickbait… He actually researched like a real journalist and conned/social-hacked several of the readers.
      …Net gain was the hottest comment thread of the week, at the expense of his ethics and, therefore, character.
      2. tl;dr… He went off half cocked on preconceived notions of race and/or religious discrimination.
      …Net gain was that he needs to research, at the expense of his objectivity and, therefore, character.

      Good journalism is always good; Bad journalists sometimes have good streaks. Often bad journalists do bad journalism.
      I guess time will tell how he does. Hopefully not at the expense of one my favorite sites.

Leave a Reply

Please be kind and respectful to help make the comments section excellent. (Comment Policy)

This site uses Akismet to reduce spam. Learn how your comment data is processed.