flat assembler
Message board for the users of flat assembler.

Index > OS Construction > Testing the Keyboard

Author
Thread Post new topic Reply to topic
SouthernMunk



Joined: 05 Aug 2005
Posts: 6
SouthernMunk 05 Aug 2005, 06:57
Hello,

I'm just starting out in OS development in order to give myself a better understanding of what's going on right near the hardware level. I've created a small boot sectore with FASM and have written it to floppy with RawWrite, and then emulate that with Bochs.

At the moment all the code does is ask the keyboard to test itself. However, there is always an error. I think there's a bug in either my Wait8042 or Empty8042 functions (I wrote these functions because I assumed it would take several CPU cycles for the KBC to process requests).

Any help would be much appreciated. This is my first post so please keep the flaming to a minimal level. Very Happy

Thanks.

Code:
; -------------------------------
; Memory table (hex):
; -------------------------------
; (07C0:0000) 07C00 | Boot Sector
; (07C0:01FF) 07DFF | (512 bytes)
; -------------------------------
; (07C0:0200) 07E00 | Stack Segment
; (07C0:03FF) 07FFF | (512 bytes)
; -------------------------------

; -------------------------------
; Equals
; -------------------------------
KBC_CTL         equ     060h           ; 8042 control port
KBC_STAT        equ     064h           ; 8042 status port

; Boot sector is loaded into 07C00:0000
ORG 0x7c00

; Jump to the start of the program.
jmp Start

; -------------------------------
; Data
; -------------------------------
MSG_TESTKBC     db "Testing KBC... ", 0
MSG_DIE         db "Error! Please restart your computer!", 0
MSG_FAIL        db "FAILED!", 13, 10, 0
MSG_OK          db "OK!", 13, 10, 0

; -------------------------------
; Function Prototypes
; -------------------------------

; An infinite loop...
Die:
        mov si, MSG_DIE
        call PrintString
        jmp $
        ret

; Prints a string to the screen...
PrintString:
        push si
        push ax
        push bx
        .PrintChar:
                lodsb
                cmp al, 0x00
                jz .Finished
                mov ah, 0x0e
                mov bx, 0x0005
                int 0x10
                jmp .PrintChar
        .Finished:
                pop bx
                pop ax
                pop si
                ret

; Asks the keyboard to test itself. Returns the result in AX.
TestKBC:
        mov si, MSG_TESTKBC
        call PrintString
        call Empty8042          ; Empty the 8042 (Keyboard Controller or KBC).
        mov al, 0xaa            ; Move the data (0xaa - Keyboard Self Test command) to AL.
        out KBC_STAT, al        ; Output the command (AL) to the status port.
        call Wait8042           ; Wait for the 8042 to process request.
        in al, KBC_CTL          ; Input the result to AL.
        cmp al, 0x55            ; 0x55 is the KBC's OK response to the 0xaa (Self Test) command.
        jnz .TestKBC_Success
        .TestKBC_Failure:
                mov si, MSG_FAIL
                call PrintString
                call Die
        .TestKBC_Success:
                mov si, MSG_OK
                call PrintString
                ret

; Waits for the 8042 (Keyboard Controller or KBC) to have data for the system.
Wait8042:
        push ax                 ; Push registers used to the stack.
        .Wait8042_Test:
        in al, KBC_STAT         ; Input data from the KBC status port to AL.
        test al, 00000001b      ; Bit 0 of the KBC status port states whether the KBC has data for the system.
        jz .Wait8042_Test
        pop ax                  ; Pop the registers used from the stack.
        ret

; Empty the 8042 of all data for the system.
Empty8042:
        push ax                 ; Push all registers used to the stack.
        .Empty8042_Test:
        in al, KBC_STAT         ; Input data from the KBC status port to AL.
        test al, 00000001b      ; Bit 0 of the KBC status port states whether the KBC has data for the system.
        jnz .Empty8042_Flush
        jmp .Empty8042_Finished ; Jump to the finish of the function.
        .Empty8042_Flush:
        in al, KBC_CTL          ; Input data from KBC control port to AL.
        jmp .Empty8042_Test     ; Jump back to the testing phase.
        .Empty8042_Finished:
        pop ax                  ; Pop all registers used from the stack.
        ret

