#include "drivers/ps2/ps2.h"

.code32 

.global ps2_wait_input_empty 
.global ps2_wait_output_full
.global ps2_write_command
.global ps2_read_data
.global ps2_write_data
.global ps2_flush_output_buffer

# helpers
ps2_wait_input_empty: #(void) -> void
    push %ebp
    mov %esp, %ebp
1:  
    inb $PS2_STATUS, %al
    andb $0x02, %al
    jnz 1b

    leave
    ret

ps2_wait_output_full: #(void) -> void
    push %ebp
    mov %esp, %ebp 
1:
    inb $PS2_STATUS, %al
    andb $0x01, %al
    jz 1b

    leave
    ret

ps2_write_command: #(uint8_t command) -> void
    push %ebp
    mov %esp, %ebp

    call ps2_wait_input_empty

    xor %eax, %eax
    movb 8(%ebp), %al
    outb %al, $PS2_COMMAND

    leave
    ret

ps2_read_data: #(void) -> uint8_t
    push %ebp
    mov %esp, %ebp

    call ps2_wait_output_full 

    xor %eax, %eax
    inb $PS2_DATA, %al

    leave
    ret

ps2_write_data: #(uint8_t) -> void
    push %ebp 
    mov %esp, %ebp

    call ps2_wait_input_empty
    
    xor %eax, %eax
    movb 8(%ebp), %al
    outb %al, $PS2_DATA

    leave
    ret


ps2_flush_output_buffer: #(void) -> void
    push %ebp
    mov %esp, %ebp
1:
    inb $PS2_STATUS, %al
    andb $0x01, %al
    jz 2f
    inb $PS2_DATA, %al
    jmp 1b
2:  
    leave
    ret