Page 1 of 1

Keyboard driver problems

Posted: Sat Aug 15, 2009 3:06 am
by Andr3w
Hello,

I have some problems with my keyboard driver.

Code: Select all

keyboard:

	scancode	db	0
	push	rax
	xor	rax, rax
	in	al,60h
	cmp	al,1			; check for Esc key
	je	reboot
	include "keyboard.asm"
	
		add	rcx, 000001h		; rcx - address of video memory
		mov	[scancode], al
		mov bl, [kbd_t + scancode]
		mov	[rcx], bl			; moves asciicode to video memory
		add	rcx, 000001h
		mov	bl, 21h
		mov	[rcx], bl
		
	
	; finish of keyboard driver

	in	al,61h			; give finishing information
	out	61h,al			; to keyboard...
	mov	al,20h
	out	20h,al			; ...and interrupt controller
	pop	rax
	iretq
keyboard.asm:

Code: Select all

	key_f1				equ 0xC0
	key_f2				equ 0xC1
	key_f3				equ 0xC2
	key_f4				equ 0xC3
	key_f5				equ 0xC4
	key_f6				equ 0xC5
	key_f7				equ 0xC6
	key_f8				equ 0xC7
	key_f9				equ 0xC8
	key_f10				equ 0xC9
	key_f11				equ 0xCa
	key_f12				equ 0xCb

	key_up				equ 0xD0
	key_down			equ 0xD1
	key_left			equ 0xD2
	key_right			equ 0xD3
	key_home			equ 0xD4
	key_end				equ 0xD5
	key_pgup			equ 0xD6
	key_pgdn		equ 0xD7
	key_insert			equ 0xD8
	key_delete			equ 0xD9	

	kbd_t:		db 0, 0x1B, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', 0x08
			db 0x09, 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', 0x0D
			db 0, 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', 0x27, 0x60
			db 0, 0x5C, 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/', 0, '*', 0, ' ', 0
			db key_f1, key_f2, key_f3, key_f4, key_f5, key_f6, key_f7, key_f8, key_f9
			db key_f10, 0, 0, key_home, key_up, key_pgup, '-', key_left, 0
			db key_right, '+', key_end, key_down, key_pgdn, key_insert, key_delete
			db 0, 0, 0, key_f11, key_f12, 0, 0, 0, 0x0D, 0, 0, 0, 0, 0, 0, 0, 0
			db '/', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x7F
			db 0, 0, 0, 0, 0, 0
When I just print scancode, it works. :!:
Help me, please! :?: What's wrong with my keyboard driver?

P.S This is Long Mode 64-bit code. For FASM.

Re: Keyboard driver problems

Posted: Sat Aug 15, 2009 4:44 am
by Brendan
Hi,

Are you sure you want the CPU to execute the scancode as if it's an instruction?

Code: Select all

keyboard:

	scancode	db	0
	push	rax

Cheers,

Brendan

Re: Keyboard driver problems

Posted: Sat Aug 15, 2009 5:04 am
by mnovotny
qandrew wrote:

Code: Select all

keyboard:

	cmp	al,1			; check for Esc key
	je	reboot
	include "keyboard.asm"
keyboard.asm:

Code: Select all

	kbd_t:		db 0, 0x1B, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', 0x08
			db 0x09, 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', 0x0D
			db 0, 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', 0x27, 0x60
			db 0, 0x5C, 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/', 0, '*', 0, ' ', 0
			db key_f1, key_f2, key_f3, key_f4, key_f5, key_f6, key_f7, key_f8, key_f9
			db key_f10, 0, 0, key_home, key_up, key_pgup, '-', key_left, 0
			db key_right, '+', key_end, key_down, key_pgdn, key_insert, key_delete
			db 0, 0, 0, key_f11, key_f12, 0, 0, 0, 0x0D, 0, 0, 0, 0, 0, 0, 0, 0
			db '/', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x7F
			db 0, 0, 0, 0, 0, 0
Also, I'm not sure how FASM handles include, but won't this execute data, if al isn't one?

Re: Keyboard driver problems

Posted: Sat Aug 15, 2009 6:27 am
by Andr3w
@Brendan:
Thanks for it. I changed it, but no result.


EDIT:: I found out that I accidentally deleted

Code: Select all

 jmp main_loop 
from my main_loop code.

My keyboard IRQ is working well, 'cause when I press Esc, it reboots.

But other keys.. nothing happens.

