2017-10-21 19:23:17 +01:00
|
|
|
import vga
|
2017-10-20 14:47:56 +01:00
|
|
|
|
|
|
|
const
|
|
|
|
vgaWidth = 80
|
|
|
|
vgaHeight = 25
|
2017-10-22 02:00:07 +01:00
|
|
|
|
|
|
|
type
|
|
|
|
VGAMemory = ptr array[0..(vgaWidth * vgaHeight - 1), VGADoubleByte]
|
|
|
|
|
|
|
|
const
|
2018-05-16 00:42:37 +01:00
|
|
|
terminalBufferBaseAddress = 0xC0000000
|
2017-10-22 01:54:51 +01:00
|
|
|
vgaMem = cast[VGAMemory](terminalBufferBaseAddress)
|
2017-10-20 18:00:17 +01:00
|
|
|
|
|
|
|
var
|
|
|
|
terminalRow, terminalColumn = 0
|
2017-10-21 16:24:06 +01:00
|
|
|
terminalColour: int
|
2017-10-20 18:00:17 +01:00
|
|
|
|
2017-10-21 16:24:06 +01:00
|
|
|
proc terminalWriteAtPoint(writeChar: char, colour: int, xPos: int, yPos: int) =
|
2017-10-22 01:54:51 +01:00
|
|
|
let index = yPos * vgaWidth + xPos
|
|
|
|
vgaMem[index] = vga.vgaEntry(writeChar, terminalColour)
|
2017-10-20 18:12:00 +01:00
|
|
|
|
2017-10-22 00:22:32 +01:00
|
|
|
proc terminalClear*() =
|
2017-11-07 01:39:24 +00:00
|
|
|
for x in 0 .. vgaWidth - 1:
|
|
|
|
for y in 0 .. vgaHeight - 1:
|
2017-10-21 16:24:06 +01:00
|
|
|
terminalWriteAtPoint(' ', terminalColour, x, y)
|
2017-10-22 00:22:32 +01:00
|
|
|
terminalRow = 0
|
|
|
|
terminalColumn = 0
|
|
|
|
|
2018-05-02 23:02:46 +01:00
|
|
|
proc init*() =
|
2017-10-22 00:22:32 +01:00
|
|
|
terminalColour = vgaEntryColour(VGA_Colour.lightGreen, VGA_Colour.red)
|
|
|
|
terminalClear()
|
|
|
|
|
|
|
|
proc terminalScroll() =
|
2017-11-07 01:39:24 +00:00
|
|
|
for y in 0 .. (vgaHeight - 2):
|
2017-10-22 02:02:19 +01:00
|
|
|
let base1 = y * vgaWidth
|
|
|
|
let base2 = (y + 1) * vgaWidth
|
2017-11-07 01:39:24 +00:00
|
|
|
for x in 0 .. vgaWidth - 1:
|
2017-10-22 02:02:19 +01:00
|
|
|
vgaMem[base1 + x] = vgaMem[base2 + x]
|
2017-11-07 01:39:24 +00:00
|
|
|
for x in 0 .. vgaWidth - 1: # Blank the "new" bottom line
|
2017-10-22 01:54:51 +01:00
|
|
|
terminalWriteAtPoint(' ', terminalColour, x, vgaHeight - 1)
|
2017-10-20 18:12:00 +01:00
|
|
|
|
2017-10-21 16:24:06 +01:00
|
|
|
proc setTerminalColour(newColour: int) =
|
2017-10-20 18:12:00 +01:00
|
|
|
terminalColour = newColour
|
|
|
|
|
|
|
|
proc terminalWriteChar(writeChar: char) =
|
2017-10-22 18:50:01 +01:00
|
|
|
|
|
|
|
proc scrollCheckAndSet() =
|
2017-10-22 01:54:51 +01:00
|
|
|
if(terminalRow < vgaHeight - 1): inc(terminalRow)
|
|
|
|
else: # If we are at the bottom of the screen then scroll the screen.
|
|
|
|
terminalScroll()
|
2017-10-21 18:35:13 +01:00
|
|
|
terminalColumn = 0
|
2017-10-22 18:50:01 +01:00
|
|
|
|
|
|
|
if(writeChar == '\L'):
|
|
|
|
scrollCheckAndSet()
|
2017-10-20 22:04:09 +01:00
|
|
|
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):
|
2017-10-22 18:50:01 +01:00
|
|
|
scrollCheckAndSet()
|
2017-10-20 18:20:42 +01:00
|
|
|
|
2018-04-28 22:22:05 +01:00
|
|
|
proc write*(data: string) =
|
2017-11-07 01:39:24 +00:00
|
|
|
for i in 0 .. data.len - 1:
|
|
|
|
terminalWriteChar(data[i])
|
2017-10-22 00:22:32 +01:00
|
|
|
|
2018-04-28 22:22:05 +01:00
|
|
|
proc write*(data: char) =
|
2017-10-22 00:22:32 +01:00
|
|
|
terminalWriteChar(data)
|
|
|
|
|
2018-04-28 22:22:05 +01:00
|
|
|
proc write*(data: SomeSignedInt) =
|
2017-11-16 17:07:52 +00:00
|
|
|
var parsedData: array[0..9, char]
|
2017-10-22 01:54:51 +01:00
|
|
|
if(data == 0):
|
2018-04-28 22:22:05 +01:00
|
|
|
tty.write('0')
|
2017-10-22 00:44:01 +01:00
|
|
|
return
|
2017-10-22 01:54:51 +01:00
|
|
|
var input = data
|
|
|
|
var i = 0
|
2017-10-22 00:44:01 +01:00
|
|
|
if(input < 0):
|
2018-04-28 22:22:05 +01:00
|
|
|
tty.write('-')
|
2017-10-22 00:44:01 +01:00
|
|
|
input = input * -1
|
|
|
|
while(input != 0):
|
|
|
|
let parsedChar: char = char(48 + (input mod 10))
|
|
|
|
parsedData[i] = parsedChar
|
|
|
|
i = i + 1
|
|
|
|
input = input div 10
|
2017-10-22 00:22:32 +01:00
|
|
|
while i > 0:
|
|
|
|
i = i - 1
|
2018-04-28 22:22:05 +01:00
|
|
|
tty.write(parsedData[i])
|
2017-10-22 00:22:32 +01:00
|
|
|
|
2018-04-28 22:22:05 +01:00
|
|
|
proc write*(data: SomeUnsignedInt) =
|
2017-11-16 17:07:52 +00:00
|
|
|
var parsedData: array[0..9, char]
|
2017-11-06 21:43:34 +00:00
|
|
|
if(data == 0):
|
2018-04-28 22:22:05 +01:00
|
|
|
tty.write('0')
|
2017-11-06 21:43:34 +00:00
|
|
|
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
|
2018-04-28 22:22:05 +01:00
|
|
|
tty.write(parsedData[i])
|
|
|
|
|
|
|
|
proc writeLine*(data: any) =
|
|
|
|
tty.write(data)
|
|
|
|
tty.write('\L')
|