alcedo/kernel/arch/i386/serial.nim

66 lines
1.6 KiB
Nim

import io
const portCOM1 = 0x3f8'u16
proc init*() =
io.outb((portCOM1 + 1, 0x00'u8)); # Disable all interrupts
io.outb((portCOM1 + 3, 0x80'u8)); # Enable DLAB (set baud rate divisor)
io.outb((portCOM1 + 0, 0x03'u8)); # Set divisor to 3 (lo byte) 38400 baud
io.outb((portCOM1 + 1, 0x00'u8)); # (hi byte)
io.outb((portCOM1 + 3, 0x03'u8)); # 8 bits, no parity, one stop bit
io.outb((portCOM1 + 2, 0xC7'u8)); # Enable FIFO, clear them, with 14-byte threshold
io.outb((portCOM1 + 4, 0x0B'u8)); # IRQs enabled, RTS/DSR set
proc isTransitEmpty(): uint =
result = io.inb(portCOM1 + 5) and 0x20'u
proc write*(input: char) =
while isTransitEmpty() == 0:
discard
io.outb((portCOM1, uint8(input)))
proc write*(input: string) =
for i in 0 .. input.len - 1:
serial.write(input[i])
var parsedData: array[0..9, char]
proc write*(data: SomeSignedInt) =
if(data == 0):
serial.write('0')
return
var input = data
var i = 0
if(input < 0):
serial.write('-')
input = input * -1
while(input != 0):
let parsedChar: char = char(48 + (input mod 10))
parsedData[i] = parsedChar
i = i + 1
input = input div 10
while i > 0:
i = i - 1
serial.write(parsedData[i])
proc write*(data: SomeUnsignedInt) =
if(data == 0):
serial.write('0')
return
var input = data
var i = 0
while(input != 0):
let parsedChar: char = char(48'u + (input mod 10))
parsedData[i] = parsedChar
i = i + 1
input = input div 10
while i > 0:
i = i - 1
serial.write(parsedData[i])
proc newLine*() = write('\L')
proc writeLine*(data: any) =
serial.write(data)
newLine()