Best way to implement keyboard buffer?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
rocketpenguin
Posts: 1
Joined: Wed Oct 07, 2015 3:20 am

Best way to implement keyboard buffer?

Post 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.
User avatar
iansjack
Member
Member
Posts: 4706
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Best way to implement keyboard buffer?

Post 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.
Post Reply