alcedo/kernel.nim

70 lines
1.8 KiB
Nim
Raw Normal View History

2017-10-20 14:47:56 +01:00
type
VGA_Colour {.pure.} = enum
black = 0,
blue = 1,
green = 2,
cyan = 3,
red = 4,
magenta = 5,
brown = 6,
lightGrey = 7,
darkGrey = 8,
lightBlue = 9,
lightGreen = 10,
lightCyan = 11,
lightRed = 12,
lightMagenta = 13,
lightBrown = 14,
white = 15
proc vgaEntryColour(fg: VGA_Colour, bg: VGA_Colour): int =
result = ord(fg) or (ord(bg) shl 4)
2017-10-20 14:47:56 +01:00
proc vgaEntry(c: char, colour: int): int16 =
result = int16(int(c) or (colour shl 8))
2017-10-20 14:47:56 +01:00
const
vgaWidth = 80
vgaHeight = 25
2017-10-20 18:00:17 +01:00
terminalBufferBaseAddress = 0xB8000
bufferWidthSkip = vgaWidth * 2
2017-10-20 18:00:17 +01:00
var
terminalRow, terminalColumn = 0
terminalColour: int
2017-10-20 18:00:17 +01:00
proc terminalWriteAtPoint(writeChar: char, colour: int, xPos: int, yPos: int) =
let index = terminalBufferBaseAddress + (yPos * bufferWidthSkip + (xPos * 2))
cast[ptr int16](index)[] = vgaEntry(writeChar, terminalColour) # Write directly to display memory
2017-10-20 18:00:17 +01:00
proc terminalInitialize() =
terminalColour = vgaEntryColour(VGA_Colour.lightGreen, VGA_Colour.red)
for x in 0..<vgaWidth:
for y in 0..<vgaHeight:
terminalWriteAtPoint(' ', terminalColour, x, y)
proc setTerminalColour(newColour: int) =
terminalColour = newColour
proc terminalWriteChar(writeChar: char) =
if(writeChar == '\L'):
inc(terminalRow)
terminalColumn = 0
if(terminalRow == vgaHeight): terminalRow = 0
return
terminalWriteAtPoint(writeChar, terminalColour, terminalColumn, terminalRow)
inc(terminalColumn)
if(terminalColumn == vgaWidth):
terminalColumn = 0
inc(terminalRow)
if(terminalRow == vgaHeight): terminalRow = 0
proc terminalWrite(data: string) =
for character in data:
terminalWriteChar(character)
2017-10-20 18:35:18 +01:00
proc kernelMain() {.exportc: "kernel_main"}=
terminalInitialize()
terminalWrite("Hello World!\LNim here!")
2017-10-20 18:35:18 +01:00