105 lines
2.5 KiB
Nim
105 lines
2.5 KiB
Nim
import vga
|
|
|
|
const
|
|
vgaWidth = 80
|
|
vgaHeight = 25
|
|
|
|
type
|
|
VGAMemory = ptr array[0..(vgaWidth * vgaHeight - 1), VGADoubleByte]
|
|
|
|
const
|
|
terminalBufferBaseAddress = 0xB8000
|
|
vgaMem = cast[VGAMemory](terminalBufferBaseAddress)
|
|
|
|
var
|
|
terminalRow, terminalColumn = 0
|
|
terminalColour: int
|
|
|
|
proc terminalWriteAtPoint(writeChar: char, colour: int, xPos: int, yPos: int) =
|
|
let index = yPos * vgaWidth + xPos
|
|
vgaMem[index] = vga.vgaEntry(writeChar, terminalColour)
|
|
|
|
proc terminalClear*() =
|
|
for x in 0 .. vgaWidth - 1:
|
|
for y in 0 .. vgaHeight - 1:
|
|
terminalWriteAtPoint(' ', terminalColour, x, y)
|
|
terminalRow = 0
|
|
terminalColumn = 0
|
|
|
|
proc initialize*() =
|
|
terminalColour = vgaEntryColour(VGA_Colour.lightGreen, VGA_Colour.red)
|
|
terminalClear()
|
|
|
|
proc terminalScroll() =
|
|
for y in 0 .. (vgaHeight - 2):
|
|
let base1 = y * vgaWidth
|
|
let base2 = (y + 1) * vgaWidth
|
|
for x in 0 .. vgaWidth - 1:
|
|
vgaMem[base1 + x] = vgaMem[base2 + x]
|
|
for x in 0 .. vgaWidth - 1: # Blank the "new" bottom line
|
|
terminalWriteAtPoint(' ', terminalColour, x, vgaHeight - 1)
|
|
|
|
proc setTerminalColour(newColour: int) =
|
|
terminalColour = newColour
|
|
|
|
proc terminalWriteChar(writeChar: char) =
|
|
|
|
proc scrollCheckAndSet() =
|
|
if(terminalRow < vgaHeight - 1): inc(terminalRow)
|
|
else: # If we are at the bottom of the screen then scroll the screen.
|
|
terminalScroll()
|
|
terminalColumn = 0
|
|
|
|
if(writeChar == '\L'):
|
|
scrollCheckAndSet()
|
|
return
|
|
terminalWriteAtPoint(writeChar, terminalColour, terminalColumn, terminalRow)
|
|
inc(terminalColumn)
|
|
if(terminalColumn == vgaWidth):
|
|
scrollCheckAndSet()
|
|
|
|
proc write*(data: string) =
|
|
for i in 0 .. data.len - 1:
|
|
terminalWriteChar(data[i])
|
|
|
|
proc write*(data: char) =
|
|
terminalWriteChar(data)
|
|
|
|
proc write*(data: SomeSignedInt) =
|
|
var parsedData: array[0..9, char]
|
|
if(data == 0):
|
|
tty.write('0')
|
|
return
|
|
var input = data
|
|
var i = 0
|
|
if(input < 0):
|
|
tty.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
|
|
tty.write(parsedData[i])
|
|
|
|
proc write*(data: SomeUnsignedInt) =
|
|
var parsedData: array[0..9, char]
|
|
if(data == 0):
|
|
tty.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
|
|
tty.write(parsedData[i])
|
|
|
|
proc writeLine*(data: any) =
|
|
tty.write(data)
|
|
tty.write('\L') |