Whats wrong with this 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
GeniusCobyWalker
Member
Member
Posts: 65
Joined: Mon Jan 12, 2009 4:17 pm

Whats wrong with this Keyboard driver?

Post by GeniusCobyWalker »

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: Select all

//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: Select all

//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: Select all

//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++;
			}
	}
}
Last edited by GeniusCobyWalker on Mon Jun 15, 2009 7:07 pm, edited 1 time in total.
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
geppyfx
Member
Member
Posts: 87
Joined: Tue Apr 28, 2009 4:58 pm

Re: Whats wrong with this Keyboard driver?

Post by geppyfx »

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

Re: Whats wrong with this Keyboard driver?

Post by kop99 »

"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.
JohnnyTheDon
Member
Member
Posts: 524
Joined: Sun Nov 09, 2008 2:55 am
Location: Pennsylvania, USA

Re: Whats wrong with this Keyboard driver?

Post by JohnnyTheDon »

It could be a ton of things. You haven't shown us the code where you setup your IDT, remap the PIC, etc.
geppyfx
Member
Member
Posts: 87
Joined: Tue Apr 28, 2009 4:58 pm

Re: Whats wrong with this Keyboard driver?

Post by geppyfx »

kop99 wrote:
"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
User avatar
kop99
Member
Member
Posts: 120
Joined: Fri May 15, 2009 2:58 am

Re: Whats wrong with this Keyboard driver?

Post by kop99 »

Code: Select all

if (scancode & 0x80)
geppyfx, you mean that code?
Is that code checking key press status?
What wrong with that code?
geppyfx
Member
Member
Posts: 87
Joined: Tue Apr 28, 2009 4:58 pm

Re: Whats wrong with this Keyboard driver?

Post by geppyfx »

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.
User avatar
salil_bhagurkar
Member
Member
Posts: 261
Joined: Mon Feb 19, 2007 10:40 am
Location: India

Re: Whats wrong with this Keyboard driver?

Post by salil_bhagurkar »

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.
jal
Member
Member
Posts: 1385
Joined: Wed Oct 31, 2007 9:09 am

Re: Whats wrong with this Keyboard driver?

Post by jal »

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
User avatar
GeniusCobyWalker
Member
Member
Posts: 65
Joined: Mon Jan 12, 2009 4:17 pm

Re: Whats wrong with this Keyboard driver?

Post by GeniusCobyWalker »

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
User avatar
kop99
Member
Member
Posts: 120
Joined: Fri May 15, 2009 2:58 am

Re: Whats wrong with this Keyboard driver?

Post by kop99 »

GeniusCobyWalker, your k_text() funcion is output string funcion, not character output function.

So, remark that part.

Code: Select all

   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: Select all

   else{
      char temp[2];
      k_text(dec(scancode));
      temp[0] = keycodes[scancode];
      temp[1] = '\0';
      k_text(keycodes[scancode]);
   }
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Whats wrong with this Keyboard driver?

Post by Combuster »

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 ]
Locked