not unique keyboard scancodes

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.
zgintasz
Posts: 18
Joined: Sat Nov 03, 2012 11:14 am

Re: not unique keyboard scancodes

Post by zgintasz »

egos wrote:Do you use Multiboot header in your executable module? If so, check for its location within the first 8 Kbytes.
I think so, here is the code:

build.sh(compiling using cygwin):

Code: Select all

#!/bin/sh

build=temp_build
compiler=i586-elf-gcc
linker=i586-elf-ld
opt="-Wall -O -fstrength-reduce -fomit-frame-pointer -nostdinc -fno-builtin -I./kernel/include -c -fno-strict-aliasing -fno-common -fno-stack-protector"

echo "Building..."
echo "============================================="

echo "--Building C files..."
$compiler $opt -o $build/main.o ./kernel/main.c
$compiler $opt -o $build/console.o ./kernel/drivers/console.c
$compiler $opt -o $build/gdt.o ./kernel/gdt.c
$compiler $opt -o $build/idt.o ./kernel/idt.c
$compiler $opt -o $build/isrs.o ./kernel/isrs.c
$compiler $opt -o $build/irq.o ./kernel/irq.c
$compiler $opt -o $build/timer.o ./kernel/timer.c
$compiler $opt -o $build/keyboard.o ./kernel/drivers/keyboard.c

echo "--Building ASM files..."
nasm -f elf ./kernel/loader.asm -o $build/loader.o

