Help with keyboard driver

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.
Locked
User avatar
Andr3w
Member
Member
Posts: 76
Joined: Tue Jun 09, 2009 4:09 am
Location: Somewhere

Help with keyboard driver

Post by Andr3w »

Hello,

does somebody have an example of simple PMode/LMode keyboard driver?

Thanks! :)
User avatar
yemista
Member
Member
Posts: 299
Joined: Fri Dec 26, 2008 12:31 pm
Location: Boston
Contact:

Re: Help with keyboard driver

Post by yemista »

Code: Select all

u8 lowercase[256] = { 0,  27, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 
   '-', '=', '\b', '\t', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[',
   ']', '\n', 0, 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', '\'', '`', 
    0,  '\\', 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/', 0, '*', 0, ' ',
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '-', 0, 0, 0, '+', 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0
};

u8 uppercase[256] = { 0,  27, '!', '@', '#', '$', '%', '^', '&', '*', '(', ')',
   '_', '+', '\b', '\t', 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{',
   '}', '\n', 0, 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', '"', '"', 0,
   '|', 'Z', 'X', 'C', 'V', 'B', 'N', 'M', '<', '>', '?', 0, '*', 0, ' ', 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '-', 0, 0, 0, '+', 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0
};

static u8 flags;
static u8* kbd_buf;
static int buf_pos, max_read;

void init_kbd() {
  register_isr(KBD_IRQ, kbd_isr);
  // init_queue(&kbd_data, &kbd_buf[0], 10);
  flags = 0;
  flags |= KBD_ECHO;
  devtab[DEV_KBD].init = init_kbd;
  devtab[DEV_KBD].read = kbd_read;
  enable_irq(KEYBOARD);
}

int kbd_read(u8* buf, int count) {
  cli();
  kbd_buf = buf;
  buf_pos = 0;
  max_read = count;
  flags |= KBD_READ;
  sti();

  while(buf_pos != count)
    ;

  return count;
  
}
  
void kbd_isr() { 
  u8 ch = inb(0x60);
  u8 ascii_char;
  //monitor_write_hex((u32)ch);

  if(ch == 0xE0)
    ch = inb(0x60);
 
  // check shift state
  if(ch == KB_LSHIFT || ch == KB_RSHIFT) {
      flags |= KBD_SHIFT;
      return;
  }
    
  else if(ch == (KB_LSHIFT + KEY_RELEASE_N) ||
          ch == (KB_RSHIFT + KEY_RELEASE_N)) {
      flags &= ~KBD_SHIFT;
      return;
  }

  /* break codes a regular codes + 0x80. If this
     is a break code, just ignore */
  else if((ch - KEY_RELEASE_N) > 0)
    return;

  // if caps lock press of release, invert switch state
  else if(ch ==  KB_CAPSLOCK) {

      if(flags & KBD_SHIFT)
        flags &= ~KBD_SHIFT;
      else
        flags |= KBD_SHIFT;

      return;
  }

  else if(ch == KB_DEL) {
    monitor_put(' ');
    update_cursor(-1, 0);
    return;
  }
  
  else if(ch == KB_DOWN) {
    update_cursor(0, -1);
    return;
  }

  else if(ch == KB_UP) {
    update_cursor(0, 1);
    return;
  }

  else if(ch == KB_LEFT) {
    update_cursor(-1, 0);
    return;
  }

  else if(ch == KB_RIGHT) {
    update_cursor(1, 0);
    return;
  }

  else {
    ascii_char = ((flags & KBD_SHIFT) ? uppercase[ch] : lowercase[ch]);

    // if someone is reading
    if(flags & KBD_READ) {
      if(ascii_char >= 0x20 && ascii_char <= 0x7E) {
        *(kbd_buf + buf_pos) = ascii_char;
        buf_pos++;

        if(buf_pos == max_read)
          flags &= ~KBD_READ;
      }  
    }

    if(flags & KBD_ECHO) 
      putc(ascii_char);
      
    return;
  }
}
User avatar
Andr3w
Member
Member
Posts: 76
Joined: Tue Jun 09, 2009 4:09 am
Location: Somewhere

Re: Help with keyboard driver

Post by Andr3w »

I'm sorry..

I forgot to add something...

Does anybody have an example written on FASM/NASM?
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Re: Help with keyboard driver

Post by JamesM »

Hi,

Here begins the crackdown on useless, ill-thought out posts.

Yes, most of us here have an example of a keyboard driver. No, we would not like to give it to you.

You learn best when you don't copy-paste code. We have a wiki which contains the information you need in meticulous detail. Read it.

Thanks,

James
Locked