hachem.wtf
aboutsummaryrefslogtreecommitdiff
path: root/kernel/console.rv
blob: a8941410373c595c98de292a5700c705b71b0ddc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
const FW = 8            # glyph width in pixels
const FH = 16           # glyph height in pixels (8x16 VGA bitmap font)
const FG = 0xd0d0d0
const CON_BG = 0x101018

global con = { col = 0, row = 0, scale = 1 }

# cell size in pixels at the current zoom. named cell_* (not cw/ch) so they don't
# collide with draw_glyph's `ch` character parameter.
fn cell_w() FW * con.scale
fn cell_h() FH * con.scale
fn con_cols() fb_width() / cell_w()
fn con_rows() fb_height() / cell_h()

fn draw_glyph(col, row, ch) do
  const x0 = col * cell_w()
  const y0 = row * cell_h()
  fill_rect(x0, y0, cell_w(), cell_h(), CON_BG) # clear the cell
  const code = ch:ascii()
  if code < 32 return :nil
  if code > 127 return :nil
  const g = FONT[code - 32]
  for fy in 0..FH do
    const bits = g[fy]
    for fx in 0..FW do
      if (bits shr fx) band 1 == 1 fill_rect(x0 + fx * con.scale, y0 + fy * con.scale, con.scale, con.scale, FG)
    end
  end
end

fn con_newline() do
  con.col = 0
  con.row += 1
  if con.row >= con_rows() do
    fb_scroll(cell_h(), CON_BG)
    con.row = con_rows() - 1
  end
end

# we dont do Shift/Cntrl
# thats a job for future me lmao
fn con_putc(ch) do
  const code = ch:ascii()
  if code == 10 do con_newline() return :nil end # \n
  if code == 13 do con.col = 0 return :nil end   # \r
  if code == 8 do                                # backspace: move left
    if con.col > 0 con.col -= 1
    return :nil
  end
  if code < 32 return :nil
  draw_glyph(con.col, con.row, ch)
  con.col += 1
  if con.col >= con_cols() con_newline()
end

fn puts(s) serial_puts(s)

const CUR = 0x50fa7b
fn cursor_paint(color) fill_rect(con.col * cell_w(), con.row * cell_h(), cell_w(), cell_h(), color)

global flush_console = fn() do
  cursor_paint(CON_BG)
  let c = serial_next()
  while c >= 0 do
    con_putc(string.of_ascii(c))
    c = serial_next()
  end
  cursor_paint(CUR)
end