echo "--Linking files..."
$linker -T linker.ld $build/*.o
echo "--Cleaning up temporary files..."
rm $build/*.o

echo "--Putting on floppy image..."
mount A: /mnt/floppy
cp kernel.bin /mnt/floppy/

echo "============================================="
echo "Build completed!"
linker.ld:

Code: Select all

ENTRY (loader)
OUTPUT ("kernel.bin")

addr = 0x100000;

SECTIONS
{
	.text addr :
	ALIGN(0x1000)
	{
		*(.text*);
		*(.rodata*);
	}

	.data :
	ALIGN(0x1000)
	{
		*(.data*);
	}

	.bss :
	ALIGN(0x1000)
	{
		*(.bss*);
	}
}
loader.asm:

Code: Select all

bits 32

global loader
global magic



extern kmain

; Multiboot header
MODULEALIGN equ 1<<0
MEMINFO     equ 1<<1
FLAGS       equ MODULEALIGN | MEMINFO
MAGIC       equ 0x1BADB002
CHECKSUM    equ -(MAGIC + FLAGS)

MultibootHeader:
	dd MAGIC
	dd FLAGS
	dd CHECKSUM

STACKSIZE   equ 0x4000     ; 16KB

loader:
	cmp eax, 0x2BADB002    ; verify booted with grub
	jne .bad

	mov esp, STACKSIZE + stack
	mov ax, 0x10
	mov ds, ax
	mov es, ax
	mov fs, ax
	mov gs, ax

	push ebx
	call kmain

	;cli				; clear interrupts
	;mov	eax, cr0		; set bit 0 in cr0--enter pmode
	;or	eax, 1
	;mov	cr0, eax

	;cli
	;hlt

.bad:
	cli
	hlt

align 4
stack:
	TIMES STACKSIZE db 0
...
main.c(there is "void* MultibootHeader", but I don't use it anywhere, I can change it to anything):

Code: Select all

...
void kmain(void* MultibootHeader)
{
	...
	for(;;);
}
How can I fix it(I don't understand your solution)? I'm really newbie in os development, I don't know very much, but I really want to create my own OS(I've got some free time, I'm ready to learn. The only thing I learnt fully is making console driver -.-).
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: not unique keyboard scancodes

Post by Combuster »

Which section in your linker script holds the multiboot header? :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 ]
zgintasz
Posts: 18
Joined: Sat Nov 03, 2012 11:14 am

Re: not unique keyboard scancodes

Post by zgintasz »

I fixed the problem with that grub message, thanks :).
Now I'm back in keyboard... I worked for it all day, can't find the solution. If I set keyboard scancode or don't set it, scancodes are the same. Just to test function:

Code: Select all

void KeyboardWaitOutport()
{
	int fail_safe=200000;
	while ((inportb(0x64)&2)!=0 && fail_safe>0) fail_safe--;
}
void KeyboardSetScancodeSet(uint8 set)
{
    if (set>3 || set <= 0) return;

    KeyboardWaitOutport();
    outportb (0x60, 0xf0);

    KeyboardWaitOutport();
    outportb (0x60, set);

}
this is how I print scancode(no if sentences):

Code: Select all

cprintf("%i", scancode);
I'm sure it's look like very easy for experts, but for I think newbie it's not :D.
egos
Member
Member
Posts: 612
Joined: Fri Nov 16, 2007 1:59 pm

Re: not unique keyboard scancodes

Post by egos »

Disable translation.
If you have seen bad English in my words, tell me what's wrong, please.
User avatar
SparrowOS
Member
Member
Posts: 72
Joined: Wed Nov 14, 2012 5:22 pm
Location: Vegas
Contact:

Re: not unique keyboard scancodes

Post by SparrowOS »

Your time-out delay is hoeky, but works for reasons you probably don't understand. IN/OUT instructions move at ISA bus speed which is roughly 1uS.

This has my keyboard routine. It only supports US ENGLISH keyboards. I don't care.
http://www.sparrowos.com/Wb/OSMain/KbdM ... board.html

This has the rest of my code on-line(in case you want to see mouse):
http://www.sparrowos.com/Wb/Accts/TS/Wb ... .html#l700

This has my ASU transcripts (someone else was wondering):
http://www.sparrowos.com/files/ASU_Transcripts.pdf
zgintasz
Posts: 18
Joined: Sat Nov 03, 2012 11:14 am

Re: not unique keyboard scancodes

Post by zgintasz »

egos wrote:Disable translation.
How to disable it? I need to somehow set byte 6 to 0 by writing to 0x20 or 0x60, right? How(code please)?

EDIT:
is it correct?

Code: Select all

    KeyboardWaitOutport();
    outportb (0x60, 6);

    KeyboardWaitOutport();
    outportb (0x60, 0);
because scancodes are still the same as without it :(.
egos
Member
Member
Posts: 612
Joined: Fri Nov 16, 2007 1:59 pm

Re: not unique keyboard scancodes

Post by egos »

zgintasz wrote:is it correct?
No, of course. Try something like this:

Code: Select all

    KeyboardWaitOutport();
    outportb (0x64, 0x60);

    KeyboardWaitOutport();
    outportb (0x60, CONTROLLER_COMMAND_BYTE);
If you are not sure what value should be in every bit, read the "Controller Command byte" first (use controller command 20h for that).
If you have seen bad English in my words, tell me what's wrong, please.
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: not unique keyboard scancodes

Post by Brendan »

Hi,

From this part of the wiki:

"Note: Bits listed in the table above as "unknown" should be treated as either "chipset specific" or "unknown". Bits that are marked as "only if 2 PS/2 ports supported" should also be treated as either "chipset specific" or "unknown" if the controller only supports one PS/2 port."

What this means is that you should read the old value, then clear bit 6, then write the new value. For example:

Code: Select all

    KeyboardWaitOutport();
    outportb (0x64, 0x20);                // Send "read configuration byte" command
    KeyboardWaitInport();
    foo = inportb(0x60);                  // foo = old value of configuration byte

    foo = foo & ~(1 << 6);                // foo = old value with translation disabled

    KeyboardWaitOutport();
    outportb (0x64, 0x60);                // Send "write configuration byte" command
    KeyboardWaitOutport();
    outportb (0x60, foo);                 // Set configuration byte to disable translation

Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
Antti
Member
Member
Posts: 923
Joined: Thu Jul 05, 2012 5:12 am
Location: Finland

Re: not unique keyboard scancodes

Post by Antti »

I disable the keyboard translation similar way. However, I have added some "extra" delay. When I write the configuration byte, I do not immediately read the status register. There is a small nop loop between the port I/Os. Same applies to other similar cases.

Brendan, is this totally pointless (or even harmful)? I know that this is not based on facts (that I know of) but I feel comfortable to be dilatory when doing port I/Os.
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: not unique keyboard scancodes

Post by Brendan »

Hi,
Antti wrote:I disable the keyboard translation similar way. However, I have added some "extra" delay. When I write the configuration byte, I do not immediately read the status register. There is a small nop loop between the port I/Os. Same applies to other similar cases.

Brendan, is this totally pointless (or even harmful)? I know that this is not based on facts (that I know of) but I feel comfortable to be dilatory when doing port I/Os.
First; if a delay was necessary, then NOPs (which may be simply "skipped" by the CPU without causing any delay at all) wouldn't be adequate. However; I doubt that any delay is necessary, as long as you're waiting for the relevant bits in the status register correctly. Small delays (e.g. less than 1 millisecond) should be harmless though.

A much larger problem is attempting to do this while a device (e.g. the keyboard and/or mouse) is still active; because there's no way to avoid race conditions. For example, you might send the "read configuration byte" command and the user might press a key at exactly the wrong time, and when you think you're reading the configuration byte you're actually reading the keyboard's scan code. To avoid this problem (and others like it), I'd recommend complete PS/2 controller initialisation (e.g. like this) where the device/s are disabled (step 3) before you do much of anything (e.g. step 5). :)


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
zgintasz
Posts: 18
Joined: Sat Nov 03, 2012 11:14 am

Re: not unique keyboard scancodes

Post by zgintasz »

Strange, still doesn't work: (by the way, using Virtual Box)

Code: Select all

void keyboard_install()
{
	irq_install_handler(1, keyboard_handler);

	KeyboardWaitOutport();
	outportb(0x60, KeybCmdReset);	   // Reset kb


	KeyboardScancodeSet(2);
	KeyboardWaitOutport();
	outportb (0x64, 0x20);                // Send "read configuration byte" command
	KeyboardWaitInport();
	foo = inportb(0x60);                  // foo = old value of configuration byte

	foo = foo & ~(1 << 6);                // foo = old value with translation disabled

	KeyboardWaitOutport();
	outportb (0x64, 0x60);                // Send "write configuration byte" command
	KeyboardWaitOutport();
	outportb (0x60, foo);                 // Set configuration byte to disable translation

}
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: not unique keyboard scancodes

Post by Brendan »

Hi,
zgintasz wrote:Strange, still doesn't work: (by the way, using Virtual Box)
Does it make no difference at all, or crash, or lock up, or make your cat explode?

Does the old "configuration byte" you read look sane?

Did you try it on any other emulators and/or computers?


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
zgintasz
Posts: 18
Joined: Sat Nov 03, 2012 11:14 am

Re: not unique keyboard scancodes

Post by zgintasz »

Neverming, it was very very very stupid mistake, sorry.
Scancode set and translation are working fine. The only thing left to do is to get the correct scancode. Example(it's a single press scancodes), numpad 7 scancodes: 108 240 108(I get 240 and 108 when I release button), home key scancodes: 224 108 224 240 108(I get 224, 240 and 108 when I release button). Please give me a code for getting the unique scancode.
Thanks, have a nice day :).
egos
Member
Member
Posts: 612
Joined: Fri Nov 16, 2007 1:59 pm

Re: not unique keyboard scancodes

Post by egos »

Heh, what you want that you get :lol: Set #2 is better but it requires a special handling.
If you have seen bad English in my words, tell me what's wrong, please.
zgintasz
Posts: 18
Joined: Sat Nov 03, 2012 11:14 am

Re: not unique keyboard scancodes

Post by zgintasz »

egos wrote:Heh, what you want that you get :lol:
single scancode, not many scancodes on key press. For example if I press home key, I get these scancodes: 224 108 224 240 108(224, 108 and 224 when key is pressed, 240 and 108 when key is released). So how to make if sentence to get unique and single(single = 1, many = more than 1) scancode, so I can write keyboard ascii map - I can't write it now, because I get not single scancode on key press. Hope you understood.
Post Reply