Administrator
|
As rleininger says, the Hack has no defined interrupt. This may sound unrealistic, but quite few of the simplest microcontrollers have no interrupts or only timer-based interrupts. The reason -- cost. If getting rid of the interrupt circuitry saves $0.001 per part and you are selling 10 million of the product a year, then that's an additional $10,000 to the bottom line every year.
Without interrupts, if your program wants to detect that a key has been pressed, it has to look at the keyboard register while the key is depressed. So our code has to run in a loop that periodically "polls" the keyboard register to see if there is anything other than zero there.
But it's not that simple. A single press of the key will likely result in the key being depressed for many, many clock cycles. Let's use some old fashioned numbers and say that we have a real fast typist doing 100 words per minute. By convention, this is then 500 characters per second, or 2 ms per keystroke. The key is down the majority of that time, but let's call it half of the time, or 1 ms. One a processor with a 4 MHz clock that takes four clock cycles to execute a single instruction, that's enough time to execute 1000 instructions.
If our loop is too slow, then there is the change that we will poll it and see nothing, and then before we poll it again the person will press and release a key and we will still see nothing the next time. This is bad.
So we make sure that we poll it many times in the span that it takes the fastest typing speed we are concerned with to press and release a key. But now we will see the same key code many times in a row. So our code has to detect when the code changes and only report a key press on the first instance of each change (this approach works pretty well, but it doesn't provide for "autorepeat" when you intentionally hold down the key -- but the code can take care of that case, too).
The code is quite a bit more complicated than it would have to be if you had an interrupt, but the code only has to be written once (and then it can be used in many, many products).
If you wanted to build a system up in hardware around the Hack, you have to make some decisions on how to do things that are beyond the Hack specification. Probably the easiest way to do this is to use dual-port memory. There are a few different styles, but in one case it is nothing more than a memory bank that has two address ports -- a read address and a write address. For the screen memory, the Hack only needs access to the write address port so that it can write data into the memory. The video system, on the other hand, only needs access to the read port since it just needs to know what is stored there. The reverse is the case for the keyboard buffer.
Another simple way to accomplish the same goal is to use normal memory but multiplex the input and drive it with a clock that it twice as fast as the Hack clock. One one clock cycle the Hack controls the memory pins and on the other clock cycle the video system does. Neither system is even aware of the presence of the other system and thinks that they are the sole owners of the memory.
|