OSDev.org

The Place to Start for Operating System Developers
It is currently Sun Apr 28, 2024 3:10 pm

All times are UTC - 6 hours




Post new topic This topic is locked, you cannot edit posts or make further replies.  [ 12 posts ] 
Author Message
 Post subject: Whats wrong with this Keyboard driver?
PostPosted: Sun Jun 14, 2009 6:52 pm 
Offline
Member
Member
User avatar

Joined: Mon Jan 12, 2009 4:17 pm
Posts: 65
EDIT: MORE SOURCE BELOW

Here is a keyboard driver I'm using.

This code displays the correct scan-codes.
However displays the wrong characters.
It usually just prints "S"

If anyone can figure out whats wrong please post.

Thanks ahead of time

Code:
//WalkerOS Keyboard Driver
#include "keyboard.h"
#include "isr.h"
#include "monitor.h"

char keycodes[128] =
{
    0,  27, '1', '2', '3', '4', '5', '6', '7', '8',   /* 9 */
  '9', '0', '-', '=', '\b',   /* Backspace */
  '\t',         /* Tab */
  'q', 'w', 'e', 'r',   /* 19 */
  't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\n',      /* Enter key */
    0,         /* 29   - Control */
  'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';',   /* 39 */
'\'', '`',   0,      /* Left shift */
'\\', 'z', 'x', 'c', 'v', 'b', 'n',         /* 49 */
  'm', ',', '.', '/',   0,               /* Right shift */
  '*',
    0,   /* Alt */
  ' ',   /* Space bar */
    0,   /* Caps lock */
    0,   /* 59 - F1 key ... > */
    0,   0,   0,   0,   0,   0,   0,   0,
    0,   /* < ... F10 */
    0,   /* 69 - Num lock*/
    0,   /* Scroll Lock */
    0,   /* Home key */
    0,   /* Up Arrow */
    0,   /* Page Up */
  '-',
    0,   /* Left Arrow */
    0,
    0,   /* Right Arrow */
  '+',
    0,   /* 79 - End key*/
    0,   /* Down Arrow */
    0,   /* Page Down */
    0,   /* Insert Key */
    0,   /* Delete Key */
    0,   0,   0,
    0,   /* F11 Key */
    0,   /* F12 Key */
    0,   /* All other keys are undefined */
};

//Keyboard Handler (Interrupt)
void keyboard_handler(/*struct regs *r*/registers_t regs){
   unsigned char scancode;
   scancode = inb(0x60);            //Read from KB-buffer
   if (scancode & 0x80){            //1=Released 0=Pressed

      }
   else{
      k_text(dec(scancode));
      k_text(keycodes[scancode]);
      }
}

void init_keyboard(){
    register_interrupt_handler(IRQ1,keyboard_handler);
}


Code:
//Print Text (Decimal)
unsigned int dec(u32int n)
{
   if (n == 0){
      return "0";
      return;
      }
   s32int acc = n;
   char c[32];
   int i = 0;
   while (acc > 0){
      c[i] = '0' + acc%10;
      acc /= 10;
      i++;
      }
   c[i] = 0;
   char c2[32];
   c2[i--] = 0;
   int j = 0;
   while(i >= 0){
      c2[i--] = c[j++];
      }
   return c2;
}


Code:
//Print Text
void k_text(char *message){
   unsigned int i=0;
   i=csr_x*2+(csr_y*80*2);
   while(*message!=0){
      if(*message=='\n'){         // check for a new line
         csr_y++;
         csr_x=0;
         i=csr_x*2+(csr_y*80*2);
         *message++;
         k_scroll();
         k_cursor();
         }
      else{
         vidmem[i]=*message;
         *message++;
         i++;
         vidmem[i]=font;
         i++;
         csr_x++;
         }
   }
}

_________________
Full Knowledge in:
GML, Ti-Basic, Zilog Z80 Assembly, SX28 Assembly and Blender
Experience in:
C++,OpenGl,NDS C++,Dark Basic,Dark Basic Pro,Dark Gdk and PSP Coding
Using:
Ubuntu ,GEdit ,NASM ,GCC ,LD ,Bochs


Last edited by GeniusCobyWalker on Mon Jun 15, 2009 7:07 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: Whats wrong with this Keyboard driver?
PostPosted: Sun Jun 14, 2009 7:17 pm 
Offline
Member
Member

Joined: Tue Apr 28, 2009 4:58 pm
Posts: 87
"scancode & 0x80" is not the same same as "scancode &= 0x80". Highest bit still set.


Top
 Profile  
 
 Post subject: Re: Whats wrong with this Keyboard driver?
PostPosted: Sun Jun 14, 2009 7:42 pm 
Offline
Member
Member
User avatar

Joined: Fri May 15, 2009 2:58 am
Posts: 120
Quote:
"scancode & 0x80" is not the same same as "scancode &= 0x80". Highest bit still set.

What relation is there between your post and GeniusCobyWalker's question?

GeniusCobyWalker, it seems like have no mistake. you can post some other related part.


Top
 Profile  
 
 Post subject: Re: Whats wrong with this Keyboard driver?
PostPosted: Sun Jun 14, 2009 7:55 pm 
Offline
Member
Member

