您的位置:首页 > 运维架构 > Linux

用nasm语言重新实现linux-0.11 键盘驱动程序(us)(博古以通今) (开发版,未精简)

2010-04-26 22:38 746 查看
;文件名:followking/kernel/chr_drv/keyboard.s
;本文件改写linux-0.11/kernel/chr_drv/keyboard.s
;目的是为了体验整个系统构建的过程。
;作者:hk0625
;开始时间: 2010年04月25日星期日 12:57
;完成时间: 2010年04月25日星期日 22:48(完成)
;最后修改时间: 2010年04月26日星期一 21:18
;地点: 北京化工大学郁夫图书馆文法阅览室小圆桌
;这是一个值得纪念的时刻,经过一天的调试,终于可以在屏幕上显示东西了。
;虽然还有debug,呵呵。不过没关系。我会解决它。激情伴随整个系统编写过程。
;初步调试我都是各个模块单独调试。这样可以让问题变得简单。这是一个很好的
;方法哦。 2010年04月26日星期一 15:08
;Email:shaohua20051231@163.com
;下面let's try!

%define KBD_US

section .text
global keyboard_interrupt, put_queue
extern do_tty_interrupt, table_list, show_stat, printk, mywrite
size equ 1024
head equ 4
tail equ 8
proc_list equ 12
buf equ 16

mode: db 0
leds: db 2
zero: db 0
;info_hello: db 'hello, in the keyboard_interrupt', 0x0a,0x00
;info_len equ $-info_hello
info_db db '?'
keyboard_interrupt:
;iret
;jmp $
push eax
push ebx
push ecx
push edx
push ds
push es
push fs
mov eax, 0x10
mov ds, ax
mov es, ax
mov eax, 0x17
mov fs, ax
;in al, 0x60
;mov byte[info_db], al
;push 1
;push info_db
;push info_len
;push info_hello ;调试用,2010年04月26日星期一 15:04
;push 0
;call mywrite
;add esp, 12
;mov al, 0x20
;out 0x20, al

;pop fs
;pop es
;pop ds
;pop edx
;pop ecx
;pop ebx
;pop eax
;iret
xor al, al
in al, 0x60
cmp al, 0xe0
je set_zero
cmp al, 0xe1
je set_e1
call [key_table+eax*4]
;mov byte[info_db], al
mov byte[zero], 0x0
zero_one:
in al, 0x61
jmp labe12
labe12:
;jmp .2f
;.2f: or al, 0x80
call delay
or al, 0x80
; jmp .3f
;.3f: jmp .4f
call delay
;.4f: out 0x61, al
out 0x61, al
call delay
; jmp .5f
;.5f: jmp .6f
;.6f: and al, 0x7F
and al, 0x7F
out 0x61, al
mov al, 0x20
out 0x20, al
;push 1
;push info_db ;调试用,呵呵。2010年04月26日星期一 15:04
push 0
;call mywrite ;这个函数这是太有才了。呵呵,2010年04月26日星期一 15:04
call do_tty_interrupt
add esp, 4
;add esp, 12
pop fs
pop es
pop ds
pop edx
pop ecx
pop ebx
pop eax
iret
set_zero:
mov byte[zero], 1
jmp zero_one
set_e1: mov byte[zero], 2
jmp zero_one

put_queue:
push ecx
push edx
mov edx, [table_list]
;jmp $
;lea edx, [table_list]
mov ecx, [head+ edx]
.1: mov [buf+edx+ecx], al
;mov byte[info_db], al
;push 1
;push info_db
;push 0
;call mywrite
;add esp, 12
inc ecx
and ecx, size-1
cmp ecx, [tail+edx]

je .3
shrd eax, ebx,8
je .2
shr ebx, 8
jmp .1
.2: mov [head+edx], ecx
mov ecx, [proc_list+edx]
test ecx, ecx
je .3
mov byte[ecx], 0
.3: pop edx
pop ecx
ret

ctrl: mov al, 0x04
jmp ctr_1
alt: mov al, 0x10
ctr_1: cmp byte[zero], 0
je .2
add al, al
.2: or al, [mode]
ret

unctrl: mov al, 0x04
jmp before
unalt: mov al, 0x10
before: cmp byte[zero], 0
je .2
add al, al
.2: not al
and al, [mode]
ret

