import mm
import serialdbg
type TerminalUpdateHook is a function->void
type Terminal is
ptr baseaddr
int width
int height
int cursor_x
int cursor_y
byte attributes
bool serial_debug_enabled
TerminalUpdateHook update_hook
function(int base, int width, int height, TerminalUpdateHook hook) Terminal::new -> Terminal does
Terminal new = malloc(@sizeof(Terminal)@)|Terminal
new.baseaddr=base|ptr
new.width=width
new.height=height
new.cursor_x=0
new.cursor_y=0
new.attributes=0x0F|byte
new.serial_debug_enabled=1|bool
new.update_hook=hook
return new
function(Terminal t, int x, int y, byte value) Terminal:setchar -> void does
t.baseaddr[(t.width*y*2)+(x*2)]=value
t.baseaddr[(t.width*y*2)+(x*2)+1]=t.attributes
t.update_hook()
return
function(Terminal t, int attrs) Terminal:setattrs -> void does
t.attributes=attrs|byte
return
function(Terminal t, byte char) Terminal:putchar -> void does
if t.serial_debug_enabled do
kswritebyte(char)
done
t:setchar(t.cursor_x, t.cursor_y, char)
t.cursor_x+=1
if t.cursor_x>=t.width do
t.cursor_x=0
t.cursor_y+=1
done
t:scroll()
return
function(Terminal t, cstr string) Terminal:print -> void does
int pos = 0
if t.serial_debug_enabled do
kswritecstr(string)
done
while string[pos]!=0|byte do
if string[pos]!=10|byte do
t:setchar(t.cursor_x, t.cursor_y, string[pos])
t.cursor_x+=1
else do
t.cursor_x=0
t.cursor_y+=1
done
if t.cursor_x>=t.width do
t.cursor_x=0
t.cursor_y+=1
done
pos+=1
done
t:scroll()
return
function(Terminal t, cstr string) Terminal:printl -> void does
t:print(string)
t:print("\n")
t:scroll()
return
function(Terminal t) Terminal:scroll -> void does
while t.cursor_y>=t.height do
int idx=0
while idx<t.height do
memcpy(t.baseaddr:offset(t.width*2*(idx+1)), t.baseaddr:offset(t.width*2*idx), t.width*2)
idx+=1
done
memset(t.baseaddr:offset(t.height*t.width*2), t.width*2, 0|byte)
t.cursor_y-=1
done
t.update_hook()
return
function(Terminal t, int relx, int rely) Terminal:movecursor -> void does
t.cursor_x+=relx
if t.cursor_x<0 do
t.cursor_x=0
elif t.cursor_x>t.width do
t.cursor_x-=t.width
t.cursor_y+=1
done
t.cursor_y+=rely
if t.cursor_y<0 do
t.cursor_y=0
done
t:scroll()
return
function(Terminal t, int attrs, cstr string) Terminal:printa -> void does
byte prev = t.attributes
t.attributes=attrs|byte
t:print(string)
t.attributes=prev
return
function(Terminal t, long val) Terminal:printlong -> void does
if val==0|long do
t:print("0")
return
done
if val<0|long do
t:print("-")
val=-val
done
long temp
int idx=9
cstr str = malloc(11)
cstr q="?"
str[10]=0|byte
int sidx=0
while sidx<10 do
str[sidx]=q[0]
sidx+=1
done
while val>0|long do
temp=val%10|long
val/=10|long
str[idx]=(48+temp|int)|byte
idx-=1
done
t:print(((str|int)+idx+1)|ptr)
free(str)
return
function(Terminal t, uint val) Terminal:dumpbin -> void does
t:print("0b")
int pos = 4*8
while pos>0 do
pos-=1
if (val&(1<<pos))>0 do
t:print("1")
else do
t:print("0")
done
done
return
function(Terminal t, uint val, int len) Terminal:dumphex -> void does
t:print("0x")
int pos = len
while pos>0 do
pos-=1
int digit = (val>>(pos*4))&0b1111
if digit<10 do
t:putchar((48+digit)|byte)
else do
t:putchar((65+digit-10)|byte)
done
done
return
function(Terminal t, uint val) Terminal:dumphexi -> void does
t:dumphex(val, 8)
return
function(Terminal t, int val) Terminal:printi -> void does
t:printlong(val|long)
return
function(Terminal t) Terminal:blank -> void does
int pos = 0
while pos<(t.width*t.height*2) do
t.baseaddr[pos]=32|byte
t.baseaddr[pos+1]=t.attributes
pos+=2
done
t.update_hook()
return
function(Terminal t) Terminal:bsod -> void does
t.attributes=0x30|byte
t:blank()
t.cursor_x=0
t.cursor_y=0
return
endtype
function() _null_updater -> void does
return