Joined: Sun Nov 09, 2008 2:55 am
Posts: 524
Location: Pennsylvania, USA
It could be a ton of things. You haven't shown us the code where you setup your IDT, remap the PIC, etc.


Top
 Profile  
 
 Post subject: Re: Whats wrong with this Keyboard driver?
PostPosted: Sun Jun 14, 2009 8:05 pm 
Offline
Member
Member

Joined: Tue Apr 28, 2009 4:58 pm
Posts: 87
kop99 wrote:
Quote:
"scancode & 0x80" is not the same same as "scancode &= 0x80". Highest bit still set.

What relation is there between your post and GeniusCobyWalker's question?
.

128 byte array, but I guess I was a bit wrong &= will not clear the bit but &= 0x7f will


Top
 Profile  
 
 Post subject: Re: Whats wrong with this Keyboard driver?
PostPosted: Sun Jun 14, 2009 9:19 pm 
Offline
Member
Member
User avatar

Joined: Fri May 15, 2009 2:58 am
Posts: 120
Code:
if (scancode & 0x80)

geppyfx, you mean that code?
Is that code checking key press status?
What wrong with that code?


Top
 Profile  
 
 Post subject: Re: Whats wrong with this Keyboard driver?
PostPosted: Sun Jun 14, 2009 9:51 pm 
Offline
Member
Member

Joined: Tue Apr 28, 2009 4:58 pm
Posts: 87
don't pay attention to my comment. Doesn't seem to be anything wrong with entire code. That was just guess that value passed to array will cause to access memory outside array boundaries if (scancode & 80h) is true. The code that causes problem he didn't post. Will never try to guess again.


Top
 Profile  
 
 Post subject: Re: Whats wrong with this Keyboard driver?
PostPosted: Sun Jun 14, 2009 11:25 pm 
Offline
Member
Member
User avatar

Joined: Mon Feb 19, 2007 10:40 am
Posts: 261
Location: India
Assuming your k_text function prints out a string, does your dec() function that prints a decimal, return a string pointer correctly? Besides, keycodes[scancode] returns a character, and not a string. So passing it to k_text should print the character but print garbage after that, as its not null-terminated.

I hope this helps.


Top
 Profile  
 
 Post subject: Re: Whats wrong with this Keyboard driver?
PostPosted: Mon Jun 15, 2009 6:45 am 
Offline
Member
Member

Joined: Wed Oct 31, 2007 9:09 am
Posts: 1385
salil_bhagurkar wrote:
Besides, keycodes[scancode] returns a character, and not a string. So passing it to k_text should print the character but print garbage after that, as its not null-terminated.


No no no. k_text probably wants the address to a string to print, but its input is a character, not a pointer to a character. @OP: did you check the warnings? The compiler no doubt warned you for this!


JAL


Top
 Profile  
 
 Post subject: Re: Whats wrong with this Keyboard driver?
PostPosted: Mon Jun 15, 2009 7:05 pm 
Offline
Member
Member
User avatar

Joined: Mon Jan 12, 2009 4:17 pm
Posts: 65
You mean this? : src/keyboard.c:55: warning: passing argument 1 of ‘k_text’ makes pointer from integer without a cast
(Thats the only type of warning)

Also added the "k_text()" and "dec(u32int n)" source, since everyones talking about them.

_________________
Full Knowledge in:
GML, Ti-Basic, Zilog Z80 Assembly, SX28 Assembly and Blender
Experience in:
C++,OpenGl,NDS C++,Dark Basic,Dark Basic Pro,Dark Gdk and PSP Coding
Using:
Ubuntu ,GEdit ,NASM ,GCC ,LD ,Bochs


Top
 Profile  
 
 Post subject: Re: Whats wrong with this Keyboard driver?
PostPosted: Tue Jun 16, 2009 1:58 am 
Offline
Member
Member
User avatar

Joined: Fri May 15, 2009 2:58 am
Posts: 120
GeniusCobyWalker, your k_text() funcion is output string funcion, not character output function.

So, remark that part.
Code:
   else{
      k_text(dec(scancode));
      k_text(keycodes[scancode]);
      }

You have to call k_text()function with string pointer parameter.
But, keycodes[scancode] is not string pointer, and it is char type.

so, try following code.
Code:
   else{
      char temp[2];
      k_text(dec(scancode));
      temp[0] = keycodes[scancode];
      temp[1] = '\0';
      k_text(keycodes[scancode]);
   }


Top
 Profile  
 
 Post subject: Re: Whats wrong with this Keyboard driver?
PostPosted: Tue Jun 16, 2009 2:11 am 
Offline
Member
Member
User avatar

Joined: Wed Oct 18, 2006 3:45 am
Posts: 9301
Location: On the balcony, where I can actually keep 1½m distance
Oh man. Can you even program C? :shock:

Passing an integer as a pointer, passing a pointer as an integer, dangling pointers, likely stack trashing, ignoring warnings, and BEING UNABLE TO READ GCCs INSULTS AND FIX THAT CODE ON YOUR OWN.

After the previous incidents, you have proven that you do not belong here at all.
Escorts OP out of the building

@kop: k_text(keycodes[scancode]); -> k_text(temp); :wink:

_________________
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic This topic is locked, you cannot edit posts or make further replies.  [ 12 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot], MichaelPetch and 35 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group