Steven Gruel is a programmer/analyst with Restaurant Integrated Computer Controls (RICC) in Norcross, GA. Presently he is working on operations control and communications on an organization-wide scale. His computer interests include C programming and assembler optimizing. You may contact him at 5175 Wexford Lane, Norcross, GA 30071.
Have you ever needed to ungetch() more than one character at a time, see how many characters are in the buffer, or even to know what the characters are without removing them from the buffer? Fortunately, Turbo C makes this easy and painless, without using interrupts or assembler. By controlling the circular character buffer and the two pointers that regulate the incoming and outgoing character data, you can create any situation in the buffer you wish.
The buffer consists of 16 consecutive words of memory and two pointers. The first pointer is called the "Head", resides at 0x40:0x1A, and points to the next word in the buffer to be read by getch() or other similar functions. The second pointer is called the "Tail" and is positioned at 0x40:0x1C. It points to the location where the next keyboard interrupt will leave its data. When the buffer is empty, Head and Tail both point to the same place, making them equal. As keys are added to the buffer, the Tail is incremented by twos. Once Tail exceeds 60, it will restart at 30, giving the buffer its circular behavior. The Tail will continue moving in this circle until it has filled the word behind the Head. At this point the Tail will not be increased, and all subsequent keys sent by the keyboard interrupt will merely beep and be ignored until the Head is moved by a get-character function. When keyboard data is brought into the C program, the Head is decremented one word per key. When getch() is called the first time, it returns 0, moves the scan code to character byte, and does not change the Head until Head reaches the location one word short of the Tail, indicating that it's full.
poll_kb() checks the buffer for any characters that may be available and returns either the character or zero, the latter indicating no data available.
readch() uses poll_kb() in a loop to wait for a non-zero result. readch() returns the non-zero result much like getch() except that it shifts left any extended character's scan code in place of the ASCII code.
push_kb() puts a scan code and ASCII character code into the buffer and moves the Tail accordingly, just like the key_board interrupt does.
kb_count() simply returns the number of keystrokes stored in the buffer.
kb_peek() looks ahead the number of reads specified, without changing any data or positions.
kb_clear() sets Head and Tail to the same value, creating an empty buffer.
Note that poll_kb(), readch() and kb_peek() return all extended characters on their first call as the scan code (i.e., the extended part of the character code) logically shifted left eight bits. Any return value of 0 means there is no data in the buffer for this call.
You can use these functions in a variety of situations, such as teaching new users to use a software package. For example, you could store keyboard sequences in a file along with prompts for popup messages. Your interactive tutorial program must first disable the keyboard interrupt by storing the current interrupt address and replacing it with an empty function. Then you must tag onto one of the system clock interrupts to read the file data, check the keyboard buffer, insert key codes, and to call the message and time delay handler. These tasks could either be linked into the target program or coded as a TSR.
You should also consider using the keyboard buffer as an easy way to add macros to your existing code with a minimum of fuss. Another interesting application might be to turn control over to a remote location through a modem or network or to provide technical support with a more hands-on approach.