.set SIO_FMT,SIOFMT # 8N1
sio_port: .long SIOPRT # Base port
.globl sio_port
.globl sio_init
.globl sio_flush
.globl sio_putc
.globl sio_getc
.globl sio_ischar
sio_init: pushl %eax
movl (sio_port),%edx
addl $0x3,%edx # Data format reg
movb $SIO_FMT|0x80,%al # Set format
outb %al,(%dx) # and DLAB
subb $0x3,%dl # Divisor latch reg
popl %eax
outw %ax,(%dx) # BPS
movl (sio_port),%edx
addl $0x3,%edx # Data format reg
movb $SIO_FMT,%al # Clear
outb %al,(%dx) # DLAB
incl %edx # Modem control reg
movb $0x3,%al # Set RTS,
outb %al,(%dx) # DTR
incl %edx # Line status reg
sio_flush: xorl %ecx,%ecx # Timeout
movb $0x80,%ch # counter
sio_flush.1: call sio_ischar # Check for character
jz sio_flush.2 # Till none
loop sio_flush.1 # or counter is zero
movb $1, %al # Exhausted all tries
sio_flush.2: ret # To caller
sio_putc: pushl %eax
movl (sio_port),%edx
addl $0x5,%edx # Line status reg
xor %ecx,%ecx # Timeout
movb $0x40,%ch # counter
sio_putc.1: inb (%dx),%al # Transmitter
testb $0x20,%al # buffer empty?
loopz sio_putc.1 # No
jz sio_putc.2 # If timeout
popl %eax # Get the character
subb $0x5,%dl # Transmitter hold reg
outb %al,(%dx) # Write character
sio_putc.2: ret # To caller
sio_getc: call sio_ischar # Character available?
jz sio_getc # No
sio_getc.1: subb $0x5,%dl # Receiver buffer reg
inb (%dx),%al # Read character
ret # To caller
sio_ischar: movl (sio_port),%edx
addl $0x5,%edx # Line status register
xorl %eax,%eax # Zero
inb (%dx),%al # Received data
andb $0x1,%al # ready?
ret # To caller