:(

All my code without keyboard.asm and pmode + lmode init code and without IRQs init code

Code: Select all

        ; for clscr function
	mov	rbx, 0B7FFEh
	xor	rax, rax

	; for keyboard driver
	mov [video_mem], 0B827Fh
	; finish

	jmp	clscr
	
	; INCLUDES
	include 'keyboard.asm'
	video_mem	dq	0
	scancode	db 0

; Clear Screen
  clscr:
	add	rbx, 000002h
	mov	[rbx], rax
	cmp	rbx, 0B8F9Eh
	jnb	main_loop
	jmp	clscr

; Main loop

  main_loop:
	mov	rax,'O x y g '                          ; put some text -byte 1: text + byte 2: color (21h)
	mov	[0B8000h],rax
	mov	rax,'e n   O '
	mov	[0B8008h],rax
	mov	rax,'S   v e '
	mov	[0B8010h],rax
	mov	rax,'r s i o '
	mov	[0B8018h],rax
	mov	rax,'n   0 . '
	mov	[0B8020h],rax
	mov	rax,'0 . 1 0 '
	mov	[0B8028h],rax
	
	jmp	main_loop                    ; do it again! :P

exception:				; exception handler
	in	al, 61h			; turn on the speaker
	or	al, 3
	out	61h, al
	mov rax, 'E X C E '           ; print out that there's an exception..
	mov [0B8140h], rax
	mov rax, 'P T I O '
	mov [0B8148h], rax
	mov rax, 'N '
	mov [0B8150h], rax
	jmp	exception		; repeat it until reboot

interrupt:				; handler for all other interrupts
	iretq

clock:                                ; system... timer
	push	rax
	mov	al,20h
	out	20h,al
	pop	rax
	iretq

keyboard:                          ; keyboard IRQ handler

   push rax
   xor	rax, rax
   in	al, 60h
   cmp	al,1         ; check for Esc key
   je	reboot
   
      add   [video_mem], 000001h      ; rcx - address of video memory
      mov   [scancode], al
      mov bl, [kbd_t + scancode]
	  mov	rcx, video_mem
      mov   [rcx], bl         ; moves asciicode to video memory
      add   [video_mem], 000001h
      mov   bl, 21h
	  mov	rcx, video_mem
      mov   [rcx], bl
      
   
   ; finish of keyboard driver

   in   al,61h         ; give finishing information
   out   61h,al         ; to keyboard...
   mov   al,20h
   out   20h,al         ; ...and interrupt controller
   pop   rax
   iretq

reboot:
	mov	al,0FEh
	out	64h,al			; reboot computer
	jmp	reboot


Re: Keyboard driver problems

Posted: Sat Aug 15, 2009 7:07 am
by Brendan
Hi,

Some notes...

Your keyboard IRQ handler trashes RBX and RCX (it doesn't save and restore them, like it does with RAX), which will cause random crashes everywhere.

The line "mov bl, [kbd_t + scancode]" is probably entirely wrong, and you probably want to do something like "movzx eax, byte [scancode]" then "mov bl,[kbd_t + rax]".

The line "mov rcx, video_mem" is also probably wrong, and you probably want "mov rcx, [video_mem]".

These first 3 things give me the impression that you lack enough skill with assembly language programming to be writing an operating system in assembly language. I'm wondering if perhaps you're a high level language programmer (and should be writing an OS in that high level language); or if you're learning programming (and should learn programming first, and then write an OS after you've learned programming).

You shouldn't need the "in al,61h" and "out 61h,al" at the end of the keyboard IRQ handler (doing the "in al, 60h" is enough to tell the PS/2 controller that you've handled it's IRQ, and the "mov al,60h" and "out 60h, al" is enough to tell the PIC chip you've handled the PS/2 controller's IRQ).

For the keyboard, the PS/2 controller sends a stream of bytes to your IRQ handler. Some of these bytes are control bytes (e.g. "ACK"), some bytes are part of a sequence of one or more bytes that describe a make code (when you press a key), and some bytes are part of a sequence of one or more bytes that describe a break code (when you release a key). Your code assumes that every byte is a one byte make code.

Note: I only looked at the keyboard IRQ handler, and for all I know there may be lots of other bugs everywhere else...


Cheers,

Brendan

Re: Keyboard driver problems

Posted: Sat Aug 15, 2009 10:59 pm
by Andr3w
Hi,
Thank you. Looks like I've been sleeping while writing this code. I checked the code again, find some bugs and now all is OK! :)