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
|
|
|
|
|
2017-10-20 18:00:17 +01:00
|
|
|
proc vgaEntryColour(fg: VGA_Colour, bg: VGA_Colour): uint8 =
|
2017-10-20 22:04:09 +01:00
|
|
|
result = uint8(ord(fg)) or (uint8(ord(bg)) shl 4)
|
2017-10-20 14:47:56 +01:00
|
|
|
|
2017-10-20 18:00:17 +01:00
|
|
|
proc vgaEntry(c: char, colour: uint8): uint16 =
|
2017-10-20 22:04:09 +01:00
|
|
|
result = uint16(c) or (uint16(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
|
|
|
|
|
|
|
|
var
|
|
|
|
terminalRow, terminalColumn = 0
|
|
|
|
terminalColour = vgaEntryColour(VGA_Colour.lightGrey, VGA_Colour.black)
|
|
|
|
|
2017-10-20 18:12:00 +01:00
|
|
|
proc terminalWriteAtPoint(writeChar: char, colour: uint8, xPos: int, yPos: int) =
|
|
|
|
let index = terminalBufferBaseAddress + (yPos * vgaWidth + xPos)
|
2017-10-20 22:04:09 +01:00
|
|
|
cast[ptr uint16](index)[] = vgaEntry(writeChar, colour) # Write directly to display memory
|
2017-10-20 18:12:00 +01:00
|
|
|
|
2017-10-20 18:00:17 +01:00
|
|
|
proc terminalInitialize() =
|
|
|
|
for y in 0..<vgaWidth:
|
|
|
|
for x in 0..<vgaHeight:
|
2017-10-20 22:04:09 +01:00
|
|
|
terminalWriteAtPoint(' ', 0, x, y)
|
2017-10-20 18:12:00 +01:00
|
|
|
|
|
|
|
proc setTerminalColour(newColour: uint8) =
|
|
|
|
terminalColour = newColour
|
|
|
|
|
|
|
|
proc terminalWriteChar(writeChar: char) =
|
2017-10-20 22:04:09 +01:00
|
|
|
if(writeChar == '\r'):
|
|
|
|
inc(terminalRow)
|
|
|
|
if(terminalRow == vgaHeight): terminalRow = 0
|
|
|
|
return
|
2017-10-20 18:12:00 +01:00
|
|
|
terminalWriteAtPoint(writeChar, terminalColour, terminalColumn, terminalRow)
|
|
|
|
inc(terminalColumn)
|
2017-10-20 22:04:09 +01:00
|
|
|
if(terminalColumn == vgaWidth):
|
|
|
|
terminalColumn = 0
|
|
|
|
inc(terminalRow)
|
|
|
|
if(terminalRow == vgaHeight): terminalRow = 0
|
2017-10-20 18:20:42 +01:00
|
|
|
|
|
|
|
proc terminalWrite(data: string) =
|
|
|
|
for character in data:
|
|
|
|
terminalWriteChar(character)
|
2017-10-20 18:35:18 +01:00
|
|
|
|
|
|
|
proc kernelMain() {.exportc: "kernel_main"}=
|
|
|
|
terminalInitialize()
|
2017-10-20 22:04:09 +01:00
|
|
|
terminalWrite("Hello World\rThis is Nim!")
|
2017-10-20 18:35:18 +01:00
|
|
|
|