lshift:
or byte[mode], 0x01
ret
unlshift:
and byte[mode], 0xfe
ret
rshift:
or byte[mode], 0x02
ret
unrshift:
and byte[mode], 0xfd
ret
caps: test byte[mode], 0x80
jne .l_ret
xor byte[leds], 4
xor byte[mode], 0x40
or byte[mode], 0x80
.l_ret: jmp set_leds
ret
set_leds:
call kb_wait
mov al, 0xed
out 0x60, al
call kb_wait
mov al, [leds]
out 0x60, al
ret
uncaps:
and byte[mode], 0x7f
ret
scroll:
xor byte[leds], 1
jmp set_leds
num: xor byte[leds], 2
jmp set_leds
cursor:
sub al, 0x47
jb l_ret
cmp al, 12
ja l_ret
jne cur2

test byte[mode], 0x0c
je cur2
test byte[mode], 0x30
jne reboot
cur2: cmp byte[zero], 0x01

je cur
test byte[leds], 0x02
je cur
test byte[mode], 0x03

jne cur
xor ebx, ebx
mov al, [num_table+eax]
jmp put_queue
l_ret: ret

reboot:
call kb_wait
mov word[0x472], 0x1234
mov al, 0xfc
out 0x64, al

cur: mov al, [cur_table+eax]
cmp al, '9'
ja ok_cur
mov ah, '~'

ok_cur: shl eax, 16
mov ax, 0x5b1b
xor ebx, ebx
jmp word put_queue

%ifdef KBD_FR
num_table:
db "789 456 1230."
%else
num_table:
db "789 456 1230,"
%endif
cur_table:
db "HA5 DGC YB623"

func:
push eax
push ecx
push edx
call show_stat
pop edx
pop ecx
pop eax
sub al, 0x3B
jb end_func
cmp al, 9
jbe ok_func
sub al, 18
cmp al, 10
jb end_func
cmp al, 11
ja end_func
ok_func:
cmp ecx, 4 ;check that there is enough room
jl end_func
mov eax, [func_table+eax*4]
xor ebx,ebx
jmp put_queue
end_func:
ret

func_table:
dd 0x415b5b1b,0x425b5b1b,0x435b5b1b,0x445b5b1b
dd 0x455b5b1b,0x465b5b1b,0x475b5b1b,0x485b5b1b
dd 0x495b5b1b,0x4a5b5b1b,0x4b5b5b1b,0x4c5b5b1b

do_self:
lea ebx, [alt_map]
test byte[mode], 0x20
jne .1f
lea ebx, [shift_map]
test byte[mode], 0x03
jne .1f
lea ebx, [key_map]

.1f: mov al, [ebx+eax]
or al, al
je none
test byte[mode], 0x4c
je .2f
cmp al, 'a'
jb .2f
cmp al, '}'
ja .2f
sub al, 32
.2f: test byte[mode], 0x0c
je .3f
cmp al, 64
jb .3f
cmp al, 64+32
jae .3f
sub al, 64
.3f: test byte[mode], 0x10
je .4f
or al, 0x80
.4f: and eax, 0xff
xor ebx, ebx
mov byte[info_db], al
;push 1
;push info_db
;push 0
;call mywrite
;add esp, 12
call put_queue
none: ret

minus: cmp byte[zero], 1
jne do_self
mov eax, '/'
xor ebx, ebx
jmp put_queue

kb_wait:
push eax
.1: in al, 0x64
test al, 0x02
jne .1
pop eax
ret

die: jmp die
delay:
jmp delay_ret
delay_ret: ret

