hachem.wtf
aboutsummaryrefslogtreecommitdiff
path: root/kernel/console.rv
blob: 4a036ab6351a60aea65d1899d854589bf6a943f0 (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
const FONT_WIDTH = 8   # glyph width in pixels
const FONT_HEIGHT = 16 # glyph height in pixels (8x16 vga bitmap font)

# base16 from lung
const BASE00 = 0x101010  # background
const BASE03 = 0x505050  # bright black
const BASE05 = 0xd0d0d0  # default foreground
const BASE07 = 0xfafafa  # bright white
const BASE08 = 0xab4642  # red
const BASE09 = 0xdc9656  # orange
const BASE0A = 0xf7ca88  # yellow
const BASE0B = 0xa1b56c  # green
const BASE0C = 0x86c1b9  # cyan
const BASE0D = 0x7cafc2  # blue
const BASE0E = 0xba8baf  # magenta
const FG_COLOR = BASE05
const BG_COLOR = BASE00
const CURSOR_COLOR = BASE0B

global wd = { path = "/" }
global shell_status = { code = 0 }

global prompt = fn() -> string do
  let middle = ""
  unless (shell_status.code == 0) do
    middle = "\x1b[31m#{shell_status.code} "
  end
  return "\x1b[33m#{wd.path} #{middle}\x1b[97m$\x1b[0m "
end

fn parse_int(text) -> num do
  let value = 0
  for index in 0..text:len() do
    const digit = text[index]:ascii()
    if (digit >= 48 and digit <= 57)
      value = value * 10 + (digit - 48)
  end
  return value
end

fn sgr_color(code, current) -> num do
  return match code
  | 0, 39 => BASE05
  | 30 => BASE00
  | 90 => BASE03
  | 31, 91 => BASE08
  | 32, 92 => BASE0B
  | 33, 93 => BASE0A
  | 34, 94 => BASE0D
  | 35, 95 => BASE0E
  | 36, 96 => BASE0C
  | 37 => BASE05
  | 97 => BASE07
  | _ => current
end

# runtime font bs
fn embolden(byte, row) -> num do
  return (byte bor (byte shl 1)) band 0xFF
end

fn slant(byte, row) -> num do
  return (byte shl ((FONT_HEIGHT - 1 - row) shr 2)) band 0xFF
end

fn remap_glyphs(transform) -> table do
  let out = {}
  for glyph_index in 0..FONT:len() do
    const glyph = FONT[glyph_index]
    let rows = {}
    for row_index in 0..FONT_HEIGHT do rows:push(transform(glyph[row_index], row_index)) end
    out:push(rows)
  end
  return out
end

global reverse8 = fn(byte) -> num do
  let out = 0
  for bit in 0..8 do
    if (byte shr bit) band 1 == 1 do
      out = out bor (1 shl (7 - bit))
    end
  end
  return out
end

global fonts = {}

global add_font = fn(name, glyphs, width, height, kind, path, size) do
  for index in 0..fonts:len() do
    if fonts[index].name == name do
      fonts[index].glyphs = glyphs
      fonts[index].width = width
      fonts[index].height = height
      fonts[index].kind = kind
      fonts[index].path = path
      fonts[index].size = size
      return :nil
    end
  end

  fonts:push({ name = name, glyphs = glyphs, width = width, height = height, kind = kind, path = path, size = size })
end

global find_font = fn(name) -> table | :nil do
  for index in 0..fonts:len() do
    if (fonts[index].name == name)
      return fonts[index]
  end
  return :nil
end

add_font("vga", FONT, FONT_WIDTH, FONT_HEIGHT, :bitmap, :nil, 0)
add_font("bold", remap_glyphs(embolden), FONT_WIDTH, FONT_HEIGHT, :bitmap, :nil, 0)
add_font("italic", remap_glyphs(slant), FONT_WIDTH, FONT_HEIGHT, :bitmap, :nil, 0)
add_font("bolditalic", remap_glyphs(fn(byte, row) do return slant(embolden(byte, row), row) end), FONT_WIDTH, FONT_HEIGHT, :bitmap, :nil, 0)

global console = {
  column = 0,
  row = 0,
  scale = 1,

  shown = :false,
  cursor_x = 0,
  cursor_y = 0,
  blink = 0,

  font = FONT,
  font_width = FONT_WIDTH,
  font_height = FONT_HEIGHT,
  font_entry = find_font("vga"),

  fg = FG_COLOR,
  ansi = 0,
  ansi_param = "",

  # this is now font dependent
  fn cell_width(self) self.font_width * self.scale,
  fn cell_height(self) self.font_height * self.scale,
  fn columns(self) fb_width() / self:cell_width(),
  fn rows(self) fb_height() / self:cell_height(),

  fn set_font(self, entry) do
    self.font = entry.glyphs
    self.font_width = entry.width
    self.font_height = entry.height
    self.font_entry = entry

    # a ttf renders at its true pixel size so we js bitmap zoom scale
    if (entry.kind == :ttf)
      self.scale = 1
    self:clear()
  end,

  fn draw_glyph(self, column, row, character) do
    const origin_x = column * self:cell_width()
    const origin_y = row * self:cell_height()
    fill_rect(origin_x, origin_y, self:cell_width(), self:cell_height(), BG_COLOR) # clear the cell

    const code = character:ascii()
    if (code < 32)
      return :nil
    if (code > 127)
      return :nil

    const glyph = self.font[code - 32]
    for glyph_row in 0..self.font_height do
      const bits = glyph[glyph_row]
      for glyph_col in 0..self.font_width do
        if (bits shr glyph_col) band 1 == 1 do
          fill_rect(origin_x + glyph_col * self.scale, origin_y + glyph_row * self.scale, self.scale, self.scale, self.fg)
        end
      end
    end
  end,

  fn newline(self) do
    self.column = 0
    self.row += 1

    if self.row >= self:rows() do
      fb_scroll(self:cell_height(), BG_COLOR)
      self.row = self:rows() - 1
    end
  end,

  # we dont do shift/cntrl, thats a job for future me lmao
  # NOTE: future me kinda delt with it, i was mad drunk
  fn apply_sgr(self, param) do
    if param:len() == 0 do
      self.fg = BASE05
      return :nil
    end

    const parts = param:split(";")
    for index in 0..parts:len() do self.fg = sgr_color(parse_int(parts[index]), self.fg) end
  end,

  fn putc(self, character) do
    const code = character:ascii()
    if self.ansi == 1 do
      if code == 91 do
        self.ansi = 2
        self.ansi_param = ""
      end else do
        self.ansi = 0
      end

      return :nil
    end

    if self.ansi == 2 do
      if (code >= 48 and code <= 57) or code == 59 do
        self.ansi_param = self.ansi_param ~ character
        return :nil
      end

      if (code == 109)
        self:apply_sgr(self.ansi_param)
      self.ansi = 0

      return :nil
    end

    if code == 27 do # ESC
      self.ansi = 1
      return :nil
    end
    # \n
    if code == 10 do # \n
      self:newline()
      return :nil
    end
    if code == 13 do # \r
      self.column = 0
      return :nil
    end
    if code == 8 do # backspace
      if (self.column > 0)
        self.column -= 1
      return :nil
    end
    if (code < 32)
      return :nil

    self:draw_glyph(self.column, self.row, character)
    self.column += 1

    if (self.column >= self:columns())
      self:newline()
  end,

  fn clear(self) do
    fill_rect(0, 0, fb_width(), fb_height(), BG_COLOR)
    self.column = 0
    self.row = 0
  end,

  fn cursor_clear(self) do
    if self.shown do
      const cw = self:cell_width()
      const ch = self:cell_height()
      const thick = 2 * self.scale
      fill_rect(self.cursor_x * cw, self.cursor_y * ch + (ch - thick), cw, thick, BG_COLOR)
      self.shown = :false
    end
  end,

  fn cursor_show(self) do
    self.cursor_x = self.column
    self.cursor_y = self.row
    const cw = self:cell_width()
    const ch = self:cell_height()
    const thick = 2 * self.scale
    fill_rect(self.cursor_x * cw, self.cursor_y * ch + (ch - thick), cw, thick, CURSOR_COLOR)
    self.shown = :true
  end,

  # render every byte the serial mirror has buffered
  fn drain(self) do
    let byte = serial_next()
    while (byte >= 0) do
      self:putc(string.of_ascii(byte))
      byte = serial_next()
    end
  end,
}

global puts = fn(text) serial_puts(text)

global fail = fn(message) do
  puts(message)
  shell_status.code = 1
end

global flush_console = fn() do
  console:cursor_clear()
  console:drain()
  console.blink = 0
  console:cursor_show()
end

global on_tick = fn(ticks) do
  let byte = serial_next()

  if byte >= 0 do
    console:cursor_clear()
    console:putc(string.of_ascii(byte))
    console:drain()
    console.blink = 0
    console:cursor_show()
    return :nil
  end

  if proc_exited() do
    console:cursor_clear()
    puts(prompt())
    console:drain()
    console.blink = 0
    console:cursor_show()
    return :nil
  end

  console.blink += 1
  if console.blink >= 50 do
    console.blink = 0
    if (console.shown) console:cursor_clear() else console:cursor_show()
  end
end