summaryrefslogtreecommitdiff
path: root/src/kernel/vga.c
blob: 75064167e2e231c090ac56dd9b3a3c3515c5953c (plain) (blame)
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
#include <stdbool.h>
#include <stdint.h>

#define VGA_COLUMNS 80
#define VGA_LINES 25
#define VGA_START (uint16_t *)0x000b8000
#define VGA_END (VGA_START + VGA_COLUMNS * VGA_LINES)
static uint16_t *cursor = VGA_START;
static uint16_t mask;

void vga_set_color(uint8_t new_color) {
  mask = new_color << 8;
}

void vga_scroll() {
  for (uint32_t *i = (uint32_t *)VGA_START; i < (uint32_t *)(VGA_END - VGA_COLUMNS); ++i)
    *i = *(i + VGA_COLUMNS / 2);
  uint32_t f = (mask | (uint8_t)' ') * 0x00010001;
  for (uint32_t *i = (uint32_t *)(VGA_END - VGA_COLUMNS); i < (uint32_t *)VGA_END; ++i)
    *i++ = f;
  cursor -= VGA_COLUMNS;
}

void vga_blank() {
  uint32_t f = (mask | (uint8_t)' ') * 0x00010001;
  uint32_t *p = (uint32_t *)VGA_START;
  while (p < (uint32_t *)VGA_END)
    *p++ = f;
  cursor = VGA_START;
}

void vga_printch(char ch) {
  if (ch == '\n') {
    if ((cursor = cursor - (cursor - VGA_START) % VGA_COLUMNS + VGA_COLUMNS) == VGA_END)
      vga_scroll();
    return;
  }
  *cursor++ = mask | (uint8_t)ch;
  if (cursor == VGA_END)
    vga_scroll();
}