key_table:
dd none,do_self,do_self,do_self ; 00-03 s0 esc 1 2
dd do_self,do_self,do_self,do_self ; 04-07 3 4 5 6
dd do_self,do_self,do_self,do_self ; 08-0B 7 8 9 0
dd do_self,do_self,do_self,do_self ; 0C-0F + ' bs tab
dd do_self,do_self,do_self,do_self ; 10-13 q w e r
dd do_self,do_self,do_self,do_self ; 14-17 t y u i
dd do_self,do_self,do_self,do_self ; 18-1B o p } ^
dd do_self,ctrl,do_self,do_self ; 1C-1F enter ctrl a s
dd do_self,do_self,do_self,do_self ; 20-23 d f g h
dd do_self,do_self,do_self,do_self ; 24-27 j k l |
dd do_self,do_self,lshift,do_self ; 28-2B { para lshift ,
dd do_self,do_self,do_self,do_self ; 2C-2F z x c v
dd do_self,do_self,do_self,do_self ; 30-33 b n m ,
dd do_self,minus,rshift,do_self ; 34-37 . - rshift *
dd alt,do_self,caps,func ; 38-3B alt sp caps f1
dd func,func,func,func ; 3C-3F f2 f3 f4 f5
dd func,func,func,func ; 40-43 f6 f7 f8 f9
dd func,num,scroll,cursor ; 44-47 f10 num scr home
dd cursor,cursor,do_self,cursor ; 48-4B up pgup - left
dd cursor,cursor,do_self,cursor ; 4C-4F n5 right + end
dd cursor,cursor,cursor,cursor ; 50-53 dn pgdn ins del
dd none,none,do_self,func ; 54-57 sysreq ? < f11
dd func,none,none,none ; 58-5B f12 ? ? ?
dd none,none,none,none ; 5C-5F ? ? ? ?
dd none,none,none,none ; 60-63 ? ? ? ?
dd none,none,none,none ; 64-67 ? ? ? ?
dd none,none,none,none ; 68-6B ? ? ? ?
dd none,none,none,none ; 6C-6F ? ? ? ?
dd none,none,none,none ; 70-73 ? ? ? ?
dd none,none,none,none ; 74-77 ? ? ? ?
dd none,none,none,none ; 78-7B ? ? ? ?
dd none,none,none,none ; 7C-7F ? ? ? ?
dd none,none,none,none ; 80-83 ? br br br
dd none,none,none,none ; 84-87 br br br br
dd none,none,none,none ; 88-8B br br br br
dd none,none,none,none ; 8C-8F br br br br
dd none,none,none,none ; 90-93 br br br br
dd none,none,none,none ; 94-97 br br br br
dd none,none,none,none ; 98-9B br br br br
dd none,unctrl,none,none ; 9C-9F br unctrl br br
dd none,none,none,none ; A0-A3 br br br br
dd none,none,none,none ; A4-A7 br br br br
dd none,none,unlshift,none ; A8-AB br br unlshift br
dd none,none,none,none ; AC-AF br br br br
dd none,none,none,none ; B0-B3 br br br br
dd none,none,unrshift,none ; B4-B7 br br unrshift br
dd unalt,none,uncaps,none ; B8-BB unalt br uncaps br
dd none,none,none,none ; BC-BF br br br br
dd none,none,none,none ; C0-C3 br br br br
dd none,none,none,none ; C4-C7 br br br br
dd none,none,none,none ; C8-CB br br br br
dd none,none,none,none ; CC-CF br br br br
dd none,none,none,none ; D0-D3 br br br br
dd none,none,none,none ; D4-D7 br br br br
dd none,none,none,none ; D8-DB br ? ? ?
dd none,none,none,none ; DC-DF ? ? ? ?
dd none,none,none,none ; E0-E3 zero e1 ? ?
dd none,none,none,none ; E4-E7 ? ? ? ?
dd none,none,none,none ; E8-EB ? ? ? ?
dd none,none,none,none ; EC-EF ? ? ? ?
dd none,none,none,none ; F0-F3 ? ? ? ?
dd none,none,none,none ; F4-F7 ? ? ? ?
dd none,none,none,none ; F8-FB ? ? ? ?
dd none,none,none,none ; FC-FF ? ? ? ?

;%ifdef KBD_FINNISH
;key_map:
; db 0,27
; db "1234567890+'"
; db 127,9
;; db "qwertyuiop}"
; db 0,13,0
; db "asdfghjkl|{"
; db 0,0
; db "'zxcvbnm,.-"
; db 0,'*',0,32 ; 36-39
; db 16,1,0 ; 3A-49
; db '-',0,0,0,'+' ; 4A-4E
; db 0,0,0,0,0,0,0 ; 4F-55
; db '<'
; db 10,1,0

;shift_map:
; db 0,2
; db '!/"%$%&/()=?`'
; db 127,9
; db "QWERTYUIOP]^"
; db 13,0
; db "ASDFGHJKL//["
; db 0,0
; db "*ZXCVBNM;:_"
; db 0,'*',0,32 ; 36-39
; db 16,1,0 ; 3A-49
; db '-',0,0,0,'+' ; 4A-4E
; db 0,0,0,0,0,0,0 ; 4F-55
; db '>'
; db 10,1,0
;
;alt_map:
; db 0,0
; db "/0@/0$/0/0{[]}///0"
; db 0,0
; db 0,0,0,0,0,0,0,0,0,0,0
; db '~',13,0
; db 0,0,0,0,0,0,0,0,0,0,0
; db 0,0
; db 0,0,0,0,0,0,0,0,0,0,0
; db 0,0,0,0 ; 36-39
; db 16,1,0 ; 3A-49
; db 0,0,0,0,0 ; 4A-4E
; db 0,0,0,0,0,0,0 ; 4F-55
; db '|'
; db 10,1,0

;%elifdef KBD_US
%ifdef KBD_US
key_map:
db 0,27
db "1234567890-="
db 127,9
db "qwertyuiop[]"
db 13,0
db "asdfghjkl;'"
db '`',0
db "/zxcvbnm,./"
db 0,'*',0,32 ; 36-39
db 16,1,0 ; 3A-49
db '-',0,0,0,'+' ; 4A-4E
db 0,0,0,0,0,0,0 ; 4F-55
db '<'
db 10,1,0

shift_map:
db 0,27
db "!@%$%^&*()_+"
db 127,9
db "QWERTYUIOP{}"
db 13,0
db "ASDFGHJKL:",'"'
db '~',0
db "|ZXCVBNM<>?"
db 0,'*',0,32 ; 36-39
db 16,1,0 ; 3A-49
db '-',0,0,0,'+' ; 4A-4E
db 0,0,0,0,0,0,0 ; 4F-55
db '>'
db 10,1,0

alt_map:
db 0,0
db "/0@/0$/0/0{[]}///0"
db 0,0
db 0,0,0,0,0,0,0,0,0,0,0
db '~',13,0
db 0,0,0,0,0,0,0,0,0,0,0
db 0,0
db 0,0,0,0,0,0,0,0,0,0,0
db 0,0,0,0 ; 36-39
db 16,1,0 ; 3A-49
db 0,0,0,0,0 ; 4A-4E
db 0,0,0,0,0,0,0 ; 4F-55
db '|'
db 10,1,0

;%elifdef KBD_GR
;
;key_map:
; db 0,27
; db "1234567890//'"
; db 127,9
; db "qwertzuiop@+"
; db 13,0
; db "asdfghjkl[]^"
; db 0,'%
; db "yxcvbnm,.-"
; db 0,'*,0,32 ; 36-39
; db 16,1,0 ; 3A-49
; db '-,0,0,0,'+ ; 4A-4E
; db 0,0,0,0,0,0,0 ; 4F-55
; db '<
; db 10,1,0
;
;
;shift_map:
; db 0,27
; db "!/"%$%&/()=?`"
; db 127,9
; db "QWERTZUIOP//*"
; db 13,0
; db "ASDFGHJKL{}~"
; db 0,''
; db "YXCVBNM;:_"
; db 0,'*,0,32 ; 36-39
; db 16,1,0 ; 3A-49
; db '-,0,0,0,'+ ; 4A-4E
; db 0,0,0,0,0,0,0 ; 4F-55
; db '>
; db 10,1,0
;
;alt_map:
; db 0,0
; db "/0@/0$/0/0{[]}///0"
; db 0,0
; db '@,0,0,0,0,0,0,0,0,0,0
; db '~,13,0
; db 0,0,0,0,0,0,0,0,0,0,0
; db 0,0
; db 0,0,0,0,0,0,0,0,0,0,0
; db 0,0,0,0 ; 36-39
; db 16,1,0 ; 3A-49
; db 0,0,0,0,0 ; 4A-4E
; db 0,0,0,0,0,0,0 ; 4F-55
; db '|
; db 10,1,0
;
;
;%elifdef KBD_FR
;
;key_map:
; db 0,27
; db "&{/"'(-}_/@)="
; db 127,9
; db "azertyuiop^$"
; db 13,0
; db "qsdfghjklm|"
; db '`,0,42 ; coin sup gauche, don't know, [*|mu]
; db "wxcvbn,;:!"
; db 0,'*,0,32 ; 36-39
; db 16,1,0 ; 3A-49
; db '-,0,0,0,'+ ; 4A-4E
; db 0,0,0,0,0,0,0 ; 4F-55
; db '<
; db 10,1,0
;
;shift_map:
; db 0,27
; db "1234567890]+"
; db 127,9
; db "AZERTYUIOP<>"
; db 13,0
; db "QSDFGHJKLM%"
; db '~,0,'%
; db "WXCVBN?.///"
; db 0,'*,0,32 ; 36-39
; db 16,1,0 ; 3A-49
; db '-,0,0,0,'+ ; 4A-4E
; db 0,0,0,0,0,0,0 ; 4F-55
; db '>
; db 10,1,0

;alt_map:
; db 0,0
; db "/0~%{[|`//^@]}"
; db 0,0
; db '@,0,0,0,0,0,0,0,0,0,0
; db '~,13,0
; db 0,0,0,0,0,0,0,0,0,0,0
; db 0,0
; db 0,0,0,0,0,0,0,0,0,0,0
; db 0,0,0,0 ; 36-39
; db 16,1,0 ; 3A-49
; db 0,0,0,0,0 ; 4A-4E
; db 0,0,0,0,0,0,0 ; 4F-55
; db '|
; db 10,1,0

%else
%error "KBD-type not defined"
%endif
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: