Page 1 of 1

Best way to implement keyboard buffer?

Posted: Wed Nov 18, 2015 11:39 pm
by rocketpenguin
I have just finished creating a proper keyboard driver for my pmode OS. However, right now I am using a rather kludgy way of putting keys into the buffer, this is the code for my keyboard interrupt handler. The handler is called from an assembly routine which calls this and then iret's:

Code: Select all

void
kbd_handler(
    void)
{
    uint8_t status;
    int8_t keycode;

    status = port_byte_in(KBD_STATUS_PORT);

    if (status & 0x01) {
        keycode = port_byte_in(KBD_DATA_PORT);
        if (keycode < 0) {
            return;
        }
        kbd_buffer = keymap[keycode];
    }

    port_byte_out(PIC_2_COMMAND, PIC_EOI);
    port_byte_out(PIC_1_COMMAND, PIC_EOI);
}
kbd_buffer is a global variable of type char. What is a better way to implement the buffer? Then, the system call for read() from stdin would be able to remove characters from buffer and shift them around.

Re: Best way to implement keyboard buffer?

Posted: Thu Nov 19, 2015 1:28 am
by iansjack
A circular buffer is one idea: https://en.wikipedia.org/wiki/Circular_buffer . Alternatively you could you a FIFO ( https://en.wikipedia.org/wiki/FIFO_(com ... ectronics) ). The former is quicker, as there is no dynamic memory allocation, but is limited to a maximum number of queued characters.