; The start of the program.
Start:

; Initialise the stack (allow 512 bytes).
mov ax, 0x07c0
mov ss, ax
mov sp, 0x03ff

; Initialise the data segment (first 64K of 1MB memory).
mov ax, 0x0000
mov ds, ax

; Clear the screen by changing the resolution.
mov ax, 0x0007
int 0x10

; Test the KBC.
call TestKBC

; -------------------------------
; Boot sectore signature.
; -------------------------------

times 510-($-$$) db 0
BOOT_SIG dw 0xaa55

    
Post 05 Aug 2005, 06:57
View user's profile Send private message Reply with quote
Night Rider



Joined: 28 Jul 2005
Posts: 72
Night Rider 05 Aug 2005, 18:35
What exactly do you mean 'there is always an error.' . Where from do you know it? What error? During compilation time or what? Message appears?
Who(what i mean) reports about error? Try to comment part of code and figure out, where error occures.
Try to comment mov al, 0xaa and next strings etc. Use bochs.... Very Happy
Post 05 Aug 2005, 18:35
View user's profile Send private message ICQ Number Reply with quote
SouthernMunk



Joined: 05 Aug 2005
Posts: 6
SouthernMunk 06 Aug 2005, 04:44
Sorry, I should have been more specific.

The program compiles fine, but the result of the KBC command 0xAA (self test) is not 0x55 (OK). Or perhaps I'm not checking the result properly, I don't know. The result is always

Quote:

Testing KBC... FAILED!
Error! Please restart your computer!


Also, do most modern processors support Fast A20?
Post 06 Aug 2005, 04:44
View user's profile Send private message Reply with quote
Night Rider



Joined: 28 Jul 2005
Posts: 72
Night Rider 06 Aug 2005, 19:14
Try debug it with Bochs
Post 06 Aug 2005, 19:14
View user's profile Send private message ICQ Number Reply with quote
SouthernMunk



Joined: 05 Aug 2005
Posts: 6
SouthernMunk 07 Aug 2005, 00:10
The same message appears with Bochs.

You can't see a problem with the code?
Post 07 Aug 2005, 00:10
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4623
Location: Argentina
LocoDelAssembly 07 Aug 2005, 01:20
I think the problem is you jump to TestKBC_Success if al != 0x55
Change "jnz .TestKBC_Success" with "jz .TestKBC_Success"

[edit]Note when the test is OK you return instead of call Die, take in mind when you return there is no more code after the call to TestKBC[/edit]
Post 07 Aug 2005, 01:20
View user's profile Send private message Reply with quote
Night Rider



Joined: 28 Jul 2005
Posts: 72
Night Rider 07 Aug 2005, 16:17
Oh, hell! I didn't say "try to execute your code with Bochs" I said try to DEBUG it with Bochs.
There is a debugger - put breakpoints etc and figure out, where error is!
Post 07 Aug 2005, 16:17
View user's profile Send private message ICQ Number Reply with quote
Night Rider



Joined: 28 Jul 2005
Posts: 72
Night Rider 07 Aug 2005, 16:20
Yes, locodelassembly, you are right.
SouthernMunk, put jz not jnz (which is the same as 'jmp if not equal')
je and it must go on. Code is right and response is OK, but you print 'failed' if everything is ok...
Post 07 Aug 2005, 16:20
View user's profile Send private message ICQ Number Reply with quote
SouthernMunk



Joined: 05 Aug 2005
Posts: 6
SouthernMunk 08 Aug 2005, 01:28
Sweedish, thankyou (I should have guessed it was a simple mistake).

Very Happy
Post 08 Aug 2005, 01:28
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
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 vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.